tlx
BTree< Key, Value, KeyOfValue, Compare, Traits, Duplicates, Allocator > Class Template Reference

Basic class implementing a B+ tree data structure in memory. More...

#include <btree.hpp>

Classes

class  const_iterator
 STL-like read-only iterator object for B+ tree items. More...
 
class  const_reverse_iterator
 STL-like read-only reverse iterator object for B+ tree items. More...
 
struct  InnerNode
 Extended structure of a inner node in-memory. More...
 
class  iterator
 STL-like iterator object for B+ tree items. More...
 
struct  LeafNode
 Extended structure of a leaf node in memory. More...
 
struct  node
 The header structure of each node in-memory. More...
 
struct  result_t
 B+ tree recursive deletion has much information which is needs to be passed upward. More...
 
class  reverse_iterator
 STL-like mutable reverse iterator object for B+ tree items. More...
 
struct  tree_stats
 A small struct containing basic statistics about the B+ tree. More...
 
class  value_compare
 Function class to compare value_type objects. Required by the STL. More...
 

Public Types

Constructed Types
typedef BTree< key_type, value_type, key_of_value, key_compare, traits, allow_duplicates, allocator_typeSelf
 Typedef of our own type. More...
 
typedef size_t size_type
 Size type used to count keys. More...
 

Public Member Functions

Constructors and Destructor
 BTree (const allocator_type &alloc=allocator_type())
 Default constructor initializing an empty B+ tree with the standard key comparison function. More...
 
 BTree (const key_compare &kcf, const allocator_type &alloc=allocator_type())
 Constructor initializing an empty B+ tree with a special key comparison object. More...
 
template<class InputIterator >
 BTree (InputIterator first, InputIterator last, const allocator_type &alloc=allocator_type())
 Constructor initializing a B+ tree with the range [first,last). More...
 
template<class InputIterator >
 BTree (InputIterator first, InputIterator last, const key_compare &kcf, const allocator_type &alloc=allocator_type())
 Constructor initializing a B+ tree with the range [first,last) and a special key comparison object. More...
 
 ~BTree ()
 Frees up all used B+ tree memory pages. More...
 
void swap (BTree &from)
 Fast swapping of two identical B+ tree objects. More...
 
Key and Value Comparison Function Objects
key_compare key_comp () const
 Constant access to the key comparison object sorting the B+ tree. More...
 
value_compare value_comp () const
 Constant access to a constructed value_type comparison object. More...
 
Allocators
allocator_type get_allocator () const
 Return the base node allocator provided during construction. More...
 
STL Iterator Construction Functions
iterator begin ()
 Constructs a read/data-write iterator that points to the first slot in the first leaf of the B+ tree. More...
 
iterator end ()
 Constructs a read/data-write iterator that points to the first invalid slot in the last leaf of the B+ tree. More...
 
const_iterator begin () const
 Constructs a read-only constant iterator that points to the first slot in the first leaf of the B+ tree. More...
 
const_iterator end () const
 Constructs a read-only constant iterator that points to the first invalid slot in the last leaf of the B+ tree. More...
 
reverse_iterator rbegin ()
 Constructs a read/data-write reverse iterator that points to the first invalid slot in the last leaf of the B+ tree. More...
 
reverse_iterator rend ()
 Constructs a read/data-write reverse iterator that points to the first slot in the first leaf of the B+ tree. More...
 
const_reverse_iterator rbegin () const
 Constructs a read-only reverse iterator that points to the first invalid slot in the last leaf of the B+ tree. More...
 
const_reverse_iterator rend () const
 Constructs a read-only reverse iterator that points to the first slot in the first leaf of the B+ tree. More...
 
Access Functions to the Item Count
size_type size () const
 Return the number of key/data pairs in the B+ tree. More...
 
bool empty () const
 Returns true if there is at least one key/data pair in the B+ tree. More...
 
size_type max_size () const
 Returns the largest possible size of the B+ Tree. More...
 
const struct tree_statsget_stats () const
 Return a const reference to the current statistics. More...
 
STL Access Functions Querying the Tree by Descending to a Leaf
bool exists (const key_type &key) const
 Non-STL function checking whether a key is in the B+ tree. More...
 
iterator find (const key_type &key)
 Tries to locate a key in the B+ tree and returns an iterator to the key/data slot if found. More...
 
const_iterator find (const key_type &key) const
 Tries to locate a key in the B+ tree and returns an constant iterator to the key/data slot if found. More...
 
size_type count (const key_type &key) const
 Tries to locate a key in the B+ tree and returns the number of identical key entries found. More...
 
