-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcache.h
77 lines (65 loc) · 2.02 KB
/
cache.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
#ifndef CASHE_H
#define CASHE_H
template <class T>
int BitSize(T v) {
return sizeof(v) * 8;
}
class Cache {
public:
struct Entry {
unsigned long long board;
float prob;
int depth : 4;
int score : 28;
};
Cache() { Clear(); }
void ShowStats() const {
int loaded_count = 0;
for (int i = 0; i < kSize; ++i) loaded_count += (entries[i].board != 0);
printf("lookups: %10lld hits: %10lld (%4.1f%%)\n", lookup_count,
hit_count, hit_count * 100.0 / lookup_count);
printf("updates: %10lld collisions: %10lld (%4.1f%%)\n", update_count,
collision_count, collision_count * 100.0 / update_count);
printf("entries: %10d loaded: %10d (%4.1f%%)\n", kSize,
loaded_count, loaded_count * 100.0 / kSize);
}
void Clear() {
for (int i = 0; i < kSize; ++i) entries[i].board = 0;
}
bool Lookup(unsigned long long board, float prob, int depth,
int* score) const {
++lookup_count;
unsigned long long hash = Hash(board);
unsigned long long index = hash & (kSize - 1);
const Entry& entry = entries[index];
if (entry.board == board && entry.prob >= prob && entry.depth >= depth) {
++hit_count;
*score = entry.score;
return true;
}
return false;
}
void Update(unsigned long long board, float prob, int depth, int score) {
++update_count;
unsigned long long hash = Hash(board);
unsigned long long index = hash & (kSize - 1);
Entry& entry = entries[index];
collision_count += entry.board != 0;
entry.board = board;
entry.prob = prob;
entry.depth = depth;
entry.score = score;
}
private:
unsigned long long Hash(unsigned long long board) const {
return board + (board >> kBits) + (board >> (kBits * 2));
}
static constexpr int kBits = 22;
static constexpr int kSize = 1 << kBits;
Entry entries[kSize];
mutable long long lookup_count = 0;
mutable long long hit_count = 0;
mutable long long update_count = 0;
mutable long long collision_count = 0;
};
#endif