-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathGlobHashTable.h
454 lines (406 loc) · 12.5 KB
/
GlobHashTable.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
#if PROFILE_DICTIONARY
#include "DictionaryStats.h"
#endif
template <typename TData, typename TElement>
class HashBucket
{
public:
TElement element;
TData value;
public:
HashBucket() : element(NULL), value(NULL) {}
static void Copy(HashBucket const& bucket, HashBucket& newBucket)
{
newBucket.element = bucket.element;
newBucket.value = bucket.value;
}
};
class Key
{
public:
static uint Get(Sym const *sym) { return static_cast<uint>(sym->m_id); }
static uint Get(ExprHash hash) { return static_cast<uint>(hash); }
};
#define FOREACH_VALUEHASHTABLE_ENTRY(BucketType, bucket, hashTable) \
for (uint _iterHash = 0; _iterHash < (hashTable)->tableSize; _iterHash++) \
{ \
FOREACH_SLISTBASE_ENTRY(BucketType, bucket, &(hashTable)->table[_iterHash]) \
{
#define NEXT_VALUEHASHTABLE_ENTRY \
} \
NEXT_SLISTBASE_ENTRY; \
}
#define FOREACH_VALUEHASHTABLE_ENTRY_EDITING(BucketType, bucket, hashTable, iter) \
for (uint _iterHash = 0; _iterHash < (hashTable)->tableSize; _iterHash++) \
{ \
FOREACH_SLISTBASE_ENTRY_EDITING(BucketType, bucket, &(hashTable)->table[_iterHash], iter) \
{
#define NEXT_VALUEHASHTABLE_ENTRY_EDITING \
} \
NEXT_SLISTBASE_ENTRY_EDITING; \
}
template<typename TData, typename TElement>
class ValueHashTable
{
private:
typedef HashBucket<TData, TElement> HashBucket;
public:
JitArenaAllocator * alloc;
uint tableSize;
SListBase<HashBucket> * table;
public:
static ValueHashTable * New(JitArenaAllocator *allocator, DECLSPEC_GUARD_OVERFLOW uint tableSize)
{
return AllocatorNewPlus(JitArenaAllocator, allocator, (tableSize*sizeof(SListBase<HashBucket>)), ValueHashTable, allocator, tableSize);
}
void Delete()
{
AllocatorDeletePlus(JitArenaAllocator, alloc, (tableSize*sizeof(SListBase<HashBucket>)), this);
}
~ValueHashTable()
{
for (uint i = 0; i< tableSize; i++)
{
table[i].Clear(alloc);
}
}
SListBase<HashBucket> * SwapBucket(SListBase<HashBucket> * newTable)
{
SListBase<HashBucket> * retTable = table;
table = newTable;
return retTable;
}
TElement * FindOrInsertNew(TData value)
{
uint key = Key::Get(value);
uint hash = this->Hash(key);
#if PROFILE_DICTIONARY
uint depth = 1;
#endif
// Keep sorted
FOREACH_SLISTBASE_ENTRY_EDITING(HashBucket, bucket, &this->table[hash], iter)
{
if (Key::Get(bucket.value) <= key)
{
if (Key::Get(bucket.value) == key)
{
return &(bucket.element);
}
break;
}
#if PROFILE_DICTIONARY
++depth;
#endif
} NEXT_SLISTBASE_ENTRY_EDITING;
HashBucket * newBucket = iter.InsertNodeBefore(this->alloc);
newBucket->value = value;
#if PROFILE_DICTIONARY
if (stats)
stats->Insert(depth);
#endif
return &newBucket->element;
}
TElement * FindOrInsertNewNoThrow(TData * value)
{
uint key = Key::Get(value);
uint hash = this->Hash(key);
#if PROFILE_DICTIONARY
uint depth = 1;
#endif
// Keep sorted
FOREACH_SLISTBASE_ENTRY_EDITING(HashBucket, bucket, &this->table[hash], iter)
{
if (Key::Get(bucket.value) <= key)
{
if (Key::Get(bucket.value) == key)
{
return &(bucket.element);
}
break;
}
#if PROFILE_DICTIONARY
++depth;
#endif
} NEXT_SLISTBASE_ENTRY_EDITING;
HashBucket * newBucket = iter.InsertNodeBeforeNoThrow(this->alloc);
if (newBucket == nullptr)
{
return nullptr;
}
newBucket->value = value;
#if PROFILE_DICTIONARY
if (stats)
stats->Insert(depth);
#endif
return &newBucket->element;
}
TElement * FindOrInsert(TElement element, TData value)
{
uint key = Key::Get(value);
uint hash = this->Hash(key);
#if PROFILE_DICTIONARY
uint depth = 1;
#endif
// Keep sorted
FOREACH_SLISTBASE_ENTRY_EDITING(HashBucket, bucket, &this->table[hash], iter)
{
if (Key::Get(bucket.value) <= key)
{
if (Key::Get(bucket.value) == key)
{
return &(bucket.element);
}
break;
}
#if PROFILE_DICTIONARY
++depth;
#endif
} NEXT_SLISTBASE_ENTRY_EDITING;
HashBucket * newBucket = iter.InsertNodeBefore(this->alloc);
Assert(newBucket != nullptr);
newBucket->value = value;
newBucket->element = element;
#if PROFILE_DICTIONARY
if (stats)
stats->Insert(depth);
#endif
return NULL;
}
TElement * Get(TData value)
{
uint key = Key::Get(value);
return Get(key);
}
TElement * Get(uint key)
{
uint hash = this->Hash(key);
// Assumes sorted lists
FOREACH_SLISTBASE_ENTRY(HashBucket, bucket, &this->table[hash])
{
if (Key::Get(bucket.value) <= key)
{
if (Key::Get(bucket.value) == key)
{
return &(bucket.element);
}
break;
}
} NEXT_SLISTBASE_ENTRY;
return NULL;
}
HashBucket * GetBucket(uint key)
{
uint hash = this->Hash(key);
// Assumes sorted lists
FOREACH_SLISTBASE_ENTRY(HashBucket, bucket, &this->table[hash])
{
if (Key::Get(bucket.value) <= key)
{
if (Key::Get(bucket.value) == key)
{
return &bucket;
}
break;
}
} NEXT_SLISTBASE_ENTRY;
return nullptr;
}
TElement GetAndClear(TData * value)
{
uint key = Key::Get(value);
uint hash = this->Hash(key);
SListBase<HashBucket> * list = &this->table[hash];
#if PROFILE_DICTIONARY
bool first = true;
#endif
// Assumes sorted lists
FOREACH_SLISTBASE_ENTRY_EDITING(HashBucket, bucket, list, iter)
{
if (Key::Get(bucket.value) <= key)
{
if (Key::Get(bucket.value) == key)
{
TElement retVal = bucket.element;
iter.RemoveCurrent(this->alloc);
#if PROFILE_DICTIONARY
if (stats)
stats->Remove(first && !(iter.Next()));
#endif
return retVal;
}
break;
}
#if PROFILE_DICTIONARY
first = false;
#endif
} NEXT_SLISTBASE_ENTRY_EDITING;
return nullptr;
}
void Clear(uint key)
{
uint hash = this->Hash(key);
SListBase<HashBucket> * list = &this->table[hash];
// Assumes sorted lists
#if PROFILE_DICTIONARY
bool first = true;
#endif
FOREACH_SLISTBASE_ENTRY_EDITING(HashBucket, bucket, list, iter)
{
if (Key::Get(bucket.value) <= key)
{
if (Key::Get(bucket.value) == key)
{
iter.RemoveCurrent(this->alloc);
#if PROFILE_DICTIONARY
if (stats)
stats->Remove(first && !(iter.Next()));
#endif
}
return;
}
#if PROFILE_DICTIONARY
first = false;
#endif
} NEXT_SLISTBASE_ENTRY_EDITING;
}
void And(ValueHashTable *this2)
{
for (uint i = 0; i < this->tableSize; i++)
{
_TYPENAME SListBase<HashBucket>::Iterator iter2(&this2->table[i]);
iter2.Next();
FOREACH_SLISTBASE_ENTRY_EDITING(HashBucket, bucket, &this->table[i], iter)
{
while (iter2.IsValid() && bucket.value < iter2.Data().value)
{
iter2.Next();
}
if (!iter2.IsValid() || bucket.value != iter2.Data().value || bucket.element != iter2.Data().element)
{
iter.RemoveCurrent(this->alloc);
#if PROFILE_DICTIONARY
if (stats)
stats->Remove(false);
#endif
continue;
}
else
{
AssertMsg(bucket.value == iter2.Data().value && bucket.element == iter2.Data().element, "Huh??");
}
iter2.Next();
} NEXT_SLISTBASE_ENTRY_EDITING;
}
}
template <class Fn>
void Or(ValueHashTable * this2, Fn fn)
{
for (uint i = 0; i < this->tableSize; i++)
{
_TYPENAME SListBase<HashBucket>::Iterator iter2(&this2->table[i]);
iter2.Next();
FOREACH_SLISTBASE_ENTRY_EDITING(HashBucket, bucket, &this->table[i], iter)
{
while (iter2.IsValid() && bucket.value < iter2.Data().value)
{
HashBucket * newBucket = iter.InsertNodeBefore(this->alloc);
newBucket->value = iter2.Data().value;
newBucket->element = fn(nullptr, iter2.Data().element);
iter2.Next();
}
if (!iter2.IsValid())
{
break;
}
if (bucket.value == iter2.Data().value)
{
bucket.element = fn(bucket.element, iter2.Data().element);
iter2.Next();
}
} NEXT_SLISTBASE_ENTRY_EDITING;
while (iter2.IsValid())
{
HashBucket * newBucket = iter.InsertNodeBefore(this->alloc);
newBucket->value = iter2.Data().value;
newBucket->element = fn(nullptr, iter2.Data().element);
iter2.Next();
}
}
}
ValueHashTable *Copy()
{
ValueHashTable *newTable = ValueHashTable::New(this->alloc, this->tableSize);
for (uint i = 0; i < this->tableSize; i++)
{
this->table[i].template CopyTo<HashBucket::Copy>(this->alloc, newTable->table[i]);
}
#if PROFILE_DICTIONARY
if (stats)
newTable->stats = stats->Clone();
#endif
return newTable;
}
void ClearAll()
{
for (uint i = 0; i < this->tableSize; i++)
{
this->table[i].Clear(this->alloc);
}
#if PROFILE_DICTIONARY
// To not lose previously collected data, we will treat cleared dictionary as a separate instance for stats tracking purpose
stats = DictionaryStats::Create(typeid(this).name(), tableSize);
#endif
}
#if DBG_DUMP
void Dump()
{
FOREACH_VALUEHASHTABLE_ENTRY(HashBucket, bucket, this)
{
Output::Print(_u("%4d => "), bucket.value);
bucket.element->Dump();
Output::Print(_u("\n"));
Output::Print(_u("\n"));
}
NEXT_VALUEHASHTABLE_ENTRY;
}
void Dump(void (*valueDump)(TData))
{
Output::Print(_u("\n-------------------------------------------------------------------------------------------------\n"));
FOREACH_VALUEHASHTABLE_ENTRY(HashBucket, bucket, this)
{
valueDump(bucket.value);
Output::Print(_u(" => "), bucket.value);
bucket.element->Dump();
Output::Print(_u("\n"));
}
NEXT_VALUEHASHTABLE_ENTRY;
}
#endif
protected:
ValueHashTable(JitArenaAllocator * allocator, uint tableSize) : alloc(allocator), tableSize(tableSize)
{
Init();
#if PROFILE_DICTIONARY
stats = DictionaryStats::Create(typeid(this).name(), tableSize);
#endif
}
void Init()
{
table = (SListBase<HashBucket> *)(((char *)this) + sizeof(ValueHashTable));
for (uint i = 0; i < tableSize; i++)
{
// placement new
::new (&table[i]) SListBase<HashBucket>();
}
}
private:
uint Hash(uint key) { return (key % this->tableSize); }
#if PROFILE_DICTIONARY
DictionaryStats *stats;
#endif
};