iterator lower_bound (const key_type &key)
 Searches the B+ tree and returns an iterator to the first pair equal to or greater than key, or end() if all keys are smaller. More...
 
const_iterator lower_bound (const key_type &key) const
 Searches the B+ tree and returns a constant iterator to the first pair equal to or greater than key, or end() if all keys are smaller. More...
 
iterator upper_bound (const key_type &key)
 Searches the B+ tree and returns an iterator to the first pair greater than key, or end() if all keys are smaller or equal. More...
 
const_iterator upper_bound (const key_type &key) const
 Searches the B+ tree and returns a constant iterator to the first pair greater than key, or end() if all keys are smaller or equal. More...
 
std::pair< iterator, iteratorequal_range (const key_type &key)
 Searches the B+ tree and returns both lower_bound() and upper_bound(). More...
 
std::pair< const_iterator, const_iteratorequal_range (const key_type &key) const
 Searches the B+ tree and returns both lower_bound() and upper_bound(). More...
 
B+ Tree Object Comparison Functions
bool operator== (const BTree &other) const
 Equality relation of B+ trees of the same type. More...
 
bool operator!= (const BTree &other) const
 Inequality relation. Based on operator==. More...
 
bool operator< (const BTree &other) const
 Total ordering relation of B+ trees of the same type. More...
 
bool operator> (const BTree &other) const
 Greater relation. Based on operator<. More...
 
bool operator<= (const BTree &other) const
 Less-equal relation. Based on operator<. More...
 
bool operator>= (const BTree &other) const
 Greater-equal relation. Based on operator<. More...
 
Public Insertion Functions
std::pair< iterator, bool > insert (const value_type &x)
 Attempt to insert a key/data pair into the B+ tree. More...
 
iterator insert (iterator, const value_type &x)
 Attempt to insert a key/data pair into the B+ tree. More...
 
template<typename InputIterator >
void insert (InputIterator first, InputIterator last)
 Attempt to insert the range [first,last) of value_type pairs into the B+ tree. More...
 
Bulk Loader - Construct Tree from Sorted Sequence
template<typename Iterator >
void bulk_load (Iterator ibegin, Iterator iend)
 Bulk load a sorted range. More...
 
Public Erase Functions
bool erase_one (const key_type &key)
 Erases one (the first) of the key/data pairs associated with the given key. More...
 
size_type erase (const key_type &key)
 Erases all the key/data pairs associated with the given key. More...
 
void erase (iterator iter)
 Erase the key/data pair referenced by the iterator. More...
 

Public Attributes

 TLX_BTREE_FRIENDS
 

Static Public Attributes

Static Constant Options and Values of the B+ Tree
static const unsigned short leaf_slotmax
 Base B+ tree parameter: The number of key/data slots in each leaf. More...
 
static const unsigned short inner_slotmax
 Base B+ tree parameter: The number of key slots in each inner node, this can differ from slots in each leaf. More...
 
static const unsigned short leaf_slotmin
 Computed B+ tree parameter: The minimum number of key/data slots used in a leaf. More...
 
static const unsigned short inner_slotmin
 Computed B+ tree parameter: The minimum number of key slots used in an inner node. More...
 
static const bool self_verify
 Debug parameter: Enables expensive and thorough checking of the B+ tree invariants after each insert/erase operation. More...
 
static const bool debug
 Debug parameter: Prints out lots of debug information about how the algorithms change the tree. More...
 

Private Types

Support Class Encapsulating Deletion Results
enum  result_flags_t { btree_ok, btree_not_found, btree_update_lastkey, btree_fixmerge }
 Result flags of recursive deletion. More...
 

Private Member Functions

Convenient Key Comparison Functions Generated From key_less
bool key_less (const key_type &a, const key_type &b) const
 True if a < b ? "constructed" from key_less_() More...
 
bool key_lessequal (const key_type &a, const key_type &b) const
 True if a <= b ? constructed from key_less() More...
 
bool key_greater (const key_type &a, const key_type &b) const
 True if a > b ? constructed from key_less() More...
 
bool key_greaterequal (const key_type &a, const key_type &b) const
 True if a >= b ? constructed from key_less() More...
 
bool key_equal (const key_type &a, const key_type &b) const
 True if a == b ? constructed from key_less(). More...
 
Node Object Allocation and Deallocation Functions
LeafNode::alloc_type leaf_node_allocator ()
 Return an allocator for LeafNode objects. More...
 
InnerNode::alloc_type inner_node_allocator ()
 Return an allocator for InnerNode objects. More...
 
