ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BucketCuckooTable.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File BucketCuckooTable.h ------------------------*/
3/*--------------------------------------------------------------------------*/
100/*--------------------------------------------------------------------------*/
101/*----------------------------- DEFINITIONS --------------------------------*/
102/*--------------------------------------------------------------------------*/
103
104#ifndef __ADS_BucketCuckooTable
105 #define __ADS_BucketCuckooTable
106/* self-identification: #endif constant */
107
108/*--------------------------------------------------------------------------*/
109/*------------------------------ INCLUDES ----------------------------------*/
110/*--------------------------------------------------------------------------*/
111
112#include "ADS/Contract.h"
113#include "ADS/Iterators.h"
116#include "ADS/memory/Prefetch.h"
117
118#include <bit>
119#include <cstddef>
120#include <cstdint>
121#include <functional>
122#include <iterator>
123#include <optional>
124#include <utility>
125#include <vector>
126
127/*--------------------------------------------------------------------------*/
128/*--------------------------- NAMESPACE ------------------------------------*/
129/*--------------------------------------------------------------------------*/
130
131namespace ADS
132{
133
134/*--------------------------------------------------------------------------*/
135/*------------------------ CLASS BucketCuckooTable -------------------------*/
136/*--------------------------------------------------------------------------*/
137
139template< class K , class V , class H = DefaultHash< K > ,
140 class Eq = std::equal_to< K > ,
141 class A = std::allocator< std::pair< const K , V > > >
143{
144
145/*--------------------------------------------------------------------------*/
146 public:
147 using Key = K;
148 using Value = V;
149
152 explicit BucketCuckooTable( std::size_t n = 0 , H h = H() , Eq eq = Eq() ,
153 const A & alloc = A() );
154
157 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
158
160 [[nodiscard]] const V * find( const K & k ) const;
161
164 [[nodiscard]] V * find( const K & k );
165
198 bool insert( const K & k , const V & v );
199
203 bool remove( const K & k );
204
206 [[nodiscard]] bool contains( const K & k ) const;
207
209 [[nodiscard]] std::size_t size( void ) const;
210
212 [[nodiscard]] bool empty( void ) const;
213
215 [[nodiscard]] std::size_t capacity( void ) const;
216
218 [[nodiscard]] double load( void ) const;
219
221 [[nodiscard]] std::size_t rehashes( void ) const;
222
224 [[nodiscard]] std::size_t moves( void ) const;
225
227 [[nodiscard]] std::size_t stashed( void ) const;
228
229/*--------------------------------------------------------------------------*/
230 private:
231 struct Slot
232 {
233 K key{};
234 V value{};
235 };
236
237 static constexpr int kDepth = 5;
238 static constexpr std::size_t kNodes = 1024;
239 static constexpr int kGrowths = 2;
240
241 public:
242/*------------------------- THE STANDARD INTERFACE -------------------------*/
243
244 // a constant view of the pairs ( key , value ), in the order of the
245 // places, bidirectional
246
248 struct Places
249 {
251 static std::size_t size( const auto & o ) {
252 return( o.f_S.size() + o.f_X.size() );
253 }
254
256 static bool occupied( const auto & o , std::size_t i ) {
257 if( i >= o.f_S.size() )
258 return( o.f_XT[ i - o.f_S.size() ] != 0 );
259 return( ( o.f_T[ i >> 2 ] >> ( 8 * ( i & 3 ) ) ) & 0xFF );
260 }
261
262 static auto read( const auto & o , std::size_t i ) {
263 const Slot & s = o.at( i );
264 return( PairRef< const K & , const V & >( s.key , s.value ) );
265 }
266 };
267
269 using value_type = std::pair< K , V >;
271 using key_type = K;
273 using mapped_type = V;
275 using size_type = std::size_t;
277 using difference_type = std::ptrdiff_t;
287 using allocator_type = A;
289 [[nodiscard]] allocator_type get_allocator( void ) const {
290 return( allocator_type( f_S.get_allocator() ) );
291 }
292
294 [[nodiscard]] const_iterator begin( void ) const {
295 return( const_iterator( this , 0 ) );
296 }
297
298 [[nodiscard]] const_iterator end( void ) const {
299 return( const_iterator( this , f_S.size() + f_X.size() ) );
300 }
301
302 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
304 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
305
306/*--------------------------------------------------------------------------*/
307 private:
309 [[nodiscard]] std::uint64_t hash( const K & k ) const;
310
312 [[nodiscard]] static std::uint32_t tag( std::uint64_t h );
313
315 [[nodiscard]] std::size_t first( std::uint64_t h ) const;
316
319 [[nodiscard]] std::size_t other( std::size_t b , std::uint32_t t ) const;
320
323 [[nodiscard]] std::size_t locate( const K & k , std::uint64_t h ) const;
324
326 [[nodiscard]] const Slot & at( std::size_t i ) const;
327
329 [[nodiscard]] Slot & at( std::size_t i );
330
332 template< class KK , class VV >
333 void stash( KK && k , VV && v );
334
336 [[nodiscard]] int freePlace( std::size_t b ) const;
337
340 template< class KK , class VV >
341 void put( std::size_t b , int i , std::uint32_t t , KK && k , VV && v );
342
345 std::size_t makeRoom( std::uint64_t h );
346
348 void grow( void );
349
351 template< class KK , class VV >
352 bool place( std::uint64_t h , KK && k , VV && v );
353
356 [[nodiscard]] bool placed( const K & k ) const;
357
359 using Tags = detail::AllocVector< A , std::uint32_t >;
361 using Slots = detail::AllocVector< A , Slot >;
362
363 Tags f_T;
364 Slots f_S;
365 Slots f_X;
366 Tags f_XT;
367 std::size_t f_xn = 0;
368 int f_lg = 1;
369 std::size_t f_n = 0;
370 std::size_t f_rehashes = 0;
371 std::size_t f_moves = 0;
372 H f_h;
373 Eq f_eq;
374
375 }; // end( class BucketCuckooTable )
376
377/*--------------------------------------------------------------------------*/
378
379} // end( namespace ADS )
380
381/*--------------------------------------------------------------------------*/
382/*-------------------------- IMPLEMENTATION --------------------------------*/
383/*--------------------------------------------------------------------------*/
384
385#include "ADS/dictionary/impl/BucketCuckooTable.tpp"
386
387/*--------------------------------------------------------------------------*/
388
389#endif /* BucketCuckooTable.h included */
390
391/*--------------------------------------------------------------------------*/
392/*---------------------- End File BucketCuckooTable.h ----------------------*/
393/*--------------------------------------------------------------------------*/
The rules of the allocator-aware containers of the standard ([container.reqmts], [allocator....
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 iterators that the containers of the library have in common, beyond the book: they give a contain...
The prefetch of a line of the cache, beyond the book: ADS::prefetch( p ) asks the processor to bring ...
std::optional< V > lookup(const K &k) const
the value of the key , or nothing, from the 8 places of its two buckets, compared first by their tags
allocator_type get_allocator(void) const
the allocator of the places
Definition BucketCuckooTable.h:289
PairRef< const K &, const V & > const_reference
a constant reference to an item
Definition BucketCuckooTable.h:281
PairRef< const K &, const V & > reference
a reference to an item
Definition BucketCuckooTable.h:279
std::size_t size_type
the type of the sizes
Definition BucketCuckooTable.h:275
std::size_t size(void) const
the number of keys
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition BucketCuckooTable.h:277
const_iterator end(void) const
the iterator past the last item
Definition BucketCuckooTable.h:298
bool insert(const K &k, const V &v)
associates the value to the key ; true if was not there
std::size_t rehashes(void) const
the doublings of the table
const_iterator cbegin(void) const
the constant iterator on the first item
Definition BucketCuckooTable.h:302
std::size_t capacity(void) const
the places, 4 per bucket
BucketCuckooTable(std::size_t n=0, H h=H(), Eq eq=Eq(), const A &alloc=A())
the empty table, with room for pairs before it grows, its places from alloc
SlotIterator< BucketCuckooTable, Places > const_iterator
the iterator on the items, constant
Definition BucketCuckooTable.h:283
double load(void) const
the load, size() / capacity()
V mapped_type
the type of the values
Definition BucketCuckooTable.h:273
K Key
the type of the keys
Definition BucketCuckooTable.h:147
const V * find(const K &k) const
the value of the key , nil if it is not there
K key_type
the type of the keys
Definition BucketCuckooTable.h:271
std::size_t stashed(void) const
the keys in the stash, 0 but for degenerate hashes
const_iterator begin(void) const
the iterator on the first item
Definition BucketCuckooTable.h:294
bool remove(const K &k)
removes the key , if it is there, clearing the tag of its place; true if it was
V Value
the type of the values
Definition BucketCuckooTable.h:148
A allocator_type
the type of the allocator
Definition BucketCuckooTable.h:287
const_iterator iterator
the iterator on the items
Definition BucketCuckooTable.h:285
V * find(const K &k)
the value of the key , to change in place, nil if it is not there
const_iterator cend(void) const
the constant iterator past the last item
Definition BucketCuckooTable.h:304
std::pair< K, V > value_type
the type of the items, for the standard library
Definition BucketCuckooTable.h:269
bool empty(void) const
true if there are no keys
std::size_t moves(void) const
the keys moved by the insertions
bool contains(const K &k) const
true if the key is there
a bidirectional iterator on the occupied places of the table O, by the policy P with the static funct...
Definition Iterators.h:691
std::conditional_t< detail::plainPair< A, B, VA, VB >, std::pair< A, B >, detail::PairProxy< A, B, VA, VB > > PairRef
the pair that an iterator reads by value, in general two references into the container,...
Definition Iterators.h:648
Definition ADT.h:96
the places, for SlotIterator
Definition BucketCuckooTable.h:249
static auto read(const auto &o, std::size_t i)
the pair of the place
Definition BucketCuckooTable.h:262
static bool occupied(const auto &o, std::size_t i)
whether the place holds a key, i.e., its tag is not 0, or its place of the stash is not a hole
Definition BucketCuckooTable.h:256
static std::size_t size(const auto &o)
the number of places, the stash included
Definition BucketCuckooTable.h:251