LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
hash_string.h
Go to the documentation of this file.
1#ifndef FROZEN_LETITGO_BITS_HASH_STRING_H
2#define FROZEN_LETITGO_BITS_HASH_STRING_H
3
4#include <cstddef>
5
6namespace frozen {
7
8template <typename String>
9constexpr 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.
18template <typename String>
19constexpr std::size_t hash_string(const String& value, std::size_t seed) {
20 std::size_t d = (0x811c9dc5 ^ seed) * static_cast<std::size_t>(0x01000193);
21 for (const auto& c : value)
22 d = (d ^ static_cast<std::size_t>(c)) * static_cast<std::size_t>(0x01000193);
23 return d >> 8 ;
24}
25
26} // namespace frozen
27
28#endif // FROZEN_LETITGO_BITS_HASH_STRING_H
Definition algorithm.h:30
constexpr std::size_t hash_string(const String &value)
Definition hash_string.h:9