-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhash.h
150 lines (123 loc) · 3.73 KB
/
hash.h
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*!
\file hash.h
Describes hash functions used in this project.
*/
#pragma once
#include <functional>
#include <type_traits>
#include <cinttypes>
#include <string>
#include <iostream>
#include "farmhash/farmhash.h"
#include "utils/hashutil.h"
template<class K>
class Hasher32 {
public:
uint32_t s; //!< hash s.
Hasher32()
: s(0xe2211) {
}
explicit Hasher32(uint32_t _s)
: s(_s) {
}
//! set bitmask and s
void setSeed(uint32_t _s) {
s = _s;
}
template<class K1>
inline typename std::enable_if<!std::is_same<K1, std::string>::value, uint64_t *>::type
getBase(const K &k0) const {
uint64_t *base;
return (uint64_t *) &k0;
}
template<class K1>
inline typename std::enable_if<std::is_same<K1, std::string>::value, uint64_t *>::type
getBase(const K &k0) const {
uint64_t *base;
return (uint64_t *) &k0[0];
}
template<class K1>
inline typename std::enable_if<!std::is_same<K1, std::string>::value, uint16_t>::type
getKeyByteLength(const K &k0) const {
return sizeof(K);
}
template<class K1>
inline typename std::enable_if<std::is_same<K1, std::string>::value, uint16_t>::type
getKeyByteLength(const K &k0) const {
return k0.length();
}
inline uint32_t operator()(const K &k0) const {
static_assert(sizeof(K) <= 32, "K length should be 32/64/96/128/160/192/224/256 bits");
uint64_t *base = getBase<K>(k0);
const uint16_t keyByteLength = getKeyByteLength<K>(k0);
return farmhash::Hash32WithSeed((char *) base, (size_t) keyByteLength, s);
// return XXH32((void*) base, keyByteLength, s);
}
};
//! \brief A hash function that hashes keyType to uint32_t. When SSE4.2 support is found, use sse4.2 instructions, otherwise use default hash function std::hash.
template<class K>
class Hasher64 {
public:
uint64_t s; //!< hash s.
Hasher64()
: s(0xe2211e2211) {
}
explicit Hasher64(const Hasher64 &h)
: s(h.s) {
}
explicit Hasher64(uint64_t _s)
: s(_s) {
}
//! set bitmask and s
void setSeed(uint64_t _s) {
s = _s;
}
template<class K1>
inline typename std::enable_if<!std::is_same<K1, std::string>::value, uint64_t *>::type
getBase(const K &k0) const {
uint64_t *base;
return (uint64_t *) &k0;
}
template<class K1>
inline typename std::enable_if<std::is_same<K1, std::string>::value, uint64_t *>::type
getBase(const K &k0) const {
uint64_t *base;
return (uint64_t *) &k0[0];
}
template<class K1>
inline typename std::enable_if<!std::is_same<K1, std::string>::value, uint16_t>::type
getKeyByteLength(const K &k0) const {
return sizeof(K);
}
template<class K1>
inline typename std::enable_if<std::is_same<K1, std::string>::value, uint16_t>::type
getKeyByteLength(const K &k0) const {
return k0.length();
}
inline uint64_t operator()(const K &k0) const {
uint64_t *base = getBase<K>(k0);
const uint16_t keyByteLength = getKeyByteLength<K>(k0);
return farmhash::Hash64WithSeed((char *) base, (size_t) keyByteLength, s);
// return XXH64((void *) base, keyByteLength, s);
}
};
//! \brief A hash function that hashes keyType to uint32_t. When SSE4.2 support is found, use sse4.2 instructions, otherwise use default hash function std::hash.
template<class K>
class FastHasher64 : public Hasher64<K> {
public :
public:
FastHasher64()
: Hasher64<K>(0xe2211e2211) {
}
explicit FastHasher64(uint64_t _s)
: Hasher64<K>(_s) {
}
inline uint64_t operator()(const K &k0) const {
void *base = this -> template getBase<K>(k0);
const uint16_t keyByteLength = this -> template getKeyByteLength<K>(k0);
uint32_t h[2];
*(uint64_t*)h = this->s;
HashUtil::BobHash(base, keyByteLength, h, h+1);
return *(uint64_t*)h;
}
};