| 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 |
|
98063 |
constexpr std::size_t hash_string(const String& value, std::size_t seed) { |
| 20 |
|
98063 |
std::size_t d = (0x811c9dc5 ^ seed) * static_cast<std::size_t>(0x01000193); |
| 21 |
4/4
✓ Branch 0 taken 245557 times.
✓ Branch 1 taken 48610 times.
✓ Branch 2 taken 248635 times.
✓ Branch 3 taken 49453 times.
|
592255 |
for (const auto& c : value) |
| 22 |
|
494192 |
d = (d ^ static_cast<std::size_t>(c)) * static_cast<std::size_t>(0x01000193); |
| 23 |
|
98063 |
return d >> 8 ; |
| 24 |
|
|
} |
| 25 |
|
|
|
| 26 |
|
|
} // namespace frozen |
| 27 |
|
|
|
| 28 |
|
|
#endif // FROZEN_LETITGO_BITS_HASH_STRING_H |
| 29 |
|
|
|