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

a hash table with open addressing and control bytes in groups of 15 More...

#include <ADS/dictionary/FlatHashTable.h>

Inheritance diagram for ADS::FlatHashTable< K, V, H, Eq, A >:

Classes

class  const_iterator
 the iterator on the full places, by its group, the slot of the first place of the group and the mask of the full places of the group from its own on, the lowest bit its place: a step clears that bit and keeps the places still full in the control bytes, so that the next place depends on the previous one by two operations on the mask and a place emptied meanwhile is skipped; past the group it skips the empty groups whole, up to a sentinel group after the last, whose byte 0 is full and whose first place is the end, so that a step tests no bound More...

Public types

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 iterator = const_iterator
 the iterator on the items
using allocator_type = A
 the type of the allocator

Public member functions

 FlatHashTable (std::size_t n=0, H h=H(), Eq eq=Eq())
 the empty table, with room for \( n \) pairs before it grows
 FlatHashTable (std::size_t n, H h, Eq eq, const A &alloc)
 the empty table, with room for \( n \) pairs before it grows, its places from alloc
 FlatHashTable (const A &alloc)
 the empty table, its places from alloc
 FlatHashTable (const FlatHashTable &o)
 a copy of other, on the allocator that select_on_container_copy_construction() gives
 FlatHashTable (const FlatHashTable &o, const A &alloc)
 a copy of other, on alloc
 FlatHashTable (FlatHashTable &&o) noexcept
 the pairs of o, which is left empty, with no places
 FlatHashTable (FlatHashTable &&o, const A &alloc)
 the pairs of o, on alloc: its places if the allocators are equal, else the pairs moved one by one to places of alloc
FlatHashTable & operator= (FlatHashTable o) noexcept
 the assignment, by copy and swap, when the allocators are all equal
FlatHashTable & operator= (const FlatHashTable &o)
 the copy assignment, with the allocator of o if it propagates
FlatHashTable & operator= (FlatHashTable &&o)
 the move assignment: the places of o if its allocator propagates or is equal, else its pairs moved one by one
 ~FlatHashTable ()
 frees the memory of the structure
std::optional< V > lookup (const K &k) const
 the value of the key \( k \), or nothing
const V * find (const K &k) const
 the value of the key \( k \), nil if it is not there
V * find (const K &k)
 the value of the key \( k \), to change in place, nil if it is not there
bool insert (const K &k, const V &v)
 associates the value \( v \) to the key \( k \); true if \( k \) was not there
bool insert (K &&k, V &&v)
 as insert( k , v ), moving the key and the value
template<class... Args>
std::pair< V *, bool > try_emplace (const K &k, Args &&... a)
 the value of the key \( k \), built from a if \( k \) was not there, and true in that case; a key already there keeps its value and a is not touched.
template<class... Args>
std::pair< V *, bool > try_emplace (K &&k, Args &&... a)
 as try_emplace( k , a ), moving the key
bool remove (const K &k)
 removes the key \( k \), if it is there, emptying its place, with no deleted mark, and adding 1 to the bound \( d \) of the places still counted; true if it was
bool contains (const K &k) const
 true if the key \( k \) is there
std::size_t size (void) const
 the number \( n \) of pairs
bool empty (void) const
 true if there are no pairs
std::size_t capacity (void) const
 the number of places, 15 times 0 or a power of 2 at least 2
double load (void) const
 the load factor \( n / m \), 0 with no places
std::size_t deleted (void) const
 the places emptied by remove() in groups that some inspection went past, which count as full until the table is rebuilt; \( O( m ) \) time, by a scan of the groups
void reserve (std::size_t n)
 room for \( n \) pairs before the table grows
void clear (void)
 removes all the pairs, keeping the places
template<class F>
void forEach (F &&f) const
 f( k , v ) on the pairs, place after place
template<class F>
void forEach (F &&f)
 f( k , v ) on the pairs, place after place, \( v \) to change in place
