|
ADS
Algorithms and Data Structures
|
a hash table with open addressing and control bytes in groups of 15 More...
#include <ADS/dictionary/FlatHashTable.h>
Classes | |
| class | const_iterator |
| the iterator on the full places, by its group, the slot of the first place of the group and the mask of the full places of the group from its own on, the lowest bit its place: a step clears that bit and keeps the places still full in the control bytes, so that the next place depends on the previous one by two operations on the mask and a place emptied meanwhile is skipped; past the group it skips the empty groups whole, up to a sentinel group after the last, whose byte 0 is full and whose first place is the end, so that a step tests no bound More... | |
Public types | |
| using | Key = K |
| the type of the keys | |
| using | Value = V |
| the type of the values | |
| using | value_type = std::pair< K , V > |
| the type of the items, for the standard library | |
| 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 between two iterators | |
| using | reference = PairRef< const K & , const V & > |
| a reference to an item | |
| using | const_reference = PairRef< const K & , const V & > |
| a constant reference to an item | |
| using | iterator = const_iterator |
| the iterator on the items | |
| using | allocator_type = A |
| the type of the allocator | |
Public member functions | |
| FlatHashTable (std::size_t n=0, H h=H(), Eq eq=Eq()) | |
| the empty table, with room for \( n \) pairs before it grows | |
| FlatHashTable (std::size_t n, H h, Eq eq, const A &alloc) | |
| the empty table, with room for \( n \) pairs before it grows, its places from alloc | |
| FlatHashTable (const A &alloc) | |
| the empty table, its places from alloc | |
| FlatHashTable (const FlatHashTable &o) | |
| a copy of other, on the allocator that select_on_container_copy_construction() gives | |
| FlatHashTable (const FlatHashTable &o, const A &alloc) | |
| a copy of other, on alloc | |
| FlatHashTable (FlatHashTable &&o) noexcept | |
| the pairs of o, which is left empty, with no places | |
| FlatHashTable (FlatHashTable &&o, const A &alloc) | |
| the pairs of o, on alloc: its places if the allocators are equal, else the pairs moved one by one to places of alloc | |
| FlatHashTable & | operator= (FlatHashTable o) noexcept |
| the assignment, by copy and swap, when the allocators are all equal | |
| FlatHashTable & | operator= (const FlatHashTable &o) |
| the copy assignment, with the allocator of o if it propagates | |
| FlatHashTable & | operator= (FlatHashTable &&o) |
| the move assignment: the places of o if its allocator propagates or is equal, else its pairs moved one by one | |
| ~FlatHashTable () | |
| frees the memory of the structure | |
| std::optional< V > | lookup (const K &k) const |
| the value of the key \( k \), or nothing | |
| const V * | find (const K &k) const |
| the value of the key \( k \), nil if it is not there | |
| V * | find (const K &k) |
| the value of the key \( k \), to change in place, nil if it is not there | |
| bool | insert (const K &k, const V &v) |
| associates the value \( v \) to the key \( k \); true if \( k \) was not there | |
| bool | insert (K &&k, V &&v) |
| as insert( k , v ), moving the key and the value | |
| template<class... Args> | |
| std::pair< V *, bool > | try_emplace (const K &k, Args &&... a) |
| the value of the key \( k \), built from a if \( k \) was not there, and true in that case; a key already there keeps its value and a is not touched. | |
| template<class... Args> | |
| std::pair< V *, bool > | try_emplace (K &&k, Args &&... a) |
| as try_emplace( k , a ), moving the key | |
| bool | remove (const K &k) |
| removes the key \( k \), if it is there, emptying its place, with no deleted mark, and adding 1 to the bound \( d \) of the places still counted; true if it was | |
| bool | contains (const K &k) const |
| true if the key \( k \) is there | |
| std::size_t | size (void) const |
| the number \( n \) of pairs | |
| bool | empty (void) const |
| true if there are no pairs | |
| std::size_t | capacity (void) const |
| the number of places, 15 times 0 or a power of 2 at least 2 | |
| double | load (void) const |
| the load factor \( n / m \), 0 with no places | |
| std::size_t | deleted (void) const |
| the places emptied by remove() in groups that some inspection went past, which count as full until the table is rebuilt; \( O( m ) \) time, by a scan of the groups | |
| void | reserve (std::size_t n) |
| room for \( n \) pairs before the table grows | |
| void | clear (void) |
| removes all the pairs, keeping the places | |
| template<class F> | |
| void | forEach (F &&f) const |
| f( k , v ) on the pairs, place after place | |
| template<class F> | |
| void | forEach (F &&f) |
| f( k , v ) on the pairs, place after place, \( v \) to change in place | |
| void | swap (FlatHashTable &o) noexcept(detail::swapNoexcept< A >) |
| exchanges the contents with other in \( O( 1 ) \), and the allocators if they propagate on a swap | |
| allocator_type | get_allocator (void) const |
| the allocator of the places | |
| const_iterator | begin (void) const |
| the iterator on the first item | |
| const_iterator | end (void) const |
| the iterator past the last item | |
| const_iterator | cbegin (void) const |
| the constant iterator on the first item | |
| const_iterator | cend (void) const |
| the constant iterator past the last item | |
| const_iterator | erase (const_iterator it) |
| removes the pair of it, and returns the iterator on the next one | |
a hash table with open addressing and control bytes in groups of 15
| using ADS::FlatHashTable< K, V, H, Eq, A >::allocator_type = A |
the type of the allocator
| using ADS::FlatHashTable< K, V, H, Eq, A >::const_reference = PairRef< const K & , const V & > |
a constant reference to an item
| using ADS::FlatHashTable< K, V, H, Eq, A >::difference_type = std::ptrdiff_t |
the type of the distances between two iterators
| using ADS::FlatHashTable< K, V, H, Eq, A >::iterator = const_iterator |
the iterator on the items
| using ADS::FlatHashTable< K, V, H, Eq, A >::Key = K |
the type of the keys
| using ADS::FlatHashTable< K, V, H, Eq, A >::key_type = K |
the type of the keys
| using ADS::FlatHashTable< K, V, H, Eq, A >::mapped_type = V |
the type of the values
| using ADS::FlatHashTable< K, V, H, Eq, A >::reference = PairRef< const K & , const V & > |
a reference to an item
| using ADS::FlatHashTable< K, V, H, Eq, A >::size_type = std::size_t |
the type of the sizes
| using ADS::FlatHashTable< K, V, H, Eq, A >::Value = V |
the type of the values
| using ADS::FlatHashTable< K, V, H, Eq, A >::value_type = std::pair< K , V > |
the type of the items, for the standard library
|
explicit |
the empty table, with room for \( n \) pairs before it grows
| ADS::FlatHashTable< K, V, H, Eq, A >::FlatHashTable | ( | std::size_t | n, |
| H | h, | ||
| Eq | eq, | ||
| const A & | alloc ) |
the empty table, with room for \( n \) pairs before it grows, its places from alloc
|
inlineexplicit |
the empty table, its places from alloc
| ADS::FlatHashTable< K, V, H, Eq, A >::FlatHashTable | ( | const FlatHashTable< K, V, H, Eq, A > & | o | ) |
a copy of other, on the allocator that select_on_container_copy_construction() gives
| ADS::FlatHashTable< K, V, H, Eq, A >::FlatHashTable | ( | const FlatHashTable< K, V, H, Eq, A > & | o, |
| const A & | alloc ) |
a copy of other, on alloc
|
noexcept |
the pairs of o, which is left empty, with no places
| ADS::FlatHashTable< K, V, H, Eq, A >::FlatHashTable | ( | FlatHashTable< K, V, H, Eq, A > && | o, |
| const A & | alloc ) |
the pairs of o, on alloc: its places if the allocators are equal, else the pairs moved one by one to places of alloc
| ADS::FlatHashTable< K, V, H, Eq, A >::~FlatHashTable | ( | ) |
frees the memory of the structure
|
nodiscard |
the iterator on the first item
|
nodiscard |
the number of places, 15 times 0 or a power of 2 at least 2
|
inlinenodiscard |
the constant iterator on the first item
|
inlinenodiscard |
the constant iterator past the last item
| void ADS::FlatHashTable< K, V, H, Eq, A >::clear | ( | void | ) |
removes all the pairs, keeping the places
|
nodiscard |
true if the key \( k \) is there
|
nodiscard |
the places emptied by remove() in groups that some inspection went past, which count as full until the table is rebuilt; \( O( m ) \) time, by a scan of the groups
|
nodiscard |
true if there are no pairs
|
nodiscard |
the iterator past the last item
| const_iterator ADS::FlatHashTable< K, V, H, Eq, A >::erase | ( | const_iterator | it | ) |
removes the pair of it, and returns the iterator on the next one
|
nodiscard |
the value of the key \( k \), to change in place, nil if it is not there
|
nodiscard |
the value of the key \( k \), nil if it is not there
| void ADS::FlatHashTable< K, V, H, Eq, A >::forEach | ( | F && | f | ) |
f( k , v ) on the pairs, place after place, \( v \) to change in place
| void ADS::FlatHashTable< K, V, H, Eq, A >::forEach | ( | F && | f | ) | const |
f( k , v ) on the pairs, place after place
|
nodiscard |
the allocator of the places
| bool ADS::FlatHashTable< K, V, H, Eq, A >::insert | ( | const K & | k, |
| const V & | v ) |
associates the value \( v \) to the key \( k \); true if \( k \) was not there
In pseudocode (beyond the book), the inspection of lookup(), \( m \) the places and \( d \) the bound of those emptied but still counted:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insert}( k , v ) \cr 2 & \quad \textbf{if}\ k \text{ is there }\ \textbf{then}\ \text{give it } \text{the value } v;\ \textbf{return false} \cr 3 & \quad \textbf{if}\ n + d \ge 7 m / 8\ \textbf{and}\ \text{the exact } \text{count leaves fewer than } m / 16 \text{ places}\ \textbf{then} \cr 4 & \qquad \text{rebuild, twice as large if } n \text{ exceeds half of } 7 m / 8 \cr 5 & \quad j \gets \text{the first empty place of the inspection, with } \beta \text{ set in the overflow of the full groups passed} \cr 6 & \quad \textbf{if}\ d > 0 \text{ and the overflow of the group of } j \text{ is not } 0\ \textbf{then}\ d \gets d - 1 \cr 7 & \quad \text{the control byte of } j \gets t;\ \text{build } ( k , v ) \text{ in } j;\ n \gets n + 1;\ \textbf{return true} \end{array}} \]
| bool ADS::FlatHashTable< K, V, H, Eq, A >::insert | ( | K && | k, |
| V && | v ) |
as insert( k , v ), moving the key and the value
|
nodiscard |
the load factor \( n / m \), 0 with no places
|
nodiscard |
the value of the key \( k \), or nothing
In pseudocode (the Swiss tables of Kulukundis and the overflow of Lopez Munoz, beyond the book), \( x \) the mixed hash of \( k \), \( t \) its fingerprint and \( \beta \) its bit of overflow; the 15 control bytes of a group are compared with \( t \) at once:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lookup}( k ) \cr 2 & \quad g \gets \text{the top bits of } x;\ i \gets 0 \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \textbf{foreach}\ \text{place } j \text{ of the group } g \text{ whose control byte is } t\ \textbf{do} \cr 5 & \qquad\quad \textbf{if}\ \text{the key of } j \text{ is } k \ \textbf{then return}\ \text{its value} \cr 6 & \qquad \textbf{if}\ \text{the overflow of } g \text{ lacks } \beta \ \textbf{then return nil} \cr 7 & \qquad i \gets i + 1;\ g \gets ( g + i ) \bmod G\quad \triangleright\ \text{the triangular order} \end{array}} \]
| FlatHashTable & ADS::FlatHashTable< K, V, H, Eq, A >::operator= | ( | const FlatHashTable< K, V, H, Eq, A > & | o | ) |
the copy assignment, with the allocator of o if it propagates
| FlatHashTable & ADS::FlatHashTable< K, V, H, Eq, A >::operator= | ( | FlatHashTable< K, V, H, Eq, A > && | o | ) |
the move assignment: the places of o if its allocator propagates or is equal, else its pairs moved one by one
|
noexcept |
the assignment, by copy and swap, when the allocators are all equal
| bool ADS::FlatHashTable< K, V, H, Eq, A >::remove | ( | const K & | k | ) |
removes the key \( k \), if it is there, emptying its place, with no deleted mark, and adding 1 to the bound \( d \) of the places still counted; true if it was
| void ADS::FlatHashTable< K, V, H, Eq, A >::reserve | ( | std::size_t | n | ) |
room for \( n \) pairs before the table grows
|
nodiscard |
the number \( n \) of pairs
|
noexcept |
exchanges the contents with other in \( O( 1 ) \), and the allocators if they propagate on a swap
| std::pair< V *, bool > ADS::FlatHashTable< K, V, H, Eq, A >::try_emplace | ( | const K & | k, |
| Args &&... | a ) |
the value of the key \( k \), built from a if \( k \) was not there, and true in that case; a key already there keeps its value and a is not touched.
The search is the one of insert(), so that a value read and then changed costs one inspection; the pointer is valid until the next removal of this pair or rebuild of the table
| std::pair< V *, bool > ADS::FlatHashTable< K, V, H, Eq, A >::try_emplace | ( | K && | k, |
| Args &&... | a ) |
as try_emplace( k , a ), moving the key