-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict.hpp
105 lines (92 loc) · 2.5 KB
/
dict.hpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// In order to utilize our allocator
template <typename SubClass, typename Key, typename Val,
typename HashFunc, typename EqFunc, typename AllocFunc>
class Dict {
public:
void insert(Key key, Val val) {
bool found;
intptr_t hash = HashFunc(key);
Entry *entry = lookupEntry(key, hash, &found);
if (!found) {
// Rehash when 1/4 full
if ((occupied << 2) > buckets) {
static_cast<SubClass *>(this)->rehash(occupied << 2);
insert(key, val);
return;
}
++occupied;
entry->status = filled;
entry->hash = hash;
}
entry->val = val;
}
Val lookup(Key key, bool *found) {
intptr_t hash = HashFunc(key);
Entry *entry = lookupEntry(key, hash, found);
return entry->val;
}
Key lookupKey(Key key, bool *found) {
intptr_t hash = HashFunc(key);
Entry *entry = lookupEntry(key, hash, found);
return entry->key;
}
void remove(Key key, bool *found) {
intptr_t hash = HashFunc(key);
Entry *entry = lookupEntry(key, hash, found);
if (*found) {
}
return entry->val;
}
private:
// Copied from `c++/4.5/backward/hashtable'
enum { kNumBuckets = 29 };
static const uintptr_t bucketList[kNumBuckets] = {
5ul, 53ul, 97ul, 193ul, 389ul,
769ul, 1543ul, 3079ul, 6151ul, 12289ul,
24593ul, 49157ul, 98317ul, 196613ul, 393241ul,
786433ul, 1572869ul, 3145739ul, 6291469ul, 12582917ul,
25165843ul, 50331653ul, 100663319ul, 201326611ul, 402653189ul,
805306457ul, 1610612741ul, 3221225473ul, 4294967291ul
};
enum EntryStatus {
Empty,
Filled,
TombStone
};
struct Entry {
Key key;
intptr_t hash;
Val val;
EntryStatus status;
};
Entry *lookupEntry(Key key, intptr_t hash, bool *found) {
Entry *lastTomb = NULL;
for (intptr_t i = 0; i < buckets; ++i) {
intptr_t ix = (hash + i * i) % buckets;
Entry *got = items + ix;
if (got->status == Empty) {
if (found) {
*found = false;
}
return got;
}
else if (got->status == TombStone) {
lastTomb = lastTomb ? lastTomb : got;
}
else if (got->status == Filled && got->hash == hash &&
EqFunc(got->key, key)) {
if (found) {
*found = true;
}
return got;
}
}
if (found) {
*found = false;
}
return lastTomb;
}
intptr_t buckets;
intptr_t occupied;
Entry *items;
};