LeafNodeallocate_leaf ()
 Allocate and initialize a leaf node. More...
 
InnerNodeallocate_inner (unsigned short level)
 Allocate and initialize an inner node. More...
 
void free_node (node *n)
 Correctly free either inner or leaf node, destructs all contained key and value objects. More...
 
B+ Tree Node Binary Search Functions
template<typename node_type >
unsigned short find_lower (const node_type *n, const key_type &key) const
 Searches for the first key in the node n greater or equal to key. More...
 
template<typename node_type >
unsigned short find_upper (const node_type *n, const key_type &key) const
 Searches for the first key in the node n greater than key. More...
 
Private Insertion Functions
std::pair< iterator, bool > insert_start (const key_type &key, const value_type &value)
 Start the insertion descent at the current root and handle root splits. More...
 
std::pair< iterator, bool > insert_descend (node *n, const key_type &key, const value_type &value, key_type *splitkey, node **splitnode)
 Insert an item into the B+ tree. More...
 
void split_leaf_node (LeafNode *leaf, key_type *out_newkey, node **out_newleaf)
 Split up a leaf node into two equally-filled sibling leaves. More...
 
void split_inner_node (InnerNode *inner, key_type *out_newkey, node **out_newinner, unsigned int addslot)
 Split up an inner node into two equally-filled sibling nodes. More...
 

Private Attributes

Tree Object Data Members
noderoot_
 Pointer to the B+ tree's root node, either leaf or inner node. More...
 
LeafNodehead_leaf_
 Pointer to first leaf in the double linked leaf chain. More...
 
LeafNodetail_leaf_
 Pointer to last leaf in the double linked leaf chain. More...
 
tree_stats stats_
 Other small statistics about the B+ tree. More...
 
key_compare key_less_
 Key comparison object. More...
 
allocator_type allocator_
 Memory allocator. More...
 

Template Parameter Types

typedef Key key_type
 First template parameter: The key type of the B+ tree. More...
 
typedef Value value_type
 Second template parameter: Composition pair of key and data types, or just the key for set containers. More...
 
typedef KeyOfValue key_of_value
 Third template: key extractor class to pull key_type from value_type. More...
 
typedef Compare key_compare
 Fourth template parameter: key_type comparison function object. More...
 
typedef Traits traits
 Fifth template parameter: Traits object used to define more parameters of the B+ tree. More...
 
typedef Allocator allocator_type
 Seventh template parameter: STL allocator for tree nodes. More...
 
static const bool allow_duplicates
 Sixth template parameter: Allow duplicate keys in the B+ tree. More...
 

Fast Destruction of the B+ Tree

void clear ()
 Frees all key/data pairs and all nodes of the tree. More...
 
void clear_recursive (node *n)
 Recursively free up nodes. More...
 

Fast Copy: Assign Operator and Copy Constructors

BTreeoperator= (const BTree &other)
 Assignment operator. All the key/data pairs are copied. More...
 
 BTree (const BTree &other)
 Copy constructor. More...
 
struct nodecopy_recursive (const node *n)
 Recursively copy nodes from another B+ tree object. More...
 

Private Erase Functions

result_t erase_one_descend (const key_type &key, node *curr, node *left, node *right, InnerNode *left_parent, InnerNode *right_parent, InnerNode *parent, unsigned int parentslot)
 Erase one (the first) key/data pair in the B+ tree matching key. More...
 
result_t erase_iter_descend (const iterator &iter, node *curr, node *left, node *right, InnerNode *left_parent, InnerNode *right_parent, InnerNode *parent, unsigned int parentslot)
 Erase one key/data pair referenced by an iterator in the B+ tree. More...
 
result_t merge_leaves (LeafNode *left, LeafNode *right, InnerNode *parent)
 Merge two leaf nodes. More...
 
static result_t merge_inner (InnerNode *left, InnerNode *right, InnerNode *parent, unsigned int parentslot)
 Merge two inner nodes. More...
 
static result_t shift_left_leaf (LeafNode *left, LeafNode *right, InnerNode *parent, unsigned int parentslot)
 Balance two leaf nodes. More...
 
static void shift_left_inner (InnerNode *left, InnerNode *right, InnerNode *parent, unsigned int parentslot)
 Balance two inner nodes. More...
 
static void shift_right_leaf (LeafNode *left, LeafNode *right, InnerNode *parent, unsigned int parentslot)
 Balance two leaf nodes. More...
 
static void shift_right_inner (InnerNode *left, InnerNode *right, InnerNode *parent, unsigned int parentslot)
 Balance two inner nodes. More...
 

