NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
rb_tree.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_CONTAINER_RB_TREE_HPP__
2#define NEFORCE_CORE_CONTAINER_RB_TREE_HPP__
3
12
20NEFORCE_BEGIN_NAMESPACE__
21
74
76NEFORCE_INLINE17 constexpr bool RB_TREE_RED = false;
77
79NEFORCE_INLINE17 constexpr bool RB_TREE_BLACK = true;
80
81
90 using color_type = bool;
92
94 base_ptr parent_ = nullptr;
95 base_ptr left_ = nullptr;
96 base_ptr right_ = nullptr;
97
103 static base_ptr minimum(base_ptr root) noexcept {
104 if (root == nullptr) {
105 return nullptr;
106 }
107 while (root->left_ != nullptr) {
108 root = root->left_;
109 }
110 return root;
111 }
112
118 static base_ptr maximum(base_ptr root) noexcept {
119 if (root == nullptr) {
120 return nullptr;
121 }
122 while (root->right_ != nullptr) {
123 root = root->right_;
124 }
125 return root;
126 }
127};
128
129
137NEFORCE_ALWAYS_INLINE_INLINE void rb_tree_rotate_left(rb_tree_node_base* axis, rb_tree_node_base*& root) noexcept {
138 rb_tree_node_base* y = axis->right_;
139 axis->right_ = y->left_;
140 if (y->left_ != nullptr) {
141 y->left_->parent_ = axis;
142 }
143
144 y->parent_ = axis->parent_;
145
146 if (axis == root) {
147 root = y;
148 } else if (axis == axis->parent_->left_) {
149 axis->parent_->left_ = y;
150 } else {
151 axis->parent_->right_ = y;
152 }
153
154 y->left_ = axis;
155 axis->parent_ = y;
156}
157
165NEFORCE_ALWAYS_INLINE_INLINE void rb_tree_rotate_right(rb_tree_node_base* axis, rb_tree_node_base*& root) noexcept {
166 rb_tree_node_base* y = axis->left_;
167 axis->left_ = y->right_;
168 if (y->right_ != nullptr) {
169 y->right_->parent_ = axis;
170 }
171 y->parent_ = axis->parent_;
172
173 if (axis == root) {
174 root = y;
175 } else if (axis == axis->parent_->right_) {
176 axis->parent_->right_ = y;
177 } else {
178 axis->parent_->left_ = y;
179 }
180
181 y->right_ = axis;
182 axis->parent_ = y;
183}
184
192NEFORCE_ALWAYS_INLINE_INLINE void rb_tree_insert_rebalance(rb_tree_node_base* insert,
193 rb_tree_node_base*& root) noexcept {
194 insert->color_ = RB_TREE_RED;
195
196 while (insert != root && insert->parent_->color_ == RB_TREE_RED) {
197 if (insert->parent_ == insert->parent_->parent_->left_) {
199
200 if (y != nullptr && y->color_ == RB_TREE_RED) {
201 insert->parent_->color_ = RB_TREE_BLACK;
203 insert->parent_->parent_->color_ = RB_TREE_RED;
204 insert = insert->parent_->parent_;
205 } else {
206 if (insert == insert->parent_->right_) {
207 insert = insert->parent_;
208 _NEFORCE rb_tree_rotate_left(insert, root);
209 }
210 insert->parent_->color_ = RB_TREE_BLACK;
211 insert->parent_->parent_->color_ = RB_TREE_RED;
212 _NEFORCE rb_tree_rotate_right(insert->parent_->parent_, root);
213 }
214 } else {
215 rb_tree_node_base* y = insert->parent_->parent_->left_;
216 if (y != nullptr && y->color_ == RB_TREE_RED) {
217 insert->parent_->color_ = RB_TREE_BLACK;
219 insert->parent_->parent_->color_ = RB_TREE_RED;
220 insert = insert->parent_->parent_;
221 } else {
222 if (insert == insert->parent_->left_) {
223 insert = insert->parent_;
224 _NEFORCE rb_tree_rotate_right(insert, root);
225 }
226 insert->parent_->color_ = RB_TREE_BLACK;
227 insert->parent_->parent_->color_ = RB_TREE_RED;
228 _NEFORCE rb_tree_rotate_left(insert->parent_->parent_, root);
229 }
230 }
231 }
232 root->color_ = RB_TREE_BLACK;
233}
234
246 rb_tree_node_base*& root,
247 rb_tree_node_base*& leftmost,
248 rb_tree_node_base*& rightmost) noexcept {
250 rb_tree_node_base* x = nullptr;
251 rb_tree_node_base* x_parent = nullptr;
252
253 if (y->left_ == nullptr) {
254 x = y->right_;
255 } else {
256 if (y->right_ == nullptr) {
257 x = y->left_;
258 } else {
259 y = y->right_;
260 while (y->left_ != nullptr) {
261 y = y->left_;
262 }
263 x = y->right_;
264 }
265 }
266
267 if (y != erase) {
268 erase->left_->parent_ = y;
269 y->left_ = erase->left_;
270
271 if (y != erase->right_) {
272 x_parent = y->parent_;
273 if (x != nullptr) {
274 x->parent_ = y->parent_;
275 }
276
277 y->parent_->left_ = x;
278 y->right_ = erase->right_;
279 erase->right_->parent_ = y;
280 } else {
281 x_parent = y;
282 }
283
284 if (root == erase) {
285 root = y;
286 } else if (erase->parent_->left_ == erase) {
287 erase->parent_->left_ = y;
288 } else {
289 erase->parent_->right_ = y;
290 }
291
292 y->parent_ = erase->parent_;
293 _NEFORCE swap(y->color_, erase->color_);
294 y = erase;
295 } else {
296 x_parent = y->parent_;
297 if (x != nullptr) {
298 x->parent_ = y->parent_;
299 }
300
301 if (root == erase) {
302 root = x;
303 } else {
304 if (erase->parent_->left_ == erase) {
305 erase->parent_->left_ = x;
306 } else {
307 erase->parent_->right_ = x;
308 }
309 }
310
311 if (leftmost == erase) {
312 if (erase->right_ == nullptr) {
313 leftmost = erase->parent_;
314 } else {
315 leftmost = rb_tree_node_base::minimum(x);
316 }
317 }
318
319 if (rightmost == erase) {
320 if (erase->left_ == nullptr) {
321 rightmost = erase->parent_;
322 } else {
323 rightmost = rb_tree_node_base::maximum(x);
324 }
325 }
326 }
327
328 if (y->color_ != RB_TREE_RED) {
329 while (x != root && (x == nullptr || x->color_ == RB_TREE_BLACK)) {
330 if (x == x_parent->left_) {
331 rb_tree_node_base* w = x_parent->right_;
332 NEFORCE_DEBUG_VERIFY(w != nullptr, "RB-tree structure corrupted: right sibling is null");
333 if (w == nullptr) {
334 x = x_parent;
335 x_parent = x_parent->parent_;
336 continue;
337 }
338
339 if (w->color_ == RB_TREE_RED) {
341 x_parent->color_ = RB_TREE_RED;
342 _NEFORCE rb_tree_rotate_left(x_parent, root);
343 w = x_parent->right_;
344 }
345
346 if ((w->left_ == nullptr || w->left_->color_ == RB_TREE_BLACK) &&
347 (w->right_ == nullptr || w->right_->color_ == RB_TREE_BLACK)) {
348 w->color_ = RB_TREE_RED;
349 x = x_parent;
350 x_parent = x_parent->parent_;
351 } else {
352 if (w->right_ == nullptr || w->right_->color_ == RB_TREE_BLACK) {
353 if (w->left_ != nullptr) {
355 }
356 w->color_ = RB_TREE_RED;
357 _NEFORCE rb_tree_rotate_right(w, root);
358 w = x_parent->right_;
359 }
360 w->color_ = x_parent->color_;
361 x_parent->color_ = RB_TREE_BLACK;
362 if (w->right_ != nullptr) {
364 }
365 _NEFORCE rb_tree_rotate_left(x_parent, root);
366 break;
367 }
368 } else {
369 rb_tree_node_base* w = x_parent->left_;
370 NEFORCE_DEBUG_VERIFY(w != nullptr, "RB-tree structure corrupted: left sibling is null");
371 if (w == nullptr) {
372 x = x_parent;
373 x_parent = x_parent->parent_;
374 continue;
375 }
376
377 if (w->color_ == RB_TREE_RED) {
379 x_parent->color_ = RB_TREE_RED;
380 _NEFORCE rb_tree_rotate_right(x_parent, root);
381 w = x_parent->left_;
382 }
383
384 if ((w->right_ == nullptr || w->right_->color_ == RB_TREE_BLACK) &&
385 (w->left_ == nullptr || w->left_->color_ == RB_TREE_BLACK)) {
386 w->color_ = RB_TREE_RED;
387 x = x_parent;
388 x_parent = x_parent->parent_;
389 } else {
390 if (w->left_ == nullptr || w->left_->color_ == RB_TREE_BLACK) {
391 if (w->right_ != nullptr) {
393 }
394
395 w->color_ = RB_TREE_RED;
396 _NEFORCE rb_tree_rotate_left(w, root);
397 w = x_parent->left_;
398 }
399 w->color_ = x_parent->color_;
400 x_parent->color_ = RB_TREE_BLACK;
401 if (w->left_ != nullptr) {
403 }
404
405 _NEFORCE rb_tree_rotate_right(x_parent, root);
406 break;
407 }
408 }
409 }
410
411 if (x != nullptr) {
413 }
414 }
415 return y;
416}
417
418
426template <typename T>
436
437
445public:
447
448protected:
450
451 base_ptr node_ = nullptr;
452
456 void increment() noexcept {
457 if (node_->right_ != nullptr) {
458 node_ = node_->right_;
459 while (node_->left_ != nullptr) {
460 node_ = node_->left_;
461 }
462 } else {
463 base_ptr y = node_->parent_;
464 while (node_ == y->right_) {
465 node_ = y;
466 y = y->parent_;
467 }
468 if (node_->right_ != y) {
469 node_ = y;
470 }
471 }
472 }
473
477 void decrement() noexcept {
478 if (node_->color_ == RB_TREE_RED && node_->parent_ != nullptr && node_->parent_->parent_ == node_) {
479 node_ = node_->right_;
480 } else if (node_->left_ != nullptr) {
481 base_ptr y = node_->left_;
482 while (y->right_ != nullptr) {
483 y = y->right_;
484 }
485 node_ = y;
486 } else {
487 base_ptr y = node_->parent_;
488 if (y == nullptr) {
489 return;
490 }
491 while (node_ == y->left_) {
492 node_ = y;
493 y = y->parent_;
494 }
495 node_ = y;
496 }
497 }
498};
499
500
509template <bool IsConst, typename RbTree>
510struct rb_tree_iterator : rb_tree_base_iterator, iiterator<rb_tree_iterator<IsConst, RbTree>> {
511public:
512 using container_type = RbTree;
513 using value_type = typename container_type::value_type;
514 using size_type = typename container_type::size_type;
515 using difference_type = typename container_type::difference_type;
517 using reference = conditional_t<IsConst, typename container_type::const_reference,
518 typename container_type::reference>;
519 using pointer = conditional_t<IsConst, typename container_type::const_pointer,
520 typename container_type::pointer>;
521
522private:
523 using base_type = rb_tree_base_iterator;
524 using node_type = rb_tree_node<value_type>;
525 using link_type = node_type*;
526
527 const container_type* container_ = nullptr;
528
529 template <typename, typename, typename, typename, typename>
530 friend class rb_tree;
531
532public:
533 rb_tree_iterator() noexcept = default;
534 ~rb_tree_iterator() = default;
535
536 rb_tree_iterator(const rb_tree_iterator&) noexcept = default;
537 rb_tree_iterator& operator=(const rb_tree_iterator&) noexcept = default;
538 rb_tree_iterator(rb_tree_iterator&&) noexcept = default;
539 rb_tree_iterator& operator=(rb_tree_iterator&&) noexcept = default;
540
546 rb_tree_iterator(node_type* ptr, const container_type* tree) noexcept :
547 container_(tree) {
548 node_ = ptr;
549 }
550
555 NEFORCE_NODISCARD reference dereference() const noexcept {
556 NEFORCE_DEBUG_VERIFY(node_ && container_, "Attempting to dereference on a null pointer");
557 link_type link = link_type(node_);
558 NEFORCE_DEBUG_VERIFY(node_ != container_->header_ && node_->parent_ != nullptr,
559 "Attempting to dereference out of boundary");
560 return link->data;
561 }
562
566 NEFORCE_CONSTEXPR20 void increment() noexcept {
567 NEFORCE_DEBUG_VERIFY(node_ && container_, "Attempting to increment a null pointer");
568 NEFORCE_DEBUG_VERIFY(link_type(node_) != container_->header_, "Attempting to increment out of boundary");
570 }
571
575 NEFORCE_CONSTEXPR20 void decrement() noexcept {
576 NEFORCE_DEBUG_VERIFY(node_ && container_, "Attempting to decrement a null pointer");
577 NEFORCE_DEBUG_VERIFY(!container_->empty() || node_ != container_->header_,
578 "Attempting to decrement on empty container");
580 }
581
587 NEFORCE_NODISCARD bool equal_to(const rb_tree_iterator& rhs) const noexcept {
588 NEFORCE_DEBUG_VERIFY(container_ == rhs.container_, "Attempting to equal to a different container");
589 return node_ == rhs.node_;
590 }
591
596 NEFORCE_NODISCARD pointer base() const noexcept { return node_; }
597
602 NEFORCE_NODISCARD const container_type* container() const noexcept { return container_; }
603};
604
605
617template <typename Key, typename Value, typename KeyOfValue, typename Compare,
618 typename Alloc = allocator<rb_tree_node<Value>>>
619class rb_tree : icollector<rb_tree<Key, Value, KeyOfValue, Compare, Alloc>> {
620 static_assert(is_allocator_v<Alloc>, "Alloc type is not a standard allocator type.");
621 static_assert(is_same_v<rb_tree_node<Value>, typename Alloc::value_type>, "allocator type mismatch.");
622 static_assert(is_object_v<Value>, "list only contains object types.");
623
624public:
625 using key_type = Key;
626 using color_type = bool;
627
628 using value_type = Value;
629 using pointer = Value*;
630 using reference = Value&;
631 using const_pointer = const Value*;
632 using const_reference = const Value&;
635 using iterator = rb_tree_iterator<false, rb_tree>;
636 using const_iterator = rb_tree_iterator<true, rb_tree>;
639 using allocator_type = Alloc;
640
641private:
642 using base_node = rb_tree_node_base;
643 using link_node = rb_tree_node<Value>;
644 using base_ptr = base_node*;
645 using link_type = link_node*;
646
647 link_type header_ = nullptr;
648 Compare key_compare_{};
649 KeyOfValue extracter_{};
650 compressed_pair<allocator_type, size_t> size_pair_{default_construct_tag{}, 0};
651
652 template <bool, typename>
653 friend struct rb_tree_iterator;
654
655private:
656 struct node_guard {
657 private:
658 rb_tree* tree_;
659 link_type node_;
660 bool released_ = false;
661
662 public:
663 node_guard(rb_tree* t, link_type n) noexcept :
664 tree_(t),
665 node_(n) {}
666
667 ~node_guard() {
668 if (!released_) {
669 tree_->destroy_node(node_);
670 }
671 }
672
673 link_type release() noexcept {
674 released_ = true;
675 return node_;
676 }
677 };
678
683 link_type& root() const noexcept { return reinterpret_cast<link_type&>(header_->parent_); }
684
689 link_type& leftmost() const noexcept { return reinterpret_cast<link_type&>(header_->left_); }
690
695 link_type& rightmost() const noexcept { return reinterpret_cast<link_type&>(header_->right_); }
696
702 static link_type& left(link_type ptr) noexcept { return reinterpret_cast<link_type&>(ptr->left_); }
703
709 static link_type& right(link_type ptr) noexcept { return reinterpret_cast<link_type&>(ptr->right_); }
710
716 static link_type& parent(link_type ptr) noexcept { return reinterpret_cast<link_type&>(ptr->parent_); }
717
723 static const Key& key(link_type ptr) noexcept { return KeyOfValue()(ptr->data); }
724
730 static const Key& key(base_ptr ptr) noexcept { return rb_tree::key(reinterpret_cast<link_type>(ptr)); }
731
737 static link_type minimum(link_type ptr) noexcept { return static_cast<link_type>(base_node::minimum(ptr)); }
738
744 static link_type maximum(link_type ptr) noexcept { return static_cast<link_type>(base_node::maximum(ptr)); }
745
752 template <typename... Args>
753 link_type create_node(Args&&... args) {
754 link_type tmp = size_pair_.get_base().allocate();
755 try {
756 _NEFORCE construct(&tmp->data, _NEFORCE forward<Args>(args)...);
757 } catch (...) {
758 rb_tree::destroy_node(tmp);
759 throw;
760 }
761 return tmp;
762 }
763
769 link_type copy_node(link_type ptr) {
770 link_type tmp = rb_tree::create_node(ptr->data);
771 tmp->color_ = ptr->color_;
772 tmp->left_ = nullptr;
773 tmp->right_ = nullptr;
774 return tmp;
775 }
776
781 void destroy_node(link_type ptr) noexcept(is_nothrow_destructible_v<link_node>) {
782 if (ptr == nullptr) {
783 return;
784 }
785 _NEFORCE destroy(&ptr->data);
786 size_pair_.get_base().deallocate(ptr);
787 }
788
796 iterator insert_into(base_ptr child, base_ptr parent, link_type ptr) {
797 auto x = static_cast<link_type>(child);
798 auto y = static_cast<link_type>(parent);
799 if (y == header_ || x != nullptr || key_compare_(rb_tree::key(ptr), rb_tree::key(y))) {
800 rb_tree::left(y) = ptr;
801 if (y == header_) {
802 root() = ptr;
803 leftmost() = ptr;
804 rightmost() = ptr;
805 } else if (y == leftmost()) {
806 leftmost() = ptr;
807 }
808 } else {
809 rb_tree::right(y) = ptr;
810 if (y == rightmost()) {
811 rightmost() = ptr;
812 }
813 }
814 rb_tree::parent(ptr) = y;
815 rb_tree::left(ptr) = nullptr;
816 rb_tree::right(ptr) = nullptr;
817 _NEFORCE rb_tree_insert_rebalance(ptr, header_->parent_);
818 ++size_pair_.value;
819 return iterator(ptr, this);
820 }
821
828 link_type copy_under(link_type src_root, link_type parent) {
829 link_type top = rb_tree::copy_node(src_root);
830 top->parent_ = parent;
831 try {
832 if (src_root->right_ != nullptr) {
833 top->right_ = rb_tree::copy_under(rb_tree::right(src_root), top);
834 }
835 parent = top;
836 src_root = rb_tree::left(src_root);
837 while (src_root != nullptr) {
838 link_type y = rb_tree::copy_node(src_root);
839 parent->left_ = y;
840 y->parent_ = parent;
841 if (src_root->right_ != nullptr) {
842 y->right_ = rb_tree::copy_under(rb_tree::right(src_root), y);
843 }
844 parent = y;
845 src_root = rb_tree::left(src_root);
846 }
847 } catch (...) {
848 rb_tree::erase_under(top);
849 throw;
850 }
851 return top;
852 }
853
858 void erase_under(link_type root) noexcept(is_nothrow_destructible_v<link_node>) {
859 if (root == nullptr) {
860 return;
861 }
862 rb_tree::erase_under(rb_tree::right(root));
863 rb_tree::erase_under(rb_tree::left(root));
864 rb_tree::destroy_node(root);
865 }
866
870 void header_init() {
871 header_ = size_pair_.get_base().allocate(1);
872 header_->color_ = RB_TREE_RED;
873 root() = nullptr;
874 leftmost() = header_;
875 rightmost() = header_;
876 }
877
882 void copy_from(const rb_tree& other) {
883 if (other.root() == nullptr) {
884 root() = nullptr;
885 leftmost() = header_;
886 rightmost() = header_;
887 } else {
888 try {
889 root() = rb_tree::copy_under(other.root(), header_);
890 } catch (...) {
891 size_pair_.get_base().deallocate(header_);
892 header_ = nullptr;
893 throw;
894 }
895 leftmost() = rb_tree::minimum(root());
896 rightmost() = rb_tree::maximum(root());
897 }
898 size_pair_.value = other.size_pair_.value;
899 }
900
906 pair<iterator, bool> insert_unique(link_type node) {
907 link_type y = header_;
908 link_type x = root();
909 bool comp = true;
910 while (x != nullptr) {
911 y = x;
912 comp = key_compare_(rb_tree::key(node), rb_tree::key(x));
913 x = comp ? rb_tree::left(x) : rb_tree::right(x);
914 }
915
916 iterator j(y, this);
917 if (comp) {
918 if (j == begin()) {
919 return pair<iterator, bool>(rb_tree::insert_into(x, y, node), true);
920 }
921 --j;
922 }
923
924 if (key_compare_(rb_tree::key(link_type(j.node_)), rb_tree::key(node))) {
925 return pair<iterator, bool>(rb_tree::insert_into(x, y, node), true);
926 }
927 rb_tree::destroy_node(node);
928 return pair<iterator, bool>(j, false);
929 }
930
936 iterator insert_equal(link_type node) {
937 link_type y = header_;
938 link_type x = root();
939 while (x != nullptr) {
940 y = x;
941 x = key_compare_(rb_tree::key(node), rb_tree::key(x)) ? rb_tree::left(x) : rb_tree::right(x);
942 }
943 return rb_tree::insert_into(x, y, node);
944 }
945
946public:
950 rb_tree() { header_init(); }
951
956 explicit rb_tree(const Compare& comp) :
957 key_compare_(comp) {
958 header_init();
959 }
960
965 rb_tree(const rb_tree& other) :
966 key_compare_(other.key_compare_),
967 extracter_(other.extracter_),
968 size_pair_(other.size_pair_) {
969 header_init();
970 rb_tree::copy_from(other);
971 }
972
978 rb_tree& operator=(const rb_tree& other) {
979 if (_NEFORCE addressof(other) == this) {
980 return *this;
981 }
982 rb_tree tmp(other);
983 rb_tree::swap(tmp);
984 return *this;
985 }
986
994 header_(_NEFORCE move(other.header_)),
995 key_compare_(_NEFORCE move(other.key_compare_)),
996 extracter_(_NEFORCE move(other.extracter_)),
997 size_pair_(_NEFORCE move(other.size_pair_)) {
998 other.header_ = nullptr;
999 other.size_pair_.value = 0;
1000 }
1001
1008 if (_NEFORCE addressof(other) == this) {
1009 return *this;
1010 }
1011 rb_tree tmp(_NEFORCE move(other));
1012 rb_tree::swap(tmp);
1013 return *this;
1014 }
1015
1020 clear();
1021 if (header_) {
1022 size_pair_.get_base().deallocate(header_);
1023 }
1024 }
1025
1030 NEFORCE_NODISCARD iterator begin() noexcept { return {leftmost(), this}; }
1031
1036 NEFORCE_NODISCARD iterator end() noexcept { return {header_, this}; }
1037
1042 NEFORCE_NODISCARD const_iterator begin() const noexcept { return cbegin(); }
1043
1048 NEFORCE_NODISCARD const_iterator end() const noexcept { return cend(); }
1049
1054 NEFORCE_NODISCARD const_iterator cbegin() const noexcept { return {leftmost(), this}; }
1055
1060 NEFORCE_NODISCARD const_iterator cend() const noexcept { return {header_, this}; }
1061
1066 NEFORCE_NODISCARD reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
1067
1072 NEFORCE_NODISCARD reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
1073
1078 NEFORCE_NODISCARD const_reverse_iterator rbegin() const noexcept { return crbegin(); }
1079
1084 NEFORCE_NODISCARD const_reverse_iterator rend() const noexcept { return crend(); }
1085
1090 NEFORCE_NODISCARD const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); }
1091
1096 NEFORCE_NODISCARD const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); }
1097
1102 NEFORCE_NODISCARD size_type size() const noexcept { return size_pair_.value; }
1103
1108 NEFORCE_NODISCARD size_type max_size() const noexcept { return static_cast<size_type>(-1); }
1109
1114 NEFORCE_NODISCARD bool empty() const noexcept { return size_pair_.value == 0; }
1115
1120 NEFORCE_NODISCARD Compare key_compare() const noexcept(is_nothrow_copy_constructible_v<Compare>) {
1121 return key_compare_;
1122 }
1123
1130 template <typename... Args>
1132 const link_type tmp = rb_tree::create_node(_NEFORCE forward<Args>(args)...);
1133 return rb_tree::insert_unique(tmp);
1134 }
1135
1142
1149
1157 template <typename... Args>
1158 iterator emplace_unique_hint(iterator position, Args&&... args) {
1159 link_type tmp = rb_tree::create_node(_NEFORCE forward<Args>(args)...);
1160 node_guard guard(this, tmp);
1161
1162 if (position.node_ == header_->left_) {
1163 if (size() > 0 && key_compare_(rb_tree::key(tmp), rb_tree::key(position.node_))) {
1164 guard.release();
1165 return rb_tree::insert_into(position.node_, position.node_, tmp);
1166 }
1167 guard.release();
1168 return rb_tree::insert_unique(tmp).first;
1169 }
1170
1171 if (position.node_ == header_) {
1172 if (key_compare_(rb_tree::key(rightmost()), rb_tree::key(tmp))) {
1173 guard.release();
1174 return rb_tree::insert_into(nullptr, rightmost(), tmp);
1175 }
1176 guard.release();
1177 return rb_tree::insert_unique(tmp).first;
1178 }
1179
1180 iterator before = position;
1181 --before;
1182
1183 if (key_compare_(rb_tree::key(before.node_), rb_tree::key(tmp)) &&
1184 key_compare_(rb_tree::key(tmp), rb_tree::key(position.node_))) {
1185 if (rb_tree::right(link_type(before.node_)) == nullptr) {
1186 guard.release();
1187 return rb_tree::insert_into(nullptr, before.node_, tmp);
1188 }
1189 if (rb_tree::left(link_type(position.node_)) == nullptr) {
1190 guard.release();
1191 return rb_tree::insert_into(position.node_, position.node_, tmp);
1192 }
1193 }
1194
1195 guard.release();
1196 return rb_tree::insert_unique(tmp).first;
1197 }
1198
1205 iterator insert_unique(iterator position, const value_type& value) {
1206 return rb_tree::emplace_unique_hint(position, value);
1207 }
1208
1216 return rb_tree::emplace_unique_hint(position, _NEFORCE move(value));
1217 }
1218
1225 template <typename Iterator, enable_if_t<is_iter_v<Iterator>, int> = 0>
1226 void insert_unique(Iterator first, Iterator last) {
1227 for (; first != last; ++first) {
1228 rb_tree::insert_unique(*first);
1229 }
1230 }
1231
1238 template <typename... Args>
1239 iterator emplace_equal(Args&&... args) {
1240 const link_type tmp = rb_tree::create_node(_NEFORCE forward<Args>(args)...);
1241 return rb_tree::insert_equal(tmp);
1242 }
1243
1250
1256 iterator insert_equal(value_type&& value) { return rb_tree::emplace_equal(_NEFORCE move(value)); }
1257
1265 template <typename... Args>
1266 iterator emplace_equal_hint(iterator position, Args&&... args) {
1267 link_type tmp = rb_tree::create_node(_NEFORCE forward<Args>(args)...);
1268 node_guard guard(this, tmp);
1269
1270 if (position.node_ == header_->left_) {
1271 if (size() > 0 && key_compare_(rb_tree::key(tmp), rb_tree::key(position.node_))) {
1272 guard.release();
1273 return rb_tree::insert_into(position.node_, position.node_, tmp);
1274 }
1275 guard.release();
1276 return rb_tree::insert_equal(tmp);
1277 }
1278
1279 if (position.node_ == header_) {
1280 if (!key_compare_(rb_tree::key(tmp), rb_tree::key(rightmost()))) {
1281 guard.release();
1282 return rb_tree::insert_into(nullptr, rightmost(), tmp);
1283 }
1284 guard.release();
1285 return rb_tree::insert_equal(tmp);
1286 }
1287
1288 iterator before = position;
1289 --before;
1290
1291 if (!key_compare_(rb_tree::key(tmp), rb_tree::key(before.node_)) &&
1292 !key_compare_(rb_tree::key(position.node_), rb_tree::key(tmp))) {
1293 if (rb_tree::right(link_type(before.node_)) == nullptr) {
1294 guard.release();
1295 return rb_tree::insert_into(nullptr, before.node_, tmp);
1296 }
1297 guard.release();
1298 return rb_tree::insert_into(position.node_, position.node_, tmp);
1299 }
1300
1301 guard.release();
1302 return rb_tree::insert_equal(tmp);
1303 }
1304
1311 iterator insert_equal(iterator position, const value_type& value) {
1312 return rb_tree::emplace_equal_hint(position, value);
1313 }
1314
1322 return rb_tree::emplace_equal_hint(position, _NEFORCE move(value));
1323 }
1324
1331 template <typename Iterator, enable_if_t<is_iter_v<Iterator>, int> = 0>
1332 void insert_equal(Iterator first, Iterator last) {
1333 for (; first != last; ++first) {
1334 rb_tree::insert_equal(*first);
1335 }
1336 }
1337
1345 const size_type n = _NEFORCE distance(p.first, p.second);
1347 return n;
1348 }
1349
1355 auto y = reinterpret_cast<link_type>(
1356 _NEFORCE rb_tree_erase_rebalance(position.node_, header_->parent_, header_->left_, header_->right_));
1357 rb_tree::destroy_node(y);
1358 --size_pair_.value;
1359 }
1360
1367 if (first == begin() && last == end()) {
1368 clear();
1369 } else {
1370 while (first != last) {
1371 rb_tree::erase(first++);
1372 }
1373 }
1374 }
1375
1379 void clear() noexcept(is_nothrow_destructible_v<link_node>) {
1380 if (header_ == nullptr) {
1381 return;
1382 }
1383 if (size_pair_.value == 0) {
1384 return;
1385 }
1386 rb_tree::erase_under(root());
1387 leftmost() = header_;
1388 root() = nullptr;
1389 rightmost() = header_;
1390 size_pair_.value = 0;
1391 }
1392
1398 NEFORCE_NODISCARD iterator find(const key_type& key) {
1399 link_type y = header_;
1400 link_type x = root();
1401
1402 while (x != nullptr) {
1403 if (!key_compare_(rb_tree::key(x), key)) {
1404 y = x;
1405 x = rb_tree::left(x);
1406 } else {
1407 x = rb_tree::right(x);
1408 }
1409 }
1410
1411 iterator j(y, this);
1412 if (j == end()) {
1413 return end();
1414 }
1415 return key_compare_(key, rb_tree::key(y)) ? end() : j;
1416 }
1417
1423 NEFORCE_NODISCARD const_iterator find(const key_type& key) const {
1424 link_type y = header_;
1425 link_type x = root();
1426
1427 while (x != nullptr) {
1428 if (!key_compare_(rb_tree::key(x), key)) {
1429 y = x;
1430 x = rb_tree::left(x);
1431 } else {
1432 x = rb_tree::right(x);
1433 }
1434 }
1435
1436 const_iterator j(y, this);
1437 if (j == cend()) {
1438 return cend();
1439 }
1440 return key_compare_(key, rb_tree::key(y)) ? cend() : j;
1441 }
1442
1448 NEFORCE_NODISCARD size_type count(const key_type& key) const {
1450 const size_type n = _NEFORCE distance(p.first, p.second);
1451 return n;
1452 }
1453
1459 NEFORCE_NODISCARD iterator lower_bound(const key_type& key) {
1460 link_type y = header_;
1461 link_type x = root();
1462
1463 while (x != nullptr) {
1464 if (!key_compare_(rb_tree::key(x), key)) {
1465 y = x;
1466 x = rb_tree::left(x);
1467 } else {
1468 x = rb_tree::right(x);
1469 }
1470 }
1471
1472 return iterator(y, this);
1473 }
1474
1480 NEFORCE_NODISCARD const_iterator lower_bound(const key_type& key) const {
1481 link_type y = header_;
1482 link_type x = root();
1483
1484 while (x != nullptr) {
1485 if (!key_compare_(rb_tree::key(x), key)) {
1486 y = x;
1487 x = rb_tree::left(x);
1488 } else {
1489 x = rb_tree::right(x);
1490 }
1491 }
1492
1493 return const_iterator(y, this);
1494 }
1495
1501 NEFORCE_NODISCARD iterator upper_bound(const key_type& key) {
1502 link_type y = header_;
1503 link_type x = root();
1504
1505 while (x != nullptr) {
1506 if (key_compare_(key, rb_tree::key(x))) {
1507 y = x;
1508 x = rb_tree::left(x);
1509 } else {
1510 x = rb_tree::right(x);
1511 }
1512 }
1513
1514 return iterator(y, this);
1515 }
1516
1522 NEFORCE_NODISCARD const_iterator upper_bound(const key_type& key) const {
1523 link_type y = header_;
1524 link_type x = root();
1525
1526 while (x != nullptr) {
1527 if (key_compare_(key, rb_tree::key(x))) {
1528 y = x;
1529 x = rb_tree::left(x);
1530 } else {
1531 x = rb_tree::right(x);
1532 }
1533 }
1534
1535 return const_iterator(y, this);
1536 }
1537
1546
1555
1562 _NEFORCE swap(header_, other.header_);
1563 _NEFORCE swap(size_pair_, other.size_pair_);
1564 _NEFORCE swap(key_compare_, other.key_compare_);
1565 _NEFORCE swap(extracter_, other.extracter_);
1566 }
1567
1573 NEFORCE_NODISCARD bool equal_to(const rb_tree& rhs) const
1574 noexcept(noexcept(_NEFORCE equal(cbegin(), cend(), rhs.cbegin()))) {
1575 return size() == rhs.size() && _NEFORCE equal(cbegin(), cend(), rhs.cbegin());
1576 }
1577
1583 NEFORCE_NODISCARD bool less_than(const rb_tree& rhs) const
1584 noexcept(noexcept(_NEFORCE lexicographical_compare(cbegin(), cend(), rhs.cbegin(), rhs.cend()))) {
1585 return _NEFORCE lexicographical_compare(cbegin(), cend(), rhs.cbegin(), rhs.cend());
1586 }
1587};
1588 // RBTree
1590
1591NEFORCE_END_NAMESPACE__
1592#endif // NEFORCE_CORE_CONTAINER_RB_TREE_HPP__
const_iterator lower_bound(const key_type &key) const
获取第一个不小于指定键的元素位置(常量版本)
void erase(iterator first, iterator last) noexcept(is_nothrow_destructible_v< link_node >)
删除指定范围内的元素
reverse_iterator rend() noexcept
获取反向结束迭代器
iterator emplace_equal(Args &&... args)
在树中构造元素(允许重复键版本)
iterator upper_bound(const key_type &key)
获取第一个大于指定键的元素位置
iterator insert_equal(value_type &&value)
移动插入元素(允许重复键版本)
iterator insert_unique(iterator position, const value_type &value)
在提示位置附近插入元素(唯一键版本)
rb_tree(rb_tree &&other) noexcept(is_nothrow_move_constructible_v< Compare > &&is_nothrow_move_constructible_v< KeyOfValue > &&is_nothrow_move_constructible_v< allocator_type >)
移动构造函数
const_iterator begin() const noexcept
获取常量起始迭代器
iterator insert_equal(iterator position, const value_type &value)
在提示位置附近插入元素(允许重复键版本)
iterator insert_equal(iterator position, value_type &&value)
在提示位置附近移动插入元素(允许重复键版本)
void insert_equal(Iterator first, Iterator last)
范围插入元素(允许重复键版本)
size_type max_size() const noexcept
获取最大可能大小
iterator emplace_unique_hint(iterator position, Args &&... args)
在提示位置附近构造元素(唯一键版本)
const_reverse_iterator crend() const noexcept
获取常量反向结束迭代器
const_reverse_iterator rend() const noexcept
获取常量反向结束迭代器
bool equal_to(const rb_tree &rhs) const noexcept(noexcept(_NEFORCE equal(cbegin(), cend(), rhs.cbegin())))
相等比较操作符
iterator end() noexcept
获取结束迭代器
pair< const_iterator, const_iterator > equal_range(const key_type &key) const
获取等于指定键的元素范围(常量版本)
const_iterator upper_bound(const key_type &key) const
获取第一个大于指定键的元素位置(常量版本)
reverse_iterator rbegin() noexcept
获取反向起始迭代器
size_type size() const noexcept
获取元素数量
pair< iterator, bool > insert_unique(const value_type &value)
插入元素(唯一键版本)
iterator insert_equal(const value_type &value)
插入元素(允许重复键版本)
pair< iterator, bool > emplace_unique(Args &&... args)
在树中构造元素(唯一键版本)
size_type erase(const key_type &key) noexcept(is_nothrow_destructible_v< link_node >)
删除所有具有指定键的元素
const_reverse_iterator crbegin() const noexcept
获取常量反向起始迭代器
void erase(iterator position) noexcept(is_nothrow_destructible_v< link_node >)
删除指定位置的元素
const_iterator find(const key_type &key) const
查找具有指定键的元素(常量版本)
pair< iterator, iterator > equal_range(const key_type &key)
获取等于指定键的元素范围
iterator lower_bound(const key_type &key)
获取第一个不小于指定键的元素位置
iterator emplace_equal_hint(iterator position, Args &&... args)
在提示位置附近构造元素(允许重复键版本)
const_iterator end() const noexcept
获取常量结束迭代器
rb_tree & operator=(rb_tree &&other) noexcept(is_nothrow_move_constructible_v< rb_tree >)
移动赋值运算符
void insert_unique(Iterator first, Iterator last)
范围插入元素(唯一键版本)
void clear() noexcept(is_nothrow_destructible_v< link_node >)
清空树
bool less_than(const rb_tree &rhs) const noexcept(noexcept(_NEFORCE lexicographical_compare(cbegin(), cend(), rhs.cbegin(), rhs.cend())))
小于比较操作符
Compare key_compare() const noexcept(is_nothrow_copy_constructible_v< Compare >)
获取键比较函数对象
rb_tree(const rb_tree &other)
拷贝构造函数
const_iterator cend() const noexcept
获取常量结束迭代器
const_reverse_iterator rbegin() const noexcept
获取常量反向起始迭代器
pair< iterator, bool > insert_unique(value_type &&value)
移动插入元素(唯一键版本)
size_type count(const key_type &key) const
统计具有指定键的元素数量
iterator begin() noexcept
获取起始迭代器
void swap(rb_tree &other) noexcept(is_nothrow_swappable_v< Compare > &&is_nothrow_swappable_v< KeyOfValue > &&is_nothrow_swappable_v< allocator_type >)
交换两个红黑树的内容
rb_tree()
默认构造函数
iterator find(const key_type &key)
查找具有指定键的元素
bool empty() const noexcept
检查是否为空
rb_tree & operator=(const rb_tree &other)
拷贝赋值运算符
~rb_tree()
析构函数
const_iterator cbegin() const noexcept
获取常量起始迭代器
iterator insert_unique(iterator position, value_type &&value)
在提示位置附近移动插入元素(唯一键版本)
rb_tree(const Compare &comp)
构造函数,指定比较函数
比较算法
压缩对实现
内存构造和销毁函数
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
constexpr T * addressof(T &x) noexcept
获取对象的地址
constexpr bool is_object_v
is_object的便捷变量模板
constexpr bool lexicographical_compare(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2, Compare comp) noexcept(noexcept(++first1) &&noexcept(++first2) &&noexcept(comp(*first1, *first2)) &&noexcept(first1==last1 &&first2 !=last2))
字典序比较两个范围
constexpr bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2, BinaryPredicate binary_pred) noexcept(noexcept(++first1) &&noexcept(++first2) &&noexcept(binary_pred(*first1, *first2)))
比较两个范围是否相等
constexpr void destroy(T *pointer) noexcept(is_nothrow_destructible_v< T >)
销毁单个对象
constexpr T * construct(T *ptr, Args &&... args) noexcept(is_nothrow_constructible_v< T, Args... >)
在指定内存位置构造对象
constexpr iter_difference_t< Iterator > distance(Iterator first, Iterator last)
计算两个迭代器之间的距离
standard_allocator< T > allocator
标准分配器别名
uint64_t size_t
无符号大小类型
int64_t ptrdiff_t
指针差类型
void rb_tree_insert_rebalance(rb_tree_node_base *insert, rb_tree_node_base *&root) noexcept
插入节点后重新平衡红黑树
constexpr bool RB_TREE_RED
红黑树节点颜色常量:红色
rb_tree_node_base * rb_tree_erase_rebalance(rb_tree_node_base *erase, rb_tree_node_base *&root, rb_tree_node_base *&leftmost, rb_tree_node_base *&rightmost) noexcept
删除节点后重新平衡红黑树
void rb_tree_rotate_right(rb_tree_node_base *axis, rb_tree_node_base *&root) noexcept
红黑树右旋转
void rb_tree_rotate_left(rb_tree_node_base *axis, rb_tree_node_base *&root) noexcept
红黑树左旋转
constexpr bool RB_TREE_BLACK
红黑树节点颜色常量:黑色
constexpr size_t erase(Container &cont, const U &value)
从容器中删除所有等于指定值的元素
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
void swap()=delete
删除无参数的swap重载
constexpr bool is_nothrow_swappable_v
is_nothrow_swappable的便捷变量模板
constexpr bool is_allocator_v
is_allocator的便捷变量模板
constexpr bool is_nothrow_copy_constructible_v
is_nothrow_copy_constructible的便捷变量模板
constexpr bool is_nothrow_default_constructible_v
is_nothrow_default_constructible的便捷变量模板
constexpr bool is_nothrow_move_constructible_v
is_nothrow_move_constructible的便捷变量模板
constexpr bool is_nothrow_destructible_v
is_nothrow_destructible的便捷变量模板
typename conditional< Test, T1, T2 >::type conditional_t
conditional的便捷别名
constexpr bool is_same_v
is_same的便捷变量模板
集合器接口
迭代器接口
键值对
标准分配器
双向迭代器标签
集合器接口模板
迭代器接口模板
存储两个值的元组对
T2 second
第二个元素
T1 first
第一个元素
红黑树迭代器基类
void increment() noexcept
递增操作(移动到中序遍历的后继节点)
rb_tree_node_base::base_ptr base_ptr
基类指针类型
void decrement() noexcept
递减操作(移动到中序遍历的前驱节点)
bidirectional_iterator_tag iterator_category
双向迭代器
base_ptr node_
当前节点指针
conditional_t< IsConst, typename container_type::const_pointer, typename container_type::pointer > pointer
指针类型
reference dereference() const noexcept
解引用操作
typename container_type::size_type size_type
大小类型
typename container_type::difference_type difference_type
差值类型
RbTree container_type
容器类型
conditional_t< IsConst, typename container_type::const_reference, typename container_type::reference > reference
引用类型
constexpr void increment() noexcept
递增操作
constexpr void decrement() noexcept
递减操作
rb_tree_base_iterator::iterator_category iterator_category
迭代器类别(双向)
typename container_type::value_type value_type
值类型
const container_type * container() const noexcept
获取关联容器
pointer base() const noexcept
获取底层指针
bool equal_to(const rb_tree_iterator &rhs) const noexcept
相等比较
红黑树节点基类
static base_ptr maximum(base_ptr root) noexcept
获取子树中的最大节点
base_ptr parent_
父节点指针
base_ptr left_
左子节点指针
base_ptr right_
右子节点指针
static base_ptr minimum(base_ptr root) noexcept
获取子树中的最小节点
color_type color_
节点颜色,默认为红色
rb_tree_node_base * base_ptr
基类指针类型
bool color_type
颜色类型
红黑树数据节点
rb_tree_node() noexcept(is_nothrow_default_constructible_v< T >)
默认构造函数