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
31# ifndef HASH_FCT_H
32# define HASH_FCT_H
33
34# include <cstddef>
35# include <cstring>
36# include <string>
37# include <concepts>
38# include <type_traits>
39# include <cstdint>
40
58namespace Aleph
59{
60 extern const unsigned Default_Hash_Seed;
61
69 void init_jsw() noexcept;
70
80
87
101 inline size_t add_hash(const void * key, const size_t len) noexcept
102 {
103 const auto *p = static_cast<const unsigned char *>(key);
104 size_t h = 0;
105
106 for (size_t i = 0; i < len; i++)
107 h += p[i];
108
109 return h;
110 }
111
122 inline size_t xor_hash(const void * key, const size_t len) noexcept
123 {
124 const auto *p = static_cast<const unsigned char *>(key);
125 size_t h = 0;
126
127 for (size_t i = 0; i < len; i++)
128 h ^= p[i];
129
130 return h;
131 }
132
143 inline size_t rot_hash(const void * key, const size_t len) noexcept
144 {
145 const auto *p = static_cast<const unsigned char *>(key);
146 size_t h = 0;
147
148 for (size_t i = 0; i < len; i++)
149 h = (h << 4) ^ (h >> 28) ^ p[i];
150
151 return h;
152 }
153
163 inline size_t djb_hash(const void * key, const size_t len) noexcept
164 {
165 const auto *p = static_cast<const unsigned char *>(key);
166 size_t h = 0;
167
168 for (size_t i = 0; i < len; i++)
169 h = 33 * h ^ p[i];
170
171 return h;
172 }
173
183 inline size_t sax_hash(const void * key, const size_t len) noexcept
184 {
185 const auto *p = static_cast<const unsigned char *>(key);
186 size_t h = 0;
187
188 for (size_t i = 0; i < len; i++)
189 h ^= (h << 5) + (h >> 2) + p[i];
190
191 return h;
192 }
193
203 inline size_t fnv_hash(const void * key, const size_t len) noexcept
204 {
205 const auto *p = static_cast<const unsigned char *>(key);
206 size_t h = 2166136261;
207
208 for (size_t i = 0; i < len; i++)
209 h = (h * 16777619) ^ p[i];
210
211 return h;
212 }
213
223 inline size_t oat_hash(const void * key, const size_t len) noexcept
224 {
225 const auto *p = static_cast<const unsigned char *>(key);
226 size_t h = 0;
227
228 for (size_t i = 0; i < len; i++)
229 {
230 h += p[i];
231 h += (h << 10);
232 h ^= (h >> 6);
233 }
234
235 h += (h << 3);
236 h ^= (h >> 11);
237 h += (h << 15);
238
239 return h;
240 }
241
264 extern size_t jsw_hash(const void * key, size_t len) noexcept;
265
279 inline size_t elf_hash(const void * key, const size_t len) noexcept
280 {
281 const auto *p = static_cast<const unsigned char *>(key);
282 size_t h = 0;
283
284 for (size_t i = 0; i < len; i++)
285 {
286 h = (h << 4) + p[i];
287 size_t g = h & 0xf0000000L;
288
289 if (g != 0)
290 h ^= g >> 24;
291
292 h &= ~g;
293 }
294
295 return h;
296 }
297
307 extern size_t jen_hash(const void *key, size_t len, unsigned initval) noexcept;
308
309 /* The following is just a exact copy of functions implemented by Peter
310 Scott. They are at:
311
312 https://github.com/PeterScott/murmur3
313
314 They are a port of MurmurHash by Austin Appleby. See
315
316 http://en.wikipedia.org/wiki/MurmurHash
317
318 For more details
319 */
320
321 void MurmurHash3_x86_32 (const void *key, int len, uint32_t seed, void *out);
322
323 void MurmurHash3_x86_128(const void *key, int len, uint32_t seed, void *out);
324
325 void MurmurHash3_x64_128(const void *key, int len, uint32_t seed, void *out);
326
328 {
329 std::uint32_t a[4];
330 };
331
332 template <typename Key>
333 requires std::is_trivially_copyable_v<Key>
334 inline size_t murmur3hash(const Key & key, std::uint32_t seed)
335 {
336# ifdef __x86_64__
337 Buf128Bits buf;
338 MurmurHash3_x64_128(&key, sizeof(key), seed, &buf);
339# else
340 size_t buf;
341 MurmurHash3_x86_32(&key, sizeof(key), seed, &buf);
342# endif
343
344 size_t ret;
345 memcpy(&ret, &buf, sizeof(ret));
346
347 return ret;
348 }
349
350
351 inline size_t murmur3hash(const char * key, const std::uint32_t seed)
352 {
353 const size_t len = strlen(key);
354# ifdef __x86_64__
355 Buf128Bits buf;
356 MurmurHash3_x64_128(key, len, seed, &buf);
357# else
358 size_t buf;
359 MurmurHash3_x86_32(key, len, seed, &buf);
360# endif
361
362 size_t ret;
363 memcpy(&ret, &buf, sizeof(ret));
364
365 return ret;
366 }
367
368 inline size_t murmur3hash(const std::string & key, const std::uint32_t seed)
369 {
370# ifdef __x86_64__
371 Buf128Bits buf;
372 MurmurHash3_x64_128(key.c_str(), key.size(), seed, &buf);
373# else
374 size_t buf;
375 MurmurHash3_x86_32(key.c_str(), key.size(), seed, &buf);
376# endif
377
378 size_t ret;
379 memcpy(&ret, &buf, sizeof(ret));
380
381 return ret;
382 }
383
384
385 /* Paul Hsieh superfast hash function.
386
387 Taken from
388
389 http://www.azillionmonkeys.com/qed/hash.html
390 */
391
392#undef get16bits
393inline std::uint16_t get16bits(const void * p) noexcept
394{
395 std::uint16_t val;
396 memcpy(&val, p, sizeof(val));
397 return val;
398}
399
408 inline size_t SuperFastHash(const void * key, size_t len, std::uint32_t seed = 0) noexcept
409 {
410 const auto * data = static_cast<const unsigned char *>(key);
411 std::uint32_t hash = static_cast<std::uint32_t>(len) ^ seed;
412
413 if (len <= 0 || data == nullptr)
414 return static_cast<size_t>(hash);
415
416 const int rem = len & 3;
417 const size_t nblocks = len >> 2;
418
419 /* Main loop */
420 for (size_t i = 0; i < nblocks; ++i)
421 {
422 hash += get16bits (data);
423 const std::uint32_t tmp = (static_cast<std::uint32_t>(get16bits(data+2)) << 11) ^ hash;
424 hash = (hash << 16) ^ tmp;
425 data += 2*sizeof (uint16_t);
426 hash += hash >> 11;
427 }
428
429 /* Handle end cases */
430 switch (rem)
431 {
432 case 3:
433 hash += get16bits (data);
434 hash ^= hash << 16;
435 hash ^= static_cast<signed char>(data[sizeof(uint16_t)]) << 18;
436 hash += hash >> 11;
437 break;
438 case 2:
439 hash += get16bits (data);
440 hash ^= hash << 11;
441 hash += hash >> 17;
442 break;
443 case 1:
444 hash += static_cast<signed char>(*data);
445 hash ^= hash << 10;
446 hash += hash >> 1;
447 break;
448 default:
449 break;
450 }
451
452 /* Force "avalanching" of final 127 bits */
453 hash ^= hash << 3;
454 hash += hash >> 5;
455 hash ^= hash << 4;
456 hash += hash >> 17;
457 hash ^= hash << 25;
458 hash += hash >> 6;
459
460 return static_cast<size_t>(hash);
461 }
462
475 size_t xxhash64_hash(const void * key, size_t len,
476 std::uint64_t seed = Default_Hash_Seed) noexcept;
477
489 size_t wyhash_hash(const void * key, size_t len,
490 std::uint64_t seed = Default_Hash_Seed) noexcept;
491
511 size_t siphash24_hash(const void * key, size_t len,
512 std::uint64_t key0 = 0x0706050403020100ULL,
513 std::uint64_t key1 = 0x0f0e0d0c0b0a0908ULL) noexcept;
514
515 template <typename Key>
517 inline size_t add_hash(const Key & key) noexcept
518 {
519 return add_hash(&key, sizeof(key));
520 }
521
522 template <typename Key>
523 requires std::is_trivially_copyable_v<Key>
524 inline size_t xor_hash(const Key & key) noexcept
525 {
526 return xor_hash(&key, sizeof(key));
527 }
528
529 template <typename Key>
530 requires std::is_trivially_copyable_v<Key>
531 inline size_t rot_hash(const Key & key) noexcept
532 {
533 return rot_hash(&key, sizeof(key));
534 }
535
536 template <typename Key>
537 requires std::is_trivially_copyable_v<Key>
538 inline size_t djb_hash(const Key & key) noexcept
539 {
540 return djb_hash(&key, sizeof(key));
541 }
542
543 template <typename Key>
544 requires std::is_trivially_copyable_v<Key>
545 inline size_t sax_hash(const Key & key) noexcept
546 {
547 return sax_hash(&key, sizeof(key));
548 }
549
550 template <typename Key>
551 requires std::is_trivially_copyable_v<Key>
552 inline size_t fnv_hash(const Key & key) noexcept
553 {
554 return fnv_hash(&key, sizeof(key));
555 }
556
557 template <typename Key>
558 requires std::is_trivially_copyable_v<Key>
559 inline size_t oat_hash(const Key & key) noexcept
560 {
561 return oat_hash(&key, sizeof(key));
562 }
563
566 template <typename Key>
567 requires std::is_trivially_copyable_v<Key>
568 inline size_t jsw_hash(const Key & key) noexcept
569 {
570 return jsw_hash(&key, sizeof(key));
571 }
572
573 template <typename Key>
574 requires std::is_trivially_copyable_v<Key>
575 inline size_t elf_hash(const Key & key) noexcept
576 {
577 return elf_hash(&key, sizeof(key));
578 }
579
580 template <typename Key>
581 requires std::is_trivially_copyable_v<Key>
582 inline size_t jen_hash(const Key & key, const unsigned initval) noexcept
583 {
584 return jen_hash(&key, sizeof(key), initval);
585 }
586
587 template <typename Key>
588 requires (std::is_trivially_copyable_v<Key> and
589 (not std::is_pointer_v<Key>) and (not std::is_array_v<Key>))
590 inline size_t SuperFastHash(const Key & key, std::uint32_t seed = 0) noexcept
591 {
592 return SuperFastHash(&key, sizeof(key), seed);
593 }
594
595 template <typename Key>
596 requires std::is_trivially_copyable_v<Key>
597 size_t xxhash64_hash(const Key & key,
598 std::uint64_t seed = Default_Hash_Seed) noexcept
599 {
600 return xxhash64_hash(&key, sizeof(key), seed);
601 }
602
603 template <typename Key>
604 requires std::is_trivially_copyable_v<Key>
605 size_t wyhash_hash(const Key & key,
606 std::uint64_t seed = Default_Hash_Seed) noexcept
607 {
608 return wyhash_hash(&key, sizeof(key), seed);
609 }
610
611 template <typename Key>
612 requires std::is_trivially_copyable_v<Key>
613 size_t siphash24_hash(const Key & key,
614 std::uint64_t key0 = 0x0706050403020100ULL,
615 std::uint64_t key1 = 0x0f0e0d0c0b0a0908ULL) noexcept
616 {
617 return siphash24_hash(&key, sizeof(key), key0, key1);
618 }
619
620 inline size_t add_hash(const char * key) noexcept
621 {
622 auto p = reinterpret_cast<const unsigned char *>(key);
623 size_t h = 0;
624
625 while (*p)
626 h += *p++;
627
628 return h;
629 }
630
631 inline size_t xor_hash(const char * key) noexcept
632 {
633 auto p = reinterpret_cast<const unsigned char *>(key);
634 size_t h = 0;
635
636 while (*p)
637 h ^= *p++;
638
639 return h;
640 }
641
642 inline size_t rot_hash(const char * key) noexcept
643 {
644 auto p = reinterpret_cast<const unsigned char *>(key);
645 size_t h = 0;
646
647 while (*p)
648 h = (h << 4) ^(h >> 28) ^ *p++;
649
650 return h;
651 }
652
653 inline size_t djb_hash(const char * key) noexcept
654 {
655 auto p = reinterpret_cast<const unsigned char *>(key);
656 size_t h = 0;
657
658 while (*p)
659 h = 33 * h ^ *p++;
660
661 return h;
662 }
663
664 inline size_t sax_hash(const char * key) noexcept
665 {
666 auto p = reinterpret_cast<const unsigned char *>(key);
667 size_t h = 0;
668
669 while (*p)
670 h ^= (h << 5) +(h >> 2) + *p++;
671
672 return h;
673 }
674
675 inline size_t fnv_hash(const char * key) noexcept
676 {
677 auto p = reinterpret_cast<const unsigned char *>(key);
678 size_t h = 2166136261;
679
680 while (*p)
681 h = (h * 16777619) ^ *p++;
682
683 return h;
684 }
685
686 inline size_t oat_hash(const char * key) noexcept
687 {
688 auto p = reinterpret_cast<const unsigned char *>(key);
689 size_t h = 0;
690
691 while (*p)
692 {
693 h += *p++;
694 h += (h << 10);
695 h ^= (h >> 6);
696 }
697
698 h += (h << 3);
699 h ^= (h >> 11);
700 h += (h << 15);
701
702 return h;
703 }
704
710 extern size_t jsw_hash(const char * key) noexcept;
711
712 inline size_t elf_hash(const char * key) noexcept
713 {
714 auto p = reinterpret_cast<const unsigned char *>(key);
715 size_t h = 0;
716
717 while (*p)
718 {
719 h =(h << 4) + *p++;
720 size_t g = h & 0xf0000000L;
721
722 if(g != 0)
723 h ^= g >> 24;
724
725 h &= ~g;
726 }
727
728 return h;
729 }
730
731 inline size_t SuperFastHash(const char * key, std::uint32_t seed = 0) noexcept
732 {
733 return SuperFastHash(key, strlen(key), seed);
734 }
735
736 inline size_t xxhash64_hash(const char * key,
737 std::uint64_t seed = Default_Hash_Seed) noexcept
738 {
739 return xxhash64_hash(key, strlen(key), seed);
740 }
741
742 inline size_t wyhash_hash(const char * key,
743 std::uint64_t seed = Default_Hash_Seed) noexcept
744 {
745 return wyhash_hash(key, strlen(key), seed);
746 }
747
748 inline size_t siphash24_hash(const char * key,
749 std::uint64_t key0 = 0x0706050403020100ULL,
750 std::uint64_t key1 = 0x0f0e0d0c0b0a0908ULL) noexcept
751 {
752 return siphash24_hash(key, strlen(key), key0, key1);
753 }
754
755 // String overloads use data()+size() so that embedded NUL bytes are hashed
756 // correctly. Calling c_str() delegates to the NUL-terminated char* overload
757 // which would stop at the first '\0'.
758
759 inline size_t add_hash(const std::string & key) noexcept
760 {
761 return add_hash(key.data(), key.size());
762 }
763
764 inline size_t xor_hash(const std::string & key) noexcept
765 {
766 return xor_hash(key.data(), key.size());
767 }
768
769 inline size_t rot_hash(const std::string & key) noexcept
770 {
771 return rot_hash(key.data(), key.size());
772 }
773
774 inline size_t djb_hash(const std::string & key) noexcept
775 {
776 return djb_hash(key.data(), key.size());
777 }
778
779 inline size_t sax_hash(const std::string & key) noexcept
780 {
781 return sax_hash(key.data(), key.size());
782 }
783
784 inline size_t fnv_hash(const std::string & key) noexcept
785 {
786 return fnv_hash(key.data(), key.size());
787 }
788
789 inline size_t oat_hash(const std::string & key) noexcept
790 {
791 return oat_hash(key.data(), key.size());
792 }
793
796 inline size_t jsw_hash(const std::string & key) noexcept
797 {
798 return jsw_hash(key.data(), key.size());
799 }
800
801 inline size_t elf_hash(const std::string & key) noexcept
802 {
803 return elf_hash(key.data(), key.size());
804 }
805
806 inline size_t jen_hash(const std::string & key, unsigned initval) noexcept
807 {
808 return jen_hash(key.c_str(), key.size(), initval);
809 }
810
811 inline size_t SuperFastHash(const std::string & key, std::uint32_t seed = 0) noexcept
812 {
813 return SuperFastHash(key.c_str(), key.size(), seed);
814 }
815
816 inline size_t xxhash64_hash(const std::string & key,
817 std::uint64_t seed = Default_Hash_Seed) noexcept
818 {
819 return xxhash64_hash(key.data(), key.size(), seed);
820 }
821
822 inline size_t wyhash_hash(const std::string & key,
823 std::uint64_t seed = Default_Hash_Seed) noexcept
824 {
825 return wyhash_hash(key.data(), key.size(), seed);
826 }
827
828 inline size_t siphash24_hash(const std::string & key,
829 std::uint64_t key0 = 0x0706050403020100ULL,
830 std::uint64_t key1 = 0x0f0e0d0c0b0a0908ULL) noexcept
831 {
832 return siphash24_hash(key.data(), key.size(), key0, key1);
833 }
834
835 // ──────────────────────────────────────────────────────────────────────
836 // hash_combine (non-commutative fold used by pair helpers and Aleph_Hash)
837 // ──────────────────────────────────────────────────────────────────────
838
850 inline void hash_combine(size_t & seed, size_t h) noexcept
851 {
852 seed ^= h + size_t{0x9e3779b97f4a7c15ULL} + (seed << 12) + (seed >> 4);
853 }
854
855 // ──────────────────────────────────────────────────────────────────────
856 // Semantic Hash Customization Layer (HASH-005)
857 // ──────────────────────────────────────────────────────────────────────
858
868 template <typename T>
869 concept HashableByADL = requires(const T & t)
870 {
871 { aleph_hash_value(t) } -> std::convertible_to<size_t>;
872 };
873
888 template <typename T>
890 {
891 // Floating-point types are excluded from the raw-byte fallback because equal
892 // values can have different bit patterns (e.g., -0.0 == +0.0, NaN != NaN
893 // but multiple bit representations exist). Implement aleph_hash_value() or
894 // specialize Aleph_Hash<> for float/double.
895 static_assert(
897 (std::is_trivially_copyable_v<T>
898 and not std::is_pointer_v<T>
899 and not std::is_floating_point_v<T>),
900 "Aleph_Hash<T>: T has no aleph_hash_value() in its namespace and cannot "
901 "be safely byte-hashed (pointer, floating-point, or non-trivially-copyable "
902 "type). Define:\n"
903 " size_t aleph_hash_value(const T &) noexcept;\n"
904 "in T's namespace, or specialize Aleph_Hash<T>."
905 );
906
907 [[nodiscard]] size_t operator()(const T & key) const noexcept
908 {
909 if constexpr (HashableByADL<T>)
910 return aleph_hash_value(key);
911 else
912 return wyhash_hash(key);
913 }
914
915 [[nodiscard]] size_t operator()(const T & key, std::uint32_t seed) const noexcept
916 {
917 if constexpr (HashableByADL<T>)
918 {
919 size_t h = aleph_hash_value(key);
920 hash_combine(h, static_cast<size_t>(seed));
921 return h;
922 }
923 else
924 return wyhash_hash(key, seed);
925 }
926 };
927
928 // Explicit specializations for floating-point types.
929 // The raw-byte fallback is excluded for floats because equal values can have
930 // different bit patterns (-0.0 == +0.0, multiple NaN encodings). These
931 // specializations normalize -0.0 to +0.0 before hashing the bits.
932
933 // Note on NaN: distinct NaN bit-patterns hash differently because NaN != NaN
934 // is not handled here — normalization is applied only to -0.0 (mapped to +0.0).
935 // Callers that store NaN keys must canonicalize them before hashing.
936 template <>
938 {
939 [[nodiscard]] size_t operator()(float key) const noexcept
940 {
941 if (key == 0.0f) key = 0.0f; // normalize -0.0 → +0.0
942 std::uint32_t bits = 0;
943 std::memcpy(&bits, &key, sizeof(bits));
944 return SuperFastHash(&bits, sizeof(bits));
945 }
946 [[nodiscard]] size_t operator()(float key, std::uint32_t seed) const noexcept
947 {
948 if (key == 0.0f) key = 0.0f;
949 std::uint32_t bits = 0;
950 std::memcpy(&bits, &key, sizeof(bits));
951 return SuperFastHash(&bits, sizeof(bits), seed);
952 }
953 };
954
955 template <>
957 {
958 [[nodiscard]] size_t operator()(double key) const noexcept
959 {
960 if (key == 0.0) key = 0.0; // normalize -0.0
961 std::uint64_t bits = 0;
962 std::memcpy(&bits, &key, sizeof(bits));
963 return wyhash_hash(bits); // pass by value → wyhash_hash<uint64_t> overload
964 }
965 [[nodiscard]] size_t operator()(double key, std::uint32_t seed) const noexcept
966 {
967 if (key == 0.0) key = 0.0;
968 std::uint64_t bits = 0;
969 std::memcpy(&bits, &key, sizeof(bits));
970 return wyhash_hash(bits, seed);
971 }
972 };
973
974 // ──────────────────────────────────────────────────────────────────────
975 // Default hash functions (route through Aleph_Hash)
976 // ──────────────────────────────────────────────────────────────────────
977
979 static constexpr std::uint32_t Aleph_Snd_Hash_Seed = 0x9e3779b9u;
980
999 template <typename Key>
1000 requires (not std::is_pointer_v<Key> and
1001 not std::is_array_v<Key> and
1002 not std::is_same_v<std::remove_cvref_t<Key>, std::string>)
1003 inline size_t dft_hash_fct(const Key & key) noexcept
1004 {
1005 return Aleph_Hash<Key>{}(key);
1006 }
1007
1008 template <typename Key>
1009 requires std::is_pointer_v<Key>
1010 inline size_t dft_hash_fct(const Key & key) noexcept
1011 {
1012 return SuperFastHash(&key, sizeof(key));
1013 }
1014
1015 inline size_t dft_hash_fct(const char * key) noexcept
1016 {
1017 return wyhash_hash(key);
1018 }
1019
1020 inline size_t dft_hash_fct(const std::string & key) noexcept
1021 {
1022 return wyhash_hash(key);
1023 }
1024
1028 template <typename Key>
1029 requires (not std::is_pointer_v<Key> and
1030 not std::is_array_v<Key> and
1031 not std::is_same_v<std::remove_cvref_t<Key>, std::string>)
1032 inline size_t dft_hash_fct(const Key & key, std::uint32_t seed) noexcept
1033 {
1034 return Aleph_Hash<Key>{}(key, seed);
1035 }
1036
1037 template <typename Key>
1038 requires std::is_pointer_v<Key>
1039 inline size_t dft_hash_fct(const Key & key, std::uint32_t seed) noexcept
1040 {
1041 return SuperFastHash(&key, sizeof(key), seed);
1042 }
1043
1044 inline size_t dft_hash_fct(const char * key, std::uint32_t seed) noexcept
1045 {
1046 return wyhash_hash(key, seed);
1047 }
1048
1049 inline size_t dft_hash_fct(const std::string & key, std::uint32_t seed) noexcept
1050 {
1051 return wyhash_hash(key, seed);
1052 }
1053
1054 template <typename Key>
1055 inline size_t dft_hash_ptr_fct(const Key & key) noexcept
1056 {
1057 return dft_hash_fct(key);
1058 }
1059
1060 template <typename Key>
1061 inline size_t dft_seed_hash_ptr_fct(const Key & key, const unsigned long seed) noexcept
1062 {
1063 return dft_hash_fct(key, static_cast<std::uint32_t>(seed));
1064 }
1065
1077 template <typename Key>
1078 requires (not std::is_pointer_v<Key> and
1079 not std::is_array_v<Key> and
1080 not std::is_same_v<std::remove_cvref_t<Key>, std::string>)
1081 inline size_t snd_hash_fct(const Key & key) noexcept
1082 {
1083 return Aleph_Hash<Key>{}(key, Aleph_Snd_Hash_Seed);
1084 }
1085
1086 template <typename Key>
1087 requires std::is_pointer_v<Key>
1088 inline size_t snd_hash_fct(const Key & key) noexcept
1089 {
1090 return SuperFastHash(&key, sizeof(key), Aleph_Snd_Hash_Seed);
1091 }
1092
1093 inline size_t snd_hash_fct(const char * key) noexcept
1094 {
1095 return wyhash_hash(key, Aleph_Snd_Hash_Seed);
1096 }
1097
1098 inline size_t snd_hash_fct(const std::string & key) noexcept
1099 {
1100 return wyhash_hash(key, Aleph_Snd_Hash_Seed);
1101 }
1102
1103 template <typename Key>
1104 inline size_t snd_hash_ptr_fct(const Key & key) noexcept
1105 {
1106 return snd_hash_fct(key);
1107 }
1108
1109 template<typename Key, typename Data, typename Fct>
1110 inline size_t map_hash_fct(Fct fct, const std::pair<Key, Data> & p) noexcept
1111 {
1112 return fct(p.first);
1113 }
1114
1115 template <typename K1, typename K2>
1116 requires requires(const K1 & k1, const K2 & k2)
1117 {
1120 }
1121 inline size_t pair_dft_hash_fct(const std::pair<K1, K2> & p) noexcept
1122 {
1123 size_t seed = dft_hash_fct(p.first);
1124 hash_combine(seed, dft_hash_fct(p.second));
1125 return seed;
1126 }
1127
1128 template <typename K1, typename K2>
1129 requires requires(const K1 & k1, const K2 & k2)
1130 {
1133 }
1134 inline size_t pair_snd_hash_fct(const std::pair<K1, K2> & p) noexcept
1135 {
1136 size_t seed = dft_hash_fct(p.first);
1137 hash_combine(seed, snd_hash_fct(p.second));
1138 return seed;
1139 }
1140
1148 template <typename K1, typename K2>
1149 inline size_t pair_dft_hash_ptr_fct(const std::pair<K1, K2> & p) noexcept
1150 {
1151 return pair_dft_hash_fct(p);
1152 }
1153
1154 template <typename K1, typename K2>
1155 inline size_t pair_snd_hash_ptr_fct(const std::pair<K1, K2> & p) noexcept
1156 {
1157 return pair_snd_hash_fct(p);
1158 }
1159
1160} // end namespace Aleph
1161
1162
1163# endif // HASH_FCT_H
long double h
Definition btreepic.C:154
Concept: T provides a semantic hash via ADL.
Definition hash-fct.H:869
size_t sax_hash(const void *key, const size_t len) noexcept
Shift-Add-XOR hash (SAX hash)
Definition hash-fct.H:183
size_t SuperFastHash(const void *key, size_t len, std::uint32_t seed=0) noexcept
Paul Hsieh super fast hash function.
Definition hash-fct.H:408
size_t add_hash(const void *key, const size_t len) noexcept
Additive hash.
Definition hash-fct.H:101
size_t jsw_hash(const void *key, size_t len) noexcept
JSW hash (Julienne Walker)
Definition hash-fct.C:247
size_t xxhash64_hash(const void *key, size_t len, std::uint64_t seed) noexcept
xxHash64 from the xxHash family.
Definition hash-fct.C:611
size_t djb_hash(const void *key, const size_t len) noexcept
Bernstein's hash (DJB hash)
Definition hash-fct.H:163
size_t jen_hash(const void *key, size_t length, unsigned initval) noexcept
Jenkins hash (lookup3)
Definition hash-fct.C:325
size_t xor_hash(const void *key, const size_t len) noexcept
XOR hash.
Definition hash-fct.H:122
size_t fnv_hash(const void *key, const size_t len) noexcept
FNV-1a hash.
Definition hash-fct.H:203
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:766
bool is_jsw_initialized() noexcept
Checks if the jsw_hash() lookup table has been initialized.
Definition hash-fct.C:211
void init_jsw() noexcept
Initializes the randomized lookup table used by jsw_hash().
Definition hash-fct.C:219
size_t rot_hash(const void *key, const size_t len) noexcept
Rotating hash.
Definition hash-fct.H:143
size_t wyhash_hash(const void *key, size_t len, std::uint64_t seed) noexcept
wyhash non-cryptographic hash.
Definition hash-fct.C:694
size_t elf_hash(const void *key, const size_t len) noexcept
ELF hash.
Definition hash-fct.H:279
size_t oat_hash(const void *key, const size_t len) noexcept
One-at-a-Time hash (OAT hash)
Definition hash-fct.H:223
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:1110
and
Check uniqueness with explicit hash + equality functors.
size_t pair_snd_hash_fct(const std::pair< K1, K2 > &p) noexcept
Definition hash-fct.H:1134
void MurmurHash3_x86_32(const void *key, int len, uint32_t seed, void *out)
Definition hash-fct.C:372
static constexpr std::uint32_t Aleph_Snd_Hash_Seed
Fixed seed used by the secondary hash function.
Definition hash-fct.H:979
void MurmurHash3_x64_128(const void *key, const int len, const uint32_t seed, void *out)
Definition hash-fct.C:531
size_t snd_hash_fct(const Key &key) noexcept
Secondary default hash: different distribution from dft_hash_fct.
Definition hash-fct.H:1081
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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:1155
size_t dft_hash_fct(const Key &key) noexcept
Primary default hash: best speed/quality trade-off.
Definition hash-fct.H:1003
void hash_combine(size_t &seed, size_t h) noexcept
Non-commutative hash combiner (Boost-style golden-ratio mix).
Definition hash-fct.H:850
size_t snd_hash_ptr_fct(const Key &key) noexcept
Definition hash-fct.H:1104
size_t pair_dft_hash_fct(const std::pair< K1, K2 > &p) noexcept
Definition hash-fct.H:1121
void MurmurHash3_x86_128(const void *key, const int len, uint32_t seed, void *out)
Definition hash-fct.C:427
const unsigned Default_Hash_Seed
Definition hash-fct.C:45
std::uint16_t get16bits(const void *p) noexcept
Definition hash-fct.H:393
size_t pair_dft_hash_ptr_fct(const std::pair< K1, K2 > &p) noexcept
Definition hash-fct.H:1149
size_t murmur3hash(const Key &key, std::uint32_t seed)
Definition hash-fct.H:334
size_t dft_hash_ptr_fct(const Key &key) noexcept
Definition hash-fct.H:1055
size_t dft_seed_hash_ptr_fct(const Key &key, const unsigned long seed) noexcept
Definition hash-fct.H:1061
STL namespace.
size_t operator()(double key) const noexcept
Definition hash-fct.H:958
size_t operator()(double key, std::uint32_t seed) const noexcept
Definition hash-fct.H:965
size_t operator()(float key, std::uint32_t seed) const noexcept
Definition hash-fct.H:946
size_t operator()(float key) const noexcept
Definition hash-fct.H:939
Primary hash functor used internally by dft_hash_fct / snd_hash_fct.
Definition hash-fct.H:890
size_t operator()(const T &key, std::uint32_t seed) const noexcept
Definition hash-fct.H:915
size_t operator()(const T &key) const noexcept
Definition hash-fct.H:907
std::uint32_t a[4]
Definition hash-fct.H:329
ValueArg< size_t > seed
Definition testHash.C:48