Verification of B+ Tree Invariants

void verify () const
 Run a thorough verification of all B+ tree invariants. More...
 
void verify_node (const node *n, key_type *minkey, key_type *maxkey, tree_stats &vstats) const
 Recursively descend down the tree and verify each node. More...
 
void verify_leaflinks () const
 Verify the double linked list of leaves. More...
 

Detailed Description

template<typename Key, typename Value, typename KeyOfValue, typename Compare = std::less<Key>, typename Traits = btree_default_traits<Key, Value>, bool Duplicates = false, typename Allocator = std::allocator<Value>>
class tlx::BTree< Key, Value, KeyOfValue, Compare, Traits, Duplicates, Allocator >

Basic class implementing a B+ tree data structure in memory.

The base implementation of an in-memory B+ tree. It is based on the implementation in Cormen's Introduction into Algorithms, Jan Jannink's paper and other algorithm resources. Almost all STL-required function calls are implemented. The asymptotic time requirements of the STL are not always fulfilled in theory, however, in practice this B+ tree performs better than a red-black tree and almost always uses less memory. The insertion function splits the nodes on the recursion unroll. Erase is largely based on Jannink's ideas.

This class is specialized into btree_set, btree_multiset, btree_map and btree_multimap using default template parameters and facade functions.

Definition at line 124 of file btree.hpp.

Member Typedef Documentation

typedef Allocator allocator_type

Seventh template parameter: STL allocator for tree nodes.

Definition at line 153 of file btree.hpp.

typedef Compare key_compare

Fourth template parameter: key_type comparison function object.

Definition at line 142 of file btree.hpp.

typedef KeyOfValue key_of_value

Third template: key extractor class to pull key_type from value_type.

Definition at line 139 of file btree.hpp.

typedef Key key_type

First template parameter: The key type of the B+ tree.

This is stored in inner nodes.

Definition at line 132 of file btree.hpp.

Typedef of our own type.

Definition at line 168 of file btree.hpp.

typedef size_t size_type

Size type used to count keys.

Definition at line 171 of file btree.hpp.

typedef Traits traits

Fifth template parameter: Traits object used to define more parameters of the B+ tree.

Definition at line 146 of file btree.hpp.

typedef Value value_type

Second template parameter: Composition pair of key and data types, or just the key for set containers.

This data type is stored in the leaves.

Definition at line 136 of file btree.hpp.

Member Enumeration Documentation

enum result_flags_t
private

Result flags of recursive deletion.

Enumerator
btree_ok 

Deletion successful and no fix-ups necessary.

btree_not_found 

Deletion not successful because key was not found.

btree_update_lastkey 

Deletion successful, the last key was updated so parent slotkeys need updates.

btree_fixmerge 

Deletion successful, children nodes were merged and the parent needs to remove the empty node.

Definition at line 2307 of file btree.hpp.

Constructor & Destructor Documentation

BTree ( const allocator_type alloc = allocator_type())
inlineexplicit

Default constructor initializing an empty B+ tree with the standard key comparison function.

Definition at line 1103 of file btree.hpp.

BTree ( const key_compare kcf,
const allocator_type alloc = allocator_type() 
)
inlineexplicit

Constructor initializing an empty B+ tree with a special key comparison object.

Definition at line 1110 of file btree.hpp.

BTree ( InputIterator  first,
InputIterator  last,
const allocator_type alloc = allocator_type() 
)
inline

Constructor initializing a B+ tree with the range [first,last).

The range need not be sorted. To create a B+ tree from a sorted range, use bulk_load().

Definition at line 1120 of file btree.hpp.

BTree ( InputIterator  first,
InputIterator  last,
const key_compare kcf,
const allocator_type alloc = allocator_type() 
)
inline

Constructor initializing a B+ tree with the range [first,last) and a special key comparison object.

The range need not be sorted. To create a B+ tree from a sorted range, use bulk_load().

Definition at line 1131 of file btree.hpp.

~BTree ( )
inline

Frees up all used B+ tree memory pages.

Definition at line 1139 of file btree.hpp.

BTree ( const BTree< Key, Value, KeyOfValue, Compare, Traits, Duplicates, Allocator > &  other)
inline

Copy constructor.

The newly initialized B+ tree object will contain a copy of all key/data pairs.

Definition at line 1779 of file btree.hpp.

Member Function Documentation

InnerNode* allocate_inner ( unsigned short  level)
inlineprivate

Allocate and initialize an inner node.

Definition at line 1261 of file btree.hpp.

LeafNode* allocate_leaf ( )
inlineprivate

