set 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #ifndef __GBLIBCPP_SET__
  2. #define __GBLIBCPP_SET__
  3. #include <bits/iter_ops>
  4. #include <bits/rbtree>
  5. #include <functional>
  6. #include <initializer_list>
  7. #include <memory>
  8. #include <cstddef>
  9. namespace std {
  10. template <typename Key,
  11. typename Compare = std::less<Key>,
  12. typename Allocator = std::allocator<Key>>
  13. class set {
  14. private:
  15. using rbtree_type = impl::rbtree<Key, Compare, Allocator>;
  16. using node_allocator = typename rbtree_type::node_allocator;
  17. private:
  18. rbtree_type tree;
  19. public:
  20. using key_type = Key;
  21. using value_type = Key;
  22. using size_type = std::size_t;
  23. using allocator_type = Allocator;
  24. using iterator = typename rbtree_type::iterator;
  25. using const_iterator = typename rbtree_type::const_iterator;
  26. public:
  27. __GBLIBCPP_CONSTEXPR
  28. iterator end(void) noexcept { return tree.end(); }
  29. __GBLIBCPP_CONSTEXPR
  30. const_iterator end(void) const noexcept { return tree.cend(); }
  31. __GBLIBCPP_CONSTEXPR
  32. const_iterator cend(void) const noexcept { return tree.cend(); }
  33. __GBLIBCPP_CONSTEXPR
  34. iterator begin(void) noexcept { return tree.begin(); }
  35. __GBLIBCPP_CONSTEXPR
  36. const_iterator begin(void) const noexcept { return tree.cbegin(); }
  37. __GBLIBCPP_CONSTEXPR
  38. const_iterator cbegin(void) const noexcept { return tree.cbegin(); }
  39. explicit __GBLIBCPP_CONSTEXPR
  40. set(const Compare& comp,
  41. const Allocator& alloc = Allocator())
  42. : tree(comp, alloc) {}
  43. explicit __GBLIBCPP_CONSTEXPR
  44. set(const Allocator& alloc)
  45. : set(Compare(), alloc) {}
  46. __GBLIBCPP_CONSTEXPR
  47. set() : set(Compare()) {}
  48. template <typename InputIter>
  49. __GBLIBCPP_CONSTEXPR
  50. set(InputIter first, InputIter last,
  51. const Compare& comp = Compare(),
  52. const Allocator& alloc = Allocator())
  53. : set(comp, alloc)
  54. {
  55. insert(first, last);
  56. }
  57. template <typename InputIter>
  58. __GBLIBCPP_CONSTEXPR
  59. set(InputIter first, InputIter last,
  60. const Allocator& alloc = Allocator())
  61. : set(first, last, Compare(), alloc) {}
  62. __GBLIBCPP_CONSTEXPR
  63. set(const set& other) : tree(other) {}
  64. __GBLIBCPP_CONSTEXPR
  65. set(const set& other, const Allocator& alloc)
  66. : tree(other, alloc) { }
  67. __GBLIBCPP_CONSTEXPR
  68. set(set&& other) : tree(std::move(other.tree)) {}
  69. __GBLIBCPP_CONSTEXPR
  70. set(set&& other, const Allocator& alloc)
  71. : tree(std::move(other.tree), alloc) {}
  72. __GBLIBCPP_CONSTEXPR
  73. set(std::initializer_list<Key> ilist,
  74. const Compare& comp = Compare(),
  75. const Allocator& alloc = Allocator())
  76. : set(comp, alloc)
  77. { insert(ilist.begin(), ilist.end()); }
  78. __GBLIBCPP_CONSTEXPR
  79. set(std::initializer_list<Key> ilist,
  80. const Allocator& alloc)
  81. : set(ilist, Compare(), alloc) {}
  82. __GBLIBCPP_CONSTEXPR
  83. ~set() { clear(); }
  84. __GBLIBCPP_CONSTEXPR
  85. set& operator=(const set& other) = default;
  86. __GBLIBCPP_CONSTEXPR
  87. set& operator=(set&& other) = default;
  88. __GBLIBCPP_CONSTEXPR
  89. set& operator=(std::initializer_list<Key> ilist)
  90. {
  91. clear();
  92. insert(ilist.begin(), ilist.end());
  93. return *this;
  94. }
  95. __GBLIBCPP_CONSTEXPR
  96. iterator find(const Key& key) { return tree.find(key); }
  97. __GBLIBCPP_CONSTEXPR
  98. const_iterator find(const Key& key) const { return tree.find(key); }
  99. __GBLIBCPP_CONSTEXPR
  100. std::pair<iterator, bool> insert(const value_type& value)
  101. { return tree.insert(value); }
  102. __GBLIBCPP_CONSTEXPR
  103. std::pair<iterator, bool> insert(value_type&& value)
  104. { return tree.insert(std::move(value)); }
  105. template <typename InputIter>
  106. __GBLIBCPP_CONSTEXPR
  107. void insert(InputIter first, InputIter last)
  108. {
  109. for ( ; first != last; ++first)
  110. insert(*first);
  111. }
  112. template <typename... Args>
  113. __GBLIBCPP_CONSTEXPR
  114. std::pair<iterator, bool> emplace(Args&&... args)
  115. { return tree.emplace(std::forward<Args>(args)...); }
  116. __GBLIBCPP_CONSTEXPR
  117. iterator erase(iterator pos) noexcept { return tree.erase(pos); }
  118. __GBLIBCPP_CONSTEXPR
  119. iterator erase(const_iterator pos) noexcept { return tree.erase(pos); }
  120. __GBLIBCPP_CONSTEXPR
  121. iterator erase(const_iterator first, const_iterator last) noexcept
  122. {
  123. while (first != last)
  124. first = erase(first);
  125. return first;
  126. }
  127. __GBLIBCPP_CONSTEXPR
  128. size_type erase(const Key& key)
  129. {
  130. auto iter = find(key);
  131. if (!iter)
  132. return 0;
  133. erase(iter);
  134. return 1;
  135. }
  136. __GBLIBCPP_CONSTEXPR
  137. void clear() noexcept { tree.destroy(); }
  138. __GBLIBCPP_CONSTEXPR
  139. bool empty() const noexcept { return tree.empty(); }
  140. __GBLIBCPP_CONSTEXPR
  141. size_type size() const noexcept { return tree.size(); }
  142. __GBLIBCPP_CONSTEXPR
  143. void swap(set& other) { tree.swap(other.tree); }
  144. __GBLIBCPP_CONSTEXPR
  145. size_type count(const Key& key) const
  146. { return find(key) ? 1 : 0; }
  147. __GBLIBCPP_CONSTEXPR
  148. bool contains(const Key& key) const { return count(key) != 0; }
  149. __GBLIBCPP_CONSTEXPR
  150. iterator upper_bound(const Key& key)
  151. { return tree.upper_bound(key); }
  152. __GBLIBCPP_CONSTEXPR
  153. const_iterator upper_bound(const Key& key) const
  154. { return tree.upper_bound(key); }
  155. __GBLIBCPP_CONSTEXPR
  156. iterator lower_bound(const Key& key)
  157. { return tree.lower_bound(key); }
  158. __GBLIBCPP_CONSTEXPR
  159. const_iterator lower_bound(const Key& key) const
  160. { return tree.lower_bound(key); }
  161. };
  162. template <typename Key, typename Compare, typename Allocator>
  163. void swap(std::set<Key, Compare, Allocator>& lhs,
  164. std::set<Key, Compare, Allocator>& rhs)
  165. { lhs.swap(rhs); }
  166. template <typename Key, typename Compare,
  167. typename Allocator, typename Pred>
  168. typename std::set<Key, Compare, Allocator>::size_type
  169. erase_if(std::set<Key, Compare, Allocator>& set, Pred pred)
  170. {
  171. auto iter = set.begin();
  172. typename std::set<Key, Compare, Allocator>::size_type count = 0;
  173. while (iter != set.end()) {
  174. if (pred(*iter)) {
  175. iter = set.erase(iter);
  176. ++count;
  177. } else {
  178. ++iter;
  179. }
  180. }
  181. return count;
  182. }
  183. template <typename Key,
  184. typename Compare = std::less<Key>,
  185. typename Allocator = std::allocator<Key>>
  186. set(std::initializer_list<Key>, Compare = Compare(),
  187. Allocator = Allocator()) -> set<Key, Compare, Allocator>;
  188. template <typename Key, typename Allocator>
  189. set(std::initializer_list<Key>, Allocator)
  190. -> set<Key, std::less<Key>, Allocator>;
  191. } // namespace std
  192. #endif