void swap (FlatHashTable &o) noexcept(detail::swapNoexcept< A >)
 exchanges the contents with other in \( O( 1 ) \), and the allocators if they propagate on a swap
allocator_type get_allocator (void) const
 the allocator of the places
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
const_iterator erase (const_iterator it)
 removes the pair of it, and returns the iterator on the next one

Detailed description

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
class ADS::FlatHashTable< K, V, H, Eq, A >

a hash table with open addressing and control bytes in groups of 15

Member typedef documentation

◆ allocator_type

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::FlatHashTable< K, V, H, Eq, A >::allocator_type = A

the type of the allocator

◆ const_reference

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::FlatHashTable< K, V, H, Eq, A >::const_reference = PairRef< const K & , const V & >

a constant reference to an item

◆ difference_type

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::FlatHashTable< K, V, H, Eq, A >::difference_type = std::ptrdiff_t

the type of the distances between two iterators

◆ iterator

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::FlatHashTable< K, V, H, Eq, A >::iterator = const_iterator

the iterator on the items

◆ Key

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::FlatHashTable< K, V, H, Eq, A >::Key = K

the type of the keys

◆ key_type

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::FlatHashTable< K, V, H, Eq, A >::key_type = K

the type of the keys

◆ mapped_type

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::FlatHashTable< K, V, H, Eq, A >::mapped_type = V

the type of the values

◆ reference

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::FlatHashTable< K, V, H, Eq, A >::reference = PairRef< const K & , const V & >

a reference to an item

◆ size_type

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::FlatHashTable< K, V, H, Eq, A >::size_type = std::size_t

the type of the sizes

◆ Value

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::FlatHashTable< K, V, H, Eq, A >::Value = V

the type of the values

◆ value_type

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
using ADS::FlatHashTable< K, V, H, Eq, A >::value_type = std::pair< K , V >

the type of the items, for the standard library

Constructor and destructor documentation

◆ FlatHashTable() [1/7]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
ADS::FlatHashTable< K, V, H, Eq, A >::FlatHashTable ( std::size_t n = 0,
H h = H(),
Eq eq = Eq() )
explicit

the empty table, with room for \( n \) pairs before it grows

◆ FlatHashTable() [2/7]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
ADS::FlatHashTable< K, V, H, Eq, A >::FlatHashTable ( std::size_t n,
H h,
Eq eq,
const A & alloc )

the empty table, with room for \( n \) pairs before it grows, its places from alloc

◆ FlatHashTable() [3/7]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
ADS::FlatHashTable< K, V, H, Eq, A >::FlatHashTable ( const A & alloc)
inlineexplicit

the empty table, its places from alloc

◆ FlatHashTable() [4/7]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
ADS::FlatHashTable< K, V, H, Eq, A >::FlatHashTable ( const FlatHashTable< K, V, H, Eq, A > & o)

a copy of other, on the allocator that select_on_container_copy_construction() gives

◆ FlatHashTable() [5/7]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
ADS::FlatHashTable< K, V, H, Eq, A >::FlatHashTable ( const FlatHashTable< K, V, H, Eq, A > & o,
const A & alloc )

a copy of other, on alloc

◆ FlatHashTable() [6/7]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
ADS::FlatHashTable< K, V, H, Eq, A >::FlatHashTable ( FlatHashTable< K, V, H, Eq, A > && o)
noexcept

the pairs of o, which is left empty, with no places

◆ FlatHashTable() [7/7]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
ADS::FlatHashTable< K, V, H, Eq, A >::FlatHashTable ( FlatHashTable< K, V, H, Eq, A > && o,
const A & alloc )

the pairs of o, on alloc: its places if the allocators are equal, else the pairs moved one by one to places of alloc

◆ ~FlatHashTable()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
ADS::FlatHashTable< K, V, H, Eq, A >::~FlatHashTable ( )

frees the memory of the structure

Member function documentation