Allocate and initialize a leaf node.

Definition at line 1253 of file btree.hpp.

iterator begin ( )
inline

Constructs a read/data-write iterator that points to the first slot in the first leaf of the B+ tree.

Definition at line 1341 of file btree.hpp.

const_iterator begin ( ) const
inline

Constructs a read-only constant iterator that points to the first slot in the first leaf of the B+ tree.

Definition at line 1353 of file btree.hpp.

void bulk_load ( Iterator  ibegin,
Iterator  iend 
)
inline

Bulk load a sorted range.

Loads items into leaves and constructs a B-tree above them. The tree must be empty when calling this function.

Definition at line 2155 of file btree.hpp.

void clear ( )
inline

Frees all key/data pairs and all nodes of the tree.

Definition at line 1294 of file btree.hpp.

void clear_recursive ( node n)
inlineprivate

Recursively free up nodes.

Definition at line 1311 of file btree.hpp.

struct node* copy_recursive ( const node n)
inlineprivate

Recursively copy nodes from another B+ tree object.

Definition at line 1796 of file btree.hpp.

size_type count ( const key_type key) const
inline

Tries to locate a key in the B+ tree and returns the number of identical key entries found.

Definition at line 1584 of file btree.hpp.

bool empty ( ) const
inline

Returns true if there is at least one key/data pair in the B+ tree.

Definition at line 1499 of file btree.hpp.

iterator end ( )
inline

Constructs a read/data-write iterator that points to the first invalid slot in the last leaf of the B+ tree.

Definition at line 1347 of file btree.hpp.

const_iterator end ( ) const
inline

Constructs a read-only constant iterator that points to the first invalid slot in the last leaf of the B+ tree.

Definition at line 1359 of file btree.hpp.

std::pair<iterator, iterator> equal_range ( const key_type key)
inline

Searches the B+ tree and returns both lower_bound() and upper_bound().

Definition at line 1695 of file btree.hpp.

std::pair<const_iterator, const_iterator> equal_range ( const key_type key) const
inline

Searches the B+ tree and returns both lower_bound() and upper_bound().

Definition at line 1702 of file btree.hpp.

size_type erase ( const key_type key)
inline

Erases all the key/data pairs associated with the given key.

This is implemented using erase_one().

Definition at line 2392 of file btree.hpp.

void erase ( iterator  iter)
inline

Erase the key/data pair referenced by the iterator.

Definition at line 2405 of file btree.hpp.

result_t erase_iter_descend ( const iterator iter,
node curr,
node left,
node right,
InnerNode left_parent,
InnerNode right_parent,
InnerNode parent,
unsigned int  parentslot 
)
inlineprivate

Erase one key/data pair referenced by an iterator in the B+ tree.

Descends down the tree in search of an iterator. During the descent the parent, left and right siblings and their parents are computed and passed down. The difficulty is that the iterator contains only a pointer to a LeafNode, which means that this function must do a recursive depth first search for that leaf node in the subtree containing all pairs of the same key. This subtree can be very large, even the whole tree, though in practice it would not make sense to have so many duplicate keys.

Once the referenced key/data pair is found, it is removed from the leaf and the same underflow cases are handled as in erase_one_descend.

Definition at line 2779 of file btree.hpp.

bool erase_one ( const key_type key)
inline

Erases one (the first) of the key/data pairs associated with the given key.

Definition at line 2368 of file btree.hpp.

result_t erase_one_descend ( const key_type key,
node curr,
node left,
node right,
InnerNode left_parent,
InnerNode right_parent,
InnerNode parent,
unsigned int  parentslot 
)
inlineprivate

Erase one (the first) key/data pair in the B+ tree matching key.

Descends down the tree in search of key. During the descent the parent, left and right siblings and their parents are computed and passed down. Once the key/data pair is found, it is removed from the leaf. If the leaf underflows 6 different cases are handled. These cases resolve the underflow by shifting key/data pairs from adjacent sibling nodes, merging two sibling nodes or trimming the tree.

Definition at line 2449 of file btree.hpp.

bool exists ( const key_type key) const
inline

Non-STL function checking whether a key is in the B+ tree.

The same as (find(k) != end()) or (count() != 0).

Definition at line 1522 of file btree.hpp.

iterator find ( const key_type key)
inline

Tries to locate a key in the B+ tree and returns an iterator to the key/data slot if found.

If unsuccessful it returns end().

Definition at line 1542 of file btree.hpp.

const_iterator find ( const key_type key) const
inline

