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

a DICTIONARY that one thread writes and many threads read at once, by linear probing on atomic words, with no read-modify-write per insertion, readers that read on while it grows, and no removal; cleared in \( O( 1 ) \) by the policy EpochClear More...

#include <ADS/concurrent/SingleWriterFlatTable.h>

Public types

using Key = K
 the type of the keys
using Value = V
 the type of the values
using Clear = C
 the policy of clear()

Public member functions

 SingleWriterFlatTable (std::size_t n=0, H h=H(), Eq eq=Eq())
 the table of the least vector that holds \( n \) pairs with no growth, at least 64 places
 SingleWriterFlatTable (const SingleWriterFlatTable &)=delete
SingleWriterFlatTable & operator= (const SingleWriterFlatTable &)=delete
std::uint64_t hash (const K &k) const
 the hash of 64 bits of \( k \), that the operations take
std::pair< V, bool > findOrInsert (const K &k, std::uint64_t h, const V &v)
 the value held for the key \( k \) of hash \( h \), which is \( v \) if absent and then inserted, and true if it was inserted; by the writer only
std::pair< V, bool > findOrInsert (const K &k, const V &v)
 findOrInsert() above, with the hash computed
std::pair< K, bool > findOrInsertKey (const K &k, std::uint64_t h)
 the key held equal to \( k \) by Eq, of hash \( h \), which is \( k \) itself if absent and then inserted; true if it was inserted, for a set (an empty value); by the writer only
bool insert (const K &k, const V &v)
 true if the key \( k \) was absent and \( ( k , v ) \) inserted; by the writer only
template<class F>
requires ( valueBytes() > 0 )
bool update (const K &k, F &&f)
 the value of the key \( k \) replaced by f( value ), true if the key is there, by a store with release that a reader sees whole; by the writer only
std::optional< V > find (const K &k, std::uint64_t h) const
 a copy of the value of the key \( k \) of hash \( h \), or nothing; by any thread, also while the writer inserts and grows the table
std::optional< V > find (const K &k) const
 find() above, with the hash computed
std::optional< K > findKey (const K &k, std::uint64_t h) const
 a copy of the key held equal to \( k \) by Eq, of hash \( h \), or nothing, by the lookup of find(); by any thread
std::optional< K > findKey (const K &k) const
 findKey() above, with the hash computed
bool contains (const K &k, std::uint64_t h) const
 whether the key \( k \) of hash \( h \) is there; by any thread
bool contains (const K &k) const
 contains() above, with the hash computed
std::size_t size (void) const
 the pairs: exact for the writer, and for another thread a number of pairs that the table held at some time since its last clear()
bool empty (void) const
 whether there is no pair
std::size_t capacity (void) const
 the places of the vector in use
std::size_t rehashes (void) const
 the growths made so far
void reserve (std::size_t n)
 the table grown so that it holds \( n \) pairs with no growth; by the writer only
void clear (void)
 no pair, in time linear in the places, or \( O( 1 ) \) with EpochClear but once per \( 2^{|S|} - 2 \) calls; the vectors replaced are freed
std::uint64_t epoch (void) const
 the epoch of the policy EpochClear, 1 for ZeroingClear
template<class F>
void forEach (F &&f) const
 f( k , v ) on each pair, in no order
bool consistent (void) const
 whether each key is reached from its place with no free place in between, once, and the pairs are those counted

Static public attributes

static constexpr bool epochs = detail::ClearStamps< C >::epochs
 whether clear() takes \( O( 1 ) \) by epochs

Detailed description

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
class ADS::SingleWriterFlatTable< K, V, H, Eq, C >

a DICTIONARY that one thread writes and many threads read at once, by linear probing on atomic words, with no read-modify-write per insertion, readers that read on while it grows, and no removal; cleared in \( O( 1 ) \) by the policy EpochClear

Member typedef documentation

◆ Clear

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
using ADS::SingleWriterFlatTable< K, V, H, Eq, C >::Clear = C

the policy of clear()

◆ Key

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
using ADS::SingleWriterFlatTable< K, V, H, Eq, C >::Key = K

the type of the keys

◆ Value

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
using ADS::SingleWriterFlatTable< K, V, H, Eq, C >::Value = V

the type of the values

Constructor and destructor documentation

◆ SingleWriterFlatTable() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
ADS::SingleWriterFlatTable< K, V, H, Eq, C >::SingleWriterFlatTable ( std::size_t n = 0,
H h = H(),
Eq eq = Eq() )
explicit

