78#ifndef __ADS_SingleWriterFlatTable
79 #define __ADS_SingleWriterFlatTable
118template<
class K ,
class V ,
class H = DefaultHash< K > ,
119 class Eq = std::equal_to< K > ,
class C = ZeroingClear >
124 "the policy of clear() is ZeroingClear or EpochClear" );
127 static constexpr std::size_t valueBytes(
void ) {
128 return( std::is_empty_v< V > ? 0 :
sizeof( V ) );
131 static_assert( std::is_trivially_copyable_v< K > &&
132 std::is_trivially_copyable_v< V > && (
sizeof( K ) <= 8 ) &&
133 ( valueBytes() <= 8 ) &&
134 ( std::has_unique_object_representations_v< K > ||
135 std::is_floating_point_v< K > ) ,
136 "the key and the value are trivially copyable words, the "
137 "key with a unique representation" );
146 static constexpr bool epochs = detail::ClearStamps< C >::epochs;
157 [[nodiscard]] std::uint64_t
hash(
const K & k )
const;
205 requires( valueBytes() == 0 );
210 bool insert(
const K & k ,
const V & v );
218 requires( valueBytes() > 0 );
243 [[nodiscard]] std::optional< V >
find(
const K & k , std::uint64_t h )
const;
246 [[nodiscard]] std::optional< V >
find(
const K & k )
const;
251 [[nodiscard]] std::optional< K >
findKey(
const K & k ,
252 std::uint64_t h )
const;
255 [[nodiscard]] std::optional< K >
findKey(
const K & k )
const;
259 [[nodiscard]]
bool contains(
const K & k , std::uint64_t h )
const;
266 [[nodiscard]] std::size_t
size(
void )
const {
267 return( f_size.load( std::memory_order_relaxed ) );
271 [[nodiscard]]
bool empty(
void )
const {
return(
size() == 0 ); }
275 return( f_table.load( std::memory_order_acquire )->places() );
280 return( f_rehashes.load( std::memory_order_relaxed ) );
295 [[nodiscard]] std::uint64_t
epoch(
void )
const {
return( now() ); }
309 static constexpr int kLeast = 6;
312 static constexpr std::size_t kWords = valueBytes() == 0 ? 1 : 2;
314 using Stamp =
typename detail::ClearStamps< C >::Stamp;
317 static constexpr Stamp kLast = std::numeric_limits< Stamp >::max();
323 std::unique_ptr< std::atomic< std::uint64_t >[] > W;
324 std::unique_ptr< std::atomic< Stamp >[] > E;
325 Table(
int l , Stamp e );
326 [[nodiscard]] std::size_t places(
void )
const {
327 return( std::size_t( 1 ) << lg );
329 [[nodiscard]] std::size_t groups(
void )
const {
330 return( ( places() + 7 ) >> 3 );
334 [[nodiscard]] Stamp now(
void )
const {
342 [[nodiscard]]
static std::uint64_t bits(
const X & x );
344 [[nodiscard]]
static K keyOf( std::uint64_t w );
346 [[nodiscard]]
static V valueOf( std::uint64_t w );
348 [[nodiscard]]
static bool zeroKey(
const K & k );
350 [[nodiscard]]
static std::size_t home( std::uint64_t h ,
const Table & T );
352 [[nodiscard]]
bool live(
const Table & T , std::size_t g )
const;
354 [[nodiscard]] std::uint64_t wordAt(
const Table & T , std::size_t i )
const;
357 [[nodiscard]]
const std::atomic< std::uint64_t > *
358 locate(
const K & k , std::uint64_t h )
const;
362 std::atomic< std::uint64_t > * place( Table & T ,
const K & k ,
367 std::atomic< Table * > f_table{
nullptr };
368 std::unique_ptr< Table > f_cur;
369 std::vector< std::unique_ptr< Table > > f_old;
370 std::atomic< std::uint64_t > f_zero[ 2 ];
371 std::atomic< std::size_t > f_size{ 0 };
372 std::atomic< std::size_t > f_rehashes{ 0 };
388#include "ADS/concurrent/impl/SingleWriterFlatTable.tpp"
A hash table that many threads read and grow at once, beyond the book: open addressing with linear pr...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The engineered hash functions of ADS, beyond the book [see HashFunctions.h for those of section 7....
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
std::optional< V > find(const K &k) const
find() above, with the hash computed
std::optional< V > find(const K &k, std::uint64_t h) const
a copy of the value of the key of hash , or nothing; by any thread, also while the writer inserts an...
C Clear
the policy of clear()
Definition SingleWriterFlatTable.h:143
bool contains(const K &k, std::uint64_t h) const
whether the key of hash is there; by any thread
std::pair< V, bool > findOrInsert(const K &k, const V &v)
findOrInsert() above, with the hash computed
bool consistent(void) const
whether each key is reached from its place with no free place in between, once, and the pairs are tho...
SingleWriterFlatTable(std::size_t n=0, H h=H(), Eq eq=Eq())
the table of the least vector that holds pairs with no growth, at least 64 places
std::size_t capacity(void) const
the places of the vector in use
Definition SingleWriterFlatTable.h:274
void forEach(F &&f) const
f( k , v ) on each pair, in no order
std::uint64_t epoch(void) const
the epoch of the policy EpochClear, 1 for ZeroingClear
Definition SingleWriterFlatTable.h:295
V Value
the type of the values
Definition SingleWriterFlatTable.h:142
std::pair< K, bool > findOrInsertKey(const K &k, std::uint64_t h)
the key held equal to by Eq, of hash , which is itself if absent and then inserted; true if it was ...
K Key
the type of the keys
Definition SingleWriterFlatTable.h:141
std::optional< K > findKey(const K &k) const
findKey() above, with the hash computed
void reserve(std::size_t n)
the table grown so that it holds pairs with no growth; by the writer only
std::optional< K > findKey(const K &k, std::uint64_t h) const
a copy of the key held equal to by Eq, of hash , or nothing, by the lookup of find(); by any thread
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...
Definition SingleWriterFlatTable.h:266
static constexpr bool epochs
whether clear() takes by epochs
Definition SingleWriterFlatTable.h:146
void clear(void)
no pair, in time linear in the places, or with EpochClear but once per calls; the vectors replaced ...
SingleWriterFlatTable(const SingleWriterFlatTable &)=delete
bool empty(void) const
whether there is no pair
Definition SingleWriterFlatTable.h:271
bool contains(const K &k) const
contains() above, with the hash computed
std::uint64_t hash(const K &k) const
the hash of 64 bits of , that the operations take
std::size_t rehashes(void) const
the growths made so far
Definition SingleWriterFlatTable.h:279
bool insert(const K &k, const V &v)
true if the key was absent and inserted; by the writer only
bool update(const K &k, F &&f)
the value of the key replaced by f( value ), true if the key is there, by a store with release that ...
std::pair< V, bool > findOrInsert(const K &k, std::uint64_t h, const V &v)
the value held for the key of hash , which is if absent and then inserted, and true if it was inser...
SingleWriterFlatTable & operator=(const SingleWriterFlatTable &)=delete
a policy of clear() of ConcurrentFlatTable
Definition ConcurrentFlatTable.h:258
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
std::vector< std::vector< int > > Table
the table of the costs
Definition ApproximateMatching.h:80