ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::PerfectHashTable Class Reference

a static SET of integers on the two-level perfect hashing More...

#include <ADS/dictionary/PerfectHashing.h>

Classes

struct  Places
 the places of the second level, for SlotIterator More...

Public types

using Key = std::uint64_t
 the type of the keys
using value_type = std::uint64_t
 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 std::uint64_t &
 a reference to an item
using const_reference = const std::uint64_t &
 a constant reference to an item
using const_iterator = SlotIterator< PerfectHashTable , Places >
 the iterator on the items, constant
using iterator = const_iterator
 the iterator on the items

Public member functions

 PerfectHashTable (const std::vector< std::uint64_t > &S, std::uint64_t seed=1)
 the table of the keys of \( S \)
bool contains (std::uint64_t k) const
 whether \( k \) is in \( S \), any \( k \), in \( O( 1 ) \) time in the worst case: the key in the place \( h_j( k ) \) of the table \( j = h( k ) \) compared with \( k \)
std::size_t size (void) const
 the number of keys
std::size_t space (void) const
 the places of the second level, \( \sum_j n_j^2 \)
std::size_t trials (void) const
 the functions drawn by the build, of both levels
template<class A>
void persist (A &a)
 the members written to, or read from, the archive \( a \) [see Persistence.h]
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

Static public member functions

static PerfectHashTable persistEmpty (void)
 the table of no key, to be read from a file [see Persistence.h]

Static public attributes

static constexpr std::string_view persistName = "PerfectHashTable"
 the name of the structure in a file [see Persistence.h]

Detailed description

a static SET of integers on the two-level perfect hashing

Member typedef documentation

◆ const_iterator

the iterator on the items, constant

◆ const_reference

using ADS::PerfectHashTable::const_reference = const std::uint64_t &

a constant reference to an item

◆ difference_type

the type of the distances between two iterators

◆ iterator

the iterator on the items

◆ Key

using ADS::PerfectHashTable::Key = std::uint64_t

the type of the keys

◆ reference

using ADS::PerfectHashTable::reference = const std::uint64_t &

a reference to an item

◆ size_type

using ADS::PerfectHashTable::size_type = std::size_t

the type of the sizes

◆ value_type

using ADS::PerfectHashTable::value_type = std::uint64_t

the type of the items, for the standard library

Constructor and destructor documentation

◆ PerfectHashTable()

ADS::PerfectHashTable::PerfectHashTable ( const std::vector< std::uint64_t > & S,
std::uint64_t seed = 1 )
explicit

the table of the keys of \( S \)

In pseudocode (Fredman, Komlós and Szemerédi, chapter 8 of Ferragina), \( n_j \) the keys of \( S \) with \( h( k ) = j \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{build}( S ) \cr 2 & \quad \textbf{repeat}\ h \gets \text{a function of } H_{p,n} \text{ at random}\ \textbf{until}\ \sum_j n_j^2 < 4 n \cr 3 & \quad \textbf{for}\ j \gets 0\ \textbf{to}\ n - 1\ \textbf{with}\ n_j > 0\ \textbf{do} \cr 4 & \qquad \textbf{repeat}\ h_j \gets \text{a function of } H_{p,n_j^2} \text{ at random} \cr 5 & \qquad \textbf{until}\ h_j \text{ has no collision on the keys of } \text{the list } j \cr 6 & \qquad \text{put each key } k \text{ of the list in the place } h_j( k ) \text{ of the table } j \end{array}} \]

Each table of the second level is found after fewer than 2 trials on average, since its expected collisions are below \( 1 / 2 \); the first level stops when \( \sum_j n_j^2 < 4 n \), which by the inequality of Markov on \( \sum_j n_j^2 - n \ge 0 \), of expectation below \( n \), happens with probability above \( 2 / 3 \) (a threshold of \( 2 n \) would guarantee only \( 1 / n \)).

Precondition
the keys are distinct (an audit) and below \( 2^{61} - 1 \)
Postcondition
each key of \( S \) is there (an audit), and \( \sum_j n_j^2 < 4 n \)

Member function documentation

◆ begin()

const_iterator ADS::PerfectHashTable::begin ( void ) const
inlinenodiscard

the iterator on the first item

◆ cbegin()

const_iterator ADS::PerfectHashTable::cbegin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ cend()

const_iterator ADS::PerfectHashTable::cend ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ contains()

bool ADS::PerfectHashTable::contains ( std::uint64_t k) const
nodiscard

whether \( k \) is in \( S \), any \( k \), in \( O( 1 ) \) time in the worst case: the key in the place \( h_j( k ) \) of the table \( j = h( k ) \) compared with \( k \)

In pseudocode (Fredman, Komlós and Szemerédi, beyond the book), \( T_j \) the table of \( n_j^2 \) places of the list \( j \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{contains}( k ) \cr 2 & \quad j \gets h( k ) \cr 3 & \quad \textbf{return}\ n_j > 0\ \textbf{and}\ T_j[ h_j( k ) ] = k \end{array}} \]

◆ end()

const_iterator ADS::PerfectHashTable::end ( void ) const
inlinenodiscard

the iterator past the last item

◆ persist()

template<class A>
void ADS::PerfectHashTable::persist ( A & a)

the members written to, or read from, the archive \( a \) [see Persistence.h]

◆ persistEmpty()

PerfectHashTable ADS::PerfectHashTable::persistEmpty ( void )
inlinestaticnodiscard

the table of no key, to be read from a file [see Persistence.h]

◆ size()

std::size_t ADS::PerfectHashTable::size ( void ) const
nodiscard

the number of keys

◆ space()

std::size_t ADS::PerfectHashTable::space ( void ) const
nodiscard

the places of the second level, \( \sum_j n_j^2 \)

◆ trials()

std::size_t ADS::PerfectHashTable::trials ( void ) const
nodiscard

the functions drawn by the build, of both levels

Member data documentation

◆ persistName

std::string_view ADS::PerfectHashTable::persistName = "PerfectHashTable"
staticconstexpr

the name of the structure in a file [see Persistence.h]


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