|
| | BTreeMap (Less less=Less()) |
| | the empty map, by the order less
|
| | BTreeMap (Less less, const A &alloc) |
| | the empty map, by the order less, the blocks of its nodes from alloc
|
| | BTreeMap (const A &alloc) |
| | the empty map, the blocks of its nodes from alloc
|
| | BTreeMap (const BTreeMap &o) |
| | a copy of o, node by node
|
| | BTreeMap (const BTreeMap &o) |
| | a copy of o, node by node, on the allocator that select_on_container_copy_construction() gives
|
| | BTreeMap (const BTreeMap &o, const A &alloc) |
| | a copy of o, node by node, on alloc
|
| | BTreeMap (BTreeMap &&o) noexcept |
| | the nodes of o, which is left empty
|
| | BTreeMap (BTreeMap &&o, const A &alloc) |
| | the pairs of o, which is left empty, on alloc: its nodes if the allocators are equal, else a copy of its pairs
|
| BTreeMap & | operator= (BTreeMap o) noexcept |
| | the pairs of o, by copy and swap
|
| BTreeMap & | operator= (const BTreeMap &o) |
| | the copy assignment, with the allocator of o if it propagates
|
| BTreeMap & | operator= (BTreeMap &&o) |
| | the move assignment: the nodes of o if its allocator propagates or is equal, else a copy of its pairs
|
| | ~BTreeMap () |
| | frees the nodes
|
| std::optional< V > | lookup (const K &k) const |
| | the value of \( k \), if there
|
| const V * | find (const K &k) const |
| | the value of \( k \), to change in place, or nullptr, by the descent of lookup()
|
| V * | find (const K &k) |
| | the value of \( k \), to change in place, or nullptr, by the descent of lookup()
|
| bool | insert (const K &k, const V &v) |
| | gives \( k \) the value \( v \); true if \( k \) was not there
|
| bool | remove (const K &k) |
| | removes \( k \); true if it was there
|
| bool | contains (const K &k) const |
| | whether \( k \) is there, by the descent of lookup()
|
| std::size_t | size (void) const |
| | the number \( n \) of pairs
|
| bool | empty (void) const |
| | true if there is no pair
|
| int | height (void) const |
| | the levels of the tree, 0 if empty, 1 if a single leaf
|
| void | clear (void) |
| | removes the pairs, the pools keeping their memory
|
| template<std::input_iterator It> |
| void | build (It first, It last) |
| | replaces the pairs with those of [ first , last ), pairs ( key , value ) by strictly increasing keys, the nodes as full and as even as they can be, \( O( n ) \)
|
| template<class F> |
| void | forEach (F &&f) const |
| | f( key , value ) on each pair, by increasing keys, along the iterators
|
| template<class F> |
| void | forRange (const K &lo, const K &hi, F &&f) const |
| | f( key , value ) on each pair of key in \( [ \mathit{lo} , \mathit{hi} ) \), by increasing keys, \( O( ( L + I ) \log n + k ) \) for \( k \) pairs
|
| void | swap (BTreeMap &o) noexcept(detail::swapNoexcept< A >) |
| | exchanges the nodes and the pools with o, each pool with its allocator
|
| allocator_type | get_allocator (void) const |
| | the allocator of the blocks of the nodes
|
| 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()
|
| const_iterator | lowerBound (const K &k) const |
| | the first pair whose key is not smaller than \( k \), the place of \( k \) in the leaf reached by the descent of lookup()
|
| const K * | findKey (const K &k) const |
| | the address of the key \( k \), nil if absent, in \( O( ( L + I ) \log 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( ( L + I ) \log n ) \) time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the next change
|
| const_iterator | upperBound (const K &k) const |
| | the first pair whose key is larger than \( k \), the place after the keys not larger than \( k \) in the leaf reached by the descent of lookup()
|
| const_iterator | erase (const_iterator it) |
| | removes the pair of it, and gives the one after it: remove( k ) and then lowerBound( k ), \( k \) the key of it
|
| std::size_t | rank (const K &k) const |
| | the pairs whose key is smaller than \( k \), \( O( ( L + I ) \log n ) \) time
|
| const_iterator | select (std::size_t i) const |
| | the pair of rank \( i \) (from 0), end() if \( i \ge n \)
|
template<class K, class V, class Less = std::less< K >, std::size_t B = 512, bool R = false, class A = HugePageAllocator< std::pair< const K , V > >>
requires std::default_initializable< K > && std::default_initializable< V >
class ADS::BTreeMap< K, V, Less, B, R, A >
an ordered map on a B+-tree of nodes of \( B \) bytes, by the order less(), with the counts of the pairs below each child if R (the ranks)
template<class K, class V, class Less = std::less< K >, std::size_t B = 512, bool R = false, class A = HugePageAllocator< std::pair< const K , V > >>
template<std::input_iterator It>
| void ADS::BTreeMap< K, V, Less, B, R, A >::build |
( |
It | first, |
|
|
It | last ) |
replaces the pairs with those of [ first , last ), pairs ( key , value ) by strictly increasing keys, the nodes as full and as even as they can be, \( O( n ) \)
In pseudocode (the bulk loading of a B+-tree, Graefe, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{build}( P )\quad \triangleright
\ P \text{ the } n \text{ pairs, by increasing keys} \cr
2 & \quad \mathit{level} \gets \lceil n / L \rceil \text{ leaves, each }
\text{with } \lfloor n / \lceil n / L \rceil \rfloor \text{ or one }
\text{more pairs of } P \text{ in order, linked} \cr
3 & \quad \textbf{while}\ \mathit{level} \text{ has more than one }
\text{node}\ \textbf{do} \cr
4 & \qquad g \gets \lceil |\mathit{level}| / ( I + 1 ) \rceil \cr
5 & \qquad \mathit{level} \gets g \text{ internal nodes, each with }
\text{the next nodes of } \mathit{level} \text{ as children,} \cr
6 & \qquad\quad \text{as evenly as possible, and the largest key }
\text{below each child} \cr
& \qquad\quad \text{but the last as a bound} \cr
7 & \quad \mathit{root} \gets \text{the node of } \mathit{level}
\end{array}}
\]
- Precondition
- the keys are strictly increasing (an audit)
template<class K, class V, class Less = std::less< K >, std::size_t B = 512, bool R = false, class A = HugePageAllocator< std::pair< const K , V > >>
template<class F>
| void ADS::BTreeMap< K, V, Less, B, R, A >::forRange |
( |
const K & | lo, |
|
|
const K & | hi, |
|
|
F && | f ) const |
f( key , value ) on each pair of key in \( [ \mathit{lo} , \mathit{hi} ) \), by increasing keys, \( O( ( L + I ) \log n + k ) \) for \( k \) pairs
In pseudocode (the scan of the leaves of a B+-tree, Comer, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{forRange}( \mathit{lo} , \mathit{hi} ,
f ) \cr
2 & \quad \textbf{foreach}\ ( k , v ) \text{ from }
\mathrm{lowerBound}( \mathit{lo} ) \text{ on}\ \textbf{do} \cr
3 & \qquad \textbf{if}\ k \ge \mathit{hi}\ \textbf{then return} \cr
4 & \qquad f( k , v )
\end{array}}
\]
template<class K, class V, class Less = std::less< K >, std::size_t B = 512, bool R = false, class A = HugePageAllocator< std::pair< const K , V > >>
| bool ADS::BTreeMap< K, V, Less, B, R, A >::insert |
( |
const K & | k, |
|
|
const V & | v ) |
gives \( k \) the value \( v \); true if \( k \) was not there
In pseudocode (the insertion of a B+-tree, Comer, beyond the book), \( \pi \) the path of the descent, the places \( ( u , i ) \) of the internal nodes:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{insert}( k , v ) \cr
2 & \quad \textbf{if}\ \text{the tree is empty}\ \textbf{then}
\ \mathit{root} \gets \text{a leaf with } ( k , v );
\ \textbf{return true} \cr
3 & \quad \textbf{if}\ k > \text{the largest key}\ \textbf{and}
\ \text{the last leaf has room}\ \textbf{then} \cr
4 & \qquad \text{append } ( k , v ) \text{ to the last leaf};
\ \textbf{return true}\quad \triangleright\ \text{no descent} \cr
5 & \quad x \gets \text{the leaf reached from the root, }
\text{recording the path } \pi;\ p \gets \mathrm{place}( x , k ) \cr
6 & \quad \textbf{if}\ x.k[ p ] = k\ \textbf{then}\ x.v[ p ] \gets
v;\ \textbf{return false} \cr
7 & \quad \textbf{if}\ x.n < L\ \textbf{then}\ \text{put } ( k , v )
\text{ at the place } p \text{ of } x \cr
8 & \quad \textbf{else if}\ x \text{ is the last leaf and } p = L
\ \textbf{then}\ r \gets \text{a new leaf with } ( k , v ) \text{ }
\text{alone};\ \mathrm{climb}( x.k[ L - 1 ] , r , \pi ) \cr
9 & \quad \textbf{else if}\ x \text{ is the first leaf and } p = 0
\ \textbf{then}\ l \gets \text{a new leaf with } ( k , v ) \text{ }
\text{alone, before } x;\ \mathrm{climb}( k , l , \pi ) \cr
10 & \quad \textbf{else} \cr
11 & \qquad r \gets \text{a new leaf after } x \text{ with the last }
\lfloor ( L + 1 ) / 2 \rfloor \text{ of the } L + 1 \text{ pairs} \cr
12 & \qquad \mathrm{climb}( \text{the largest key left in } x , r , \pi
) \cr
13 & \quad \textbf{return true} \cr
14 & \textbf{procedure}\ \mathrm{climb}( s , r , \pi )\quad
\triangleright\ \text{a child split: } s \text{ bounds its left }
\text{part, } r \text{ is new} \cr
15 & \quad \textbf{foreach}\ ( u , i ) \text{ of } \pi \text{, from }
\text{the leaf up}\ \textbf{do} \cr
16 & \qquad \textbf{if}\ u.n < I\ \textbf{then}\ \text{put } s
\text{ and } r \text{ after the child } i \text{ of } u;
\ \textbf{return} \cr
17 & \qquad \text{split the } I + 1 \text{ keys and } I + 2 \text{ }
\text{children into } u \text{ and a new } w \text{ after it,} \cr
& \qquad\quad \text{almost all in } u \text{ on the right edge, in }
w \text{ on the left edge, half each otherwise} \cr
18 & \qquad s \gets \text{the key between } u \text{ and } w;\ r \gets
w \cr
19 & \quad \mathit{root} \gets \text{a new node with the key } s
\text{ and the children the old root and } r
\end{array}}
\]
and with R the counts along \( \pi \) grow by 1, and those of the nodes that split are counted again.
template<class K, class V, class Less = std::less< K >, std::size_t B = 512, bool R = false, class A = HugePageAllocator< std::pair< const K , V > >>
| std::optional< V > ADS::BTreeMap< K, V, Less, B, R, A >::lookup |
( |
const K & | k | ) |
const |
|
nodiscard |
the value of \( k \), if there
In pseudocode (the search of a B+-tree, Comer, with the prefetching of Chen, Gibbons and Mowry, beyond the book), \( h \) the height:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{lookup}( k ) \cr
2 & \quad u \gets \mathit{root} \cr
3 & \quad \textbf{for}\ h' \gets h\ \textbf{downto}\ 2\ \textbf{do} \cr
4 & \qquad u \gets u.c[ \mathrm{place}( u , k ) ];\ \text{prefetch the }
\text{keys of } u \cr
5 & \quad p \gets \mathrm{place}( u , k ) \cr
6 & \quad \textbf{if}\ p < u.n\ \textbf{and}\ u.k[ p ] = k
\ \textbf{then return}\ u.v[ p ]\ \textbf{else return nil}
\end{array}}
\]
where \( \mathrm{place}( u , k ) \) is the number of the keys of \( u \) smaller than \( k \), counted on all the padded places at once on the arithmetic keys, by a binary search on the others.
template<class K, class V, class Less = std::less< K >, std::size_t B = 512, bool R = false, class A = HugePageAllocator< std::pair< const K , V > >>
removes \( k \); true if it was there
In pseudocode (the removal of a B+-tree, Comer, beyond the book), \( \pi \) the path of the descent:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{remove}( k ) \cr
2 & \quad x \gets \text{the leaf reached from the root, }
\text{recording the path } \pi;\ p \gets \mathrm{place}( x , k ) \cr
3 & \quad \textbf{if}\ x.k[ p ] \ne k\ \textbf{then return false} \cr
4 & \quad \text{take the pair of place } p \text{ out of } x \cr
5 & \quad \textbf{if}\ x \text{ is the root and empty}
\ \textbf{then}\ \text{the tree is empty} \cr
6 & \quad \textbf{else if}\ x.n < L / 2\ \textbf{then}
\ \mathrm{shrink}( x , \pi ) \cr
7 & \quad \textbf{return true} \cr
8 & \textbf{procedure}\ \mathrm{shrink}( x , \pi )\quad
\triangleright\ x \text{ is less than half full} \cr
9 & \quad ( u , i ) \gets \text{the last place of } \pi;\ l , r \gets
\text{the children of } u \text{ around } x \cr
10 & \quad \textbf{if}\ l \text{ and } r \text{ fit in one node}
\ \textbf{then} \cr
11 & \qquad \text{move } r \text{ (and, if internal, the bound of } u
\text{ between them) into } l;\ \text{take } r \text{ out of } u \cr
12 & \qquad \textbf{if}\ u \text{ is the root with no key}
\ \textbf{then}\ \mathit{root} \gets l \cr
13 & \qquad \textbf{else if}\ u \text{ is less than half full}
\ \textbf{then}\ \mathrm{shrink}( u , \pi \text{ without its last }
\text{place} ) \cr
14 & \quad \textbf{else}\ \text{move items between } l \text{ and } r
\text{ until they hold half each, and set the bound of } u
\end{array}}
\]
and with R the counts along \( \pi \) fall by 1, and those of the nodes that change are counted again.