2018-11-09 02:04:15 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_UTIL_HASH_H
|
|
|
|
|
2020-12-11 22:33:15 +01:00
|
|
|
// constexpr bernstein string hasher suite; these functions will hash the
|
|
|
|
// string at compile time leaving an integer residue at runtime. Decent
|
|
|
|
// primes are at least 7681 and 5381.
|
|
|
|
|
2019-06-23 10:01:26 +02:00
|
|
|
namespace ircd {
|
|
|
|
inline namespace util
|
2018-11-09 02:04:15 +01:00
|
|
|
{
|
2020-12-11 22:33:15 +01:00
|
|
|
template<size_t PRIME = 7681>
|
|
|
|
constexpr size_t hash(const string_view str, size_t i = 0, size_t r = PRIME) noexcept;
|
|
|
|
|
|
|
|
template<size_t PRIME = 7681>
|
|
|
|
constexpr size_t hash(const char16_t *const str, size_t i = 0, size_t r = PRIME) noexcept;
|
2018-11-09 02:04:15 +01:00
|
|
|
|
|
|
|
// Note that at runtime this hash uses multiplication on every character
|
|
|
|
// which can consume many cycles...
|
2020-12-11 22:33:15 +01:00
|
|
|
template<size_t PRIME = 7681>
|
|
|
|
size_t hash(const std::u16string &str, size_t i = 0, size_t r = PRIME) noexcept;
|
2019-06-23 10:01:26 +02:00
|
|
|
}}
|
2018-11-09 02:04:15 +01:00
|
|
|
|
|
|
|
/// Runtime hashing of a std::u16string (for js). Non-cryptographic.
|
|
|
|
template<size_t PRIME>
|
2019-08-06 01:19:55 +02:00
|
|
|
[[gnu::pure]]
|
2020-12-11 22:33:15 +01:00
|
|
|
inline size_t
|
2018-11-09 02:04:15 +01:00
|
|
|
ircd::util::hash(const std::u16string &str,
|
2020-12-11 22:33:15 +01:00
|
|
|
size_t i,
|
|
|
|
size_t r)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-11-09 02:04:15 +01:00
|
|
|
{
|
2020-12-11 22:33:15 +01:00
|
|
|
for(; i < str.size(); ++i)
|
|
|
|
r = str[i] ^ (r * 33ULL);
|
|
|
|
|
|
|
|
return r;
|
2018-11-09 02:04:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Runtime hashing of a string_view. Non-cryptographic.
|
|
|
|
template<size_t PRIME>
|
2019-08-06 01:19:55 +02:00
|
|
|
[[gnu::pure]]
|
2018-11-09 02:04:15 +01:00
|
|
|
constexpr size_t
|
|
|
|
ircd::util::hash(const string_view str,
|
2020-12-11 22:33:15 +01:00
|
|
|
size_t i,
|
|
|
|
size_t r)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-11-09 02:04:15 +01:00
|
|
|
{
|
2020-12-11 22:33:15 +01:00
|
|
|
for(; i < str.size(); ++i)
|
|
|
|
r = str[i] ^ (r * 33ULL);
|
|
|
|
|
|
|
|
return r;
|
2018-11-09 02:04:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Compile-time hashing of a wider string literal (for js). Non-cryptographic.
|
|
|
|
template<size_t PRIME>
|
2019-08-06 01:19:55 +02:00
|
|
|
[[gnu::pure]]
|
2018-11-09 02:04:15 +01:00
|
|
|
constexpr size_t
|
|
|
|
ircd::util::hash(const char16_t *const str,
|
2020-12-11 22:33:15 +01:00
|
|
|
size_t i,
|
|
|
|
size_t r)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-11-09 02:04:15 +01:00
|
|
|
{
|
2020-12-11 22:33:15 +01:00
|
|
|
for(assert(str); str[i]; ++i)
|
|
|
|
r = str[i] ^ (r * 33ULL);
|
|
|
|
|
|
|
|
return r;
|
2018-11-09 02:04:15 +01:00
|
|
|
}
|