flat_set.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Copyright (c) 2023, QuantStack and Mamba Contributors
  2. //
  3. // Distributed under the terms of the BSD 3-Clause License.
  4. //
  5. // The full license is in the file LICENSE, distributed with this software.
  6. #ifndef MAMBA_UTILFLAT_SET_HPP
  7. #define MAMBA_UTILFLAT_SET_HPP
  8. #include <algorithm>
  9. #include <utility>
  10. #include <vector>
  11. namespace mamba::util
  12. {
  13. /**
  14. * A sorted vector behaving like a set.
  15. *
  16. * Like, ``std::set``, uniqueness is determined by using the equivalence relation.
  17. * In imprecise terms, two objects ``a`` and ``b`` are considered equivalent if neither
  18. * compares less than the other: ``!comp(a, b) && !comp(b, a)``
  19. *
  20. * @todo C++23 This is implemented in <flat_set>
  21. */
  22. template <typename Key, typename Compare = std::less<Key>, typename Allocator = std::allocator<Key>>
  23. class flat_set : private std::vector<Key, Allocator>
  24. {
  25. public:
  26. using Base = std::vector<Key, Allocator>;
  27. using typename Base::allocator_type;
  28. using typename Base::const_iterator;
  29. using typename Base::const_reverse_iterator;
  30. using typename Base::size_type;
  31. using typename Base::value_type;
  32. using key_compare = Compare;
  33. using value_compare = Compare;
  34. using Base::cbegin;
  35. using Base::cend;
  36. using Base::crbegin;
  37. using Base::crend;
  38. using Base::clear;
  39. using Base::empty;
  40. using Base::reserve;
  41. using Base::size;
  42. flat_set() = default;
  43. flat_set(
  44. std::initializer_list<value_type> il,
  45. key_compare compare = key_compare(),
  46. const allocator_type& alloc = allocator_type()
  47. );
  48. template <typename InputIterator>
  49. flat_set(
  50. InputIterator first,
  51. InputIterator last,
  52. key_compare compare = key_compare(),
  53. const allocator_type& alloc = Allocator()
  54. );
  55. flat_set(const flat_set&) = default;
  56. flat_set(flat_set&&) = default;
  57. explicit flat_set(std::vector<Key, Allocator>&& other, key_compare compare = key_compare());
  58. explicit flat_set(const std::vector<Key, Allocator>& other, key_compare compare = key_compare());
  59. flat_set& operator=(const flat_set&) = default;
  60. flat_set& operator=(flat_set&&) = default;
  61. bool contains(const value_type&) const;
  62. const value_type& front() const noexcept;
  63. const value_type& back() const noexcept;
  64. const_iterator begin() const noexcept;
  65. const_iterator end() const noexcept;
  66. const_reverse_iterator rbegin() const noexcept;
  67. const_reverse_iterator rend() const noexcept;
  68. /** Insert an element in the set.
  69. *
  70. * Like std::vector and unlike std::set, inserting an element invalidates iterators.
  71. */
  72. std::pair<const_iterator, bool> insert(value_type&& value);
  73. std::pair<const_iterator, bool> insert(const value_type& value);
  74. template <typename InputIterator>
  75. void insert(InputIterator first, InputIterator last);
  76. const_iterator erase(const_iterator pos);
  77. const_iterator erase(const_iterator first, const_iterator last);
  78. size_type erase(const value_type& value);
  79. private:
  80. key_compare m_compare;
  81. bool key_eq(const value_type& a, const value_type& b) const;
  82. template <typename U>
  83. std::pair<const_iterator, bool> insert_impl(U&& value);
  84. void sort_and_remove_duplicates();
  85. template <typename K, typename C, typename A>
  86. friend bool operator==(const flat_set<K, C, A>& lhs, const flat_set<K, C, A>& rhs);
  87. };
  88. template <class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key>>
  89. flat_set(std::initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
  90. -> flat_set<Key, Compare, Allocator>;
  91. template <
  92. class InputIt,
  93. class Comp = std::less<typename std::iterator_traits<InputIt>::value_type>,
  94. class Alloc = std::allocator<typename std::iterator_traits<InputIt>::value_type>>
  95. flat_set(InputIt, InputIt, Comp = Comp(), Alloc = Alloc())
  96. -> flat_set<typename std::iterator_traits<InputIt>::value_type, Comp, Alloc>;
  97. template <class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key>>
  98. flat_set(std::vector<Key, Allocator>&&, Compare compare = Compare())
  99. -> flat_set<Key, Compare, Allocator>;
  100. template <class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key>>
  101. flat_set(const std::vector<Key, Allocator>&, Compare compare = Compare())
  102. -> flat_set<Key, Compare, Allocator>;
  103. template <typename Key, typename Compare, typename Allocator>
  104. bool
  105. operator==(const flat_set<Key, Compare, Allocator>& lhs, const flat_set<Key, Compare, Allocator>& rhs);
  106. template <typename Key, typename Compare, typename Allocator>
  107. bool
  108. operator!=(const flat_set<Key, Compare, Allocator>& lhs, const flat_set<Key, Compare, Allocator>& rhs);
  109. /*******************************
  110. * vector_set Implementation *
  111. *******************************/
  112. template <typename K, typename C, typename A>
  113. flat_set<K, C, A>::flat_set(
  114. std::initializer_list<value_type> il,
  115. key_compare compare,
  116. const allocator_type& alloc
  117. )
  118. : Base(std::move(il), alloc)
  119. , m_compare(std::move(compare))
  120. {
  121. sort_and_remove_duplicates();
  122. }
  123. template <typename K, typename C, typename A>
  124. template <typename InputIterator>
  125. flat_set<K, C, A>::flat_set(
  126. InputIterator first,
  127. InputIterator last,
  128. key_compare compare,
  129. const allocator_type& alloc
  130. )
  131. : Base(first, last, alloc)
  132. , m_compare(std::move(compare))
  133. {
  134. sort_and_remove_duplicates();
  135. }
  136. template <typename K, typename C, typename A>
  137. flat_set<K, C, A>::flat_set(std::vector<K, A>&& other, C compare)
  138. : Base(std::move(other))
  139. , m_compare(std::move(compare))
  140. {
  141. sort_and_remove_duplicates();
  142. }
  143. template <typename K, typename C, typename A>
  144. flat_set<K, C, A>::flat_set(const std::vector<K, A>& other, C compare)
  145. : Base(std::move(other))
  146. , m_compare(std::move(compare))
  147. {
  148. sort_and_remove_duplicates();
  149. }
  150. template <typename K, typename C, typename A>
  151. auto flat_set<K, C, A>::contains(const value_type& value) const -> bool
  152. {
  153. return std::binary_search(begin(), end(), value);
  154. }
  155. template <typename K, typename C, typename A>
  156. auto flat_set<K, C, A>::front() const noexcept -> const value_type&
  157. {
  158. return Base::front();
  159. }
  160. template <typename K, typename C, typename A>
  161. auto flat_set<K, C, A>::back() const noexcept -> const value_type&
  162. {
  163. return Base::back();
  164. }
  165. template <typename K, typename C, typename A>
  166. auto flat_set<K, C, A>::begin() const noexcept -> const_iterator
  167. {
  168. return Base::begin();
  169. }
  170. template <typename K, typename C, typename A>
  171. auto flat_set<K, C, A>::end() const noexcept -> const_iterator
  172. {
  173. return Base::end();
  174. }
  175. template <typename K, typename C, typename A>
  176. auto flat_set<K, C, A>::rbegin() const noexcept -> const_reverse_iterator
  177. {
  178. return Base::rbegin();
  179. }
  180. template <typename K, typename C, typename A>
  181. auto flat_set<K, C, A>::rend() const noexcept -> const_reverse_iterator
  182. {
  183. return Base::rend();
  184. }
  185. template <typename K, typename C, typename A>
  186. auto flat_set<K, C, A>::insert(const value_type& value) -> std::pair<const_iterator, bool>
  187. {
  188. return insert_impl(value);
  189. }
  190. template <typename K, typename C, typename A>
  191. auto flat_set<K, C, A>::insert(value_type&& value) -> std::pair<const_iterator, bool>
  192. {
  193. return insert_impl(std::move(value));
  194. }
  195. template <typename K, typename C, typename A>
  196. bool flat_set<K, C, A>::key_eq(const value_type& a, const value_type& b) const
  197. {
  198. return !m_compare(a, b) && !m_compare(b, a);
  199. }
  200. template <typename K, typename C, typename A>
  201. void flat_set<K, C, A>::sort_and_remove_duplicates()
  202. {
  203. std::sort(Base::begin(), Base::end(), m_compare);
  204. auto is_eq = [this](const value_type& a, const value_type& b) { return key_eq(a, b); };
  205. Base::erase(std::unique(Base::begin(), Base::end(), is_eq), Base::end());
  206. }
  207. template <typename K, typename C, typename A>
  208. template <typename InputIterator>
  209. void flat_set<K, C, A>::insert(InputIterator first, InputIterator last)
  210. {
  211. Base::insert(Base::end(), first, last);
  212. sort_and_remove_duplicates();
  213. }
  214. template <typename K, typename C, typename A>
  215. template <typename U>
  216. auto flat_set<K, C, A>::insert_impl(U&& value) -> std::pair<const_iterator, bool>
  217. {
  218. auto it = std::lower_bound(begin(), end(), value, m_compare);
  219. if ((it != end()) && (key_eq(*it, value)))
  220. {
  221. return { it, false };
  222. }
  223. it = Base::insert(it, std::forward<U>(value));
  224. return { it, true };
  225. }
  226. template <typename K, typename C, typename A>
  227. auto flat_set<K, C, A>::erase(const_iterator pos) -> const_iterator
  228. {
  229. // No need to sort or remove duplicates again
  230. return Base::erase(pos);
  231. }
  232. template <typename K, typename C, typename A>
  233. auto flat_set<K, C, A>::erase(const_iterator first, const_iterator last) -> const_iterator
  234. {
  235. // No need to sort or remove duplicates again
  236. return Base::erase(first, last);
  237. }
  238. template <typename K, typename C, typename A>
  239. auto flat_set<K, C, A>::erase(const value_type& value) -> size_type
  240. {
  241. auto it = std::lower_bound(begin(), end(), value, m_compare);
  242. if ((it == end()) || (!(key_eq(*it, value))))
  243. {
  244. return 0;
  245. }
  246. erase(it);
  247. return 1;
  248. }
  249. template <typename K, typename C, typename A>
  250. bool operator==(const flat_set<K, C, A>& lhs, const flat_set<K, C, A>& rhs)
  251. {
  252. auto is_eq = [&lhs](const auto& a, const auto& b) { return lhs.key_eq(a, b); };
  253. return std::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend(), is_eq);
  254. }
  255. template <typename K, typename C, typename A>
  256. bool operator!=(const flat_set<K, C, A>& lhs, const flat_set<K, C, A>& rhs)
  257. {
  258. return !(lhs == rhs);
  259. }
  260. }
  261. #endif