From 3c57e05a659bad9103f5c1ae51f55de14be394e9 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 25 Jun 2024 19:28:45 +0300 Subject: [PATCH] BAN: Implement hash for StringView --- BAN/include/BAN/StringView.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/BAN/include/BAN/StringView.h b/BAN/include/BAN/StringView.h index 47e0fcab..40f8d850 100644 --- a/BAN/include/BAN/StringView.h +++ b/BAN/include/BAN/StringView.h @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -222,6 +223,25 @@ namespace BAN size_type m_size = 0; }; + template<> + struct hash + { + hash_t operator()(StringView string) const + { + constexpr hash_t FNV_offset_basis = 0x811c9dc5; + constexpr hash_t FNV_prime = 0x01000193; + + hash_t hash = FNV_offset_basis; + for (StringView::size_type i = 0; i < string.size(); i++) + { + hash *= FNV_prime; + hash ^= (uint8_t)string[i]; + } + + return hash; + } + }; + } inline constexpr BAN::StringView operator""_sv(const char* str, BAN::StringView::size_type len) { return BAN::StringView(str, len); }