compact_set-inl.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #ifndef COMPACT_SET_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include compact_set.h"
  3. // For the sake of sane code completion.
  4. #include "compact_set.h"
  5. #endif
  6. #include <library/cpp/yt/assert/assert.h>
  7. namespace NYT {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. template <typename T, size_t N, typename C, typename A>
  10. class TCompactSet<T, N, C, A>::const_iterator
  11. {
  12. private:
  13. friend class TCompactSet<T, N, C, A>;
  14. union
  15. {
  16. TVectorConstIterator VIter;
  17. TSetConstIterator SIter;
  18. };
  19. bool Small;
  20. const_iterator(TVectorConstIterator it)
  21. : VIter(it)
  22. , Small(true)
  23. { }
  24. const_iterator(TSetConstIterator it)
  25. : SIter(it)
  26. , Small(false)
  27. { }
  28. template <typename TOther>
  29. void ConstructFrom(TOther&& rhs)
  30. {
  31. YT_ASSERT(Small == rhs.Small);
  32. if (Small) {
  33. new (&VIter)TVectorConstIterator(std::forward<TOther>(rhs).VIter);
  34. } else {
  35. new (&SIter)TSetConstIterator(std::forward<TOther>(rhs).SIter);
  36. }
  37. }
  38. template <typename TOther>
  39. const_iterator& AssignFrom(TOther&& rhs)
  40. {
  41. if (this == &rhs) {
  42. return *this;
  43. }
  44. if (Small && rhs.Small) {
  45. VIter = std::forward<TOther>(rhs).VIter;
  46. } else if (!Small && !rhs.Small) {
  47. SIter = std::forward<TOther>(rhs).SIter;
  48. } else {
  49. if (Small) {
  50. VIter.~TVectorConstIterator();
  51. } else {
  52. SIter.~TSetConstIterator();
  53. }
  54. if (rhs.Small) {
  55. new (&VIter)TVectorConstIterator(std::forward<TOther>(rhs).VIter);
  56. } else {
  57. new (&SIter)TSetConstIterator(std::forward<TOther>(rhs).SIter);
  58. }
  59. }
  60. Small = rhs.Small;
  61. return *this;
  62. }
  63. public:
  64. static_assert(std::is_same_v<
  65. typename std::iterator_traits<TVectorConstIterator>::difference_type,
  66. typename std::iterator_traits<TSetConstIterator>::difference_type>);
  67. static_assert(std::is_same_v<
  68. typename std::iterator_traits<TVectorConstIterator>::value_type,
  69. typename std::iterator_traits<TSetConstIterator>::value_type>);
  70. static_assert(std::is_same_v<
  71. typename std::iterator_traits<TVectorConstIterator>::pointer,
  72. typename std::iterator_traits<TSetConstIterator>::pointer>);
  73. static_assert(std::is_same_v<
  74. typename std::iterator_traits<TVectorConstIterator>::reference,
  75. typename std::iterator_traits<TSetConstIterator>::reference>);
  76. using difference_type = typename std::iterator_traits<TVectorConstIterator>::difference_type;
  77. using value_type = typename std::iterator_traits<TVectorConstIterator>::value_type;
  78. using pointer = typename std::iterator_traits<TVectorConstIterator>::pointer;
  79. using reference = typename std::iterator_traits<TVectorConstIterator>::reference;
  80. using iterator_category = std::bidirectional_iterator_tag;
  81. const_iterator(const const_iterator& rhs)
  82. : Small(rhs.Small)
  83. {
  84. ConstructFrom(rhs);
  85. }
  86. const_iterator(const_iterator&& rhs)
  87. : Small(rhs.Small)
  88. {
  89. ConstructFrom(std::move(rhs));
  90. }
  91. ~const_iterator()
  92. {
  93. if (Small) {
  94. VIter.~TVectorConstIterator();
  95. } else {
  96. SIter.~TSetConstIterator();
  97. }
  98. }
  99. const_iterator& operator=(const const_iterator& rhs)
  100. {
  101. return AssignFrom(rhs);
  102. }
  103. const_iterator& operator=(const_iterator&& rhs)
  104. {
  105. return AssignFrom(std::move(rhs));
  106. }
  107. const_iterator& operator++()
  108. {
  109. if (Small) {
  110. ++VIter;
  111. } else {
  112. ++SIter;
  113. }
  114. return *this;
  115. }
  116. const_iterator operator++(int)
  117. {
  118. auto result = *this;
  119. if (Small) {
  120. ++VIter;
  121. } else {
  122. ++SIter;
  123. }
  124. return result;
  125. }
  126. const_iterator& operator--()
  127. {
  128. if (Small) {
  129. --VIter;
  130. } else {
  131. --SIter;
  132. }
  133. return *this;
  134. }
  135. const_iterator operator--(int)
  136. {
  137. auto result = *this;
  138. if (Small) {
  139. --VIter;
  140. } else {
  141. --SIter;
  142. }
  143. return result;
  144. }
  145. bool operator==(const const_iterator& rhs) const
  146. {
  147. if (Small != rhs.Small) {
  148. return false;
  149. }
  150. return Small ? (VIter == rhs.VIter) : (SIter == rhs.SIter);
  151. }
  152. bool operator!=(const const_iterator& rhs) const
  153. {
  154. return !(*this == rhs);
  155. }
  156. const T& operator*() const
  157. {
  158. return Small ? *VIter : *SIter;
  159. }
  160. const T* operator->() const
  161. {
  162. return &operator*();
  163. }
  164. };
  165. ////////////////////////////////////////////////////////////////////////////////
  166. template <typename T, size_t N, typename C, typename A>
  167. TCompactSet<T, N, C, A>::TCompactSet(const A& allocator)
  168. : Set_(allocator)
  169. { }
  170. template <typename T, size_t N, typename C, typename A>
  171. bool TCompactSet<T, N, C, A>::empty() const
  172. {
  173. return Vector_.empty() && Set_.empty();
  174. }
  175. template <typename T, size_t N, typename C, typename A>
  176. typename TCompactSet<T, N, C, A>::size_type TCompactSet<T, N, C, A>::size() const
  177. {
  178. return is_small() ? Vector_.size() : Set_.size();
  179. }
  180. template <typename T, size_t N, typename C, typename A>
  181. const T& TCompactSet<T, N, C, A>::front() const
  182. {
  183. return is_small() ? Vector_.front() : *Set_.begin();
  184. }
  185. template <typename T, size_t N, typename C, typename A>
  186. typename TCompactSet<T, N, C, A>::size_type TCompactSet<T, N, C, A>::count(const T& v) const
  187. {
  188. if (is_small()) {
  189. return std::binary_search(Vector_.begin(), Vector_.end(), v, C()) ? 1 : 0;
  190. } else {
  191. return Set_.count(v);
  192. }
  193. }
  194. template <typename T, size_t N, typename C, typename A>
  195. bool TCompactSet<T, N, C, A>::contains(const T& v) const
  196. {
  197. return count(v) == 1;
  198. }
  199. template <typename T, size_t N, typename C, typename A>
  200. std::pair<typename TCompactSet<T, N, C, A>::const_iterator, bool> TCompactSet<T, N, C, A>::insert(const T& v)
  201. {
  202. if (!is_small()) {
  203. auto [it, inserted] = Set_.insert(v);
  204. return {const_iterator(std::move(it)), inserted};
  205. }
  206. auto it = std::lower_bound(Vector_.begin(), Vector_.end(), v, C());
  207. if (it != Vector_.end() && !C()(v, *it)) {
  208. return {const_iterator(std::move(it)), false}; // Don't reinsert if it already exists.
  209. }
  210. if (Vector_.size() < N) {
  211. auto newIt = Vector_.insert(it, v);
  212. return {const_iterator(std::move(newIt)), true};
  213. }
  214. Set_.insert(Vector_.begin(), Vector_.end());
  215. Vector_.clear();
  216. auto [newIt, inserted] = Set_.insert(v);
  217. YT_ASSERT(inserted);
  218. return {const_iterator(std::move(newIt)), true};
  219. }
  220. template <typename T, size_t N, typename C, typename A>
  221. template <typename TIter>
  222. void TCompactSet<T, N, C, A>::insert(TIter i, TIter e)
  223. {
  224. for (; i != e; ++i) {
  225. insert(*i);
  226. }
  227. }
  228. template <typename T, size_t N, typename C, typename A>
  229. bool TCompactSet<T, N, C, A>::erase(const T& v)
  230. {
  231. if (!is_small()) {
  232. return Set_.erase(v);
  233. }
  234. auto [rangeBegin, rangeEnd] = std::equal_range(Vector_.begin(), Vector_.end(), v, C());
  235. if (rangeBegin != rangeEnd) {
  236. Vector_.erase(rangeBegin, rangeEnd);
  237. return true;
  238. } else {
  239. return false;
  240. }
  241. }
  242. template <typename T, size_t N, typename C, typename A>
  243. void TCompactSet<T, N, C, A>::clear()
  244. {
  245. Vector_.clear();
  246. Set_.clear();
  247. }
  248. template <typename T, size_t N, typename C, typename A>
  249. typename TCompactSet<T, N, C, A>::const_iterator TCompactSet<T, N, C, A>::begin() const
  250. {
  251. return is_small() ? const_iterator(Vector_.begin()) : const_iterator(Set_.begin());
  252. }
  253. template <typename T, size_t N, typename C, typename A>
  254. typename TCompactSet<T, N, C, A>::const_iterator TCompactSet<T, N, C, A>::cbegin() const
  255. {
  256. return begin();
  257. }
  258. template <typename T, size_t N, typename C, typename A>
  259. typename TCompactSet<T, N, C, A>::const_iterator TCompactSet<T, N, C, A>::end() const
  260. {
  261. return is_small() ? const_iterator(Vector_.end()) : const_iterator(Set_.end());
  262. }
  263. template <typename T, size_t N, typename C, typename A>
  264. typename TCompactSet<T, N, C, A>::const_iterator TCompactSet<T, N, C, A>::cend() const
  265. {
  266. return end();
  267. }
  268. template <typename T, size_t N, typename C, typename A>
  269. bool TCompactSet<T, N, C, A>::is_small() const
  270. {
  271. return Set_.empty();
  272. }
  273. ////////////////////////////////////////////////////////////////////////////////
  274. } // namespace NYT