diff options
| author | Maxime Coste <mawww@kakoune.org> | 2024-02-06 21:57:17 +1100 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2024-02-06 21:57:17 +1100 |
| commit | 04a96b059faac8100a291e56bfbdb1962d53d4e1 (patch) | |
| tree | 82191cd45870634637c4a7ee0f437d5fd52111e3 /src/hash.hh | |
| parent | 53d9b9b67650a2b34345d9153bef2a01cb75c418 (diff) | |
Use different hash algorithms for strings and file hashing
For hash map, using fnv1a is faster as it is a much simpler algorithm
we can afford to inline. For files murmur3 should win as it processes
bytes 4 by 4.
Diffstat (limited to 'src/hash.hh')
| -rw-r--r-- | src/hash.hh | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/src/hash.hh b/src/hash.hh index 8dcd719f..deed78f2 100644 --- a/src/hash.hh +++ b/src/hash.hh @@ -5,11 +5,23 @@ #include <utility> #include <cstddef> +#include <cstdint> namespace Kakoune { -size_t hash_data(const char* data, size_t len); +inline size_t fnv1a(const char* data, size_t len) +{ + constexpr uint32_t FNV_prime_32 = 16777619; + constexpr uint32_t offset_basis_32 = 2166136261; + + uint32_t hash_value = offset_basis_32; + for (size_t i = 0; i < len; ++i) + hash_value = (hash_value ^ data[i]) * FNV_prime_32; + return hash_value; +} + +size_t murmur3(const char* input, size_t len); template<typename Type> requires std::is_integral_v<Type> constexpr size_t hash_value(const Type& val) |
