Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
hash-fct.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
42# ifndef HASH_FCT_H
43# define HASH_FCT_H
44
45# include <cstddef>
46# include <cstring>
47# include <string>
48# include <concepts>
49# include <type_traits>
50# include <cstdint>
51
69namespace Aleph
70{
71 extern const unsigned Default_Hash_Seed;
72
80 void init_jsw() noexcept;
81
91
98
112 inline size_t add_hash(const void * key, const size_t len) noexcept
113 {
114 const auto *p = static_cast<const unsigned char *>(key);
115 size_t h = 0;
116
117 for (size_t i = 0; i < len; i++)
118 h += p[i];
119
120 return h;
121 }
122
133 inline size_t xor_hash(const void * key, const size_t len) noexcept
134 {
135 const auto *p = static_cast<const unsigned char *>(key);
136 size_t h = 0;
137
138 for (size_t i = 0; i < len; i++)
139 h ^= p[i];
140
141 return h;
142 }
143
154 inline size_t rot_hash(const void * key, const size_t len) noexcept
155 {
156 const auto *p = static_cast<const unsigned char *>(key);
157 size_t h = 0;
158
159 for (size_t i = 0; i < len; i++)
160 h = (h << 4) ^ (h >> 28) ^ p[i];
161
162 return h;
163 }
164
174 inline size_t djb_hash(const void * key, const size_t len) noexcept
175 {
176 const auto *p = static_cast<const unsigned char *>(key);
177 size_t h = 0;
178
179 for (size_t i = 0; i < len; i++)
180 h = 33 * h ^ p[i];
181
182 return h;
183 }
184
194 inline size_t sax_hash(const void * key, const size_t len) noexcept
195 {
196 const auto *p = static_cast<const unsigned char *>(key);
197 size_t h = 0;
198
199 for (size_t i = 0; i < len; i++)
200 h ^= (h << 5) + (h >> 2) + p[i];
201
202 return h;
203 }
204
214 inline size_t fnv_hash(const void * key, const size_t len) noexcept
215 {
216 const auto *p = static_cast<const unsigned char *>(key);
217 size_t h = 2166136261;
218
219 for (size_t i = 0; i < len; i++)
220 h = (h * 16777619) ^ p[i];
221
222 return h;
223 }
224
234 inline size_t oat_hash(const void * key, const size_t len) noexcept
235 {
236 const auto *p = static_cast<const unsigned char *>(key);
237 size_t h = 0;
238
239 for (size_t i = 0; i < len; i++)
240 {
241 h += p[i];
242 h += (h << 10);
243 h ^= (h >> 6);
244 }
245
246 h += (h << 3);
247 h ^= (h >> 11);
248 h += (h << 15);
249
250 return h;
251 }
252
275 extern size_t jsw_hash(const void * key, size_t len) noexcept;
276
290 inline size_t elf_hash(const void * key, const size_t len) noexcept
291 {
292 const auto *p = static_cast<const unsigned char *>(key);
293 size_t h = 0;
294
295 for (size_t i = 0; i < len; i++)
296 {
297 h = (h << 4) + p[i];
298 size_t g = h & 0xf0000000L;
299
300 if (g != 0)
301 h ^= g >> 24;
302
303 h &= ~g;
304 }
305
306 return h;
307 }
308
318 extern size_t jen_hash(const void *key, size_t len, unsigned initval) noexcept;
319
320 /* The following is just a exact copy of functions implemented by Peter
321 Scott. They are at:
322
323 https://github.com/PeterScott/murmur3
324
325 They are a port of MurmurHash by Austin Appleby. See
326
327 http://en.wikipedia.org/wiki/MurmurHash
328
329 For more details
330 */
331
332 void MurmurHash3_x86_32 (const void *key, int len, uint32_t seed, void *out);
333
334 void MurmurHash3_x86_128(const void *key, int len, uint32_t seed, void *out);
335
336 void MurmurHash3_x64_128(const void *key, int len, uint32_t seed, void *out);
337
339 {
340 std::uint32_t a[4];
341 };
342
343 // Pick the 128-bit MurmurHash3 mixer on every 64-bit architecture, not
344 // just x86-64. The earlier `# ifdef __x86_64__` was misnamed: the
345 // implementation `MurmurHash3_x64_128` is plain C using `uint64_t`
346 // arithmetic with no x86 intrinsics, so it runs identically on AArch64
347 // and Windows ARM64. Routing those targets through `MurmurHash3_x86_32`
348 // produced 32 bits of payload written into a 64-bit `size_t`, leaving
349 // the high half uninitialised — undefined behaviour that the ARM64
350 // Clang Release pipeline happily folded into garbage hashes, breaking
351 // Count-Min Sketch / HyperLogLog / MinHash / Quotient-Filter tests.
352# if defined(__x86_64__) || defined(__aarch64__) \
353 || defined(_M_X64) || defined(_M_ARM64)
354# define ALEPH_HASH_USE_MURMUR3_64 1
355# else
356# define ALEPH_HASH_USE_MURMUR3_64 0
357# endif
358
359 template <typename Key>
360 requires std::is_trivially_copyable_v<Key>
361 inline size_t murmur3hash(const Key & key, std::uint32_t seed)
362 {
363# if ALEPH_HASH_USE_MURMUR3_64
364 Buf128Bits buf;
365 MurmurHash3_x64_128(&key, sizeof(key), seed, &buf);
366# else
367 std::uint32_t buf = 0;
368 MurmurHash3_x86_32(&key, sizeof(key), seed, &buf);
369# endif
370
371 size_t ret = 0;
372 memcpy(&ret, &buf, std::min(sizeof(ret), sizeof(buf)));
373
374 return ret;
375 }
376
377
378 inline size_t murmur3hash(const char * key, const std::uint32_t seed)
379 {
380 const size_t len = strlen(key);
381# if ALEPH_HASH_USE_MURMUR3_64
382 Buf128Bits buf;
383 MurmurHash3_x64_128(key, len, seed, &buf);
384# else
385 std::uint32_t buf = 0;
386 MurmurHash3_x86_32(key, len, seed, &buf);
387# endif
388
389 size_t ret = 0;
390 memcpy(&ret, &buf, std::min(sizeof(ret), sizeof(buf)));
391
392 return ret;
393 }
394
395 inline size_t murmur3hash(const std::string & key, const std::uint32_t seed)
396 {
397# if ALEPH_HASH_USE_MURMUR3_64
398 Buf128Bits buf;
399 MurmurHash3_x64_128(key.c_str(), key.size(), seed, &buf);
400# else
401 std::uint32_t buf = 0;
402 MurmurHash3_x86_32(key.c_str(), key.size(), seed, &buf);
403# endif
404
405 size_t ret = 0;
406 memcpy(&ret, &buf, std::min(sizeof(ret), sizeof(buf)));
407
408 return ret;
409 }
410
411
412 /* Paul Hsieh superfast hash function.
413
414 Taken from
415
416 http://www.azillionmonkeys.com/qed/hash.html
417 */
418
419#undef get16bits
420inline std::uint16_t get16bits(const void * p) noexcept
421{
422 std::uint16_t val;
423 memcpy(&val, p, sizeof(val));
424 return val;
425}
426
435 inline size_t SuperFastHash(const void * key, const size_t len, std::uint32_t seed = 0) noexcept
436 {
437 const auto * data = static_cast<const unsigned char *>(key);
438 std::uint32_t hash = static_cast<std::uint32_t>(len) ^ seed;
439
440 if (len <= 0 || data == nullptr)
441 return static_cast<size_t>(hash);
442
443 const int rem = len & 3;
444 const size_t nblocks = len >> 2;
445
446 /* Main loop */
447 for (size_t i = 0; i < nblocks; ++i)
448 {
449 hash += get16bits (data);
450 const std::uint32_t tmp = (static_cast<std::uint32_t>(get16bits(data+2)) << 11) ^ hash;
451 hash = (hash << 16) ^ tmp;
452 data += 2*sizeof (uint16_t);
453 hash += hash >> 11;
454 }
455
456 /* Handle end cases */
457 switch (rem)
458 {
459 case 3:
460 hash += get16bits (data);
461 hash ^= hash << 16;
462 hash ^= static_cast<signed char>(data[sizeof(uint16_t)]) << 18;
463 hash += hash >> 11;
464 break;
465 case 2:
466 hash += get16bits (data);
467 hash ^= hash << 11;
468 hash += hash >> 17;
469 break;
470 case 1:
471 hash += static_cast<signed char>(*data);
472 hash ^= hash << 10;
473 hash += hash >> 1;
474 break;
475 default:
476 break;
477 }
478
479 /* Force "avalanching" of final 127 bits */
480 hash ^= hash << 3;
481 hash += hash >> 5;
482 hash ^= hash << 4;
483 hash += hash >> 17;
484 hash ^= hash << 25;
485 hash += hash >> 6;
486
487 return static_cast<size_t>(hash);
488 }
489
502 size_t xxhash64_hash(const void * key, size_t len,
503 std::uint64_t seed = Default_Hash_Seed) noexcept;
504
516 size_t wyhash_hash(const void * key, size_t len,
517 std::uint64_t seed = Default_Hash_Seed) noexcept;
518
538 size_t siphash24_hash(const void * key, size_t len,
539 std::uint64_t key0 = 0x0706050403020100ULL,
540 std::uint64_t key1 = 0x0f0e0d0c0b0a0908ULL) noexcept;
541
542 template <typename Key>
544 inline size_t add_hash(const Key & key) noexcept
545 {
546 return add_hash(&key, sizeof(key));
547 }
548
549 template <typename Key>
550 requires std::is_trivially_copyable_v<Key>
551 inline size_t xor_hash(const Key & key) noexcept
552 {
553 return xor_hash(&key, sizeof(key));
554 }
555
556 template <typename Key>
557 requires std::is_trivially_copyable_v<Key>
558 inline size_t rot_hash(const Key & key) noexcept
559 {
560 return rot_hash(&key, sizeof(key));
561 }
562
563 template <typename Key>
564 requires std::is_trivially_copyable_v<Key>
565 inline size_t djb_hash(const Key & key) noexcept
566 {
567 return djb_hash(&key, sizeof(key));
568 }
569
570 template <typename Key>
571 requires std::is_trivially_copyable_v<Key>
572 inline size_t sax_hash(const Key & key) noexcept
573 {
574 return sax_hash(&key, sizeof(key));
575 }
576
577 template <typename Key>
578 requires std::is_trivially_copyable_v<Key>
579 inline size_t fnv_hash(const Key & key) noexcept
580 {
581 return fnv_hash(&key, sizeof(key));
582 }
583
584 template <typename Key>
585 requires std::is_trivially_copyable_v<Key>
586 inline size_t oat_hash(const Key & key) noexcept
587 {
588 return oat_hash(&key, sizeof(key));
589 }
590
593 template <typename Key>
594 requires std::is_trivially_copyable_v<Key>
595 inline size_t jsw_hash(const Key & key) noexcept
596 {
597 return jsw_hash(&key, sizeof(key));
598 }
599
600 template <typename Key>
601 requires std::is_trivially_copyable_v<Key>
602 inline size_t elf_hash(const Key & key) noexcept
603 {
604 return elf_hash(&key, sizeof(key));
605 }
606
607 template <typename Key>
608 requires std::is_trivially_copyable_v<Key>
609 inline size_t jen_hash(const Key & key, const unsigned initval) noexcept
610 {
611 return jen_hash(&key, sizeof(key), initval);
612 }
613
614 template <typename Key>
615 requires (std::is_trivially_copyable_v<Key> and
616 (not std::is_pointer_v<Key>) and (not std::is_array_v<Key>))
617 inline size_t SuperFastHash(const Key & key, std::uint32_t seed = 0) noexcept
618 {
619 return SuperFastHash(&key, sizeof(key), seed);
620 }
621
622 template <typename Key>
623 requires std::is_trivially_copyable_v<Key>
624 size_t xxhash64_hash(const Key & key,
625 std::uint64_t seed = Default_Hash_Seed) noexcept
626 {
627 return xxhash64_hash(&key, sizeof(key), seed);
628 }
629
630 template <typename Key>
631 requires std::is_trivially_copyable_v<Key>
632 size_t wyhash_hash(const Key & key,
633 std::uint64_t seed = Default_Hash_Seed) noexcept
634 {
635 return wyhash_hash(&key, sizeof(key), seed);
636 }
637
638 template <typename Key>
639 requires std::is_trivially_copyable_v<Key>
640 size_t siphash24_hash(const Key & key,
641 std::uint64_t key0 = 0x0706050403020100ULL,
642 std::uint64_t key1 = 0x0f0e0d0c0b0a0908ULL) noexcept
643 {
644 return siphash24_hash(&key, sizeof(key), key0, key1);
645 }
646
647 inline size_t add_hash(const char * key) noexcept
648 {
649 auto p = reinterpret_cast<const unsigned char *>(key);
650 size_t h = 0;
651
652 while (*p)
653 h += *p++;
654
655 return h;
656 }
657
658 inline size_t xor_hash(const char * key) noexcept
659 {
660 auto p = reinterpret_cast<const unsigned char *>(key);
661 size_t h = 0;
662
663 while (*p)
664 h ^= *p++;
665
666 return h;
667 }
668
669 inline size_t rot_hash(const char * key) noexcept
670 {
671 auto p = reinterpret_cast<const unsigned char *>(key);
672 size_t h = 0;
673
674 while (*p)
675 h = (h << 4) ^(h >> 28) ^ *p++;
676
677 return h;
678 }
679
680 inline size_t djb_hash(const char * key) noexcept
681 {
682 auto p = reinterpret_cast<const unsigned char *>(key);
683 size_t h = 0;
684
685 while (*p)
686 h = 33 * h ^ *p++;
687
688 return h;
689 }
690
691 inline size_t sax_hash(const char * key) noexcept
692 {
693 auto p = reinterpret_cast<const unsigned char *>(key);
694 size_t h = 0;
695
696 while (*p)
697 h ^= (h << 5) +(h >> 2) + *p++;
698
699 return h;
700 }
701
702 inline size_t fnv_hash(const char * key) noexcept
703 {
704 auto p = reinterpret_cast<const unsigned char *>(key);
705 size_t h = 2166136261;
706
707 while (*p)
708 h = (h * 16777619) ^ *p++;
709
710 return h;
711 }
712
713 inline size_t oat_hash(const char * key) noexcept
714 {
715 auto p = reinterpret_cast<const unsigned char *>(key);
716 size_t h = 0;
717
718 while (*p)
719 {
720 h += *p++;
721 h += (h << 10);
722 h ^= (h >> 6);
723 }
724
725 h += (h << 3);
726 h ^= (h >> 11);
727 h += (h << 15);
728
729 return h;
730 }
731
737 extern size_t jsw_hash(const char * key) noexcept;
738
739 inline size_t elf_hash(const char * key) noexcept
740 {
741 auto p = reinterpret_cast<const unsigned char *>(key);
742 size_t h = 0;
743
744 while (*p)
745 {
746 h =(h << 4) + *p++;
747 size_t g = h & 0xf0000000L;
748
749 if(g != 0)
750 h ^= g >> 24;
751
752 h &= ~g;
753 }
754
755 return h;
756 }
757
758 inline size_t SuperFastHash(const char * key, std::uint32_t seed = 0) noexcept
759 {
760 return SuperFastHash(key, strlen(key), seed);
761 }
762
763 inline size_t xxhash64_hash(const char * key,
764 std::uint64_t seed = Default_Hash_Seed) noexcept
765 {
766 return xxhash64_hash(key, strlen(key), seed);
767 }
768
769 inline size_t wyhash_hash(const char * key,
770 std::uint64_t seed = Default_Hash_Seed) noexcept
771 {
772 return wyhash_hash(key, strlen(key), seed);
773 }
774
775 inline size_t siphash24_hash(const char * key,
776 std::uint64_t key0 = 0x0706050403020100ULL,
777 std::uint64_t key1 = 0x0f0e0d0c0b0a0908ULL) noexcept
778 {
779 return siphash24_hash(key, strlen(key), key0, key1);
780 }
781
782 // String overloads use data()+size() so that embedded NUL bytes are hashed
783 // correctly. Calling c_str() delegates to the NUL-terminated char* overload
784 // which would stop at the first '\0'.
785
786 inline size_t add_hash(const std::string & key) noexcept
787 {
788 return add_hash(key.data(), key.size());
789 }
790
791 inline size_t xor_hash(const std::string & key) noexcept
792 {
793 return xor_hash(key.data(), key.size());
794 }
795
796 inline size_t rot_hash(const std::string & key) noexcept
797 {
798 return rot_hash(key.data(), key.size());
799 }
800
801 inline size_t djb_hash(const std::string & key) noexcept
802 {
803 return djb_hash(key.data(), key.size());
804 }
805
806 inline size_t sax_hash(const std::string & key) noexcept
807 {
808 return sax_hash(key.data(), key.size());
809 }
810
811 inline size_t fnv_hash(const std::string & key) noexcept
812 {
813 return fnv_hash(key.data(), key.size());
814 }
815
816 inline size_t oat_hash(const std::string & key) noexcept
817 {
818 return oat_hash(key.data(), key.size());
819 }
820
823 inline size_t jsw_hash(const std::string & key) noexcept
824 {
825 return jsw_hash(key.data(), key.size());
826 }
827
828 inline size_t elf_hash(const std::string & key) noexcept
829 {
830 return elf_hash(key.data(), key.size());
831 }
832
833 inline size_t jen_hash(const std::string & key, unsigned initval) noexcept
834 {
835 return jen_hash(key.c_str(), key.size(), initval);
836 }
837
838 inline size_t SuperFastHash(const std::string & key, std::uint32_t seed = 0) noexcept
839 {
840 return SuperFastHash(key.c_str(), key.size(), seed);
841 }
842
843 inline size_t xxhash64_hash(const std::string & key,
844 std::uint64_t seed = Default_Hash_Seed) noexcept
845 {
846 return xxhash64_hash(key.data(), key.size(), seed);
847 }
848
849 inline size_t wyhash_hash(const std::string & key,
850 std::uint64_t seed = Default_Hash_Seed) noexcept
851 {
852 return wyhash_hash(key.data(), key.size(), seed);
853 }
854
855 inline size_t siphash24_hash(const std::string & key,
856 std::uint64_t key0 = 0x0706050403020100ULL,
857 std::uint64_t key1 = 0x0f0e0d0c0b0a0908ULL) noexcept
858 {
859 return siphash24_hash(key.data(), key.size(), key0, key1);
860 }
861
862 // ──────────────────────────────────────────────────────────────────────
863 // hash_combine (non-commutative fold used by pair helpers and Aleph_Hash)
864 // ──────────────────────────────────────────────────────────────────────
865
877 inline void hash_combine(size_t & seed, size_t h) noexcept
878 {
879 seed ^= h + size_t{0x9e3779b97f4a7c15ULL} + (seed << 12) + (seed >> 4);
880 }
881
882 // ──────────────────────────────────────────────────────────────────────
883 // Semantic Hash Customization Layer (HASH-005)
884 // ──────────────────────────────────────────────────────────────────────
885
895 template <typename T>
896 concept HashableByADL = requires(const T & t)
897 {
898 { aleph_hash_value(t) } -> std::convertible_to<size_t>;
899 };
900
915 template <typename T>
917 {
918 // Floating-point types are excluded from the raw-byte fallback because equal
919 // values can have different bit patterns (e.g., -0.0 == +0.0, NaN != NaN
920 // but multiple bit representations exist). Implement aleph_hash_value() or
921 // specialize Aleph_Hash<> for float/double.
922 static_assert(
924 (std::is_trivially_copyable_v<T>
925 and not std::is_pointer_v<T>
926 and not std::is_floating_point_v<T>),
927 "Aleph_Hash<T>: T has no aleph_hash_value() in its namespace and cannot "
928 "be safely byte-hashed (pointer, floating-point, or non-trivially-copyable "
929 "type). Define:\n"
930 " size_t aleph_hash_value(const T &) noexcept;\n"
931 "in T's namespace, or specialize Aleph_Hash<T>."
932 );
933
934 [[nodiscard]] size_t operator()(const T & key) const noexcept
935 {
936 if constexpr (HashableByADL<T>)
937 return aleph_hash_value(key);
938 else
939 return wyhash_hash(key);
940 }
941
942 [[nodiscard]] size_t operator()(const T & key, std::uint32_t seed) const noexcept
943 {
944 if constexpr (HashableByADL<T>)
945 {
946 size_t h = aleph_hash_value(key);
947 hash_combine(h, static_cast<size_t>(seed));
948 return h;
949 }
950 else
951 return wyhash_hash(key, seed);
952 }
953 };
954
955 // Explicit specializations for floating-point types.
956 // The raw-byte fallback is excluded for floats because equal values can have
957 // different bit patterns (-0.0 == +0.0, multiple NaN encodings). These
958 // specializations normalize -0.0 to +0.0 before hashing the bits.
959
960 // Note on NaN: distinct NaN bit-patterns hash differently because NaN != NaN
961 // is not handled here — normalization is applied only to -0.0 (mapped to +0.0).
962 // Callers that store NaN keys must canonicalize them before hashing.
963 template <>
965 {
966 [[nodiscard]] size_t operator()(float key) const noexcept
967 {
968 if (key == 0.0f) key = 0.0f; // normalize -0.0 → +0.0
969 std::uint32_t bits = 0;
970 std::memcpy(&bits, &key, sizeof(bits));
971 return SuperFastHash(&bits, sizeof(bits));
972 }
973 [[nodiscard]] size_t operator()(float key, std::uint32_t seed) const noexcept
974 {
975 if (key == 0.0f) key = 0.0f;
976 std::uint32_t bits = 0;
977 std::memcpy(&bits, &key, sizeof(bits));
978 return SuperFastHash(&bits, sizeof(bits), seed);
979 }
980 };
981
982 template <>
984 {
985 [[nodiscard]] size_t operator()(double key) const noexcept
986 {
987 if (key == 0.0) key = 0.0; // normalize -0.0
988 std::uint64_t bits = 0;
989 std::memcpy(&bits, &key, sizeof(bits));
990 return wyhash_hash(bits); // pass by value → wyhash_hash<uint64_t> overload
991 }
992 [[nodiscard]] size_t operator()(double key, std::uint32_t seed) const noexcept
993 {
994 if (key == 0.0) key = 0.0;
995 std::uint64_t bits = 0;
996 std::memcpy(&bits, &key, sizeof(bits));
997 return wyhash_hash(bits, seed);
998 }
999 };
1000
1001 // ──────────────────────────────────────────────────────────────────────
1002 // Default hash functions (route through Aleph_Hash)
1003 // ──────────────────────────────────────────────────────────────────────
1004
1006 static constexpr std::uint32_t Aleph_Snd_Hash_Seed = 0x9e3779b9u;
1007
1026 template <typename Key>
1027 requires (not std::is_pointer_v<Key> and
1028 not std::is_array_v<Key> and
1029 not std::is_same_v<std::remove_cvref_t<Key>, std::string>)
1030 inline size_t dft_hash_fct(const Key & key) noexcept
1031 {
1032 return Aleph_Hash<Key>{}(key);
1033 }
1034
1035 template <typename Key>
1036 requires std::is_pointer_v<Key>
1037 inline size_t dft_hash_fct(const Key & key) noexcept
1038 {
1039 return SuperFastHash(&key, sizeof(key));
1040 }
1041
1042 inline size_t dft_hash_fct(const char * key) noexcept
1043 {
1044 return wyhash_hash(key);
1045 }
1046
1047 inline size_t dft_hash_fct(const std::string & key) noexcept
1048 {
1049 return wyhash_hash(key);
1050 }
1051
1055 template <typename Key>
1056 requires (not std::is_pointer_v<Key> and
1057 not std::is_array_v<Key> and
1058 not std::is_same_v<std::remove_cvref_t<Key>, std::string>)
1059 inline size_t dft_hash_fct(const Key & key, std::uint32_t seed) noexcept
1060 {
1061 return Aleph_Hash<Key>{}(key, seed);
1062 }
1063
1064 template <typename Key>
1065 requires std::is_pointer_v<Key>
1066 inline size_t dft_hash_fct(const Key & key, std::uint32_t seed) noexcept
1067 {
1068 return SuperFastHash(&key, sizeof(key), seed);
1069 }
1070
1071 inline size_t dft_hash_fct(const char * key, std::uint32_t seed) noexcept
1072 {
1073 return wyhash_hash(key, seed);
1074 }
1075
1076 inline size_t dft_hash_fct(const std::string & key, std::uint32_t seed) noexcept
1077 {
1078 return wyhash_hash(key, seed);
1079 }
1080
1081 template <typename Key>
1082 inline size_t dft_hash_ptr_fct(const Key & key) noexcept
1083 {
1084 return dft_hash_fct(key);
1085 }
1086
1087 template <typename Key>
1088 inline size_t dft_seed_hash_ptr_fct(const Key & key, const unsigned long seed) noexcept
1089 {
1090 return dft_hash_fct(key, static_cast<std::uint32_t>(seed));
1091 }
1092
1104 template <typename Key>
1105 requires (not std::is_pointer_v<Key> and
1106 not std::is_array_v<Key> and
1107 not std::is_same_v<std::remove_cvref_t<Key>, std::string>)
1108 inline size_t snd_hash_fct(const Key & key) noexcept
1109 {
1110 return Aleph_Hash<Key>{}(key, Aleph_Snd_Hash_Seed);
1111 }
1112
1113 template <typename Key>
1114 requires std::is_pointer_v<Key>
1115 inline size_t snd_hash_fct(const Key & key) noexcept
1116 {
1117 return SuperFastHash(&key, sizeof(key), Aleph_Snd_Hash_Seed);
1118 }
1119
1120 inline size_t snd_hash_fct(const char * key) noexcept
1121 {
1122 return wyhash_hash(key, Aleph_Snd_Hash_Seed);
1123 }
1124
1125 inline size_t snd_hash_fct(const std::string & key) noexcept
1126 {
1127 return wyhash_hash(key, Aleph_Snd_Hash_Seed);
1128 }
1129
1130 template <typename Key>
1131 inline size_t snd_hash_ptr_fct(const Key & key) noexcept
1132 {
1133 return snd_hash_fct(key);
1134 }
1135
1136 template<typename Key, typename Data, typename Fct>
1137 inline size_t map_hash_fct(Fct fct, const std::pair<Key, Data> & p) noexcept
1138 {
1139 return fct(p.first);
1140 }
1141
1142 template <typename K1, typename K2>
1143 requires requires(const K1 & k1, const K2 & k2)
1144 {
1147 }
1148 inline size_t pair_dft_hash_fct(const std::pair<K1, K2> & p) noexcept
1149 {
1150 size_t seed = dft_hash_fct(p.first);
1151 hash_combine(seed, dft_hash_fct(p.second));
1152 return seed;
1153 }
1154
1155 template <typename K1, typename K2>
1156 requires requires(const K1 & k1, const K2 & k2)
1157 {
1160 }
1161 inline size_t pair_snd_hash_fct(const std::pair<K1, K2> & p) noexcept
1162 {
1163 size_t seed = dft_hash_fct(p.first);
1164 hash_combine(seed, snd_hash_fct(p.second));
1165 return seed;
1166 }
1167
1175 template <typename K1, typename K2>
1176 inline size_t pair_dft_hash_ptr_fct(const std::pair<K1, K2> & p) noexcept
1177 {
1178 return pair_dft_hash_fct(p);
1179 }
1180
1181 template <typename K1, typename K2>
1182 inline size_t pair_snd_hash_ptr_fct(const std::pair<K1, K2> & p) noexcept
1183 {
1184 return pair_snd_hash_fct(p);
1185 }
1186
1187} // end namespace Aleph
1188
1189
1190# endif // HASH_FCT_H
long double h
Definition btreepic.C:154
size_t size_t int32_t * out
Definition ca-c-api.h:120
Concept: T provides a semantic hash via ADL.
Definition hash-fct.H:896
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
size_t sax_hash(const void *key, const size_t len) noexcept
Shift-Add-XOR hash (SAX hash)
Definition hash-fct.H:194
size_t add_hash(const void *key, const size_t len) noexcept
Additive hash.
Definition hash-fct.H:112
size_t jsw_hash(const void *key, size_t len) noexcept
JSW hash (Julienne Walker)
Definition hash-fct.C:290
size_t xxhash64_hash(const void *key, size_t len, std::uint64_t seed) noexcept
xxHash64 from the xxHash family.
Definition hash-fct.C:652
size_t djb_hash(const void *key, const size_t len) noexcept
Bernstein's hash (DJB hash)
Definition hash-fct.H:174
size_t jen_hash(const void *key, size_t length, unsigned initval) noexcept
Jenkins hash (lookup3)
Definition hash-fct.C:368
size_t xor_hash(const void *key, const size_t len) noexcept
XOR hash.
Definition hash-fct.H:133
size_t SuperFastHash(const void *key, const size_t len, std::uint32_t seed=0) noexcept
Paul Hsieh super fast hash function.
Definition hash-fct.H:435
size_t fnv_hash(const void *key, const size_t len) noexcept
FNV-1a hash.
Definition hash-fct.H:214
size_t siphash24_hash(const void *key, size_t len, std::uint64_t key0, std::uint64_t key1) noexcept
SipHash-2-4 keyed hash.
Definition hash-fct.C:807
bool is_jsw_initialized() noexcept
Checks if the jsw_hash() lookup table has been initialized.
Definition hash-fct.C:254
void init_jsw() noexcept
Initializes the randomized lookup table used by jsw_hash().
Definition hash-fct.C:262
size_t rot_hash(const void *key, const size_t len) noexcept
Rotating hash.
Definition hash-fct.H:154
size_t wyhash_hash(const void *key, size_t len, std::uint64_t seed) noexcept
wyhash non-cryptographic hash.
Definition hash-fct.C:735
size_t elf_hash(const void *key, const size_t len) noexcept
ELF hash.
Definition hash-fct.H:290
size_t oat_hash(const void *key, const size_t len) noexcept
One-at-a-Time hash (OAT hash)
Definition hash-fct.H:234
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
size_t map_hash_fct(Fct fct, const std::pair< Key, Data > &p) noexcept
Definition hash-fct.H:1137
size_t pair_snd_hash_fct(const std::pair< K1, K2 > &p) noexcept
Definition hash-fct.H:1161
void MurmurHash3_x86_32(const void *key, int len, uint32_t seed, void *out)
Definition hash-fct.C:415
static constexpr std::uint32_t Aleph_Snd_Hash_Seed
Fixed seed used by the secondary hash function.
Definition hash-fct.H:1006
void MurmurHash3_x64_128(const void *key, const int len, const uint32_t seed, void *out)
Definition hash-fct.C:572
size_t snd_hash_fct(const Key &key) noexcept
Secondary default hash: different distribution from dft_hash_fct.
Definition hash-fct.H:1108
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
size_t aleph_hash_value(const AdversarialTranspositionKey< StateKey > &key) noexcept
Definition Negamax.H:134
size_t pair_snd_hash_ptr_fct(const std::pair< K1, K2 > &p) noexcept
Definition hash-fct.H:1182
size_t dft_hash_fct(const Key &key) noexcept
Primary default hash: best speed/quality trade-off.
Definition hash-fct.H:1030
void hash_combine(size_t &seed, size_t h) noexcept
Non-commutative hash combiner (Boost-style golden-ratio mix).
Definition hash-fct.H:877
size_t snd_hash_ptr_fct(const Key &key) noexcept
Definition hash-fct.H:1131
size_t pair_dft_hash_fct(const std::pair< K1, K2 > &p) noexcept
Definition hash-fct.H:1148
void MurmurHash3_x86_128(const void *key, const int len, uint32_t seed, void *out)
Definition hash-fct.C:468
const unsigned Default_Hash_Seed
Definition hash-fct.C:46
std::uint16_t get16bits(const void *p) noexcept
Definition hash-fct.H:420
size_t pair_dft_hash_ptr_fct(const std::pair< K1, K2 > &p) noexcept
Definition hash-fct.H:1176
size_t murmur3hash(const Key &key, std::uint32_t seed)
Definition hash-fct.H:361
size_t dft_hash_ptr_fct(const Key &key) noexcept
Definition hash-fct.H:1082
size_t dft_seed_hash_ptr_fct(const Key &key, const unsigned long seed) noexcept
Definition hash-fct.H:1088
STL namespace.
size_t operator()(double key) const noexcept
Definition hash-fct.H:985
size_t operator()(double key, std::uint32_t seed) const noexcept
Definition hash-fct.H:992
size_t operator()(float key, std::uint32_t seed) const noexcept
Definition hash-fct.H:973
size_t operator()(float key) const noexcept
Definition hash-fct.H:966
Primary hash functor used internally by dft_hash_fct / snd_hash_fct.
Definition hash-fct.H:917
size_t operator()(const T &key, std::uint32_t seed) const noexcept
Definition hash-fct.H:942
size_t operator()(const T &key) const noexcept
Definition hash-fct.H:934
std::uint32_t a[4]
Definition hash-fct.H:340
ValueArg< size_t > seed
Definition testHash.C:53