ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::BTree< K, V, Policy > Class Template Reference

a DICTIONARY on a B-tree of minimum degree \( t \), its changes told through Policy More...

#include <ADS/tree/BTree.h>

Classes

class  const_iterator
 the iterator on the pairs by increasing keys, with the path from the root More...

Public types

using Key = K
 the type of the keys
using Value = V
 the type of the values
using Inserted = mod::Inserted< K , K , V >
 an insertion of a key, the key its handle
using Erased = mod::Erased< K , K , V >
 an erasure of a key, told before the removal
using Assigned = mod::Assigned< K , V >
 a change of the value of a key
using modifications
 the events: a key inserted or erased with its value, its value changed, the tree emptied or rebuilt
using Subject = typename Policy::template bind< modifications , BTree >
 the subject
using value_type = std::pair< K , V >
 the pairs ( key , value ) by value, which own their copies
using key_type = K
 the type of the keys
using mapped_type = V
 the type of the values
using size_type = std::size_t
 the type of the sizes
using difference_type = std::ptrdiff_t
 the type of the distances
using reference = typename const_iterator::reference
 references, by value
using const_reference = reference
 the same
using iterator = const_iterator
 the constant iterator

Public member functions

template<class... O>
 BTree (int t=3, O &... o)
 the empty B-tree of minimum degree \( t \), the static observers o...
std::optional< V > lookup (const K &k) const
 the value of the key \( k \), or nothing
void insert (const K &k, const V &v)
 gives the key \( k \) the value \( v \), in one pass down from the root
void remove (const K &k)
 removes the key \( k \), if there, in one pass down from the root
void clear (void)
 removes all the keys, in \( O( n ) \) time
bool contains (const K &k) const
 whether the key \( k \) is there, lookup() above
std::optional< std::pair< K, V > > lowerBound (const K &k) const
 the smallest key not smaller than \( k \) and its value, or nothing (the find( x ) of an SSet of Morin), in \( O( t \log_t n ) \) time
const K * findKey (const K &k) const
 the address of the key \( k \), nil if absent, in \( O( t \log_t n ) \) time: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed
const K * lowerBoundKey (const K &k) const
 the address of the smallest key not smaller than \( k \), nil if none, in \( O( t \log_t n ) \) time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed
std::size_t size (void) const
 the number \( n \) of keys
bool empty (void) const
 true if there is no key
int height (void) const
 the height, 0 for a single leaf, -1 if empty
std::vector< std::vector< std::vector< K > > > levels (void) const
 the keys of the nodes, level by level from the root, each level from left to right: the shape of the tree, as in the figures of the books, by a visit in breadth, no level if empty
template<class F>
void forEach (F &&f) const
 f( k , v ) on the pairs by increasing keys
Subject & subject (void)
 the subject, for ADS::batch() and ADS::quiet()
const Subject & subject (void) const
 the subject
template<class O>
requires ( Subject::dynamic )
int attach (O &o)
 attaches the observer \( o \) and gives its number
void detach (int id)
 detaches the observer number \( \mathit{id} \)
const_iterator begin (void) const
 the iterator on the smallest key
const_iterator end (void) const
 the iterator past the largest key
const_iterator cbegin (void) const
 as begin()
const_iterator cend (void) const
 as end()

Detailed description

template<std::totally_ordered K, class V, class Policy = NoObservers>
class ADS::BTree< K, V, Policy >

a DICTIONARY on a B-tree of minimum degree \( t \), its changes told through Policy

Member typedef documentation

◆ Assigned

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::Assigned = mod::Assigned< K , V >

a change of the value of a key

◆ const_reference

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::const_reference = reference

the same

◆ difference_type

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::difference_type = std::ptrdiff_t

the type of the distances

◆ Erased

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::Erased = mod::Erased< K , K , V >

an erasure of a key, told before the removal

◆ Inserted

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::Inserted = mod::Inserted< K , K , V >

an insertion of a key, the key its handle

◆ iterator

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::iterator = const_iterator

the constant iterator

◆ Key

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::Key = K

the type of the keys

◆ key_type

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::key_type = K

the type of the keys

◆ mapped_type

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::mapped_type = V

the type of the values

◆ modifications

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::modifications
Initial value:
a list of types, the events of a structure [see Modification.h]
Definition Modification.h:482

the events: a key inserted or erased with its value, its value changed, the tree emptied or rebuilt

◆ reference

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::reference = typename const_iterator::reference

