Program Listing for File CuckooHashing.h
↰ Return to documentation for file (ADS/dictionary/CuckooHashing.h)
/*--------------------------------------------------------------------------*/
/*-------------------------- File CuckooHashing.h --------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/
#ifndef __ADS_CuckooHashing
#define __ADS_CuckooHashing
/* self-identification: #endif constant */
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include "ADS/dictionary/RandomHashing.h"
#include <bit>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <optional>
#include <utility>
#include <vector>
/*--------------------------------------------------------------------------*/
/*--------------------------- NAMESPACE ------------------------------------*/
/*--------------------------------------------------------------------------*/
namespace ADS {
/*--------------------------------------------------------------------------*/
/*------------------------- CLASS CuckooHashTable --------------------------*/
/*--------------------------------------------------------------------------*/
template< class K , class V , class H = std::hash< K > >
class CuckooHashTable {
/*--------------------------------------------------------------------------*/
public:
using Key = K;
using Value = V;
explicit CuckooHashTable( std::size_t m = 16 , double maxLoad = 0.4 ,
std::size_t s = 0 , std::uint64_t seed = 1 ,
H h = H() );
[[nodiscard]] std::optional< V > lookup( const K & k ) const;
void insert( const K & k , const V & v );
void remove( const K & k );
[[nodiscard]] bool contains( const K & k ) const;
[[nodiscard]] std::size_t size( void ) const;
[[nodiscard]] std::size_t capacity( void ) const;
[[nodiscard]] std::size_t rehashes( void ) const;
[[nodiscard]] std::size_t evictions( void ) const;
[[nodiscard]] std::size_t stashed( void ) const;
/*--------------------------------------------------------------------------*/
private:
struct Cell {
bool full = false;
K key{};
V value{};
};
public:
/*------------------------- THE STANDARD INTERFACE -------------------------*/
// a constant view of the pairs ( key , value ), in the order of the
// places and then of the stash, bidirectional; an insertion or a removal
// invalidates it
struct Places {
static std::size_t size( const auto & o ) { return( o.f_T.size() ); }
static bool occupied( const auto & o , std::size_t i ) {
return( o.f_T[ i ].full );
}
static auto read( const auto & o , std::size_t i ) {
return( std::pair< const K & , const V & >( o.f_T[ i ].key ,
o.f_T[ i ].value ) );
}
};
using value_type = std::pair< const K & , const V & >;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = std::pair< const K & , const V & >;
using const_reference = std::pair< const K & , const V & >;
using const_iterator = SlotIterator< CuckooHashTable , Places >;
using iterator = const_iterator;
[[nodiscard]] const_iterator begin( void ) const {
return( const_iterator( this , 0 ) );
}
[[nodiscard]] const_iterator end( void ) const {
return( const_iterator( this , f_T.size() ) );
}
[[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
[[nodiscard]] const_iterator cend( void ) const { return( end() ); }
private:
[[nodiscard]] std::size_t place( const K & k , int i ) const;
[[nodiscard]] std::optional< std::size_t > find( const K & k ) const;
bool walk( Cell & c );
bool settle( Cell & c );
void rebuild( std::size_t m , std::optional< Cell > extra );
void draw( void );
[[nodiscard]] bool placed( const K & k ) const;
std::vector< Cell > f_T;
std::size_t f_m;
std::size_t f_m0;
std::size_t f_s;
double f_maxLoad;
std::size_t f_n = 0;
std::size_t f_rehashes = 0;
std::size_t f_evictions = 0;
SplitMix64 f_seeds;
TabulationHash f_g1 , f_g2;
H f_h;
}; // end( class CuckooHashTable )
/*--------------------------------------------------------------------------*/
} // end( namespace ADS )
/*--------------------------------------------------------------------------*/
/*-------------------------- IMPLEMENTATION --------------------------------*/
/*--------------------------------------------------------------------------*/
#include "ADS/dictionary/impl/CuckooHashing.tpp"
/*--------------------------------------------------------------------------*/
#endif /* CuckooHashing.h included */
/*--------------------------------------------------------------------------*/
/*------------------------ End File CuckooHashing.h ------------------------*/
/*--------------------------------------------------------------------------*/