b-tree-plus-alpha
|
An implementation of B+-tree. More...
#include <bp_tree.hpp>
Public Types | |
using | NodePointer = BPNodePointer< LEAF_CONTAINER, VALUE, MAX_DEGREE > |
using | Node = BPInternalNode< LEAF_CONTAINER, VALUE, MAX_DEGREE > |
using | PostorderIterator = BPPostorderIterator< LEAF_CONTAINER, VALUE, MAX_DEGREE > |
using | ValueForwardIterator = BPValueForwardIterator< LEAF_CONTAINER, VALUE, MAX_DEGREE > |
using | LeafForwardIterator = BPLeafForwardIterator< LEAF_CONTAINER, VALUE, MAX_DEGREE > |
using | BPFunctions = BPInternalNodeFunctions< LEAF_CONTAINER, VALUE, USE_PARENT_FIELD, USE_PSUM, MAX_DEGREE > |
Public Member Functions | |
BPTree (const BPTree &)=delete | |
BPTree (BPTree &&other) noexcept | |
BPTree & | operator= (BPTree &&other) noexcept |
Initializers | |
void | initialize () |
Initializes the B+ tree with specified maximum degrees for internal nodes and leaf nodes. | |
void | swap (BPTree &_tree, bool swap_linked_tree) |
Swaps the contents of this B+ tree with another B+ tree. | |
void | swap (BPTree &_tree) |
Swaps the contents of this B+ tree with another B+ tree. | |
void | clear () |
Clears all contents of the B+ tree and resets it to an empty state. | |
Properties | |
The properties of this class. | |
const std::vector< NodePointer > & | get_temporary_path () const |
Returns the temporary path used during tree operations. | |
uint64_t | get_max_degree_of_internal_node () const |
Return the max degree of this tree. The number of children of every internal node does not exceed the max degree. | |
uint64_t | get_split_process_counter () const |
uint64_t | capacity () const |
double | get_value_density () const |
uint64_t | get_max_count_of_values_in_leaf () const |
Returns the maximum number of values that can be stored in a leaf node. | |
uint64_t | height () const |
Return the height of this tree. | |
uint64_t | size () const |
Return the number of values stored in this tree. | |
uint64_t | get_leaf_count () const |
Return the number of leaves in this tree. | |
uint64_t | get_leaf_container_vector_size () const |
Return the size of the vector that stores leaf containers. | |
Convertion functions | |
Convertion functions | |
std::vector< VALUE > | to_value_vector () const |
Converts the B+ tree to a vector of values. | |
Const print and verification functions | |
Const print functions | |
bool | verify_sub () const |
void | verify_sum_deque (Node *node) const |
bool | verify () const |
Verifies the integrity of the B+ tree. | |
void | print_tree () const |
Prints the B+ tree in a tree-like format. | |
void | print_info (int message_paragraph=stool::Message::SHOW_MESSAGE) const |
Prints detailed information about the B+ tree. | |
void | print_leaves () const |
Prints the leaves of the B+ tree. | |
void | print_internal_nodes () const |
void | print_leaf_containers () const |
Prints the leaf containers of the B+ tree. | |
uint64_t | size_in_bytes () const |
Returns the total memory size of the B+ tree. | |
uint64_t | compute_internal_node_count () const |
Returns the number of internal nodes in the B+ tree. | |
uint64_t | compute_leaf_container_count () const |
Returns the number of leaf containers in the B+ tree. | |
uint64_t | get_internal_node_memory () const |
Returns the memory size of the internal nodes in the B+ tree. | |
uint64_t | get_unused_leaf_container_vector_memory () const |
Returns the memory size of the unused leaf container vector in the B+ tree. | |
uint64_t | get_leaf_container_vector_memory () const |
Returns the memory size of the leaf container vector in the B+ tree. | |
uint64_t | get_leaf_container_memory () const |
Returns the memory size of the leaf containers in the B+ tree. | |
uint64_t | get_leaf_container_unused_memory () const |
uint64_t | get_unused_node_pointers_memory () const |
Returns the memory size of the unused node pointers in the B+ tree. | |
uint64_t | get_parent_vector_memory () const |
Returns the memory size of the parent vector in the B+ tree. | |
std::vector< std::string > | get_memory_usage_info (int message_paragraph=stool::Message::SHOW_MESSAGE) const |
Returns a vector of strings containing memory usage information about the B+ tree. | |
void | print_memory_usage (int message_paragraph=stool::Message::SHOW_MESSAGE) const |
Prints the memory usage information of the B+ tree. | |
Const private functions | |
Const private functions | |
void | get_path_from_root_to_first_leaf (std::vector< NodePointer > &output_path) const |
Returns a vector of NodePointer objects representing the path from the root to the first leaf. | |
void | get_path_from_root_to_last_leaf (std::vector< NodePointer > &output_path) const |
Returns a vector of NodePointer objects representing the path from the root to the last leaf. | |
int64_t | compute_path_from_root_to_leaf (uint64_t i) const |
Computes the path from the root to the leaf at position i. | |
int64_t | compute_path_from_root_to_leaf (uint64_t i, std::vector< NodePointer > &output_path) const |
Computes the path from root to leaf containing position i and returns leaf position. | |
Public standard functions | |
Public standard functions | |
static uint64_t | time_count = 0 |
PostorderIterator | get_postorder_iterator_begin () const |
Returns an iterator pointing to the first node in postorder traversal. | |
PostorderIterator | get_postorder_iterator_end () const |
Returns an iterator pointing to the end of postorder traversal. | |
ValueForwardIterator | get_value_forward_iterator_begin () const |
Returns an iterator pointing to the first value in forward traversal. | |
ValueForwardIterator | get_value_forward_iterator_end () const |
Returns an iterator pointing to the end of forward value traversal. | |
LeafForwardIterator | get_leaf_forward_iterator_begin () const |
Returns an iterator pointing to the first leaf in forward traversal. | |
LeafForwardIterator | get_leaf_forward_iterator_end () const |
Returns an iterator pointing to the end of forward leaf traversal. | |
bool | empty () const |
Checks if the B+ tree is empty. | |
uint64_t | psum () const |
Returns the prefix sum of the B+ tree. | |
uint64_t | psum (uint64_t i) const |
Returns the prefix sum of the B+ tree at position i. | |
int64_t | select0 (uint64_t i) const |
Returns the position of the i-th 0 in the B+ tree. | |
int64_t | search (uint64_t sum) const |
Returns the position of the sum in the B+ tree. | |
VALUE | at (uint64_t pos) const |
Returns the value at position pos in the B+ tree. | |
uint64_t | get_value_index (uint64_t leaf_index, uint64_t position_in_leaf_container) const |
Returns the value index at position pos in the B+ tree. | |
const LEAF_CONTAINER & | get_leaf_container (uint64_t leaf_index) const |
Returns the leaf container at position leaf_index. | |
LEAF_CONTAINER & | get_leaf_container (uint64_t leaf_index) |
Returns a reference to the leaf container at position leaf_index. | |
Non-const public functions | |
Non-const public functions | |
void | push_back (VALUE value) |
Pushes a single value to the B+ tree. | |
void | push_front (VALUE value) |
Inserts a value at the front of the B+ tree. | |
void | push_many (std::vector< LEAF_CONTAINER > &containers) |
Pushes multiple leaf containers into the B+ tree. | |
void | push_many (const std::vector< VALUE > &values) |
Pushes multiple values into the B+ tree. | |
uint64_t | remove_using_path (const std::vector< NodePointer > &path, uint64_t position_in_leaf_container) |
Removes a value at a specific position using a path to the leaf. | |
void | remove (uint64_t pos) |
Removes a value at a specific position in the B+ tree. | |
void | insert (uint64_t pos, VALUE value, uint64_t sum_delta) |
Inserts a value at a specific position in the B+ tree. | |
void | resize (uint64_t _size, VALUE default_value) |
Resizes the B+ tree to contain a specified number of elements. | |
void | set_linked_tree (BPTree *_tree) |
Sets the linked B+ tree. | |
BPTree * | get_linked_tree () const |
Gets the linked B+ tree. | |
Non-const private functions for updating nodes | |
Non-const private functions for updating nodes | |
void | move_values_right (Node *left_node, Node *right_node, uint64_t len, bool is_leaf, Node *parent, int64_t parent_edge_index_of_left_node) |
Moves values from a left node to a right node in the B+ tree. | |
void | move_values_left (Node *left_node, Node *right_node, uint64_t len, bool is_leaf, Node *parent, int64_t parent_edge_index_of_right_node) |
Moves values from a right node to a left node in the B+ tree. | |
void | split_node (Node *left_node, Node *right_node, bool is_leaf, Node *parent, int64_t parent_edge_index_of_left_node, std::vector< Node * > &parent_vec) |
Splits a node into two nodes in the B+ tree. | |
void | increment (uint64_t i, int64_t delta) |
Increments a value at a given position in the B+ tree. | |
bool | check_if_leaf_container_vec_is_sorted () const |
Checks if the leaf containers in the B+ tree are sorted. | |
void | print_debug_info () const |
void | sort_leaf_containers () |
Sorts the leaf containers in the B+ tree. | |
std::vector< Node * > | build_from_leaf_containers_sub2 (std::vector< Node * > &nodes, uint64_t current_height) |
Builds a layer of internal nodes from a vector of child nodes. | |
std::vector< Node * > | build_from_leaf_containers_sub1 () |
Creates the first layer of internal nodes from leaf containers. | |
void | build_sub (const std::vector< VALUE > &_values) |
Creates leaf containers from a vector of values. | |
void | build (const std::vector< VALUE > &_values) |
Builds a complete B+ tree from a vector of values. | |
void | build_from_leaf_containers (std::vector< LEAF_CONTAINER > &_leaf_containers) |
Builds a B+ tree from a vector of leaf containers. | |
void | save (std::vector< uint8_t > &output, uint64_t &pos) |
Saves the B+ tree structure to a byte vector. | |
void | save (std::ofstream &os) |
Saves the B+ tree structure to a file stream. | |
void | build_from_data (const std::vector< uint8_t > &data, uint64_t &pos) |
Builds a B+ tree from serialized data in a byte vector. | |
void | build_from_data (std::ifstream &ifs) |
Builds a B+ tree from serialized data in a file stream. | |
void | print_information_about_performance (int message_paragraph=stool::Message::SHOW_MESSAGE) const |
void | print_statistics (int message_paragraph=stool::Message::SHOW_MESSAGE) const |
Prints statistical information about the B+ tree. | |
An implementation of B+-tree.
|
inline |
Returns the value at position pos in the B+ tree.
pos | The position in the B+ tree |
|
inline |
Builds a complete B+ tree from a vector of values.
_values | Vector of values to build the tree from |
This function constructs a complete B+ tree by:
|
inline |
Builds a B+ tree from serialized data in a byte vector.
data | Vector containing the serialized tree data |
pos | Starting position in the data vector |
This function reconstructs the tree by:
|
inline |
Builds a B+ tree from serialized data in a file stream.
ifs | Input file stream to read from |
This function reconstructs the tree by:
|
inline |
Builds a B+ tree from a vector of leaf containers.
_leaf_containers | Vector of leaf containers to build the tree from |
This function constructs a B+ tree by:
|
inline |
Creates the first layer of internal nodes from leaf containers.
This function builds the first layer of internal nodes by:
|
inline |
Builds a layer of internal nodes from a vector of child nodes.
nodes | Vector of child nodes to build from |
current_height | Current height of the tree being built |
This function creates a new layer of internal nodes by:
|
inline |
Creates leaf containers from a vector of values.
_values | Vector of values to build leaf containers from |
This function distributes values across leaf containers by:
|
inline |
Checks if the leaf containers in the B+ tree are sorted.
This function verifies that the leaf container indices are in sequential order by:
|
inline |
Clears all contents of the B+ tree and resets it to an empty state.
This function:
|
inline |
Returns the number of internal nodes in the B+ tree.
|
inline |
Returns the number of leaf containers in the B+ tree.
|
inline |
Computes the path from the root to the leaf at position i.
i | The position of the leaf |
This function computes the path from the root to the leaf at position i in the B+ tree. If the tree is empty, it returns -1.
|
inline |
Computes the path from root to leaf containing position i and returns leaf position.
i | The position in the B+ tree to find the path to |
output_path | Vector to store the path from root to leaf |
This function computes the path from the root to the leaf node containing position i in the B+ tree. The path is stored in output_path as a sequence of NodePointers. Each NodePointer contains either an internal node or leaf pointer along with its parent edge index. The function returns the position within the final leaf node where value i is located.
|
inline |
Checks if the B+ tree is empty.
|
inline |
Returns the memory size of the internal nodes in the B+ tree.
|
inline |
Returns a reference to the leaf container at position leaf_index.
leaf_index | The position of the leaf container |
|
inline |
Returns the leaf container at position leaf_index.
leaf_index | The position of the leaf container |
|
inline |
Returns the memory size of the leaf containers in the B+ tree.
|
inline |
Returns the memory size of the leaf container vector in the B+ tree.
|
inline |
Returns an iterator pointing to the first leaf in forward traversal.
This function returns a LeafForwardIterator that points to the first leaf when traversing the tree in forward order. If the tree is empty, returns an iterator pointing to nullptr. Otherwise returns an iterator pointing to the root.
|
inline |
Returns an iterator pointing to the end of forward leaf traversal.
This function returns a LeafForwardIterator that represents the end of the forward leaf traversal sequence (nullptr)
|
inline |
Gets the linked B+ tree.
Returns the pointer to the B+ tree that was previously linked using set_linked_tree()
|
inline |
Returns the maximum number of values that can be stored in a leaf node.
|
inline |
Returns a vector of strings containing memory usage information about the B+ tree.
message_paragraph | The paragraph number for the message |
|
inline |
Returns the memory size of the parent vector in the B+ tree.
|
inline |
Returns a vector of NodePointer objects representing the path from the root to the first leaf.
output_path | The vector to store the path |
This function constructs a path from the root to the first leaf in the B+ tree. If the tree is empty, the output path remains empty.
|
inline |
Returns a vector of NodePointer objects representing the path from the root to the last leaf.
output_path | The vector to store the path |
This function constructs a path from the root to the last leaf in the B+ tree. If the tree is empty, the output path remains empty.
|
inline |
Returns an iterator pointing to the first node in postorder traversal.
This function returns a PostorderIterator that points to the first node when traversing the tree in postorder (left-right-root). If the tree is empty, returns an iterator pointing to nullptr. If the root is a leaf, returns an iterator pointing to the root leaf container index. Otherwise returns an iterator pointing to the root node.
|
inline |
Returns an iterator pointing to the end of postorder traversal.
This function returns a PostorderIterator that represents the end of the postorder traversal sequence (nullptr)
|
inline |
Returns the temporary path used during tree operations.
|
inline |
Returns the memory size of the unused leaf container vector in the B+ tree.
|
inline |
Returns the memory size of the unused node pointers in the B+ tree.
|
inline |
Returns an iterator pointing to the first value in forward traversal.
This function returns a ValueForwardIterator that points to the first value when traversing the tree in forward order. If the tree is empty, returns an iterator pointing to nullptr. Otherwise returns an iterator initialized with the first node in postorder and the leaf container vector.
|
inline |
Returns an iterator pointing to the end of forward value traversal.
This function returns a ValueForwardIterator that represents the end of the forward value traversal sequence (nullptr)
|
inline |
Returns the value index at position pos in the B+ tree.
pos | The position in the B+ tree |
|
inline |
Increments a value at a given position in the B+ tree.
i | The position in the B+ tree where the value should be incremented |
delta | The amount to increment the value by |
This function:
|
inline |
Initializes the B+ tree with specified maximum degrees for internal nodes and leaf nodes.
std::runtime_error | if either parameter is less than 4 |
This function initializes a new empty B+ tree with the given parameters. Both parameters must be at least 4 to maintain B+ tree properties. The function clears any existing data before initialization.
|
inline |
Inserts a value at a specific position in the B+ tree.
pos | Position to insert value |
value | Value to insert |
sum_delta | Change in prefix sum for parent updates |
This function:
|
inline |
Moves values from a right node to a left node in the B+ tree.
left_node | Pointer to the left node |
right_node | Pointer to the right node |
len | Number of values to move |
is_leaf | True if nodes are leaves, false if internal nodes |
parent | Pointer to the parent node |
parent_edge_index_of_right_node | Index of right node in parent's children |
This function moves len values from right_node to left_node. For leaf nodes, it moves values between leaf containers and updates parent prefix sums and counts. For internal nodes, it delegates to BPFunctions::move_left().
|
inline |
Moves values from a left node to a right node in the B+ tree.
left_node | Pointer to the left node |
right_node | Pointer to the right node |
len | Number of values to move |
is_leaf | True if nodes are leaves, false if internal nodes |
parent | Pointer to the parent node |
parent_edge_index_of_left_node | Index of left node in parent's children |
This function moves len values from left_node to right_node. For leaf nodes, it moves values between leaf containers and updates parent prefix sums and counts. For internal nodes, it delegates to BPFunctions::move_right().
|
inline |
Prints detailed information about the B+ tree.
This function prints the height of the tree, the number of internal nodes, and the number of leaf containers. It also prints the contents of each leaf container and the parent vector if USE_PARENT_FIELD is true.
|
inline |
Prints the leaf containers of the B+ tree.
This function prints the leaf containers of the B+ tree, where each leaf container is represented by its index and its contents. If the tree is empty, it prints an empty string.
|
inline |
Prints the leaves of the B+ tree.
This function prints the leaves of the B+ tree, where each leaf is represented by its index and its contents. If the tree is empty, it prints an empty string.
|
inline |
Prints the memory usage information of the B+ tree.
message_paragraph | The paragraph number for the message |
This function prints the memory usage information of the B+ tree, where each line is printed with the specified message paragraph number.
|
inline |
Prints statistical information about the B+ tree.
message_paragraph | Indentation level for output messages (default: stool::Message::SHOW_MESSAGE) |
This function prints detailed statistics about the B+ tree structure including:
|
inline |
Prints the B+ tree in a tree-like format.
This function prints the B+ tree in a tree-like format, where each internal node is represented by its value and its children are printed on the next line. If the tree is empty, it prints an empty string.
|
inline |
Returns the prefix sum of the B+ tree.
|
inline |
Returns the prefix sum of the B+ tree at position i.
i | The position in the B+ tree |
|
inline |
Pushes a single value to the B+ tree.
value | The value to be pushed |
This function pushes a single value to the B+ tree by creating a vector with the value and calling push_many() with the vector.
|
inline |
Inserts a value at the front of the B+ tree.
value | The value to be inserted at the front |
This function inserts a value at the beginning of the B+ tree by:
|
inline |
Pushes multiple values into the B+ tree.
values | Vector of values to insert |
This function iteratively:
|
inline |
Pushes multiple leaf containers into the B+ tree.
containers | Vector of leaf containers to insert |
This function iteratively:
|
inline |
Removes a value at a specific position in the B+ tree.
pos | Position of value to remove |
std::invalid_argument | if tree is empty |
This function:
|
inline |
Removes a value at a specific position using a path to the leaf.
path | Vector of NodePointers representing path from root to leaf |
position_in_leaf_container | Position in leaf container to remove |
This function:
|
inline |
Resizes the B+ tree to contain a specified number of elements.
_size | The new size of the B+ tree |
default_value | The value to initialize new elements with if expanding |
If the new size is larger than the current size, this function:
|
inline |
Saves the B+ tree structure to a file stream.
os | Output file stream to write to |
This function serializes the tree by:
|
inline |
Saves the B+ tree structure to a byte vector.
output | Vector to store the serialized tree data |
pos | Starting position in the output vector |
This function serializes the tree by:
|
inline |
Returns the position of the sum in the B+ tree.
sum | The sum in the B+ tree |
|
inline |
Returns the position of the i-th 0 in the B+ tree.
i | The position of the 0 in the B+ tree |
|
inline |
Sets the linked B+ tree.
_tree | Pointer to the B+ tree to be linked |
Associates another B+ tree with this one by storing a pointer to it
|
inline |
Returns the total memory size of the B+ tree.
|
inline |
Sorts the leaf containers in the B+ tree.
This function sorts and reorganizes the leaf containers in the B+ tree by:
The function does nothing if:
After sorting, the leaf containers will be arranged in sequential order matching their positions in the tree traversal.
|
inline |
Splits a node into two nodes in the B+ tree.
left_node | Pointer to the node being split (original node) |
right_node | Pointer to the new node that will receive half the values |
is_leaf | True if nodes are leaves, false if internal nodes |
parent | Pointer to the parent node |
parent_edge_index_of_left_node | Index of left node in parent's children |
parent_vec | Vector of parent pointers (unused parameter) |
This function splits a node that has reached maximum capacity into two nodes. For leaf nodes, it splits the leaf container. For internal nodes, it splits the children array. The split is roughly even, with the right node receiving slightly more values if the total is odd. Updates parent relationships and maintains tree invariants.
|
inline |
Swaps the contents of this B+ tree with another B+ tree.
_tree | The B+ tree to swap contents with |
This is a convenience overload that calls swap() with swap_linked_tree set to true
|
inline |
Swaps the contents of this B+ tree with another B+ tree.
_tree | The B+ tree to swap contents with |
swap_linked_tree | Whether to also swap the linked tree pointer |
This function swaps all internal data structures between this tree and the given tree, including leaf containers, parent vectors, unused node pointers, tree parameters, and tree properties. The swap_linked_tree parameter controls whether the linked tree pointer should also be swapped.
|
inline |
Converts the B+ tree to a vector of values.
|
inline |
Verifies the integrity of the B+ tree.