references, by value

◆ size_type

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::size_type = std::size_t

the type of the sizes

◆ Subject

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::Subject = typename Policy::template bind< modifications , BTree >

the subject

◆ Value

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::Value = V

the type of the values

◆ value_type

template<std::totally_ordered K, class V, class Policy = NoObservers>
using ADS::BTree< K, V, Policy >::value_type = std::pair< K , V >

the pairs ( key , value ) by value, which own their copies

Constructor and destructor documentation

◆ BTree()

template<std::totally_ordered K, class V, class Policy = NoObservers>
template<class... O>
ADS::BTree< K, V, Policy >::BTree ( int t = 3,
O &... o )
explicit

the empty B-tree of minimum degree \( t \), the static observers o...

Precondition
\( t \ge 2 \)

Member function documentation

◆ attach()

template<std::totally_ordered K, class V, class Policy = NoObservers>
template<class O>
requires ( Subject::dynamic )
int ADS::BTree< K, V, Policy >::attach ( O & o)
inline

attaches the observer \( o \) and gives its number

Precondition
no batch is open

◆ begin()

template<std::totally_ordered K, class V, class Policy = NoObservers>
const_iterator ADS::BTree< K, V, Policy >::begin ( void ) const
inlinenodiscard

the iterator on the smallest key

◆ cbegin()

template<std::totally_ordered K, class V, class Policy = NoObservers>
const_iterator ADS::BTree< K, V, Policy >::cbegin ( void ) const
inlinenodiscard

as begin()

◆ cend()

template<std::totally_ordered K, class V, class Policy = NoObservers>
const_iterator ADS::BTree< K, V, Policy >::cend ( void ) const
inlinenodiscard

as end()

◆ clear()

template<std::totally_ordered K, class V, class Policy = NoObservers>
void ADS::BTree< K, V, Policy >::clear ( void )

removes all the keys, in \( O( n ) \) time

◆ contains()

template<std::totally_ordered K, class V, class Policy = NoObservers>
bool ADS::BTree< K, V, Policy >::contains ( const K & k) const
nodiscard

whether the key \( k \) is there, lookup() above

◆ detach()

template<std::totally_ordered K, class V, class Policy = NoObservers>
void ADS::BTree< K, V, Policy >::detach ( int id)
inline

detaches the observer number \( \mathit{id} \)

Precondition
it is attached, no batch is open

◆ empty()

template<std::totally_ordered K, class V, class Policy = NoObservers>
bool ADS::BTree< K, V, Policy >::empty ( void ) const
nodiscard

true if there is no key

◆ end()

template<std::totally_ordered K, class V, class Policy = NoObservers>
const_iterator ADS::BTree< K, V, Policy >::end ( void ) const
inlinenodiscard

the iterator past the largest key

◆ findKey()

template<std::totally_ordered K, class V, class Policy = NoObservers>
const K * ADS::BTree< K, V, Policy >::findKey ( const K & k) const
nodiscard

the address of the key \( k \), nil if absent, in \( O( t \log_t n ) \) time: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed

◆ forEach()

template<std::totally_ordered K, class V, class Policy = NoObservers>
template<class F>
void ADS::BTree< K, V, Policy >::forEach ( F && f) const

f( k , v ) on the pairs by increasing keys

In pseudocode (the symmetric visit of section 6.1 of the book, on the nodes of many keys):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{visit}( x , f )\quad \triangleright\ x \text{ of } j \text{ keys} \cr 2 & \quad \textbf{if}\ x = \textbf{nil}\ \textbf{then return} \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ j\ \textbf{do}\quad \triangleright \ \text{the subtree on the left of each key, then the key} \cr 4 & \qquad \textbf{if}\ x \text{ is an inner node}\ \textbf{then} \ \mathrm{visit}( x.c[ i ] , f ) \cr 5 & \qquad \textbf{if}\ i < j\ \textbf{then}\ f( x.k[ i ] , x.v[ i ] ) \end{array}} \]

◆ height()

template<std::totally_ordered K, class V, class Policy = NoObservers>
int ADS::BTree< K, V, Policy >::height ( void ) const
nodiscard

the height, 0 for a single leaf, -1 if empty

◆ insert()

template<std::totally_ordered K, class V, class Policy = NoObservers>
void ADS::BTree< K, V, Policy >::insert ( const K & k,
const V & v )

gives the key \( k \) the value \( v \), in one pass down from the root

