-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_set.cpp
More file actions
175 lines (128 loc) · 3.38 KB
/
Copy pathstring_set.cpp
File metadata and controls
175 lines (128 loc) · 3.38 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "string_set.hpp"
#include "memory.h"
namespace tl {
static u32 const default_string_set_loglen = 8;
StringSet::StringSet()
: hshift(32 - default_string_set_loglen), keycount(0) {
u32 len = 1 << default_string_set_loglen;
//this->prof_hprobes = 0;
//this->prof_getcount = 0;
this->tab = (Slot *)calloc(len, sizeof(Slot));
}
StringSet::~StringSet() {
free(this->tab);
}
u32 StringSet::count() {
u32 hmask = ((u32)-1) >> this->hshift;
u32 count = 0;
for (u32 p = 0; p <= hmask; ++p) {
if (this->tab[p].h) {
++count;
}
}
return count;
}
#define HASH2IDX(h) ((h) >> hshift)
#define HTAB(x) (tab[x].h)
#define ITAB(x) (tab[x].i)
#define IS_EMPTY() (p_hash == 0)
u32 StringSet::max_dtb() {
u32 hmask = ((u32)-1) >> this->hshift;
u32 max_dist = 0;
for (u32 p = 0; p <= hmask; ++p) {
if (this->tab[p].h) {
u32 p_hash = HTAB(p);
u32 p_dist = (p - HASH2IDX(p_hash)) & hmask;
if (p_dist > max_dist) {
max_dist = p_dist;
}
}
}
return max_dist;
}
static void resize(StringSet* self);
static int insert(
StringSet* self,
u32 hash, u32 str_offset, u8 const* str, u32 len,
u32 hshift, StringSet::Slot* tab,
u32* ret) {
u32 hmask = ((u32)-1) >> hshift;
u32 p = HASH2IDX(hash);
u32 dist = 0;
//++self->prof_getcount;
for (;;) {
u32 p_hash = HTAB(p);
u32 p_dist = (p - HASH2IDX(p_hash)) & hmask;
//++self->prof_hprobes;
if (str && p_hash == hash) {
u32 cand_offset = ITAB(p);
u8 const* cand_ptr = self->source.begin() + cand_offset;
u32 max_cand_len = (u32)(self->source.end() - cand_ptr);
if (max_cand_len >= len && !memcmp(cand_ptr, str, len)) {
// Exists
*ret = cand_offset;
return 1;
}
}
if (IS_EMPTY() || dist > p_dist) {
u32 p_offset = ITAB(p);
HTAB(p) = hash;
ITAB(p) = str_offset;
if (IS_EMPTY()) {
// Slot was empty. We're done.
return 0;
}
// Not empty so we need to push it down
str_offset = p_offset;
dist = p_dist;
hash = p_hash;
str = NULL; // Disable matching. No other entry in the table can be equal to the pushed out entry.
}
p = (p + 1) & hmask;
++dist;
}
}
static void resize(StringSet* self) {
u32 newhshift = self->hshift - 1;
u32 newloglen = 32 - newhshift;
u32 newlen = 1 << newloglen;
StringSet::Slot *newtab = (StringSet::Slot *)calloc(newlen, sizeof(StringSet::Slot));
StringSet::Slot *oldtab = self->tab;
u32 oldhmask = ((u32)-1) >> self->hshift;
for (u32 p = 0; p <= oldhmask; ++p) {
u32 p_hash = oldtab[p].h;
if (!IS_EMPTY()) {
u32 oldi = oldtab[p].i;
// len is not used when str == NULL
insert(self, oldtab[p].h, oldi, NULL, 0, newhshift, newtab, NULL);
}
}
self->tab = newtab;
self->hshift = newhshift;
free(oldtab);
}
bool StringSet::get(u32 hash, u8 const* str, u32 prel_offset, u32 len, u32* ret) {
//u32 prel_offset = (u32)(str - base);
int existing = insert(this, hash, prel_offset, str, len, this->hshift, this->tab, ret);
if (existing) {
return true;
}
u32 hmask = ((u32)-1) >> this->hshift;
if (++this->keycount > hmask - (hmask >> 2)) {
#if 0
double log2 = 1.0 / log(2.0);
u32 dtb = max_dtb();
double k = dtb / (log(this->keycount) * log2);
double k2 = dtb / (log(hmask + 1) * log2);
printf("Max: %d. dtb = %f * log2(keycount). dtb = %f * log2(bucketcount).\n", dtb, k, k2);
#endif
resize(this);
}
*ret = prel_offset;
return false;
}
#undef HASH2IDX
#undef ITAB
#undef HTAB
#undef IS_EMPTY
}