8#ifndef __BOXES_HASH_UTILS_HPP__
9#define __BOXES_HASH_UTILS_HPP__
11#include "compiler.hpp"
20namespace boxes::hash {
34concept Hash64 =
requires(T t,
const uint8_t *data, std::size_t len) {
36 { t.final() } -> std::same_as<uint64_t>;
58 requires(T t, std::size_t i,
const uint8_t *data, std::size_t len) {
60 { t.final(i) } -> std::same_as<uint64_t>;
62 { t.size() } -> std::same_as<std::size_t>;
78template <
typename HashFamilyT,
typename T>
79uint64_t
hashType64(HashFamilyT &h, std::size_t idx,
const T &t) {
80 h(idx,
reinterpret_cast<const uint8_t *
>(&t),
sizeof(T));
95template <
typename HashFamilyT>
96uint64_t
hashType64(HashFamilyT &h, std::size_t idx,
const std::string &s) {
97 h(idx,
reinterpret_cast<const uint8_t *
>(s.data()), s.size());
111template <
typename HashFamilyT,
typename T>
112uint64_t
hashType64(HashFamilyT &h, std::size_t idx,
const std::vector<T> &v) {
113 h(
reinterpret_cast<const uint8_t *
>(v.data()), v.size() *
sizeof(T));
126template <
typename HashFamilyT>
128 const std::vector<std::string> &v) {
129 for (
const auto &s : v) {
130 h(
reinterpret_cast<const uint8_t *
>(s.data()), s.size());
143 std::random_device rd;
144 std::mt19937 gen(rd());
145 std::uniform_int_distribution<uint64_t> dis;
173 : h1{std::move(h1)}, h2{std::move(h2)}, k{k} {}
180 void operator()(std::size_t idx,
const uint8_t *data, std::size_t len) {
187 uint64_t
final(std::size_t i)
const {
return h1.final() + i * h2.final(); }
189 std::size_t size() const BOXES_NOTHROW {
return k; }
212 std::random_device rd;
213 std::mt19937 gen(rd());
214 std::uniform_int_distribution<uint64_t> dis;
215 std::set<uint64_t> seeds;
217 for (std::size_t i = 0; i < n; ++i) {
222 }
while (!seeds.insert(seed).second);
223 hashes.emplace_back(dis(gen));
228 for (
auto &h : hashes) {
233 std::size_t size() const BOXES_NOTHROW {
return hashes.size(); }
235 void operator()(std::size_t idx,
const uint8_t *data, std::size_t len) {
236 hashes.at(idx)(data, len);
239 uint64_t
final(std::size_t i)
const {
return hashes.at(i).final(); }
242 std::vector<HashT> hashes;
253template <Hash64 H1, Hash64 H2>
Abstracts a hash family functor that requires a seed n times with different seed.
HashWithSeedFamily(std::size_t n)
Defines a hash family functor that uses a given hash functor requiring a seed and instantiates it n t...
Abstracts a Kirsch-Mitzenmacher hash family functor.
KirschMitzenmacher(H1 h1, H2 h2, std::size_t k)
Abstracts a Kirsch-Mitzenmacher hash family functor.
Defines a minimal interface for Hashing functor.
Defines a minimal interface for HashFamily functor.
KirschMitzenmacher< H1, H2 > makeKirschMitzenmacher(H1 h1, H2 h2, std::size_t k)
Helper function to instantiate a Kirsch-Mitzenmacher hash family.
HT makeHashWithRandomSeed()
Instantiates a hash function that requires a seed with a randomly generated seed.
uint64_t hashType64(HashFamilyT &h, std::size_t idx, const T &t)
Applies a given hash family to a type T