-
Notifications
You must be signed in to change notification settings - Fork 89
/
hash.hpp
76 lines (58 loc) · 1.67 KB
/
hash.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef BF_HASH_POLICY_HPP
#define BF_HASH_POLICY_HPP
#include <functional>
#include <bf/h3.hpp>
#include <bf/object.hpp>
namespace bf {
/// The hash digest type.
typedef size_t digest;
/// The hash function type.
typedef std::function<digest(object const&)> hash_function;
/// A function that hashes an object *k* times.
typedef std::function<std::vector<digest>(object const&)> hasher;
class default_hash_function
{
public:
constexpr static size_t max_obj_size = 36;
default_hash_function(size_t seed);
size_t operator()(object const& o) const;
private:
h3<size_t, max_obj_size> h3_;
};
/// A hasher which hashes an object *k* times.
class default_hasher
{
public:
default_hasher(std::vector<hash_function> fns);
std::vector<digest> operator()(object const& o) const;
private:
std::vector<hash_function> fns_;
};
/// A hasher which hashes an object two times and generates *k* digests through
/// a linear combinations of the two digests.
class double_hasher
{
public:
double_hasher(size_t k, hash_function h1, hash_function h2);
std::vector<digest> operator()(object const& o) const;
private:
size_t k_;
hash_function h1_;
hash_function h2_;
};
/// Creates a default or double hasher with the default hash function, using
/// seeds from a linear congruential PRNG.
///
/// @param k The number of hash functions to use.
///
/// @param seed The initial seed of the PRNG.
///
/// @param double_hashing If `true`, the function constructs a ::double_hasher
/// and a ::default_hasher otherwise.
///
/// @return A ::hasher with the *k* hash functions.
///
/// @pre `k > 0`
hasher make_hasher(size_t k, size_t seed = 0, bool double_hashing = false);
} // namespace bf
#endif