Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_interval_tree.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
58# ifndef TPL_INTERVAL_TREE_H
59# define TPL_INTERVAL_TREE_H
60
61# include <ah-random.H>
62
63# include <cassert>
64# include <ctime>
65# include <optional>
66# include <type_traits>
67
68# include <ahFunction.H>
69# include <tpl_binNodeUtils.H>
70# include <treapNode.H>
71# include <tpl_binTreeOps.H>
72# include <ah-errors.H>
73# include <ah-concepts.H>
74# include <ah-dry.H>
75# include <ahDry.H>
76# include <htlist.H>
77# include <utility>
78# include <memory>
79
80namespace Aleph
81{
90 template <typename T>
91 struct Interval
92 {
95
100 noexcept(std::is_nothrow_default_constructible_v<T>)
101 : low(), high() {}
102
108 Interval(const T & lo, const T & hi)
109 noexcept(std::is_nothrow_copy_constructible_v<T>)
110 : low(lo), high(hi) {}
111
117 Interval(T && lo, T && hi)
118 noexcept(std::is_nothrow_move_constructible_v<T>)
119 : low(std::move(lo)), high(std::move(hi)) {}
120
126 static Interval point(const T & p)
127 noexcept(std::is_nothrow_copy_constructible_v<T>)
128 {
129 return {p, p};
130 }
131
139 template <class Compare = Aleph::less<T>>
140 [[nodiscard]] bool overlaps(const Interval & other,
141 const Compare & cmp = Compare()) const
142 noexcept(NothrowPredicateWith<const Compare &, const T &, const T &>)
143 {
144 // [a,b] overlaps [c,d] iff a <= d and c <= b
145 return not static_cast<bool>(cmp(other.high, low))
146 and not static_cast<bool>(cmp(high, other.low));
147 }
148
156 template <class Compare = Aleph::less<T>>
157 [[nodiscard]] bool contains(const T & p,
158 const Compare & cmp = Compare()) const
159 noexcept(NothrowPredicateWith<const Compare &, const T &, const T &>)
160 {
161 return not static_cast<bool>(cmp(p, low)) and not static_cast<bool>(cmp(high, p));
162 }
163
170 template <class Compare = Aleph::less<T>>
171 [[nodiscard]] bool is_valid(const Compare & cmp = Compare()) const
172 noexcept(NothrowPredicateWith<const Compare &, const T &, const T &>)
173 {
174 return not static_cast<bool>(cmp(high, low));
175 }
176
183 bool operator ==(const Interval & other) const
184 noexcept(noexcept(static_cast<bool>(std::declval<const T &>() == std::declval<const T &>())))
185 {
186 return static_cast<bool>(low == other.low) and static_cast<bool>(high == other.high);
187 }
188
194 bool operator !=(const Interval & other) const
195 noexcept(noexcept(*this == other))
196 {
197 return not (*this == other);
198 }
199 };
200
207 template <typename T, class Compare = Aleph::less<T>>
209 {
210 Compare cmp;
211
216 Interval_Less(const Compare & c = Compare())
217 noexcept(std::is_nothrow_copy_constructible_v<Compare>)
218 : cmp(c) {}
219
226 bool operator ()(const Interval<T> & a, const Interval<T> & b) const
228 {
229 if (static_cast<bool>(cmp(a.low, b.low)))
230 return true;
231 if (static_cast<bool>(cmp(b.low, a.low)))
232 return false;
233 return static_cast<bool>(cmp(a.high, b.high));
234 }
235 };
236
240 template <typename Key>
242
244 template <typename T>
246 {
247 using type = T;
248 };
249
250 template <typename Key>
252
261 template <typename T>
263 {
264 unsigned long priority;
266
267 public:
269 noexcept(std::is_nothrow_default_constructible_v<T>)
271
273 noexcept(std::is_nothrow_default_constructible_v<T>)
275
276 unsigned long &getPriority() noexcept { return priority; }
277
278 T &getMaxEndpoint() noexcept { return max_endpoint; }
279 const T &getMaxEndpoint() const noexcept { return max_endpoint; }
280
281 static void reset() noexcept
282 { /* empty */
283 }
284 };
285
295 template <typename Key>
297 : public Interval_Tree_Node_Data<interval_endpoint_t<Key>>
298 {
299 public:
302
303 static constexpr size_t MaxHeight = 80;
305
306 using key_type = Key;
307 using Key_Type = Key;
308
309 private:
310 Key key = Key();
313
314 // sentinel node
316
317 public:
318 Key &get_key() noexcept { return key; }
319 const Key &get_key() const noexcept { return key; }
320
321 Interval_Tree_Node *&getL() noexcept { return lLink; }
322 Interval_Tree_Node *&getR() noexcept { return rLink; }
323 const Interval_Tree_Node * getL() const noexcept { return lLink; }
324 const Interval_Tree_Node * getR() const noexcept { return rLink; }
325
328 {
329 this->getMaxEndpoint() = k.high;
330 }
331
333 noexcept(std::is_nothrow_move_constructible_v<Key> and
334 std::is_nothrow_default_constructible_v<Data> and
335 std::is_nothrow_copy_assignable_v<Endpoint>)
336 : key(std::move(k)), lLink(NullPtr), rLink(NullPtr)
337 {
338 this->getMaxEndpoint() = std::as_const(key).high;
339 }
340
342 : Data(node),
343 key(node.key), lLink(NullPtr), rLink(NullPtr)
344 {
345 /* empty */
346 }
347
349 noexcept(std::is_nothrow_move_constructible_v<Key> and
350 std::is_nothrow_move_constructible_v<Data>)
351 : Data(std::move(static_cast<Data &>(node))),
352 key(std::move(node.key)), lLink(NullPtr), rLink(NullPtr)
353 {
354 /* empty */
355 }
356
359 {
360 /* empty */
361 }
362
368 void reset() noexcept(std::is_nothrow_copy_assignable_v<Endpoint>)
369 {
370 Data::reset();
371 this->getMaxEndpoint() = std::as_const(key).high;
372 rLink = lLink = NullPtr;
373 }
374
375 private:
376 // sentinel constructor
378 : Data(sentinelCtor), lLink(nullptr), rLink(nullptr)
379 {
380 /* empty - links set to nullptr will be fixed when NullPtr is initialized */
381 }
382 };
383
385 template <typename Key>
387
388 template <typename Key>
391
396 template <typename Key>
398 : public Interval_Tree_Node_Data<interval_endpoint_t<Key>>
399 {
400 public:
403
404 static constexpr size_t MaxHeight = 80;
406
407 using key_type = Key;
408 using Key_Type = Key;
409
410 private:
411 Key key = Key();
414
416
417 public:
418 Key &get_key() noexcept { return key; }
419 const Key &get_key() const noexcept { return key; }
420
421 Interval_Tree_NodeVtl *&getL() noexcept { return lLink; }
422 Interval_Tree_NodeVtl *&getR() noexcept { return rLink; }
423 const Interval_Tree_NodeVtl * getL() const noexcept { return lLink; }
424 const Interval_Tree_NodeVtl * getR() const noexcept { return rLink; }
425
428 {
429 this->getMaxEndpoint() = k.high;
430 }
431
433 noexcept(std::is_nothrow_move_constructible_v<Key> and
434 std::is_nothrow_default_constructible_v<Data> and
435 std::is_nothrow_copy_assignable_v<Endpoint>)
436 : key(std::move(k)), lLink(NullPtr), rLink(NullPtr)
437 {
438 this->getMaxEndpoint() = std::as_const(key).high;
439 }
440
442 : Data(node),
443 key(node.key), lLink(NullPtr), rLink(NullPtr)
444 {
445 /* empty */
446 }
447
449 noexcept(std::is_nothrow_move_constructible_v<Key> and
450 std::is_nothrow_move_constructible_v<Data>)
451 : Data(std::move(static_cast<Data &>(node))),
452 key(std::move(node.key)), lLink(NullPtr), rLink(NullPtr)
453 {
454 /* empty */
455 }
456
459 {
460 /* empty */
461 }
462
464 virtual ~Interval_Tree_NodeVtl() = default;
465
471 void reset() noexcept(std::is_nothrow_copy_assignable_v<Endpoint>)
472 {
473 Data::reset();
474 this->getMaxEndpoint() = std::as_const(key).high;
475 rLink = lLink = NullPtr;
476 }
477
478 private:
480 : Data(sentinelCtor), lLink(nullptr), rLink(nullptr)
481 {
482 /* empty */
483 }
484 };
485
487 template <typename Key>
489
490 template <typename Key>
493
495 template <class Node>
496 inline auto &MAX_EP(Node *p) noexcept { return p->getMaxEndpoint(); }
497
498 template <class Node>
499 inline const auto &MAX_EP(const Node *p) noexcept
500 {
501 return p->getMaxEndpoint();
502 }
503
504 namespace interval_tree_detail
505 {
516 template <typename T, bool = std::is_nothrow_copy_assignable_v<T>>
518 {
519 const T *from = nullptr;
520
521 public:
526 void set(const T & max) noexcept { from = std::addressof(max); }
527
531 void give(T & max) const noexcept
532 {
533 if (from != nullptr)
534 max = *from;
535 }
536 };
537
549 template <typename T>
550 class Pending_Max<T, false>
551 {
552 std::optional<T> copy;
553
554 public:
559 void set(const T & max) { copy.emplace(max); }
560
566 void give(T & max) noexcept(std::is_nothrow_move_assignable_v<T>)
567 {
568 if (not copy.has_value())
569 return;
570 if constexpr (std::is_move_assignable_v<T>)
571 max = std::move(*copy);
572 else
573 max = *copy; // its move assignment is deleted
574 }
575 };
576 }
577
600 template <template <typename> class NodeType,
601 typename T,
602 class Compare = Aleph::less<T>>
603 requires StrictWeakOrder<Compare, T>
605 {
606 public:
609 using Node = NodeType<Key>;
610
611 private:
615 mutable Random_Source r; // const members give its Mersenne Twister
616 Compare cmp;
617 size_t num_nodes;
618
619 // The seed of the generator for that of a constructor, as it was taken
620 // for GSL: through an unsigned int, modulo 2^32 - 1
621 static unsigned long generator_seed(const unsigned long seed) noexcept
622 {
623 return static_cast<unsigned int>(seed)
624 % static_cast<unsigned long>(Gsl_Compatible_Rng::max());
625 }
626
628
630 struct Step
631 {
634 bool left;
636 };
637
639 Node *& link_from(Step *step) noexcept
640 {
641 if (step == nullptr)
642 return tree_root;
643 return step->left ? LLINK(step->node) : RLINK(step->node);
644 }
645
648 static bool goes_before(const Key & a, const Key & b, const Compare & cmp)
649 {
650 if (cmp(a.low, b.low))
651 return true;
652 if (cmp(b.low, a.low))
653 return false;
654 return static_cast<bool>(cmp(a.high, b.high));
655 }
656
659 static int side_of(const Key & a, const Key & b, const Compare & cmp)
660 {
661 if (cmp(a.low, b.low))
662 return -1;
663 if (cmp(b.low, a.low))
664 return 1;
665 if (cmp(a.high, b.high))
666 return -1;
667 return cmp(b.high, a.high) ? 1 : 0;
668 }
669
683 [[gnu::noinline]] void place(Node *p, Step *bottom)
684 {
685 const Compare & less = cmp;
686 const T & high = KEY(p).high;
687
688 // The nodes that p goes above are those of the end of the way whose
689 // priority is greater. Each one keeps its subtree of the other side
690 // of the way, and takes, at the side of the way, the nodes below it
691 // that are at its side of p
692 Step *above = bottom; // the first step that stays above p
693 const T *lmax = nullptr; // the maximum of what goes to the left of p,
694 const T *rmax = nullptr; // and of what goes to its right
695 for (; above != nullptr and PRIO(p) < PRIO(above->node); above = above->up)
696 {
697 Node *q = above->node;
698 const T *& side = above->left ? rmax : lmax;
699 const T *max = std::addressof(KEY(q).high);
700 if (Node *kept = above->left ? RLINK(q) : LLINK(q);
701 kept != Node::NullPtr and less(*max, MAX_EP(kept)))
702 max = std::addressof(MAX_EP(kept));
703 if (side != nullptr and less(*max, *side))
704 max = side;
705 above->max.set(*max);
706 side = max;
707 }
708
709 Pending own; // the maximum of p, if it is not its high endpoint
710 {
711 const T *max = std::addressof(high);
712 if (lmax != nullptr and less(*max, *lmax))
713 max = lmax;
714 if (rmax != nullptr and less(*max, *rmax))
715 max = rmax;
716 if (max != std::addressof(high))
717 own.set(*max);
718 }
719
720 // The nodes that stay above p take its high endpoint if their maximum
721 // is less; above the first one whose maximum is not, none is
722 Step *whole = above; // the first step whose maximum stays
723 for (; whole != nullptr and less(MAX_EP(whole->node), high); whole = whole->up)
724 whole->max.set(high);
725
726 // Every comparison and every copy is made: from here on nothing
727 // throws. The maxima go first, and then the links
728 for (Step *s = bottom; s != whole; s = s->up)
729 s->max.give(MAX_EP(s->node));
730 own.give(MAX_EP(p));
731
732 Node *l = Node::NullPtr; // what hangs from p at its left,
733 Node *r = Node::NullPtr; // and at its right
734 for (Step *s = bottom; s != above; s = s->up)
735 if (s->left)
736 {
737 LLINK(s->node) = r;
738 r = s->node;
739 }
740 else
741 {
742 RLINK(s->node) = l;
743 l = s->node;
744 }
745 LLINK(p) = l;
746 RLINK(p) = r;
747 link_from(above) = p;
748 }
749
753 {
754 if (root == Node::NullPtr)
755 {
756 place(p, up);
757 return p;
758 }
759
760 const int side = side_of(KEY(p), KEY(root), cmp);
761 if (side == 0)
762 return Node::NullPtr; // duplicate
763 Step step{root, up, side < 0, Pending()};
764 return insert(step.left ? LLINK(root) : RLINK(root), p, &step);
765 }
766
769 void insert_dup(Node *root, Node *p, Step *up)
770 {
771 if (root == Node::NullPtr)
772 {
773 place(p, up);
774 return;
775 }
776
777 Step step{root, up, goes_before(KEY(p), KEY(root), cmp), Pending()};
778 insert_dup(step.left ? LLINK(root) : RLINK(root), p, &step);
779 }
780
800 Node * join(Node *t1, Node *t2, Step *up, const Step *whole, Step *spine)
801 {
802 if (t1 == Node::NullPtr or t2 == Node::NullPtr)
803 {
804 // Every comparison and every copy is made: from here on nothing
805 // throws. The maxima go first; the links, on the way back
806 for (Step *s = up; s != whole; s = s->up)
807 s->max.give(MAX_EP(s->node));
808 for (Step *s = spine; s != nullptr; s = s->up)
809 s->max.give(MAX_EP(s->node));
810 return t1 == Node::NullPtr ? t2 : t1;
811 }
812
813 // The root is the one of less priority, and the other tree goes down
814 // its inner spine: its maximum grows if that of the other is greater.
815 // The maximum that it takes does not change later: it is greater than
816 // those of the nodes of this spine that go below the other
817 const bool first = PRIO(t1) < PRIO(t2);
818 Node *top = first ? t1 : t2;
819 Node *other = first ? t2 : t1;
820 Step grown{top, spine, not first, Pending()};
821 if (const Compare & less = cmp; less(MAX_EP(top), MAX_EP(other)))
822 grown.max.set(MAX_EP(other));
823
824 Node *rest = first ? join(RLINK(t1), t2, up, whole, &grown)
825 : join(t1, LLINK(t2), up, whole, &grown);
826 (first ? RLINK(top) : LLINK(top)) = rest;
827 return top;
828 }
829
840 [[gnu::noinline]] void unlink(Node *p, Step *up)
841 {
842 const Compare & less = cmp;
843 const T & high = KEY(p).high;
844 Node *t1 = LLINK(p);
845 Node *t2 = RLINK(p);
846
847 Step self{p, up, false, Pending()}; // p goes out with its high
848 self.max.set(high); // endpoint as its maximum, given with the others
849
850 // The maximum of a node above p may fall only if the high endpoint of
851 // p is not less than it; above the first one where it is less, none
852 // falls. The new one is the greatest of its high endpoint, the
853 // maximum of its other subtree and that of what stays below it
854 Step *whole = up; // the first step whose maximum stays
855 const T *below = nullptr; // the maximum of what stays below the step
856 for (bool first = true;
857 whole != nullptr and not less(high, MAX_EP(whole->node));
858 whole = whole->up, first = false)
859 {
860 if (first) // what takes the place of p: its two subtrees
861 {
862 if (t1 != Node::NullPtr)
863 below = std::addressof(MAX_EP(t1));
864 if (t2 != Node::NullPtr and
865 (below == nullptr or less(*below, MAX_EP(t2))))
866 below = std::addressof(MAX_EP(t2));
867 }
868 Node *q = whole->node;
869 const T *max = std::addressof(KEY(q).high);
870 if (Node *kept = whole->left ? RLINK(q) : LLINK(q);
871 kept != Node::NullPtr and less(*max, MAX_EP(kept)))
872 max = std::addressof(MAX_EP(kept));
873 if (below != nullptr and less(*max, *below))
874 max = below;
875 whole->max.set(*max);
876 below = max;
877 }
878
879 Node *joined = join(t1, t2, &self, whole, nullptr);
880 link_from(up) = joined;
881 LLINK(p) = RLINK(p) = Node::NullPtr;
882 }
883
886 Node * remove(Node *root, const Key & key, Step *up)
887 {
888 if (root == Node::NullPtr)
889 return Node::NullPtr;
890
891 const int side = side_of(key, KEY(root), cmp);
892 if (side == 0)
893 {
894 unlink(root, up);
895 return root;
896 }
897 Step step{root, up, side < 0, Pending()};
898 return remove(step.left ? LLINK(root) : RLINK(root), key, &step);
899 }
900
902 template <class Op>
903 static void all_overlaps(Node *root, const Key & query,
904 Op & op, const Compare & cmp)
905 {
906 if (root == Node::NullPtr)
907 return;
908
909 // Prune: if max_endpoint < query.low, no overlap in this subtree
910 if (cmp(MAX_EP(root), query.low))
911 return;
912
913 // Recurse left
914 all_overlaps(LLINK(root), query, op, cmp);
915
916 // Check current node
917 if (KEY(root).overlaps(query, cmp))
918 op(KEY(root));
919
920 // Prune right: if root.low > query.high, no overlap in right subtree
921 if (cmp(query.high, KEY(root).low))
922 return;
923
924 // Recurse right
925 all_overlaps(RLINK(root), query, op, cmp);
926 }
927
929 static bool check_max(Node *root, const Compare & cmp)
930 {
931 if (root == Node::NullPtr)
932 return true;
933
934 T expected = KEY(root).high;
935 if (LLINK(root) != Node::NullPtr)
936 if (cmp(expected, MAX_EP(LLINK(root))))
938 if (RLINK(root) != Node::NullPtr)
939 if (cmp(expected, MAX_EP(RLINK(root))))
941
943 return false; // MAX_EP(root) != expected
944
946 }
947
948 static size_t count_nodes(Node *root) noexcept
949 {
950 if (root == Node::NullPtr)
951 return 0;
952 return 1 + count_nodes(LLINK(root)) + count_nodes(RLINK(root));
953 }
954
955 public:
965 void set_seed(const unsigned long seed)
966 {
968 }
969
973 IntervalCmp key_comp() const noexcept(std::is_nothrow_copy_constructible_v<Compare>)
974 {
975 return IntervalCmp(cmp);
976 }
977
979 const Compare & endpoint_comp() const noexcept { return cmp; }
980
982 const Compare & get_compare() const noexcept { return endpoint_comp(); }
983
986 noexcept(noexcept(std::swap(tree_root, tree.tree_root)) and
987 noexcept(std::swap(cmp, tree.cmp)) and
988 noexcept(r.swap(tree.r)) and
989 noexcept(std::swap(num_nodes, tree.num_nodes)))
990 {
991 std::swap(tree_root, tree.tree_root);
992 std::swap(cmp, tree.cmp);
993 r.swap(tree.r);
994 std::swap(num_nodes, tree.num_nodes);
995 }
996
1006 Gen_Interval_Tree(const unsigned long seed,
1007 const Compare & _cmp = Compare())
1009 r(Random_Source::Engine::Mersenne_Twister, generator_seed(seed)),
1010 cmp(_cmp), num_nodes(0)
1011 {
1013 }
1014
1020 Gen_Interval_Tree(const Compare & _cmp = Compare())
1022 {
1024 }
1025
1034
1036 [[nodiscard]] Random_Source & random_source() noexcept { return r; }
1037
1040 [[nodiscard]] const Random_Source & random_source() const noexcept { return r; }
1041
1054 [[deprecated("it makes a Mersenne Twister in a tree that has none: "
1055 "use random_source(); it goes in 8.0.0")]]
1056 [[nodiscard]] Gsl_Compatible_Rng & rng() const { return r.mersenne_twister(); }
1057
1058# if ALEPH_WITH_GSL
1067 [[deprecated("the generator is no longer of GSL: use random_source(); it goes in 8.0.0")]]
1068 [[nodiscard]] gsl_rng * gsl_rng_object() const { return r.gsl(); }
1069# endif
1070
1072 Node *&getRoot() noexcept { return tree_root; }
1073
1075 const Node * getRoot() const { return tree_root; }
1076
1078 [[nodiscard]] size_t size() const noexcept { return num_nodes; }
1079
1081 [[nodiscard]] bool is_empty() const noexcept { return num_nodes == 0; }
1082
1084 void reset_num_nodes() noexcept { num_nodes = 0; }
1085
1093 const Node * search(const Key & key) const
1094 noexcept(std::is_nothrow_copy_constructible_v<Compare> and
1096 {
1097 Node *ret_val = searchInBinTree(tree_root, key, key_comp());
1098 return ret_val == Node::NullPtr ? nullptr : ret_val;
1099 }
1100
1117 {
1118 assert(p != Node::NullPtr);
1119 PRIO(p) = r.get();
1120 p->getMaxEndpoint() = KEY(p).high;
1121 if (insert(tree_root, p, nullptr) == Node::NullPtr)
1122 return nullptr;
1123 ++num_nodes;
1124 return p;
1125 }
1126
1137 {
1138 assert(p != Node::NullPtr);
1139 PRIO(p) = r.get();
1140 p->getMaxEndpoint() = KEY(p).high;
1141 insert_dup(tree_root, p, nullptr);
1142 ++num_nodes;
1143 return p;
1144 }
1145
1161 Node * remove(const Key & key)
1162 {
1163 Node *ret_val = remove(tree_root, key, nullptr);
1164 if (ret_val == Node::NullPtr)
1165 return nullptr;
1166 --num_nodes;
1167 return ret_val;
1168 }
1169
1177 const Node * overlap_search(const Key & query) const
1178 {
1179 Node *x = tree_root;
1180 while (x != Node::NullPtr)
1181 {
1182 if (KEY(x).overlaps(query, cmp))
1183 return x;
1184
1185 if (LLINK(x) != Node::NullPtr and
1186 not cmp(MAX_EP(LLINK(x)), query.low))
1187 x = LLINK(x);
1188 else
1189 x = RLINK(x);
1190 }
1191 return nullptr;
1192 }
1193
1201 template <class Op>
1202 void all_overlaps(const Key & query, Op && op) const
1203 {
1204 all_overlaps(tree_root, query, op, cmp);
1205 }
1206
1212 [[nodiscard]] DynList<Key> find_all_overlaps(const Key & query) const
1213 {
1214 DynList<Key> result;
1215 all_overlaps(query, [&result](const Key & iv) { result.append(iv); });
1216 return result;
1217 }
1218
1227 template <class Op>
1228 void stab(const T & p, Op && op) const
1229 {
1230 Key point_iv = Key::point(p);
1231 all_overlaps(point_iv, std::forward<Op>(op));
1232 }
1233
1239 [[nodiscard]] DynList<Key> find_stab(const T & p) const
1240 {
1241 return find_all_overlaps(Key::point(p));
1242 }
1243
1248 [[nodiscard]] bool verify() const
1249 {
1250 if (not check_bst(tree_root, key_comp()))
1251 return false;
1252 if (not is_treap(tree_root))
1253 return false;
1254 if (not check_max(tree_root, cmp))
1255 return false;
1257 return false;
1258 return true;
1259 }
1260
1262 struct Iterator : public BinNodeInfixIterator<Node>
1263 {
1264 Iterator() noexcept = default;
1265
1268 };
1269 };
1270
1277 template <typename T, class Compare = Aleph::less<T>>
1278 requires StrictWeakOrder<Compare, T>
1280 : public Gen_Interval_Tree<Interval_Tree_Node, T, Compare>
1281 {
1283 using Base::Base;
1284 };
1285
1292 template <typename T, class Compare = Aleph::less<T>>
1295 : public Gen_Interval_Tree<Interval_Tree_NodeVtl, T, Compare>
1296 {
1298 using Base::Base;
1299 };
1300
1327 template <typename T, class Compare = Aleph::less<T>>
1330 : public EqualSortedMethod<DynIntervalTree<T, Compare>>,
1331 public FunctionalMethods<DynIntervalTree<T, Compare>, Interval<T>>,
1332 public GenericKeys<DynIntervalTree<T, Compare>, Interval<T>>,
1333 public StlAlephIterator<DynIntervalTree<T, Compare>>
1334 {
1335 public:
1338 using Key_Type = Key;
1341
1342 private:
1344
1345 void copy_from(const DynIntervalTree & src)
1346 {
1347 src.for_each([this](const Key & iv) { append(iv); });
1348 }
1349
1351 {
1354 }
1355
1356 public:
1360 DynIntervalTree(const Compare & cmp = Compare()) : tree(cmp) {}
1361
1366 DynIntervalTree(const unsigned long seed,
1367 const Compare & cmp = Compare())
1368 : tree(seed, cmp) {}
1369
1374 : tree(src.tree.endpoint_comp())
1375 {
1377 tmp.copy_from(src);
1378 this->swap(tmp);
1379 }
1380
1391 : tree(src.tree.endpoint_comp())
1392 {
1393 tree.swap(src.tree);
1394 }
1395
1401 {
1402 if (this == &src)
1403 return *this;
1404 DynIntervalTree tmp(src);
1405 swap(tmp);
1406 return *this;
1407 }
1408
1414 noexcept(noexcept(std::declval<Tree &>().swap(std::declval<Tree &>())))
1415 {
1416 if (this == &src)
1417 return *this;
1418 destroy_tree();
1419 tree.swap(src.tree);
1420 return *this;
1421 }
1422
1425
1429 void swap(DynIntervalTree & other)
1430 noexcept(noexcept(std::declval<Tree &>().swap(std::declval<Tree &>())))
1431 {
1432 tree.swap(other.tree);
1433 }
1434
1436
1451 Key &insert(const Key & iv)
1452 {
1454 << "Invalid interval: low > high";
1455 std::unique_ptr<Node> p(new Node(iv));
1456 Node *result = tree.insert(p.get());
1457 if (result == nullptr)
1458 ah_domain_error() << "Duplicate interval";
1459
1460 p.release();
1461 return KEY(result);
1462 }
1463
1479 {
1480 ah_domain_error_if(not iv.is_valid(tree.endpoint_comp()))
1481 << "Invalid interval: low > high";
1482 // The tree takes p, or gives nullptr for a duplicate; the guard frees
1483 // p if it is a duplicate or if the insertion throws. It is not a
1484 // unique_ptr, whose release() would say that the caller takes p
1485 struct Guard
1486 {
1487 Node *p;
1488 ~Guard() { delete p; }
1489 } guard{new Node(std::move(iv))};
1490
1491 Node *result = tree.insert(guard.p);
1492 if (result == nullptr)
1493 ah_domain_error() << "Duplicate interval";
1494
1495 guard.p = nullptr; // the tree took it
1496 return KEY(result);
1497 }
1498
1506 Key &insert(const T & lo, const T & hi)
1507 {
1508 return insert(Key(lo, hi));
1509 }
1510
1524 Key &insert_dup(const Key & iv)
1525 {
1527 << "Invalid interval: low > high";
1528 struct Guard // frees p if the insertion throws, as in insert()
1529 {
1530 Node *p;
1531 ~Guard() { delete p; }
1532 } guard{new Node(iv)};
1533
1534 Node *p = tree.insert_dup(guard.p);
1535 guard.p = nullptr; // the tree took it
1536 return KEY(p);
1537 }
1538
1544 Key &append(const Key & iv) { return insert_dup(iv); }
1545
1558 bool remove(const Key & iv)
1559 {
1560 Node *p = tree.remove(iv);
1561 if (p == nullptr)
1562 return false;
1563 delete p;
1564 return true;
1565 }
1566
1573 bool remove(const T & lo, const T & hi)
1574 {
1575 return remove(Key(lo, hi));
1576 }
1577
1585 const Key * search(const Key & iv) const
1586 noexcept(noexcept(std::declval<const Tree &>().search(iv)))
1587 {
1588 auto *p = tree.search(iv);
1589 return p != nullptr ? &KEY(p) : nullptr;
1590 }
1591
1598 const Key * overlap_search(const Key & query) const
1599 {
1600 auto *p = tree.overlap_search(query);
1601 return p != nullptr ? &KEY(p) : nullptr;
1602 }
1603
1609 const Key * overlap_search(const T & lo, const T & hi) const
1610 {
1611 return overlap_search(Key(lo, hi));
1612 }
1613
1621 template <class Op>
1622 void all_overlaps(const Key & query, Op && op) const
1623 {
1624 tree.all_overlaps(query, std::forward<Op>(op));
1625 }
1626
1632 [[nodiscard]] DynList<Key> find_all_overlaps(const Key & query) const
1633 {
1634 return tree.find_all_overlaps(query);
1635 }
1636
1642 [[nodiscard]] DynList<Key> find_all_overlaps(const T & lo,
1643 const T & hi) const
1644 {
1645 return find_all_overlaps(Key(lo, hi));
1646 }
1647
1654 template <class Op>
1655 void stab(const T & p, Op && op) const
1656 {
1657 tree.stab(p, std::forward<Op>(op));
1658 }
1659
1664 [[nodiscard]] DynList<Key> find_stab(const T & p) const
1665 {
1666 return tree.find_stab(p);
1667 }
1668
1673 [[nodiscard]] size_t size() const noexcept { return tree.size(); }
1674
1681 [[nodiscard]] auto key_comp() const noexcept(std::is_nothrow_copy_constructible_v<Compare>)
1682 {
1683 return tree.key_comp();
1684 }
1685
1690 [[nodiscard]] bool is_empty() const noexcept { return tree.is_empty(); }
1691
1696 [[nodiscard]] bool empty() const noexcept { return is_empty(); }
1697
1701 [[nodiscard]] bool verify() const { return tree.verify(); }
1702
1712 template <class Operation>
1714 bool traverse(Operation & op)
1715 {
1716 return Aleph::traverse(tree.getRoot(), [&op](const Node *p)
1717 {
1718 return op(p->get_key());
1719 });
1720 }
1721
1728 template <class Operation>
1730 bool traverse(Operation && op = Operation())
1731 {
1732 return traverse<Operation>(op);
1733 }
1734
1741 template <class Operation>
1743 bool traverse(Operation & op) const
1744 {
1745 return Aleph::traverse(tree.getRoot(), [&op](const Node *p)
1746 {
1747 return op(p->get_key());
1748 });
1749 }
1750
1757 template <class Operation>
1759 bool traverse(Operation && op = Operation()) const
1760 {
1761 return traverse<Operation>(op);
1762 }
1763
1769 {
1771
1772 public:
1774 Iterator() noexcept = default;
1775
1779 Iterator(const DynIntervalTree & t) : it(t.tree) {}
1780
1784 [[nodiscard]] bool has_curr() const noexcept { return it.has_curr(); }
1785
1790 const Key &get_curr() const
1791 {
1792 return KEY(static_cast<const Node*>(it.get_curr()));
1793 }
1794
1799 const Key &get_curr_ne() const noexcept
1800 {
1801 return KEY(static_cast<const Node*>(it.get_curr_ne()));
1802 }
1803
1807 void next() { it.next(); }
1808
1814 void next_ne() noexcept(noexcept(it.next_ne())) { it.next_ne(); }
1815
1819 void reset_first() noexcept(noexcept(it.reset_first())) { it.reset_first(); }
1820
1824 void end() noexcept { it.end(); }
1825
1830 [[nodiscard]] size_t get_pos() const { return it.get_pos(); }
1831 };
1832 };
1833} // end namespace Aleph
1834
1835# endif // TPL_INTERVAL_TREE_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Container traversal and functional operation mixins.
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
Definition ah-errors.H:702
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
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
@ sentinelCtor
Definition ahDefs.H:83
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:123
Standard functor implementations and comparison objects.
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
Inorder iterator on the nodes of a binary tree.
Node * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
void next()
Move the iterator one position forward.
Node * get_curr() const
Return the current node. Throw overflow_error if there is no current.
bool has_curr() const noexcept
Return true the iterator has current node.
void next_ne()
Move forward without checking the position; it may throw bad_alloc, as the constructor does.
void reset_first()
Reset the iterator to the first node inorder; it may throw bad_alloc, as the constructor does.
size_t get_pos() const
Return the current position of iterator. Only valid if has_curr() == true.
Inorder iterator yielding Interval<T> keys.
bool has_curr() const noexcept
Check if there is a current element.
void end() noexcept
Move to the position after the last element.
size_t get_pos() const
Return the 0-indexed current position.
void reset_first() noexcept(noexcept(it.reset_first()))
Reset to the first element in traversal order.
const Key & get_curr_ne() const noexcept
Return the current interval (no check).
void next_ne() noexcept(noexcept(it.next_ne()))
Advance to the next element (no check).
void next()
Advance to the next element.
Iterator() noexcept=default
Default constructor.
const Key & get_curr() const
Return the current interval.
High-level interval tree with automatic memory management.
Key & insert(const T &lo, const T &hi)
Insert an interval given endpoints (unique).
DynList< Key > find_all_overlaps(const Key &query) const
Find all overlapping intervals.
const Key * search(const Key &iv) const noexcept(noexcept(std::declval< const Tree & >().search(iv)))
Search for an exact interval.
DynIntervalTree(const unsigned long seed, const Compare &cmp=Compare())
Construct with explicit seed for the internal RNG.
DynList< Key > find_stab(const T &p) const
Find all intervals containing point p.
DynList< Key > find_all_overlaps(const T &lo, const T &hi) const
Overload of find_all_overlaps() using endpoints.
bool is_empty() const noexcept
Return true if the tree contains no intervals.
Key & insert_dup(const Key &iv)
Insert interval, allowing duplicates.
void copy_from(const DynIntervalTree &src)
auto key_comp() const noexcept(std::is_nothrow_copy_constructible_v< Compare >)
Return the comparator that orders the intervals: by low endpoint, then by high endpoint.
Key & insert(const Key &iv)
Insert an interval (unique).
const Key * overlap_search(const T &lo, const T &hi) const
Overload of overlap_search() using endpoints.
const Key * overlap_search(const Key &query) const
Find any interval overlapping query.
void all_overlaps(const Key &query, Op &&op) const
Apply op to every overlapping interval.
void swap(DynIntervalTree &other) noexcept(noexcept(std::declval< Tree & >().swap(std::declval< Tree & >())))
Swap state with another tree in O(1).
DynIntervalTree & operator=(const DynIntervalTree &src)
Copy assignment.
bool verify() const
Verify all tree invariants (BST, Treap, MaxEndpoint).
Key & append(const Key &iv)
Append an interval (alias for insert_dup).
DynIntervalTree(DynIntervalTree &&src)
Move constructor.
bool empty() const noexcept
Return true if the tree contains no intervals (STL compatible).
bool traverse(Operation &op)
Traverse all intervals in inorder (sorted by low endpoint).
void stab(const T &p, Op &&op) const
Apply op to every interval containing point p.
size_t size() const noexcept
Return the number of intervals in the tree.
bool remove(const T &lo, const T &hi)
Remove an interval given endpoints.
Key & insert(Key &&iv)
Insert an interval (move version, unique).
bool remove(const Key &iv)
Remove an interval from the tree.
bool traverse(Operation &op) const
Const overload of traverse().
bool traverse(Operation &&op=Operation()) const
Const rvalue overload of traverse().
bool traverse(Operation &&op=Operation())
Overload of traverse() for rvalue callbacks.
typename Interval_Tree< T, Compare >::Node Node
DynIntervalTree(const Compare &cmp=Compare())
Default constructor.
DynIntervalTree(const DynIntervalTree &src)
Copy constructor.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
Augmented treap storing intervals with overlap/stabbing queries.
Node * insert_dup(Node *p)
Insert a node (duplicates allowed).
DynList< Key > find_all_overlaps(const Key &query) const
Find all intervals overlapping query.
size_t size() const noexcept
Return the number of nodes.
Gen_Interval_Tree(const unsigned long seed, const Compare &_cmp=Compare())
Construct with explicit seed and comparator: the priorities are those of GSL for the seed,...
bool verify() const
Verify BST, heap, and max_endpoint invariants.
static int side_of(const Key &a, const Key &b, const Compare &cmp)
The side of b where a goes: -1 before it, 1 after it, and 0 if they are the same interval.
void unlink(Node *p, Step *up)
Take a node out of the tree: its two subtrees, joined, take its place.
Interval_Less< T, Compare > IntervalCmp
const Compare & endpoint_comp() const noexcept
Return the endpoint comparison functor (const)
void insert_dup(Node *root, Node *p, Step *up)
Go down to the place of p and put it there, after the intervals that are the same as its own.
const Compare & get_compare() const noexcept
Backwards-compatible accessor for the endpoint comparator.
interval_tree_detail::Pending_Max< T > Pending
Node * remove(const Key &key)
Remove an interval from the tree.
IntervalCmp key_comp() const noexcept(std::is_nothrow_copy_constructible_v< Compare >)
Return the interval comparison functor (over Interval<T> keys).
void stab(const T &p, Op &&op) const
Apply op to every interval containing point p.
Node * insert(Node *root, Node *p, Step *up)
Go down to the place of p and put it there, unless its interval is in the tree, which then gives Node...
Gsl_Compatible_Rng & rng() const
The Mersenne Twister of the tree, which a tree that was given no seed makes here, and draws from it f...
Node *& getRoot() noexcept
Return the tree's root.
static void all_overlaps(Node *root, const Key &query, Op &op, const Compare &cmp)
Recursive: find all intervals overlapping query.
Node * insert(Node *p)
Insert a node (no duplicates).
static unsigned long generator_seed(const unsigned long seed) noexcept
Gen_Interval_Tree(const Compare &_cmp=Compare())
Construct without a seed: the priorities are drawn from a SplitMix64 with a seed of its own,...
Gen_Interval_Tree & operator=(const Gen_Interval_Tree &)=delete
Deleted copy assignment.
Node *& link_from(Step *step) noexcept
The link that hangs from a step: that of the root if there is none.
Gen_Interval_Tree(const Gen_Interval_Tree &)=delete
Deleted copy constructor.
bool is_empty() const noexcept
Return true if the tree is empty.
void reset_num_nodes() noexcept
Reset the node count (used after manual tree destruction)
const Node * overlap_search(const Key &query) const
Find any interval that overlaps with query (CLRS algorithm).
static bool check_max(Node *root, const Compare &cmp)
Verify max_endpoint invariant.
Random_Source & random_source() noexcept
The source of random numbers, which gives the priorities.
void place(Node *p, Step *bottom)
Put a node in its place, at the end of the way down.
void set_seed(const unsigned long seed)
Seeds the Mersenne Twister of the tree, as gsl_rng_set() did: the priorities are from then on those o...
Node * join(Node *t1, Node *t2, Step *up, const Step *whole, Step *spine)
Join two trees, every interval of the first one before those of the second one.
void all_overlaps(const Key &query, Op &&op) const
Apply op to every interval overlapping query.
static size_t count_nodes(Node *root) noexcept
DynList< Key > find_stab(const T &p) const
Find all intervals containing point p.
const Node * search(const Key &key) const noexcept(std::is_nothrow_copy_constructible_v< Compare > and NothrowPredicateWith< const Compare &, const T &, const T & >)
Search for an exact interval in the tree.
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.
Gen_Interval_Tree(Gen_Interval_Tree &&)=delete
Deleted move constructor.
const Node * getRoot() const
Return the tree's root (const)
Node * remove(Node *root, const Key &key, Step *up)
Go down to the node of the interval key and take it out of the tree.
void swap(Gen_Interval_Tree &tree) noexcept(noexcept(std::swap(tree_root, tree.tree_root)) and noexcept(std::swap(cmp, tree.cmp)) and noexcept(r.swap(tree.r)) and noexcept(std::swap(num_nodes, tree.num_nodes)))
Swap all state with tree in O(1)
static bool goes_before(const Key &a, const Key &b, const Compare &cmp)
True if a goes before b: by its low endpoint, then by its high one.
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
Interval tree node with virtual destructor.
void reset() noexcept(std::is_nothrow_copy_assignable_v< Endpoint >)
Leave the node as one that is in no tree: without links, and with the high endpoint of its interval a...
virtual ~Interval_Tree_NodeVtl()=default
Virtual destructor.
const Interval_Tree_NodeVtl * getL() const noexcept
interval_endpoint_t< Key > Endpoint
static Interval_Tree_NodeVtl *const NullPtr
const Key & get_key() const noexcept
Interval_Tree_NodeVtl *& getL() noexcept
Interval_Tree_NodeVtl * rLink
Interval_Tree_NodeVtl(const Interval_Tree_NodeVtl &node)
Interval_Tree_NodeVtl * lLink
Interval_Tree_NodeVtl *& getR() noexcept
static Interval_Tree_NodeVtl sentinel_node
The sentinel node instance (virtual version).
Interval_Tree_NodeVtl(Interval_Tree_NodeVtl &&node) noexcept(std::is_nothrow_move_constructible_v< Key > and std::is_nothrow_move_constructible_v< Data >)
Interval_Tree_NodeVtl(Key &&k) noexcept(std::is_nothrow_move_constructible_v< Key > and std::is_nothrow_default_constructible_v< Data > and std::is_nothrow_copy_assignable_v< Endpoint >)
const Interval_Tree_NodeVtl * getR() const noexcept
static constexpr size_t MaxHeight
Data portion of an interval tree node.
Interval_Tree_Node_Data() noexcept(std::is_nothrow_default_constructible_v< T >)
const T & getMaxEndpoint() const noexcept
unsigned long & getPriority() noexcept
Interval_Tree_Node_Data(SentinelCtor) noexcept(std::is_nothrow_default_constructible_v< T >)
Interval tree node with sentinel support.
Interval_Tree_Node *& getR() noexcept
const Interval_Tree_Node * getR() const noexcept
void reset() noexcept(std::is_nothrow_copy_assignable_v< Endpoint >)
Leave the node as one that is in no tree: without links, and with the high endpoint of its interval a...
const Key & get_key() const noexcept
static constexpr size_t MaxHeight
static Interval_Tree_Node *const NullPtr
interval_endpoint_t< Key > Endpoint
static Interval_Tree_Node sentinel_node
The sentinel node instance.
Interval_Tree_Node(Key &&k) noexcept(std::is_nothrow_move_constructible_v< Key > and std::is_nothrow_default_constructible_v< Data > and std::is_nothrow_copy_assignable_v< Endpoint >)
Interval_Tree_Node(Interval_Tree_Node &&node) noexcept(std::is_nothrow_move_constructible_v< Key > and std::is_nothrow_move_constructible_v< Data >)
Interval_Tree_Node * lLink
Interval_Tree_Node *& getL() noexcept
const Interval_Tree_Node * getL() const noexcept
Interval_Tree_Node(const Interval_Tree_Node &node)
Interval_Tree_Node * rLink
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
Minimal std::expected-style result type for C++20.
void set(const T &max)
Take note of the new maximum.
void give(T &max) noexcept(std::is_nothrow_move_assignable_v< T >)
Give the node its new maximum, if it has one.
std::optional< T > copy
The new maximum, if there is one.
What the maximum endpoint of a node is to become.
void set(const T &max) noexcept
Take note of the new maximum.
void give(T &max) const noexcept
Give the node its new maximum, if it has one.
const T * from
Where the new maximum is, if there is one.
Equality test for containers sorted by a comparator.
Definition ah-dry.H:3115
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:1235
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
PredicateWith<F, Args...>, and neither the call nor the conversion of its result to bool can throw.
f(args...) is a valid call whose result is usable as a condition.
Strict weak ordering constraint for BST comparators.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4188
__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
bool traverse(Node *root, Op op)
See infix_traverse().
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
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.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
typename interval_endpoint< Key >::type interval_endpoint_t
auto & MAX_EP(Node *p) noexcept
Access max_endpoint of an interval tree node.
bool is_treap(Node *root) noexcept
Validate that a tree satisfies treap (heap) property.
Definition treapNode.H:144
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
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.
Inorder iterator over nodes.
A step of the way down from the root: a node, and the side taken there.
Pending max
The new maximum of the node, if it changes.
bool left
True if the way goes on through the left link.
Step * up
The step above; nullptr at the root.
BST comparator for intervals: order by (low, then high).
bool operator()(const Interval< T > &a, const Interval< T > &b) const noexcept(NothrowPredicateWith< const Compare &, const T &, const T & >)
Comparison operator.
Interval_Less(const Compare &c=Compare()) noexcept(std::is_nothrow_copy_constructible_v< Compare >)
Constructor.
Interval tree using nodes with virtual destructor.
Interval tree using nodes without virtual destructor.
Closed interval [low, high].
bool contains(const T &p, const Compare &cmp=Compare()) const noexcept(NothrowPredicateWith< const Compare &, const T &, const T & >)
Return true if this interval contains the point p.
static Interval point(const T &p) noexcept(std::is_nothrow_copy_constructible_v< T >)
Construct a point interval [p, p].
bool operator==(const Interval &other) const noexcept(noexcept(static_cast< bool >(std::declval< const T & >()==std::declval< const T & >())))
Equality operator.
Interval() noexcept(std::is_nothrow_default_constructible_v< T >)
Initializes the interval as [T{}, T{}].
bool is_valid(const Compare &cmp=Compare()) const noexcept(NothrowPredicateWith< const Compare &, const T &, const T & >)
Return true if low <= high.
bool overlaps(const Interval &other, const Compare &cmp=Compare()) const noexcept(NothrowPredicateWith< const Compare &, const T &, const T & >)
Return true if this interval overlaps with other.
T low
Lower endpoint.
Interval(T &&lo, T &&hi) noexcept(std::is_nothrow_move_constructible_v< T >)
Construct interval [lo, hi] (move version).
T high
Upper endpoint.
bool operator!=(const Interval &other) const noexcept(noexcept(*this==other))
Inequality operator.
Interval(const T &lo, const T &hi) noexcept(std::is_nothrow_copy_constructible_v< T >)
Construct interval [lo, hi].
Trait to extract endpoint type from Interval<T>.
Generic list of items stored in a container.
Definition ah-dry.H:2896
#define RLINK(i, n)
#define LLINK(i, n)
ValueArg< size_t > seed
Definition testHash.C:53
static int * k
Utility functions for binary tree operations.
Binary tree operations (split, join, rotate).
Treap node definition with BST key and heap priority.
DynList< int > l