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

The hash table with lists of overflow of section 7.2.1, a realization of the DICTIONARY [see ADT.h]: the place \( i \) of a vector of size \( m \) holds a LIST with the pairs whose keys have \( H( k ) = i \), insert() puts a new key at the head of its list, lookup() and remove() go along it. More...

#include "ADS/Contract.h"
#include "ADS/Portable.h"
#include "ADS/dictionary/TableSize.h"
#include "ADS/sequence/List.h"
#include <concepts>
#include <cstddef>
#include <functional>
#include <iterator>
#include <optional>
#include <utility>
#include <vector>
#include "ADS/dictionary/impl/ChainedHashTable.tpp"

Go to the source code of this file.

Classes

class  ADS::ChainedHashTable< K, V, C, H >
 a hash table with lists of overflow More...
class  ADS::ChainedHashTable< K, V, C, H >::const_iterator
 the iterator on the pairs, list by list More...

Namespaces

namespace  ADS

Enumerations

enum class  ADS::Chain { ADS::Chain::FRONT , ADS::Chain::MOVE_TO_FRONT , ADS::Chain::SORTED }
 how a ChainedHashTable keeps its lists More...

Detailed description

The hash table with lists of overflow of section 7.2.1, a realization of the DICTIONARY [see ADT.h]: the place \( i \) of a vector of size \( m \) holds a LIST with the pairs whose keys have \( H( k ) = i \), insert() puts a new key at the head of its list, lookup() and remove() go along it.

A list may also bring to its head the key just found, so that the keys looked up often stay close to it (exercise 7.5), or be kept sorted, so that an unsuccessful search stops at the first larger key (exercise 7.10) [see Chain].

With the load factor \( \alpha = n / m \), an unsuccessful search accesses \( 1 + \alpha \) cells on average and a successful one \( 1 + \alpha / 2 \); probes() gives the cells that the last operation examined, the nil that ends an unsuccessful search included. The table may restructure itself (section 7.4), doubling \( m \) when \( \alpha > 2 \) and halving it, down to the initial size, when \( \alpha < 1 / 2 \): the two thresholds are 4 times apart, so that between two restructurings there are at least \( m / 4 \) operations and the cost stays \( O( 1 ) \) amortized [see TableSize.h]; any other two loads \( g > 2 s \) may be given, e.g., the 1 and \( 1 / 3 \) of the ChainedHashTable of section 5.1 of Morin's book. A hash that is a PlaceHash [see TableSize.h], e.g., the multiplicative hashing of Morin's book [see MultiplicativeHash in UniversalHashing.h], gives the place itself on a table of \( 2^d \) lists, which with \( g = 1 \) and \( s = 1 / 3 \) is the table of the book, its lists in the List of ADS instead of an array each.

The contracts [see Contract.h]: \( m > 0 \) and a list that exists for chain(); after insert() the key is in the table and after remove() it is not; the invariant is that each key is in the list of its own place, that the lists are sorted when they must be, that size() counts the keys, and that the key of the operation is in its list at most once. On a table of more than 1024 lists the invariant is checked on the list of that key only, so that a long sequence of operations at the level 3 stays linear; the checks do not change probes(). The operations are defined in ChainedHashTable.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, sections 7.2-7.4, https://algoritmicamente.it/materiali/libro/capitoli/cap07.pdf
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 5.1
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