Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_treapRk.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_TREAPRK_H
51# define TPL_TREAPRK_H
52
53# include <limits>
54# include <type_traits>
55# include <ah-random.H>
56# include <ahFunction.H>
57# include <tpl_binTreeOps.H>
58# include <treapNode.H>
59# include <ah-errors.H>
60# include <ah-concepts.H>
61
62using namespace Aleph;
63
64namespace Aleph {
65
67{
68 unsigned long priority;
69
70 unsigned long count;
71
72public:
73
75 {
76 /* empty */
77 }
78
80 {
81 /* empty */
82 }
83
84 unsigned long & getPriority() noexcept { return priority; }
85
86 unsigned long & getCount() noexcept { return count; }
87
88 void reset() noexcept { count = 1; }
89};
90
92
187 template <template <class> class NodeType, class Key, class Compare>
190{
191public:
192
193 using Node = NodeType<Key>;
194
195private:
196
201 Compare cmp;
202
203 // The seed of the generator for that of a constructor, as it was taken
204 // for GSL: through an unsigned int, modulo 2^32 - 1
205 static unsigned long generator_seed(const unsigned long seed) noexcept
206 {
207 return static_cast<unsigned int>(seed)
208 % static_cast<unsigned long>(Gsl_Compatible_Rng::max());
209 }
210
211public:
212
222 void set_seed(const unsigned long seed)
223 {
225 }
226
228 [[nodiscard]] Random_Source & random_source() noexcept { return r; }
229
232 [[nodiscard]] const Random_Source & random_source() const noexcept { return r; }
233
243 [[deprecated("it makes a Mersenne Twister in a treap that has none: "
244 "use random_source(); it goes in 8.0.0")]]
245 [[nodiscard]] Gsl_Compatible_Rng & rng() { return r.mersenne_twister(); }
246
248 Compare & key_comp() noexcept { return cmp; }
249
251 Compare & get_compare() noexcept { return key_comp(); }
252
263 Gen_Treap_Rk(const unsigned long seed, Compare __cmp = Compare())
265 r(Random_Source::Engine::Mersenne_Twister, generator_seed(seed)), cmp(__cmp)
266 {
268 }
269
285 noexcept(std::is_nothrow_default_constructible_v<Node> and
286 std::is_nothrow_default_constructible_v<Compare> and
287 std::is_nothrow_constructible_v<Compare, Compare &>)
288 : Gen_Treap_Rk(Compare())
289 {
290 // empty
291 }
292
304 Gen_Treap_Rk(Compare __cmp)
305 noexcept(std::is_nothrow_default_constructible_v<Node> and
306 std::is_nothrow_constructible_v<Compare, Compare &>)
307 : head_ptr(&head), tree_root(RLINK(head_ptr)), cmp(__cmp)
308 {
310 }
311
335 noexcept(bin_node_detail::nothrow_header_from_v<Node> and
336 bin_node_detail::nothrow_compare_taken_v<Compare>)
337 : head(bin_node_detail::header_from(tree.head)),
338 head_ptr(&head), tree_root(RLINK(head_ptr)), r(std::move(tree.r)),
339 cmp(bin_node_detail::compare_to_take(tree.cmp))
340 {
342 tree_root = tree.tree_root;
343 tree.tree_root = Node::NullPtr;
344 }
345
353 void swap(Gen_Treap_Rk & tree) noexcept(std::is_nothrow_swappable_v<Compare>)
354 {
355 // The comparison first: if its swap throws, the nodes stay where
356 // they were
357 std::swap(cmp, tree.cmp);
358 std::swap(tree_root, tree.tree_root);
359 r.swap(tree.r);
360 }
361
363 Node *& getRoot() noexcept { return tree_root; }
364
365 Node * getRoot() const noexcept { return tree_root; }
366
377 Node * search(const Key & key) const noexcept(nothrow_compare_v<Compare, Key>)
378 {
379 Node* ret_val = searchInBinTree(tree_root, key, cmp);
380 return ret_val == Node::NullPtr ? nullptr : ret_val;
381 }
382
383private:
384
385 // The insertions and the removal compare on the way down and link,
386 // count or rotate on the way back, so a comparison that throws leaves
387 // the tree as it was
388 bool insert(Node *& root, Node * p) noexcept(nothrow_compare_v<Compare, Key>)
389 {
390 if (root == Node::NullPtr)
391 {
392 root = p;
393 return true;
394 }
395
396 const Key & pk = KEY(p);
397 const Key & rk = KEY(root);
398 if (cmp(pk, rk))
399 {
400 if (insert(LLINK(root), p))
401 {
402 ++COUNT(root);
403 if (PRIO(LLINK(root)) < PRIO(root) )
405
406 return true;
407 }
408 }
409 else if (cmp(rk, pk))
410 {
411 if (insert(RLINK(root), p))
412 {
413 ++COUNT(root);
414 if (PRIO(RLINK(root)) < PRIO(root) )
416
417 return true;
418 }
419 }
420
421 return false;
422 }
423
424 // Search or insert p. Return p if KEY(p) is not in the tree;
425 // otherwise, it returns a pointer to a node containing KEY(p)
427 noexcept(nothrow_compare_v<Compare, Key>)
428 {
429 if (root == Node::NullPtr)
430 return root = p;
431
432 const Key & pk = KEY(p);
433 const Key & rk = KEY(root);
434 if (cmp(pk, rk))
435 {
436 Node * ret = search_or_insert(LLINK(root), p);
437 if (ret == p) // insertion done?
438 { // yes ==> increase counter and perhaps rotate
439 ++COUNT(root);
440 if (PRIO(LLINK(root)) < PRIO(root))
442
443 assert(PRIO(root) <= PRIO(LLINK(root)) and
444 PRIO(root) <= PRIO(LLINK(root)));
445 }
446
447 return ret;
448 }
449 if (cmp(rk, pk))
450 {
451 Node * ret = search_or_insert(RLINK(root), p);
452 if (ret == p) // insertion done?
453 { // yes ==> increase counter and perhaps rotate
454 ++COUNT(root);
455 if (PRIO(RLINK(root)) < PRIO(root))
457
458 assert(PRIO(root) <= PRIO(LLINK(root)) and
459 PRIO(root) <= PRIO(LLINK(root)));
460 }
461
462 return ret;
463 }
464
465 assert(PRIO(root) <= PRIO(LLINK(root)) and PRIO(root) <= PRIO(LLINK(root)));
466
467 return root;
468 }
469
470 // Return p; root could be modified
471 Node * insert_dup(Node *& root, Node * p) noexcept(nothrow_compare_v<Compare, Key>)
472 {
473 if (root == Node::NullPtr)
474 return root = p;
475
476 const Key & pk = KEY(p);
477 const Key & rk = KEY(root);
478 if (cmp(pk, rk))
479 {
480 insert_dup(LLINK(root), p);
481 ++COUNT(root);
482 if (PRIO(LLINK(root)) < PRIO(root))
484 }
485 else
486 {
487 insert_dup(RLINK(root), p);
488 ++COUNT(root);
489 if (PRIO(RLINK(root)) < PRIO(root))
491 }
492
493 return p;
494 }
495
496public:
497
513 Node * insert(Node * p) noexcept(nothrow_compare_v<Compare, Key>)
514 {
515 assert(p != Node::NullPtr);
516
517 PRIO(p) = r.get();
518
519 return insert(tree_root, p) ? p : nullptr;
520 }
521
535 Node * insert_dup(Node * p) noexcept(nothrow_compare_v<Compare, Key>)
536 {
537 assert(p != Node::NullPtr);
538
539 PRIO(p) = r.get();
540
541 return insert_dup(tree_root, p);
542 }
543
562 Node * search_or_insert(Node * p) noexcept(nothrow_compare_v<Compare, Key>)
563 {
564 assert(p != Node::NullPtr);
565
566 PRIO(p) = r.get();
567
568 return search_or_insert(tree_root, p);
569 }
570
572 bool verify() const
573 {
576 }
577
578private:
579
580 static Node * join_exclusive(Node * t1, Node * t2) noexcept
581 {
582 if (t1 == Node::NullPtr)
583 return t2;
584
585 if (t2 == Node::NullPtr)
586 return t1;
587
588 if (PRIO(t1) < PRIO(t2))
589 {
590 COUNT(t1) += COUNT(t2);
591 RLINK(t1) = join_exclusive(RLINK(t1), t2);
592 return t1;
593 }
594 COUNT(t2) += COUNT(t1);
595 LLINK(t2) = join_exclusive(t1, LLINK(t2) );
596 return t2;
597 }
598
599 Node * remove(Node *& root, const Key & key) noexcept(nothrow_compare_v<Compare, Key>)
600 {
601 if (root == Node::NullPtr)
602 return Node::NullPtr;
603
604 Node * ret_val;
605 const Key & rk = KEY(root);
606 if (cmp(key, rk))
607 ret_val = remove(LLINK(root), key);
608 else if (cmp(rk, key))
609 ret_val = remove(RLINK(root), key);
610 else
611 {
612 ret_val = root;
614
615 return ret_val;
616 }
617
618 if (ret_val == Node::NullPtr)
619 return Node::NullPtr;
620
621 --COUNT(root);
622
623 return ret_val;
624 }
625
626public:
627
638 Node * remove(const Key & key) noexcept(nothrow_compare_v<Compare, Key>)
639 {
640 Node * ret_val = remove(tree_root, key);
641 if (ret_val == Node::NullPtr)
642 return nullptr;
643
644 ret_val->reset();
645
646 return ret_val;
647 }
648
657 Node * remove(const size_t beg, const size_t end)
658 {
659 ah_range_error_if(beg > end or end > COUNT(tree_root)) << "remove of TreapRk out of range";
660
661 Node * before_beg, * after_end, * aux;
662
663 Node * ret_val = tree_root;
664
665 split_pos_rec(ret_val, end + 1, aux, after_end);
666 split_pos_rec(aux, beg, before_beg, ret_val);
667
668 tree_root = join_exclusive(before_beg, after_end);
669
670 return ret_val;
671 }
672
673private:
674
675 static Node * remove_pos(Node *& root, const size_t pos) noexcept
676 {
677 if (pos == COUNT(LLINK(root)))
678 {
679 Node * ret_val = root;
680 root = join_exclusive(LLINK(ret_val), RLINK(ret_val));
681 return ret_val;
682 }
683
684 --COUNT(root);
685 if (pos < COUNT(LLINK(root)))
686 return remove_pos(LLINK(root), pos);
687 return remove_pos(RLINK(root), pos - COUNT(LLINK(root)) - 1);
688 }
689
690public:
691
699 Node * remove_pos(const size_t pos)
700 {
701 ah_out_of_range_error_if(pos >= COUNT(tree_root)) << "infix position out of range";
702
703 return remove_pos(tree_root, pos);
704 }
705
713 Node * select(const size_t i) const
714 {
715 return Aleph::select(tree_root, i);
716 }
717
719 size_t size() const noexcept { return COUNT(tree_root); }
720
722 bool is_empty() const noexcept { return tree_root == Node::NullPtr; }
723
736 std::pair<int, Node*> position(const Key & key) const
737 noexcept(nothrow_compare_copy_v<Compare, Key>)
738 {
739 std::pair<int, Node*> ret_val;
740
742 inorder_position(tree_root, key, ret_val.second);
743
744 return ret_val;
745 }
746
777 std::pair<int, Node*> find_position(const Key & key) const
778 noexcept(nothrow_compare_copy_v<Compare, Key>)
779 {
780 std::pair<int, Node*> r(-2, nullptr);
781
782 r.first = BinTreeXt_Operation <Node, Compare>(cmp) .
783 find_position(tree_root, key, r.second);
784
785 return r;
786 }
787
804 bool split_key(const Key & key, Gen_Treap_Rk & t1, Gen_Treap_Rk & t2)
805 noexcept(nothrow_compare_v<Compare, Key>)
806 {
807 return split_key_rec_xt(tree_root, key, t1.getRoot(), t2.getRoot(), cmp);
808 }
809
827 void
828 split_key_dup(const Key & key, Gen_Treap_Rk & t1, Gen_Treap_Rk & t2)
829 noexcept(nothrow_compare_v<Compare, Key>)
830 {
831 split_key_dup_rec_xt(tree_root, key, t1.getRoot(), t2.getRoot(), cmp);
832 }
833
840 void split_pos(size_t pos, Gen_Treap_Rk & t1, Gen_Treap_Rk & t2)
841 {
842 split_pos_rec(tree_root, pos, t1.getRoot(), t2.getRoot());
843 }
844
845private:
846
847 void join_dup(Node *& t1, Node * t2) noexcept
848 {
849 if (t2 == Node::NullPtr)
850 return;
851
852 Node * l = LLINK(t2), * r = RLINK(t2);
853 t2->reset();
854 insert_dup(t1, t2); // insert_dup modifies t1 by reference
855 join_dup(t1, l);
856 join_dup(t1, r);
857 }
858
859 void join(Node *& t1, Node * t2, Node *& dup) noexcept
860 {
861 if (t2 == Node::NullPtr)
862 return;
863
864 Node * l = LLINK(t2), * r = RLINK(t2);
865 t2->reset();
866 ins:
867 const bool inserted = insert(t1, t2);
868 if (not inserted)
869 {
870 Node * removed = remove(t1, KEY(t2));
871 removed->reset();
872 insert_dup(dup, removed);
873 goto ins;
874 }
875 join(t1, l, dup);
876 join(t1, r, dup);
877 }
878
879public:
880
898 void join(Gen_Treap_Rk & t, Gen_Treap_Rk & dup) noexcept
899 {
900 join(tree_root, t.tree_root, dup.tree_root);
901 t.tree_root = Node::NullPtr;
902 }
903
918 void join_dup(Gen_Treap_Rk & t) noexcept
919 {
920 join_dup(tree_root, t.tree_root);
921 t.tree_root = Node::NullPtr;
922 }
923
935 void join_exclusive(Gen_Treap_Rk & t) noexcept
936 {
937 tree_root = join_exclusive(tree_root, t.tree_root);
938 t.tree_root = Node::NullPtr;
939 }
940
948 {
949 protected:
950
952 mutable Node * curr;
953 mutable int curr_pos;
954
955 static constexpr int Pos_Not_Current = -1;
956 static constexpr int Pos_Empty_Container = -2;
957 static constexpr int Pos_Not_Updated = -3;
958 static constexpr int Pos_Out_Of_Range = std::numeric_limits<int>::min();
959
960 private:
961
962 bool is_container_empty() const noexcept
963 {
964 return COUNT(tree_ptr->getRoot()) == 0;
965 }
966
967 bool pos_updated() const noexcept
968 {
969 return curr_pos != Pos_Not_Updated;
970 }
971
972 bool curr_updated() const noexcept
973 {
974 return curr != nullptr;
975 }
976
977 bool is_updated() const noexcept
978 {
979 return pos_updated() and curr_updated();
980 }
981
982 // The position of the node itself: among equivalent keys, a search by
983 // its key could find another node and put the iterator on it. It
984 // compares with the criteria of the tree, without copying it
985 void update_pos() const noexcept(nothrow_compare_v<Compare, Key>)
986 {
987 assert(curr != nullptr);
989 tree_ptr->cmp);
990 }
991
992 // A position that an int cannot hold is past the end of every tree
993 // whose positions it holds: it is kept as Pos_Out_Of_Range, the least
994 // int, a negative position, which has no node either, and from which
995 // next_ne() would take some 2^31 steps to leave the negative ones, as
996 // next_ne() goes to it from the greatest int. Converting it would drop
997 // its high bits and could give a position with a node
998 static int saturated_pos(const size_t pos) noexcept
999 {
1000 constexpr auto max_pos =
1001 static_cast<size_t>(std::numeric_limits<int>::max());
1002 return pos < max_pos ? static_cast<int>(pos) : Pos_Out_Of_Range;
1003 }
1004
1005 // A position outside [0, size()) has no node: past the end it comes
1006 // from reset_to_pos(), from Iterator(tree, pos), from next_ne() at the
1007 // end or from keys removed from the tree, and select() would throw
1008 // inside noexcept. A negative one is checked first: an iterator built
1009 // by default has no tree to read
1010 void update_curr() const noexcept
1011 {
1012 assert(curr_pos != Pos_Not_Updated);
1013
1014 if (curr_pos < 0)
1015 {
1016 curr = nullptr;
1017 return;
1018 }
1019
1020 Node * root = tree_ptr->getRoot();
1021 if (static_cast<size_t>(curr_pos) >= COUNT(root))
1022 {
1023 curr = nullptr;
1024 return;
1025 }
1026
1027 curr = Aleph::select_ne(root, static_cast<size_t>(curr_pos));
1028 }
1029
1030 public:
1031
1032 Iterator() noexcept
1033 : tree_ptr(nullptr), curr(nullptr), curr_pos(Pos_Not_Current)
1034 {
1035 /* empty */
1036 }
1037
1039 Iterator(const Gen_Treap_Rk & __tree) noexcept
1040 : tree_ptr(&const_cast<Gen_Treap_Rk&>(__tree)), curr(nullptr)
1041 {
1043 }
1044
1046 Iterator(const Gen_Treap_Rk & __tree, Node * __curr) noexcept
1047 : tree_ptr(&const_cast<Gen_Treap_Rk&>(__tree)),
1048 curr(__curr), curr_pos(Pos_Not_Updated)
1049 {
1050 // empty
1051 }
1052
1060 Iterator(const Gen_Treap_Rk & __tree, const size_t pos) noexcept
1061 : tree_ptr(&const_cast<Gen_Treap_Rk&>(__tree)),
1062 curr(nullptr), curr_pos(saturated_pos(pos))
1063 {
1064 // empty
1065 }
1066
1067 Iterator(const Iterator & itor) noexcept
1068 : tree_ptr(itor.tree_ptr), curr(itor.curr), curr_pos(itor.curr_pos)
1069 {
1070 // Empty
1071 }
1072
1073 Iterator & operator = (const Iterator & itor) noexcept
1074 {
1075 if (this == &itor)
1076 return *this;
1077
1078 tree_ptr = itor.tree_ptr;
1079 curr = itor.curr;
1080 curr_pos = itor.curr_pos;
1081
1082 return *this;
1083 }
1084
1086 void reset_first() noexcept
1087 {
1088 curr = nullptr;
1090 }
1091
1093 void reset_last() noexcept
1094 {
1095 curr = nullptr;
1097 COUNT(tree_ptr->getRoot()) - 1);
1098 }
1099
1101 void end() noexcept
1102 {
1103 put_itor_at_the_end(*this);
1104 }
1105
1117 void reset_to_key(const Key & key)
1118 noexcept(noexcept(tree_ptr->find_position(key)))
1119 {
1120 std::pair<int, Node*> p = tree_ptr->find_position(key);
1121 curr = nullptr;
1122 curr_pos = p.first;
1123 }
1124
1134 void reset_to_node(Node * node) noexcept
1135 {
1136 curr = node;
1138 }
1139
1146 void reset_to_pos(const size_t pos) noexcept
1147 {
1148 curr = nullptr;
1149 curr_pos = saturated_pos(pos);
1150 }
1151
1164 Node * get_curr_ne() const noexcept
1165 {
1166 if (not curr_updated())
1167 update_curr();
1168
1169 return curr;
1170 }
1171
1177 Node * get_curr() const noexcept
1178 {
1179 return get_curr_ne();
1180 }
1181
1191 {
1192 if (not pos_updated())
1193 update_pos();
1194
1195 ah_range_error_if(curr_pos < -1) << "TreapRk iterator has not current";
1196
1197 ah_range_error_if(curr_pos > COUNT(tree_ptr->getRoot())) << "TreapRk iterator has not current";
1198
1199 return curr_pos;
1200 }
1201
1203 size_t get_pos() const { return get_current_position(); }
1204
1213 bool has_curr() const noexcept(nothrow_compare_v<Compare, Key>)
1214 {
1215 if (not pos_updated())
1216 update_pos();
1217
1218 return curr_pos >= 0 and curr_pos < COUNT(tree_ptr->getRoot());
1219 }
1220
1223 void prev()
1224 {
1225 ah_underflow_error_if(not has_curr()) << "TreapRk iterator has not current";
1226
1227 --curr_pos;
1228 curr = nullptr;
1229 }
1230
1239 void next_ne() noexcept
1240 {
1241 curr_pos = static_cast<int>(static_cast<unsigned>(curr_pos) + 1u);
1242 curr = nullptr;
1243 }
1244
1247 void next()
1248 {
1249 ah_underflow_error_if(not has_curr()) << "TreapRk iterator has not current";
1250 next_ne();
1251 }
1252
1255 {
1256 ah_underflow_error_if(not has_curr()) << "TreapRk iterator has not current";
1257
1258 if (not curr_updated())
1259 update_curr();
1260
1261 Node * ret_val = tree_ptr->remove(KEY(curr) );
1262
1263 curr = nullptr;
1264
1265 return ret_val;
1266 }
1267
1277 bool operator == (const Iterator & itor) const
1278 noexcept(nothrow_compare_v<Compare, Key>)
1279 {
1280 if (is_container_empty() and itor.is_container_empty())
1281 return true;
1282
1283 if (pos_updated() and itor.pos_updated())
1284 return curr_pos == itor.curr_pos;
1285
1286 if (curr_updated() and itor.curr_updated())
1287 return curr == itor.curr;
1288
1289 if (not pos_updated())
1290 {
1291 update_pos();
1292 return curr_pos == itor.curr_pos;
1293 }
1294
1295 itor.update_pos();
1296
1297 return curr_pos == itor.curr_pos;
1298 }
1299
1301 bool operator != (const Iterator & itor) const
1302 {
1303 return not (*this == itor);
1304 }
1305
1306 bool verify(Gen_Treap_Rk * r) const noexcept
1307 {
1308 return tree_ptr->getRoot() == r->getRoot();
1309 }
1310
1311 bool verify(const Iterator & it) const noexcept
1312 {
1313 return tree_ptr->getRoot() == it.tree_ptr->getRoot();
1314 }
1315 }; // end class Iterator
1316};
1317
1358 template <typename Key, class Compare = Aleph::less<Key>>
1359 requires StrictWeakOrder<Compare, Key>
1360struct Treap_Rk : public Gen_Treap_Rk<Treap_Rk_Node, Key, Compare>
1361{
1363 using Base::Base;
1364};
1365
1406 template <typename Key, class Compare = Aleph::less<Key>>
1408struct Treap_Rk_Vtl : public Gen_Treap_Rk<Treap_Rk_NodeVtl, Key, Compare>
1409{
1411 using Base::Base;
1412};
1413
1414
1415} // end namespace Aleph
1416
1417# endif // TPL_TREAPRK_H
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
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:517
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Definition ah-errors.H:356
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
SentinelCtor
Tag type for sentinel node construction.
Definition ahDefs.H:83
Standard functor implementations and comparison objects.
void put_itor_at_the_end(Itor &it) noexcept
Definition aleph.H:54
@ KEY
Definition btreepic.C:169
Functor encompassing basic operation for extended binary search trees.
Iterator on nodes of the tree.
static constexpr int Pos_Not_Updated
void update_curr() const noexcept
Node * del()
Remove the current node and move the iterator one position forward.
Node * get_curr() const noexcept
Return the current node.
void reset_first() noexcept
Reset the iterator to the first position.
Iterator & operator=(const Iterator &itor) noexcept
static constexpr int Pos_Out_Of_Range
Iterator(const Gen_Treap_Rk &__tree, const size_t pos) noexcept
Initialize an iterator starting from the inorder position pos.
bool curr_updated() const noexcept
bool verify(const Iterator &it) const noexcept
static constexpr int Pos_Empty_Container
bool operator!=(const Iterator &itor) const
Return true if this is not equal to itor
bool operator==(const Iterator &itor) const noexcept(nothrow_compare_v< Compare, Key >)
Return true if this and itor are on the same position.
Node * get_curr_ne() const noexcept
Return the current node.
void update_pos() const noexcept(nothrow_compare_v< Compare, Key >)
void prev()
Move the iterator one position backward.
bool has_curr() const noexcept(nothrow_compare_v< Compare, Key >)
Return true if the iterator has a current node.
size_t get_current_position() const
return the position of current node
Iterator(const Gen_Treap_Rk &__tree) noexcept
Initialize an iterator on __tree
void reset_last() noexcept
Reset the iterator to the last position.
size_t get_pos() const
See get_current_position().
void reset_to_pos(const size_t pos) noexcept
Put the iterator on the inorder position pos.
bool verify(Gen_Treap_Rk *r) const noexcept
void reset_to_key(const Key &key) noexcept(noexcept(tree_ptr->find_position(key)))
Put the iterator on the node of a key.
void next()
Move the iterator one position forward.
bool is_container_empty() const noexcept
static constexpr int Pos_Not_Current
static int saturated_pos(const size_t pos) noexcept
void reset_to_node(Node *node) noexcept
Put the current node to a specific node.
Iterator(const Iterator &itor) noexcept
Iterator(const Gen_Treap_Rk &__tree, Node *__curr) noexcept
Initialize an iterator starting from node __curr
bool pos_updated() const noexcept
void end() noexcept
Put the iterator in the end state.
bool is_updated() const noexcept
void next_ne() noexcept
Move the iterator one position forward, without the check of next().
Extended Treap with rank support for O(log n) indexed access.
Gen_Treap_Rk(const unsigned long seed, Compare __cmp=Compare())
Builds an empty treap whose priorities are those of GSL for seed: it draws them from a Mersenne Twist...
Node head
The type of node.
Compare & key_comp() noexcept
return the comparison criteria
Node *& getRoot() noexcept
Return the tree's root.
void split_key_dup(const Key &key, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2) noexcept(nothrow_compare_v< Compare, Key >)
Split the tree according to a key that could be in the tree.
static Node * remove_pos(Node *&root, const size_t pos) noexcept
const Random_Source & random_source() const noexcept
The source of random numbers, to look at it: its engine, or whether it is equal to another one.
bool verify() const
Return true if the treap is consistent.
Compare & get_compare() noexcept
See key_comp().
Node * select(const size_t i) const
Return the i-th node in order sense.
void join_exclusive(Gen_Treap_Rk &t) noexcept
Join exclusive of this with t
size_t size() const noexcept
Return the number of nodes contained in the tree.
NodeType< Key > Node
static Node * join_exclusive(Node *t1, Node *t2) noexcept
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove a key from the tree.
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search or insert a key.
bool insert(Node *&root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
void join_dup(Node *&t1, Node *t2) noexcept
Node * search_or_insert(Node *&root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Node * insert_dup(Node *&root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
void split_pos(size_t pos, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2)
Split the tree at the inorder position pos.
Node * remove(const size_t beg, const size_t end)
Remove from the treap all the keys between inorder position beg and end.
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node in a treap.
void join(Node *&t1, Node *t2, Node *&dup) noexcept
Node * remove_pos(const size_t pos)
Remove the node at the inorder position pos.
Random_Source r
Gen_Treap_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 treap with a comparison criteria built by default, which draws its priorities from a ...
Random_Source & random_source() noexcept
The source of random numbers, which gives the priorities.
Gen_Treap_Rk(Gen_Treap_Rk &&tree) noexcept(bin_node_detail::nothrow_header_from_v< Node > and bin_node_detail::nothrow_compare_taken_v< Compare >)
Takes the nodes, the comparison criteria and the generator of tree, which keeps no nodes,...
Node * getRoot() const noexcept
bool split_key(const Key &key, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2) noexcept(nothrow_compare_v< Compare, Key >)
Split the tree according to a key.
void set_seed(const unsigned long seed)
Seeds the Mersenne Twister of the treap, as gsl_rng_set() did: the priorities are from then on those ...
void join_dup(Gen_Treap_Rk &t) noexcept
Join this with t independently of the presence of duplicated keys.
void join(Gen_Treap_Rk &t, Gen_Treap_Rk &dup) noexcept
Join this with t filtering the duplicated keys.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a key in a treap.
void swap(Gen_Treap_Rk &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and tree.
Gsl_Compatible_Rng & rng()
The Mersenne Twister of the treap, which a treap that was given no seed makes here,...
bool is_empty() const noexcept
Return true if tree is empty.
Node * remove(Node *&root, const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
std::pair< int, Node * > find_position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Find the inorder position of a key in the tree.
Gen_Treap_Rk(Compare __cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty treap with the comparison criteria __cmp, which draws its priorities from a SplitMix6...
static unsigned long generator_seed(const unsigned long seed) noexcept
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node in the tree.
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:259
The source of random numbers of a randomized structure, which draws from one of two generators.
Definition ah-random.H:883
@ Mersenne_Twister
A Gsl_Compatible_Rng, in dynamic memory.
unsigned long get() noexcept
The next number, as operator()().
Definition ah-random.H:1128
Gsl_Compatible_Rng & mersenne_twister()
The Mersenne Twister of the source, which is its engine from now on.
Definition ah-random.H:1087
void seed(const std::uint64_t s) noexcept
Draws from now on from the SplitMix64 of a seed.
Definition ah-random.H:1040
void swap(Random_Source &o) noexcept
Swaps the generators, with their states.
Definition ah-random.H:1021
void reset() noexcept
Definition tpl_treapRk.H:88
TreapRkNode_Data() noexcept
Definition tpl_treapRk.H:74
unsigned long priority
Definition tpl_treapRk.H:68
unsigned long & getPriority() noexcept
Definition tpl_treapRk.H:84
unsigned long & getCount() noexcept
Definition tpl_treapRk.H:86
TreapRkNode_Data(SentinelCtor) noexcept
Definition tpl_treapRk.H:79
Strict weak ordering constraint for BST comparators.
__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
std::pair< int, Node * > position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Compute the inorder position of a key.
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.
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.
#define DECLARE_BINNODE_SENTINEL(Name, height, Control_Data)
Specify tree node for a binary tree.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search 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.
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.
unsigned long & PRIO(Node *p) noexcept
Access the priority of a treap node.
Definition treapNode.H:120
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Search a key in a binary search tree using optimistic search.
Node * rotate_to_right_xt(Node *p) noexcept
Rotate to right the extended bianry tree with root p
constexpr bool nothrow_compare_v
Whether Compare compares two keys of type Key without throwing, as the operations of the trees call i...
Main namespace for Aleph-w library functions.
bool is_treap(Node *root) noexcept
Validate that a tree satisfies treap (heap) property.
Definition treapNode.H:144
and
Check uniqueness with explicit hash + equality functors.
const long Max_Priority
Maximum priority value (used for sentinel/leaves)
Definition treapNode.H:75
const long Min_Priority
Minimum priority value.
Definition treapNode.H:78
STL namespace.
Extended treap (a special type of randomized binary search tree) which manages selection and splittin...
Extended treap (a special type of randomized binary search tree) which manages selection and splittin...
#define RLINK(i, n)
#define LLINK(i, n)
ValueArg< size_t > seed
Definition testHash.C:53
Binary tree operations (split, join, rotate).
Treap node definition with BST key and heap priority.
DynList< int > l