-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
stringtable.d
268 lines (245 loc) · 7.41 KB
/
stringtable.d
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Compiler implementation of the D programming language
// Copyright (c) 1999-2015 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
module ddmd.root.stringtable;
import core.stdc.string;
import ddmd.root.rmem;
enum POOL_BITS = 12;
enum POOL_SIZE = (1U << POOL_BITS);
// TODO: Merge with root.String
// MurmurHash2 was written by Austin Appleby, and is placed in the public
// domain. The author hereby disclaims copyright to this source code.
// https://sites.google.com/site/murmurhash/
private uint calcHash(const(char)* key, size_t len) pure nothrow @nogc
{
// 'm' and 'r' are mixing constants generated offline.
// They're not really 'magic', they just happen to work well.
enum uint m = 0x5bd1e995;
enum int r = 24;
// Initialize the hash to a 'random' value
uint h = cast(uint)len;
// Mix 4 bytes at a time into the hash
const(ubyte)* data = cast(const(ubyte)*)key;
while (len >= 4)
{
uint k = data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0];
k *= m;
k ^= k >> r;
h = (h * m) ^ (k * m);
data += 4;
len -= 4;
}
// Handle the last few bytes of the input array
switch (len & 3)
{
case 3:
h ^= data[2] << 16;
case 2:
h ^= data[1] << 8;
case 1:
h ^= data[0];
h *= m;
default:
break;
}
// Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated.
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
private size_t nextpow2(size_t val) pure nothrow @nogc @safe
{
size_t res = 1;
while (res < val)
res <<= 1;
return res;
}
enum loadFactor = 0.8;
struct StringEntry
{
uint hash;
uint vptr;
}
// StringValue is a variable-length structure. It has neither proper c'tors nor a
// factory method because the only thing which should be creating these is StringTable.
struct StringValue
{
void* ptrvalue;
size_t length;
extern (C++) char* lstring()
{
return cast(char*)(&this + 1);
}
extern (C++) size_t len() const
{
return length;
}
extern (C++) const(char)* toDchars() const
{
return cast(const(char)*)(&this + 1);
}
}
struct StringTable
{
private:
StringEntry* table;
size_t tabledim;
ubyte** pools;
size_t npools;
size_t nfill;
size_t count;
public:
extern (C++) void _init(size_t size = 0)
{
size = nextpow2(cast(size_t)(size / loadFactor));
if (size < 32)
size = 32;
table = cast(StringEntry*)mem.xcalloc(size, (table[0]).sizeof);
tabledim = size;
pools = null;
npools = nfill = 0;
count = 0;
}
extern (C++) void reset(size_t size = 0)
{
for (size_t i = 0; i < npools; ++i)
mem.xfree(pools[i]);
mem.xfree(table);
mem.xfree(pools);
table = null;
pools = null;
_init(size);
}
extern (C++) ~this()
{
for (size_t i = 0; i < npools; ++i)
mem.xfree(pools[i]);
mem.xfree(table);
mem.xfree(pools);
table = null;
pools = null;
}
extern (C++) StringValue* lookup(const(char)* s, size_t length)
{
const(hash_t) hash = calcHash(s, length);
const(size_t) i = findSlot(hash, s, length);
// printf("lookup %.*s %p\n", (int)length, s, table[i].value ?: NULL);
return getValue(table[i].vptr);
}
extern (C++) StringValue* insert(const(char)* s, size_t length)
{
const(hash_t) hash = calcHash(s, length);
size_t i = findSlot(hash, s, length);
if (table[i].vptr)
return null; // already in table
if (++count > tabledim * loadFactor)
{
grow();
i = findSlot(hash, s, length);
}
table[i].hash = hash;
table[i].vptr = allocValue(s, length);
// printf("insert %.*s %p\n", (int)length, s, table[i].value ?: NULL);
return getValue(table[i].vptr);
}
extern (C++) StringValue* update(const(char)* s, size_t length)
{
const(hash_t) hash = calcHash(s, length);
size_t i = findSlot(hash, s, length);
if (!table[i].vptr)
{
if (++count > tabledim * loadFactor)
{
grow();
i = findSlot(hash, s, length);
}
table[i].hash = hash;
table[i].vptr = allocValue(s, length);
}
// printf("update %.*s %p\n", (int)length, s, table[i].value ?: NULL);
return getValue(table[i].vptr);
}
/********************************
* Walk the contents of the string table,
* calling fp for each entry.
* Params:
* fp = function to call. Returns !=0 to stop
* Returns:
* last return value of fp call
*/
extern (C++) int apply(int function(StringValue*) fp)
{
for (size_t i = 0; i < tabledim; ++i)
{
StringEntry* se = &table[i];
if (!se.vptr)
continue;
StringValue* sv = getValue(se.vptr);
int result = (*fp)(sv);
if (result)
return result;
}
return 0;
}
private:
extern (C++) uint allocValue(const(char)* s, size_t length)
{
const(size_t) nbytes = StringValue.sizeof + length + 1;
if (!npools || nfill + nbytes > POOL_SIZE)
{
pools = cast(ubyte**)mem.xrealloc(pools, ++npools * (pools[0]).sizeof);
pools[npools - 1] = cast(ubyte*)mem.xmalloc(nbytes > POOL_SIZE ? nbytes : POOL_SIZE);
nfill = 0;
}
StringValue* sv = cast(StringValue*)&pools[npools - 1][nfill];
sv.ptrvalue = null;
sv.length = length;
.memcpy(sv.lstring(), s, length);
sv.lstring()[length] = 0;
const(uint) vptr = cast(uint)(npools << POOL_BITS | nfill);
nfill += nbytes + (-nbytes & 7); // align to 8 bytes
return vptr;
}
extern (C++) StringValue* getValue(uint vptr)
{
if (!vptr)
return null;
const(size_t) idx = (vptr >> POOL_BITS) - 1;
const(size_t) off = vptr & POOL_SIZE - 1;
return cast(StringValue*)&pools[idx][off];
}
extern (C++) size_t findSlot(hash_t hash, const(char)* s, size_t length)
{
// quadratic probing using triangular numbers
// http://stackoverflow.com/questions/2348187/moving-from-linear-probing-to-quadratic-probing-hash-collisons/2349774#2349774
for (size_t i = hash & (tabledim - 1), j = 1;; ++j)
{
StringValue* sv;
if (!table[i].vptr || table[i].hash == hash && (sv = getValue(table[i].vptr)).length == length && .memcmp(s, sv.lstring(), length) == 0)
return i;
i = (i + j) & (tabledim - 1);
}
}
extern (C++) void grow()
{
const(size_t) odim = tabledim;
StringEntry* otab = table;
tabledim *= 2;
table = cast(StringEntry*)mem.xcalloc(tabledim, (table[0]).sizeof);
for (size_t i = 0; i < odim; ++i)
{
StringEntry* se = &otab[i];
if (!se.vptr)
continue;
StringValue* sv = getValue(se.vptr);
table[findSlot(se.hash, sv.lstring(), sv.length)] = *se;
}
mem.xfree(otab);
}
}