In pseudocode (Bayer and McCreight, Cormen et al., chapter 18, beyond the book), each full node met split before it is entered, so that its parent has room for the median:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( k , v ) \cr 2 & \quad \textbf{if}\ \mathit{root} \text{ has } 2 t - 1 \text{ keys} \ \textbf{then} \cr 3 & \qquad \mathit{root} \gets \text{a new node with the old root as } \text{its child};\ \mathrm{split}( \mathit{root} , 0 ) \cr 4 & \quad x \gets \mathit{root} \cr 5 & \quad \textbf{loop} \cr 6 & \qquad i \gets \text{the first place with } x.k[ i ] \ge k \cr 7 & \qquad \textbf{if}\ x.k[ i ] = k\ \textbf{then}\ x.v[ i ] \gets v; \ \textbf{return} \cr 8 & \qquad \textbf{if}\ x \text{ is a leaf}\ \textbf{then} \ \text{put } ( k , v ) \text{ at the place } i \text{ of } x; \ \textbf{return} \cr 9 & \qquad \textbf{if}\ x.c[ i ] \text{ has } 2 t - 1 \text{ keys} \ \textbf{then} \cr 10 & \qquad\quad \mathrm{split}( x , i ) \cr 11 & \qquad\quad \textbf{if}\ x.k[ i ] < k\ \textbf{then}\ i \gets i + 1 \cr 12 & \qquad\quad \textbf{else if}\ x.k[ i ] = k\ \textbf{then}\ x.v[ i ] \gets v;\ \textbf{return} \cr 13 & \qquad x \gets x.c[ i ] \cr 14 & \textbf{procedure}\ \mathrm{split}( x , i )\quad \triangleright \ \text{the child } y = x.c[ i ] \text{ has } 2 t - 1 \text{ keys} \cr 15 & \quad z \gets \text{a new node with the keys } y.k[ t .. 2 t - 2 ] \text{ and their children} \cr 16 & \quad \text{put } y.k[ t - 1 ] \text{ at the place } i \text{ of } x \text{, and } z \text{ as the child } i + 1 \cr 17 & \quad \text{keep in } y \text{ the keys } y.k[ 0 .. t - 2 ] \text{ and their children} \end{array}} \]

Postcondition
\( k \) has the value \( v \)

◆ levels()

template<std::totally_ordered K, class V, class Policy = NoObservers>
std::vector< std::vector< std::vector< K > > > ADS::BTree< K, V, Policy >::levels ( void ) const
nodiscard

the keys of the nodes, level by level from the root, each level from left to right: the shape of the tree, as in the figures of the books, by a visit in breadth, no level if empty

◆ lookup()

template<std::totally_ordered K, class V, class Policy = NoObservers>
std::optional< V > ADS::BTree< K, V, Policy >::lookup ( const K & k) const
nodiscard

the value of the key \( k \), or nothing

In pseudocode (Bayer and McCreight, Cormen et al., chapter 18, beyond the book), \( x.k[ 0 .. j - 1 ] \) the keys of a node \( x \), in increasing order, and \( x.c[ 0 .. j ] \) its children:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lookup}( k ) \cr 2 & \quad x \gets \mathit{root} \cr 3 & \quad \textbf{while}\ x \ne \textbf{nil}\ \textbf{do} \cr 4 & \qquad i \gets \text{the first place with } x.k[ i ] \ge k \cr 5 & \qquad \textbf{if}\ i < j\ \textbf{and}\ x.k[ i ] = k \ \textbf{then return}\ x.v[ i ] \cr 6 & \qquad \textbf{if}\ x \text{ is a leaf}\ \textbf{then return nil} \cr 7 & \qquad x \gets x.c[ i ] \cr 8 & \quad \textbf{return nil} \end{array}} \]

◆ lowerBound()

template<std::totally_ordered K, class V, class Policy = NoObservers>
std::optional< std::pair< K, V > > ADS::BTree< K, V, Policy >::lowerBound ( const K & k) const
nodiscard

the smallest key not smaller than \( k \) and its value, or nothing (the find( x ) of an SSet of Morin), in \( O( t \log_t n ) \) time

◆ lowerBoundKey()

template<std::totally_ordered K, class V, class Policy = NoObservers>
const K * ADS::BTree< K, V, Policy >::lowerBoundKey ( const K & k) const
nodiscard

the address of the smallest key not smaller than \( k \), nil if none, in \( O( t \log_t n ) \) time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed

