set 5.4 KB

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