BAN: Add FNV hash for strings

This commit is contained in:
Bananymous 2023-03-16 15:11:22 +02:00
parent 1b7625581d
commit 1292be71b2
1 changed files with 20 additions and 0 deletions

View File

@ -2,6 +2,7 @@
#include <BAN/ForwardList.h> #include <BAN/ForwardList.h>
#include <BAN/Formatter.h> #include <BAN/Formatter.h>
#include <BAN/Hash.h>
namespace BAN namespace BAN
{ {
@ -76,6 +77,25 @@ namespace BAN
return result; return result;
} }
template<>
struct hash<String>
{
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 namespace BAN::Formatter