mirror of
https://github.com/matrix-construct/construct
synced 2024-12-28 00:14:07 +01:00
ircd: Allow configurable prime on the bernstein hasher.
This commit is contained in:
parent
688ff545d6
commit
f7ee7ea211
1 changed files with 19 additions and 13 deletions
|
@ -28,15 +28,16 @@
|
|||
namespace ircd
|
||||
{
|
||||
// constexpr bernstein string hasher suite; these functions will hash the
|
||||
// string at compile time leaving an integer residue at runtime.
|
||||
constexpr size_t hash(const char *const &str, const size_t i = 0);
|
||||
constexpr size_t hash(const char16_t *const &str, const size_t i = 0);
|
||||
constexpr size_t hash(const std::string_view &str, const size_t i = 0);
|
||||
// string at compile time leaving an integer residue at runtime. Decent
|
||||
// primes are at least 7681 and 5381.
|
||||
template<size_t PRIME = 7681> constexpr size_t hash(const char *const &str, const size_t i = 0);
|
||||
template<size_t PRIME = 7681> constexpr size_t hash(const char16_t *const &str, const size_t i = 0);
|
||||
template<size_t PRIME = 7681> constexpr size_t hash(const std::string_view &str, const size_t i = 0);
|
||||
|
||||
// Note that at runtime this hash uses multiplication on every character
|
||||
// which can consume many cycles...
|
||||
size_t hash(const std::string &str, const size_t i = 0);
|
||||
size_t hash(const std::u16string &str, const size_t i = 0);
|
||||
template<size_t PRIME = 7681> size_t hash(const std::string &str, const size_t i = 0);
|
||||
template<size_t PRIME = 7681> size_t hash(const std::u16string &str, const size_t i = 0);
|
||||
|
||||
/// ircd:: reserves the $ character over a string as an alias for hash()
|
||||
template<class string>
|
||||
|
@ -150,41 +151,46 @@ const
|
|||
}
|
||||
|
||||
/// Runtime hashing of a std::u16string (for js). Non-cryptographic.
|
||||
inline size_t
|
||||
template<size_t PRIME>
|
||||
size_t
|
||||
ircd::hash(const std::u16string &str,
|
||||
const size_t i)
|
||||
{
|
||||
return i >= str.size()? 7681ULL : (hash(str, i+1) * 33ULL) ^ str.at(i);
|
||||
return i >= str.size()? PRIME : (hash(str, i+1) * 33ULL) ^ str.at(i);
|
||||
}
|
||||
|
||||
/// Runtime hashing of a std::string. Non-cryptographic.
|
||||
inline size_t
|
||||
template<size_t PRIME>
|
||||
size_t
|
||||
ircd::hash(const std::string &str,
|
||||
const size_t i)
|
||||
{
|
||||
return i >= str.size()? 7681ULL : (hash(str, i+1) * 33ULL) ^ str.at(i);
|
||||
return i >= str.size()? PRIME : (hash(str, i+1) * 33ULL) ^ str.at(i);
|
||||
}
|
||||
|
||||
/// Runtime hashing of a string_view. Non-cryptographic.
|
||||
template<size_t PRIME>
|
||||
constexpr size_t
|
||||
ircd::hash(const std::string_view &str,
|
||||
const size_t i)
|
||||
{
|
||||
return i >= str.size()? 7681ULL : (hash(str, i+1) * 33ULL) ^ str.at(i);
|
||||
return i >= str.size()? PRIME : (hash(str, i+1) * 33ULL) ^ str.at(i);
|
||||
}
|
||||
|
||||
/// Compile-time hashing of a wider string literal (for js). Non-cryptographic.
|
||||
template<size_t PRIME>
|
||||
constexpr size_t
|
||||
ircd::hash(const char16_t *const &str,
|
||||
const size_t i)
|
||||
{
|
||||
return !str[i]? 7681ULL : (hash(str, i+1) * 33ULL) ^ str[i];
|
||||
return !str[i]? PRIME : (hash(str, i+1) * 33ULL) ^ str[i];
|
||||
}
|
||||
|
||||
/// Compile-time hashing of a string literal. Non-cryptographic.
|
||||
template<size_t PRIME>
|
||||
constexpr size_t
|
||||
ircd::hash(const char *const &str,
|
||||
const size_t i)
|
||||
{
|
||||
return !str[i]? 7681ULL : (hash(str, i+1) * 33ULL) ^ str[i];
|
||||
return !str[i]? PRIME : (hash(str, i+1) * 33ULL) ^ str[i];
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue