-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
aav.d
333 lines (296 loc) · 7.89 KB
/
aav.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* Compiler implementation of the D programming language
* http://dlang.org
*
* Copyright: Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved
* Authors: Walter Bright, http://www.digitalmars.com
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/root/aav.d, root/_aav.d)
* Documentation: https://dlang.org/phobos/dmd_root_aav.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/root/aav.d
*/
module dmd.root.aav;
import core.stdc.string;
import dmd.root.rmem;
extern (C++) size_t hash(size_t a)
{
a ^= (a >> 20) ^ (a >> 12);
return a ^ (a >> 7) ^ (a >> 4);
}
struct KeyValueTemplate(K,V)
{
K key;
V value;
}
alias Key = void*;
alias Value = void*;
alias KeyValue = KeyValueTemplate!(Key, Value);
struct aaA
{
aaA* next;
KeyValue keyValue;
alias keyValue this;
}
struct AA
{
aaA** b;
size_t b_length;
size_t nodes; // total number of aaA nodes
aaA*[4] binit; // initial value of b[]
aaA aafirst; // a lot of these AA's have only one entry
}
/****************************************************
* Determine number of entries in associative array.
*/
extern (C++) size_t dmd_aaLen(const AA* aa) pure
{
return aa ? aa.nodes : 0;
}
/*************************************************
* Get pointer to value in associative array indexed by key.
* Add entry for key if it is not already there, returning a pointer to a null Value.
* Create the associative array if it does not already exist.
*/
extern (C++) Value* dmd_aaGet(AA** paa, Key key)
{
//printf("paa = %p\n", paa);
if (!*paa)
{
AA* a = cast(AA*)mem.xmalloc(AA.sizeof);
a.b = cast(aaA**)a.binit;
a.b_length = 4;
a.nodes = 0;
a.binit[0] = null;
a.binit[1] = null;
a.binit[2] = null;
a.binit[3] = null;
*paa = a;
assert((*paa).b_length == 4);
}
//printf("paa = %p, *paa = %p\n", paa, *paa);
assert((*paa).b_length);
size_t i = hash(cast(size_t)key) & ((*paa).b_length - 1);
aaA** pe = &(*paa).b[i];
aaA* e;
while ((e = *pe) !is null)
{
if (key == e.key)
return &e.value;
pe = &e.next;
}
// Not found, create new elem
//printf("create new one\n");
size_t nodes = ++(*paa).nodes;
e = (nodes != 1) ? cast(aaA*)mem.xmalloc(aaA.sizeof) : &(*paa).aafirst;
//e = new aaA();
e.next = null;
e.key = key;
e.value = null;
*pe = e;
//printf("length = %d, nodes = %d\n", (*paa)->b_length, nodes);
if (nodes > (*paa).b_length * 2)
{
//printf("rehash\n");
dmd_aaRehash(paa);
}
return &e.value;
}
/*************************************************
* Get value in associative array indexed by key.
* Returns NULL if it is not already there.
*/
extern (C++) Value dmd_aaGetRvalue(AA* aa, Key key)
{
//printf("_aaGetRvalue(key = %p)\n", key);
if (aa)
{
size_t i;
size_t len = aa.b_length;
i = hash(cast(size_t)key) & (len - 1);
aaA* e = aa.b[i];
while (e)
{
if (key == e.key)
return e.value;
e = e.next;
}
}
return null; // not found
}
debug
{
/**
Gets a range of key/values for `aa`.
Returns: a range of key/values for `aa`.
*/
@property auto asRange(AA* aa)
{
return AARange!(Key, Value)(aa);
}
private struct AARange(K,V)
{
AA* aa;
// current index into bucket array `aa.b`
size_t bIndex;
aaA* current;
this(AA* aa)
{
if (aa)
{
this.aa = aa;
toNext();
}
}
@property bool empty() const { return current is null; }
@property auto front() { return cast(KeyValueTemplate!(K,V))current.keyValue; }
void popFront()
{
if (current.next)
current = current.next;
else
{
bIndex++;
toNext();
}
}
private void toNext()
{
for (; bIndex < aa.b_length; bIndex++)
{
if (auto next = aa.b[bIndex])
{
current = next;
return;
}
}
current = null;
}
}
unittest
{
AA* aa = null;
foreach(keyValue; aa.asRange)
assert(0);
enum totalKeyLength = 50;
foreach (i; 1 .. totalKeyLength + 1)
{
auto key = cast(void*)i;
{
auto valuePtr = dmd_aaGet(&aa, key);
assert(valuePtr);
*valuePtr = key;
}
bool[totalKeyLength] found;
size_t rangeCount = 0;
foreach (keyValue; aa.asRange)
{
assert(keyValue.key <= key);
assert(keyValue.key == keyValue.value);
rangeCount++;
assert(!found[cast(size_t)keyValue.key - 1]);
found[cast(size_t)keyValue.key - 1] = true;
}
assert(rangeCount == i);
}
}
}
/********************************************
* Rehash an array.
*/
extern (C++) void dmd_aaRehash(AA** paa)
{
//printf("Rehash\n");
if (*paa)
{
AA* aa = *paa;
if (aa)
{
size_t len = aa.b_length;
if (len == 4)
len = 32;
else
len *= 4;
aaA** newb = cast(aaA**)mem.xmalloc(aaA.sizeof * len);
memset(newb, 0, len * (aaA*).sizeof);
for (size_t k = 0; k < aa.b_length; k++)
{
aaA* e = aa.b[k];
while (e)
{
aaA* enext = e.next;
size_t j = hash(cast(size_t)e.key) & (len - 1);
e.next = newb[j];
newb[j] = e;
e = enext;
}
}
if (aa.b != cast(aaA**)aa.binit)
mem.xfree(aa.b);
aa.b = newb;
aa.b_length = len;
}
}
}
unittest
{
AA* aa = null;
Value v = dmd_aaGetRvalue(aa, null);
assert(!v);
Value* pv = dmd_aaGet(&aa, null);
assert(pv);
*pv = cast(void*)3;
v = dmd_aaGetRvalue(aa, null);
assert(v == cast(void*)3);
}
struct AssocArray(K,V)
{
private AA* aa;
/**
Returns: The number of key/value pairs.
*/
@property size_t length() const { return dmd_aaLen(aa); }
/**
Lookup value associated with `key` and return the address to it. If the `key`
has not been added, it adds it and returns the address to the new value.
Params:
key = key to lookup the value for
Returns: the address to the value associated with `key`. If `key` does not exist, it
is added and the address to the new value is returned.
*/
V* getLvalue(const(K) key)
{
return cast(V*)dmd_aaGet(&aa, cast(void*)key);
}
/**
Lookup and return the value associated with `key`, if the `key` has not been
added, it returns null.
Params:
key = key to lookup the value for
Returns: the value associated with `key` if present, otherwise, null.
*/
V opIndex(const(K) key)
{
return cast(V)dmd_aaGetRvalue(aa, cast(void*)key);
}
debug
{
/**
Gets a range of key/values for `aa`.
Returns: a range of key/values for `aa`.
*/
@property auto asRange() { return AARange!(K,V)(aa); }
}
}
///
unittest
{
auto foo = new Object();
auto bar = new Object();
AssocArray!(Object, Object) aa;
assert(aa[foo] is null);
assert(aa.length == 0);
auto fooValuePtr = aa.getLvalue(foo);
*fooValuePtr = bar;
assert(aa[foo] is bar);
assert(aa.length == 1);
}