1 | #include "mycpp/hash.h"
|
2 |
|
3 | #include "mycpp/gc_str.h"
|
4 | #include "mycpp/gc_tuple.h"
|
5 |
|
6 | unsigned fnv1(const char* data, int len) {
|
7 | // FNV-1 from http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1
|
8 | unsigned h = 2166136261; // 32-bit FNV-1 offset basis
|
9 | constexpr int p = 16777619; // 32-bit FNV-1 prime
|
10 | for (int i = 0; i < len; i++) {
|
11 | h *= data[i];
|
12 | h ^= p;
|
13 | }
|
14 | return h;
|
15 | }
|
16 |
|
17 | unsigned hash_key(BigStr* s) {
|
18 | return s->hash(fnv1);
|
19 | }
|
20 |
|
21 | unsigned hash_key(int n) {
|
22 | return n;
|
23 | }
|
24 |
|
25 | unsigned hash_key(Tuple2<int, int>* t1) {
|
26 | return t1->at0() + t1->at1();
|
27 | }
|
28 |
|
29 | unsigned hash_key(Tuple2<BigStr*, int>* t1) {
|
30 | return t1->at0()->hash(fnv1) + t1->at1();
|
31 | }
|