Tries to locate a key in the B+ tree and returns an constant iterator to the key/data slot if found.

If unsuccessful it returns end().

Definition at line 1563 of file btree.hpp.

unsigned short find_lower ( const node_type *  n,
const key_type key 
) const
inlineprivate

Searches for the first key in the node n greater or equal to key.

Uses binary search with an optional linear self-verification. This is a template function, because the slotkey array is located at different places in LeafNode and InnerNode.

Definition at line 1398 of file btree.hpp.

unsigned short find_upper ( const node_type *  n,
const key_type key 
) const
inlineprivate

Searches for the first key in the node n greater than key.

Uses binary search with an optional linear self-verification. This is a template function, because the slotkey array is located at different places in LeafNode and InnerNode.

Definition at line 1445 of file btree.hpp.

void free_node ( node n)
inlineprivate

Correctly free either inner or leaf node, destructs all contained key and value objects.

Definition at line 1270 of file btree.hpp.

allocator_type get_allocator ( ) const
inline

Return the base node allocator provided during construction.

Definition at line 1232 of file btree.hpp.

const struct tree_stats& get_stats ( ) const
inline

Return a const reference to the current statistics.

Definition at line 1510 of file btree.hpp.

InnerNode::alloc_type inner_node_allocator ( )
inlineprivate

Return an allocator for InnerNode objects.

Definition at line 1248 of file btree.hpp.

std::pair<iterator, bool> insert ( const value_type x)
inline

Attempt to insert a key/data pair into the B+ tree.

If the tree does not allow duplicate keys, then the insert may fail if it is already present.

Definition at line 1846 of file btree.hpp.

iterator insert ( iterator  ,
const value_type x 
)
inline

Attempt to insert a key/data pair into the B+ tree.

The iterator hint is currently ignored by the B+ tree insertion routine.

Definition at line 1852 of file btree.hpp.

void insert ( InputIterator  first,
InputIterator  last 
)
inline

Attempt to insert the range [first,last) of value_type pairs into the B+ tree.

Each key/data pair is inserted individually; to bulk load the tree, use a constructor with range.

Definition at line 1860 of file btree.hpp.

std::pair<iterator, bool> insert_descend ( node n,
const key_type key,
const value_type value,
key_type splitkey,
node **  splitnode 
)
inlineprivate

Insert an item into the B+ tree.

Descend down the nodes to a leaf, insert the key/data pair in a free slot. If the node overflows, then it must be split and the new split node inserted into the parent. Unroll / this splitting up to the root.

Definition at line 1928 of file btree.hpp.

std::pair<iterator, bool> insert_start ( const key_type key,
const value_type value 
)
inlineprivate

Start the insertion descent at the current root and handle root splits.

Returns true if the item was inserted

Definition at line 1878 of file btree.hpp.

key_compare key_comp ( ) const
inline

Constant access to the key comparison object sorting the B+ tree.

Definition at line 1183 of file btree.hpp.

bool key_equal ( const key_type a,
const key_type b 
) const
inlineprivate

True if a == b ? constructed from key_less().

This requires the < relation to be a total order, otherwise the B+ tree cannot be sorted.

Definition at line 1221 of file btree.hpp.

bool key_greater ( const key_type a,
const key_type b 
) const
inlineprivate

True if a > b ? constructed from key_less()

Definition at line 1210 of file btree.hpp.

bool key_greaterequal ( const key_type a,
const key_type b 
) const
inlineprivate

True if a >= b ? constructed from key_less()

Definition at line 1215 of file btree.hpp.

bool key_less ( const key_type a,
const key_type b 
) const
inlineprivate

True if a < b ? "constructed" from key_less_()

Definition at line 1200 of file btree.hpp.

bool key_lessequal ( const key_type a,
const key_type b 
) const
inlineprivate

True if a <= b ? constructed from key_less()

Definition at line 1205 of file btree.hpp.

LeafNode::alloc_type leaf_node_allocator ( )
inlineprivate

Return an allocator for LeafNode objects.

Definition at line 1243 of file btree.hpp.

iterator lower_bound ( const key_type key)
inline

Searches the B+ tree and returns an iterator to the first pair equal to or greater than key, or end() if all keys are smaller.

Definition at line 1616 of file btree.hpp.

const_iterator lower_bound ( const key_type key) const
inline

Searches the B+ tree and returns a constant iterator to the first pair equal to or greater than key, or end() if all keys are smaller.

Definition at line 1636 of file btree.hpp.

size_type max_size ( ) const
inline

Returns the largest possible size of the B+ Tree.

This is just a function required by the STL standard, the B+ Tree can hold more items.

