0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-01-15 17:16:49 +01:00

ircd: Allow configurable prime on the bernstein hasher.

This commit is contained in:
Jason Volk 2017-11-06 12:20:30 -08:00
parent 688ff545d6
commit f7ee7ea211

View file

@ -28,15 +28,16 @@
namespace ircd namespace ircd
{ {
// constexpr bernstein string hasher suite; these functions will hash the // constexpr bernstein string hasher suite; these functions will hash the
// string at compile time leaving an integer residue at runtime. // string at compile time leaving an integer residue at runtime. Decent
constexpr size_t hash(const char *const &str, const size_t i = 0); // primes are at least 7681 and 5381.
constexpr size_t hash(const char16_t *const &str, const size_t i = 0); template<size_t PRIME = 7681> constexpr size_t hash(const char *const &str, const size_t i = 0);
constexpr size_t hash(const std::string_view &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 // Note that at runtime this hash uses multiplication on every character
// which can consume many cycles... // which can consume many cycles...
size_t hash(const std::string &str, const size_t i = 0); template<size_t PRIME = 7681> 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::u16string &str, const size_t i = 0);
/// ircd:: reserves the $ character over a string as an alias for hash() /// ircd:: reserves the $ character over a string as an alias for hash()
template<class string> template<class string>
@ -150,41 +151,46 @@ const
} }
/// Runtime hashing of a std::u16string (for js). Non-cryptographic. /// 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, ircd::hash(const std::u16string &str,
const size_t i) 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. /// Runtime hashing of a std::string. Non-cryptographic.
inline size_t template<size_t PRIME>
size_t
ircd::hash(const std::string &str, ircd::hash(const std::string &str,
const size_t i) 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. /// Runtime hashing of a string_view. Non-cryptographic.
template<size_t PRIME>
constexpr size_t constexpr size_t
ircd::hash(const std::string_view &str, ircd::hash(const std::string_view &str,
const size_t i) 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. /// Compile-time hashing of a wider string literal (for js). Non-cryptographic.
template<size_t PRIME>
constexpr size_t constexpr size_t
ircd::hash(const char16_t *const &str, ircd::hash(const char16_t *const &str,
const size_t i) 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. /// Compile-time hashing of a string literal. Non-cryptographic.
template<size_t PRIME>
constexpr size_t constexpr size_t
ircd::hash(const char *const &str, ircd::hash(const char *const &str,
const size_t i) 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];
} }