/home/uke/oil/mycpp/hash.cc
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | #include "mycpp/hash.h"  | 
2  |  |  | 
3  |  | #include "mycpp/gc_str.h"  | 
4  |  | #include "mycpp/gc_tuple.h"  | 
5  |  |  | 
6  | 77  | unsigned fnv1(const char* data, int len) { | 
7  |  |   // FNV-1 from http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1  | 
8  | 77  |   unsigned h = 2166136261;     // 32-bit FNV-1 offset basis  | 
9  | 77  |   constexpr int p = 16777619;  // 32-bit FNV-1 prime  | 
10  | 563  |   for (int i = 0; i < len; i++) { | 
11  | 486  |     h *= p;  | 
12  |  |     // log("1. h = %d", h); | 
13  | 486  |     h ^= data[i];  | 
14  |  |     // log("2. h = %d", h); | 
15  | 486  |   }  | 
16  | 77  |   return h;  | 
17  | 77  | }  | 
18  |  |  | 
19  | 236  | unsigned hash_key(BigStr* s) { | 
20  | 236  |   return s->hash(fnv1);  | 
21  | 236  | }  | 
22  |  |  | 
23  | 0  | unsigned hash_key(int n) { | 
24  | 0  |   return fnv1(reinterpret_cast<const char*>(&n), sizeof(n));  | 
25  | 0  | }  | 
26  |  |  | 
27  | 0  | unsigned hash_key(mops::BigInt n) { | 
28  |  |   // Bug fix: our dict sizing is a power of 2, and we don't want integers in  | 
29  |  |   // the workload to interact badly with it.  | 
30  | 0  |   return fnv1(reinterpret_cast<const char*>(&n), sizeof(n));  | 
31  | 0  | }  | 
32  |  |  | 
33  | 0  | unsigned hash_key(void* p) { | 
34  |  |   // e.g. for Dict<Token*, int>, hash the pointer itself, which means we use  | 
35  |  |   // object IDENTITY, not value.  | 
36  | 0  |   return fnv1(reinterpret_cast<const char*>(&p), sizeof(void*));  | 
37  | 0  | }  | 
38  |  |  | 
39  | 0  | unsigned hash_key(Tuple2<int, int>* t1) { | 
40  | 0  |   return t1->at0() + t1->at1();  | 
41  | 0  | }  | 
42  |  |  | 
43  | 0  | unsigned hash_key(Tuple2<BigStr*, int>* t1) { | 
44  | 0  |   return t1->at0()->hash(fnv1) + t1->at1();  | 
45  | 0  | }  |