frozen/bits/hash_string.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef FROZEN_LETITGO_BITS_HASH_STRING_H | ||
| 2 | #define FROZEN_LETITGO_BITS_HASH_STRING_H | ||
| 3 | |||
| 4 | #include <cstddef> | ||
| 5 | |||
| 6 | namespace frozen { | ||
| 7 | |||
| 8 | template <typename String> | ||
| 9 | constexpr std::size_t hash_string(const String& value) { | ||
| 10 | std::size_t d = 5381; | ||
| 11 | for (const auto& c : value) | ||
| 12 | d = d * 33 + static_cast<std::size_t>(c); | ||
| 13 | return d; | ||
| 14 | } | ||
| 15 | |||
| 16 | // https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function | ||
| 17 | // With the lowest bits removed, based on experimental setup. | ||
| 18 | template <typename String> | ||
| 19 | 98279 | constexpr std::size_t hash_string(const String& value, std::size_t seed) { | |
| 20 | 98279 | std::size_t d = (0x811c9dc5 ^ seed) * static_cast<std::size_t>(0x01000193); | |
| 21 | 593467 | for (const auto& c : value) | |
| 22 | 495188 | d = (d ^ static_cast<std::size_t>(c)) * static_cast<std::size_t>(0x01000193); | |
| 23 | 98279 | return d >> 8 ; | |
| 24 | } | ||
| 25 | |||
| 26 | } // namespace frozen | ||
| 27 | |||
| 28 | #endif // FROZEN_LETITGO_BITS_HASH_STRING_H | ||
| 29 |