Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_tdRbTreeRk.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_TDRBTREERK_H
51# define TPL_TDRBTREERK_H
52
53# include <functional>
54# include <limits>
55# include <tpl_binNode.H>
56# include <tpl_binNodeUtils.H>
57# include <tpl_binNodeXt.H>
58# include <tpl_binTreeOps.H>
59# include <rbNodeRk.H>
60# include <ah-concepts.H>
61
62namespace Aleph
63{
64
96template <template <class> class NodeType, typename Key,
97 class Compare = Aleph::less<Key>>
98 requires StrictWeakOrder<Compare, Key>
100{
101public:
102 using Node = NodeType<Key>;
103 using key_type = Key;
104 using compare_type = Compare;
105 static constexpr size_t PATH_STACK_CAPACITY = 128;
106 static_assert(PATH_STACK_CAPACITY >= 2 * std::numeric_limits<size_t>::digits,
107 "PATH_STACK_CAPACITY too small for red-black worst-case height");
108
109private:
115 Compare cmp;
116
119 bool less(const Key& a, const Key& b) const
120 noexcept(nothrow_compare_v<Compare, Key>)
121 {
122 return cmp(a, b);
123 }
124
127 bool equals(const Key& a, const Key& b) const
128 noexcept(nothrow_compare_v<Compare, Key>)
129 {
130 return not static_cast<bool>(cmp(a, b)) and not static_cast<bool>(cmp(b, a));
131 }
132
134 static Node* rotate_to_right_rk(Node *p, Node *pp) noexcept
135 {
136 assert(p != Node::NullPtr);
137 assert(pp != Node::NullPtr);
138
139 Node *q = LLINK(p);
140 LLINK(p) = RLINK(q);
141 RLINK(q) = p;
142
143 // Update counters
144 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
145 COUNT(q) = COUNT(LLINK(q)) + COUNT(RLINK(q)) + 1;
146
147 if (LLINK(pp) == p)
148 LLINK(pp) = q;
149 else
150 RLINK(pp) = q;
151
152 return q;
153 }
154
156 static Node* rotate_to_left_rk(Node *p, Node *pp) noexcept
157 {
158 assert(p != Node::NullPtr);
159 assert(pp != Node::NullPtr);
160
161 Node *q = RLINK(p);
162 RLINK(p) = LLINK(q);
163 LLINK(q) = p;
164
165 // Update counters
166 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
167 COUNT(q) = COUNT(LLINK(q)) + COUNT(RLINK(q)) + 1;
168
169 if (LLINK(pp) == p)
170 LLINK(pp) = q;
171 else
172 RLINK(pp) = q;
173
174 return q;
175 }
176
178 void restoreRedCondition(Node *p, Node *&fp, Node *ffp, Node *fffp) noexcept
179 {
180 assert(LLINK(fp) == p or RLINK(fp) == p);
181 assert(COLOR(fp) == RED);
182 assert(COLOR(p) == RED);
183
184 if (fp == root)
185 {
186 COLOR(fp) = BLACK;
187 return;
188 }
189
190 assert(LLINK(ffp) == fp or RLINK(ffp) == fp);
191 assert(COLOR(ffp) == BLACK);
192 assert(LLINK(fffp) == ffp or RLINK(fffp) == ffp);
193
194 COLOR(ffp) = RED;
195
196 if (LLINK(fp) == p and LLINK(ffp) == fp)
197 {
198 COLOR(fp) = BLACK;
199 rotate_to_right_rk(ffp, fffp);
200 }
201 else if (RLINK(fp) == p and RLINK(ffp) == fp)
202 {
203 COLOR(fp) = BLACK;
204 rotate_to_left_rk(ffp, fffp);
205 }
206 else
207 {
208 COLOR(p) = BLACK;
209 if (RLINK(fp) == p)
210 {
211 rotate_to_left_rk(fp, ffp);
212 rotate_to_right_rk(ffp, fffp);
213 }
214 else
215 {
216 rotate_to_right_rk(fp, ffp);
217 rotate_to_left_rk(ffp, fffp);
218 }
219 fp = fffp;
220 }
221 }
222
224 static void flipColors(Node* p) noexcept
225 {
226 assert(p != Node::NullPtr);
227 assert(COLOR(p) == BLACK);
228 assert(COLOR(LLINK(p)) == RED and COLOR(RLINK(p)) == RED);
229
230 COLOR(p) = RED;
231 COLOR(LLINK(p)) = COLOR(RLINK(p)) = BLACK;
232 }
233
241 noexcept(nothrow_compare_v<Compare, Key>)
242 {
243 assert(q != Node::NullPtr);
244 assert(root != Node::NullPtr);
245 assert(COLOR(q) == RED);
246
247 // Stack to track visited nodes for count updates
249 size_t stack_len = 0;
250
251 Node *p = root;
252 Node *fp = head;
253 Node *ffp = fHead;
254 Node *fffp = Node::NullPtr;
255 Node *nextNode;
256
257 stack[stack_len++] = p;
258
259 while (true)
260 {
261 if (equals(KEY(q), KEY(p)))
262 return nullptr; // Duplicate key
263
264 if (COLOR(p) == BLACK and COLOR(LLINK(p)) == RED
265 and COLOR(RLINK(p)) == RED)
266 {
267 flipColors(p);
268 if (COLOR(fp) == RED)
269 {
270 assert(fffp != Node::NullPtr);
271 restoreRedCondition(p, fp, ffp, fffp);
272 }
273 }
274
275 if (less(KEY(q), KEY(p)))
276 {
277 if (LLINK(p) == Node::NullPtr)
278 break;
279 nextNode = LLINK(p);
280 }
281 else
282 {
283 if (RLINK(p) == Node::NullPtr)
284 break;
285 nextNode = RLINK(p);
286 }
287
288 fffp = ffp;
289 ffp = fp;
290 fp = p;
291 p = nextNode;
292 stack[stack_len++] = p;
293 }
294
295 // Perform insertion
296 if (less(KEY(q), KEY(p)))
297 LLINK(p) = q;
298 else
299 RLINK(p) = q;
300
301 if (COLOR(p) == RED)
302 restoreRedCondition(q, p, fp, ffp);
303
304 // Update counts bottom-up - O(log n)
305 // Each node's count = left_count + right_count + 1
306 // Children are already correct (leaf has count=1, rotated nodes recalculated)
307 updateCountsFromStack(stack, stack_len);
308
309 return q;
310 }
311
316 noexcept(nothrow_compare_v<Compare, Key>)
317 {
318 assert(q != Node::NullPtr);
319 assert(root != Node::NullPtr);
320 assert(COLOR(q) == RED);
321
323 size_t stack_len = 0;
324
325 Node *p = root;
326 Node *fp = head;
327 Node *ffp = fHead;
328 Node *fffp = Node::NullPtr;
329 Node *nextNode;
330
331 stack[stack_len++] = p;
332
333 while (true)
334 {
335 if (COLOR(p) == BLACK and COLOR(LLINK(p)) == RED
336 and COLOR(RLINK(p)) == RED)
337 {
338 flipColors(p);
339 if (COLOR(fp) == RED)
340 {
341 assert(fffp != Node::NullPtr);
342 restoreRedCondition(p, fp, ffp, fffp);
343 }
344 }
345
346 if (less(KEY(q), KEY(p)))
347 {
348 if (LLINK(p) == Node::NullPtr)
349 break;
350 nextNode = LLINK(p);
351 }
352 else
353 {
354 if (RLINK(p) == Node::NullPtr)
355 break;
356 nextNode = RLINK(p);
357 }
358
359 fffp = ffp;
360 ffp = fp;
361 fp = p;
362 p = nextNode;
363 stack[stack_len++] = p;
364 }
365
366 if (less(KEY(q), KEY(p)))
367 LLINK(p) = q;
368 else
369 RLINK(p) = q;
370
371 if (COLOR(p) == RED)
372 restoreRedCondition(q, p, fp, ffp);
373
374 // Update counts bottom-up - O(log n)
375 updateCountsFromStack(stack, stack_len);
376
377 return q;
378 }
379
381 static size_t updateCountRec(Node *p) noexcept
382 {
383 if (p == Node::NullPtr)
384 return 0;
385
386 size_t left_count = updateCountRec(LLINK(p));
387 size_t right_count = updateCountRec(RLINK(p));
388 COUNT(p) = left_count + right_count + 1;
389 return COUNT(p);
390 }
391
395 static void updateCountsFromStack(Node** stack, size_t len) noexcept
396 {
397 // Process bottom-up: children are already correct
398 while (len > 0)
399 {
400 Node* p = stack[--len];
401 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
402 }
403 }
404
405
407 static Node* gotoLeftAndColorRed(Node *fp, Node *&ffp) noexcept
408 {
409 assert(fp != Node::NullPtr);
410 assert(ffp != Node::NullPtr);
411 assert(LLINK(fp) != Node::NullPtr);
412
413 Node *p = LLINK(fp);
414
415 if (COLOR(p) == RED)
416 return p;
417
418 Node *sp = RLINK(fp);
419
420 if (COLOR(fp) == BLACK)
421 {
422 assert(COLOR(sp) == RED);
423 rotate_to_left_rk(fp, ffp);
424 COLOR(fp) = RED;
425 COLOR(sp) = BLACK;
426 ffp = sp;
427 sp = RLINK(fp);
428 }
429
430 if (COLOR(LLINK(p)) == BLACK and COLOR(RLINK(p)) == BLACK)
431 {
432 assert(COLOR(fp) == RED);
433
434 COLOR(p) = RED;
435 COLOR(fp) = BLACK;
436
437 Node *np = RLINK(sp);
438 Node *snp = LLINK(sp);
439
440 if (COLOR(snp) == BLACK and COLOR(np) == BLACK)
441 {
442 COLOR(sp) = RED;
443 return p;
444 }
445
446 if (COLOR(np) == RED)
447 {
448 ffp = rotate_to_left_rk(fp, ffp);
449 COLOR(sp) = RED;
450 COLOR(np) = BLACK;
451 return p;
452 }
453
454 assert(COLOR(snp) == RED);
455
456 rotate_to_right_rk(sp, fp);
457 ffp = rotate_to_left_rk(fp, ffp);
458 }
459 return p;
460 }
461
463 static Node* gotoRightAndColorRed(Node *fp, Node *&ffp) noexcept
464 {
465 assert(fp != Node::NullPtr);
466 assert(ffp != Node::NullPtr);
467 assert(RLINK(fp) != Node::NullPtr);
468
469 Node *p = RLINK(fp);
470
471 if (COLOR(p) == RED)
472 return p;
473
474 Node *sp = LLINK(fp);
475
476 if (COLOR(fp) == BLACK)
477 {
478 assert(COLOR(sp) == RED);
479 rotate_to_right_rk(fp, ffp);
480 COLOR(fp) = RED;
481 COLOR(sp) = BLACK;
482 ffp = sp;
483 sp = LLINK(fp);
484 }
485
486 if (COLOR(LLINK(p)) == BLACK and COLOR(RLINK(p)) == BLACK)
487 {
488 assert(COLOR(fp) == RED);
489
490 COLOR(p) = RED;
491 COLOR(fp) = BLACK;
492
493 Node *np = LLINK(sp);
494 Node *snp = RLINK(sp);
495
496 if (COLOR(snp) == BLACK and COLOR(np) == BLACK)
497 {
498 COLOR(sp) = RED;
499 return p;
500 }
501
502 if (COLOR(np) == RED)
503 {
504 ffp = rotate_to_right_rk(fp, ffp);
505 COLOR(sp) = RED;
506 COLOR(np) = BLACK;
507 return p;
508 }
509
510 assert(COLOR(snp) == RED);
511
512 rotate_to_left_rk(sp, fp);
513 ffp = rotate_to_right_rk(fp, ffp);
514 }
515 return p;
516 }
517
519 static void findSuccAndSwap(Node *p, Node *&fp) noexcept
520 {
521 assert(p != Node::NullPtr);
522 assert(RLINK(p) != Node::NullPtr);
523
524 size_t original_count = COUNT(p);
525
526 Node *fSucc = p;
527 Node *succ = gotoRightAndColorRed(p, fp);
528 Node *ffSucc = fp;
529
530 while (LLINK(succ) != Node::NullPtr)
531 {
532 ffSucc = fSucc;
533 fSucc = succ;
534 succ = gotoLeftAndColorRed(fSucc, ffSucc);
535 }
536
537 if (LLINK(fp) == p)
538 LLINK(fp) = succ;
539 else
540 RLINK(fp) = succ;
541
542 LLINK(succ) = LLINK(p);
543 LLINK(p) = Node::NullPtr;
544
545 if (RLINK(p) == succ)
546 {
547 RLINK(p) = RLINK(succ);
548 RLINK(succ) = p;
549 fSucc = fp;
550 fp = succ;
551 }
552 else
553 {
554 Node *tmp = fp;
555 Node *succr = RLINK(succ);
556
557 RLINK(succ) = RLINK(p);
558 LLINK(fSucc) = p;
559 RLINK(p) = succr;
560 fp = fSucc;
561 fSucc = tmp;
562 }
563
564 Color tmp = COLOR(succ);
565 COLOR(succ) = COLOR(p);
566 COLOR(p) = tmp;
567
568 // Preserve count at successor's new position
569 COUNT(succ) = original_count;
570 }
571
573 static void findPredAndSwap(Node *p, Node *&fp) noexcept
574 {
575 assert(p != Node::NullPtr);
576 assert(LLINK(p) != Node::NullPtr);
577
578 size_t original_count = COUNT(p);
579
580 Node *fPred = p;
581 Node *pred = gotoLeftAndColorRed(p, fp);
582 Node *ffPred = fp;
583
584 while (RLINK(pred) != Node::NullPtr)
585 {
586 ffPred = fPred;
587 fPred = pred;
588 pred = gotoRightAndColorRed(fPred, ffPred);
589 }
590
591 if (LLINK(fp) == p)
592 LLINK(fp) = pred;
593 else
594 RLINK(fp) = pred;
595
596 RLINK(pred) = RLINK(p);
597 RLINK(p) = Node::NullPtr;
598
599 if (LLINK(p) == pred)
600 {
601 LLINK(p) = LLINK(pred);
602 LLINK(pred) = p;
603 fPred = fp;
604 fp = pred;
605 }
606 else
607 {
608 Node *tmp = fp;
609 Node *predl = LLINK(pred);
610
611 LLINK(pred) = LLINK(p);
612 RLINK(fPred) = p;
613 LLINK(p) = predl;
614 fp = fPred;
615 fPred = tmp;
616 }
617
618 Color tmp = COLOR(pred);
619 COLOR(pred) = COLOR(p);
620 COLOR(p) = tmp;
621
622 COUNT(pred) = original_count;
623 }
624
626 void colorRootAsRed() noexcept
627 {
628 if (COLOR(root) == RED)
629 return;
630
631 if (COLOR(LLINK(root)) == BLACK and COLOR(RLINK(root)) == BLACK)
632 COLOR(root) = RED;
633 }
634
639 Node* searchAndColorRed(const Key& key, Node *&fp,
640 Node** stack, size_t& stack_len)
641 noexcept(nothrow_compare_v<Compare, Key>)
642 {
643 Node *p = root;
644 fp = head;
645 Node *ffp = fHead;
646
647 stack_len = 0;
648
650 stack[stack_len++] = p;
651
652 while (true)
653 {
654 if (equals(key, KEY(p)))
655 return p;
656
657 ffp = fp;
658 fp = p;
659
660 if (less(key, KEY(p)))
661 {
662 if (LLINK(p) == Node::NullPtr)
663 return p; // Key not found, return current
664 p = gotoLeftAndColorRed(fp, ffp);
665 }
666 else
667 {
668 if (RLINK(p) == Node::NullPtr)
669 return p; // Key not found, return current
670 p = gotoRightAndColorRed(fp, ffp);
671 }
672
673 stack[stack_len++] = p;
674 }
675 }
676
678 static void removeAndRendLeafRed(Node *p, Node *fp) noexcept
679 {
680 assert(p != Node::NullPtr);
681 assert(fp != Node::NullPtr);
682
683 while (LLINK(p) != Node::NullPtr or RLINK(p) != Node::NullPtr)
684 {
685 if (RLINK(p) != Node::NullPtr)
686 findSuccAndSwap(p, fp);
687 else if (LLINK(p) != Node::NullPtr)
688 findPredAndSwap(p, fp);
689 }
690
691 if (LLINK(fp) == p)
692 LLINK(fp) = Node::NullPtr;
693 else
694 RLINK(fp) = Node::NullPtr;
695 }
696
698 void init() noexcept
699 {
700 RLINK(fHead) = head;
701 COLOR(Node::NullPtr) = BLACK;
702 COUNT(Node::NullPtr) = 0;
703 COLOR(head) = BLACK;
704 COUNT(head) = 0;
705 }
706
707public:
715 noexcept(std::is_nothrow_default_constructible_v<Node> and
716 std::is_nothrow_default_constructible_v<Compare>)
717 : head(&headNode), fHead(&headParent), root(headNode.getR()), cmp()
718 {
719 init();
720 }
721
728 explicit GenTdRbTreeRk(const Compare & __cmp)
729 noexcept(std::is_nothrow_default_constructible_v<Node> and
730 std::is_nothrow_copy_constructible_v<Compare>)
731 : head(&headNode), fHead(&headParent), root(headNode.getR()), cmp(__cmp)
732 {
733 init();
734 }
735
754 noexcept(bin_node_detail::nothrow_header_from_v<Node> and
755 bin_node_detail::nothrow_compare_taken_v<Compare>)
756 : headNode(bin_node_detail::header_from(other.headNode)),
757 headParent(bin_node_detail::header_from(other.headParent)),
759 cmp(bin_node_detail::compare_to_take(other.cmp))
760 {
761 init();
762 root = other.root;
763 other.root = Node::NullPtr;
764 }
765
774 noexcept(std::is_nothrow_move_assignable_v<Compare>)
775 {
776 if (this != &other)
777 {
778 // The comparison first: if its move throws, no tree has changed
779 cmp = std::move(other.cmp);
780 root = other.root;
781 other.root = Node::NullPtr;
782 }
783 return *this;
784 }
785
786 GenTdRbTreeRk(const GenTdRbTreeRk &) = delete;
788
790 void reset() noexcept
791 {
792 root = Node::NullPtr;
793 }
794
802 void swap(GenTdRbTreeRk & other) noexcept(std::is_nothrow_swappable_v<Compare>)
803 {
804 // The comparison first: if its swap throws, the nodes stay where
805 // they were
806 std::swap(cmp, other.cmp);
807 std::swap(root, other.root);
808 }
809
810 virtual ~GenTdRbTreeRk() = default;
811
813 size_t size() const noexcept { return COUNT(root); }
814
816 bool is_empty() const noexcept { return root == Node::NullPtr; }
817
819 Compare & get_compare() noexcept { return cmp; }
820 const Compare & get_compare() const noexcept { return cmp; }
821
832 Node* insert(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
833 {
834 assert(p != Node::NullPtr);
835 assert(COLOR(p) == RED);
836 COUNT(p) = 1;
837
838 if (root == Node::NullPtr)
839 {
840 root = p;
841 return p;
842 }
843
845 }
846
855 Node* insert_dup(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
856 {
857 assert(p != Node::NullPtr);
858 assert(COLOR(p) == RED);
859 COUNT(p) = 1;
860
861 if (root == Node::NullPtr)
862 {
863 root = p;
864 return p;
865 }
866
868 }
869
878 Node* search_or_insert(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
879 {
880 assert(p != Node::NullPtr);
881 assert(COLOR(p) == RED);
882 COUNT(p) = 1;
883
884 if (root == Node::NullPtr)
885 {
886 root = p;
887 return p;
888 }
889
890 Node *found = search(KEY(p));
891 if (found != nullptr)
892 return found;
893
895 }
896
905 Node* search(const Key& key) const noexcept(nothrow_compare_v<Compare, Key>)
906 {
907 Node* p = root;
908 while (p != Node::NullPtr)
909 {
910 if (equals(key, KEY(p)))
911 return p;
912 p = less(key, KEY(p)) ? LLINK(p) : RLINK(p);
913 }
914 return nullptr;
915 }
916
929 Node* remove(const Key& key) noexcept(nothrow_compare_v<Compare, Key>)
930 {
931 if (root == Node::NullPtr)
932 return nullptr;
933
935 size_t stack_len = 0;
936
937 Node *fp;
938 Node *p = searchAndColorRed(key, fp, stack, stack_len);
939
940 if (not equals(KEY(p), key))
941 return nullptr;
942
944
945 // Recalculate counts for visited nodes - usually O(log n)
946 // For complex cases with many swaps, fall back to full recalc
947 if (root != Node::NullPtr)
948 {
949 // The stack contains nodes visited during search
950 // After swaps, some may have moved, so do full recalc for safety
952 }
953
954 return p;
955 }
956
958 Node*& getRoot() noexcept { return root; }
959 Node* getRoot() const noexcept { return root; }
960
961 // ===================== RANK OPERATIONS =====================
962
968 Node* select(size_t i) const
969 {
970 return Aleph::select(root, i);
971 }
972
981 std::pair<long, Node*> position(const Key & key) const
982 noexcept(nothrow_compare_copy_v<Compare, Key>)
983 {
984 std::pair<long, Node*> ret_val;
986 inorder_position(root, key, ret_val.second);
987 return ret_val;
988 }
989
998 std::pair<long, Node*> find_position(const Key & key) const
999 noexcept(nothrow_compare_copy_v<Compare, Key>)
1000 {
1001 std::pair<long, Node*> r(-2, nullptr);
1003 find_position(root, key, r.second);
1004 return r;
1005 }
1006
1011 Node* remove_pos(size_t i)
1012 {
1013 Node* p = select(i);
1014 if (p == nullptr)
1015 return nullptr;
1016 return remove(KEY(p));
1017 }
1018
1029 void split_pos(size_t pos, GenTdRbTreeRk & t1, GenTdRbTreeRk & t2) noexcept
1030 {
1031 if (root == Node::NullPtr)
1032 return;
1033
1034 if (pos == 0)
1035 {
1036 t2.root = root;
1037 root = Node::NullPtr;
1038 return;
1039 }
1040
1041 if (pos >= size())
1042 {
1043 t1.root = root;
1044 root = Node::NullPtr;
1045 return;
1046 }
1047
1048 // Simple O(n) implementation
1049 size_t count = 0;
1050 split_pos_inorder(root, pos, t1, t2, count);
1051 root = Node::NullPtr;
1052 }
1053
1054private:
1055 void split_pos_inorder(Node *p, size_t pos,
1056 GenTdRbTreeRk & t1, GenTdRbTreeRk & t2,
1057 size_t & count) noexcept
1058 {
1059 if (p == Node::NullPtr)
1060 return;
1061
1062 Node *left = LLINK(p);
1063 Node *right = RLINK(p);
1064
1065 split_pos_inorder(left, pos, t1, t2, count);
1066
1067 p->reset();
1068 if (count < pos)
1069 t1.insert(p);
1070 else
1071 t2.insert(p);
1072 ++count;
1073
1074 split_pos_inorder(right, pos, t1, t2, count);
1075 }
1076
1077public:
1079 bool verify() const
1080 {
1082 }
1083
1090};
1091
1096template <typename Key, class Compare = Aleph::less<Key>>
1097class TdRbTreeRk : public GenTdRbTreeRk<RbNodeRk, Key, Compare>
1098{
1100public:
1101 using Base::Base;
1102};
1103
1108template <typename Key, class Compare = Aleph::less<Key>>
1109class TdRbTreeRkVtl : public GenTdRbTreeRk<RbNodeRkVtl, Key, Compare>
1110{
1112public:
1113 using Base::Base;
1114};
1115
1116} // namespace Aleph
1117
1118# endif /* TPL_TDRBTREERK_H */
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
@ KEY
Definition btreepic.C:169
Inorder iterator on the nodes of a binary tree.
Functor encompassing basic operation for extended binary search trees.
Top-down red-black tree with rank support (select/position).
bool less(const Key &a, const Key &b) const noexcept(nothrow_compare_v< Compare, Key >)
Returns true if a < b according to comparator; it throws what the comparator throws.
Node * select(size_t i) const
Select i-th node in order.
static void findPredAndSwap(Node *p, Node *&fp) noexcept
Find predecessor and swap for deletion.
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node allowing duplicates.
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node.
GenTdRbTreeRk(const GenTdRbTreeRk &)=delete
NodeType< Key > Node
Node * getRoot() const noexcept
Node * remove_pos(size_t i)
Remove node at position i.
static Node * rotate_to_left_rk(Node *p, Node *pp) noexcept
Rotate to left with counter update.
const Compare & get_compare() const noexcept
static Node * gotoRightAndColorRed(Node *fp, Node *&ffp) noexcept
Descend to right child for deletion, ensuring red.
std::pair< long, Node * > find_position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Find position of a key (even if not in tree).
std::pair< long, Node * > position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Find position of a key.
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search or insert.
Node headParent
Sentinel grandparent node.
void restoreRedCondition(Node *p, Node *&fp, Node *ffp, Node *fffp) noexcept
Restore red-black property after color flip.
static void flipColors(Node *p) noexcept
Flip colors with counter preservation.
Node *& getRoot() noexcept
Get root pointer.
GenTdRbTreeRk & operator=(GenTdRbTreeRk &&other) noexcept(std::is_nothrow_move_assignable_v< Compare >)
Takes the nodes and the comparison criteria of other, which is left empty.
static size_t updateCountRec(Node *p) noexcept
Recursively update counts in subtree - O(n)
static void updateCountsFromStack(Node **stack, size_t len) noexcept
Update counts for nodes in stack (bottom-up) - O(log n) After rotations, nodes may have moved but the...
static constexpr size_t PATH_STACK_CAPACITY
Node * searchFlipColorsAndInsert(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Search for insertion point, flipping colors proactively.
GenTdRbTreeRk() noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare >)
Builds an empty tree with a comparison criteria built by default.
void colorRootAsRed() noexcept
Color root red if safe.
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove node with given key.
Node headNode
Sentinel header node (parent of root)
Node * searchFlipColorsAndInsertDup(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Search for insertion point allowing duplicates.
static Node * rotate_to_right_rk(Node *p, Node *pp) noexcept
Rotate to right with counter update.
size_t size() const noexcept
Get number of nodes O(1)
void split_pos(size_t pos, GenTdRbTreeRk &t1, GenTdRbTreeRk &t2) noexcept
Split tree by position.
void reset() noexcept
Reset tree (does not free nodes)
Compare & get_compare() noexcept
Get comparator.
bool equals(const Key &a, const Key &b) const noexcept(nothrow_compare_v< Compare, Key >)
Returns true if a == b (neither a < b nor b < a); it throws what the comparator throws.
Compare cmp
Comparison functor.
static void findSuccAndSwap(Node *p, Node *&fp) noexcept
Find successor and swap for deletion.
Node *& root
Reference to root (right child of head)
static Node * gotoLeftAndColorRed(Node *fp, Node *&ffp) noexcept
Descend to left child for deletion, ensuring red.
Node * head
Pointer to header.
void init() noexcept
Initialize header nodes.
GenTdRbTreeRk & operator=(const GenTdRbTreeRk &)=delete
GenTdRbTreeRk(const Compare &__cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_copy_constructible_v< Compare >)
Builds an empty tree with the comparison criteria __cmp.
GenTdRbTreeRk(GenTdRbTreeRk &&other) 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 other, which is left empty and can take nodes again.
Node * searchAndColorRed(const Key &key, Node *&fp, Node **stack, size_t &stack_len) noexcept(nothrow_compare_v< Compare, Key >)
Search for key while coloring nodes red for deletion.
void swap(GenTdRbTreeRk &other) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and other.
bool verify() const
Verify tree properties. It throws what the comparator throws.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search for a key.
bool is_empty() const noexcept
Check if empty.
Node * fHead
Pointer to header's parent.
void split_pos_inorder(Node *p, size_t pos, GenTdRbTreeRk &t1, GenTdRbTreeRk &t2, size_t &count) noexcept
virtual ~GenTdRbTreeRk()=default
static void removeAndRendLeafRed(Node *p, Node *fp) noexcept
Remove node, making it a red leaf.
Top-down red-black tree with rank (virtual destructor).
Top-down red-black tree with rank (no virtual destructor).
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.
Freq_Node * pred
Predecessor node in level-order traversal.
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
Iterator(const GenTdRbTreeRk &t)
#define RLINK(i, n)
#define LLINK(i, n)
Gsl_Compatible_Rng r
Utility functions for binary tree operations.
Extended binary node with subtree count.
Basic binary tree node definitions.
Binary tree operations (split, join, rotate).