set 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. std::pair<iterator, bool> emplace(Args&&... args)
  105. { return tree.emplace(std::forward<Args>(args)...); }
  106. __GBLIBCPP_CONSTEXPR
  107. iterator erase(iterator pos) noexcept { return tree.erase(pos); }
  108. __GBLIBCPP_CONSTEXPR
  109. iterator erase(const_iterator pos) noexcept { return tree.erase(pos); }
  110. __GBLIBCPP_CONSTEXPR
  111. iterator erase(const_iterator first, const_iterator last) noexcept
  112. {
  113. while (first != last)
  114. first = erase(first);
  115. return first;
  116. }
  117. __GBLIBCPP_CONSTEXPR
  118. size_type erase(const Key& key)
  119. {
  120. auto iter = find(key);
  121. if (!iter)
  122. return 0;
  123. erase(iter);
  124. return 1;
  125. }
  126. __GBLIBCPP_CONSTEXPR
  127. void clear() noexcept { tree.destroy(); }
  128. __GBLIBCPP_CONSTEXPR
  129. bool empty() const noexcept { return tree.empty(); }
  130. __GBLIBCPP_CONSTEXPR
  131. size_type size() const noexcept { return tree.size(); }
  132. __GBLIBCPP_CONSTEXPR
  133. void swap(set& other) { tree.swap(other.tree); }
  134. __GBLIBCPP_CONSTEXPR
  135. size_type count(const Key& key) const
  136. { return find(key) ? 1 : 0; }
  137. __GBLIBCPP_CONSTEXPR
  138. bool contains(const Key& key) const { return count(key) != 0; }
  139. };
  140. template <typename Key, typename Compare, typename Allocator>
  141. void swap(std::set<Key, Compare, Allocator>& lhs,
  142. std::set<Key, Compare, Allocator>& rhs)
  143. { lhs.swap(rhs); }
  144. } // namespace std
  145. #endif