◆ begin()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
const_iterator ADS::FlatHashTable< K, V, H, Eq, A >::begin ( void ) const
nodiscard

the iterator on the first item

◆ capacity()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
std::size_t ADS::FlatHashTable< K, V, H, Eq, A >::capacity ( void ) const
nodiscard

the number of places, 15 times 0 or a power of 2 at least 2

◆ cbegin()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
const_iterator ADS::FlatHashTable< K, V, H, Eq, A >::cbegin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ cend()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
const_iterator ADS::FlatHashTable< K, V, H, Eq, A >::cend ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ clear()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
void ADS::FlatHashTable< K, V, H, Eq, A >::clear ( void )

removes all the pairs, keeping the places

◆ contains()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
bool ADS::FlatHashTable< K, V, H, Eq, A >::contains ( const K & k) const
nodiscard

true if the key \( k \) is there

◆ deleted()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
std::size_t ADS::FlatHashTable< K, V, H, Eq, A >::deleted ( void ) const
nodiscard

the places emptied by remove() in groups that some inspection went past, which count as full until the table is rebuilt; \( O( m ) \) time, by a scan of the groups

◆ empty()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
bool ADS::FlatHashTable< K, V, H, Eq, A >::empty ( void ) const
nodiscard

true if there are no pairs

◆ end()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
const_iterator ADS::FlatHashTable< K, V, H, Eq, A >::end ( void ) const
nodiscard

the iterator past the last item

◆ erase()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
const_iterator ADS::FlatHashTable< K, V, H, Eq, A >::erase ( const_iterator it)

removes the pair of it, and returns the iterator on the next one

Precondition
it is on a pair of the table

◆ find() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
V * ADS::FlatHashTable< K, V, H, Eq, A >::find ( const K & k)
nodiscard

the value of the key \( k \), to change in place, nil if it is not there

◆ find() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
const V * ADS::FlatHashTable< K, V, H, Eq, A >::find ( const K & k) const
nodiscard

the value of the key \( k \), nil if it is not there

◆ forEach() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
template<class F>
void ADS::FlatHashTable< K, V, H, Eq, A >::forEach ( F && f)

f( k , v ) on the pairs, place after place, \( v \) to change in place

◆ forEach() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
template<class F>
void ADS::FlatHashTable< K, V, H, Eq, A >::forEach ( F && f) const

f( k , v ) on the pairs, place after place

◆ get_allocator()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
allocator_type ADS::FlatHashTable< K, V, H, Eq, A >::get_allocator ( void ) const
nodiscard

the allocator of the places

◆ insert() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
bool ADS::FlatHashTable< K, V, H, Eq, A >::insert ( const K & k,
const V & v )

associates the value \( v \) to the key \( k \); true if \( k \) was not there

In pseudocode (beyond the book), the inspection of lookup(), \( m \) the places and \( d \) the bound of those emptied but still counted:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insert}( k , v ) \cr 2 & \quad \textbf{if}\ k \text{ is there }\ \textbf{then}\ \text{give it } \text{the value } v;\ \textbf{return false} \cr 3 & \quad \textbf{if}\ n + d \ge 7 m / 8\ \textbf{and}\ \text{the exact } \text{count leaves fewer than } m / 16 \text{ places}\ \textbf{then} \cr 4 & \qquad \text{rebuild, twice as large if } n \text{ exceeds half of } 7 m / 8 \cr 5 & \quad j \gets \text{the first empty place of the inspection, with } \beta \text{ set in the overflow of the full groups passed} \cr 6 & \quad \textbf{if}\ d > 0 \text{ and the overflow of the group of } j \text{ is not } 0\ \textbf{then}\ d \gets d - 1 \cr 7 & \quad \text{the control byte of } j \gets t;\ \text{build } ( k , v ) \text{ in } j;\ n \gets n + 1;\ \textbf{return true} \end{array}} \]

Postcondition
\( k \) is in the table

