ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CuckooHashing.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File CuckooHashing.h --------------------------*/
3/*--------------------------------------------------------------------------*/
90/*--------------------------------------------------------------------------*/
91/*----------------------------- DEFINITIONS --------------------------------*/
92/*--------------------------------------------------------------------------*/
93
94#ifndef __ADS_CuckooHashing
95 #define __ADS_CuckooHashing
96/* self-identification: #endif constant */
97
98/*--------------------------------------------------------------------------*/
99/*------------------------------ INCLUDES ----------------------------------*/
100/*--------------------------------------------------------------------------*/
101
102#include "ADS/Contract.h"
103#include "ADS/Iterators.h"
106
107#include <algorithm>
108#include <bit>
109#include <cmath>
110#include <cstddef>
111#include <cstdint>
112#include <functional>
113#include <optional>
114#include <utility>
115#include <vector>
116
117/*--------------------------------------------------------------------------*/
118/*--------------------------- NAMESPACE ------------------------------------*/
119/*--------------------------------------------------------------------------*/
120
121namespace ADS
122{
123
124/*--------------------------------------------------------------------------*/
125/*------------------------- CLASS CuckooHashTable --------------------------*/
126/*--------------------------------------------------------------------------*/
127
129template< class K , class V , class H = std::hash< K > ,
130 class A = std::allocator< std::pair< const K , V > > >
132{
133
134/*--------------------------------------------------------------------------*/
135 public:
136 using Key = K;
137 using Value = V;
138
142 explicit CuckooHashTable( std::size_t m = 16 , double maxLoad = 0.4 ,
143 std::size_t s = 0 , std::uint64_t seed = 1 ,
144 H h = H() , const A & alloc = A() );
145
149 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
150
190 void insert( const K & k , const V & v );
191
195 void remove( const K & k );
196
198 [[nodiscard]] bool contains( const K & k ) const;
199
201 [[nodiscard]] std::size_t size( void ) const;
202
204 [[nodiscard]] std::size_t capacity( void ) const;
205
207 [[nodiscard]] std::size_t rehashes( void ) const;
208
210 [[nodiscard]] std::size_t evictions( void ) const;
211
213 [[nodiscard]] std::size_t stashed( void ) const;
214
215/*--------------------------------------------------------------------------*/
216 private:
217 struct Cell
218 {
219 bool full = false;
220 K key{};
221 V value{};
222 };
223
224 public:
225/*------------------------- THE STANDARD INTERFACE -------------------------*/
226
227 // a constant view of the pairs ( key , value ), in the order of the
228 // places and then of the stash, bidirectional; an insertion or a removal
229 // invalidates it
230
232 struct Places
233 {
235 static std::size_t size( const auto & o ) { return( o.f_T.size() ); }
237 static bool occupied( const auto & o , std::size_t i ) {
238 return( o.f_T[ i ].full );
239 }
240
241 static auto read( const auto & o , std::size_t i ) {
242 return(
243 PairRef< const K & , const V & >( o.f_T[ i ].key , o.f_T[ i ].value ) );
244 }
245 };
246
248 using value_type = std::pair< K , V >;
250 using key_type = K;
252 using mapped_type = V;
254 using size_type = std::size_t;
256 using difference_type = std::ptrdiff_t;
266 using allocator_type = A;
268 [[nodiscard]] allocator_type get_allocator( void ) const {
269 return( allocator_type( f_T.get_allocator() ) );
270 }
271
273 [[nodiscard]] const_iterator begin( void ) const {
274 return( const_iterator( this , 0 ) );
275 }
276
277 [[nodiscard]] const_iterator end( void ) const {
278 return( const_iterator( this , f_T.size() ) );
279 }
280
281 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
283 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
284
285 private:
287 [[nodiscard]] std::size_t place( const K & k , int i ) const;
288
290 [[nodiscard]] std::optional< std::size_t > find( const K & k ) const;
291
294 bool walk( Cell & c );
295
297 bool settle( Cell & c );
298
301 void rebuild( std::size_t m , std::optional< Cell > extra );
302
304 void draw( void );
305
308 [[nodiscard]] bool placed( const K & k ) const;
309
310 static constexpr int kReseeds = 8;
311 detail::AllocVector< A , Cell > f_T;
312 std::size_t f_m;
313 std::size_t f_m0;
314 std::size_t f_s;
315 double f_maxLoad;
316 std::size_t f_n = 0;
317 std::size_t f_rehashes = 0;
318 std::size_t f_evictions = 0;
319 SplitMix64 f_seeds;
320 TabulationHash f_g1 , f_g2;
321 H f_h;
322
323 }; // end( class CuckooHashTable )
324
325/*--------------------------------------------------------------------------*/
326
327} // end( namespace ADS )
328
329/*--------------------------------------------------------------------------*/
330/*-------------------------- IMPLEMENTATION --------------------------------*/
331/*--------------------------------------------------------------------------*/
332
333#include "ADS/dictionary/impl/CuckooHashing.tpp"
334
335/*--------------------------------------------------------------------------*/
336
337#endif /* CuckooHashing.h included */
338
339/*--------------------------------------------------------------------------*/
340/*------------------------ End File CuckooHashing.h ------------------------*/
341/*--------------------------------------------------------------------------*/
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 iterators that the containers of the library have in common, beyond the book: they give a contain...
Hashing with random tables, beyond the book:
std::size_t size(void) const
the number of keys
K Key
the type of the keys
Definition CuckooHashing.h:136
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition CuckooHashing.h:256
allocator_type get_allocator(void) const
the allocator of the places
Definition CuckooHashing.h:268
CuckooHashTable(std::size_t m=16, double maxLoad=0.4, std::size_t s=0, std::uint64_t seed=1, H h=H(), const A &alloc=A())
the empty table of places and a stash of places, from alloc
A allocator_type
the type of the allocator
Definition CuckooHashing.h:266
V Value
the type of the values
Definition CuckooHashing.h:137
std::size_t size_type
the type of the sizes
Definition CuckooHashing.h:254
K key_type
the type of the keys
Definition CuckooHashing.h:250
bool contains(const K &k) const
true if the key is there
V mapped_type
the type of the values
Definition CuckooHashing.h:252
const_iterator iterator
the iterator on the items
Definition CuckooHashing.h:264
std::size_t stashed(void) const
the keys in the stash
PairRef< const K &, const V & > reference
a reference to an item
Definition CuckooHashing.h:258
PairRef< const K &, const V & > const_reference
a constant reference to an item
Definition CuckooHashing.h:260
void insert(const K &k, const V &v)
associates the value to the key , in amortized expected time
const_iterator cbegin(void) const
the constant iterator on the first item
Definition CuckooHashing.h:281
std::optional< V > lookup(const K &k) const
the value of the key , or nothing, from its two places and and the stash, in the worst case
const_iterator cend(void) const
the constant iterator past the last item
Definition CuckooHashing.h:283
std::size_t capacity(void) const
the number of places, the stash excluded
SlotIterator< CuckooHashTable, Places > const_iterator
the iterator on the items, constant
Definition CuckooHashing.h:262
const_iterator begin(void) const
the iterator on the first item
Definition CuckooHashing.h:273
std::size_t rehashes(void) const
the rebuilds with new functions after a failed insertion
std::size_t evictions(void) const
the evictions made by the insertions
std::pair< K, V > value_type
the type of the items, for the standard library
Definition CuckooHashing.h:248
void remove(const K &k)
removes the key , if it is there, emptying its place; the table is halved below a load of
const_iterator end(void) const
the iterator past the last item
Definition CuckooHashing.h:277
a bidirectional iterator on the occupied places of the table O, by the policy P with the static funct...
Definition Iterators.h:691
the generator SplitMix64, a UniformRandomBitGenerator
Definition RandomHashing.h:122
the simple tabulation hashing of the keys of 64 bits
Definition RandomHashing.h:220
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 and the stash, for SlotIterator
Definition CuckooHashing.h:233
static bool occupied(const auto &o, std::size_t i)
whether the place holds a key
Definition CuckooHashing.h:237
static auto read(const auto &o, std::size_t i)
the pair of the place
Definition CuckooHashing.h:241
static std::size_t size(const auto &o)
the number of places, the stash included
Definition CuckooHashing.h:235