summaryrefslogtreecommitdiffstats
path: root/include/frozen/bits/hash_string.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/frozen/bits/hash_string.h')
-rw-r--r--include/frozen/bits/hash_string.h28
1 files changed, 28 insertions, 0 deletions
diff --git a/include/frozen/bits/hash_string.h b/include/frozen/bits/hash_string.h
new file mode 100644
index 0000000..da8fc51
--- /dev/null
+++ b/include/frozen/bits/hash_string.h
@@ -0,0 +1,28 @@
+#ifndef FROZEN_LETITGO_BITS_HASH_STRING_H
+#define FROZEN_LETITGO_BITS_HASH_STRING_H
+
+#include <cstddef>
+
+namespace frozen {
+
+template <typename String>
+constexpr std::size_t hash_string(const String& value) {
+ std::size_t d = 5381;
+ for (const auto& c : value)
+ d = d * 33 + static_cast<size_t>(c);
+ return d;
+}
+
+// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
+// With the lowest bits removed, based on experimental setup.
+template <typename String>
+constexpr std::size_t hash_string(const String& value, std::size_t seed) {
+ std::size_t d = (0x811c9dc5 ^ seed) * static_cast<size_t>(0x01000193);
+ for (const auto& c : value)
+ d = (d ^ static_cast<size_t>(c)) * static_cast<size_t>(0x01000193);
+ return d >> 8 ;
+}
+
+} // namespace frozen
+
+#endif // FROZEN_LETITGO_BITS_HASH_STRING_H \ No newline at end of file