|
| | ChainedHashTable (std::size_t m, bool restructure=false, H h=H()) |
| | the empty table of \( m \) lists, which restructures itself or not, at the loads 2 and \( 1 / 2 \)
|
| | ChainedHashTable (std::size_t m, double g, double s, H h=H()) |
| | the empty table of \( m \) lists, which doubles when its load exceeds \( g \) and halves, down to \( m \), when it falls below \( s \); \( g = 1 \) and \( s = 1 / 3 \) are the rules of the ChainedHashTable of section 5.1 of Morin's book, which keep the lists shorter, \( 1 + \alpha \le 2 \) cells per unsuccessful search, at the price of a table up to twice as large
|
| std::optional< V > | lookup (const K &k) const |
| | the value of the key \( k \), or nothing
|
| void | insert (const K &k, const V &v) |
| | associates the value \( v \) to the key \( k \)
|
| void | remove (const K &k) |
| | removes the key \( k \), if it is there, from its list; the table, if it restructures itself, halves below the load \( s \), \( 1 / 2 \) by default, down to its initial size
|
| bool | contains (const K &k) const |
| | true if the key \( k \) is there
|
| const K * | findKey (const K &k) const |
| | the address of the key \( k \), nil if absent, in \( O( 1 ) \) expected time: the find( x ) of a USet of Morin, which makes DictionarySet a USet [see SetInterfaces.h]; valid until the next change of the table
|
| std::size_t | size (void) const |
| | the number \( n \) of keys
|
| bool | empty (void) const |
| | true if there are no keys
|
| std::size_t | capacity (void) const |
| | the number \( m \) of lists
|
| double | load (void) const |
| | the load factor \( n / m \)
|
| template<class F> |
| void | forEach (F &&f) const |
| | the foreach of the book: f( k , v ) on the pairs, list after list, \( O( m + n ) \) time
|
| std::vector< K > | chain (std::size_t i) const |
| | the keys of the list \( i \), from its head
|
| std::size_t | probes (void) const |
| | the cells of the list examined by the last operation, plus one for the nil that ends an unsuccessful search
|
| std::size_t | restructures (void) const |
| | the number of restructurings so far
|
| std::size_t | moved (void) const |
| | the number of pairs moved by the restructurings so far
|
| 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
|
template<class K, class V,
Chain C = Chain::FRONT, class H = std::hash< K >>
requires ( C !=
Chain::SORTED || std::totally_ordered< K > )
class ADS::ChainedHashTable< K, V, C, H >
a hash table with lists of overflow
template<class K, class V,
Chain C = Chain::FRONT, class H = std::hash< K >>
associates the value \( v \) to the key \( k \)
In pseudocode (sections 7.2.1 and 7.4), the table doubling past the load \( g \), 2 by default, if it restructures itself [see resized()]:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{insert}( k , v ) \cr
2 & \quad L \gets T[ H( k ) ];\ c \gets \text{the cell of } L
\text{ with key } k \text{, by a scan} \cr
3 & \quad \textbf{if}\ c \ne \textbf{nil}\ \textbf{then}
\ \text{set the value of } c \text{ to } v;\ \textbf{return} \cr
4 & \quad \text{insert } ( k , v ) \text{ in } L
\text{, in its order if the lists are sorted, else at the head};\ n
\gets n + 1 \cr
5 & \quad \textbf{if}\ g m < n\ \textbf{then}
\ \text{rehash all the pairs into } \mathrm{resized}( m )
\text{ new lists}\quad \triangleright\ \text{the load above } g
\end{array}}
\]
- Postcondition
- \( k \) is in the table
template<class K, class V,
Chain C = Chain::FRONT, class H = std::hash< K >>
the value of the key \( k \), or nothing
In pseudocode (section 7.2.1, with the move to the front of exercise 7.5), \( T[ 0 .. m - 1 ] \) the lists; with Chain::SORTED the scan stops also at the first key larger than \( k \) (exercise 7.10):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{lookup}( k ) \cr
2 & \quad L \gets T[ H( k ) ];\ c \gets \text{the cell of } L
\text{ with key } k \text{, by a scan}\quad \triangleright
\ \text{only this list} \cr
3 & \quad \textbf{if}\ c = \textbf{nil}\ \textbf{then return nil} \cr
4 & \quad \textbf{if}\ \text{the lists move to the front}
\ \textbf{then}\ \text{move } c \text{ to the head of } L \cr
5 & \quad \textbf{return}\ \text{the value of } c
\end{array}}
\]