ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::ChainedHashTable< K, V, C, H > Class Template Reference

a hash table with lists of overflow More...

#include <ADS/dictionary/ChainedHashTable.h>

Classes

class  const_iterator
 the iterator on the pairs, list by list More...

Public types

using Key = K
 the type of the keys
using Value = V
 the type of the values
using value_type = Pair
 the type of the items, for the standard library
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 = const Pair &
 a reference to an item
using const_reference = const Pair &
 a constant reference to an item
using iterator = const_iterator
 the iterator on the items

Public member functions

 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

Detailed description

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

Member typedef documentation

◆ const_reference

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
using ADS::ChainedHashTable< K, V, C, H >::const_reference = const Pair &

a constant reference to an item

◆ difference_type

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
using ADS::ChainedHashTable< K, V, C, H >::difference_type = std::ptrdiff_t

the type of the distances between two iterators

◆ iterator

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
using ADS::ChainedHashTable< K, V, C, H >::iterator = const_iterator

the iterator on the items

◆ Key

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
using ADS::ChainedHashTable< K, V, C, H >::Key = K

the type of the keys

◆ reference

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
using ADS::ChainedHashTable< K, V, C, H >::reference = const Pair &

a reference to an item

◆ size_type

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
using ADS::ChainedHashTable< K, V, C, H >::size_type = std::size_t

the type of the sizes

◆ Value

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
using ADS::ChainedHashTable< K, V, C, H >::Value = V

the type of the values

◆ value_type

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
using ADS::ChainedHashTable< K, V, C, H >::value_type = Pair

the type of the items, for the standard library

Constructor and destructor documentation

◆ ChainedHashTable() [1/2]

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
ADS::ChainedHashTable< K, V, C, H >::ChainedHashTable ( std::size_t m,
bool restructure = false,
H h = H() )
explicit

the empty table of \( m \) lists, which restructures itself or not, at the loads 2 and \( 1 / 2 \)

Precondition
\( m > 0 \), a power of two if \( H \) is a PlaceHash

◆ ChainedHashTable() [2/2]

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
ADS::ChainedHashTable< K, V, C, H >::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

Precondition
\( m > 0 \), a power of two if \( H \) is a PlaceHash; \( g > 2 s \ge 0 \), so that a table just doubled or halved is within the two loads

Member function documentation

◆ begin()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
const_iterator ADS::ChainedHashTable< K, V, C, H >::begin ( void ) const
inlinenodiscard

the iterator on the first item

◆ capacity()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
std::size_t ADS::ChainedHashTable< K, V, C, H >::capacity ( void ) const
nodiscard

the number \( m \) of lists

◆ cbegin()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
const_iterator ADS::ChainedHashTable< K, V, C, H >::cbegin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ cend()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
const_iterator ADS::ChainedHashTable< K, V, C, H >::cend ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ chain()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
std::vector< K > ADS::ChainedHashTable< K, V, C, H >::chain ( std::size_t i) const
nodiscard

the keys of the list \( i \), from its head

Precondition
\( i \) is less than capacity()

◆ contains()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
bool ADS::ChainedHashTable< K, V, C, H >::contains ( const K & k) const
nodiscard

true if the key \( k \) is there

◆ empty()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
bool ADS::ChainedHashTable< K, V, C, H >::empty ( void ) const
nodiscard

true if there are no keys

◆ end()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
const_iterator ADS::ChainedHashTable< K, V, C, H >::end ( void ) const
inlinenodiscard

the iterator past the last item

◆ findKey()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
const K * ADS::ChainedHashTable< K, V, C, H >::findKey ( const K & k) const
nodiscard

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

◆ forEach()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
template<class F>
void ADS::ChainedHashTable< K, V, C, H >::forEach ( F && f) const

the foreach of the book: f( k , v ) on the pairs, list after list, \( O( m + n ) \) time

◆ insert()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
void ADS::ChainedHashTable< K, V, C, H >::insert ( const K & k,
const V & v )

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

◆ load()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
double ADS::ChainedHashTable< K, V, C, H >::load ( void ) const
nodiscard

the load factor \( n / m \)

◆ lookup()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
std::optional< V > ADS::ChainedHashTable< K, V, C, H >::lookup ( const K & k) const
nodiscard

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}} \]

◆ moved()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
std::size_t ADS::ChainedHashTable< K, V, C, H >::moved ( void ) const
nodiscard

the number of pairs moved by the restructurings so far

◆ probes()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
std::size_t ADS::ChainedHashTable< K, V, C, H >::probes ( void ) const
nodiscard

the cells of the list examined by the last operation, plus one for the nil that ends an unsuccessful search

◆ remove()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
void ADS::ChainedHashTable< K, V, C, H >::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

Postcondition
\( k \) is not in the table

◆ restructures()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
std::size_t ADS::ChainedHashTable< K, V, C, H >::restructures ( void ) const
nodiscard

the number of restructurings so far

◆ size()

template<class K, class V, Chain C = Chain::FRONT, class H = std::hash< K >>
std::size_t ADS::ChainedHashTable< K, V, C, H >::size ( void ) const
nodiscard

the number \( n \) of keys


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