◆ insert() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
bool ADS::FlatHashTable< K, V, H, Eq, A >::insert ( K && k,
V && v )

as insert( k , v ), moving the key and the value

◆ load()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
double ADS::FlatHashTable< K, V, H, Eq, A >::load ( void ) const
nodiscard

the load factor \( n / m \), 0 with no places

◆ lookup()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
std::optional< V > ADS::FlatHashTable< K, V, H, Eq, A >::lookup ( const K & k) const
nodiscard

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

In pseudocode (the Swiss tables of Kulukundis and the overflow of Lopez Munoz, beyond the book), \( x \) the mixed hash of \( k \), \( t \) its fingerprint and \( \beta \) its bit of overflow; the 15 control bytes of a group are compared with \( t \) at once:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lookup}( k ) \cr 2 & \quad g \gets \text{the top bits of } x;\ i \gets 0 \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \textbf{foreach}\ \text{place } j \text{ of the group } g \text{ whose control byte is } t\ \textbf{do} \cr 5 & \qquad\quad \textbf{if}\ \text{the key of } j \text{ is } k \ \textbf{then return}\ \text{its value} \cr 6 & \qquad \textbf{if}\ \text{the overflow of } g \text{ lacks } \beta \ \textbf{then return nil} \cr 7 & \qquad i \gets i + 1;\ g \gets ( g + i ) \bmod G\quad \triangleright\ \text{the triangular order} \end{array}} \]

◆ operator=() [1/3]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
FlatHashTable & ADS::FlatHashTable< K, V, H, Eq, A >::operator= ( const FlatHashTable< K, V, H, Eq, A > & o)

the copy assignment, with the allocator of o if it propagates

◆ operator=() [2/3]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
FlatHashTable & ADS::FlatHashTable< K, V, H, Eq, A >::operator= ( FlatHashTable< K, V, H, Eq, A > && o)

the move assignment: the places of o if its allocator propagates or is equal, else its pairs moved one by one

◆ operator=() [3/3]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
FlatHashTable & ADS::FlatHashTable< K, V, H, Eq, A >::operator= ( FlatHashTable< K, V, H, Eq, A > o)
noexcept

the assignment, by copy and swap, when the allocators are all equal

◆ remove()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
bool ADS::FlatHashTable< K, V, H, Eq, A >::remove ( const K & k)

removes the key \( k \), if it is there, emptying its place, with no deleted mark, and adding 1 to the bound \( d \) of the places still counted; true if it was

Postcondition
\( k \) is not in the table

◆ reserve()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
void ADS::FlatHashTable< K, V, H, Eq, A >::reserve ( std::size_t n)

room for \( n \) pairs before the table grows

◆ size()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
std::size_t ADS::FlatHashTable< K, V, H, Eq, A >::size ( void ) const
nodiscard

the number \( n \) of pairs

◆ swap()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
void ADS::FlatHashTable< K, V, H, Eq, A >::swap ( FlatHashTable< K, V, H, Eq, A > & o)
noexcept

exchanges the contents with other in \( O( 1 ) \), and the allocators if they propagate on a swap

Precondition
the allocators are equal, if they do not propagate

◆ try_emplace() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
template<class... Args>
std::pair< V *, bool > ADS::FlatHashTable< K, V, H, Eq, A >::try_emplace ( const K & k,
Args &&... a )

the value of the key \( k \), built from a if \( k \) was not there, and true in that case; a key already there keeps its value and a is not touched.

The search is the one of insert(), so that a value read and then changed costs one inspection; the pointer is valid until the next removal of this pair or rebuild of the table

Postcondition
\( k \) is in the table

◆ try_emplace() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class A = std::allocator< std::pair< const K , V > >>
template<class... Args>
std::pair< V *, bool > ADS::FlatHashTable< K, V, H, Eq, A >::try_emplace ( K && k,
Args &&... a )

as try_emplace( k , a ), moving the key


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