Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_binNodeXt.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
43# ifndef TPL_BINNODEXT_H
44# define TPL_BINNODEXT_H
45
46# include <limits>
47
48# include <ah-concepts.H>
49
50# include <tpl_binNode.H>
51# include <tpl_binNodeUtils.H>
52
53using namespace Aleph;
54
55namespace Aleph {
56
58{
59 size_t count; // Cardinality of the tree
60
61public:
62
63 BinNodeXt_Data() noexcept : count(1) {}
65
66 size_t & getCount() noexcept { return count; }
67 size_t size() const noexcept { return count; }
68
69 void reset() noexcept { count = 1; }
70};
71
79
85template <RankedBinNodeLike Node> inline auto & COUNT(Node * p) noexcept
86{
87 return p->getCount();
88}
89
90
91 template <RankedBinNodeLike Node> static inline
92Node * __select_rec(Node * r, const size_t i) noexcept
93{
94 assert(r != Node::NullPtr);
95 assert(COUNT(Node::NullPtr) == 0);
96
97 if (i == COUNT(LLINK(r)))
98 return r;
99
100 if (i < COUNT(LLINK(r)))
101 return __select_rec(LLINK(r), i);
102
103 return __select_rec(RLINK(r), i - COUNT(LLINK(r)) - 1);
104}
105
117 template <RankedBinNodeLike Node> inline
118Node * select_rec(Node * r, const size_t i)
119{
120 ah_out_of_range_error_if(i >= COUNT(r)) << "infix position out of range";
121
122 return __select_rec(r, i);
123}
124
135 template <RankedBinNodeLike Node> inline
136Node * select_ne(Node * r, const size_t pos) noexcept
137{
138 assert(COUNT(Node::NullPtr) == 0);
139 for (size_t i = pos; i != COUNT(LLINK(r)); /* nothing */)
140 {
141 assert(i < COUNT(r) and
142 COUNT(LLINK(r)) + COUNT(RLINK(r)) + 1 == COUNT(r));
143
144 if (i < COUNT(LLINK(r)))
145 r = LLINK(r);
146 else
147 {
148 i -= COUNT(LLINK(r)) + 1;
149 r = RLINK(r);
150 }
151 }
152
153 return r;
154}
155
167 template <RankedBinNodeLike Node> inline
168Node * select(Node * r, const size_t pos)
169{
170 ah_out_of_range_error_if(pos >= COUNT(r)) << "infix position out of range";
171
172 return select_ne(r, pos);
173}
174
188 template <RankedBinNodeLike Node> inline
189Node * select(Node * root, const size_t pos, Node *& parent)
190{
191 ah_out_of_range_error_if(pos >= COUNT(root)) << "infix position out of range";
192
193 parent = Node::NullPtr;
194 for (size_t i = pos; i != COUNT(LLINK(root)); /* nada */)
195 {
196 assert(i < COUNT(root) and
197 COUNT(LLINK(root)) + COUNT(RLINK(root)) + 1 == COUNT(root));
198
199 parent = root;
200 if (i < COUNT(LLINK(root)))
201 root = LLINK(root);
202 else
203 {
204 i -= COUNT(LLINK(root)) + 1;
205 root = RLINK(root);
206 }
207 }
208
209 return root;
210}
211
225 template <RankedBinNodeLike Node, class Compare> inline
227 const typename Node::key_type & key,
228 Node *& p, Compare & cmp)
229 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
230{
231 assert(COUNT(Node::NullPtr) == 0);
232
233 if (r == Node::NullPtr)
234 return -1;
235
236 if (cmp(key, KEY(r)))
237 return inorder_position(LLINK(r), key, p, cmp);
238 if (cmp(KEY(r), key))
239 {
240 long ret = inorder_position(RLINK(r), key, p, cmp);
241 if (ret != -1)
242 return ret + COUNT(LLINK(r)) + 1;
243 return ret;
244 }
245 p = r;
246 return COUNT(LLINK(r));
247}
248
250template <BinNodeLike Node, class Compare = Aleph::less<typename Node::key_type>>
251 inline long inorder_position(Node * r,
252 const typename Node::key_type & key,
253 Node *& p, Compare && cmp = Compare())
254 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
255{
256 return inorder_position(r, key, p, cmp);
257}
258
260 template <BinNodeLike Node, class Compare> inline
261long inorder_position(Node * r, const typename Node::key_type & key,
262 Compare & cmp)
263 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
264{
265 Node * p = nullptr;
266 return inorder_position(r, key, p, cmp);
267}
268
270 template <BinNodeLike Node, class Compare = Aleph::less<typename Node::key_type>>
271inline long inorder_position(Node * r, const typename Node::key_type & key,
272 Compare && cmp = Compare())
273 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
274{
275 return inorder_position(r, key, cmp);
276}
277
307 template <RankedBinNodeLike Node,
308 class Compare = Aleph::less<typename Node::key_type>> inline
309long find_position(Node * r, const typename Node::key_type & key,
310 Node *& p, Compare & cmp)
311 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
312{
313 assert(COUNT(Node::NullPtr) == 0);
314
315 long offset = 0;
316 Node * candidate = Node::NullPtr;
317
318 while (r != Node::NullPtr)
319 if (cmp(key, KEY(r)))
320 {
321 candidate = r;
322 r = LLINK(r);
323 }
324 else if (cmp(KEY(r), key))
325 {
326 offset += static_cast<long>(COUNT(LLINK(r)) + 1);
327 r = RLINK(r);
328 }
329 else
330 {
331 p = r;
332 return offset + static_cast<long>(COUNT(LLINK(r)));
333 }
334
335 if (candidate != Node::NullPtr)
336 {
337 p = candidate;
338 return offset + static_cast<long>(COUNT(LLINK(candidate))) - 1;
339 }
340
341 p = Node::NullPtr;
342 return offset - 1;
343}
344
346template <BinNodeLike Node,
347 class Compare = Aleph::less<typename Node::key_type>> inline
348long find_position(Node * r, const typename Node::key_type & key,
349 Node *& p, Compare && cmp = Compare())
350 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
351{
352 return find_position(r, key, p, cmp);
353}
354
373template <RankedBinNodeLike Node, class Compare> inline
374long inorder_position_of_node(Node * r, Node * p, Compare & cmp)
375 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
376{
377 const typename Node::key_type & key = KEY(p);
378 long offset = 0;
379 while (r != Node::NullPtr)
380 if (r == p)
381 return offset + static_cast<long>(COUNT(LLINK(r)));
382 else if (cmp(key, KEY(r)))
383 r = LLINK(r);
384 else if (cmp(KEY(r), key))
385 {
386 offset += static_cast<long>(COUNT(LLINK(r)) + 1);
387 r = RLINK(r);
388 }
389 else // an equivalent key: p is on one of its sides
390 {
391 const long pos = inorder_position_of_node(LLINK(r), p, cmp);
392 if (pos >= 0)
393 return offset + pos;
394 offset += static_cast<long>(COUNT(LLINK(r)) + 1);
395 r = RLINK(r);
396 }
397 return -1;
398}
399
413 template <RankedBinNodeLike Node, class Compare> inline
414Node * insert_by_key_xt(Node *& r, Node * p, Compare & cmp) noexcept
415{
416 assert(COUNT(Node::NullPtr) == 0);
417
418 if (r == Node::NullPtr)
419 return r = p;
420
421 Node * q = Node::NullPtr;
422 if (cmp(KEY(p), KEY(r)))
423 {
424 q = insert_by_key_xt(LLINK(r), p, cmp);
425 if (q != Node::NullPtr)
426 ++COUNT(r);
427 }
428 else if (cmp(KEY(r), KEY(p)))
429 {
430 q = insert_by_key_xt(RLINK(r), p, cmp);
431 if (q != Node::NullPtr)
432 ++COUNT(r);
433 }
434 // else return Node::NullPtr; is not needed
435
436 return q;
437}
438
440 template <BinNodeLike Node,
441 class Compare = Aleph::less<typename Node::key_type>> inline
442Node * insert_by_key_xt(Node *& r, Node * p, Compare && cmp = Compare())
443 noexcept
444{
445 return insert_by_key_xt(r, p, cmp);
446}
447
458 template <RankedBinNodeLike Node, class Compare> inline
459Node * insert_dup_by_key_xt(Node *& r, Node * p, Compare & cmp) noexcept
460{
461 assert(COUNT(Node::NullPtr) == 0);
462
463 if (r == Node::NullPtr)
464 return r = p;
465
466 Node * q;
467 if (cmp(KEY(p), KEY(r)))
469 else
471
472 ++COUNT(r);
473
474 return q;
475}
476
478 template <BinNodeLike Node,
479 class Compare = Aleph::less<typename Node::key_type>> inline
480Node * insert_dup_by_key_xt(Node *& r, Node * p, Compare && cmp = Compare())
481 noexcept
482{
483 return insert_dup_by_key_xt(r, p, cmp);
484}
485
499 template <RankedBinNodeLike Node, class Compare> inline
500Node * search_or_insert_by_key_xt(Node *& r, Node * p, Compare & cmp) noexcept
501{
502 assert(COUNT(Node::NullPtr) == 0);
503
504 if (r == Node::NullPtr)
505 return r = p;
506
507 Node * q;
508 if (cmp(KEY(p), KEY(r)))
509 {
511 if (q == p)
512 ++COUNT(r);
513 }
514 else if (cmp(KEY(r), KEY(p)))
515 {
517 if (q == p)
518 ++COUNT(r);
519 }
520 else
521 return r;
522
523 return q;
524}
525
527template <BinNodeLike Node,
528 class Compare = Aleph::less<typename Node::key_type>> inline
530 Compare && cmp = Compare()) noexcept
531{
532 return search_or_insert_by_key_xt(r, p, cmp);
533}
534
535
551 template <RankedBinNodeLike Node, class Compare> static inline
552bool __split_key_rec_xt(Node * root, const typename Node::key_type & key,
553 Node *& l, Node *& r, Compare & cmp)
554 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
555{
556 if (root == Node::NullPtr)
557 {
558 l = r = Node::NullPtr;
559 return true;
560 }
561
562 if (cmp(key, KEY(root)))
563 {
564 if (not __split_key_rec_xt(LLINK(root), key, l, LLINK(root), cmp))
565 return false;
566
567 r = root;
568 COUNT(r) -= COUNT(l);
569 }
570 else if (cmp(KEY(root), key))
571 {
572 if (not __split_key_rec_xt(RLINK(root), key, RLINK(root), r, cmp))
573 return false;
574
575 l = root;
576 COUNT(l) -= COUNT(r);
577 }
578 else
579 return false;
580
581 return true;
582}
583
603template <BinNodeLike Node, class Compare> inline
604bool split_key_rec_xt(Node *& root, const typename Node::key_type & key,
605 Node *& l, Node *& r, Compare & cmp)
606 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
607{
608 const bool ret = __split_key_rec_xt(root, key, l, r, cmp);
609 if (ret)
610 root = Node::NullPtr;
611 return ret;
612}
613
627template <BinNodeLike Node,
628 class Compare = Aleph::less<typename Node::key_type>> inline
629bool split_key_rec_xt(Node *& root, const typename Node::key_type & key,
630 Node *& l, Node *& r, Compare && cmp = Compare())
631 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
632{
633 return split_key_rec_xt(root, key, l, r, cmp);
634}
635
636
652 template <RankedBinNodeLike Node, class Compare> static inline
653void __split_key_dup_rec_xt(Node * root, const typename Node::key_type & key,
654 Node *& l, Node *& r, Compare & cmp)
655 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
656{
657 if (root == Node::NullPtr)
658 {
659 l = r = Node::NullPtr;
660 return;
661 }
662
663 if (cmp(key, KEY(root)))
664 {
666 r = root;
667 COUNT(r) -= COUNT(l);
668 }
669 else
670 {
672 l = root;
673 COUNT(l) -= COUNT(r);
674 }
675}
676
677
696 template <BinNodeLike Node, class Compare> inline
697void split_key_dup_rec_xt(Node *& root, const typename Node::key_type & key,
698 Node *& l, Node *& r, Compare & cmp)
699 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
700{
701 __split_key_dup_rec_xt<Node, Compare>(root, key, l, r, cmp);
702 root = Node::NullPtr;
703}
704
717 template <BinNodeLike Node,
718 class Compare = Aleph::less<typename Node::key_type>> inline
719void split_key_dup_rec_xt(Node *& root, const typename Node::key_type & key,
720 Node *& l, Node *& r, Compare && cmp = Compare())
721 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
722{
723 return split_key_dup_rec_xt(root, key, l, r, cmp);
724}
725
741 template <RankedBinNodeLike Node, class Compare> inline
742Node * insert_root_xt(Node *& root, Node * p, Compare & cmp) noexcept
743{
744 if (root == Node::NullPtr)
745 {
746 root = p;
747 return p;
748 }
749
750 if (not split_key_rec_xt(root, KEY(p), LLINK(p), RLINK(p), cmp))
751 return Node::NullPtr;
752
753 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
754 root = p;
755
756 return p;
757}
758
760 template <BinNodeLike Node,
761 class Compare = Aleph::less<typename Node::key_type>> inline
762Node * insert_root_xt(Node *& root, Node * p, Compare && cmp = Compare())
763 noexcept
764{
765 return insert_root_xt(root, p, cmp);
766}
767
782 template <RankedBinNodeLike Node, class Compare> inline
783Node * insert_dup_root_xt(Node *& root, Node * p, Compare & cmp) noexcept
784{
785 if (root == Node::NullPtr)
786 {
787 COUNT(p) = 1;
788 LLINK(p) = RLINK(p) = Node::NullPtr;
789 return root = p;
790 }
791
793 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
794
795 return root = p;
796}
797
799template <BinNodeLike Node,
800 class Compare = Aleph::less<typename Node::key_type>> inline
801Node * insert_dup_root_xt(Node *& root, Node * p, Compare && cmp = Compare())
802 noexcept
803{
804 return insert_dup_root_xt(root, p, cmp);
805}
806
807
808 template <RankedBinNodeLike Node> static inline
809void __split_pos_rec(Node * r, size_t i, Node *& ts, Node *& tg) noexcept
810{
811 if (i == COUNT(LLINK(r)))
812 {
813 ts = LLINK(r);
814 tg = r;
815 LLINK(tg) = Node::NullPtr;
816 COUNT(tg) -= COUNT(ts);
817 return;
818 }
819
820 if (i < COUNT(LLINK(r)))
821 {
822 __split_pos_rec(LLINK(r), i, ts, LLINK(r));
823 tg = r;
824 COUNT(r) -= COUNT(ts);
825 }
826 else
827 {
828 __split_pos_rec(RLINK(r), i - (COUNT(LLINK(r)) + 1), RLINK(r), tg);
829 ts = r;
830 COUNT(r) -= COUNT(tg);
831 }
832}
833
834
850template <RankedBinNodeLike Node> inline
851void split_pos_rec(Node *& r, const size_t i, Node *& ts, Node *& tg)
852{
853 ah_out_of_range_error_if(i > COUNT(r)) << "infix position out of range";
854
855 if (i == COUNT(r)) // Is it the last position?
856 {
857 ts = r;
858 r = tg = Node::NullPtr;
859 return;
860 }
861
862 __split_pos_rec<Node>(r, i, ts, tg);
863
864 r = Node::NullPtr;
865}
866
881 template <RankedBinNodeLike Node> inline
882void insert_by_pos_xt(Node *& r, Node * p, size_t pos)
883{
884 assert(COUNT(Node::NullPtr) == 0);
885
886 split_pos_rec(r, pos, LLINK(p), RLINK(p));
887 COUNT(p) = COUNT(LLINK(p)) + 1 + COUNT(RLINK(p));
888 r = p;
889}
890
903 template <RankedBinNodeLike Node> inline
904Node * join_exclusive_xt(Node *& ts, Node *& tg) noexcept
905{
906 if (ts == Node::NullPtr)
907 return tg;
908
909 if (tg == Node::NullPtr)
910 return ts;
911
912 LLINK(tg) = join_exclusive_xt(RLINK(ts), LLINK(tg));
913 RLINK(ts) = tg;
914
915 // Update Counters
916 COUNT(tg) = COUNT(LLINK(tg)) + 1 + COUNT(RLINK(tg));
917 COUNT(ts) = COUNT(LLINK(ts)) + 1 + COUNT(RLINK(ts));
918
919 Node * ret_val = ts;
920 ts = tg = Node::NullPtr; // should be empty after joining
921
922 return ret_val;
923}
924
925
941 template <RankedBinNodeLike Node,
942 class Compare = Aleph::less<typename Node::key_type>> inline
943Node * remove_by_key_xt(Node *& root, const typename Node::key_type & key,
944 Compare & cmp) noexcept
945{
946 if (root == Node::NullPtr)
947 return Node::NullPtr;
948
949 Node * ret_val = Node::NullPtr;
950 if (cmp(key, KEY(root)))
951 {
952 ret_val = remove_by_key_xt(LLINK(root), key, cmp);
953 if (ret_val != Node::NullPtr)
954 --COUNT(root);
955
956 return ret_val;
957 }
958 if (cmp(KEY(root), key))
959 {
960 ret_val = remove_by_key_xt(RLINK(root), key, cmp);
961 if (ret_val != Node::NullPtr)
962 --COUNT(root);
963
964 return ret_val;
965 }
966
967 ret_val = root;
969
970 ret_val->reset();
971
972 return ret_val;
973}
974
976template <BinNodeLike Node, class Compare = Aleph::less<typename Node::key_type>>
978 const typename Node::key_type & key,
979 Compare && cmp = Compare()) noexcept
980{
981 return remove_by_key_xt(root, key, cmp);
982}
983
984
985 template <RankedBinNodeLike Node> static inline
986Node * __remove_by_pos_xt(Node *& root, size_t pos) noexcept
987{
988 if (COUNT(LLINK(root)) == pos) // found position?
989 {
990 Node * ret_val = root;
992
993 ret_val->reset();
994
995 return ret_val;
996 }
997
998 Node * ret_val;
999 if (pos < COUNT(LLINK(root)))
1000 ret_val = __remove_by_pos_xt(LLINK(root), pos);
1001 else
1002 ret_val = __remove_by_pos_xt(RLINK(root), pos - (COUNT(LLINK(root)) + 1));
1003
1004 if (ret_val != Node::NullPtr) // removal was done?
1005 --COUNT(root);
1006
1007 return ret_val;
1008}
1009
1020 template <RankedBinNodeLike Node> inline
1022{
1023 ah_out_of_range_error_if(pos >= COUNT(root)) << "infix position out of range";
1024
1025 return __remove_by_pos_xt(root, pos);
1026}
1027
1032 template <RankedBinNodeLike Node> inline
1033bool check_rank_tree(Node * root) noexcept
1034{
1035 if (root == Node::NullPtr)
1036 return true;
1037
1038 if (COUNT(LLINK(root)) + COUNT(RLINK(root)) + 1 != COUNT(root))
1039 return false;
1040
1042}
1043
1050 template <RankedBinNodeLike Node> inline
1052{
1053 assert(p != Node::NullPtr);
1054 assert(COUNT(LLINK(p)) + 1 + COUNT(RLINK(p)) == COUNT(p));
1055
1056 Node * q = LLINK(p);
1057 LLINK(p) = RLINK(q);
1058 RLINK(q) = p;
1059 COUNT(p) -= 1 + COUNT(LLINK(q));
1060 COUNT(q) += 1 + COUNT(RLINK(p));
1061 assert(COUNT(LLINK(q)) + 1 + COUNT(RLINK(q)) == COUNT(q));
1062 return q;
1063}
1064
1071 template <RankedBinNodeLike Node> inline
1073{
1074 assert(p != Node::NullPtr);
1075 assert(COUNT(LLINK(p)) + 1 + COUNT(RLINK(p)) == COUNT(p));
1076
1077 Node * q = RLINK(p);
1078 RLINK(p) = LLINK(q);
1079 LLINK(q) = p;
1080 COUNT(p) -= 1 + COUNT(RLINK(q));
1081 COUNT(q) += 1 + COUNT(LLINK(p));
1082 assert(COUNT(LLINK(q)) + 1 + COUNT(RLINK(q)) == COUNT(q));
1083 return q;
1084}
1085
1086
1097 template <RankedBinNodeLike Node, class Key, class Compare> inline
1099 noexcept
1100{
1101 if (root == Node::NullPtr)
1102 return p; // insertion in empty tree
1103
1104 if (cmp(KEY(p), KEY(root)))
1105 { // insert in left subtree
1106 Node * left_branch = search_or_insert_root_rec_xt<Node, Key>(LLINK(root), p, cmp);
1107 if (left_branch == p) // p was inserted?
1108 {
1109 ++COUNT(root);
1110 LLINK(root) = left_branch;
1112 return p;
1113 }
1114
1115 return left_branch;
1116 }
1117 if (cmp(KEY(root), KEY(p)))
1118 { // insert in right subtree
1119 Node * right_branch = search_or_insert_root_rec_xt<Node, Key>(RLINK(root), p, cmp);
1120 if (right_branch == p) // p was inserted?
1121 {
1122 ++COUNT(root);
1123 RLINK(root) = right_branch;
1125 return p;
1126 }
1127
1128 return right_branch;
1129 }
1130
1131 return root;
1132}
1133
1135template <BinNodeLike Node, class Key,
1136 class Compare = Aleph::less<typename Node::key_type>> inline
1138 Compare && cmp = Compare()) noexcept
1139{
1140 return search_or_insert_root_rec_xt<Node, Key>(root, p, cmp);
1141}
1142
1143
1162template <class TreeType, class Node, typename Key, class Compare>
1164{
1165protected:
1166
1168 mutable Node * curr;
1169 mutable int curr_pos;
1170
1171 static constexpr int Pos_Not_Current = -1;
1172 static constexpr int Pos_Empty_Container = -2;
1173 static constexpr int Pos_Not_Updated = -3;
1174 static constexpr int Pos_Out_Of_Range = std::numeric_limits<int>::min();
1175
1176private:
1177
1178 bool is_container_empty() const noexcept
1179 {
1180 return COUNT(tree_ptr->getRoot()) == 0;
1181 }
1182
1183 bool pos_updated() const noexcept
1184 {
1185 return curr_pos != Pos_Not_Updated;
1186 }
1187
1188 bool curr_updated() const noexcept
1189 {
1190 return curr != nullptr;
1191 }
1192
1193 // The position of the node itself: among equivalent keys, a search by
1194 // its key could find another node and put the iterator on it. A free
1195 // function of this header: BinTreeXt_Operation is declared in
1196 // tpl_binTreeOps.H, after this one, and naming it here kept has_curr()
1197 // from compiling
1198 void update_pos() const noexcept(nothrow_compare_v<Compare, Key>)
1199 {
1200 assert(curr != nullptr);
1202 tree_ptr->key_comp());
1203 }
1204
1205 // A position that an int cannot hold is past the end of every tree whose
1206 // positions it holds: it is kept as Pos_Out_Of_Range, the least int, a
1207 // negative position, which has no node either, and from which next_ne()
1208 // would take some 2^31 steps to leave the negative ones, as next_ne()
1209 // goes to it from the greatest int. Converting it would drop its high
1210 // bits and could give a position with a node
1211 static int saturated_pos(const size_t pos) noexcept
1212 {
1213 constexpr auto max_pos =
1214 static_cast<size_t>(std::numeric_limits<int>::max());
1215 return pos < max_pos ? static_cast<int>(pos) : Pos_Out_Of_Range;
1216 }
1217
1218 // A position outside [0, size()) has no node: past the end it comes
1219 // from reset_to_pos(), from the constructor with a position, from
1220 // next_ne() at the end or from keys removed from the tree, and select()
1221 // would throw inside noexcept. A negative one is checked first: an
1222 // iterator built by default has no tree to read
1223 void update_curr() const noexcept
1224 {
1225 assert(curr_pos != Pos_Not_Updated);
1226
1227 if (curr_pos < 0)
1228 {
1229 curr = nullptr;
1230 return;
1231 }
1232
1233 Node * root = tree_ptr->getRoot();
1234 if (static_cast<size_t>(curr_pos) >= COUNT(root))
1235 {
1236 curr = nullptr;
1237 return;
1238 }
1239
1240 curr = Aleph::select_ne(root, static_cast<size_t>(curr_pos));
1241 }
1242
1243public:
1244
1246 : tree_ptr(nullptr), curr(nullptr), curr_pos(Pos_Not_Current)
1247 { /* empty */ }
1248
1249 BinTreeXt_Iterator(const TreeType & __tree) noexcept
1250 : tree_ptr(&const_cast<TreeType&>(__tree)), curr(nullptr)
1251 {
1253 }
1254
1255 BinTreeXt_Iterator(const TreeType & __tree, Node * __curr) noexcept
1256 : tree_ptr(&const_cast<TreeType&>(__tree)),
1257 curr(__curr), curr_pos(Pos_Not_Updated)
1258 { /* empty */ }
1259
1266 BinTreeXt_Iterator(const TreeType & __tree, const size_t pos) noexcept
1267 : tree_ptr(&const_cast<TreeType&>(__tree)),
1268 curr(nullptr), curr_pos(saturated_pos(pos))
1269 { /* empty */ }
1270
1272 : tree_ptr(itor.tree_ptr), curr(itor.curr), curr_pos(itor.curr_pos)
1273 { /* empty */ }
1274
1276 {
1277 if (this == &itor)
1278 return *this;
1279
1280 tree_ptr = itor.tree_ptr;
1281 curr = itor.curr;
1282 curr_pos = itor.curr_pos;
1283
1284 return *this;
1285 }
1286
1287 void reset_first() noexcept
1288 {
1289 curr = nullptr;
1291 }
1292
1293 void reset_last() noexcept
1294 {
1295 curr = nullptr;
1297 static_cast<int>(COUNT(tree_ptr->getRoot())) - 1;
1298 }
1299
1300 void end() noexcept
1301 {
1302 put_itor_at_the_end(*this);
1303 }
1304
1316 void reset_to_key(const Key & key)
1317 noexcept(noexcept(tree_ptr->find_position(key)))
1318 {
1319 std::pair<long, Node*> p = tree_ptr->find_position(key);
1320 curr = nullptr;
1321 curr_pos = p.first;
1322 }
1323
1324 void reset_to_node(Node * node) noexcept
1325 {
1326 curr = node;
1328 }
1329
1335 void reset_to_pos(const size_t pos) noexcept
1336 {
1337 curr = nullptr;
1338 curr_pos = saturated_pos(pos);
1339 }
1340
1353 Node * get_curr_ne() const noexcept
1354 {
1355 if (not curr_updated())
1356 update_curr();
1357 return curr;
1358 }
1359
1365 Node * get_curr() const noexcept
1366 {
1367 return get_curr_ne();
1368 }
1369
1371 {
1372 if (not pos_updated())
1373 update_pos();
1374
1375 ah_range_error_if(curr_pos < -1) << "Iterator has no current";
1376 ah_range_error_if(curr_pos > static_cast<int>(COUNT(tree_ptr->getRoot())))
1377 << "Iterator has no current";
1378
1379 return curr_pos;
1380 }
1381
1382 size_t get_pos() const { return get_current_position(); }
1383
1392 bool has_curr() const noexcept(nothrow_compare_v<Compare, Key>)
1393 {
1394 if (not pos_updated())
1395 update_pos();
1396
1397 return curr_pos >= 0 and
1398 curr_pos < static_cast<int>(COUNT(tree_ptr->getRoot()));
1399 }
1400
1401 void prev()
1402 {
1403 ah_underflow_error_if(not has_curr()) << "Iterator has no current";
1404 --curr_pos;
1405 curr = nullptr;
1406 }
1407
1416 void next_ne() noexcept
1417 {
1418 curr_pos = static_cast<int>(static_cast<unsigned>(curr_pos) + 1u);
1419 curr = nullptr;
1420 }
1421
1422 void next()
1423 {
1424 ah_overflow_error_if(not has_curr()) << "Iterator has no current";
1425 next_ne();
1426 }
1427
1429 {
1430 ah_underflow_error_if(not has_curr()) << "Iterator has no current";
1431
1432 if (not curr_updated())
1433 update_curr();
1434
1435 Node * ret_val = tree_ptr->remove(KEY(curr));
1436 curr = nullptr;
1437
1438 return ret_val;
1439 }
1440
1450 bool operator == (const BinTreeXt_Iterator & itor) const
1451 noexcept(nothrow_compare_v<Compare, Key>)
1452 {
1453 if (is_container_empty() and itor.is_container_empty())
1454 return true;
1455
1456 if (pos_updated() and itor.pos_updated())
1457 return curr_pos == itor.curr_pos;
1458
1459 if (curr_updated() and itor.curr_updated())
1460 return curr == itor.curr;
1461
1462 if (not pos_updated())
1463 {
1464 update_pos();
1465 return curr_pos == itor.curr_pos;
1466 }
1467
1468 itor.update_pos();
1469 return curr_pos == itor.curr_pos;
1470 }
1471
1472 bool operator != (const BinTreeXt_Iterator & itor) const
1473 {
1474 return not (*this == itor);
1475 }
1476}; // end class BinTreeXt_Iterator
1477
1478
1479} // end namespace Aleph
1480
1481# endif // TPL_BINNODEXT_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:517
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Definition ah-errors.H:356
SentinelCtor
Tag type for sentinel node construction.
Definition ahDefs.H:83
void put_itor_at_the_end(Itor &it) noexcept
Definition aleph.H:54
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
BinNodeXt_Data(SentinelCtor) noexcept
size_t size() const noexcept
void reset() noexcept
size_t & getCount() noexcept
Node for extended binary search tree.
Base iterator template for ranked binary search trees.
static constexpr int Pos_Empty_Container
bool operator==(const BinTreeXt_Iterator &itor) const noexcept(nothrow_compare_v< Compare, Key >)
Return true if this and itor are on the same position.
void update_curr() const noexcept
bool has_curr() const noexcept(nothrow_compare_v< Compare, Key >)
Return true if the iterator has a current node.
void next_ne() noexcept
Move the iterator one position forward, without the check of next().
void update_pos() const noexcept(nothrow_compare_v< Compare, Key >)
void reset_to_key(const Key &key) noexcept(noexcept(tree_ptr->find_position(key)))
Put the iterator on the node of a key.
static constexpr int Pos_Out_Of_Range
Node * get_curr_ne() const noexcept
Return the current node.
BinTreeXt_Iterator(const TreeType &__tree, Node *__curr) noexcept
bool curr_updated() const noexcept
void reset_to_node(Node *node) noexcept
static constexpr int Pos_Not_Current
BinTreeXt_Iterator(const TreeType &__tree, const size_t pos) noexcept
Initialize an iterator on the inorder position pos.
BinTreeXt_Iterator & operator=(const BinTreeXt_Iterator &itor) noexcept
void reset_to_pos(const size_t pos) noexcept
Put the iterator on the inorder position pos.
bool is_container_empty() const noexcept
size_t get_current_position() const
static constexpr int Pos_Not_Updated
static int saturated_pos(const size_t pos) noexcept
BinTreeXt_Iterator(const BinTreeXt_Iterator &itor) noexcept
bool pos_updated() const noexcept
BinTreeXt_Iterator(const TreeType &__tree) noexcept
bool operator!=(const BinTreeXt_Iterator &itor) const
Node * get_curr() const noexcept
Return the current node.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
long inorder_position_of_node(Node *r, Node *p, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
The inorder position of the node p, in a tree that may have keys equivalent to that of p.
static void __split_key_dup_rec_xt(Node *root, const typename Node::key_type &key, Node *&l, Node *&r, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
The recursion of split_key_dup_rec_xt().
Node * insert_dup_root_xt(Node *&root, Node *p, Compare &cmp) noexcept
Insert a node as root of an extended binary search tree.
Node * insert_dup_by_key_xt(Node *&r, Node *p, Compare &cmp) noexcept
Insert a node in an extended binary search tree without testing for duplicity.
static bool __split_key_rec_xt(Node *root, const typename Node::key_type &key, Node *&l, Node *&r, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
The recursion of split_key_rec_xt().
bool split_key_rec_xt(Node *&root, const typename Node::key_type &key, Node *&l, Node *&r, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Split an extended binary search tree according to a key.
bool check_rank_tree(Node *root) noexcept
Return true if root is a valid extended binary tree.
void split_pos_rec(Node *&r, const size_t i, Node *&ts, Node *&tg)
Split a extended binary tree according to a position.
Node * insert_by_key_xt(Node *&r, Node *p, Compare &cmp) noexcept
Insert a node in an extended binary search tree.
#define DECLARE_BINNODE_SENTINEL(Name, height, Control_Data)
Specify tree node for a binary tree.
auto & COUNT(Node *p) noexcept
Return the number of nodes of the tree fron p is root.
void split_key_dup_rec_xt(Node *&root, const typename Node::key_type &key, Node *&l, Node *&r, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Split an extended binary search tree according to a key which can be in the tree.
Node * insert_root_xt(Node *&root, Node *p, Compare &cmp) noexcept
Insert a node p as root of an extended binary search tree.
Node * remove_by_pos_xt(Node *&root, size_t pos)
Remove from a extended binary tree the node whose inorder position is pos.
Node * remove_by_key_xt(Node *&root, const typename Node::key_type &key, Compare &cmp) noexcept
Remove a key of extended binary tree.
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 * rotate_to_left_xt(Node *p) noexcept
Rotate to left the extended binary tree with root p.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
Node * select_ne(Node *r, const size_t pos) noexcept
Iterative selection of a node according to inorder position without exception.
Node * rotate_to_right_xt(Node *p) noexcept
Rotate to right the extended bianry tree with root p
void insert_by_pos_xt(Node *&r, Node *p, size_t pos)
Insert a node in a specific inorder position in a binary tree.
Node * select_rec(Node *r, const size_t i)
Recursively select the i-th node inorder sense.
constexpr bool nothrow_compare_v
Whether Compare compares two keys of type Key without throwing, as the operations of the trees call i...
Node * join_exclusive_xt(Node *&ts, Node *&tg) noexcept
Exclusive union of two extended binary search trees.
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Main namespace for Aleph-w library functions.
Node * search_or_insert_by_key_xt(Node *&r, Node *p, Compare &cmp) noexcept
Search or insert a node in an extended binary search tree.
static Node * __remove_by_pos_xt(Node *&root, size_t pos) noexcept
and
Check uniqueness with explicit hash + equality functors.
Node * search_or_insert_root_rec_xt(Node *root, Node *p, Compare &cmp) noexcept
Search or insert a key in an extended binary search tree.
static Node * __select_rec(Node *r, const size_t i) noexcept
long find_position(Node *r, const typename Node::key_type &key, Node *&p, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Find the inorder position of a key in an extended binary search tree.
static void __split_pos_rec(Node *r, size_t i, Node *&ts, Node *&tg) noexcept
TreeType
#define RLINK(i, n)
#define LLINK(i, n)
Gsl_Compatible_Rng r
Utility functions for binary tree operations.
Basic binary tree node definitions.
DynList< int > l