◆ remove()

template<std::totally_ordered K, class V, class Policy = NoObservers>
void ADS::BTree< K, V, Policy >::remove ( const K & k)

removes the key \( k \), if there, in one pass down from the root

In pseudocode (Cormen et al., chapter 18, beyond the book), each node entered below the root having at least \( t \) keys, so that a key can leave it:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( x , k ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad i \gets \text{the first place with } x.k[ i ] \ge k \cr 4 & \qquad \textbf{if}\ x \text{ is a leaf}\ \textbf{then} \ \text{take } k \text{ out of } x \text{, if there}; \ \textbf{return} \cr 5 & \qquad \textbf{if}\ x.k[ i ] = k\ \textbf{then} \cr 6 & \qquad\quad \textbf{if}\ x.c[ i ] \text{ has } t \text{ keys or } \text{more}\ \textbf{then} \cr 7 & \qquad\qquad k' \gets \text{the predecessor of } k;\ x.k[ i ] \gets k';\ \mathrm{remove}( x.c[ i ] , k' );\ \textbf{return} \cr 8 & \qquad\quad \textbf{else if}\ x.c[ i + 1 ] \text{ has } t \text{ keys or more}\ \textbf{then} \cr 9 & \qquad\qquad k' \gets \text{the successor of } k;\ x.k[ i ] \gets k';\ \mathrm{remove}( x.c[ i + 1 ] , k' ); \ \textbf{return} \cr 10 & \qquad\quad \mathrm{merge}( x , i );\ x \gets x.c[ i ]; \ \textbf{continue} \cr 11 & \qquad x \gets x.c[ \mathrm{fill}( x , i ) ] \cr 12 & \textbf{function}\ \mathrm{fill}( x , i )\quad \triangleright \ \text{the child to go down, with } t \text{ keys at least} \cr 13 & \quad \textbf{if}\ x.c[ i ] \text{ has } t \text{ keys or more} \ \textbf{then return}\ i \cr 14 & \quad \textbf{if}\ i > 0\ \textbf{and}\ x.c[ i - 1 ] \text{ has } t \text{ keys or more}\ \textbf{then} \cr 15 & \qquad \text{move } x.k[ i - 1 ] \text{ down to the front of } x.c[ i ] \text{, the last key of } x.c[ i - 1 ] \text{ up to its } \text{place,} \cr & \qquad\quad \text{and the last child of } x.c[ i - 1 ] \text{ (if } \text{any) to the front of } x.c[ i ];\ \textbf{return}\ i \cr 16 & \quad \textbf{if}\ x.c[ i + 1 ] \text{ exists and has } t \text{ keys or more}\ \textbf{then} \cr 17 & \qquad \text{move } x.k[ i ] \text{ down to the back of } x.c[ i ] \text{, the first key of } x.c[ i + 1 ] \text{ up to its place,} \cr & \qquad\quad \text{and the first child of } x.c[ i + 1 ] \text{ (if } \text{any) to the back of } x.c[ i ];\ \textbf{return}\ i \cr 18 & \quad \textbf{if}\ i \text{ is not the last child} \ \textbf{then}\ \mathrm{merge}( x , i );\ \textbf{return}\ i \cr 19 & \quad \mathrm{merge}( x , i - 1 );\ \textbf{return}\ i - 1 \cr 20 & \textbf{procedure}\ \mathrm{merge}( x , j ) \cr 21 & \quad \text{append } x.k[ j ] \text{ and the keys and the } \text{children of } x.c[ j + 1 ] \text{ to } x.c[ j ] \cr 22 & \quad \text{take } x.k[ j ] \text{ and } x.c[ j + 1 ] \text{ out } \text{of } x \end{array}} \]

and a root left with no key gives way to its only child.

Postcondition
\( k \) is not there

◆ size()

template<std::totally_ordered K, class V, class Policy = NoObservers>
std::size_t ADS::BTree< K, V, Policy >::size ( void ) const
nodiscard

the number \( n \) of keys

◆ subject() [1/2]

template<std::totally_ordered K, class V, class Policy = NoObservers>
Subject & ADS::BTree< K, V, Policy >::subject ( void )
inlinenodiscard

the subject, for ADS::batch() and ADS::quiet()

◆ subject() [2/2]

template<std::totally_ordered K, class V, class Policy = NoObservers>
const Subject & ADS::BTree< K, V, Policy >::subject ( void ) const
inlinenodiscard

the subject


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