Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_rbRk.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
50# ifndef TPL_RBRK_H
51# define TPL_RBRK_H
52
53# include <ahFunction.H>
54# include <tpl_arrayStack.H>
55# include <tpl_binNodeUtils.H>
56# include <tpl_binNodeXt.H>
57# include <tpl_binTreeOps.H>
58# include <rbNodeRk.H>
59# include <ah-errors.H>
60# include <ah-concepts.H>
61
62namespace Aleph {
63
107template <template <typename> class NodeType, typename Key, class Compare>
108 requires StrictWeakOrder<Compare, Key>
110{
111public:
112
113 typedef NodeType<Key> Node;
114
115private:
116
120 Compare cmp;
121 static constexpr signed char CmpLess = -1;
122 static constexpr signed char CmpEqual = 0;
123 static constexpr signed char CmpGreater = 1;
124
125 /* The path from the head node to the node an operation works on. Each
126 operation builds it, consumes it and drops it: it lives in the frame
127 of the operation, so the tree never allocates. */
128 // The head node, then at most Node::MaxHeight nodes of the tree.
130
134 Node * search_and_stack_rb(const Key & key,
135 signed char & cmp_result, Path & path)
136 {
137 Node *p = root;
138 Node *candidate = nullptr; // Tracks potential duplicate when going right
139 size_t candidate_pos = 0; // Stack size when candidate was set
140
141 path.push(head);
142 do
143 {
144 path.push(p);
145 if (cmp(key, KEY(p))) // key < KEY(p)
146 {
147 cmp_result = CmpLess;
148 p = LLINK(p);
149 }
150 else // key >= KEY(p), could be equal
151 {
152 candidate = p;
153 candidate_pos = path.size();
154 cmp_result = CmpGreater;
155 p = RLINK(p);
156 }
157 }
158 while (p != Node::NullPtr);
159
160 // Optimistic duplicate check: when going right, key >= KEY(candidate)
161 // If also KEY(candidate) >= key (i.e., not less), then key == KEY(candidate)
162 if (candidate != nullptr and not static_cast<bool>(cmp(KEY(candidate), key))) [[unlikely]]
163 {
164 cmp_result = CmpEqual;
165 // Truncate stack: remove nodes pushed after candidate
166 const size_t to_pop =
167 path.size() > candidate_pos ?
168 path.size() - candidate_pos : 0;
169 if (to_pop > 0)
170 path.popn(to_pop);
171 return candidate;
172 }
173
174 return path.top();
175 }
176
177 Node * search_dup_and_stack_rb(const Key & key,
178 signed char & cmp_result, Path & path)
179 {
180 Node * p = root;
181 path.push(head);
182 cmp_result = CmpEqual;
183 do
184 {
185 path.push(p);
186 const Key & pk = KEY(p);
187 if (cmp(key, pk))
188 {
189 cmp_result = CmpLess;
190 p = LLINK(p);
191 }
192 else
193 {
194 cmp_result = CmpGreater;
195 p = RLINK(p);
196 }
197 }
198 while (p != Node::NullPtr);
199
200 return path.top();
201 }
202
203 // Rotate to right with counter update
204 Node * rotate_to_right_rk(Node * p, Node * pp) noexcept
205 {
206 assert(p != Node::NullPtr);
207 assert(pp != Node::NullPtr);
208 assert(LLINK(pp) == p or RLINK(pp) == p);
209
210 Node * q = LLINK(p);
211 LLINK(p) = RLINK(q);
212 RLINK(q) = p;
213
214 // Update counters
215 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
216 COUNT(q) = COUNT(LLINK(q)) + COUNT(RLINK(q)) + 1;
217
218 if (LLINK(pp) == p)
219 LLINK(pp) = q;
220 else
221 RLINK(pp) = q;
222
223 return q;
224 }
225
226 // Rotate to left with counter update
227 Node * rotate_to_left_rk(Node * p, Node * pp) noexcept
228 {
229 assert(p != Node::NullPtr);
230 assert(pp != Node::NullPtr);
231 assert(LLINK(pp) == p or RLINK(pp) == p);
232
233 Node * q = RLINK(p);
234 RLINK(p) = LLINK(q);
235 LLINK(q) = p;
236
237 // Update counters
238 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
239 COUNT(q) = COUNT(LLINK(q)) + COUNT(RLINK(q)) + 1;
240
241 if (LLINK(pp) == p)
242 LLINK(pp) = q;
243 else
244 RLINK(pp) = q;
245
246 return q;
247 }
248
249 void fix_red_condition(Node * p, Path & path)
250 {
251 assert(COLOR(p) == RED);
252
253 while (p != root)
254 {
255 Node * pp = path.pop();
256 if (COLOR(pp) == BLACK)
257 break;
258
259 if (root == pp)
260 {
261 COLOR(root) = BLACK;
262 break;
263 }
264
265 Node * ppp = path.pop();
266 Node * spp = LLINK(ppp) == pp ? RLINK(ppp) : LLINK(ppp);
267
268 if (COLOR(spp) == RED)
269 {
270 COLOR(ppp) = RED;
271 COLOR(pp) = BLACK;
272 COLOR(spp) = BLACK;
273 p = ppp;
274 continue;
275 }
276
277 Node * pppp = path.pop();
278 if (LLINK(pp) == p and LLINK(ppp) == pp)
279 {
280 rotate_to_right_rk(ppp, pppp);
281 COLOR(pp) = BLACK;
282 }
283 else if (RLINK(pp) == p and RLINK(ppp) == pp)
284 {
285 rotate_to_left_rk(ppp, pppp);
286 COLOR(pp) = BLACK;
287 }
288 else
289 {
290 if (RLINK(pp) == p)
291 {
292 rotate_to_left_rk(pp, ppp);
293 rotate_to_right_rk(ppp, pppp);
294 }
295 else
296 {
297 rotate_to_right_rk(pp, ppp);
298 rotate_to_left_rk(ppp, pppp);
299 }
300 COLOR(p) = BLACK;
301 }
302
303 COLOR(ppp) = RED;
304 break;
305 }
306 }
307
308 void find_succ_and_swap(Node * p, Node *& pp, Path & path)
309 {
310 Node *& ref_path_top = path.top();
311
312 Node * fSucc = p;
313 Node * succ = RLINK(p);
314 path.push(succ);
315
316 while (LLINK(succ) != Node::NullPtr)
317 {
318 fSucc = succ;
319 succ = LLINK(succ);
320 path.push(succ);
321 }
322
323 ref_path_top = succ;
324 path.top() = p;
325
326 if (LLINK(pp) == p)
327 LLINK(pp) = succ;
328 else
329 RLINK(pp) = succ;
330
331 LLINK(succ) = LLINK(p);
332 LLINK(p) = Node::NullPtr;
333
334 if (RLINK(p) == succ)
335 {
336 RLINK(p) = RLINK(succ);
337 RLINK(succ) = p;
338 pp = succ;
339 }
340 else
341 {
342 Node * succr = RLINK(succ);
343 RLINK(succ) = RLINK(p);
344 LLINK(fSucc) = p;
345 RLINK(p) = succr;
346 pp = fSucc;
347 }
348
349 std::swap(COLOR(succ), COLOR(p));
350 COUNT(succ) = COUNT(p); // Copy counter to successor position
351 }
352
353 void fix_black_condition(Node * p, Path & path)
354 {
355 if (COLOR(p) == RED)
356 {
357 COLOR(p) = BLACK;
358 return;
359 }
360
361 Node * pp = path.popn(2);
362 while (p != root)
363 {
364 assert(LLINK(pp) == p or RLINK(pp) == p);
365 assert(LLINK(path.top()) == pp or RLINK(path.top()) == pp);
366
367 Node * sp = LLINK(pp) == p ? RLINK(pp) : LLINK(pp);
368 if (COLOR(sp) == RED)
369 {
370 Node *& ppp = path.top();
371
372 if (LLINK(pp) == p)
373 {
374 sp = LLINK(sp);
375 ppp = rotate_to_left_rk(pp, ppp);
376 }
377 else
378 {
379 sp = RLINK(sp);
380 ppp = rotate_to_right_rk(pp, ppp);
381 }
382
383 COLOR(ppp) = BLACK;
384 COLOR(pp) = RED;
385 }
386
387 Node * np, * snp;
388 if (LLINK(pp) == p)
389 {
390 np = RLINK(sp);
391 snp = LLINK(sp);
392 }
393 else
394 {
395 np = LLINK(sp);
396 snp = RLINK(sp);
397 }
398
399 if (COLOR(np) == RED)
400 {
401 Node * ppp = path.top();
402
403 if (RLINK(sp) == np)
404 rotate_to_left_rk(pp, ppp);
405 else
406 rotate_to_right_rk(pp, ppp);
407
408 COLOR(sp) = COLOR(pp);
409 COLOR(pp) = BLACK;
410 COLOR(np) = BLACK;
411
412 return;
413 }
414
415 if (COLOR(snp) == RED)
416 {
417 Node * ppp = path.top();
418
419 if (LLINK(sp) == snp)
420 {
421 rotate_to_right_rk(sp, pp);
422 rotate_to_left_rk(pp, ppp);
423 }
424 else
425 {
426 rotate_to_left_rk(sp, pp);
427 rotate_to_right_rk(pp, ppp);
428 }
429
430 COLOR(snp) = COLOR(pp);
431 COLOR(pp) = BLACK;
432
433 return;
434 }
435
436 if (COLOR(pp) == RED)
437 {
438 COLOR(pp) = BLACK;
439 COLOR(sp) = RED;
440 return;
441 }
442
443 COLOR(sp) = RED;
444 p = pp;
445 pp = path.pop();
446 }
447 }
448
449 // Update counters along the stack after insertion
450 void update_counters_after_insertion(const Path & path) noexcept
451 {
452 // Stack contains: [head, ..., nodes in path, ..., closest to insertion]
453 // top(0) = closest node, top(size-1) = head (don't update)
454 const size_t sz = path.size();
455 for (size_t i = 0; i < sz - 1; ++i)
456 ++COUNT(path.top(i));
457 }
458
459 // Update counters along the stack after deletion
460 void update_counters_after_deletion(const Path & path) noexcept
461 {
462 const size_t sz = path.size();
463 for (size_t i = 0; i < sz - 1; ++i)
464 --COUNT(path.top(i));
465 }
466
467public:
468
469 typedef Key key_type;
470
471 Compare & key_comp() noexcept { return cmp; }
472
473 Compare & get_compare() noexcept { return key_comp(); }
474
489 noexcept(std::is_nothrow_default_constructible_v<Node> and
490 std::is_nothrow_default_constructible_v<Compare> and
491 std::is_nothrow_constructible_v<Compare, Compare &>)
492 : Gen_Rb_Tree_Rk(Compare())
493 {
494 // empty
495 }
496
505 Gen_Rb_Tree_Rk(Compare cmp_arg)
506 noexcept(std::is_nothrow_default_constructible_v<Node> and
507 std::is_nothrow_constructible_v<Compare, Compare &>)
508 : head(&head_node), root(head_node.getR()),
509 cmp(cmp_arg)
510 { /* empty */ }
511
519 void swap(Gen_Rb_Tree_Rk & tree) noexcept(std::is_nothrow_swappable_v<Compare>)
520 {
521 // The comparison first: if its swap throws, the nodes stay where
522 // they were
523 std::swap(cmp, tree.cmp);
524 std::swap(root, tree.root);
525 }
526
545 noexcept(bin_node_detail::nothrow_header_from_v<Node> and
546 bin_node_detail::nothrow_compare_taken_v<Compare>)
547 : head_node(bin_node_detail::header_from(tree.head_node)),
548 head(&head_node), root(head_node.getR()),
549 cmp(bin_node_detail::compare_to_take(tree.cmp))
550 {
551 root = tree.root;
552 tree.root = Node::NullPtr;
553 }
554
563 noexcept(std::is_nothrow_move_assignable_v<Compare>)
564 {
565 if (this != &tree)
566 {
567 // The comparison first: if its move throws, no tree has changed
568 cmp = std::move(tree.cmp);
569 root = tree.root;
570 tree.root = Node::NullPtr;
571 }
572 return *this;
573 }
574
575 virtual ~Gen_Rb_Tree_Rk() = default;
576
577 Node * search(const Key & key)
578 {
579 Node * retVal = Aleph::searchInBinTree<Node, Compare>(root, key, cmp);
580 return retVal == Node::NullPtr ? nullptr : retVal;
581 }
582
583 Node *& getRoot() noexcept { return root; }
584
585 [[nodiscard]] bool is_empty() const noexcept { return root == Node::NullPtr; }
586
587 [[nodiscard]] size_t size() const noexcept { return COUNT(root); }
588
590 {
591 assert(p != nullptr and p != Node::NullPtr);
592 assert(COLOR(p) == RED);
593
594 if (root == Node::NullPtr)
595 return root = p;
596
597 signed char cmp_result = CmpEqual;
598 Path path;
599 Node * q = search_and_stack_rb(KEY(p), cmp_result, path);
600 if (cmp_result == CmpLess)
601 LLINK(q) = p;
602 else if (cmp_result == CmpGreater)
603 RLINK(q) = p;
604 else
605 return nullptr;
606
608 fix_red_condition(p, path);
609
610 return p;
611 }
612
614 {
615 assert(p != nullptr and p != Node::NullPtr);
616 assert(COLOR(p) == RED);
617
618 if (root == Node::NullPtr)
619 return root = p;
620
621 signed char cmp_result = CmpEqual;
622 Path path;
623 Node * q = search_and_stack_rb(KEY(p), cmp_result, path);
624 if (cmp_result == CmpLess)
625 LLINK(q) = p;
626 else if (cmp_result == CmpGreater)
627 RLINK(q) = p;
628 else
629 return q;
630
632 fix_red_condition(p, path);
633
634 return p;
635 }
636
638 {
639 assert(p != nullptr and p != Node::NullPtr);
640 assert(COLOR(p) == RED);
641
642 if (root == Node::NullPtr)
643 return root = p;
644
645 signed char cmp_result = CmpEqual;
646 Path path;
647 Node * q = search_dup_and_stack_rb(KEY(p), cmp_result, path);
648 if (cmp_result == CmpLess)
649 LLINK(q) = p;
650 else
651 RLINK(q) = p;
652
654 fix_red_condition(p, path);
655
656 return p;
657 }
658
659 [[nodiscard]] bool verify() const { return is_red_black_bst_rk(root, cmp) and check_rank_tree(root); }
660
661 Node * remove(const Key & key)
662 {
663 if (root == Node::NullPtr)
664 return nullptr;
665
666 signed char cmp_result = CmpEqual;
667 Path path;
668 Node * q = search_and_stack_rb(key, cmp_result, path);
669 if (cmp_result != CmpEqual)
670 return nullptr;
671
672 Node * pq = path.top(1);
673 Node * p;
674
675 while (true)
676 {
677 if (LLINK(q) == Node::NullPtr)
678 {
679 if (LLINK(pq) == q)
680 p = LLINK(pq) = RLINK(q);
681 else
682 p = RLINK(pq) = RLINK(q);
683 break;
684 }
685
686 if (RLINK(q) == Node::NullPtr)
687 {
688 if (LLINK(pq) == q)
689 p = LLINK(pq) = LLINK(q);
690 else
691 p = RLINK(pq) = LLINK(q);
692 break;
693 }
694
695 find_succ_and_swap(q, pq, path);
696 }
697
699
700 if (COLOR(q) == BLACK)
701 fix_black_condition(p, path);
702
703 q->reset();
704
705 return q;
706 }
707
715 Node * select(const size_t i) const
716 {
717 return Aleph::select(root, i);
718 }
719
730 std::pair<long, Node*> position(const Key & key) const
731 noexcept(nothrow_compare_copy_v<Compare, Key>)
732 {
733 std::pair<long, Node*> ret_val;
734
736 inorder_position(root, key, ret_val.second);
737
738 return ret_val;
739 }
740
750 std::pair<long, Node*> find_position(const Key & key) const
751 noexcept(nothrow_compare_copy_v<Compare, Key>)
752 {
753 std::pair<long, Node*> r(-2, nullptr);
754
756 find_position(root, key, r.second);
757
758 return r;
759 }
760
761private:
762
763 // Compute black height of RB tree (count black nodes to any leaf)
764 static size_t black_height(Node * p) noexcept
765 {
766 size_t h = 0;
767 while (p != Node::NullPtr)
768 {
769 if (COLOR(p) == BLACK)
770 ++h;
771 p = LLINK(p); // Any path works since all paths have same black height
772 }
773 return h;
774 }
775
776 // Simple rotation without parent link update (for internal use)
777 static Node * rotate_right_simple(Node * p) noexcept
778 {
779 Node * q = LLINK(p);
780 LLINK(p) = RLINK(q);
781 RLINK(q) = p;
782 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
783 COUNT(q) = COUNT(LLINK(q)) + COUNT(RLINK(q)) + 1;
784 return q;
785 }
786
787 static Node * rotate_left_simple(Node * p) noexcept
788 {
789 Node * q = RLINK(p);
790 RLINK(p) = LLINK(q);
791 LLINK(q) = p;
792 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
793 COUNT(q) = COUNT(LLINK(q)) + COUNT(RLINK(q)) + 1;
794 return q;
795 }
796
797 // Fix red-red violation going up from node p
798 // pp_ptr is a pointer to the pointer that points to the subtree being fixed
799 static void fix_red_violation(Node *& subtree_root) noexcept
800 {
801 // Simplified iterative fix for red-red violations
802 // This version uses recursion-based fixing during join
803 if (subtree_root == Node::NullPtr)
804 return;
805
806 // Check left child for red-red
807 if (LLINK(subtree_root) != Node::NullPtr and
808 COLOR(LLINK(subtree_root)) == RED)
809 {
810 Node * l = LLINK(subtree_root);
811 if (LLINK(l) != Node::NullPtr and COLOR(LLINK(l)) == RED)
812 {
813 // Left-left case
814 subtree_root = rotate_right_simple(subtree_root);
815 COLOR(subtree_root) = BLACK;
816 COLOR(RLINK(subtree_root)) = RED;
817 return;
818 }
819 if (RLINK(l) != Node::NullPtr and COLOR(RLINK(l)) == RED)
820 {
821 // Left-right case
822 LLINK(subtree_root) = rotate_left_simple(l);
823 subtree_root = rotate_right_simple(subtree_root);
824 COLOR(subtree_root) = BLACK;
825 COLOR(RLINK(subtree_root)) = RED;
826 return;
827 }
828 }
829
830 // Check right child for red-red
831 if (RLINK(subtree_root) != Node::NullPtr and
832 COLOR(RLINK(subtree_root)) == RED)
833 {
834 Node * r = RLINK(subtree_root);
835 if (RLINK(r) != Node::NullPtr and COLOR(RLINK(r)) == RED)
836 {
837 // Right-right case
838 subtree_root = rotate_left_simple(subtree_root);
839 COLOR(subtree_root) = BLACK;
840 COLOR(LLINK(subtree_root)) = RED;
841 return;
842 }
843 if (LLINK(r) != Node::NullPtr and COLOR(LLINK(r)) == RED)
844 {
845 // Right-left case
846 RLINK(subtree_root) = rotate_right_simple(r);
847 subtree_root = rotate_left_simple(subtree_root);
848 COLOR(subtree_root) = BLACK;
849 COLOR(LLINK(subtree_root)) = RED;
850 return;
851 }
852 }
853 }
854
855 // Extract minimum node with rebalancing
856 // Returns the extracted min and updates p to the new root
857 static Node * extract_min_rb(Node *& p) noexcept
858 {
859 if (p == Node::NullPtr)
860 return Node::NullPtr;
861
862 if (LLINK(p) == Node::NullPtr)
863 {
864 Node * ret = p;
865 p = RLINK(p);
866 ret->reset();
867 return ret;
868 }
869
870 // Use recursive approach with fix-up
871 struct ExtractHelper
872 {
873 static Node * extract(Node *& p, bool & height_decreased) noexcept
874 {
875 if (LLINK(p) == Node::NullPtr)
876 {
877 Node * ret = p;
878 height_decreased = (COLOR(p) == BLACK);
879 p = RLINK(p);
880 ret->reset();
881 return ret;
882 }
883
884 bool child_decreased = false;
885 Node * ret = extract(LLINK(p), child_decreased);
886 --COUNT(p);
887
888 if (child_decreased)
889 {
890 // Left subtree's black height decreased
891 // Need to rebalance
892 Node * s = RLINK(p); // sibling
893 if (s != Node::NullPtr)
894 {
895 if (COLOR(s) == RED)
896 {
897 // Case 1: sibling is red
898 p = rotate_left_simple(p);
899 COLOR(p) = BLACK;
900 COLOR(LLINK(p)) = RED;
901 // Continue fixing in LLINK(p)
902 s = RLINK(LLINK(p));
903 }
904
905 if (s != Node::NullPtr)
906 {
907 if ((LLINK(s) == Node::NullPtr or COLOR(LLINK(s)) == BLACK) and
908 (RLINK(s) == Node::NullPtr or COLOR(RLINK(s)) == BLACK))
909 {
910 // Case 2: sibling and its children are black
911 COLOR(s) = RED;
912 if (COLOR(p) == RED)
913 {
914 COLOR(p) = BLACK;
915 height_decreased = false;
916 }
917 else
918 height_decreased = true;
919 }
920 else
921 {
922 // Case 3 or 4: sibling has a red child
923 if (RLINK(s) == Node::NullPtr or COLOR(RLINK(s)) == BLACK)
924 {
925 // Case 3: left child of sibling is red
926 COLOR(LLINK(s)) = BLACK;
927 COLOR(s) = RED;
928 RLINK(p) = rotate_right_simple(s);
929 }
930 // Case 4: right child of sibling is red
931 Color old_color = COLOR(p);
932 p = rotate_left_simple(p);
933 COLOR(p) = old_color;
934 COLOR(LLINK(p)) = BLACK;
935 if (RLINK(p) != Node::NullPtr)
936 COLOR(RLINK(p)) = BLACK;
937 height_decreased = false;
938 }
939 }
940 else
941 height_decreased = (COLOR(p) == BLACK);
942 }
943 else
944 height_decreased = (COLOR(p) == BLACK);
945 }
946 else
947 height_decreased = false;
948
949 return ret;
950 }
951 };
952
953 bool dummy = false;
954 return ExtractHelper::extract(p, dummy);
955 }
956
957 // Extract maximum node with rebalancing
958 static Node * extract_max_rb(Node *& p) noexcept
959 {
960 if (p == Node::NullPtr)
961 return Node::NullPtr;
962
963 if (RLINK(p) == Node::NullPtr)
964 {
965 Node * ret = p;
966 p = LLINK(p);
967 ret->reset();
968 return ret;
969 }
970
971 struct ExtractHelper
972 {
973 static Node * extract(Node *& p, bool & height_decreased) noexcept
974 {
975 if (RLINK(p) == Node::NullPtr)
976 {
977 Node * ret = p;
978 height_decreased = (COLOR(p) == BLACK);
979 p = LLINK(p);
980 ret->reset();
981 return ret;
982 }
983
984 bool child_decreased = false;
985 Node * ret = extract(RLINK(p), child_decreased);
986 --COUNT(p);
987
988 if (child_decreased)
989 {
990 Node * s = LLINK(p); // sibling
991 if (s != Node::NullPtr)
992 {
993 if (COLOR(s) == RED)
994 {
995 p = rotate_right_simple(p);
996 COLOR(p) = BLACK;
997 COLOR(RLINK(p)) = RED;
998 s = LLINK(RLINK(p));
999 }
1000
1001 if (s != Node::NullPtr)
1002 {
1003 if ((LLINK(s) == Node::NullPtr or COLOR(LLINK(s)) == BLACK) and
1004 (RLINK(s) == Node::NullPtr or COLOR(RLINK(s)) == BLACK))
1005 {
1006 COLOR(s) = RED;
1007 if (COLOR(p) == RED)
1008 {
1009 COLOR(p) = BLACK;
1010 height_decreased = false;
1011 }
1012 else
1013 height_decreased = true;
1014 }
1015 else
1016 {
1017 if (LLINK(s) == Node::NullPtr or COLOR(LLINK(s)) == BLACK)
1018 {
1019 COLOR(RLINK(s)) = BLACK;
1020 COLOR(s) = RED;
1021 LLINK(p) = rotate_left_simple(s);
1022 }
1023 Color old_color = COLOR(p);
1024 p = rotate_right_simple(p);
1025 COLOR(p) = old_color;
1026 COLOR(RLINK(p)) = BLACK;
1027 if (LLINK(p) != Node::NullPtr)
1028 COLOR(LLINK(p)) = BLACK;
1029 height_decreased = false;
1030 }
1031 }
1032 else
1033 height_decreased = (COLOR(p) == BLACK);
1034 }
1035 else
1036 height_decreased = (COLOR(p) == BLACK);
1037 }
1038 else
1039 height_decreased = false;
1040
1041 return ret;
1042 }
1043 };
1044
1045 bool dummy = false;
1046 return ExtractHelper::extract(p, dummy);
1047 }
1048
1049 // Join two RB trees with a pivot node
1050 // All keys in t1 < pivot < all keys in t2
1051 // bh1 and bh2 are black heights
1052 static Node * join_with_pivot_rb(Node * t1, size_t bh1, Node * pivot,
1053 Node * t2, size_t bh2) noexcept
1054 {
1055 if (bh1 == bh2)
1056 {
1057 // Simple case: equal black heights
1058 LLINK(pivot) = t1;
1059 RLINK(pivot) = t2;
1060 COLOR(pivot) = RED;
1061 COUNT(pivot) = COUNT(t1) + COUNT(t2) + 1;
1062 return pivot;
1063 }
1064
1065 if (bh1 > bh2)
1066 {
1067 // t1 is taller (more black nodes), descend right spine
1068 return join_right_rb(t1, bh1, pivot, t2, bh2);
1069 }
1070 // t2 is taller, descend left spine
1071 return join_left_rb(t1, bh1, pivot, t2, bh2);
1072 }
1073
1074 // Join when bh1 > bh2
1075 static Node * join_right_rb(Node * t1, size_t bh1, Node * pivot,
1076 Node * t2, size_t bh2) noexcept
1077 {
1078 if (t1 == Node::NullPtr)
1079 {
1080 LLINK(pivot) = Node::NullPtr;
1081 RLINK(pivot) = t2;
1082 COLOR(pivot) = BLACK;
1083 COUNT(pivot) = COUNT(t2) + 1;
1084 return pivot;
1085 }
1086
1087 // Descend right spine until black height matches bh2
1088 size_t curr_bh = bh1;
1089 if (COLOR(t1) == BLACK)
1090 --curr_bh;
1091
1092 if (curr_bh == bh2)
1093 {
1094 // Found the join point
1095 LLINK(pivot) = RLINK(t1);
1096 RLINK(pivot) = t2;
1097 COLOR(pivot) = RED;
1098 COUNT(pivot) = COUNT(RLINK(t1)) + COUNT(t2) + 1;
1099 RLINK(t1) = pivot;
1100 COUNT(t1) = COUNT(LLINK(t1)) + COUNT(pivot) + 1;
1101
1102 // Fix red-red if needed
1103 if (COLOR(t1) == RED)
1104 {
1105 // t1 is red and pivot is red - need rotation
1106 // Since we came from the right, do left rotation
1107 Node * result = rotate_left_simple(t1);
1108 COLOR(result) = BLACK;
1109 COLOR(LLINK(result)) = RED;
1110 return result;
1111 }
1112 return t1;
1113 }
1114
1115 // Continue descending
1116 Node * right_result = join_right_rb(RLINK(t1), curr_bh, pivot, t2, bh2);
1117 RLINK(t1) = right_result;
1118 COUNT(t1) = COUNT(LLINK(t1)) + COUNT(RLINK(t1)) + 1;
1119
1120 // Check for red-red violation
1121 if (COLOR(t1) == BLACK and RLINK(t1) != Node::NullPtr and COLOR(RLINK(t1)) == RED)
1122 {
1123 if (RLINK(RLINK(t1)) != Node::NullPtr and COLOR(RLINK(RLINK(t1))) == RED)
1124 {
1125 Node * result = rotate_left_simple(t1);
1126 COLOR(result) = BLACK;
1127 COLOR(LLINK(result)) = RED;
1128 COLOR(RLINK(result)) = BLACK;
1129 return result;
1130 }
1131 if (LLINK(RLINK(t1)) != Node::NullPtr and COLOR(LLINK(RLINK(t1))) == RED)
1132 {
1133 RLINK(t1) = rotate_right_simple(RLINK(t1));
1134 Node * result = rotate_left_simple(t1);
1135 COLOR(result) = BLACK;
1136 COLOR(LLINK(result)) = RED;
1137 COLOR(RLINK(result)) = BLACK;
1138 return result;
1139 }
1140 }
1141
1142 return t1;
1143 }
1144
1145 // Join when bh2 > bh1
1146 static Node * join_left_rb(Node * t1, size_t bh1, Node * pivot,
1147 Node * t2, size_t bh2) noexcept
1148 {
1149 if (t2 == Node::NullPtr)
1150 {
1151 LLINK(pivot) = t1;
1152 RLINK(pivot) = Node::NullPtr;
1153 COLOR(pivot) = BLACK;
1154 COUNT(pivot) = COUNT(t1) + 1;
1155 return pivot;
1156 }
1157
1158 size_t curr_bh = bh2;
1159 if (COLOR(t2) == BLACK)
1160 --curr_bh;
1161
1162 if (curr_bh == bh1)
1163 {
1164 RLINK(pivot) = LLINK(t2);
1165 LLINK(pivot) = t1;
1166 COLOR(pivot) = RED;
1167 COUNT(pivot) = COUNT(t1) + COUNT(LLINK(t2)) + 1;
1168 LLINK(t2) = pivot;
1169 COUNT(t2) = COUNT(pivot) + COUNT(RLINK(t2)) + 1;
1170
1171 if (COLOR(t2) == RED)
1172 {
1173 Node * result = rotate_right_simple(t2);
1174 COLOR(result) = BLACK;
1175 COLOR(RLINK(result)) = RED;
1176 return result;
1177 }
1178 return t2;
1179 }
1180
1181 Node * left_result = join_left_rb(t1, bh1, pivot, LLINK(t2), curr_bh);
1182 LLINK(t2) = left_result;
1183 COUNT(t2) = COUNT(LLINK(t2)) + COUNT(RLINK(t2)) + 1;
1184
1185 // Check for red-red violation
1186 if (COLOR(t2) == BLACK and LLINK(t2) != Node::NullPtr and COLOR(LLINK(t2)) == RED)
1187 {
1188 if (LLINK(LLINK(t2)) != Node::NullPtr and COLOR(LLINK(LLINK(t2))) == RED)
1189 {
1190 Node * result = rotate_right_simple(t2);
1191 COLOR(result) = BLACK;
1192 COLOR(LLINK(result)) = BLACK;
1193 COLOR(RLINK(result)) = RED;
1194 return result;
1195 }
1196 if (RLINK(LLINK(t2)) != Node::NullPtr and COLOR(RLINK(LLINK(t2))) == RED)
1197 {
1198 LLINK(t2) = rotate_left_simple(LLINK(t2));
1199 Node * result = rotate_right_simple(t2);
1200 COLOR(result) = BLACK;
1201 COLOR(LLINK(result)) = BLACK;
1202 COLOR(RLINK(result)) = RED;
1203 return result;
1204 }
1205 }
1206
1207 return t2;
1208 }
1209
1210 // Recursive join of two RB trees
1211 static Node * join_exclusive_rec_rb(Node * t1, size_t bh1,
1212 Node * t2, size_t bh2) noexcept
1213 {
1214 if (t1 == Node::NullPtr)
1215 return t2;
1216 if (t2 == Node::NullPtr)
1217 return t1;
1218
1219 // Extract a pivot from the larger tree
1220 Node * pivot;
1221 if (bh1 >= bh2)
1222 {
1223 pivot = extract_max_rb(t1);
1224 if (t1 != Node::NullPtr)
1225 bh1 = black_height(t1);
1226 else
1227 bh1 = 0;
1228 }
1229 else
1230 {
1231 pivot = extract_min_rb(t2);
1232 if (t2 != Node::NullPtr)
1233 bh2 = black_height(t2);
1234 else
1235 bh2 = 0;
1236 }
1237
1238 return join_with_pivot_rb(t1, bh1, pivot, t2, bh2);
1239 }
1240
1241 // Split by key recursively. It compares on the way down and relinks on
1242 // the way back, so a comparison that throws leaves the tree as it was
1243 Node * split_key_rec_rb(Node * p, const Key & key,
1244 Node *& t1, Node *& t2)
1245 noexcept(nothrow_compare_v<Compare, Key>)
1246 {
1247 if (p == Node::NullPtr)
1248 {
1249 t1 = t2 = Node::NullPtr;
1250 return Node::NullPtr;
1251 }
1252
1253 Node * found;
1254 if (cmp(key, KEY(p)))
1255 {
1256 Node * left_t2;
1257 found = split_key_rec_rb(LLINK(p), key, t1, left_t2);
1258
1259 LLINK(p) = Node::NullPtr;
1260 const size_t left_t2_bh = (left_t2 != Node::NullPtr) ? black_height(left_t2) : 0;
1261 const size_t right_bh = (RLINK(p) != Node::NullPtr) ? black_height(RLINK(p)) : 0;
1262
1263 Node * right_tree = RLINK(p);
1264 RLINK(p) = Node::NullPtr;
1265 COUNT(p) = 1;
1266 COLOR(p) = RED;
1267
1268 t2 = join_with_pivot_rb(left_t2, left_t2_bh, p, right_tree, right_bh);
1269 if (t2 != Node::NullPtr)
1270 COLOR(t2) = BLACK;
1271 }
1272 else if (cmp(KEY(p), key))
1273 {
1274 Node * right_t1;
1275 found = split_key_rec_rb(RLINK(p), key, right_t1, t2);
1276
1277 RLINK(p) = Node::NullPtr;
1278 const size_t right_t1_bh = (right_t1 != Node::NullPtr) ? black_height(right_t1) : 0;
1279 const size_t left_bh = (LLINK(p) != Node::NullPtr) ? black_height(LLINK(p)) : 0;
1280
1281 Node * left_tree = LLINK(p);
1282 LLINK(p) = Node::NullPtr;
1283 COUNT(p) = 1;
1284 COLOR(p) = RED;
1285
1286 t1 = join_with_pivot_rb(left_tree, left_bh, p, right_t1, right_t1_bh);
1287 if (t1 != Node::NullPtr)
1288 COLOR(t1) = BLACK;
1289 }
1290 else
1291 {
1292 t1 = LLINK(p);
1293 t2 = RLINK(p);
1294 LLINK(p) = RLINK(p) = Node::NullPtr;
1295 COUNT(p) = 1;
1296 COLOR(p) = RED;
1297 found = p;
1298 if (t1 != Node::NullPtr)
1299 COLOR(t1) = BLACK;
1300 if (t2 != Node::NullPtr)
1301 COLOR(t2) = BLACK;
1302 }
1303
1304 return found;
1305 }
1306
1307 // Split by key including duplicates, as split_key_rec_rb()
1308 void split_key_dup_rec_rb(Node * p, const Key & key,
1309 Node *& t1, Node *& t2)
1310 noexcept(nothrow_compare_v<Compare, Key>)
1311 {
1312 if (p == Node::NullPtr)
1313 {
1314 t1 = t2 = Node::NullPtr;
1315 return;
1316 }
1317
1318 if (cmp(key, KEY(p)))
1319 {
1320 Node * left_t2;
1321 split_key_dup_rec_rb(LLINK(p), key, t1, left_t2);
1322
1323 LLINK(p) = Node::NullPtr;
1324 const size_t left_t2_bh = (left_t2 != Node::NullPtr) ? black_height(left_t2) : 0;
1325 const size_t right_bh = (RLINK(p) != Node::NullPtr) ? black_height(RLINK(p)) : 0;
1326
1327 Node * right_tree = RLINK(p);
1328 RLINK(p) = Node::NullPtr;
1329 COUNT(p) = 1;
1330 COLOR(p) = RED;
1331
1332 t2 = join_with_pivot_rb(left_t2, left_t2_bh, p, right_tree, right_bh);
1333 if (t2 != Node::NullPtr)
1334 COLOR(t2) = BLACK;
1335 }
1336 else
1337 {
1338 Node * right_t1;
1339 split_key_dup_rec_rb(RLINK(p), key, right_t1, t2);
1340
1341 RLINK(p) = Node::NullPtr;
1342 const size_t right_t1_bh = (right_t1 != Node::NullPtr) ? black_height(right_t1) : 0;
1343 const size_t left_bh = (LLINK(p) != Node::NullPtr) ? black_height(LLINK(p)) : 0;
1344
1345 Node * left_tree = LLINK(p);
1346 LLINK(p) = Node::NullPtr;
1347 COUNT(p) = 1;
1348 COLOR(p) = RED;
1349
1350 t1 = join_with_pivot_rb(left_tree, left_bh, p, right_t1, right_t1_bh);
1351 if (t1 != Node::NullPtr)
1352 COLOR(t1) = BLACK;
1353 }
1354 }
1355
1356 // Split by position recursively
1357 void split_pos_rec_rb(Node * p, size_t pos, Node *& t1, Node *& t2) noexcept
1358 {
1359 if (p == Node::NullPtr)
1360 {
1361 t1 = t2 = Node::NullPtr;
1362 return;
1363 }
1364
1365 size_t left_count = COUNT(LLINK(p));
1366
1367 if (pos <= left_count)
1368 {
1369 Node * left_t2;
1370 split_pos_rec_rb(LLINK(p), pos, t1, left_t2);
1371
1372 LLINK(p) = Node::NullPtr;
1373 const size_t left_t2_bh = (left_t2 != Node::NullPtr) ? black_height(left_t2) : 0;
1374 const size_t right_bh = (RLINK(p) != Node::NullPtr) ? black_height(RLINK(p)) : 0;
1375
1376 Node * right_tree = RLINK(p);
1377 RLINK(p) = Node::NullPtr;
1378 COUNT(p) = 1;
1379 COLOR(p) = RED;
1380
1381 t2 = join_with_pivot_rb(left_t2, left_t2_bh, p, right_tree, right_bh);
1382 if (t2 != Node::NullPtr)
1383 COLOR(t2) = BLACK;
1384 }
1385 else
1386 {
1387 Node * right_t1;
1388 split_pos_rec_rb(RLINK(p), pos - left_count - 1, right_t1, t2);
1389
1390 RLINK(p) = Node::NullPtr;
1391 const size_t right_t1_bh = (right_t1 != Node::NullPtr) ? black_height(right_t1) : 0;
1392 const size_t left_bh = (LLINK(p) != Node::NullPtr) ? black_height(LLINK(p)) : 0;
1393
1394 Node * left_tree = LLINK(p);
1395 LLINK(p) = Node::NullPtr;
1396 COUNT(p) = 1;
1397 COLOR(p) = RED;
1398
1399 t1 = join_with_pivot_rb(left_tree, left_bh, p, right_t1, right_t1_bh);
1400 if (t1 != Node::NullPtr)
1401 COLOR(t1) = BLACK;
1402 }
1403 }
1404
1405public:
1406
1421 {
1422 if (t.root == Node::NullPtr)
1423 return;
1424
1425 if (root == Node::NullPtr)
1426 {
1427 root = t.root;
1428 t.root = Node::NullPtr;
1429 return;
1430 }
1431
1432 // Collect nodes from t in preorder and insert them one by one
1433 // This is O(m log(n+m)) but guarantees correctness
1435 stack.push(t.root);
1436 t.root = Node::NullPtr;
1437
1438 while (not stack.is_empty())
1439 {
1440 Node * p = stack.pop();
1441 Node * left = LLINK(p);
1442 Node * right = RLINK(p);
1443
1444 // Reset node and insert
1445 p->reset();
1446 insert(p);
1447
1448 if (right != Node::NullPtr)
1449 stack.push(right);
1450 if (left != Node::NullPtr)
1451 stack.push(left);
1452 }
1453 }
1454
1473 Node * split_key(const Key & key, Gen_Rb_Tree_Rk & t1,
1474 Gen_Rb_Tree_Rk & t2) noexcept
1475 {
1476 if (root == Node::NullPtr)
1477 return nullptr;
1478
1479 // Simple O(n) implementation: iterate through tree and partition
1480 Node * found = nullptr;
1482 stack.push(root);
1483 root = Node::NullPtr;
1484
1485 while (not stack.is_empty())
1486 {
1487 Node * p = stack.pop();
1488 Node * left = LLINK(p);
1489 Node * right = RLINK(p);
1490
1491 if (right != Node::NullPtr)
1492 stack.push(right);
1493 if (left != Node::NullPtr)
1494 stack.push(left);
1495
1496 p->reset();
1497
1498 if (cmp(KEY(p), key))
1499 t1.insert(p);
1500 else if (cmp(key, KEY(p)))
1501 t2.insert(p);
1502 else
1503 found = p; // Found the key
1504 }
1505
1506 return found;
1507 }
1508
1526 void split_key_dup(const Key & key, Gen_Rb_Tree_Rk & t1,
1527 Gen_Rb_Tree_Rk & t2) noexcept
1528 {
1529 if (root == Node::NullPtr)
1530 return;
1531
1533 stack.push(root);
1534 root = Node::NullPtr;
1535
1536 while (not stack.is_empty())
1537 {
1538 Node * p = stack.pop();
1539 Node * left = LLINK(p);
1540 Node * right = RLINK(p);
1541
1542 if (right != Node::NullPtr)
1543 stack.push(right);
1544 if (left != Node::NullPtr)
1545 stack.push(left);
1546
1547 p->reset();
1548
1549 if (cmp(key, KEY(p)))
1550 t2.insert(p);
1551 else
1552 t1.insert_dup(p);
1553 }
1554 }
1555
1573 void split_pos(size_t pos, Gen_Rb_Tree_Rk & t1,
1574 Gen_Rb_Tree_Rk & t2) noexcept
1575 {
1576 if (root == Node::NullPtr)
1577 return;
1578
1579 if (pos == 0)
1580 {
1581 t2.root = root;
1582 root = Node::NullPtr;
1583 return;
1584 }
1585
1586 if (pos >= size())
1587 {
1588 t1.root = root;
1589 root = Node::NullPtr;
1590 return;
1591 }
1592
1593 // Simple O(n) implementation using inorder traversal
1594 size_t count = 0;
1596 Node * curr = root;
1597 root = Node::NullPtr;
1598
1599 // Inorder traversal
1600 while (curr != Node::NullPtr or not stack.is_empty())
1601 {
1602 while (curr != Node::NullPtr)
1603 {
1604 stack.push(curr);
1605 Node * left = LLINK(curr);
1606 LLINK(curr) = Node::NullPtr; // Disconnect
1607 curr = left;
1608 }
1609
1610 curr = stack.pop();
1611 Node * right = RLINK(curr);
1612 curr->reset();
1613
1614 if (count < pos)
1615 t1.insert(curr);
1616 else
1617 t2.insert(curr);
1618
1619 ++count;
1620 curr = right;
1621 }
1622 }
1623
1630 class Iterator : public BinTreeXt_Iterator<Gen_Rb_Tree_Rk, Node, Key, Compare>
1631 {
1633
1634 public:
1635
1636 using Base::Base;
1637 using Base::operator=;
1638 }; // end class Iterator
1639};
1640
1641
1650template <typename Key, class Compare = Aleph::less<Key>>
1652struct Rb_Tree_Rk : public Gen_Rb_Tree_Rk<RbNodeRk, Key, Compare>
1653{
1655 using Base::Base;
1656};
1657
1658
1667template <typename Key, class Compare = Aleph::less<Key>>
1669struct Rb_Tree_Rk_Vtl : public Gen_Rb_Tree_Rk<RbNodeRkVtl, Key, Compare>
1670{
1672 using Base::Base;
1673};
1674
1675
1676} // end namespace Aleph
1677
1678# endif // TPL_RBRK_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
Standard functor implementations and comparison objects.
@ KEY
Definition btreepic.C:169
long double h
Definition btreepic.C:154
Base iterator template for ranked binary search trees.
Functor encompassing basic operation for extended binary search trees.
Iterator on nodes of the tree.
Definition tpl_rbRk.H:1631
Red-black tree with rank support (select/position operations).
Definition tpl_rbRk.H:110
static Node * join_exclusive_rec_rb(Node *t1, size_t bh1, Node *t2, size_t bh2) noexcept
Definition tpl_rbRk.H:1211
Node * split_key(const Key &key, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
Split tree by key.
Definition tpl_rbRk.H:1473
static Node * join_left_rb(Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
Definition tpl_rbRk.H:1146
Node * search_dup_and_stack_rb(const Key &key, signed char &cmp_result, Path &path)
Definition tpl_rbRk.H:177
size_t size() const noexcept
Definition tpl_rbRk.H:587
void fix_red_condition(Node *p, Path &path)
Definition tpl_rbRk.H:249
void swap(Gen_Rb_Tree_Rk &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and tree.
Definition tpl_rbRk.H:519
void join_exclusive(Gen_Rb_Tree_Rk &t) noexcept
Join this tree exclusively with another tree.
Definition tpl_rbRk.H:1420
Node * select(const size_t i) const
Return the i-th node in order sense.
Definition tpl_rbRk.H:715
void update_counters_after_insertion(const Path &path) noexcept
Definition tpl_rbRk.H:450
static Node * join_right_rb(Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
Definition tpl_rbRk.H:1075
void find_succ_and_swap(Node *p, Node *&pp, Path &path)
Definition tpl_rbRk.H:308
static Node * extract_min_rb(Node *&p) noexcept
Definition tpl_rbRk.H:857
static Node * rotate_right_simple(Node *p) noexcept
Definition tpl_rbRk.H:777
void fix_black_condition(Node *p, Path &path)
Definition tpl_rbRk.H:353
static constexpr signed char CmpEqual
Definition tpl_rbRk.H:122
virtual ~Gen_Rb_Tree_Rk()=default
static void fix_red_violation(Node *&subtree_root) noexcept
Definition tpl_rbRk.H:799
void split_key_dup_rec_rb(Node *p, const Key &key, Node *&t1, Node *&t2) noexcept(nothrow_compare_v< Compare, Key >)
Definition tpl_rbRk.H:1308
Node * insert_dup(Node *p)
Definition tpl_rbRk.H:637
void update_counters_after_deletion(const Path &path) noexcept
Definition tpl_rbRk.H:460
bool is_empty() const noexcept
Definition tpl_rbRk.H:585
static Node * join_with_pivot_rb(Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
Definition tpl_rbRk.H:1052
Gen_Rb_Tree_Rk & operator=(Gen_Rb_Tree_Rk &&tree) noexcept(std::is_nothrow_move_assignable_v< Compare >)
Takes the nodes and the comparison criteria of tree, which is left empty.
Definition tpl_rbRk.H:562
Node * search(const Key &key)
Definition tpl_rbRk.H:577
Gen_Rb_Tree_Rk() noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with a comparison criteria built by default.
Definition tpl_rbRk.H:488
Gen_Rb_Tree_Rk(Gen_Rb_Tree_Rk &&tree) noexcept(bin_node_detail::nothrow_header_from_v< Node > and bin_node_detail::nothrow_compare_taken_v< Compare >)
Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again.
Definition tpl_rbRk.H:544
Node * search_and_stack_rb(const Key &key, signed char &cmp_result, Path &path)
Search for key and stack ancestors (optimistic duplicate detection).
Definition tpl_rbRk.H:134
bool verify() const
Definition tpl_rbRk.H:659
Node * rotate_to_right_rk(Node *p, Node *pp) noexcept
Definition tpl_rbRk.H:204
Gen_Rb_Tree_Rk(Compare cmp_arg) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with the comparison criteria cmp_arg.
Definition tpl_rbRk.H:505
std::pair< long, Node * > position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Compute the inorder position of a key.
Definition tpl_rbRk.H:730
void split_pos(size_t pos, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
Split tree by inorder position.
Definition tpl_rbRk.H:1573
static constexpr signed char CmpGreater
Definition tpl_rbRk.H:123
std::pair< long, Node * > find_position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Find the inorder position of a key in the tree.
Definition tpl_rbRk.H:750
Node * search_or_insert(Node *p)
Definition tpl_rbRk.H:613
Compare & key_comp() noexcept
Definition tpl_rbRk.H:471
Node * insert(Node *p)
Definition tpl_rbRk.H:589
static Node * rotate_left_simple(Node *p) noexcept
Definition tpl_rbRk.H:787
Node * split_key_rec_rb(Node *p, const Key &key, Node *&t1, Node *&t2) noexcept(nothrow_compare_v< Compare, Key >)
Definition tpl_rbRk.H:1243
void split_key_dup(const Key &key, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
Split tree by key including duplicates.
Definition tpl_rbRk.H:1526
Node *& getRoot() noexcept
Definition tpl_rbRk.H:583
static constexpr signed char CmpLess
Definition tpl_rbRk.H:121
Node * remove(const Key &key)
Definition tpl_rbRk.H:661
void split_pos_rec_rb(Node *p, size_t pos, Node *&t1, Node *&t2) noexcept
Definition tpl_rbRk.H:1357
Node * rotate_to_left_rk(Node *p, Node *pp) noexcept
Definition tpl_rbRk.H:227
Compare & get_compare() noexcept
Definition tpl_rbRk.H:473
static Node * extract_max_rb(Node *&p) noexcept
Definition tpl_rbRk.H:958
static size_t black_height(Node *p) noexcept
Definition tpl_rbRk.H:764
NodeType< Key > Node
Definition tpl_rbRk.H:113
Stack of fixed capacity whose storage is inside the object.
size_t size() const noexcept
Return the number of items.
T pop() noexcept
Pop the top; the stack must not be empty.
T & push(const T &data) noexcept
Push a copy of data.
T & top() noexcept
Return a modifiable reference to the top; the stack must not be empty.
T popn(const size_t n) noexcept
Perform n pops in constant time.
bool is_empty() const noexcept
Return true if the stack is empty.
Strict weak ordering constraint for BST comparators.
bool check_rank_tree(Node *root) noexcept
Return true if root is a valid extended binary tree.
auto & COUNT(Node *p) noexcept
Return the number of nodes of the tree fron p is root.
long inorder_position(Node *r, const typename Node::key_type &key, Node *&p, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Compute the inorder position of a key.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
Main namespace for Aleph-w library functions.
bool is_red_black_bst_rk(Node *node, Compare &cmp)
Verify that tree rooted at node is a valid red-black BST with counters.
Definition rbNodeRk.H:201
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
unsigned char Color
Definition rbNodeRk.H:50
STL namespace.
#define COLOR(p)
Definition quadnode.H:59
Red-Black tree node with rank (subtree count).
#define BLACK
Black color constant.
Definition rbNode.H:80
#define RED
Red color constant (newly inserted nodes are red)
Definition rbNode.H:77
Red-Black binary search tree with virtual destructor in its nodes and with subtree counters for selec...
Definition tpl_rbRk.H:1670
Red-Black binary search tree with nodes without virtual destructor and with subtree counters for sele...
Definition tpl_rbRk.H:1653
#define RLINK(i, n)
#define LLINK(i, n)
Gsl_Compatible_Rng r
Stack implementations backed by dynamic or fixed arrays.
Utility functions for binary tree operations.
Extended binary node with subtree count.
Binary tree operations (split, join, rotate).
DynList< int > l