ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
OpenHashTable.h File Reference

The hash table with open addressing of section 7.2.2, a realization of the DICTIONARY [see ADT.h]. More...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include "ADS/dictionary/TableSize.h"
#include "ADS/numeric/RandomNumbers.h"
#include <algorithm>
#include <atomic>
#include <concepts>
#include <cstddef>
#include <functional>
#include <numeric>
#include <optional>
#include <random>
#include <stdexcept>
#include <utility>
#include <vector>
#include "ADS/dictionary/impl/OpenHashTable.tpp"

Go to the source code of this file.

Classes

class  ADS::Probing
 a sequence of inspection \( H( k , i ) \) of an OpenHashTable More...
class  ADS::OpenHashTable< K, V, H >
 a hash table with open addressing More...
struct  ADS::OpenHashTable< K, V, H >::Places
 the places of the table, for SlotIterator More...

Namespaces

namespace  ADS

Detailed description

The hash table with open addressing of section 7.2.2, a realization of the DICTIONARY [see ADT.h].

The table keeps the pairs in the vector itself (open addressing): the key \( k \) goes into the first free place of its sequence of inspection \( H( k , 0 ) , H( k , 1 ) , \ldots , H( k , m - 1 ) \), which is

  • linear, \( H( k , i ) = ( H_0( k ) + \delta i ) \bmod m \), a permutation of the places when \( \gcd( \delta , m ) = 1 \);
  • quadratic, \( H( k , i ) = ( H_0( k ) + c_1 i + c_2 i^2 ) \bmod m \) with \( m \) prime, which in general is not a permutation, so an insertion may fail while some place is still free;
  • double hashing, \( H( k , i ) = ( H_0( k ) + H_\delta( k ) i ) \bmod m \), with \( H_\delta( k ) = 1 + ( h( k ) \bmod ( m - 1 ) ) \) for \( m \) prime and \( H_\delta( k ) = ( \lfloor h( k ) / m \rfloor \bmod m ) | 1 \), odd, for \( m \) a power of two, so that the step is coprime with \( m \)

or, beyond the book, the triangular inspection \( H( k , i ) = ( H_0( k ) + i ( i + 1 ) / 2 ) \bmod m \), a permutation when \( m \) is a power of two, and the double hashing whose step comes from a second function \( g \) of the hash, \( H_\delta( k ) = 1 + ( g( h( k ) ) \bmod ( m - 1 ) ) \) for \( m \) prime and \( ( g( h( k ) ) \bmod m ) | 1 \) for \( m \) a power of two, and the pseudo-random inspection \( H( k , i ) = ( H_0( k ) + p_i ) \bmod m \), where \( p_0 = 0 , p_1 , \ldots , p_{m-1} \) is a fixed random permutation of \( 0 , \ldots , m - 1 \), drawn from a seed for each size \( m \), so that it works with every \( m \) and two keys with different \( H_0 \) do not share the rest of their inspection as in the linear one (no primary clustering)

[see Probing], with \( H_0( k ) = h( k ) \bmod m \) and \( h \) the hash of the keys. A place is nil (never used), full or deleted: the deleted places, left by remove(), do not stop an inspection but are reused by insert(). The scan of the book stops at the key, at a nil place or after \( m \) inspections, and remembers the first deleted place met [see scan()].

With the load factor \( \alpha = n / m \), the expected number of places accessed by an unsuccessful search \( I( \alpha ) \) and by a successful one \( S( \alpha ) \) are, under uniform hashing,

\[ \begin{array}{lll} \mbox{linear} & \frac{1}{2} \left( 1 + \frac{1}{( 1 - \alpha )^2} \right) & \frac{1}{2} \left( 1 + \frac{1}{1 - \alpha} \right) \cr \mbox{double} & \frac{1}{1 - \alpha} & - \frac{1}{\alpha} \ln( 1 - \alpha ) \end{array} \]

and probes() gives the number of places that the last operation examined, the nil that ends an unsuccessful search included.

The table may restructure itself (section 7.4): it doubles \( m \) when the full and deleted places are more than \( m / 2 \), so that \( \alpha \leq 1 / 2 \) always and the deleted places go away, and halves it, down to the initial size, when \( \alpha < 1 / 8 \). The two thresholds are 4 times apart, not twice as in the example of the book (0.5 and 0.25): with the latter the table just doubled has \( \alpha \approx 1 / 4 \), a couple of removals halve it back to \( \alpha \approx 1 / 2 \), a single insertion doubles it again, and a sequence alternating them costs \( O( m ) \) per operation [see TableSize.h]. A new size that the inspection does not work with, a prime that divides the step of a linear one, is replaced by the next prime that it works with. Since only a removal halves \( m \), a table emptied may stay above its initial size. When the places are more than \( m / 2 \) because of the deleted ones, with at most \( m / 4 \) keys, the table is rebuilt at the same size instead, as the LinearHashTable of section 5.2 of Morin's book does by sizing the new table on the keys alone (the smallest \( 2^d \ge 3 n \)): the deleted places go away, and at least \( m / 4 \) insertions come before the next rebuild, so that the cost stays \( O( 1 ) \) amortized without the doubling that a table emptied by removals and refilled with other keys would otherwise repeat.

A hash that is a PlaceHash [see TableSize.h] gives \( H_0( k ) \) itself on a table of \( 2^d \) places, e.g., the tabulation hashing of section 5.2.3 of Morin's book [see TabulationHash in RandomHashing.h], with which the table with the linear inspection and the restructuring is the LinearHashTable of the book; the other places of the inspection come from \( H_0( k ) \) alone, so that the double hashing degenerates into the linear inspection with such a hash.

The contracts [see Contract.h]: \( m > 0 \) and an inspection that works with \( m \) (a linear step coprime with \( m \), a prime \( m \) for the quadratic one, a power of two for the triangular one, a prime or a power of two for the double hashing, any \( m \) for the pseudo-random one), a full place for keyAt(); after insert() the key is in the table and after remove() it is not; the invariant is that the first place of the inspection of each key that holds it is its own place, with no nil before it, and that size() and the deleted places are counted right. On a table of more than 1024 places the invariant is checked on the inspection of the key of the operation only, up to its first nil, so that a long sequence of operations at the level 3 stays linear; the checks do not change probes(). An insertion whose inspection finds no place, which a quadratic inspection allows while some place is free, is not a fault of the caller and throws std::overflow_error. The operations are defined in OpenHashTable.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, sections 7.2-7.4, https://algoritmicamente.it/materiali/libro/capitoli/cap07.pdf
D. E. Knuth, The Art of Computer Programming, vol. 3, 2nd ed., Addison-Wesley, 1998, section 6.4 (the pseudo-random and the quadratic inspections)
W. W. Peterson, Addressing for random-access storage, IBM Journal of Research and Development 1(2), 130-146, 1957, doi:10.1147/rd.12.0130
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa