forked from APrioriInvestments/typed_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictType.hpp
More file actions
494 lines (385 loc) · 16.1 KB
/
Copy pathDictType.hpp
File metadata and controls
494 lines (385 loc) · 16.1 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#pragma once
#include "Type.hpp"
#include "ReprAccumulator.hpp"
#include <unordered_map>
class Dict : public Type {
public:
class layout {
public:
layout() :
refcount(0),
items(nullptr),
items_populated(nullptr),
items_reserved(0),
top_item_slot(0),
hash_table_slots(nullptr),
hash_table_hashes(nullptr),
hash_table_size(0),
hash_table_count(0),
hash_table_empty_slots(0)
{
}
enum { EMPTY = -1, DELETED = -2 };
//return the index of the object indexed by 'hash', or -1
template<class eq_func>
int32_t find(int32_t kv_pair_size, int32_t hash, const eq_func& compare) {
if (!hash_table_slots) {
return -1;
}
int32_t offset = hash % hash_table_size;
while (true) {
//slot is empty
int32_t slot = hash_table_slots[offset];
if (slot == EMPTY) {
return -1;
}
if (slot != DELETED && hash_table_hashes[offset] == hash && compare(items + kv_pair_size * slot)) {
return slot;
}
offset = nextOffset(offset);
}
}
//linear search
int32_t nextOffset(int32_t offset) const {
offset += 1;
if (offset >= hash_table_size) {
offset = 0;
}
return offset;
}
//add an item to the hash table
void add(int32_t hash, int32_t slot) {
if (hash_table_count * 2 + 1 > hash_table_size || hash_table_empty_slots < hash_table_size / 4 + 1) {
resizeTable();
}
int32_t offset = hash % hash_table_size;
while (true) {
if (hash_table_slots[offset] == EMPTY || hash_table_slots[offset] == DELETED) {
if (hash_table_slots[offset] == EMPTY) {
hash_table_empty_slots--;
}
hash_table_slots[offset] = slot;
hash_table_hashes[offset] = hash;
items_populated[slot] = 1;
hash_table_count++;
return;
}
offset = nextOffset(offset);
}
}
//remove an item with the given hash. returning the item slot where it lived.
//-1 if not found
template<class eq_func>
int32_t remove(int32_t kv_pair_size, int32_t hash, const eq_func& compare) {
if (!hash_table_slots) {
return -1;
}
if (items_reserved > (hash_table_count + 2) * 4) {
compressItemTable(kv_pair_size);
}
//compress the hashtable if it's really empty
if (hash_table_count < hash_table_size / 8) {
resizeTable();
}
int32_t offset = hash % hash_table_size;
while (true) {
int32_t slot = hash_table_slots[offset];
if (slot == EMPTY) {
//we never found the item
return -1;
}
if (slot != DELETED && compare(items + kv_pair_size * slot)) {
items_populated[slot] = 0;
hash_table_slots[offset] = DELETED;
hash_table_hashes[offset] = -1;
hash_table_count -= 1;
return slot;
}
offset = nextOffset(offset);
}
}
void compressItemTable(size_t kv_pair_size) {
std::vector<int32_t> newItemPositions;
int32_t count_so_far = 0;
for (long k = 0; k < items_reserved; k++) {
if (items_populated[k]) {
newItemPositions.push_back(count_so_far);
if (k != count_so_far) {
items_populated[count_so_far] = 1;
items_populated[k] = 0;
memcpy(items + kv_pair_size * count_so_far, items + kv_pair_size * k, kv_pair_size);
}
count_so_far++;
} else {
newItemPositions.push_back(-1);
}
}
items_reserved = count_so_far;
items_populated = (uint8_t*)realloc(items_populated, count_so_far);
items = (uint8_t*)realloc(items, count_so_far * kv_pair_size);
top_item_slot = items_reserved;
for (long k = 0; k < hash_table_size; k++) {
if (hash_table_slots[k] >= 0) {
if (hash_table_slots[k] >= newItemPositions.size()) {
throw std::runtime_error("corrupt slot");
}
hash_table_slots[k] = newItemPositions[hash_table_slots[k]];
if (hash_table_slots[k] < 0) {
throw std::runtime_error("invalid slot");
}
}
}
for (long k = 0; k < hash_table_size; k++) {
if (hash_table_slots[k] >= 0) {
if (hash_table_slots[k] >= items_reserved) {
throw std::runtime_error("failed during compression");
}
}
}
}
int32_t allocateNewSlot(size_t kv_pair_size) {
if (!items) {
items = (uint8_t*)malloc(4 * kv_pair_size);
items_populated = (uint8_t*)malloc(4);
items_reserved = 4;
top_item_slot = 0;
for (long k = 0; k < items_reserved; k++) {
items_populated[k] = 0;
}
}
while (top_item_slot >= items_reserved) {
size_t old_reserved = items_reserved;
items_reserved = items_reserved * 1.25 + 1;
items = (uint8_t*)realloc(items, kv_pair_size * items_reserved);
items_populated = (uint8_t*)realloc(items_populated, items_reserved);
for (long k = old_reserved; k < items_reserved; k++) {
items_populated[k] = 0;
}
}
return top_item_slot++;
}
int32_t computeNextPrime(int32_t p) {
static std::vector<int32_t> primes;
if (!primes.size()) {
primes.push_back(2);
}
auto isprime = [&](int32_t candidate) {
for (auto d: primes) {
if (candidate % d == 0) {
return false;
}
if (d*d > candidate) {
return true;
}
}
throw std::logic_error("Expected to clear the primes list.");
};
while (true) {
while (primes.back() * primes.back() < p) {
int32_t cur = primes.back() + 1;
while (!isprime(cur)) {
cur++;
}
primes.push_back(cur);
}
if (isprime(p)) {
return p;
}
p++;
}
}
void resizeTable() {
if (!hash_table_slots) {
hash_table_slots = (int32_t*)malloc(7 * sizeof(int32_t));
hash_table_hashes = (int32_t*)malloc(7 * sizeof(int32_t));
hash_table_size = 7;
hash_table_count = 0;
hash_table_empty_slots = hash_table_size;
for (long k = 0; k < hash_table_size; k++) {
hash_table_slots[k] = EMPTY;
hash_table_hashes[k] = -1;
}
} else {
int32_t oldSize = hash_table_size;
int32_t* oldSlots = hash_table_slots;
int32_t* oldHashes = hash_table_hashes;
//make sure the table's not too small
hash_table_size = computeNextPrime(hash_table_count * 4 + 7);
hash_table_slots = (int32_t*)malloc(hash_table_size * sizeof(int32_t));
hash_table_hashes = (int32_t*)malloc(hash_table_size * sizeof(int32_t));
for (long k = 0; k < hash_table_size; k++) {
hash_table_slots[k] = EMPTY;
hash_table_hashes[k] = -1;
}
hash_table_count = 0;
hash_table_empty_slots = hash_table_size;
for (long k = 0; k < oldSize; k++) {
if (oldSlots[k] != EMPTY && oldSlots[k] != DELETED) {
add(oldHashes[k], oldSlots[k]);
}
}
free(oldSlots);
free(oldHashes);
}
}
void prepareForDeserialization(uint32_t slotCount, size_t kv_pair_size) {
if (hash_table_size) {
throw std::runtime_error("deserialization prepare should only be called on empty tables");
}
items_reserved = slotCount;
items_populated = (uint8_t*)malloc(slotCount);
items = (uint8_t*)malloc(slotCount * kv_pair_size);
for (long k = 0; k < items_reserved; k++) {
items_populated[k] = true;
}
top_item_slot = items_reserved;
}
template<class hash_fun_type>
void buildHashTableAfterDeserialization(size_t kv_pair_size, const hash_fun_type& hash_fun) {
hash_table_size = computeNextPrime(items_reserved * 2.5 + 7);
hash_table_slots = (int32_t*)malloc(hash_table_size * sizeof(int32_t));
hash_table_hashes = (int32_t*)malloc(hash_table_size * sizeof(int32_t));
hash_table_count = 0;
hash_table_empty_slots = hash_table_size;
for (long k = 0; k < hash_table_size; k++) {
hash_table_slots[k] = EMPTY;
hash_table_hashes[k] = -1;
}
for (long k = 0; k < items_reserved; k++) {
add(hash_fun(items + kv_pair_size * k), k);
}
}
void checkInvariants(std::string reason) {
int64_t popCount = 0;
for (long k = 0; k < items_reserved; k++) {
if (items_populated[k]) {
popCount++;
if (top_item_slot <= k) {
throw std::runtime_error(reason + ": top item slot should be greater than all populated items");
}
}
}
if (popCount != hash_table_count) {
throw std::runtime_error(reason + ": populated item count is not the same as the hashtable count");
}
int64_t filledSlots = 0;
int64_t deletedSlots = 0;
for (long k = 0; k < hash_table_size; k++) {
if (hash_table_slots[k] == DELETED) {
deletedSlots++;
} else if (hash_table_slots[k] != EMPTY) {
filledSlots++;
if (hash_table_slots[k] >= items_reserved) {
throw std::runtime_error(reason + ": hash table has slot entry out of bounds with item list");
}
if (!items_populated[hash_table_slots[k]]) {
throw std::runtime_error(reason + ": hash table points to unmarked slot");
}
}
}
if (filledSlots != hash_table_count) {
throw std::runtime_error(reason + ": Filled slot count is not the same as the hashtable's known count");
}
if (hash_table_size - filledSlots - deletedSlots != hash_table_empty_slots) {
throw std::runtime_error(reason + ": empty slot count is not consistent");
}
}
std::atomic<int64_t> refcount;
uint8_t* items; //packed set of key_value pairs.
uint8_t* items_populated; //array of bool for whether populated
size_t items_reserved; //count of items reserved
size_t top_item_slot; //index of the next item slot to use
int32_t* hash_table_slots; //a hashtable. each actual object hash to the slot it holds. -1 if not populated.
int32_t* hash_table_hashes; //a hashtable. each actual object hash to the slot it holds. -1 if not populated.
size_t hash_table_size; //size of the table
size_t hash_table_count; //populated count of the table
size_t hash_table_empty_slots; //slots that are not empty in the table
};
public:
Dict(Type* key, Type* value) :
Type(TypeCategory::catDict),
m_key(key),
m_value(value)
{
forwardTypesMayHaveChanged();
}
template<class visitor_type>
void _visitContainedTypes(const visitor_type& visitor) {
}
template<class visitor_type>
void _visitReferencedTypes(const visitor_type& visitor) {
visitor(m_key);
visitor(m_value);
}
void _forwardTypesMayHaveChanged();
bool isBinaryCompatibleWithConcrete(Type* other);
static Dict* Make(Type* key, Type* value);
template<class buf_t>
void serialize(instance_ptr self, buf_t& buffer) {
layout& l = **(layout**)self;
uint32_t id;
bool isNew;
std::tie(id, isNew) = buffer.cachePointer(&l, this);
buffer.write_uint32(id);
if (isNew) {
buffer.write_uint32(l.hash_table_count);
for (long k = 0; k < l.items_reserved; k++) {
if (l.items_populated[k]) {
m_key->serialize(l.items + m_bytes_per_key_value_pair * k, buffer);
m_value->serialize(l.items + m_bytes_per_key_value_pair * k + m_bytes_per_key, buffer);
}
}
}
}
template<class buf_t>
void deserialize(instance_ptr self, buf_t& buffer) {
int32_t id = buffer.read_uint32();
void* ptr = buffer.lookupCachedPointer(id);
if (ptr) {
*((layout**)self) = (layout*)ptr;
(*(layout**)self)->refcount++;
return;
}
constructor(self);
layout& l = **((layout**)self);
//incref it before putting it in.
l.refcount++;
buffer.addCachedPointer(id, *((layout**)self), this);
int32_t count = buffer.read_uint32();
l.prepareForDeserialization(count, m_bytes_per_key_value_pair);
for (long k = 0; k < count; k++) {
m_key->deserialize(l.items + m_bytes_per_key_value_pair * k, buffer);
m_value->deserialize(l.items + m_bytes_per_key_value_pair * k + m_bytes_per_key, buffer);
}
l.buildHashTableAfterDeserialization(
m_bytes_per_key_value_pair,
[&](instance_ptr ptr) { return m_key->hash32(ptr); }
);
l.checkInvariants("after deserialization");
}
void repr(instance_ptr self, ReprAccumulator& stream);
int32_t hash32(instance_ptr left);
bool cmp(instance_ptr left, instance_ptr right, int pyComparisonOp);
int64_t refcount(instance_ptr self) const;
int64_t size(instance_ptr self) const;
int64_t slotCount(instance_ptr self) const;
bool slotPopulated(instance_ptr self, size_t offset) const;
instance_ptr keyAtSlot(instance_ptr self, size_t offset) const;
instance_ptr valueAtSlot(instance_ptr self, size_t offset) const;
instance_ptr lookupValueByKey(instance_ptr self, instance_ptr key) const;
instance_ptr insertKey(instance_ptr self, instance_ptr key) const;
bool deleteKey(instance_ptr self, instance_ptr key) const;
void constructor(instance_ptr self);
void destroy(instance_ptr self);
void copy_constructor(instance_ptr self, instance_ptr other);
void assign(instance_ptr self, instance_ptr other);
Type* keyValuePairType() const { return m_key_value_pair_type; }
Type* keyType() const { return m_key; }
Type* valueType() const { return m_value; }
private:
Type* m_key;
Type* m_value;
Type* m_key_value_pair_type;
size_t m_bytes_per_key;
size_t m_bytes_per_key_value_pair;
};