54#include <initializer_list>
69template <
typename Compare,
typename Key> [[nodiscard]]
bool equiv(
const Compare &
cmp,
const Key &lhs,
const Key &rhs)
71 return not
cmp(lhs, rhs)
and not
cmp(rhs, lhs);
130template <
typename Key,
132 size_t MinDegree = 16>
133 requires StrictWeakOrder<Compare, Key>
136 static_assert(MinDegree >= 2,
"B_Tree requires MinDegree >= 2");
137 static_assert(std::copy_constructible<Key>,
"B_Tree requires copy-constructible keys");
141 static constexpr size_t MaxKeys = 2 * MinDegree - 1;
142 static constexpr size_t MinKeys = MinDegree - 1;
153 const size_t mid = lo + (hi - lo) / 2;
167 const size_t mid = lo + (hi - lo) / 2;
176 [[nodiscard]]
bool equals(
const Key &lhs,
const Key &rhs)
const
183 for (; first != last; ++first)
189 if (idx == array.
size())
198 array(idx) = std::forward<U>(
value);
203 T ret = std::move(array(idx));
204 if (idx + 1 < array.
size())
215 while (array.
size() > new_size)
222 for (
size_t i = from; i < src.
size(); ++i)
229 for (
size_t i = from; i < src.
size(); ++i)
230 dst.
append(std::move(src(i)));
238 auto copy = std::make_unique<Node>(node->
leaf);
243 for (
const auto &child : node->
children)
252 for (
const Node *curr = node; curr !=
nullptr;)
255 if (curr->leaf or curr->children.is_empty())
257 curr = curr->children.get_first().get();
264 const Node *curr = node;
265 while (curr !=
nullptr)
280 auto right = std::make_unique<Node>(child->
leaf);
316 [[nodiscard]] std::optional<Key>
min_in(
const Node *node)
const
321 const Node *curr = node;
322 while (curr !=
nullptr and not curr->
leaf)
326 curr = curr->
children.get_first().get();
335 [[nodiscard]] std::optional<Key>
max_in(
const Node *node)
const
340 const Node *curr = node;
341 while (curr !=
nullptr and not curr->
leaf)
345 curr = curr->
children.get_last().get();
366 (void) left->
children.remove_last();
389 auto right = std::move(parent->
children[idx + 1]);
403 if (parent->
children[idx]->keys.size() >= MinDegree)
406 if (idx > 0
and parent->
children[idx - 1]->keys.size() >= MinDegree)
412 if (idx + 1 < parent->
children.size()
and parent->
children[idx + 1]->keys.size() >= MinDegree)
418 if (idx + 1 < parent->
children.size())
441 if (node->
children[idx]->keys.size() >= MinDegree)
445 <<
"max_in returned nullopt on non-empty child in remove_from";
446 const Key
pred = *pred_opt;
451 if (node->
children[idx + 1]->keys.size() >= MinDegree)
455 <<
"min_in returned nullopt on non-empty child in remove_from";
456 const Key succ = *succ_opt;
457 node->
keys[idx] = succ;
479 for (
const auto &key : node->
keys)
484 for (
size_t i = 0; i < node->
keys.
size(); ++i)
493 const size_t depth,
size_t &leaf_depth,
size_t &counted)
const
498 if (node !=
root_.get())
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]))
531 if (leaf_depth ==
static_cast<size_t>(-1))
533 return leaf_depth == depth;
539 for (
size_t i = 0; i < node->
children.size(); ++i)
541 std::optional<Key> child_min =
min_key;
542 std::optional<Key> child_max =
max_key;
544 child_min = node->
keys[i - 1];
546 child_max = node->
keys[i];
547 if (not
verify_node(node->
children[i].get(), child_min, child_max, depth + 1, leaf_depth, counted))
653 root_.swap(other.root_);
654 std::swap(
size_, other.size_);
662 [[nodiscard]]
const Compare &
key_comp() const noexcept
682 [[nodiscard]]
bool empty() const noexcept
702 [[nodiscard]]
size_t size() const noexcept
712 [[nodiscard]]
size_t height() const noexcept
744 [[nodiscard]]
bool search(
const Key &key)
const
761 if (
root_ ==
nullptr)
763 root_ = std::make_unique<Node>(
true);
764 root_->keys.append(key);
771 auto new_root = std::make_unique<Node>(
false);
772 new_root->children.append(std::move(
root_));
774 root_ = std::move(new_root);
791 Key
copy = std::move(key);
803 if (
root_ ==
nullptr)
820 if (
root_->children.is_empty())
826 root_ = std::move(
root_->children.get_first());
841 [[nodiscard]] std::optional<Key>
min_key()
const
851 [[nodiscard]] std::optional<Key>
max_key()
const
862 [[nodiscard]] std::optional<Key>
lower_bound(
const Key &key)
const
865 std::optional<Key> candidate;
867 while (curr !=
nullptr)
871 candidate = curr->
keys[idx];
886 [[nodiscard]] std::optional<Key>
upper_bound(
const Key &key)
const
889 std::optional<Key> candidate;
891 while (curr !=
nullptr)
895 candidate = curr->
keys[idx];
925 if (
root_ ==
nullptr)
928 auto leaf_depth =
static_cast<size_t>(-1);
939template <
typename Key,
class Compare = Aleph::less<Key>,
size_t MinDegree = 16>
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.
Standard functor implementations and comparison objects.
size_t size_t int32_t value
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
T & get_last()
Return a modifiable reference to the last element.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
T & get_first()
Return a modifiable reference to the first element.
Generic B-Tree with configurable minimum degree.
void borrow_from_next(Node *parent, const size_t idx)
bool remove(const Key &key)
Remove a key if present.
Gen_B_Tree(It first, It last, const Compare &cmp=Compare())
Construct a B-Tree from an iterator range.
size_t height() const noexcept
Return the current tree height.
bool insert(Key &&key)
Insert a key by move if it is not already present.
size_t upper_bound_index(const Array< Key > &keys, const Key &key) const
void insert_range(It first, It last)
static void append_move_range(Array< T > &dst, Array< T > &src, const size_t from)
Gen_B_Tree & operator=(const Gen_B_Tree &other)
Copy assignment.
bool contains_in(const Node *node, const Key &key) const
void split_child(Node *parent, const size_t idx)
const Compare & key_comp() const noexcept
Return the comparison object (read-only).
std::unique_ptr< Node > clone_node(const Node *node) const
std::optional< Key > max_key() const
Return the maximum key, if any.
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
size_t height_of(const Node *node) const noexcept
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.
std::optional< Key > lower_bound(const Key &key) const
Return the first key not less than key.
const Compare & get_compare() const noexcept
Return the comparison object (read-only).
bool search(const Key &key) const
Synonym for contains().
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.
bool insert(const Key &key)
Insert a key if it is not already present.
void borrow_from_prev(Node *parent, const size_t idx)
bool empty() const noexcept
Return whether the tree has no keys.
static constexpr size_t MinKeys
bool is_empty() const noexcept
Synonym for empty() in traditional Aleph style.
void insert_nonfull(Node *node, const Key &key)
std::optional< Key > min_key() const
Return the minimum key, if any.
bool equals(const Key &lhs, const Key &rhs) const
std::optional< Key > max_in(const Node *node) const
size_t lower_bound_index(const Array< Key > &keys, const Key &key) const
bool verify() const
Verify structural B-Tree invariants.
void swap(Gen_B_Tree &other) noexcept(std::is_nothrow_swappable_v< Compare >)
Exchange the keys and the comparators of this tree and another.
bool contains(const Key &key) const
Return whether a key is present.
Gen_B_Tree(std::initializer_list< Key > init, const Compare &cmp=Compare())
Construct a B-Tree from an initializer list.
Gen_B_Tree(const Gen_B_Tree &other)
Copy constructor performing a deep copy.
void clear() noexcept
Remove every key from the tree.
bool remove_from(Node *node, const Key &key)
Key key_type
Stored key type.
std::unique_ptr< Node > root_
static void append_copy_range(Array< T > &dst, const Array< T > &src, const size_t from)
static T erase_at(Array< T > &array, const size_t idx)
void collect_keys(const Node *node, Array< Key > &out) const
Gen_B_Tree(const Compare &cmp=Compare())
Construct an empty B-Tree.
Array< Key > keys() const
Materialize the tree contents in sorted order.
void merge_children(Node *parent, const size_t idx)
size_t ensure_child_has_extra(Node *parent, const size_t idx)
static void truncate(Array< T > &array, const size_t new_size)
static constexpr size_t MaxKeys
std::optional< Key > min_in(const Node *node) const
static void insert_at(Array< T > &array, const size_t idx, U &&value)
std::optional< Key > upper_bound(const Key &key) const
Return the first key strictly greater than key.
size_t size() const noexcept
Return the number of stored keys.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Freq_Node * pred
Predecessor node in level-order traversal.
bool equiv(const Compare &cmp, const Key &lhs, const Key &rhs)
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
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.
std::decay_t< typename HeadC::Item_Type > T
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.
static bool is_leaf(BinNode< std::string > *p) noexcept
static std::atomic< bool > init
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.
BTreeNode(const bool is_leaf)
Array< std::unique_ptr< BTreeNode > > children
Aleph::DynList< T > keys() const
See items().
Dynamic array container with automatic resizing.
Comprehensive sorting algorithms and search utilities for Aleph-w.