the table of the least vector that holds \( n \) pairs with no growth, at least 64 places

◆ SingleWriterFlatTable() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
ADS::SingleWriterFlatTable< K, V, H, Eq, C >::SingleWriterFlatTable ( const SingleWriterFlatTable< K, V, H, Eq, C > & )
delete

Member function documentation

◆ capacity()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::size_t ADS::SingleWriterFlatTable< K, V, H, Eq, C >::capacity ( void ) const
inlinenodiscard

the places of the vector in use

◆ clear()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
void ADS::SingleWriterFlatTable< K, V, H, Eq, C >::clear ( void )

no pair, in time linear in the places, or \( O( 1 ) \) with EpochClear but once per \( 2^{|S|} - 2 \) calls; the vectors replaced are freed

Precondition
no other thread accesses the table

◆ consistent()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::SingleWriterFlatTable< K, V, H, Eq, C >::consistent ( void ) const
nodiscard

whether each key is reached from its place with no free place in between, once, and the pairs are those counted

Precondition
no other thread accesses the table

◆ contains() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::SingleWriterFlatTable< K, V, H, Eq, C >::contains ( const K & k) const
nodiscard

contains() above, with the hash computed

◆ contains() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::SingleWriterFlatTable< K, V, H, Eq, C >::contains ( const K & k,
std::uint64_t h ) const
nodiscard

whether the key \( k \) of hash \( h \) is there; by any thread

Precondition
hash( k ) is \( h \) (audit)

◆ empty()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::SingleWriterFlatTable< K, V, H, Eq, C >::empty ( void ) const
inlinenodiscard

whether there is no pair

◆ epoch()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::uint64_t ADS::SingleWriterFlatTable< K, V, H, Eq, C >::epoch ( void ) const
inlinenodiscard

the epoch of the policy EpochClear, 1 for ZeroingClear

◆ find() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::optional< V > ADS::SingleWriterFlatTable< K, V, H, Eq, C >::find ( const K & k) const
nodiscard

find() above, with the hash computed

◆ find() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::optional< V > ADS::SingleWriterFlatTable< K, V, H, Eq, C >::find ( const K & k,
std::uint64_t h ) const
nodiscard

a copy of the value of the key \( k \) of hash \( h \), or nothing; by any thread, also while the writer inserts and grows the table

In pseudocode (beyond the book), with the notation of findOrInsert() above:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{find}( k , h ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad T \gets \text{the vector in use } W\quad \triangleright \ \text{acquire} \cr 4 & \qquad i \gets \lfloor h / 2^{64 - l} \rfloor \cr 5 & \qquad \textbf{while}\ \text{the group of } i \text{ is in use} \ \textbf{and}\ T[ i ].\mathit{key} \ne 0\ \textbf{do}\quad \triangleright\ \text{acquire} \cr 6 & \qquad\quad \textbf{if}\ T[ i ].\mathit{key} = k\ \textbf{then} \ \textbf{return}\ T[ i ].\mathit{value} \cr 7 & \qquad\quad i \gets ( i + 1 ) \bmod 2^l \cr 8 & \qquad \textbf{if}\ T \text{ is still the vector in use} \ \textbf{then return nil} \end{array}} \]

Precondition
hash( k ) is \( h \) (audit)

◆ findKey() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::optional< K > ADS::SingleWriterFlatTable< K, V, H, Eq, C >::findKey ( const K & k) const
nodiscard

findKey() above, with the hash computed

◆ findKey() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::optional< K > ADS::SingleWriterFlatTable< K, V, H, Eq, C >::findKey ( const K & k,
std::uint64_t h ) const
nodiscard

a copy of the key held equal to \( k \) by Eq, of hash \( h \), or nothing, by the lookup of find(); by any thread

Precondition
hash( k ) is \( h \) (audit)

◆ findOrInsert() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::pair< V, bool > ADS::SingleWriterFlatTable< K, V, H, Eq, C >::findOrInsert ( const K & k,
const V & v )

findOrInsert() above, with the hash computed

◆ findOrInsert() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::pair< V, bool > ADS::SingleWriterFlatTable< K, V, H, Eq, C >::findOrInsert ( const K & k,
std::uint64_t h,
const V & v )

the value held for the key \( k \) of hash \( h \), which is \( v \) if absent and then inserted, and true if it was inserted; by the writer only

