ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RandomHashing.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File RandomHashing.h --------------------------*/
3/*--------------------------------------------------------------------------*/
52/*--------------------------------------------------------------------------*/
53/*----------------------------- DEFINITIONS --------------------------------*/
54/*--------------------------------------------------------------------------*/
55
56#ifndef __ADS_RandomHashing
57 #define __ADS_RandomHashing
58/* self-identification: #endif constant */
59
60/*--------------------------------------------------------------------------*/
61/*------------------------------ INCLUDES ----------------------------------*/
62/*--------------------------------------------------------------------------*/
63
64#include "ADS/Contract.h"
65
66#include <array>
67#include <atomic>
68#include <bit>
69#include <cstddef>
70#include <cstdint>
71#include <limits>
72#include <ranges>
73#include <vector>
74
75/*--------------------------------------------------------------------------*/
76/*--------------------------- NAMESPACE ------------------------------------*/
77/*--------------------------------------------------------------------------*/
78
79namespace ADS
80{
81
99[[nodiscard]] constexpr std::uint64_t splitmix64( std::uint64_t x );
100
113[[nodiscard]] constexpr std::uint64_t fibonacciPlace( std::uint64_t h ,
114 int b );
115
116/*--------------------------------------------------------------------------*/
117/*-------------------------- CLASS SplitMix64 ------------------------------*/
118/*--------------------------------------------------------------------------*/
119
122{
123 public:
124 using result_type = std::uint64_t;
125
127 explicit constexpr SplitMix64( std::uint64_t seed = 0 ) : f_s( seed ) { }
128
131 constexpr std::uint64_t operator()( void ) {
132 const std::uint64_t r = splitmix64( f_s );
133 f_s += 0x9e3779b97f4a7c15ULL;
134 return( r );
135 }
136
138 static constexpr std::uint64_t min( void ) { return( 0 ); }
139
141 static constexpr std::uint64_t max( void ) {
142 return( std::numeric_limits< std::uint64_t >::max() );
143 }
144
145 private:
146 std::uint64_t f_s;
147 };
148
149namespace detail
150{
151
158inline std::uint64_t instanceSeed( void )
159{
160 static std::atomic< std::uint64_t > c{ 0 };
161 return( splitmix64( c.fetch_add( 1 , std::memory_order_relaxed ) ) );
162 }
163
164} // end( namespace detail )
165
166/*--------------------------------------------------------------------------*/
167/*-------------------------- CLASS ZobristTable ----------------------------*/
168/*--------------------------------------------------------------------------*/
169
172{
173 public:
176 ZobristTable( std::size_t n , std::size_t m , std::uint64_t seed = 1 );
177
181 [[nodiscard]] std::uint64_t operator()( std::size_t f , std::size_t v ) const;
182
198 template< std::ranges::random_access_range S >
199 requires std::ranges::sized_range< S >
200 [[nodiscard]] std::uint64_t hash( const S & s ) const;
201
206 [[nodiscard]] std::uint64_t update( std::uint64_t h , std::size_t f ,
207 std::size_t a , std::size_t b ) const;
208
209 private:
210 std::size_t f_n , f_m;
211 std::vector< std::uint64_t > f_R;
212 };
213
214/*--------------------------------------------------------------------------*/
215/*------------------------- CLASS TabulationHash ---------------------------*/
216/*--------------------------------------------------------------------------*/
217
220{
221 public:
223 explicit TabulationHash( std::uint64_t seed = 1 );
224
236 [[nodiscard]] std::uint64_t operator()( std::uint64_t x ) const;
237
244 [[nodiscard]] std::size_t place( std::uint64_t x , std::size_t m ) const;
245
246 private:
247 std::array< std::array< std::uint64_t , 256 > , 8 > f_T;
248 };
249
250/*--------------------------------------------------------------------------*/
251
252} // end( namespace ADS )
253
254/*--------------------------------------------------------------------------*/
255/*-------------------------- IMPLEMENTATION --------------------------------*/
256/*--------------------------------------------------------------------------*/
257
258#include "ADS/dictionary/impl/RandomHashing.tpp"
259
260/*--------------------------------------------------------------------------*/
261
262#endif /* RandomHashing.h included */
263
264/*--------------------------------------------------------------------------*/
265/*------------------------ End File RandomHashing.h ------------------------*/
266/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
constexpr std::uint64_t operator()(void)
the next word, splitmix64() of the state, which then grows by
Definition RandomHashing.h:131
static constexpr std::uint64_t max(void)
the largest word,
Definition RandomHashing.h:141
static constexpr std::uint64_t min(void)
the least word, 0
Definition RandomHashing.h:138
constexpr SplitMix64(std::uint64_t seed=0)
the generator from the state seed
Definition RandomHashing.h:127
std::uint64_t result_type
the words generated
Definition RandomHashing.h:124
std::size_t place(std::uint64_t x, std::size_t m) const
the place of in a table of places, the most significant bits of its hash, as the LinearHashTable o...
TabulationHash(std::uint64_t seed=1)
the 8 tables of 256 random words, from the seed
std::uint64_t operator()(std::uint64_t x) const
the XOR of the words of the 8 bytes of
std::uint64_t operator()(std::size_t f, std::size_t v) const
the word of the feature with the value
std::uint64_t hash(const S &s) const
the hash of a state, the value of each feature by its place in , any range with a size and access by ...
ZobristTable(std::size_t n, std::size_t m, std::uint64_t seed=1)
the words of features with values each, from the seed
std::uint64_t update(std::uint64_t h, std::size_t f, std::size_t a, std::size_t b) const
the hash of a state after the feature changes from to , , since takes the old word away
constexpr std::uint64_t splitmix64(std::uint64_t x)
the mix of SplitMix64: a bijection of the words of 64 bits
constexpr std::uint64_t fibonacciPlace(std::uint64_t h, int b)
the place of the hash in a table of places, its top bits after the product by
Definition ADT.h:96