ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SingleWriterFlatTable.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------- File SingleWriterFlatTable.h ----------------------*/
3/*--------------------------------------------------------------------------*/
74/*--------------------------------------------------------------------------*/
75/*----------------------------- DEFINITIONS --------------------------------*/
76/*--------------------------------------------------------------------------*/
77
78#ifndef __ADS_SingleWriterFlatTable
79 #define __ADS_SingleWriterFlatTable
80/* self-identification: #endif constant */
81
82/*--------------------------------------------------------------------------*/
83/*------------------------------ INCLUDES ----------------------------------*/
84/*--------------------------------------------------------------------------*/
85
86#include "ADS/Contract.h"
87#include "ADS/Portable.h"
90
91#include <atomic>
92#include <cstddef>
93#include <cstdint>
94#include <functional>
95#include <limits>
96#include <memory>
97#include <optional>
98#include <type_traits>
99#include <utility>
100#include <variant>
101#include <vector>
102
103/*--------------------------------------------------------------------------*/
104/*--------------------------- NAMESPACE ------------------------------------*/
105/*--------------------------------------------------------------------------*/
106
107namespace ADS
108{
109
110/*--------------------------------------------------------------------------*/
111/*---------------------- CLASS SingleWriterFlatTable -----------------------*/
112/*--------------------------------------------------------------------------*/
113
118template< class K , class V , class H = DefaultHash< K > ,
119 class Eq = std::equal_to< K > , class C = ZeroingClear >
121{
122
123 static_assert( FlatClear< C > ,
124 "the policy of clear() is ZeroingClear or EpochClear" );
125
127 static constexpr std::size_t valueBytes( void ) {
128 return( std::is_empty_v< V > ? 0 : sizeof( V ) );
129 }
130
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" );
138
139/*--------------------------------------------------------------------------*/
140 public:
141 using Key = K;
142 using Value = V;
143 using Clear = C;
144
146 static constexpr bool epochs = detail::ClearStamps< C >::epochs;
147
150 explicit SingleWriterFlatTable( std::size_t n = 0 , H h = H() ,
151 Eq eq = Eq() );
152
155
157 [[nodiscard]] std::uint64_t hash( const K & k ) const;
158
194 std::pair< V , bool > findOrInsert( const K & k , std::uint64_t h ,
195 const V & v );
196
198 std::pair< V , bool > findOrInsert( const K & k , const V & v );
199
204 std::pair< K , bool > findOrInsertKey( const K & k , std::uint64_t h )
205 requires( valueBytes() == 0 );
206
210 bool insert( const K & k , const V & v );
211
216 template< class F >
217 bool update( const K & k , F && f )
218 requires( valueBytes() > 0 );
219
243 [[nodiscard]] std::optional< V > find( const K & k , std::uint64_t h ) const;
244
246 [[nodiscard]] std::optional< V > find( const K & k ) const;
247
251 [[nodiscard]] std::optional< K > findKey( const K & k ,
252 std::uint64_t h ) const;
253
255 [[nodiscard]] std::optional< K > findKey( const K & k ) const;
256
259 [[nodiscard]] bool contains( const K & k , std::uint64_t h ) const;
260
262 [[nodiscard]] bool contains( const K & k ) const;
263
266 [[nodiscard]] std::size_t size( void ) const {
267 return( f_size.load( std::memory_order_relaxed ) );
268 }
269
271 [[nodiscard]] bool empty( void ) const { return( size() == 0 ); }
272
274 [[nodiscard]] std::size_t capacity( void ) const {
275 return( f_table.load( std::memory_order_acquire )->places() );
276 }
277
279 [[nodiscard]] std::size_t rehashes( void ) const {
280 return( f_rehashes.load( std::memory_order_relaxed ) );
281 }
282
286 void reserve( std::size_t n );
287
292 void clear( void );
293
295 [[nodiscard]] std::uint64_t epoch( void ) const { return( now() ); }
296
299 template< class F >
300 void forEach( F && f ) const;
301
305 [[nodiscard]] bool consistent( void ) const;
306
307/*--------------------------------------------------------------------------*/
308 private:
309 static constexpr int kLeast = 6;
310
312 static constexpr std::size_t kWords = valueBytes() == 0 ? 1 : 2;
313
314 using Stamp = typename detail::ClearStamps< C >::Stamp;
315
317 static constexpr Stamp kLast = std::numeric_limits< Stamp >::max();
318
320 struct Table
321 {
322 int lg;
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 );
328 }
329 [[nodiscard]] std::size_t groups( void ) const {
330 return( ( places() + 7 ) >> 3 );
331 }
332 };
333
334 [[nodiscard]] Stamp now( void ) const {
335 if constexpr( epochs )
336 return( f_epoch );
337 else
338 return( 1 );
339 }
340
341 template< class X >
342 [[nodiscard]] static std::uint64_t bits( const X & x );
343
344 [[nodiscard]] static K keyOf( std::uint64_t w );
345
346 [[nodiscard]] static V valueOf( std::uint64_t w );
347
348 [[nodiscard]] static bool zeroKey( const K & k );
349
350 [[nodiscard]] static std::size_t home( std::uint64_t h , const Table & T );
351
352 [[nodiscard]] bool live( const Table & T , std::size_t g ) const;
353
354 [[nodiscard]] std::uint64_t wordAt( const Table & T , std::size_t i ) const;
355
356 // the place of the key k in the vector in use, or nullptr; by any thread
357 [[nodiscard]] const std::atomic< std::uint64_t > *
358 locate( const K & k , std::uint64_t h ) const;
359
360 // the place of the key k in the vector in use for the writer, its key
361 // word 0 if it is absent, the group renewed if out of use
362 std::atomic< std::uint64_t > * place( Table & T , const K & k ,
363 std::uint64_t h );
364
365 void grow( int lg );
366
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 };
373 ADS_NO_UNIQUE_ADDRESS std::conditional_t< epochs , Stamp , std::monostate >
374 f_epoch{};
375 H f_h;
376 Eq f_eq;
377
378 }; // end( class SingleWriterFlatTable )
379
380/*--------------------------------------------------------------------------*/
381
382} // end( namespace ADS )
383
384/*--------------------------------------------------------------------------*/
385/*-------------------------- IMPLEMENTATION --------------------------------*/
386/*--------------------------------------------------------------------------*/
387
388#include "ADS/concurrent/impl/SingleWriterFlatTable.tpp"
389
390/*--------------------------------------------------------------------------*/
391
392#endif /* SingleWriterFlatTable.h included */
393
394/*--------------------------------------------------------------------------*/
395/*--------------------- End File SingleWriterFlatTable.h -------------------*/
396/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96