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