cpp

Coverage Report

Created: 2024-06-09 06:28

/home/uke/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
264
unsigned fnv1(const char* data, int len) {
7
  // FNV-1 from http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1
8
264
  unsigned h = 2166136261;     // 32-bit FNV-1 offset basis
9
264
  constexpr int p = 16777619;  // 32-bit FNV-1 prime
10
3.58k
  for (int i = 0; i < len; i++) {
11
3.32k
    h *= p;
12
    // log("1. h = %d", h);
13
3.32k
    h ^= data[i];
14
    // log("2. h = %d", h);
15
3.32k
  }
16
264
  return h;
17
264
}
18
19
752
unsigned hash_key(BigStr* s) {
20
752
  return s->hash(fnv1);
21
752
}
22
23
65.9k
unsigned hash_key(int n) {
24
65.9k
  return n;
25
65.9k
}
26
27
12
unsigned hash_key(Tuple2<int, int>* t1) {
28
12
  return t1->at0() + t1->at1();
29
12
}
30
31
12
unsigned hash_key(Tuple2<BigStr*, int>* t1) {
32
12
  return t1->at0()->hash(fnv1) + t1->at1();
33
12
}
34
35
// e.g. for Dict<Token*, int>, hash the pointer itself, which means we use
36
// object IDENTITY, not value.
37
6
unsigned hash_key(void* p) {
38
6
  return fnv1(reinterpret_cast<const char*>(&p), sizeof(void*));
39
6
}