Definition at line 1505 of file btree.hpp.

static result_t merge_inner ( InnerNode left,
InnerNode right,
InnerNode parent,
unsigned int  parentslot 
)
inlinestaticprivate

Merge two inner nodes.

The function moves all key/childid pairs from right to left and sets right's slotuse to zero. The right slot is then removed by the calling parent node.

Definition at line 3169 of file btree.hpp.

result_t merge_leaves ( LeafNode left,
LeafNode right,
InnerNode parent 
)
inlineprivate

Merge two leaf nodes.

The function moves all key/data pairs from right to left and sets right's slotuse to zero. The right slot is then removed by the calling parent node.

Definition at line 3139 of file btree.hpp.

bool operator!= ( const BTree< Key, Value, KeyOfValue, Compare, Traits, Duplicates, Allocator > &  other) const
inline

Inequality relation. Based on operator==.

Definition at line 1722 of file btree.hpp.

bool operator< ( const BTree< Key, Value, KeyOfValue, Compare, Traits, Duplicates, Allocator > &  other) const
inline

Total ordering relation of B+ trees of the same type.

It uses std::lexicographical_compare() for the actual comparison of elements.

Definition at line 1728 of file btree.hpp.

bool operator<= ( const BTree< Key, Value, KeyOfValue, Compare, Traits, Duplicates, Allocator > &  other) const
inline

Less-equal relation. Based on operator<.

Definition at line 1739 of file btree.hpp.

BTree& operator= ( const BTree< Key, Value, KeyOfValue, Compare, Traits, Duplicates, Allocator > &  other)
inline

Assignment operator. All the key/data pairs are copied.

Definition at line 1755 of file btree.hpp.

bool operator== ( const BTree< Key, Value, KeyOfValue, Compare, Traits, Duplicates, Allocator > &  other) const
inline

Equality relation of B+ trees of the same type.

B+ trees of the same size and equal elements (both key and data) are considered equal. Beware of the random ordering of duplicate keys.

Definition at line 1716 of file btree.hpp.

bool operator> ( const BTree< Key, Value, KeyOfValue, Compare, Traits, Duplicates, Allocator > &  other) const
inline

Greater relation. Based on operator<.

Definition at line 1734 of file btree.hpp.

bool operator>= ( const BTree< Key, Value, KeyOfValue, Compare, Traits, Duplicates, Allocator > &  other) const
inline

Greater-equal relation. Based on operator<.

Definition at line 1744 of file btree.hpp.

reverse_iterator rbegin ( )
inline

Constructs a read/data-write reverse iterator that points to the first invalid slot in the last leaf of the B+ tree.

Uses STL magic.

Definition at line 1365 of file btree.hpp.

const_reverse_iterator rbegin ( ) const
inline

Constructs a read-only reverse iterator that points to the first invalid slot in the last leaf of the B+ tree.

Uses STL magic.

Definition at line 1377 of file btree.hpp.

reverse_iterator rend ( )
inline

Constructs a read/data-write reverse iterator that points to the first slot in the first leaf of the B+ tree.

Uses STL magic.

Definition at line 1371 of file btree.hpp.

const_reverse_iterator rend ( ) const
inline

Constructs a read-only reverse iterator that points to the first slot in the first leaf of the B+ tree.

Uses STL magic.

Definition at line 1383 of file btree.hpp.

static void shift_left_inner ( InnerNode left,
InnerNode right,
InnerNode parent,
unsigned int  parentslot 
)
inlinestaticprivate

Balance two inner nodes.

The function moves key/data pairs from right to left so that both nodes are equally filled. The parent node is updated if possible.

Definition at line 3264 of file btree.hpp.

static result_t shift_left_leaf ( LeafNode left,
LeafNode right,
InnerNode parent,
unsigned int  parentslot 
)
inlinestaticprivate

Balance two leaf nodes.

The function moves key/data pairs from right to left so that both nodes are equally filled. The parent node is updated if possible.

Definition at line 3215 of file btree.hpp.

static void shift_right_inner ( InnerNode left,
InnerNode right,
InnerNode parent,
unsigned int  parentslot 
)
inlinestaticprivate

Balance two inner nodes.

The function moves key/data pairs from left to right so that both nodes are equally filled. The parent node is updated if possible.

Definition at line 3385 of file btree.hpp.

static void shift_right_leaf ( LeafNode left,
LeafNode right,
InnerNode parent,
unsigned int  parentslot 
)
inlinestaticprivate

Balance two leaf nodes.