In pseudocode (beyond the book), \( W \) the vector in use, of \( 2^l \) places, and \( n \) the pairs:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{findOrInsert}( k , h , v ) \cr 2 & \quad \textbf{if}\ 2 ( n + 1 ) > 2^l\ \textbf{then} \ \mathrm{grow}() \cr 3 & \quad i \gets \lfloor h / 2^{64 - l} \rfloor \cr 4 & \quad \textbf{loop} \cr 5 & \qquad \textbf{if}\ \text{the group of } i \text{ is out of use} \ \textbf{then}\ \text{write 0 to its words; store the epoch as} \text{ its stamp}\quad \triangleright\ \text{release} \cr 6 & \qquad \textbf{if}\ W[ i ].\mathit{key} = 0\ \textbf{then} \cr 7 & \qquad\quad W[ i ].\mathit{value} \gets v;\ W[ i ].\mathit{key} \gets k\quad \triangleright\ \text{the key with release} \cr 8 & \qquad\quad n \gets n + 1;\ \textbf{return}\ ( v , \textbf{true} ) \cr 9 & \qquad \textbf{if}\ W[ i ].\mathit{key} = k\ \textbf{then} \ \textbf{return}\ ( W[ i ].\mathit{value} , \textbf{false} ) \cr 10 & \qquad i \gets ( i + 1 ) \bmod 2^l \cr 11 & \textbf{procedure}\ \mathrm{grow}() \cr 12 & \quad W' \gets \text{a vector of } 2^{l + 1} \text{ free places} \cr 13 & \quad \textbf{foreach}\ \text{pair } ( k , v ) \text{ of } W \ \textbf{do}\ \text{put it at the first free place of } k \text{ in } W' \cr 14 & \quad \text{keep } W \text{ for the readers};\ W \gets W'\quad \triangleright\ \text{release} \end{array}} \]

Precondition
the caller is the writer; hash( k ) is \( h \) (audit)

◆ findOrInsertKey()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::pair< K, bool > ADS::SingleWriterFlatTable< K, V, H, Eq, C >::findOrInsertKey ( const K & k,
std::uint64_t h )

the key held equal to \( k \) by Eq, of hash \( h \), which is \( k \) itself if absent and then inserted; true if it was inserted, for a set (an empty value); by the writer only

Precondition
the caller is the writer; hash( k ) is \( h \) (audit)

◆ forEach()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
template<class F>
void ADS::SingleWriterFlatTable< K, V, H, Eq, C >::forEach ( F && f) const

f( k , v ) on each pair, in no order

Precondition
no other thread accesses the table

◆ hash()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::uint64_t ADS::SingleWriterFlatTable< K, V, H, Eq, C >::hash ( const K & k) const
nodiscard

the hash of 64 bits of \( k \), that the operations take

◆ insert()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::SingleWriterFlatTable< K, V, H, Eq, C >::insert ( const K & k,
const V & v )

true if the key \( k \) was absent and \( ( k , v ) \) inserted; by the writer only

Precondition
the caller is the writer

◆ operator=()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
SingleWriterFlatTable & ADS::SingleWriterFlatTable< K, V, H, Eq, C >::operator= ( const SingleWriterFlatTable< K, V, H, Eq, C > & )
delete

◆ rehashes()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::size_t ADS::SingleWriterFlatTable< K, V, H, Eq, C >::rehashes ( void ) const
inlinenodiscard

the growths made so far

◆ reserve()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
void ADS::SingleWriterFlatTable< K, V, H, Eq, C >::reserve ( std::size_t n)

the table grown so that it holds \( n \) pairs with no growth; by the writer only

Precondition
the caller is the writer

◆ size()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::size_t ADS::SingleWriterFlatTable< K, V, H, Eq, C >::size ( void ) const
inlinenodiscard

the pairs: exact for the writer, and for another thread a number of pairs that the table held at some time since its last clear()

◆ update()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
template<class F>
requires ( valueBytes() > 0 )
bool ADS::SingleWriterFlatTable< K, V, H, Eq, C >::update ( const K & k,
F && f )

the value of the key \( k \) replaced by f( value ), true if the key is there, by a store with release that a reader sees whole; by the writer only

Precondition
the caller is the writer

Member data documentation

◆ epochs

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::SingleWriterFlatTable< K, V, H, Eq, C >::epochs = detail::ClearStamps< C >::epochs
staticconstexpr

whether clear() takes \( O( 1 ) \) by epochs


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