Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynSetTree.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
45# ifndef TPL_DYNSETTREE_H
46# define TPL_DYNSETTREE_H
47
48# include <cassert>
49# include <concepts>
50# include <typeinfo>
51# include <type_traits>
52# include <utility>
53# include <algorithm>
54# include <memory>
55# include <stdexcept>
56# include <ah-errors.H>
57# include <ah-args-ctor.H>
58# include <ahIterator.H>
59# include <ah-zip.H>
60# include <ah-arena.H>
61# include <tpl_binNodeUtils.H>
62# include <tpl_binNodeXt.H>
63# include <tpl_binTree.H>
64# include <tpl_treap.H>
65# include <tpl_treapRk.H>
66# include <tpl_avl.H>
67# include <tpl_avlRk.H>
68# include <tpl_rand_tree.H>
69# include <tpl_rb_tree.H>
70# include <tpl_rbRk.H>
71# include <tpl_tdRbTree.H>
72# include <tpl_tdRbTreeRk.H>
73# include <tpl_hRbTree.H>
74# include <tpl_hRbTreeRk.H>
75# include <tpl_splay_tree.H>
76# include <tpl_splay_treeRk.H>
77# include <ah-concepts.H>
78
79using namespace Aleph;
80
81namespace Aleph
82{
83 template <typename Node>
85 {
86 public:
88
89 virtual Node * alloc_lval(const typename Node::key_type & key) = 0;
90
91 virtual Node * alloc_rval(typename Node::key_type && key) = 0;
92
93 virtual void unalloc(Node *) = 0;
94
95 virtual ~AbstractTreeNodeAllocator() = default;
96 };
97
98 template <typename Node>
100 {
101 public:
102 Node * alloc_lval(const typename Node::key_type & key) override
103 {
104 return new Node(key);
105 }
106
107 Node * alloc_rval(typename Node::key_type && key) override
108 {
109 return new Node(std::forward<typename Node::key_type>(key));
110 }
111
112 void unalloc(Node *p) override
113 {
114 delete p;
115 }
116
117 ~DftTreeNodeAllocator() override = default;
118 };
119
135 template <typename Node>
137 {
139
141 ArenaTreeAllocator(const size_t & sz = 1024 * 1024)
142 : arena(sz)
143 {
144 // empty
145 }
146
148 ArenaTreeAllocator(const char *base_addr, const size_t & sz)
149 : arena(base_addr, sz)
150 {
151 // empty
152 }
153
155 Node * alloc_lval(const typename Node::key_type & key) override
156 {
157 return build(key);
158 }
159
161 Node * alloc_rval(typename Node::key_type && key) override
162 {
163 return build(std::forward<typename Node::key_type>(key));
164 }
165
167 void unalloc(Node *p) override
168 {
169 const bool own = arena.contains(p);
170 p->~Node();
171 if (own)
172 give_back(p);
173 }
174
176 void pin() noexcept { pinned = true; }
177
179 [[nodiscard]] bool is_pinned() const noexcept { return pinned; }
180
185 void reset() noexcept
186 {
187 assert(not pinned);
188 free_slots = nullptr;
189 arena.reset();
190 }
191
193 [[nodiscard]] size_t allocated_size() const noexcept
194 {
195 return arena.allocated_size();
196 }
197
199 [[nodiscard]] size_t available_size() const noexcept
200 {
201 return arena.available_size();
202 }
203
205
206 private:
209 {
211 };
212
213 Free_Slot * free_slots = nullptr;
214 bool pinned = false;
215
217 void * take() noexcept
218 {
219 if (free_slots == nullptr)
220 return arena.alloc_aligned(sizeof(Node), alignof(Node));
221
222 Free_Slot * slot = free_slots;
223 free_slots = slot->next;
224 return slot;
225 }
226
228 void give_back(void * p) noexcept
229 {
230 static_assert(sizeof(Node) >= sizeof(Free_Slot) and
231 alignof(Node) >= alignof(Free_Slot));
232
233 const size_t before = arena.allocated_size();
234 arena.dealloc(p, sizeof(Node));
235 if (arena.allocated_size() == before)
236 free_slots = ::new (p) Free_Slot{free_slots};
237 }
238
240 template <class K>
241 Node * build(K && key)
242 {
243 void * room = take();
244 ah_bad_alloc_if(room == nullptr);
245 try
246 {
247 return ::new (room) Node(std::forward<K>(key));
248 }
249 catch (...)
250 {
251 give_back(room);
252 throw;
253 }
254 }
255 };
256
257 template <typename Container, typename T>
259 {
260 bool operator ()(const Container & c1, const Container & c2) const
261 {
262 return std::lexicographical_compare(c1.begin(), c1.end(), c2.begin(), c2.end(),
263 [](const T & item1, const T & item2)
264 {
265 return item1 < item2;
266 });
267 }
268 };
269
353 template <typename Key,
354 template <typename, class> class Tree = Avl_Tree,
355 class Compare = Aleph::less<Key>>
356 requires StrictWeakOrder<Compare, Key> &&
357 BSTPolicy<Tree<Key, Compare>, Key>
359 : public LocateFunctions<DynSetTree<Key, Tree, Compare>, Key>,
360 public FunctionalMethods<DynSetTree<Key, Tree, Compare>, Key>,
361 public GenericKeys<DynSetTree<Key, Tree, Compare>, Key>,
362 public EqualSortedMethod<DynSetTree<Key, Tree, Compare>>,
363 public StlAlephIterator<DynSetTree<Key, Tree, Compare>>
364 {
365 public:
369
371
372 private:
373 template <typename T>
375 {
376 // The const forms cover RB, AVL, etc.; the non-const ones cover the
377 // splay tree, whose lookups restructure the tree.
378 static constexpr bool has_select =
379 requires(const T & t) { t.select(std::declval<size_t>()); } or
380 requires(T & t) { t.select(std::declval<size_t>()); };
381 static constexpr bool has_remove_pos =
382 requires(T & t) { t.remove_pos(std::declval<size_t>()); };
383 static constexpr bool has_split_pos =
384 requires(T & t, T & l, T & r) { t.split_pos(std::declval<size_t>(), l, r); };
385 static constexpr bool has_position =
386 requires(const T & t, const Key & k) { t.position(k); } or
387 requires(T & t, const Key & k) { t.position(k); };
388 static constexpr bool has_find_position =
389 requires(const T & t, const Key & k) { t.find_position(k); } or
390 requires(T & t, const Key & k) { t.find_position(k); };
391 };
392
393 // call_select for const trees (RB, AVL, etc.)
394 template <typename T>
395 static auto call_select(const T & t, const size_t i, int)
396 -> decltype(t.select(i))
397 {
398 return t.select(i);
399 }
400
401 // call_select for non-const trees (Splay - mutates on access)
402 template <typename T>
403 static auto call_select_nc(T & t, const size_t i, int)
404 -> decltype(t.select(i))
405 {
406 return t.select(i);
407 }
408
409 template <typename T>
410 static Node * call_select(const T &, const size_t, ...)
411 {
412 ah_domain_error() << "select is not supported by underlying tree";
413 return nullptr;
414 }
415
416 template <typename T>
417 static Node * call_select_nc(T &, const size_t, ...)
418 {
419 ah_domain_error() << "select is not supported by underlying tree";
420 return nullptr;
421 }
422
423 template <typename T>
424 static auto call_remove_pos(T & t, const size_t i, int)
425 -> decltype(t.remove_pos(i))
426 {
427 return t.remove_pos(i);
428 }
429
430 template <typename T>
431 static Node * call_remove_pos(T &, const size_t, ...)
432 {
433 ah_domain_error() << "remove_pos is not supported by underlying tree";
434 return nullptr;
435 }
436
437 template <typename T>
438 static auto call_split_pos(T & t, const size_t pos, T & l, T & r, int)
439 -> decltype(t.split_pos(pos, l, r), void())
440 {
441 t.split_pos(pos, l, r);
442 }
443
444 template <typename T>
445 static void call_split_pos(T &, const size_t, T &, T &, ...)
446 {
447 ah_domain_error() << "split_pos is not supported by underlying tree";
448 }
449
450 template <typename T>
451 static auto call_position(const T & t, const Key & key, int)
452 -> decltype(t.position(key))
453 {
454 return t.position(key);
455 }
456
457 template <typename T>
458 static std::pair<long, Node *> call_position(const T &, const Key &, ...)
459 {
460 ah_domain_error() << "position is not supported by underlying tree";
461 return std::pair<long, Node *>(0, nullptr);
462 }
463
464 template <typename T>
465 static auto call_find_position(const T & t, const Key & key, int)
466 -> decltype(t.find_position(key))
467 {
468 return t.find_position(key);
469 }
470
471 template <typename T>
472 static std::pair<long, Node *> call_find_position(const T &, const Key &, ...)
473 {
474 ah_domain_error() << "find_position is not supported by underlying tree";
475 return std::pair<long, Node *>(0, nullptr);
476 }
477
478 static constexpr size_t dim = 13;
479
481 size_t num_nodes;
482 std::unique_ptr<ArenaTreeAllocator<Node>> arena_allocator;
483
484 Node * alloc_node(const Key & key)
485 {
486 if (arena_allocator)
487 return arena_allocator->alloc_lval(key);
488 return new Node(key);
489 }
490
491 Node * alloc_node(Key && key)
492 {
493 if (arena_allocator)
494 return arena_allocator->alloc_rval(std::forward<Key>(key));
495 return new Node(std::forward<Key>(key));
496 }
497
499 {
500 if (arena_allocator)
501 arena_allocator->unalloc(p);
502 else
503 delete p;
504 }
505
507 void pin_arena() noexcept
508 {
509 if (arena_allocator)
510 arena_allocator->pin();
511 }
512
513 // A copy of the key of p, a node that left the tree, which it frees.
514 // The node leaves the set even if the copy throws: freed, so that it
515 // does not leak, and not counted, so that size() stays that of the tree
516 Key release(Node *p)
517 {
518 struct Release
519 {
520 DynSetTree * set;
521 Node * node;
522
523 ~Release()
524 {
525 set->free_node(node);
526 --set->num_nodes;
527 }
528 } release_p{this, p};
529
530 return p->get_key();
531 }
532
533 public:
535
536 typedef Key Item_Type;
537
538 typedef Key Key_Type;
539
545 void swap(DynSetTree & dset)
546 noexcept(noexcept(tree.swap(dset.tree)) and
547 noexcept(std::swap(num_nodes, dset.num_nodes)) and
548 noexcept(std::swap(arena_allocator, dset.arena_allocator)))
549 {
550 tree.swap(dset.tree);
551 std::swap(num_nodes, dset.num_nodes);
552 std::swap(arena_allocator, dset.arena_allocator);
553 }
554
555 private:
563 static constexpr bool nothrow_empty_tree =
564 std::is_nothrow_default_constructible_v<Compare> and
565 std::is_nothrow_constructible_v<Tree<Key, Compare>, Compare>;
566
567 public:
586 : tree(Compare()), num_nodes(0)
587 {
588 // empty
589 }
590
597 DynSetTree(const Compare & cmp)
598 noexcept(std::is_nothrow_constructible_v<Tree<Key, Compare>,
599 const Compare &>)
600 : tree(cmp), num_nodes(0)
601 {
602 // empty
603 }
604
606 DynSetTree(const char *base_addr, const size_t & sz,
607 const Compare & cmp = Compare())
608 : tree(cmp), num_nodes(0),
609 arena_allocator(std::make_unique<ArenaTreeAllocator<Node>>(base_addr, sz))
610 {
611 // empty
612 }
613
615 explicit DynSetTree(const size_t & arena_sz,
616 const Compare & cmp = Compare())
617 : tree(cmp), num_nodes(0),
618 arena_allocator(std::make_unique<ArenaTreeAllocator<Node>>(arena_sz))
619 {
620 // empty
621 }
622
624 DynSetTree(const DynSetTree & srcTree)
625 : tree(srcTree.tree.get_compare()), num_nodes(srcTree.num_nodes)
626 {
627 Node *srcRoot = srcTree.tree.getRoot();
628 try
629 {
630 tree.getRoot() = copyRec(srcRoot);
631 }
632 catch (...)
633 {
634 num_nodes = 0;
635 throw;
636 }
637 }
638
640
673 noexcept(std::is_nothrow_move_constructible_v<Tree<Key, Compare>>)
674 requires (not std::is_copy_constructible_v<Tree<Key, Compare>>)
675 : tree(std::move(srcTree.tree)), num_nodes(srcTree.num_nodes),
676 arena_allocator(std::move(srcTree.arena_allocator))
677 {
678 // the move of a tree leaves the other one without nodes
679 assert(srcTree.tree.getRoot() == Node::NullPtr);
680 srcTree.num_nodes = 0;
681 }
682
700 noexcept(nothrow_empty_tree and noexcept(swap(srcTree)))
701 requires std::is_copy_constructible_v<Tree<Key, Compare>>
702 : tree(Compare()), num_nodes(0)
703 {
704 // The comparison of the empty tree is built above, as the default
705 // constructor does, and not as the default argument of the
706 // constructor of the tree
707 swap(srcTree);
708 }
709
716 void empty()
717 {
718 if (arena_allocator)
719 {
720 if (arena_allocator->is_pinned())
721 {
722 const auto release = [this] (Node *p) noexcept { free_node(p); };
723 dispose_tree_nodes(tree.getRoot(), release);
724 }
725 else
726 {
727 // The keys are destroyed, and the arena takes back the nodes
728 callKeyDestructorsRec(tree.getRoot());
729 arena_allocator->reset();
730 }
731
732 tree.getRoot() = Node::NullPtr;
733 num_nodes = 0;
734 return;
735 }
736
737 destroyRec(tree.getRoot());
738 tree.getRoot() = Node::NullPtr;
739 num_nodes = 0;
740 }
741
747 void clear() { empty(); }
748
750 {
751 return *this = DynSetTree(list);
752 }
753
768 {
769 if (this == &srcTree)
770 return *this;
771
772 DynSetTree copy(srcTree);
773 swap(copy);
774
775 return *this;
776 }
777
787 noexcept(noexcept(swap(srcTree)))
788 {
789 if (this == &srcTree)
790 return *this;
791
792 empty();
793 swap(srcTree);
794 return *this;
795 }
796
798 virtual ~DynSetTree()
799 {
800 empty();
801 }
802
803 private:
804 Key * __insert(Node *p)
805 {
806 Node *q = nullptr;
807 try
808 {
809 q = tree.search_or_insert(p);
810 }
811 catch (...)
812 {
813 free_node(p);
814 throw;
815 }
816
817 if (q != p)
818 return nullptr;
819
820 ++num_nodes;
821
822 return &p->get_key();
823 }
824
825 public:
834 Key * insert(const Key & key)
835 {
836 Node *p = alloc_node(key);
837 Key *key_p = __insert(p);
838 if (key_p == nullptr) // was there insertion?
839 { // No (KEY(p) is already in the tree) ==> free p and return nullptr
840 free_node(p);
841 return nullptr;
842 }
843
844 return key_p;
845 }
846
847 Key * insert(Key && key)
848 {
849 Node *p = alloc_node(std::forward<Key>(key));
850 Key *key_p = __insert(p);
851 if (key_p == nullptr) // was there insertion?
852 {
853 free_node(p);
854 return nullptr;
855 }
856
857 return key_p;
858 }
859
860 Key * append(const Key & key)
861 {
862 return insert(key);
863 }
864
865 Key * append(Key && key)
866 {
867 return insert(std::forward<Key>(key));
868 }
869
870 private:
872 {
873 Node *q = nullptr;
874 try
875 {
876 q = tree.search_or_insert(p);
877 }
878 catch (...)
879 {
880 free_node(p);
881 throw;
882 }
883 if (q != p) // was there an insertion?
884 free_node(p); // No (KEY(p) is already in the tree) ==> free
885 // allocated node
886 else
887 ++num_nodes;
888
889 return &q->get_key();
890 }
891
892 std::pair<Node *, bool> __contains_or_insert(Node *p)
893 {
894 Node *q = nullptr;
895 try
896 {
897 q = tree.search_or_insert(p);
898 }
899 catch (...)
900 {
901 free_node(p);
902 throw;
903 }
904 if (q != p)
905 { // KEY(p) is already inside the tree
906 free_node(p);
907 return std::pair<Node *, bool>(q, true);
908 }
909 ++num_nodes;
910 return std::pair<Node *, bool>(p, false);
911 }
912
913 public:
926 Key * search_or_insert(const Key & key)
927 {
928 return __search_or_insert(alloc_node(key));
929 }
930
931 Key * search_or_insert(Key && key)
932 {
933 return
934 __search_or_insert(alloc_node(std::forward<Key>(key)));
935 }
936
937 /* Look for the key <code>key</code> in the binary search tree and
938 eventually inserts it if it is not found.
939
940 <code>contains_or_insert(key)</code> searches the tree for the key
941 <code>key</code>. If the key is already found, then it is returned
942 true. Otherwise, the key is inserted and false is returned.
943
944 @param[in] key to find or insert
945 @return a std::pair whose first field is a pointer to the found or
946 inserted key, and the second field is a boolean whose value is
947 `false` if the key was inserted; `true` otherwise, that is if the
948 key is already present in the tree.
949 */
950 std::pair<Key *, bool> contains_or_insert(const Key & key)
951 {
952 auto p = __contains_or_insert(alloc_node(key));
953 return std::pair<Key *, bool>(&p.first->get_key(), p.second);
954 }
955
956 std::pair<Key *, bool> contains_or_insert(Key && key)
957 {
958 auto p = __contains_or_insert(alloc_node(std::forward<Key>(key)));
959 return std::pair<Key *, bool>(&p.first->get_key(), p.second);
960 }
961
962 private:
964 {
965 try
966 {
967 Node *p = tree.insert_dup(q);
968 ++num_nodes;
969 return &p->get_key();
970 }
971 catch (...)
972 {
973 free_node(q);
974 throw;
975 }
976 }
977
978 public:
979 Key * insert_dup(const Key & key)
980 {
981 return __insert_dup(alloc_node(key));
982 }
983
984 Key * insert_dup(Key && key)
985 {
986 return __insert_dup(alloc_node(std::forward<Key>(key)));
987 }
988
989 Key * put(const Key & key)
990 {
991 return insert(key);
992 }
993
994 Key * put(Key && key)
995 {
996 return insert(std::forward<Key>(key));
997 }
998
1006 size_t remove(const Key & key)
1007 {
1008 Node *p = static_cast<Node *>(tree.remove(key));
1009
1010 if (p == nullptr)
1011 return num_nodes;
1012
1013 free_node(p);
1014
1015 return --num_nodes;
1016 }
1017
1030 Key del(const Key & key)
1031 {
1032 Node *p = static_cast<Node *>(tree.remove(key));
1033
1034 ah_domain_error_if(p == nullptr)
1035 << "DynSetTree::del key is not found in the tree";
1036
1037 return release(p);
1038 }
1039
1049 Key remove_pos(const size_t i)
1050 {
1052 << "remove_pos is not supported by underlying tree";
1053
1055 << "remove_pos index out of range";
1056
1057 Node *p = static_cast<Node *>(call_remove_pos(tree, i, 0));
1058 ah_logic_error_if(p == nullptr)
1059 << "remove_pos returned nullptr";
1060
1061 return release(p);
1062 }
1063
1065 bool exist(const Key & key) const
1066 {
1067 return search(key) != nullptr;
1068 }
1069
1070 bool has(const Key & key) const
1071 {
1072 return exist(key);
1073 }
1074
1081 bool contains(const Key & key) const
1082 {
1083 return exist(key);
1084 }
1085
1100 Key &find(const Key & key)
1101 {
1102 Node *node = static_cast<Node *>(tree.search(key));
1103 ah_domain_error_if(node == nullptr)
1104 << "key not found";
1105 return node->get_key();
1106 }
1107
1110 const Key &find(const Key & key) const
1111 {
1112 Node *node = static_cast<Node *>(tree.search(key));
1113 ah_domain_error_if(node == nullptr)
1114 << "key not found";
1115 return node->get_key();
1116 }
1117
1132 std::pair<long, Key *> find_position(const Key & key) const
1133 {
1135 << "find_position is not supported by underlying tree";
1136
1137 if (num_nodes == 0)
1138 return std::pair<long, Key *>(0, nullptr);
1139
1140 auto p = call_find_position(tree, key, 0);
1141 if (p.second == nullptr)
1142 return std::pair<long, Key *>(static_cast<long>(p.first), nullptr);
1143
1144 return std::pair<long, Key *>(static_cast<long>(p.first),
1145 &p.second->get_key());
1146 }
1147
1162 Key * search(const Key & key)
1163 {
1164 Node *node = static_cast<Node *>(tree.search(key));
1165 if (node == nullptr)
1166 return nullptr;
1167 return &(node->get_key());
1168 }
1169
1172 const Key * search(const Key & key) const
1173 {
1174 Node *node = static_cast<Node *>(tree.search(key));
1175 if (node == nullptr)
1176 return nullptr;
1177 return &(node->get_key());
1178 }
1179
1182 const Key &min() const
1183 {
1185 << "set is empty";
1186
1187 return find_min(tree.getRoot())->get_key();
1188 }
1189
1191 const Key &get_first() const { return min(); }
1192
1195 const Key &max() const
1196 {
1198 << "set is empty";
1199
1200 return find_max(tree.getRoot())->get_key();
1201 }
1202
1204 const Key &get_last() const { return max(); }
1205
1207 const Key &get() const { return max(); }
1208
1210 const size_t &size() const { return num_nodes; }
1211
1213 bool is_empty() const { return num_nodes == 0; }
1214
1216 [[nodiscard]] bool uses_arena() const noexcept
1217 {
1218 return arena_allocator != nullptr;
1219 }
1220
1222 [[nodiscard]] size_t arena_allocated_size() const noexcept
1223 {
1224 if (arena_allocator)
1225 return arena_allocator->allocated_size();
1226 return 0;
1227 }
1228
1230 [[nodiscard]] size_t arena_available_size() const noexcept
1231 {
1232 if (arena_allocator)
1233 return arena_allocator->available_size();
1234 return 0;
1235 }
1236
1240 {
1241 return Aleph::internal_path_length(tree.getRoot());
1242 }
1243
1244 Node * get_root_node() const { return tree.getRoot(); }
1245
1246 const Key &get_root() const
1247 {
1249 << "Tree is empty";
1250 return KEY(tree.getRoot());
1251 }
1252
1254 const Key &get_item() const { return get_root(); }
1255
1257 size_t height() const { return computeHeightRec(tree.getRoot()); }
1258
1261 template <class Op>
1262 void for_each_in_preorder(void (*visitFct)(Node *, int, int))
1263 {
1264 Node *root = static_cast<Node *>(tree.getRoot());
1265 preOrderRec(root, visitFct);
1266 }
1267
1280 long position(const Key & key) const
1281 {
1283 << "position is not supported by underlying tree";
1284
1285 auto p = call_position(tree, key, 0);
1286 return static_cast<long>(p.first);
1287 }
1288
1294 Key &select(size_t i)
1295 {
1297 << "select is not supported by underlying tree";
1298
1300 << "select index out of range";
1301
1302 // Try non-const first (for Splay trees), then const
1303 Node *p = call_select_nc(tree, i, 0);
1304 ah_logic_error_if(p == nullptr)
1305 << "select returned nullptr";
1306 return p->get_key();
1307 }
1308
1309 const Key &select(size_t i) const
1310 {
1312 << "select is not supported by underlying tree";
1313
1315 << "select index out of range";
1316
1317 // For const version, only const select works (not for Splay trees)
1318 Node *p = call_select(tree, i, 0);
1319 ah_logic_error_if(p == nullptr)
1320 << "select returned nullptr";
1321 return p->get_key();
1322 }
1323
1324 Key &operator ()(size_t i)
1325 {
1326 return select(i);
1327 }
1328
1329 const Key &operator [](const Key & key) const
1330 {
1331 return find(key);
1332 }
1333
1334 const Key &operator [](const Key & key)
1335 {
1336 return *search_or_insert(key);
1337 }
1338
1339 Key &access(size_t i)
1340 {
1341 return select(i);
1342 }
1343
1344 bool verify() const
1345 {
1346 return tree.verify() and check_bst(tree.getRoot(), tree.get_compare());
1347 }
1348
1349 private:
1350 template <class Key_Op>
1351 struct Node_Op
1352 {
1353 Key_Op & key_op;
1354
1355 Node_Op(Key_Op & __key_op) : key_op(__key_op)
1356 { /* empty */
1357 }
1358
1359 Node_Op(Key_Op && __key_op) : key_op(__key_op)
1360 {
1361 /* empty */
1362 }
1363
1365 {
1366 assert(root != nullptr);
1367 key_op(KEY(root));
1368 }
1369 };
1370
1371 public:
1396 template <class Key_Op>
1397 void for_each_preorder(Key_Op & key_op) const
1398 {
1399 Node *root = static_cast<Node *>(tree.getRoot());
1400
1401 Node_Op<Key_Op> node_op(const_cast<Key_Op &>(key_op));
1402
1403 For_Each_Preorder<Node>()(root, node_op);
1404 }
1405
1413 template <class Key_Op>
1414 void for_each_preorder(Key_Op && key_op = Key_Op()) const
1415 {
1416 for_each_preorder<Key_Op>(key_op);
1417 }
1418
1443 template <class Key_Op>
1444 void for_each_inorder(Key_Op & key_op) const
1445 {
1446 Node *root = static_cast<Node *>(tree.getRoot());
1447
1448 Node_Op<Key_Op> node_op(const_cast<Key_Op &>(key_op));
1449
1450 For_Each_In_Order<Node>()(root, node_op);
1451 }
1452
1460 template <class Key_Op>
1461 void for_each_inorder(Key_Op && key_op = Key_Op()) const
1462 {
1463 for_each_inorder<Key_Op>(key_op);
1464 }
1465
1490 template <class Key_Op>
1491 void for_each_postorder(Key_Op & key_op) const
1492 {
1493 Node *root = static_cast<Node *>(tree.getRoot());
1494
1495 Node_Op<Key_Op> node_op(const_cast<Key_Op &>(key_op));
1496
1497 For_Each_Postorder<Node>()(root, node_op);
1498 }
1499
1507 template <class Key_Op>
1508 void for_each_postorder(Key_Op && key_op = Key_Op()) const
1509 {
1510 for_each_postorder<Key_Op>(key_op);
1511 }
1512
1530 {
1531 t.pin_arena(); // its nodes go to this, and maybe to dup
1532 const size_t dup_before = dup.num_nodes;
1533 tree.join(t.tree, dup.tree);
1534 t.tree.getRoot() = Node::NullPtr;
1535 t.num_nodes = 0;
1537 dup.num_nodes = compute_cardinality_rec(dup.tree.getRoot());
1538 if (dup.num_nodes != dup_before)
1539 pin_arena(); // the node that went to dup may be one of this
1540 return *this;
1541 }
1542
1553 {
1554 return join(t, dup);
1555 }
1556
1570 {
1571 t.pin_arena(); // its nodes come to this
1572 tree.join_dup(t.tree);
1573 t.num_nodes = 0;
1574 t.tree.getRoot() = Node::NullPtr;
1576 return *this;
1577 }
1578
1594 bool split_key(const Key & key, DynSetTree & l, DynSetTree & r)
1595 {
1596 if (not tree.split_key(key, l.tree, r.tree))
1597 return false;
1598
1599 pin_arena();
1600 tree.getRoot() = Node::NullPtr;
1601 num_nodes = 0;
1602 l.num_nodes = compute_cardinality_rec(l.tree.getRoot());
1603 r.num_nodes = compute_cardinality_rec(r.tree.getRoot());
1604
1605 return true;
1606 }
1607
1620 void split_pos(const size_t pos, DynSetTree & l, DynSetTree & r)
1621 {
1623 << "split_pos is not supported by underlying tree";
1624
1626 << "split_pos position out of range";
1627
1628 // Underlying split_pos splits at [0, pos) and [pos, N)
1629 // But we want [0, pos] and (pos, N), so we split at pos+1
1631 pin_arena();
1632 call_split_pos(tree, pos + 1, l.tree, r.tree, 0);
1633 tree.getRoot() = Node::NullPtr;
1634 num_nodes = 0;
1635 l.num_nodes = compute_cardinality_rec(l.tree.getRoot());
1636 r.num_nodes = compute_cardinality_rec(r.tree.getRoot());
1637 }
1638
1651 void split_key_dup(const Key & key, DynSetTree & l, DynSetTree & r)
1652 {
1653 pin_arena();
1654 tree.split_key_dup(key, l.tree, r.tree);
1655 tree.getRoot() = Node::NullPtr;
1656 num_nodes = 0;
1657 l.num_nodes = compute_cardinality_rec(l.tree.getRoot());
1658 r.num_nodes = compute_cardinality_rec(r.tree.getRoot());
1659 }
1660
1661 struct Iterator : public Tree_Type::Iterator
1662 {
1663 using Base = typename Tree_Type::Iterator;
1664
1666
1668 Iterator() noexcept = default;
1669
1672 { /* empty */
1673 }
1674
1676 Iterator(const DynSetTree &) = delete;
1677
1678 const Key &get_curr_ne() const noexcept
1679 {
1680 return Base::get_curr_ne()->get_key();
1681 }
1682
1683 Key &get_curr_ne() noexcept { return Base::get_curr_ne()->get_key(); }
1684
1685 const Key &get_curr() const { return Base::get_curr()->get_key(); }
1686
1687 Key &get_curr() { return Base::get_curr()->get_key(); }
1688 };
1689
1692
1710 template <class Operation>
1712 bool traverse(Operation & op)
1713 {
1714 return Aleph::traverse(tree.getRoot(), [&op](Node *p)
1715 {
1716 return op(p->get_key());
1717 });
1718 }
1719
1721 template <class Operation>
1723 bool traverse(Operation && op = Operation())
1724 {
1725 return traverse<Operation>(op);
1726 }
1727
1735 template <class Operation>
1737 bool traverse(Operation & op) const
1738 {
1739 return Aleph::traverse(tree.getRoot(), [&op](Node *p)
1740 {
1741 return op(static_cast<const Key &>(p->get_key()));
1742 });
1743 }
1744
1746 template <class Operation>
1748 bool traverse(Operation && op = Operation()) const
1749 {
1750 return traverse<Operation>(op);
1751 }
1752
1757 [[nodiscard]] Compare key_comp() const
1758 {
1759 return tree.get_compare();
1760 }
1761
1762 protected:
1776 [[nodiscard]] decltype(auto) get_compare() const
1777 noexcept(noexcept(tree.get_compare()))
1778 {
1779 return tree.get_compare();
1780 }
1781 };
1782
1783
1784# define SETTREE_ITOR(Name, Key, Cmp) \
1785 class Iterator : public DynSetTree<Key, Name, Cmp>::Iterator \
1786 { \
1787 public: \
1788 Iterator() : DynSetTree<Key, Name, Cmp>::Iterator() \
1789 { /* empty */ } \
1790 \
1791 Iterator(DynSetTree<Key, Name, Cmp> & tree) \
1792 : DynSetTree<Key, Name, Cmp>::Iterator(tree) \
1793 { /* empty */ } \
1794 };
1795
1796
1803 template <typename Key, class Compare = Aleph::less<Key>>
1804 class DynSetBinTree : public DynSetTree<Key, BinTree, Compare>
1805 {
1806 public:
1808 using Base::Base;
1810 };
1811
1812
1819 template <typename Key, class Compare = Aleph::less<Key>>
1820 class DynSetAvlTree : public DynSetTree<Key, Avl_Tree, Compare>
1821 {
1822 public:
1824 using Base::Base;
1826 };
1827
1828
1835 template <typename Key, class Compare = Aleph::less<Key>>
1836 class DynSetSplayTree : public DynSetTree<Key, Splay_Tree, Compare>
1837 {
1838 public:
1840 using Base::Base;
1842 };
1843
1844
1858 template <typename Key, class Compare = Aleph::less<Key>>
1859 class DynSetSplayRkTree : public DynSetTree<Key, Splay_Tree_Rk, Compare>
1860 {
1861 public:
1863 using Base::Base;
1866 };
1867
1868
1875 template <typename Key, class Compare = Aleph::less<Key>>
1876 class DynSetRandTree : public DynSetTree<Key, Rand_Tree, Compare>
1877 {
1878 public:
1880 using Base::Base;
1882
1883 class Iterator : public DynSetTree<Key, Rand_Tree, Compare>::Iterator
1884 {
1885 public:
1887 { /* empty */
1888 }
1889
1891 : DynSetTree<Key, Rand_Tree, Compare>::Iterator(tree)
1892 { /* empty */
1893 }
1894 };
1895 };
1896
1897
1904 template <typename Key, class Compare = Aleph::less<Key>>
1905 class DynSetTreap : public DynSetTree<Key, Treap, Compare>
1906 {
1907 public:
1909 using Base::Base;
1911 };
1912
1919 template <typename Key, class Compare = Aleph::less<Key>>
1920 class DynSetTreapRk : public DynSetTree<Key, Treap_Rk, Compare>
1921 {
1922 public:
1924 using Base::Base;
1926 SETTREE_ITOR(Treap_Rk, Key, Compare);
1927 };
1928
1929
1938 template <typename Key, class Compare = Aleph::less<Key>>
1939 class DynSetAvlRkTree : public DynSetTree<Key, Avl_Tree_Rk, Compare>
1940 {
1941 public:
1943 using Base::Base;
1946 };
1947
1948
1955 template <typename Key, class Compare = Aleph::less<Key>>
1956 class DynSetRbTree : public DynSetTree<Key, Rb_Tree, Compare>
1957 {
1958 public:
1960 using Base::Base;
1962 };
1963
1964
1975 template <typename Key, class Compare = Aleph::less<Key>>
1976 class DynSetTdRbTree : public DynSetTree<Key, TdRbTree, Compare>
1977 {
1978 public:
1980 using Base::Base;
1982 };
1983
1984
1993 template <typename Key, class Compare = Aleph::less<Key>>
1994 class DynSetRbRkTree : public DynSetTree<Key, Rb_Tree_Rk, Compare>
1995 {
1996 public:
1998 using Base::Base;
2000 SETTREE_ITOR(Rb_Tree_Rk, Key, Compare);
2001 };
2002
2003
2013 template <typename Key, class Compare = Aleph::less<Key>>
2014 class DynSetTdRbRkTree : public DynSetTree<Key, TdRbTreeRk, Compare>
2015 {
2016 public:
2018 using Base::Base;
2020 SETTREE_ITOR(TdRbTreeRk, Key, Compare);
2021 };
2022
2023
2037 template <typename Key, class Compare = Aleph::less<Key>>
2038 class DynSetHtdRbTree : public DynSetTree<Key, HtdRbTree, Compare>
2039 {
2040 public:
2042 using Base::Base;
2044 SETTREE_ITOR(HtdRbTree, Key, Compare);
2045 };
2046
2047
2061 template <typename Key, class Compare = Aleph::less<Key>>
2062 class DynSetHtdRbRkTree : public DynSetTree<Key, HtdRbTreeRk, Compare>
2063 {
2064 public:
2066 using Base::Base;
2069 };
2070
2071
2072 template <typename T, class Op, class C>
2073 DynSetTree<T> set_unify(const C & c, Op op)
2074 {
2075 DynSetTree<T> ret;
2076 for (auto it = c.get_it(); it.has_curr(); it.next_ne())
2077 ret.insert(op(it.get_curr()));
2078 return ret;
2079 }
2080} // end namespace Aleph
2081
2082# endif /* TPL_DYNSETTREE_H */
Memory arena for fast bulk allocations.
Variadic constructor macros for containers.
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
#define Derived_Build(Name)
Declare build() in a class derived from a container.
Definition ah-dry.H:2881
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_constexpr(C)
Definition ah-errors.H:705
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_logic_error_if(C)
Throws std::logic_error if condition holds.
Definition ah-errors.H:474
#define ah_bad_alloc_if(C)
Throws std::bad_alloc if condition holds.
Definition ah-errors.H:578
Zip iterators and functional operations for multiple containers.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:123
Iterator traits and STL-compatible iterator wrappers.
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
virtual void unalloc(Node *)=0
virtual Node * alloc_rval(typename Node::key_type &&key)=0
virtual Node * alloc_lval(const typename Node::key_type &key)=0
virtual ~AbstractTreeNodeAllocator()=default
Arena allocator for fast bump-pointer allocation.
Definition ah-arena.H:122
bool contains(const void *ptr) const noexcept
Check if a pointer is within this arena's range.
Definition ah-arena.H:439
void * alloc_aligned(const size_t size, const size_t alignment) noexcept
Allocate aligned memory from the arena.
Definition ah-arena.H:294
void reset() noexcept
Reset arena, making all memory available again.
Definition ah-arena.H:256
void dealloc(const void *addr, const size_t size) noexcept
Deallocate memory (only effective for LIFO pattern).
Definition ah-arena.H:326
size_t allocated_size() const noexcept
Get total bytes currently allocated.
Definition ah-arena.H:395
size_t available_size() const noexcept
Get remaining bytes available.
Definition ah-arena.H:401
Read-only view of an Aleph iterator: the iterator of a constant container.
~DftTreeNodeAllocator() override=default
Node * alloc_lval(const typename Node::key_type &key) override
Node * alloc_rval(typename Node::key_type &&key) override
void unalloc(Node *p) override
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
Dynamic set implemented using extended AVL binary search trees with rank support of type Avl_Tree_Rk<...
Dynamic set implemented using AVL binary search trees of type Avl_Tree<Key>.
Dynamic set implemented using binary search trees of type BinTree<Key>.
Dynamic set implemented using Hybrid Red-Black trees with rank support of type HtdRbTreeRk<Key>.
Dynamic set implemented using Hybrid Top-Down/Bottom-Up Red-Black trees of type HtdRbTree<Key>.
Iterator(DynSetTree< Key, Rand_Tree, Compare > &tree)
Dynamic set implemented using randomized binary search trees of type Rand_Tree<Key>.
Dynamic set implemented using extended Red-Black binary search trees with rank support of type Rb_Tre...
Dynamic set implemented using Red-Black binary search trees of type Rb_Tree<Key> (bottom-up implement...
Dynamic set implemented using splay trees with rank support of type Splay_Tree_Rk<Key>.
Dynamic set implemented using splay binary search trees of type Splay_Tree<Key>.
Dynamic set implemented using Top-Down Red-Black binary search trees with rank support of type TdRbTr...
Dynamic set implemented using Top-Down Red-Black binary search trees of type TdRbTree<Key>.
Dynamic set implemented using extended treap binary search trees with rank support of type Treap_Rk<K...
Dynamic set implemented using randomized treap binary search trees of type Treap<Key>.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key & access(size_t i)
const Key & get_first() const
See min.
DynSetTree(const Compare &cmp) noexcept(std::is_nothrow_constructible_v< Tree< Key, Compare >, const Compare & >)
Builds an empty set with the comparison criteria cmp.
size_t height() const
Calculates and returns the height of the binary search tree.
virtual ~DynSetTree()
Destroyer; all elements are released.
const Key & min() const
Returns the smallest key contained in the set according to the criterion comparison given.
long position(const Key &key) const
Returns the infix (ordered) position of the key.
DynSetTree() noexcept(nothrow_empty_tree)
Builds an empty set, with a comparison criteria built by default.
void free_node(Node *p)
Key * append(const Key &key)
Key & find(const Key &key)
Returns a modifiable reference to an element within the set.
const Key & get_last() const
See max.
Key * __insert_dup(Node *q)
bool split_key(const Key &key, DynSetTree &l, DynSetTree &r)
Partitions the binary search tree based on a key.
typename Tree< Key, Compare >::Node Node
Type of binary node used by the binary search tree internal.
std::pair< Key *, bool > contains_or_insert(Key &&key)
const Key & get_item() const
Returns any element of the set.
DynSetTree & join(DynSetTree &t, DynSetTree &&dup=DynSetTree())
This is an overloaded member function, provided for convenience. It differs from the above function o...
Key & operator()(size_t i)
const size_t & size() const
Returns the cardinality of the set.
Key release(Node *p)
Key remove_pos(const size_t i)
Removes the key at the inorder position i.
static auto call_select(const T &t, const size_t i, int) -> decltype(t.select(i))
Node * alloc_node(const Key &key)
static auto call_find_position(const T &t, const Key &key, int) -> decltype(t.find_position(key))
static auto call_split_pos(T &t, const size_t pos, T &l, T &r, int) -> decltype(t.split_pos(pos, l, r), void())
Key * append(Key &&key)
static constexpr size_t dim
Key del(const Key &key)
Deletes key and returns a full copy of stored key.
void clear()
Empties the container.
std::pair< long, Key * > find_position(const Key &key) const
Returns the infix (ordinate) position of the key key or whatever It would be your position of belongi...
const Key * search(const Key &key) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynSetTree(const DynSetTree &srcTree)
instantiates a dynamic copy of srcTree
Tree< Key, Compare > tree
const Key & operator[](const Key &key) const
Key * insert(const Key &key)
Inserts a key into the dynamic set.
Key * put(Key &&key)
const Key & max() const
Returns the largest key contained in the set according to the criteria comparison given.
bool exist(const Key &key) const
Returns true if key belongs to the dynamic set.
void split_key_dup(const Key &key, DynSetTree &l, DynSetTree &r)
Partitions the binary search tree based on a key that may be present in the tree.
bool has(const Key &key) const
void swap(DynSetTree &dset) noexcept(noexcept(tree.swap(dset.tree)) and noexcept(std::swap(num_nodes, dset.num_nodes)) and noexcept(std::swap(arena_allocator, dset.arena_allocator)))
Exchange all elements of this set with dset in constant time (and extremely fast).
void for_each_postorder(Key_Op &key_op) const
Performs a postfix traversal over all keys in the set and invokes operation Op.
Key * put(const Key &key)
const Key & find(const Key &key) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
static std::pair< long, Node * > call_position(const T &, const Key &,...)
size_t internal_path_length() const
Calculates and returns the length of the internal path of the tree search binary.
static constexpr bool nothrow_empty_tree
Whether building an empty tree with a comparison built by default throws nothing.
size_t remove(const Key &key)
Removes a key from the dynamic set.
std::unique_ptr< ArenaTreeAllocator< Node > > arena_allocator
bool contains(const Key &key) const
Checks if a key exists in the set.
const Key & get() const
Synonym of max.
const Key & get_root() const
DynSetTree(const size_t &arena_sz, const Compare &cmp=Compare())
Instantiate a dynamic set using an arena allocator with dynamic buffer.
Key * search_or_insert(Key &&key)
Key * insert(Key &&key)
DynSetTree(DynSetTree &&srcTree) noexcept(nothrow_empty_tree and noexcept(swap(srcTree)))
Take the elements of another set, which is left empty: on a tree that can be copied,...
Node * alloc_node(Key &&key)
Key * __insert(Node *p)
bool traverse(Operation &op) const
Traverse a constant set in order; each key is handed out as a constant reference.
Key * search(const Key &key)
Find an element in the set.
Key * insert_dup(const Key &key)
void for_each_preorder(Key_Op &&key_op=Key_Op()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void split_pos(const size_t pos, DynSetTree &l, DynSetTree &r)
Partitions the binary search tree based on an infix position.
bool traverse(Operation &&op=Operation())
See traverse(Operation & op).
void for_each_postorder(Key_Op &&key_op=Key_Op()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void for_each_in_preorder(void(*visitFct)(Node *, int, int))
Performs a prefix traversal over all nodes in the tree and invokes the visitFct operation on each vis...
DynSetTree(const char *base_addr, const size_t &sz, const Compare &cmp=Compare())
Instantiate a dynamic set using an arena allocator with external buffer.
static Node * call_select_nc(T &, const size_t,...)
static auto call_position(const T &t, const Key &key, int) -> decltype(t.position(key))
size_t arena_allocated_size() const noexcept
Returns the allocated size from the arena (0 if not using arena)
DynSetTree(DynSetTree &&srcTree) noexcept(std::is_nothrow_move_constructible_v< Tree< Key, Compare > >)
Take the elements of another set, which is left empty: on a tree that cannot be copied,...
Key & select(size_t i)
Returns the ith node in infix position.
Key * __search_or_insert(Node *p)
decltype(auto) get_compare() const noexcept(noexcept(tree.get_compare()))
The comparator that orders the keys, as the tree gives it.
static auto call_select_nc(T &t, const size_t i, int) -> decltype(t.select(i))
std::pair< Node *, bool > __contains_or_insert(Node *p)
void empty()
Remove all the elements of the set.
bool uses_arena() const noexcept
Returns true if the set is using an arena allocator.
static auto call_remove_pos(T &t, const size_t i, int) -> decltype(t.remove_pos(i))
static Node * call_select(const T &, const size_t,...)
void for_each_inorder(Key_Op &&key_op=Key_Op()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const Key & select(size_t i) const
DynSetTree & operator=(const DynList< Key > &list)
bool traverse(Operation &op)
Traverse all the set of pairs and for each key executes the operation op.
Compare key_comp() const
Return a copy of the comparator that orders the keys.
Key * search_or_insert(const Key &key)
Look for the key in the binary search tree or inserts it if it is not found.
static void call_split_pos(T &, const size_t, T &, T &,...)
void for_each_preorder(Key_Op &key_op) const
Performs a prefix traversal over all keys in the set and invokes operation Op.
static Node * call_remove_pos(T &, const size_t,...)
void pin_arena() noexcept
Pin the arena, if any: nodes of this set are going to another one.
void for_each_inorder(Key_Op &key_op) const
Performs an infix traversal over all keys in the set and invokes operation Op.
bool traverse(Operation &&op=Operation()) const
See traverse(Operation & op) const.
std::pair< Key *, bool > contains_or_insert(const Key &key)
bool is_empty() const
returns true if the set is empty
static std::pair< long, Node * > call_find_position(const T &, const Key &,...)
Key * insert_dup(Key &&key)
Node * get_root_node() const
size_t arena_available_size() const noexcept
Returns the available size in the arena (0 if not using arena)
Generic inorder traversal of a binary tree.
Generic postorder traversal of a binary tree.
Generic preorder traversal of a binary tree.
Hybrid top-down/bottom-up red-black tree with rank support.
Hybrid top-down/bottom-up red-black tree.
Definition tpl_hRbTree.H:86
Top-down red-black tree with rank (no virtual destructor).
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
and
Insert a new element into the container by constructing it from the given args.
Definition ah-dry.H:1273
Common sequential searching methods on containers.
Definition ah-dry.H:555
Node for QuadTree spatial data structure.
Definition quadnode.H:95
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
f(args...) is a valid call whose result is usable as a condition.
__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
DynSetTree & join(DynSetTree &t, DynSetTree &dup)
Union of two binary search trees.
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
Node * find_min(Node *root) noexcept
Return the minimum key contained in a binary search tree.
bool traverse(Node *root, Op op)
See infix_traverse().
Node * copyRec(Node *root)
Copy a tree.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
DynSetTree & join_dup(DynSetTree &t)
Union of two binary search trees.
size_t internal_path_length(Node *p) noexcept
Compute the internal path length.
void dispose_tree_nodes(Node *root, Dispose &dispose, const size_t levels=Bin_Tree_Depth_Bound) noexcept
Dispose of every node of a tree in postorder.
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
size_t compute_cardinality_rec(Node *root) noexcept
Count the number of nodes of a binary tree.
size_t computeHeightRec(Node *root) noexcept
Compute the height of root
Node * find_max(Node *root) noexcept
Return the maximum key contained in a binary search tree.
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
DynSetTree< T > set_unify(const C &c, Op op)
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
and
Check uniqueness with explicit hash + equality functors.
void callKeyDestructorsRec(Node *&root) noexcept
Traverses the tree and calls key's destructors.
STL namespace.
The room of a released node, in the list of those to give again.
Free_Slot * next
The following free room.
Allocator of the nodes of a tree in an arena.
Node * alloc_rval(typename Node::key_type &&key) override
Build a node with key moved into it; throw bad_alloc if it does not fit.
Free_Slot * free_slots
The released rooms.
void reset() noexcept
Make the whole arena available again.
ArenaTreeAllocator(const size_t &sz=1024 *1024)
Build an arena of sz bytes.
size_t available_size() const noexcept
Return the bytes of the arena that were never taken.
void * take() noexcept
Return the room of a node, or nullptr if the arena has none left.
bool pinned
Whether some node was given to another set.
ArenaTreeAllocator(const char *base_addr, const size_t &sz)
Build an arena on the sz bytes at base_addr, owned by the caller.
size_t allocated_size() const noexcept
Return the bytes taken from the arena, released nodes included.
Node * alloc_lval(const typename Node::key_type &key) override
Build a node with a copy of key; throw bad_alloc if it does not fit.
void pin() noexcept
Note that some node of this arena is now in another set.
void give_back(void *p) noexcept
Release a room: the arena takes back its last block, the list any other.
AhArenaAllocator arena
The memory of the nodes.
bool is_pinned() const noexcept
Return true if some node of this arena was given to another set.
void unalloc(Node *p) override
Destroy the node p and keep its room, if it is of this arena.
Node * build(K &&key)
Build a node of key in a free room; throw bad_alloc if there is none.
Ranked AVL tree with nodes without a virtual destructor.
Definition tpl_avlRk.H:1592
AVL binary search tree with nodes without a virtual destructor.
Definition tpl_avl.H:831
bool operator()(const Container &c1, const Container &c2) const
static constexpr bool has_find_position
Iterator(const DynSetTree &)=delete
A constant set is iterated through Const_Iterator.
const Key & get_curr_ne() const noexcept
typename Tree_Type::Iterator Base
Iterator() noexcept=default
Default constructor creates an "end" iterator.
const Key & get_curr() const
Randomized binary search tree.
Red-Black binary search tree with nodes without virtual destructor and with subtree counters for sele...
Definition tpl_rbRk.H:1653
Extended treap (a special type of randomized binary search tree) which manages selection and splittin...
Generic list of items stored in a container.
Definition ah-dry.H:2896
static int * k
Gsl_Compatible_Rng r
AVL tree with rank (order statistics).
AVL tree implementation (height-balanced BST).
Utility functions for binary tree operations.
Extended binary node with subtree count.
Generic unbalanced binary search tree.
#define SETTREE_ITOR(Name, Key, Cmp)
Hybrid top-down/bottom-up red-black tree with rank support.
Hybrid top-down/bottom-up red-black tree implementation.
Randomized binary search tree.
Red-Black tree with rank (order statistics).
Red-Black tree implementation (bottom-up balancing).
Top-down splay tree with rank support.
Top-down splay tree implementation (without rank support).
Top-down Red-Black tree with rank support.
Top-down Red-Black tree implementation.
Treap with rank (order statistics).
Treap: randomized BST combining tree and heap properties.
DynList< int > l