54#include <initializer_list>
129template <
typename Key,
135 static_assert(
MinDegree >= 2,
"B_Tree requires MinDegree >= 2");
136 static_assert(std::copy_constructible<Key>,
"B_Tree requires copy-constructible keys");
152 const size_t mid = lo + (hi - lo) / 2;
166 const size_t mid = lo + (hi - lo) / 2;
182 for (; first != last; ++first)
186 template <
typename T,
typename U>
static void insert_at(
Array<T> &array,
const size_t idx,
U &&value)
188 if (idx == array.
size())
190 array.
append(std::forward<U>(value));
197 array(idx) = std::forward<U>(value);
202 T ret = std::move(array(idx));
203 if (idx + 1 < array.
size())
221 for (
size_t i = from; i < src.
size(); ++i)
228 for (
size_t i = from; i < src.
size(); ++i)
229 dst.append(std::move(src(i)));
237 auto copy = std::make_unique<Node>(node->
leaf);
242 for (
const auto &child : node->
children)
251 for (
const Node *curr = node; curr !=
nullptr;)
254 if (curr->leaf
or curr->children.is_empty())
256 curr = curr->children.get_first().get();
263 const Node *curr = node;
264 while (curr !=
nullptr)
279 auto right = std::make_unique<Node>(child->
leaf);
320 const Node *curr = node;
325 curr = curr->
children.get_first().get();
339 const Node *curr = node;
344 curr = curr->
children.get_last().get();
388 auto right = std::move(parent->
children[idx + 1]);
417 if (idx + 1 < parent->
children.size())
444 <<
"max_in returned nullopt on non-empty child in remove_from";
454 <<
"min_in returned nullopt on non-empty child in remove_from";
456 node->
keys[idx] = succ;
478 for (
const auto &key : node->
keys)
483 for (
size_t i = 0; i < node->
keys.
size(); ++i)
492 const size_t depth,
size_t &leaf_depth,
size_t &
counted)
const
497 if (node !=
root_.get())
512 const auto &
ks = node->
keys;
513 for (
size_t ki = 0;
ki + 1 <
ks.size(); ++
ki)
530 if (leaf_depth ==
static_cast<size_t>(-1))
532 return leaf_depth == depth;
538 for (
size_t i = 0; i < node->
children.size(); ++i)
728 if (
root_ ==
nullptr)
730 root_ = std::make_unique<Node>(
true);
731 root_->keys.append(key);
738 auto new_root = std::make_unique<Node>(
false);
758 Key
copy = std::move(key);
770 if (
root_ ==
nullptr)
787 if (
root_->children.is_empty())
793 root_ = std::move(
root_->children.get_first());
834 while (curr !=
nullptr)
858 while (curr !=
nullptr)
892 if (
root_ ==
nullptr)
895 auto leaf_depth =
static_cast<size_t>(-1);
906template <
typename Key,
class Compare = Aleph::less<Key>,
size_t MinDegree = 16>
C++20 concepts for constraining comparison functors.
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.
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_first() noexcept
return a modifiable reference to the first element.
T & append(const T &data)
Append a copy of data
T & get_last() noexcept
return a modifiable reference to the last element.
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.
Generic filter iterator wrapper.
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)=default
Move constructor.
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().
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.
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)
Gen_B_Tree & operator=(Gen_B_Tree &&other)=default
Move assignment.
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.
and
Check uniqueness with explicit hash + equality functors.
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.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::decay_t< typename HeadC::Item_Type > T
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
Dynamic array container with automatic resizing.
Comprehensive sorting algorithms and search utilities for Aleph-w.