Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_b_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
49#ifndef TPL_B_TREE_H
50#define TPL_B_TREE_H
51
52#include <algorithm>
53#include <concepts>
54#include <initializer_list>
55#include <iterator>
56#include <memory>
57#include <optional>
58#include <type_traits>
59#include <utility>
60
61#include <ah-concepts.H>
62#include <ah-errors.H>
63#include <ahFunction.H>
64#include <tpl_array.H>
65#include <tpl_sort_utils.H>
66
67namespace Aleph {
68namespace detail {
69template <typename Compare, typename Key> [[nodiscard]] bool equiv(const Compare &cmp, const Key &lhs, const Key &rhs)
70{
71 return not cmp(lhs, rhs) and not cmp(rhs, lhs);
72}
73
74template <typename Key> struct BTreeNode
75{
76 bool leaf = true;
79
80 explicit BTreeNode(const bool is_leaf) : leaf(is_leaf) {}
81};
82} // namespace detail
83
130template <typename Key,
131 class Compare = Aleph::less<Key>,
132 size_t MinDegree = 16>
133 requires StrictWeakOrder<Compare, Key>
135{
136 static_assert(MinDegree >= 2, "B_Tree requires MinDegree >= 2");
137 static_assert(std::copy_constructible<Key>, "B_Tree requires copy-constructible keys");
138
140
141 static constexpr size_t MaxKeys = 2 * MinDegree - 1;
142 static constexpr size_t MinKeys = MinDegree - 1;
143
144 std::unique_ptr<Node> root_;
145 size_t size_ = 0;
146 Compare cmp_;
147
148 [[nodiscard]] size_t lower_bound_index(const Array<Key> &keys, const Key &key) const
149 {
150 size_t lo = 0, hi = keys.size();
151 while (lo < hi)
152 {
153 const size_t mid = lo + (hi - lo) / 2;
154 if (cmp_(keys[mid], key))
155 lo = mid + 1;
156 else
157 hi = mid;
158 }
159 return lo;
160 }
161
162 [[nodiscard]] size_t upper_bound_index(const Array<Key> &keys, const Key &key) const
163 {
164 size_t lo = 0, hi = keys.size();
165 while (lo < hi)
166 {
167 const size_t mid = lo + (hi - lo) / 2;
168 if (cmp_(key, keys[mid]))
169 hi = mid;
170 else
171 lo = mid + 1;
172 }
173 return lo;
174 }
175
176 [[nodiscard]] bool equals(const Key &lhs, const Key &rhs) const
177 {
178 return detail::equiv(cmp_, lhs, rhs);
179 }
180
181 template <std::input_iterator It> void insert_range(It first, It last)
182 {
183 for (; first != last; ++first)
184 insert(*first);
185 }
186
187 template <typename T, typename U> static void insert_at(Array<T> &array, const size_t idx, U &&value)
188 {
189 if (idx == array.size())
190 {
191 array.append(std::forward<U>(value));
192 return;
193 }
194
195 array.putn(1);
196 T *base = &array[0];
197 Aleph::open_gap(base, array.size(), idx, 1);
198 array(idx) = std::forward<U>(value);
199 }
200
201 template <typename T> static T erase_at(Array<T> &array, const size_t idx)
202 {
203 T ret = std::move(array(idx));
204 if (idx + 1 < array.size())
205 {
206 T *base = &array[0];
207 Aleph::close_gap(base, array.size(), idx, 1);
208 }
209 (void) array.remove_last();
210 return ret;
211 }
212
213 template <typename T> static void truncate(Array<T> &array, const size_t new_size)
214 {
215 while (array.size() > new_size)
216 (void) array.remove_last();
217 }
218
219 template <typename T> static void append_copy_range(Array<T> &dst, const Array<T> &src, const size_t from)
220 {
221 dst.reserve(dst.size() + src.size() - from);
222 for (size_t i = from; i < src.size(); ++i)
223 dst.append(src[i]);
224 }
225
226 template <typename T> static void append_move_range(Array<T> &dst, Array<T> &src, const size_t from)
227 {
228 dst.reserve(dst.size() + src.size() - from);
229 for (size_t i = from; i < src.size(); ++i)
230 dst.append(std::move(src(i)));
231 }
232
233 [[nodiscard]] std::unique_ptr<Node> clone_node(const Node *node) const
234 {
235 if (node == nullptr)
236 return nullptr;
237
238 auto copy = std::make_unique<Node>(node->leaf);
239 copy->keys = node->keys;
240 if (not node->leaf)
241 {
242 copy->children.reserve(node->children.size());
243 for (const auto &child : node->children)
244 copy->children.append(clone_node(child.get()));
245 }
246 return copy;
247 }
248
249 [[nodiscard]] size_t height_of(const Node *node) const noexcept
250 {
251 size_t height = 0;
252 for (const Node *curr = node; curr != nullptr;)
253 {
254 ++height;
255 if (curr->leaf or curr->children.is_empty())
256 break;
257 curr = curr->children.get_first().get();
258 }
259 return height;
260 }
261
262 [[nodiscard]] bool contains_in(const Node *node, const Key &key) const
263 {
264 const Node *curr = node;
265 while (curr != nullptr)
266 {
267 const size_t idx = lower_bound_index(curr->keys, key);
268 if (idx < curr->keys.size() and equals(curr->keys[idx], key))
269 return true;
270 if (curr->leaf)
271 return false;
272 curr = curr->children[idx].get();
273 }
274 return false;
275 }
276
277 void split_child(Node *parent, const size_t idx)
278 {
279 Node *child = parent->children[idx].get();
280 auto right = std::make_unique<Node>(child->leaf);
281
282 const Key median = child->keys[MinKeys];
283
284 append_copy_range(right->keys, child->keys, MinDegree);
285 truncate(child->keys, MinKeys);
286
287 if (not child->leaf)
288 {
289 append_move_range(right->children, child->children, MinDegree);
290 truncate(child->children, MinDegree);
291 }
292
293 insert_at(parent->keys, idx, median);
294 insert_at(parent->children, idx + 1, std::move(right));
295 }
296
297 void insert_nonfull(Node *node, const Key &key)
298 {
299 if (node->leaf)
300 {
301 const size_t idx = lower_bound_index(node->keys, key);
302 insert_at(node->keys, idx, key);
303 return;
304 }
305
306 size_t idx = lower_bound_index(node->keys, key);
307 if (node->children[idx]->keys.size() == MaxKeys)
308 {
309 split_child(node, idx);
310 if (cmp_(node->keys[idx], key))
311 ++idx;
312 }
313 insert_nonfull(node->children[idx].get(), key);
314 }
315
316 [[nodiscard]] std::optional<Key> min_in(const Node *node) const
317 {
318 if (node == nullptr)
319 return std::nullopt;
320
321 const Node *curr = node;
322 while (curr != nullptr and not curr->leaf)
323 {
324 if (curr->children.is_empty())
325 return std::nullopt;
326 curr = curr->children.get_first().get();
327 }
328
329 if (curr == nullptr or curr->keys.is_empty())
330 return std::nullopt;
331
332 return curr->keys.get_first();
333 }
334
335 [[nodiscard]] std::optional<Key> max_in(const Node *node) const
336 {
337 if (node == nullptr)
338 return std::nullopt;
339
340 const Node *curr = node;
341 while (curr != nullptr and not curr->leaf)
342 {
343 if (curr->children.is_empty())
344 return std::nullopt;
345 curr = curr->children.get_last().get();
346 }
347
348 if (curr == nullptr or curr->keys.is_empty())
349 return std::nullopt;
350
351 return curr->keys.get_last();
352 }
353
354 void borrow_from_prev(Node *parent, const size_t idx)
355 {
356 Node *child = parent->children[idx].get();
357 Node *left = parent->children[idx - 1].get();
358
359 insert_at(child->keys, 0, parent->keys[idx - 1]);
360 parent->keys[idx - 1] = left->keys.get_last();
361 (void) left->keys.remove_last();
362
363 if (not left->leaf)
364 {
365 insert_at(child->children, 0, std::move(left->children.get_last()));
366 (void) left->children.remove_last();
367 }
368 }
369
370 void borrow_from_next(Node *parent, const size_t idx)
371 {
372 Node *child = parent->children[idx].get();
373 Node *right = parent->children[idx + 1].get();
374
375 child->keys.append(parent->keys[idx]);
376 parent->keys[idx] = right->keys.get_first();
377 erase_at(right->keys, 0);
378
379 if (not right->leaf)
380 {
381 child->children.append(std::move(right->children.get_first()));
382 erase_at(right->children, 0);
383 }
384 }
385
386 void merge_children(Node *parent, const size_t idx)
387 {
388 Node *left = parent->children[idx].get();
389 auto right = std::move(parent->children[idx + 1]);
390
391 left->keys.append(parent->keys[idx]);
392 append_copy_range(left->keys, right->keys, 0);
393
394 if (not left->leaf)
395 append_move_range(left->children, right->children, 0);
396
397 erase_at(parent->keys, idx);
398 erase_at(parent->children, idx + 1);
399 }
400
401 [[nodiscard]] size_t ensure_child_has_extra(Node *parent, const size_t idx)
402 {
403 if (parent->children[idx]->keys.size() >= MinDegree)
404 return idx;
405
406 if (idx > 0 and parent->children[idx - 1]->keys.size() >= MinDegree)
407 {
408 borrow_from_prev(parent, idx);
409 return idx;
410 }
411
412 if (idx + 1 < parent->children.size() and parent->children[idx + 1]->keys.size() >= MinDegree)
413 {
414 borrow_from_next(parent, idx);
415 return idx;
416 }
417
418 if (idx + 1 < parent->children.size())
419 {
420 merge_children(parent, idx);
421 return idx;
422 }
423
424 merge_children(parent, idx - 1);
425 return idx - 1;
426 }
427
428 [[nodiscard]] bool remove_from(Node *node, const Key &key)
429 {
430 const size_t idx = lower_bound_index(node->keys, key);
431 const bool found = idx < node->keys.size() and equals(node->keys[idx], key);
432
433 if (found)
434 {
435 if (node->leaf)
436 {
437 erase_at(node->keys, idx);
438 return true;
439 }
440
441 if (node->children[idx]->keys.size() >= MinDegree)
442 {
443 auto pred_opt = max_in(node->children[idx].get());
444 ah_runtime_error_unless(pred_opt.has_value())
445 << "max_in returned nullopt on non-empty child in remove_from";
446 const Key pred = *pred_opt;
447 node->keys[idx] = pred;
448 return remove_from(node->children[idx].get(), pred);
449 }
450
451 if (node->children[idx + 1]->keys.size() >= MinDegree)
452 {
453 auto succ_opt = min_in(node->children[idx + 1].get());
454 ah_runtime_error_unless(succ_opt.has_value())
455 << "min_in returned nullopt on non-empty child in remove_from";
456 const Key succ = *succ_opt;
457 node->keys[idx] = succ;
458 return remove_from(node->children[idx + 1].get(), succ);
459 }
460
461 merge_children(node, idx);
462 return remove_from(node->children[idx].get(), key);
463 }
464
465 if (node->leaf)
466 return false;
467
468 const size_t child_idx = ensure_child_has_extra(node, idx);
469 return remove_from(node->children[child_idx].get(), key);
470 }
471
472 void collect_keys(const Node *node, Array<Key> &out) const
473 {
474 if (node == nullptr)
475 return;
476
477 if (node->leaf)
478 {
479 for (const auto &key : node->keys)
480 out.append(key);
481 return;
482 }
483
484 for (size_t i = 0; i < node->keys.size(); ++i)
485 {
486 collect_keys(node->children[i].get(), out);
487 out.append(node->keys[i]);
488 }
489 collect_keys(node->children.get_last().get(), out);
490 }
491
492 [[nodiscard]] bool verify_node(const Node *node, const std::optional<Key> &min_key, const std::optional<Key> &max_key,
493 const size_t depth, size_t &leaf_depth, size_t &counted) const
494 {
495 if (node == nullptr)
496 return false;
497
498 if (node != root_.get())
499 {
500 if (node->keys.size() < MinKeys or node->keys.size() > MaxKeys)
501 return false;
502 }
503 else
504 {
505 if (node->keys.size() > MaxKeys)
506 return false;
507 if (not node->leaf and node->keys.size() < 1)
508 return false;
509 }
510
511 // Keys must be strictly increasing — no equal adjacent entries are allowed.
512 {
513 const auto & ks = node->keys;
514 for (size_t ki = 0; ki + 1 < ks.size(); ++ki)
515 if (not cmp_(ks[ki], ks[ki + 1]))
516 return false;
517 }
518
519 if (min_key.has_value() and not node->keys.is_empty() and not cmp_(*min_key, node->keys.get_first()))
520 return false;
521
522 if (max_key.has_value() and not node->keys.is_empty() and not cmp_(node->keys.get_last(), *max_key))
523 return false;
524
525 counted += node->keys.size();
526
527 if (node->leaf)
528 {
529 if (not node->children.is_empty())
530 return false;
531 if (leaf_depth == static_cast<size_t>(-1))
532 leaf_depth = depth;
533 return leaf_depth == depth;
534 }
535
536 if (node->children.size() != node->keys.size() + 1)
537 return false;
538
539 for (size_t i = 0; i < node->children.size(); ++i)
540 {
541 std::optional<Key> child_min = min_key;
542 std::optional<Key> child_max = max_key;
543 if (i > 0)
544 child_min = node->keys[i - 1];
545 if (i < node->keys.size())
546 child_max = node->keys[i];
547 if (not verify_node(node->children[i].get(), child_min, child_max, depth + 1, leaf_depth, counted))
548 return false;
549 }
550
551 return true;
552 }
553
554public:
555 using key_type = Key;
556
562 explicit Gen_B_Tree(const Compare &cmp = Compare()) : cmp_(cmp) {}
563
571 template <std::input_iterator It> Gen_B_Tree(It first, It last, const Compare &cmp = Compare()) : cmp_(cmp)
572 {
573 insert_range(first, last);
574 }
575
582 Gen_B_Tree(std::initializer_list<Key> init, const Compare &cmp = Compare())
583 : Gen_B_Tree(init.begin(), init.end(), cmp)
584 {}
585
591 Gen_B_Tree(const Gen_B_Tree &other) : root_(clone_node(other.root_.get())), size_(other.size_), cmp_(other.cmp_) {}
592
604 Gen_B_Tree(Gen_B_Tree &&other) noexcept(std::is_nothrow_copy_constructible_v<Compare>)
605 : root_(std::move(other.root_)), size_(std::exchange(other.size_, 0)), cmp_(other.cmp_)
606 {}
607
614 {
615 if (this == &other)
616 return *this;
617
618 // Copy-and-swap: build a full copy first so that if cmp_'s copy-assignment
619 // (or clone_node) throws, *this is left unchanged.
620 Gen_B_Tree tmp(other);
621 std::swap(root_, tmp.root_);
622 std::swap(size_, tmp.size_);
623 std::swap(cmp_, tmp.cmp_);
624 return *this;
625 }
626
633 Gen_B_Tree &operator=(Gen_B_Tree &&other) noexcept(noexcept(swap(other)))
634 {
635 swap(other);
636 return *this;
637 }
638
647 void swap(Gen_B_Tree &other) noexcept(std::is_nothrow_swappable_v<Compare>)
648 {
649 // the swap that the specification evaluates: that of Compare found by
650 // its arguments, else std::swap(); first, since it may throw
651 using std::swap;
652 swap(cmp_, other.cmp_);
653 root_.swap(other.root_);
654 std::swap(size_, other.size_);
655 }
656
662 [[nodiscard]] const Compare &key_comp() const noexcept
663 {
664 return cmp_;
665 }
666
672 [[nodiscard]] const Compare &get_compare() const noexcept
673 {
674 return key_comp();
675 }
676
682 [[nodiscard]] bool empty() const noexcept
683 {
684 return size_ == 0;
685 }
686
692 [[nodiscard]] bool is_empty() const noexcept
693 {
694 return empty();
695 }
696
702 [[nodiscard]] size_t size() const noexcept
703 {
704 return size_;
705 }
706
712 [[nodiscard]] size_t height() const noexcept
713 {
714 return height_of(root_.get());
715 }
716
721 void clear() noexcept
722 {
723 root_.reset();
724 size_ = 0;
725 }
726
733 [[nodiscard]] bool contains(const Key &key) const
734 {
735 return contains_in(root_.get(), key);
736 }
737
744 [[nodiscard]] bool search(const Key &key) const
745 {
746 return contains(key);
747 }
748
756 bool insert(const Key &key)
757 {
758 if (contains(key))
759 return false;
760
761 if (root_ == nullptr)
762 {
763 root_ = std::make_unique<Node>(true);
764 root_->keys.append(key);
765 size_ = 1;
766 return true;
767 }
768
769 if (root_->keys.size() == MaxKeys)
770 {
771 auto new_root = std::make_unique<Node>(false);
772 new_root->children.append(std::move(root_));
773 split_child(new_root.get(), 0);
774 root_ = std::move(new_root);
775 }
776
777 insert_nonfull(root_.get(), key);
778 ++size_;
779 return true;
780 }
781
789 bool insert(Key &&key)
790 {
791 Key copy = std::move(key);
792 return insert(copy);
793 }
794
801 bool remove(const Key &key)
802 {
803 if (root_ == nullptr)
804 return false;
805
806 const bool removed = remove_from(root_.get(), key);
807
808 // The search path in remove_from may trigger merges (via
809 // ensure_child_has_extra) that leave the root with zero keys even
810 // when the key is not found. Promote the sole child regardless
811 // so the invariant "non-leaf root has >= 1 key" is preserved.
812 while (root_ != nullptr and root_->keys.is_empty())
813 {
814 if (root_->leaf)
815 {
816 root_.reset();
817 break;
818 }
819
820 if (root_->children.is_empty())
821 {
822 root_.reset();
823 break;
824 }
825
826 root_ = std::move(root_->children.get_first());
827 }
828
829 if (not removed)
830 return false;
831
832 --size_;
833 return true;
834 }
835
841 [[nodiscard]] std::optional<Key> min_key() const
842 {
843 return min_in(root_.get());
844 }
845
851 [[nodiscard]] std::optional<Key> max_key() const
852 {
853 return max_in(root_.get());
854 }
855
862 [[nodiscard]] std::optional<Key> lower_bound(const Key &key) const
863 {
864 const Node *curr = root_.get();
865 std::optional<Key> candidate;
866
867 while (curr != nullptr)
868 {
869 const size_t idx = lower_bound_index(curr->keys, key);
870 if (idx < curr->keys.size())
871 candidate = curr->keys[idx];
872 if (curr->leaf)
873 return candidate;
874 curr = curr->children[idx].get();
875 }
876
877 return candidate;
878 }
879
886 [[nodiscard]] std::optional<Key> upper_bound(const Key &key) const
887 {
888 const Node *curr = root_.get();
889 std::optional<Key> candidate;
890
891 while (curr != nullptr)
892 {
893 const size_t idx = upper_bound_index(curr->keys, key);
894 if (idx < curr->keys.size())
895 candidate = curr->keys[idx];
896 if (curr->leaf)
897 return candidate;
898 curr = curr->children[idx].get();
899 }
900
901 return candidate;
902 }
903
910 [[nodiscard]] Array<Key> keys() const
911 {
913 out.reserve(size_ == 0 ? 1 : size_);
914 collect_keys(root_.get(), out);
915 return out;
916 }
917
923 [[nodiscard]] bool verify() const
924 {
925 if (root_ == nullptr)
926 return size_ == 0;
927
928 auto leaf_depth = static_cast<size_t>(-1);
929 size_t counted = 0;
930 return verify_node(root_.get(), std::nullopt, std::nullopt, 1, leaf_depth, counted) and counted == size_;
931 }
932};
933
939template <typename Key, class Compare = Aleph::less<Key>, size_t MinDegree = 16>
941} // namespace Aleph
942
943#endif // TPL_B_TREE_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
Definition ah-errors.H:399
Standard functor implementations and comparison objects.
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_array.H:444
T & get_last()
Return a modifiable reference to the last element.
Definition tpl_array.H:483
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:402
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
Definition tpl_array.H:392
T & get_first()
Return a modifiable reference to the first element.
Definition tpl_array.H:465
Generic B-Tree with configurable minimum degree.
Definition tpl_b_tree.H:135
void borrow_from_next(Node *parent, const size_t idx)
Definition tpl_b_tree.H:370
bool remove(const Key &key)
Remove a key if present.
Definition tpl_b_tree.H:801
Gen_B_Tree(It first, It last, const Compare &cmp=Compare())
Construct a B-Tree from an iterator range.
Definition tpl_b_tree.H:571
size_t height() const noexcept
Return the current tree height.
Definition tpl_b_tree.H:712
bool insert(Key &&key)
Insert a key by move if it is not already present.
Definition tpl_b_tree.H:789
size_t upper_bound_index(const Array< Key > &keys, const Key &key) const
Definition tpl_b_tree.H:162
void insert_range(It first, It last)
Definition tpl_b_tree.H:181
static void append_move_range(Array< T > &dst, Array< T > &src, const size_t from)
Definition tpl_b_tree.H:226
Gen_B_Tree & operator=(const Gen_B_Tree &other)
Copy assignment.
Definition tpl_b_tree.H:613
bool contains_in(const Node *node, const Key &key) const
Definition tpl_b_tree.H:262
void split_child(Node *parent, const size_t idx)
Definition tpl_b_tree.H:277
const Compare & key_comp() const noexcept
Return the comparison object (read-only).
Definition tpl_b_tree.H:662
std::unique_ptr< Node > clone_node(const Node *node) const
Definition tpl_b_tree.H:233
std::optional< Key > max_key() const
Return the maximum key, if any.
Definition tpl_b_tree.H:851
bool verify_node(const Node *node, const std::optional< Key > &min_key, const std::optional< Key > &max_key, const size_t depth, size_t &leaf_depth, size_t &counted) const
Definition tpl_b_tree.H:492
size_t height_of(const Node *node) const noexcept
Definition tpl_b_tree.H:249
Gen_B_Tree(Gen_B_Tree &&other) noexcept(std::is_nothrow_copy_constructible_v< Compare >)
Move constructor: take the keys of another tree, which is left empty.
Definition tpl_b_tree.H:604
std::optional< Key > lower_bound(const Key &key) const
Return the first key not less than key.
Definition tpl_b_tree.H:862
const Compare & get_compare() const noexcept
Return the comparison object (read-only).
Definition tpl_b_tree.H:672
bool search(const Key &key) const
Synonym for contains().
Definition tpl_b_tree.H:744
Gen_B_Tree & operator=(Gen_B_Tree &&other) noexcept(noexcept(swap(other)))
Move assignment: exchange this tree with another, which is left with the previous keys of this one.
Definition tpl_b_tree.H:633
bool insert(const Key &key)
Insert a key if it is not already present.
Definition tpl_b_tree.H:756
void borrow_from_prev(Node *parent, const size_t idx)
Definition tpl_b_tree.H:354
bool empty() const noexcept
Return whether the tree has no keys.
Definition tpl_b_tree.H:682
static constexpr size_t MinKeys
Definition tpl_b_tree.H:142
bool is_empty() const noexcept
Synonym for empty() in traditional Aleph style.
Definition tpl_b_tree.H:692
void insert_nonfull(Node *node, const Key &key)
Definition tpl_b_tree.H:297
std::optional< Key > min_key() const
Return the minimum key, if any.
Definition tpl_b_tree.H:841
bool equals(const Key &lhs, const Key &rhs) const
Definition tpl_b_tree.H:176
std::optional< Key > max_in(const Node *node) const
Definition tpl_b_tree.H:335
size_t lower_bound_index(const Array< Key > &keys, const Key &key) const
Definition tpl_b_tree.H:148
bool verify() const
Verify structural B-Tree invariants.
Definition tpl_b_tree.H:923
void swap(Gen_B_Tree &other) noexcept(std::is_nothrow_swappable_v< Compare >)
Exchange the keys and the comparators of this tree and another.
Definition tpl_b_tree.H:647
bool contains(const Key &key) const
Return whether a key is present.
Definition tpl_b_tree.H:733
Gen_B_Tree(std::initializer_list< Key > init, const Compare &cmp=Compare())
Construct a B-Tree from an initializer list.
Definition tpl_b_tree.H:582
Gen_B_Tree(const Gen_B_Tree &other)
Copy constructor performing a deep copy.
Definition tpl_b_tree.H:591
void clear() noexcept
Remove every key from the tree.
Definition tpl_b_tree.H:721
bool remove_from(Node *node, const Key &key)
Definition tpl_b_tree.H:428
Key key_type
Stored key type.
Definition tpl_b_tree.H:555
std::unique_ptr< Node > root_
Definition tpl_b_tree.H:144
static void append_copy_range(Array< T > &dst, const Array< T > &src, const size_t from)
Definition tpl_b_tree.H:219
static T erase_at(Array< T > &array, const size_t idx)
Definition tpl_b_tree.H:201
void collect_keys(const Node *node, Array< Key > &out) const
Definition tpl_b_tree.H:472
Gen_B_Tree(const Compare &cmp=Compare())
Construct an empty B-Tree.
Definition tpl_b_tree.H:562
Array< Key > keys() const
Materialize the tree contents in sorted order.
Definition tpl_b_tree.H:910
void merge_children(Node *parent, const size_t idx)
Definition tpl_b_tree.H:386
size_t ensure_child_has_extra(Node *parent, const size_t idx)
Definition tpl_b_tree.H:401
static void truncate(Array< T > &array, const size_t new_size)
Definition tpl_b_tree.H:213
static constexpr size_t MaxKeys
Definition tpl_b_tree.H:141
std::optional< Key > min_in(const Node *node) const
Definition tpl_b_tree.H:316
static void insert_at(Array< T > &array, const size_t idx, U &&value)
Definition tpl_b_tree.H:187
std::optional< Key > upper_bound(const Key &key) const
Return the first key strictly greater than key.
Definition tpl_b_tree.H:886
size_t size() const noexcept
Return the number of stored keys.
Definition tpl_b_tree.H:702
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
Freq_Node * pred
Predecessor node in level-order traversal.
bool equiv(const Compare &cmp, const Key &lhs, const Key &rhs)
Definition tpl_b_tree.H:69
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
const T * median(const T &a, const T &b, const T &c, const Compare &cmp=Compare())
Return a pointer to the median value among three elements.
Definition ahUtils.H:85
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
and
Check uniqueness with explicit hash + equality functors.
void open_gap(Tarray &ptr, size_t n, size_t pos=0, size_t num_entries=1)
Open a gap in an array by shifting elements right.
Definition array_utils.H:96
static bool is_leaf(BinNode< std::string > *p) noexcept
Definition Huffman.H:109
static std::atomic< bool > init
Definition hash-fct.C:54
void close_gap(T *ptr, size_t n, size_t pos, size_t num_entries=1)
Close a gap in an array by shifting elements left.
STL namespace.
BTreeNode(const bool is_leaf)
Definition tpl_b_tree.H:80
Array< std::unique_ptr< BTreeNode > > children
Definition tpl_b_tree.H:78
Aleph::DynList< T > keys() const
See items().
Definition ah-dry.H:2916
Dynamic array container with automatic resizing.
Comprehensive sorting algorithms and search utilities for Aleph-w.