ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
FastHash.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File FastHash.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
83/*--------------------------------------------------------------------------*/
84/*----------------------------- DEFINITIONS --------------------------------*/
85/*--------------------------------------------------------------------------*/
86
87#ifndef __ADS_FastHash
88 #define __ADS_FastHash
89/* self-identification: #endif constant */
90
91/*--------------------------------------------------------------------------*/
92/*------------------------------ INCLUDES ----------------------------------*/
93/*--------------------------------------------------------------------------*/
94
95#include "ADS/Abi.h"
96#include "ADS/Contract.h"
97#include "ADS/Portable.h"
98#include "ADS/Wide.h"
99#include "ADS/Simd.h"
100#include "ADS/memory/Prefetch.h"
101
102#include <array>
103#include <bit>
104#include <concepts>
105#include <cstddef>
106#include <cstdint>
107#include <cstring>
108#include <functional>
109#include <string>
110#include <string_view>
111#include <type_traits>
112#include <utility>
113
114/*--------------------------------------------------------------------------*/
115/*--------------------------- NAMESPACE ------------------------------------*/
116/*--------------------------------------------------------------------------*/
117
118namespace ADS
119{
120
122
123/*--------------------------------------------------------------------------*/
124/*--------------------------- THE HASH FUNCTIONS ---------------------------*/
125/*--------------------------------------------------------------------------*/
126
129{
130 std::uint64_t lo ,
132 };
133
135[[nodiscard]] [[gnu::always_inline]] constexpr Product128
136product128( std::uint64_t a , std::uint64_t b );
137
141[[nodiscard]] ADS_ALWAYS_INLINE constexpr std::uint64_t
142foldedProduct( std::uint64_t a , std::uint64_t b );
143
146[[nodiscard]] ADS_ALWAYS_INLINE constexpr std::uint64_t
147hashWord( std::uint64_t x , std::uint64_t seed = 0 );
148
179[[nodiscard]] ADS_ALWAYS_INLINE std::uint64_t
180hashBytes( const void * p , std::size_t n , std::uint64_t seed = 0 );
181
182/*--------------------------------------------------------------------------*/
183/*---------------------------- CLASS FastHash ------------------------------*/
184/*--------------------------------------------------------------------------*/
185
189template< class T >
190struct FastHash;
191
193// the hash of an integer, an enumeration, a pointer or a floating point
194// number, +0 and -0 the same; out of the documentation, which describes it
195// with the primary template
196template< class T >
197 requires( std::is_integral_v< T > || std::is_enum_v< T > ||
198 std::is_pointer_v< T > ||
199 ( std::is_floating_point_v< T > && ( sizeof( T ) <= 8 ) ) )
200struct FastHash< T >
201{
202 using is_avalanching = void;
203
204 std::uint64_t seed = 0;
205
208 template< class A >
209 void persist( A & a ) {
210 a( seed );
211 }
212
215 [[nodiscard]] std::uint64_t hash64( T x ) const noexcept {
216 if constexpr( std::is_pointer_v< T > )
217 return( hashWord( std::uint64_t( reinterpret_cast< std::uintptr_t >( x ) ) ,
218 seed ) );
219 else if constexpr( std::is_enum_v< T > )
220 return(
221 hashWord( std::uint64_t( std::underlying_type_t< T >( x ) ) , seed ) );
222 else if constexpr( std::is_floating_point_v< T > ) {
223 if( x == T( 0 ) )
224 x = T( 0 ); // -0 == +0
225 std::uint64_t w = 0;
226 std::memcpy( &w , &x , sizeof( T ) );
227 return( hashWord( w , seed ) );
228 }
229 else
230 return( hashWord( std::uint64_t( x ) , seed ) );
231 }
232
235 [[nodiscard]] std::size_t operator()( T x ) const noexcept {
236 return( std::size_t( hash64( x ) ) );
237 }
238 };
239
241
244template<>
245struct FastHash< std::string_view >
246{
247 using is_avalanching = void;
248 using is_transparent = void;
249 std::uint64_t seed = 0;
250
253 template< class A >
254 void persist( A & a ) {
255 a( seed );
256 }
257
259 [[nodiscard]] std::uint64_t hash64( std::string_view s ) const noexcept {
260 return( hashBytes( s.data() , s.size() , seed ) );
261 }
262
264 [[nodiscard]] std::size_t operator()( std::string_view s ) const noexcept {
265 return( std::size_t( hash64( s ) ) );
266 }
267 };
268
270template<>
271struct FastHash< std::string > : FastHash< std::string_view >
272{ };
273
275template<>
277{ };
278
280
281namespace detail
282{
284
286template< class K >
287struct DefaultHashOf
288{
289 using type = std::hash< K >;
290 };
291
292template< class K >
293 requires requires { sizeof( FastHash< K > ); }
294struct DefaultHashOf< K >
295{
296 using type = FastHash< K >;
297 };
298
302template< class H , class K >
303inline constexpr bool kHash64 = requires( const H & h , const K & k )
304{
305 { h.hash64( k ) } -> std::same_as< std::uint64_t >;
306 };
307
310template< class H , class K >
311[[nodiscard]] ADS_ALWAYS_INLINE constexpr std::uint64_t hash64( const H & h ,
312 const K & k )
313{
314 if constexpr( kHash64< H , K > )
315 return( h.hash64( k ) );
316 else
317 return( std::uint64_t( h( k ) ) );
318 }
319
324template< class H , class K >
325inline constexpr bool kAvalanching64 =
326 requires { typename H::is_avalanching; } &&
327 ( kHash64< H , K > || ( sizeof( decltype( std::declval< const H & >()(
328 std::declval< const K & >() ) ) ) >= 8 ) );
329
332template< class K >
333inline constexpr bool kIndirectKey = requires( const K & k )
334{
335 { k.data() } -> std::convertible_to< const void * >;
336 k.size();
337 };
338
341template< class K >
342inline void prefetchKey( const K & k )
343{
344 if constexpr( kIndirectKey< K > )
345 ADS::detail::prefetchLine( static_cast< const void * >( k.data() ) );
346 else
347 ( void )k;
348 }
349
351} // end( namespace detail )
352
354
358template< class K >
359using DefaultHash = typename detail::DefaultHashOf< K >::type;
360
361/*--------------------------------------------------------------------------*/
362
364
365} // end( namespace ADS )
366
367/*--------------------------------------------------------------------------*/
368/*-------------------------- IMPLEMENTATION --------------------------------*/
369/*--------------------------------------------------------------------------*/
370
371#include "ADS/dictionary/impl/FastHash.tpp"
372
373/*--------------------------------------------------------------------------*/
374
375#endif /* FastHash.h included */
376
377/*--------------------------------------------------------------------------*/
378/*-------------------------- End File FastHash.h ---------------------------*/
379/*--------------------------------------------------------------------------*/
The name of the configuration of the layouts, an inline namespace: the headers whose classes change t...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
The prefetch of a line of the cache, beyond the book: ADS::prefetch( p ) asks the processor to bring ...
The vector kernels of ADS on Google Highway, with ADS_WITH_HIGHWAY: the loops over arrays that a stru...
The arithmetic of two words that C++20 does not spell, in one form for every compiler: the product of...
#define ADS_ABI_BEGIN
opens the inline namespace of the configuration
Definition Abi.h:106
#define ADS_ABI_END
closes the inline namespace of the configuration
Definition Abi.h:111
#define ADS_ALWAYS_INLINE
a function inlined even where the compiler would not
Definition Portable.h:90
typename detail::DefaultHashOf< K >::type DefaultHash
the hash of the engineered tables and filters of ADS: FastHash< K > for the integers,...
Definition FastHash.h:359
constexpr Product128 product128(std::uint64_t a, std::uint64_t b)
the product on 128 bits
constexpr std::uint64_t hashWord(std::uint64_t x, std::uint64_t seed=0)
the hash of the word under the seed, the folded product of and of the seed XOR a constant
std::uint64_t hashBytes(const void *p, std::size_t n, std::uint64_t seed=0)
the hash of the bytes from under the seed
constexpr std::uint64_t foldedProduct(std::uint64_t a, std::uint64_t b)
the folded product: the low half of on 128 bits XOR its high half,
Definition ADT.h:96
the hash of a string, by its bytes; any std::string_view, std::string and C string with the same char...
Definition FastHash.h:246
void is_avalanching
the hash needs no further mixing
Definition FastHash.h:247
void persist(A &a)
the seed written to, or read from, the archive [see Persistence.h]
Definition FastHash.h:254
std::size_t operator()(std::string_view s) const noexcept
the low bits of hash64() that a std::size_t holds
Definition FastHash.h:264
std::uint64_t hash64(std::string_view s) const noexcept
hashBytes() of the characters of , on 64 bits
Definition FastHash.h:259
std::uint64_t seed
the seed of hashBytes()
Definition FastHash.h:249
void is_transparent
for the lookups by any string
Definition FastHash.h:248
the hash of the values of T, for the integers, the enumerations, the pointers, the floating point num...
Definition FastHash.h:190
the product on 128 bits, its low and its high half
Definition FastHash.h:129
std::uint64_t lo
the low half,
Definition FastHash.h:130
std::uint64_t hi
the high half,
Definition FastHash.h:131