The function moves key/data pairs from left to right so that both nodes are equally filled. The parent node is updated if possible.

Definition at line 3329 of file btree.hpp.

size_type size ( ) const
inline

Return the number of key/data pairs in the B+ tree.

Definition at line 1494 of file btree.hpp.

void split_inner_node ( InnerNode inner,
key_type out_newkey,
node **  out_newinner,
unsigned int  addslot 
)
inlineprivate

Split up an inner node into two equally-filled sibling nodes.

Returns the new nodes and it's insertion key in the two parameters. Requires the slot of the item will be inserted, so the nodes will be the same size after the insert.

Definition at line 2110 of file btree.hpp.

void split_leaf_node ( LeafNode leaf,
key_type out_newkey,
node **  out_newleaf 
)
inlineprivate

Split up a leaf node into two equally-filled sibling leaves.

Returns the new nodes and it's insertion key in the two parameters.

Definition at line 2074 of file btree.hpp.

void swap ( BTree< Key, Value, KeyOfValue, Compare, Traits, Duplicates, Allocator > &  from)
inline

Fast swapping of two identical B+ tree objects.

Definition at line 1144 of file btree.hpp.

iterator upper_bound ( const key_type key)
inline

Searches the B+ tree and returns an iterator to the first pair greater than key, or end() if all keys are smaller or equal.

Definition at line 1656 of file btree.hpp.

const_iterator upper_bound ( const key_type key) const
inline

Searches the B+ tree and returns a constant iterator to the first pair greater than key, or end() if all keys are smaller or equal.

Definition at line 1676 of file btree.hpp.

value_compare value_comp ( ) const
inline

Constant access to a constructed value_type comparison object.

Required by the STL.

Definition at line 1189 of file btree.hpp.

void verify ( ) const
inline

Run a thorough verification of all B+ tree invariants.

The program aborts via tlx_die_unless() if something is wrong.

Definition at line 3541 of file btree.hpp.

void verify_leaflinks ( ) const
inlineprivate

Verify the double linked list of leaves.

Definition at line 3653 of file btree.hpp.

void verify_node ( const node n,
key_type minkey,
key_type maxkey,
tree_stats vstats 
) const
inlineprivate

Recursively descend down the tree and verify each node.

Definition at line 3559 of file btree.hpp.

Member Data Documentation

allocator_type allocator_
private

Memory allocator.

Definition at line 1093 of file btree.hpp.

const bool allow_duplicates
static

Sixth template parameter: Allow duplicate keys in the B+ tree.

Used to implement multiset and multimap.

Definition at line 150 of file btree.hpp.

const bool debug
static

Debug parameter: Prints out lots of debug information about how the algorithms change the tree.

Requires the header file to be compiled with TLX_BTREE_DEBUG and the key type must be std::ostream printable.

Definition at line 203 of file btree.hpp.

LeafNode* head_leaf_
private

Pointer to first leaf in the double linked leaf chain.

Definition at line 1080 of file btree.hpp.

const unsigned short inner_slotmax
static

Base B+ tree parameter: The number of key slots in each inner node, this can differ from slots in each leaf.

Definition at line 184 of file btree.hpp.

const unsigned short inner_slotmin
static

Computed B+ tree parameter: The minimum number of key slots used in an inner node.

If fewer slots are used, the inner node will be merged or slots shifted from it's siblings.

Definition at line 194 of file btree.hpp.

key_compare key_less_
private

Key comparison object.

More comparison functions are generated from this < relation.

Definition at line 1090 of file btree.hpp.

const unsigned short leaf_slotmax
static

Base B+ tree parameter: The number of key/data slots in each leaf.

Definition at line 180 of file btree.hpp.

const unsigned short leaf_slotmin
static

Computed B+ tree parameter: The minimum number of key/data slots used in a leaf.

If fewer slots are used, the leaf will be merged or slots shifted from it's siblings.

Definition at line 189 of file btree.hpp.

node* root_
private

Pointer to the B+ tree's root node, either leaf or inner node.

Definition at line 1077 of file btree.hpp.

const bool self_verify
static

Debug parameter: Enables expensive and thorough checking of the B+ tree invariants after each insert/erase operation.

Definition at line 198 of file btree.hpp.

tree_stats stats_
private

Other small statistics about the B+ tree.

Definition at line 1086 of file btree.hpp.

LeafNode* tail_leaf_
private

Pointer to last leaf in the double linked leaf chain.

Definition at line 1083 of file btree.hpp.

TLX_BTREE_FRIENDS

Definition at line 160 of file btree.hpp.


The documentation for this class was generated from the following file: