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

a hash table with open addressing More...

#include <ADS/dictionary/OpenHashTable.h>

Classes

struct  Places
 the places of the table, for SlotIterator More...

Public types

enum class  Slot { NIL , FULL , DELETED }
 the state of a place More...
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 const_iterator = SlotIterator< OpenHashTable , Places >
 the iterator on the items, constant
using iterator = const_iterator
 the iterator on the items

Public member functions

 OpenHashTable (std::size_t m, Probing probe, bool restructure=false, H h=H())
 the empty table of size m with the given inspection, which restructures itself or not
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 \); throws std::overflow_error if the inspection finds no place
void remove (const K &k)
 removes the key \( k \), if it is there, leaving a deleted place, by scan( k , false ) of lookup(); the table, if it restructures itself, halves below the load \( 1 / 8 \)
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 size \( m \) of the vector
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, place after place, \( O( m ) \) time
Slot slot (std::size_t i) const
 the state of the place \( i \)
const K & keyAt (std::size_t i) const
 the key in the place \( i \)
std::size_t probes (void) const
 the places examined by the last operation
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, class H = std::hash< K >>
class ADS::OpenHashTable< K, V, H >

a hash table with open addressing

Member typedef documentation

◆ const_iterator

template<class K, class V, class H = std::hash< K >>
using ADS::OpenHashTable< K, V, H >::const_iterator = SlotIterator< OpenHashTable , Places >

the iterator on the items, constant

◆ const_reference

template<class K, class V, class H = std::hash< K >>
using ADS::OpenHashTable< K, V, H >::const_reference = PairRef< const K & , const V & >

a constant reference to an item

◆ difference_type

template<class K, class V, class H = std::hash< K >>
using ADS::OpenHashTable< K, V, H >::difference_type = std::ptrdiff_t

the type of the distances between two iterators

◆ iterator

template<class K, class V, class H = std::hash< K >>
using ADS::OpenHashTable< K, V, H >::iterator = const_iterator

the iterator on the items

◆ Key

template<class K, class V, class H = std::hash< K >>
using ADS::OpenHashTable< K, V, H >::Key = K

the type of the keys

◆ key_type

template<class K, class V, class H = std::hash< K >>
using ADS::OpenHashTable< K, V, H >::key_type = K

the type of the keys

◆ mapped_type

template<class K, class V, class H = std::hash< K >>
using ADS::OpenHashTable< K, V, H >::mapped_type = V

the type of the values

◆ reference

template<class K, class V, class H = std::hash< K >>
using ADS::OpenHashTable< K, V, H >::reference = PairRef< const K & , const V & >

a reference to an item

◆ size_type

template<class K, class V, class H = std::hash< K >>
using ADS::OpenHashTable< K, V, H >::size_type = std::size_t

the type of the sizes

◆ Value

template<class K, class V, class H = std::hash< K >>
using ADS::OpenHashTable< K, V, H >::Value = V

the type of the values

◆ value_type

template<class K, class V, class H = std::hash< K >>
using ADS::OpenHashTable< K, V, H >::value_type = std::pair< K , V >

the type of the items, for the standard library

Member enumeration documentation

◆ Slot

template<class K, class V, class H = std::hash< K >>
enum class ADS::OpenHashTable::Slot
strong

the state of a place

Enumerator
NIL 

never used: it stops an inspection

FULL 

it holds a pair

DELETED 

emptied by remove(): an inspection goes past it

Constructor and destructor documentation

◆ OpenHashTable()

template<class K, class V, class H = std::hash< K >>
ADS::OpenHashTable< K, V, H >::OpenHashTable ( std::size_t m,
Probing probe,
bool restructure = false,
H h = H() )

the empty table of size m with the given inspection, which restructures itself or not

Precondition
\( m > 0 \) and probe.works( m )

Member function documentation

◆ begin()

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

the iterator on the first item

◆ capacity()

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

the size \( m \) of the vector

◆ cbegin()

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

the constant iterator on the first item

◆ cend()

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

the constant iterator past the last item

◆ contains()

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

true if the key \( k \) is there

◆ empty()

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

true if there are no keys

◆ end()

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

the iterator past the last item

◆ findKey()

template<class K, class V, class H = std::hash< K >>
const K * ADS::OpenHashTable< K, V, 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, class H = std::hash< K >>
template<class F>
void ADS::OpenHashTable< K, V, H >::forEach ( F && f) const

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

◆ insert()

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

associates the value \( v \) to the key \( k \); throws std::overflow_error if the inspection finds no place

In pseudocode (sections 7.2.2 and 7.4), the table doubling when its full and deleted places pass \( m / 2 \), if it restructures itself:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( k , v ) \cr 2 & \quad j \gets \mathrm{scan}( k , \textbf{true} ) \cr 3 & \quad \textbf{if}\ T[ j ] \text{ is full with a key other than } k \ \textbf{then}\ \text{throw an overflow}\quad \triangleright\ m \text{ probes, no place} \cr 4 & \quad \textbf{if}\ T[ j ] \text{ is not full}\ \textbf{then}\ n \gets n + 1 \cr 5 & \quad T[ j ] \gets ( k , v ) \text{, full} \cr 6 & \quad \textbf{if}\ m < 2 ( \text{the full places} + \text{the deleted places} )\ \textbf{then}\ \text{rebuild on } \mathrm{resized}( m ) \text{ places} \end{array}} \]

Postcondition
\( k \) is in the table

◆ keyAt()

template<class K, class V, class H = std::hash< K >>
const K & ADS::OpenHashTable< K, V, H >::keyAt ( std::size_t i) const
nodiscard

the key in the place \( i \)

Precondition
\( i \) is less than capacity() and the place is full

◆ load()

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

the load factor \( n / m \)

◆ lookup()

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

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

In pseudocode (section 7.2.2), \( T[ 0 .. m - 1 ] \) the places; the scan of the book stops at the key, at a nil place or after \( m \) inspections, and for an insertion remembers the first deleted place:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{scan}( k , \mathit{insert} )\quad \triangleright\ \text{the probes } H( k , 0 ) , H( k , 1 ) , \ldots \cr 2 & \quad i \gets 0;\ j \gets H( k , 0 );\ d \gets \textbf{nil}\quad \triangleright\ d \text{ the first deleted place met} \cr 3 & \quad \textbf{while}\ T[ j ] \text{ does not hold } k\ \textbf{and} \ T[ j ] \text{ is not nil}\ \textbf{and}\ i < m\ \textbf{do} \cr 4 & \qquad \textbf{if}\ d = \textbf{nil}\ \textbf{and}\ T[ j ] \text{ is deleted}\ \textbf{then}\ d \gets j \cr 5 & \qquad i \gets i + 1;\ j \gets H( k , i ) \cr 6 & \quad \textbf{if}\ \mathit{insert}\ \textbf{and}\ d \ne \textbf{nil}\ \textbf{and}\ T[ j ] \text{ does not hold } k \ \textbf{then return}\ d\quad \triangleright\ \text{reuse it} \cr 7 & \quad \textbf{return}\ j \cr 8 & \textbf{function}\ \mathrm{lookup}( k ) \cr 9 & \quad j \gets \mathrm{scan}( k , \textbf{false} ) \cr 10 & \quad \textbf{return}\ T[ j ].\mathit{value} \text{ if } T[ j ] \text{ holds } k \text{, else } \textbf{nil} \end{array}} \]

◆ moved()

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

the number of pairs moved by the restructurings so far

◆ probes()

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

the places examined by the last operation

◆ remove()

template<class K, class V, class H = std::hash< K >>
void ADS::OpenHashTable< K, V, H >::remove ( const K & k)

removes the key \( k \), if it is there, leaving a deleted place, by scan( k , false ) of lookup(); the table, if it restructures itself, halves below the load \( 1 / 8 \)

Postcondition
\( k \) is not in the table

◆ restructures()

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

the number of restructurings so far

◆ size()

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

the number \( n \) of keys

◆ slot()

template<class K, class V, class H = std::hash< K >>
Slot ADS::OpenHashTable< K, V, H >::slot ( std::size_t i) const
nodiscard

the state of the place \( i \)

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

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