From 1292be71b2fa27102bcbef4ed075c18f79bfad88 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 16 Mar 2023 15:11:22 +0200 Subject: [PATCH] BAN: Add FNV hash for strings --- BAN/include/BAN/String.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/BAN/include/BAN/String.h b/BAN/include/BAN/String.h index 0596a0461..4518e6bae 100644 --- a/BAN/include/BAN/String.h +++ b/BAN/include/BAN/String.h @@ -2,6 +2,7 @@ #include #include +#include namespace BAN { @@ -76,6 +77,25 @@ namespace BAN return result; } + template<> + struct hash + { + hash_t operator()(const String& string) const + { + constexpr hash_t FNV_offset_basis = 0x811c9dc5; + constexpr hash_t FNV_prime = 0x01000193; + + hash_t hash = FNV_offset_basis; + for (String::size_type i = 0; i < string.size(); i++) + { + hash *= FNV_prime; + hash ^= (uint8_t)string[i]; + } + + return hash; + } + }; + } namespace BAN::Formatter