Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_hRbTreeRk.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
45# ifndef TPL_HTDRBTREERK_H
46# define TPL_HTDRBTREERK_H
47
48# ifdef DEBUG
49# include <cmath>
50# endif
51# include <stdexcept>
52# include <ah-errors.H>
53# include <tpl_binNode.H>
54# include <tpl_binNodeUtils.H>
55# include <tpl_binNodeXt.H>
56# include <tpl_binTreeOps.H>
57# include <tpl_arrayStack.H>
58# include <rbNodeRk.H>
59# include <ah-concepts.H>
60
61namespace Aleph
62{
63
92template <class Key, class Compare = Aleph::less<Key>>
93 requires StrictWeakOrder<Compare, Key>
95{
96public:
97 using Color = unsigned char;
99
100private:
104
109
110 /* The path from the head node to the node being removed. remove() builds
111
112 it, consumes it and drops it: it lives in the frame of the operation,
113
114 so the tree never allocates. */
115
116 // The head node, then at most Node::MaxHeight nodes of the tree.
118
119 Compare cmp;
120
121 // They throw what the comparison throws
122 bool less(const Key & k1, const Key & k2) const
123 noexcept(nothrow_compare_v<Compare, Key>)
124 { return cmp(k1, k2); }
125
126 bool equals(const Key & k1, const Key & k2) const
127 noexcept(nothrow_compare_v<Compare, Key>)
128 { return not static_cast<bool>(cmp(k1, k2)) and not static_cast<bool>(cmp(k2, k1)); }
129
130 static Node * getSibling(Node *p, Node *fp) noexcept
131 {
132 assert(LLINK(fp) == p || RLINK(fp) == p);
133 return LLINK(fp) == p ? RLINK(fp) : LLINK(fp);
134 }
135
136 // ===================== RANK-AWARE ROTATIONS =====================
137
145 static Node * rotate_to_right_rk(Node *p, Node *fp) noexcept
146 {
147 assert(p != Node::NullPtr);
148 assert(LLINK(p) != Node::NullPtr);
149
150 Node *q = LLINK(p);
151
152 // q takes p's count (it will be in p's position)
153 COUNT(q) = COUNT(p);
154
155 LLINK(p) = RLINK(q);
156 RLINK(q) = p;
157
158 // Update p's count based on its new children
159 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
160
161 if (LLINK(fp) == p)
162 LLINK(fp) = q;
163 else
164 RLINK(fp) = q;
165
166 return q;
167 }
168
170 static Node * rotate_to_left_rk(Node *p, Node *fp) noexcept
171 {
172 assert(p != Node::NullPtr);
173 assert(RLINK(p) != Node::NullPtr);
174
175 Node *q = RLINK(p);
176
177 COUNT(q) = COUNT(p);
178
179 RLINK(p) = LLINK(q);
180 LLINK(q) = p;
181
182 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
183
184 if (LLINK(fp) == p)
185 LLINK(fp) = q;
186 else
187 RLINK(fp) = q;
188
189 return q;
190 }
191
192 // ===================== INSERTION ROUTINES =====================
193
194 void restoreRedCondition(Node *p, Node *& fp, Node *ffp, Node *fffp) noexcept
195 {
196 assert(LLINK(fp) == p || RLINK(fp) == p);
197 assert(COLOR(fp) == RED);
198 assert(COLOR(p) == RED);
199
200 if (fp == root)
201 {
202 COLOR(fp) = BLACK;
203 return;
204 }
205
206 assert(LLINK(ffp) == fp || RLINK(ffp) == fp);
207 assert(COLOR(ffp) == BLACK);
208 assert(LLINK(fffp) == ffp || RLINK(fffp) == ffp);
209
210 COLOR(ffp) = RED;
211
212 if (LLINK(fp) == p && LLINK(ffp) == fp)
213 {
214 COLOR(fp) = BLACK;
215 rotate_to_right_rk(ffp, fffp);
216 }
217 else if (RLINK(fp) == p && RLINK(ffp) == fp)
218 {
219 COLOR(fp) = BLACK;
220 rotate_to_left_rk(ffp, fffp);
221 }
222 else
223 {
224 COLOR(p) = BLACK;
225 if (RLINK(fp) == p)
226 {
227 rotate_to_left_rk(fp, ffp);
228 rotate_to_right_rk(ffp, fffp);
229 }
230 else
231 {
232 rotate_to_right_rk(fp, ffp);
233 rotate_to_left_rk(ffp, fffp);
234 }
235 fp = fffp;
236 }
237 }
238
239 static void flipColors(Node *p) noexcept
240 {
241 assert(p != Node::NullPtr);
242 assert(COLOR(p) == BLACK);
243 assert(COLOR(LLINK(p)) == RED && COLOR(RLINK(p)) == RED);
244
245 COLOR(p) = RED;
246 COLOR(LLINK(p)) = COLOR(RLINK(p)) = BLACK;
247 }
248
258 noexcept(nothrow_compare_v<Compare, Key>)
259 {
260 assert(q != Node::NullPtr);
261 assert(root != Node::NullPtr);
262 assert(COLOR(q) == RED);
263 assert(COUNT(q) == 1);
264
265 const Key & qk = KEY(q);
266
267 // Track path for count updates
268 Node* insertPath[128];
269 size_t insertPathLen = 0;
270
271 Node *p = root;
272 Node *fp = head;
273 Node *ffp = fHead;
274 Node *fffp = ffHead;
275 Node *nextNode;
276
277 insertPath[insertPathLen++] = p;
278
279 while (true)
280 {
281 const Key & pk = KEY(p);
282
283 if (equals(qk, pk)) [[unlikely]]
284 return nullptr; // Duplicate key
285
286 if (COLOR(p) == BLACK && COLOR(LLINK(p)) == RED
287 && COLOR(RLINK(p)) == RED)
288 {
289 flipColors(p);
290 if (COLOR(fp) == RED)
291 {
292 assert(fffp != Node::NullPtr);
293 restoreRedCondition(p, fp, ffp, fffp);
294 }
295 }
296
297 if (less(qk, pk))
298 {
299 if (LLINK(p) == Node::NullPtr)
300 break;
301 nextNode = LLINK(p);
302 }
303 else
304 {
305 if (RLINK(p) == Node::NullPtr)
306 break;
307 nextNode = RLINK(p);
308 }
309
310 fffp = ffp;
311 ffp = fp;
312 fp = p;
313 p = nextNode;
314 insertPath[insertPathLen++] = p;
315 }
316
317 // Perform insertion
318 const Key & pk = KEY(p);
319 if (less(qk, pk))
320 LLINK(p) = q;
321 else
322 RLINK(p) = q;
323
324 // Update counts bottom-up - O(log n). This goes before the last
325 // rotations: they keep the counts right only if they find them right,
326 // and a double rotation leaves q, which is not in the path, above p.
327 for (size_t i = insertPathLen; i > 0; --i)
328 {
329 Node* node = insertPath[i - 1];
330 COUNT(node) = COUNT(LLINK(node)) + COUNT(RLINK(node)) + 1;
331 }
332
333 if (COLOR(p) == RED)
334 restoreRedCondition(q, p, fp, ffp);
335
336 return q;
337 }
338
339 // As searchFlipColorsAndInsert(), a comparison that throws leaves a
340 // valid tree without q
342 noexcept(nothrow_compare_v<Compare, Key>)
343 {
344 assert(q != Node::NullPtr);
345 assert(root != Node::NullPtr);
346 assert(COLOR(q) == RED);
347 assert(COUNT(q) == 1);
348
349 const Key & qk = KEY(q);
350
351 Node* insertPath[128];
352 size_t insertPathLen = 0;
353
354 Node *p = root;
355 Node *fp = head;
356 Node *ffp = fHead;
357 Node *fffp = ffHead;
358 Node *nextNode;
359
360 insertPath[insertPathLen++] = p;
361
362 while (true)
363 {
364 const Key & pk = KEY(p);
365
366 if (COLOR(p) == BLACK && COLOR(LLINK(p)) == RED
367 && COLOR(RLINK(p)) == RED)
368 {
369 flipColors(p);
370 if (COLOR(fp) == RED)
371 {
372 assert(fffp != Node::NullPtr);
373 restoreRedCondition(p, fp, ffp, fffp);
374 }
375 }
376
377 if (less(qk, pk))
378 {
379 if (LLINK(p) == Node::NullPtr)
380 break;
381 nextNode = LLINK(p);
382 }
383 else
384 {
385 if (RLINK(p) == Node::NullPtr)
386 break;
387 nextNode = RLINK(p);
388 }
389
390 fffp = ffp;
391 ffp = fp;
392 fp = p;
393 p = nextNode;
394 insertPath[insertPathLen++] = p;
395 }
396
397 const Key & pk = KEY(p);
398 if (less(qk, pk))
399 LLINK(p) = q;
400 else
401 RLINK(p) = q;
402
403 // Update counts bottom-up, before the last rotations (see
404 // searchFlipColorsAndInsert())
405 for (size_t i = insertPathLen; i > 0; --i)
406 {
407 Node* node = insertPath[i - 1];
408 COUNT(node) = COUNT(LLINK(node)) + COUNT(RLINK(node)) + 1;
409 }
410
411 if (COLOR(p) == RED)
412 restoreRedCondition(q, p, fp, ffp);
413
414 return q;
415 }
416
417 // ===================== DELETION ROUTINES =====================
418
419 // It only reads the tree: a comparison that throws leaves it as it was
420 Node * searchAndBuildPath(const Key & key, Path & path)
421 noexcept(nothrow_compare_v<Compare, Key>)
422 {
423 assert(root != Node::NullPtr);
424
425 Node *p = root;
426 path.push(head);
427
428 while (true)
429 {
430 path.push(p);
431
432 const Key & pk = KEY(p);
433
434 if (equals(key, pk))
435 return p;
436
437 if (less(key, pk))
438 {
439 if (LLINK(p) == Node::NullPtr)
440 return p;
441 p = LLINK(p);
442 continue;
443 }
444
445 if (RLINK(p) == Node::NullPtr)
446 return p;
447
448 p = RLINK(p);
449 }
450 }
451
452 void findSuccAndSwap(Node *p, Node *& fp, Path & path) noexcept
453 {
454 assert(p != Node::NullPtr);
455 assert(RLINK(p) != Node::NullPtr);
456 assert(fp != Node::NullPtr);
457 assert(LLINK(fp) == p || RLINK(fp) == p);
458
459 Node *& refToSearchPath = path.top();
460 assert(refToSearchPath == p);
461
462 Node *fSucc = p;
463 Node *succ = RLINK(p);
464
465 path.push(succ);
466 while (LLINK(succ) != Node::NullPtr)
467 {
468 fSucc = succ;
469 succ = LLINK(succ);
470 path.push(succ);
471 }
472
473 refToSearchPath = succ;
474 path.top() = p;
475
476 if (LLINK(fp) == p)
477 LLINK(fp) = succ;
478 else
479 RLINK(fp) = succ;
480
481 LLINK(succ) = LLINK(p);
482 LLINK(p) = Node::NullPtr;
483
484 if (RLINK(p) == succ)
485 {
486 RLINK(p) = RLINK(succ);
487 RLINK(succ) = p;
488 fp = succ;
489 }
490 else
491 {
492 Node *succr = RLINK(succ);
493 RLINK(succ) = RLINK(p);
494 LLINK(fSucc) = p;
495 RLINK(p) = succr;
496 fp = fSucc;
497 }
498
499 Color tmp = COLOR(succ);
500 COLOR(succ) = COLOR(p);
501 COLOR(p) = tmp;
502 // Counts will be recalculated after removal completes
503 }
504
505 void balanceDownAndColor(Node *p, Node *& fp, Node *& sp, Path & path) noexcept
506 {
507 assert(LLINK(fp) == p || RLINK(fp) == p);
508 assert(LLINK(fp) == sp || RLINK(fp) == sp);
509 assert(COLOR(fp) == BLACK);
510 assert(COLOR(sp) == RED);
511 assert(COLOR(p) == BLACK);
512 assert(!path.is_empty());
513
514 Node *& ffp = path.top();
515 assert(LLINK(ffp) == fp || RLINK(ffp) == fp);
516
517 if (LLINK(fp) == p)
518 {
519 sp = LLINK(sp);
520 ffp = rotate_to_left_rk(fp, ffp);
521 }
522 else
523 {
524 sp = RLINK(sp);
525 ffp = rotate_to_right_rk(fp, ffp);
526 }
527
528 assert(LLINK(fp) == sp || RLINK(fp) == sp);
529 assert(COLOR(ffp) == RED);
530
531 COLOR(ffp) = BLACK;
532 COLOR(fp) = RED;
533 }
534
535 void rotateNephewAndColor(Node *fp, Node *sp, Node *np, Path & path) noexcept
536 {
537 assert(LLINK(fp) == sp || RLINK(fp) == sp);
538 assert(LLINK(sp) == np || RLINK(sp) == np);
539 assert(COLOR(sp) == BLACK);
540 assert(COLOR(np) == RED);
541 assert(!path.is_empty());
542
543 Node *ffp = path.top();
544 assert(LLINK(ffp) == fp || RLINK(ffp) == fp);
545
546 if (RLINK(sp) == np)
547 rotate_to_left_rk(fp, ffp);
548 else
549 rotate_to_right_rk(fp, ffp);
550
551 COLOR(sp) = COLOR(fp);
552 COLOR(fp) = BLACK;
553 COLOR(np) = BLACK;
554 }
555
556 void doubleRotateNephewAndColor(Node *fp, Node *sp, Node *snp, Path & path) noexcept
557 {
558 assert(LLINK(fp) == sp || RLINK(fp) == sp);
559 assert(LLINK(sp) == snp || RLINK(sp) == snp);
560 assert(COLOR(sp) == BLACK);
561 assert(COLOR(snp) == RED);
562 assert(!path.is_empty());
563
564 Node *ffp = path.top();
565 assert(LLINK(ffp) == fp || RLINK(ffp) == fp);
566
567 if (LLINK(sp) == snp)
568 {
569 rotate_to_right_rk(sp, fp);
570 rotate_to_left_rk(fp, ffp);
571 }
572 else
573 {
574 rotate_to_left_rk(sp, fp);
575 rotate_to_right_rk(fp, ffp);
576 }
577
578 COLOR(snp) = COLOR(fp);
579 COLOR(fp) = BLACK;
580 }
581
582 static void colorSiblingAsRed(Node *sp) noexcept
583 {
584 assert(COLOR(sp) == BLACK);
585 COLOR(sp) = RED;
586 }
587
588 static void colorParentAndSibling(Node *fp, Node *sp) noexcept
589 {
590 assert(LLINK(fp) == sp || RLINK(fp) == sp);
591 assert(COLOR(fp) == RED);
592 assert(COLOR(sp) == BLACK);
593
594 COLOR(fp) = BLACK;
595 COLOR(sp) = RED;
596 }
597
599 static void updateCountRec(Node * p) noexcept
600 {
601 if (p == Node::NullPtr)
602 return;
605 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
606 }
607
614 void removeAndFixBlackCondition(Node *q, Path & path) noexcept
615 {
616 assert(path.top() == q);
617
618 Node *fq = path.top(1);
619 Node *p;
620
621 assert(fq != Node::NullPtr);
622 assert(LLINK(fq) == q || RLINK(fq) == q);
623
624 while (1)
625 {
626 if (LLINK(q) == Node::NullPtr)
627 {
628 if (LLINK(fq) == q)
629 p = LLINK(fq) = RLINK(q);
630 else
631 p = RLINK(fq) = RLINK(q);
632 break;
633 }
634
635 if (RLINK(q) == Node::NullPtr)
636 {
637 if (LLINK(fq) == q)
638 p = LLINK(fq) = LLINK(q);
639 else
640 p = RLINK(fq) = LLINK(q);
641 break;
642 }
643
644 findSuccAndSwap(q, fq, path);
645 }
646
647 if (COLOR(q) == RED)
648 {
649 assert(COLOR(p) == BLACK);
650 if (root != Node::NullPtr)
652 return;
653 }
654
655 if (COLOR(p) == RED)
656 {
657 COLOR(p) = BLACK;
658 if (root != Node::NullPtr)
660 return;
661 }
662
663 Node *np, *snp;
664 Node *fp = fq;
665
666 path.popn(2);
667
668 while (true)
669 {
670 if (p == root)
671 break;
672
673 Node *sp = getSibling(p, fp);
674
675 if (COLOR(sp) == RED)
676 balanceDownAndColor(p, fp, sp, path);
677
678 assert(COLOR(sp) == BLACK);
679
680 if (LLINK(fp) == p)
681 {
682 np = RLINK(sp);
683 snp = LLINK(sp);
684 }
685 else
686 {
687 np = LLINK(sp);
688 snp = RLINK(sp);
689 }
690
691 if (COLOR(np) == RED)
692 {
693 rotateNephewAndColor(fp, sp, np, path);
694 break;
695 }
696
697 if (COLOR(snp) == RED)
698 {
699 doubleRotateNephewAndColor(fp, sp, snp, path);
700 break;
701 }
702
703 if (COLOR(fp) == RED)
704 {
705 colorParentAndSibling(fp, sp);
706 break;
707 }
708
710 p = fp;
711 fp = path.pop();
712 }
713
714 // Update all counts after removal - O(n)
715 if (root != Node::NullPtr)
717 }
718
719 void init() noexcept
720 {
721 RLINK(fHead) = head;
722 RLINK(ffHead) = fHead;
724 COUNT(Node::NullPtr) = 0;
725 COLOR(head) = BLACK;
726 COUNT(head) = 0;
727 COLOR(fHead) = BLACK;
728 COUNT(fHead) = 0;
729 COLOR(ffHead) = BLACK;
730 COUNT(ffHead) = 0;
731 }
732
733public:
734 using key_type = Key;
735
736 Compare & key_comp() noexcept { return cmp; }
737 const Compare & key_comp() const noexcept { return cmp; }
738 Compare & get_compare() noexcept { return cmp; }
739 [[nodiscard]] constexpr const Compare & get_compare() const noexcept { return cmp; }
740
755 noexcept(std::is_nothrow_default_constructible_v<Node> and
756 std::is_nothrow_default_constructible_v<Compare> and
757 std::is_nothrow_constructible_v<Compare, Compare &>)
758 : HtdRbTreeRk(Compare())
759 {
760 // empty
761 }
762
771 HtdRbTreeRk(Compare __cmp)
772 noexcept(std::is_nothrow_default_constructible_v<Node> and
773 std::is_nothrow_constructible_v<Compare, Compare &>)
774 : head(&headNode),
777 root(headNode.getR()),
778 cmp(__cmp)
779 {
780 init();
781 }
782
790 void swap(HtdRbTreeRk & tree) noexcept(std::is_nothrow_swappable_v<Compare>)
791 {
792 // The comparison first: if its swap throws, the nodes stay where
793 // they were
794 std::swap(cmp, tree.cmp);
795 std::swap(root, tree.root);
796 }
797
816 noexcept(bin_node_detail::nothrow_header_from_v<Node> and
817 bin_node_detail::nothrow_compare_taken_v<Compare>)
818 : headNode(bin_node_detail::header_from(tree.headNode)),
819 headParent(bin_node_detail::header_from(tree.headParent)),
820 headGrandParent(bin_node_detail::header_from(tree.headGrandParent)),
821 head(&headNode),
824 root(headNode.getR()),
825 cmp(bin_node_detail::compare_to_take(tree.cmp))
826 {
827 init();
828 root = tree.root;
829 tree.root = Node::NullPtr;
830 }
831
840 noexcept(std::is_nothrow_swappable_v<Compare>)
841 {
842 swap(tree);
843 return *this;
844 }
845
846 HtdRbTreeRk(const HtdRbTreeRk &) = delete;
847 HtdRbTreeRk & operator=(const HtdRbTreeRk &) = delete;
848
849 virtual ~HtdRbTreeRk() = default;
850
851 [[nodiscard]] constexpr bool is_empty() const noexcept { return root == Node::NullPtr; }
852
854 size_t size() const noexcept { return COUNT(root); }
855
856 void reset() noexcept { root = Node::NullPtr; }
857
869 Node * insert(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
870 {
871 assert(p != Node::NullPtr);
872 assert(COLOR(p) == RED);
873 COUNT(p) = 1;
874
875 if (root == Node::NullPtr) [[unlikely]]
876 {
877 root = p;
878 return p;
879 }
880
882 }
883
893 Node * search_or_insert(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
894 {
895 assert(p != Node::NullPtr);
896 assert(COLOR(p) == RED);
897 COUNT(p) = 1;
898
899 if (root == Node::NullPtr) [[unlikely]]
900 {
901 root = p;
902 return p;
903 }
904
905 const Key & pk = KEY(p);
906 Node *found = search(pk);
907 if (found != nullptr) [[unlikely]]
908 return found;
909
910 return insert(p);
911 }
912
922 Node * insert_dup(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
923 {
924 assert(p != Node::NullPtr);
925 assert(COLOR(p) == RED);
926 COUNT(p) = 1;
927
928 if (root == Node::NullPtr) [[unlikely]]
929 {
930 root = p;
931 return p;
932 }
933
935 }
936
946 Node * search(const Key & key) const noexcept(nothrow_compare_v<Compare, Key>)
947 {
948 Node *p = root;
949 while (p != Node::NullPtr)
950 {
951 const Key & pk = KEY(p);
952 if (equals(key, pk))
953 return p;
954 p = less(key, pk) ? LLINK(p) : RLINK(p);
955 }
956 return nullptr;
957 }
958
968 Node * remove(const Key & key) noexcept(nothrow_compare_v<Compare, Key>)
969 {
970 if (root == Node::NullPtr) [[unlikely]]
971 return nullptr;
972
973 Path path;
974
975 Node *p = searchAndBuildPath(key, path);
976
977 if (not equals(KEY(p), key))
978 {
979 return nullptr;
980 }
981
983
984 p->reset();
985
986 return p;
987 }
988
989 Node *& getRoot() noexcept { return root; }
990 Node * getRoot() const noexcept { return root; }
991
992 // ===================== RANK OPERATIONS =====================
993
1000 Node * select(size_t i) const
1001 {
1002 return Aleph::select(root, i);
1003 }
1004
1014 std::pair<long, Node*> position(const Key & key) const
1015 noexcept(nothrow_compare_copy_v<Compare, Key>)
1016 {
1017 std::pair<long, Node*> ret_val;
1018 ret_val.first = BinTreeXt_Operation<Node, Compare>(cmp).
1019 inorder_position(root, key, ret_val.second);
1020 return ret_val;
1021 }
1022
1034 std::pair<long, Node*> find_position(const Key & key) const
1035 noexcept(nothrow_compare_copy_v<Compare, Key>)
1036 {
1037 std::pair<long, Node*> r;
1039 find_position(root, key, r.second);
1040 return r;
1041 }
1042
1049 Node * remove_pos(size_t i)
1050 {
1051 ah_out_of_range_error_if(i >= size()) << "remove_pos: position " << i << " out of range";
1052 Node * p = select(i);
1053 return remove(KEY(p));
1054 }
1055
1073 void split_pos(size_t pos, HtdRbTreeRk & t1, HtdRbTreeRk & t2)
1074 {
1075 ah_out_of_range_error_if(pos > size()) << "split_pos: position " << pos << " out of range";
1076
1077 t1.reset();
1078 t2.reset();
1079
1080 // A comparison that threw between the removal of a node and its
1081 // insertion would lose the node: the move ends the program instead
1082 [&] () noexcept
1083 {
1084 while (root != Node::NullPtr)
1085 {
1086 Node * p = remove_pos(0);
1087 if (t1.size() < pos)
1088 t1.insert(p);
1089 else
1090 t2.insert(p);
1091 }
1092 } ();
1093 }
1094
1100 bool verify() const
1101 {
1102 if (root == Node::NullPtr)
1103 return true;
1104
1105 // Verify red-black properties
1107 return false;
1108
1109 // Verify counts
1110 return verifyCountsRec(root);
1111 }
1112
1113private:
1114 static bool verifyCountsRec(Node * p) noexcept
1115 {
1116 if (p == Node::NullPtr)
1117 return true;
1118
1119 size_t expected = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
1120 if (COUNT(p) != expected)
1121 return false;
1122
1123 return verifyCountsRec(LLINK(p)) && verifyCountsRec(RLINK(p));
1124 }
1125
1126public:
1128 struct Iterator : public BinNodeInfixIterator<Node>
1129 {
1130 Iterator(HtdRbTreeRk & t) noexcept
1131 : BinNodeInfixIterator<Node>(t.getRoot()) {}
1132
1133 Iterator(const HtdRbTreeRk & t) noexcept
1134 : BinNodeInfixIterator<Node>(const_cast<HtdRbTreeRk &>(t).getRoot()) {}
1135 };
1136};
1137
1139template <class Key, class Compare = Aleph::less<Key>>
1140 requires StrictWeakOrder<Compare, Key>
1141class HtdRbTreeRkVtl : public HtdRbTreeRk<Key, Compare>
1142{
1143public:
1145 using Node = typename Base::Node;
1146 using Base::Base;
1147};
1148
1149} // namespace Aleph
1150
1151#endif /* TPL_HTDRBTREERK_H */
1152
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
@ KEY
Definition btreepic.C:169
Inorder iterator on the nodes of a binary tree.
Functor encompassing basic operation for extended binary search trees.
Hybrid RB tree with rank and virtual destructor.
typename Base::Node Node
Hybrid top-down/bottom-up red-black tree with rank support.
void init() noexcept
const Compare & key_comp() const noexcept
void restoreRedCondition(Node *p, Node *&fp, Node *ffp, Node *fffp) noexcept
constexpr const Compare & get_compare() const noexcept
static void colorParentAndSibling(Node *fp, Node *sp) noexcept
static Node * getSibling(Node *p, Node *fp) noexcept
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node into the tree.
Node * searchAndBuildPath(const Key &key, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
Node * searchFlipColorsAndInsertDup(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
HtdRbTreeRk & operator=(HtdRbTreeRk &&tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps the nodes and the comparison criteria with tree.
unsigned char Color
void rotateNephewAndColor(Node *fp, Node *sp, Node *np, Path &path) noexcept
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search for key or insert if not found.
static void flipColors(Node *p) noexcept
static Node * rotate_to_left_rk(Node *p, Node *fp) noexcept
Left rotation with count update.
virtual ~HtdRbTreeRk()=default
HtdRbTreeRk() 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.
void balanceDownAndColor(Node *p, Node *&fp, Node *&sp, Path &path) noexcept
bool equals(const Key &k1, const Key &k2) const noexcept(nothrow_compare_v< Compare, Key >)
static bool verifyCountsRec(Node *p) noexcept
void reset() noexcept
Compare & key_comp() noexcept
static void updateCountRec(Node *p) noexcept
Recursively update counts for entire subtree - O(n)
Node *& getRoot() noexcept
std::pair< long, Node * > find_position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Find position where key would be inserted.
size_t size() const noexcept
O(1) size using root count.
void split_pos(size_t pos, HtdRbTreeRk &t1, HtdRbTreeRk &t2)
Split tree at position.
Node * select(size_t i) const
Select the i-th smallest element (0-indexed).
Node * searchFlipColorsAndInsert(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Insert with stack tracking for count updates.
HtdRbTreeRk & operator=(const HtdRbTreeRk &)=delete
Node * getRoot() const noexcept
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search for a key.
HtdRbTreeRk(Compare __cmp) 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.
bool less(const Key &k1, const Key &k2) const noexcept(nothrow_compare_v< Compare, Key >)
RbNodeRk< Key > Node
void findSuccAndSwap(Node *p, Node *&fp, Path &path) noexcept
void doubleRotateNephewAndColor(Node *fp, Node *sp, Node *snp, Path &path) noexcept
Compare & get_compare() noexcept
HtdRbTreeRk(HtdRbTreeRk &&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.
std::pair< long, Node * > position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Find position of a key.
constexpr bool is_empty() const noexcept
void swap(HtdRbTreeRk &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and tree.
HtdRbTreeRk(const HtdRbTreeRk &)=delete
bool verify() const
Verify red-black and rank invariants.
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove node with given key.
static Node * rotate_to_right_rk(Node *p, Node *fp) noexcept
Right rotation with count update.
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert allowing duplicate keys.
static void colorSiblingAsRed(Node *sp) noexcept
Node * remove_pos(size_t i)
Remove element at position i.
void removeAndFixBlackCondition(Node *q, Path &path) noexcept
Remove node and fix red-black violations.
Stack of fixed capacity whose storage is inside the object.
Extended Red-Black node with subtree counter.
Definition rbNodeRk.H:84
void reset() noexcept
Definition rbNodeRk.H:84
static RbNodeRk *const NullPtr
Definition rbNodeRk.H:84
Minimal std::expected-style result type for C++20.
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.
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 HtdRbTreeRk &t) noexcept
Iterator(HtdRbTreeRk &t) noexcept
#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.
Basic binary tree node definitions.
Binary tree operations (split, join, rotate).