forked from APrioriInvestments/typed_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictType.cpp
More file actions
238 lines (172 loc) · 6.56 KB
/
Copy pathDictType.cpp
File metadata and controls
238 lines (172 loc) · 6.56 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
#include "AllTypes.hpp"
void Dict::_forwardTypesMayHaveChanged() {
m_name = "Dict(" + m_key->name() + "->" + m_value->name() + ")";
m_size = sizeof(void*);
m_is_default_constructible = true;
m_bytes_per_key = m_key->bytecount();
m_bytes_per_key_value_pair = m_key->bytecount() + m_value->bytecount();
m_key_value_pair_type = Tuple::Make({m_key, m_value});
}
bool Dict::isBinaryCompatibleWithConcrete(Type* other) {
if (other->getTypeCategory() != m_typeCategory) {
return false;
}
Dict* otherO = (Dict*)other;
return m_key->isBinaryCompatibleWith(otherO->m_key) &&
m_value->isBinaryCompatibleWith(otherO->m_value);
}
// static
Dict* Dict::Make(Type* key, Type* value) {
static std::mutex guard;
std::lock_guard<std::mutex> lock(guard);
static std::map<std::pair<Type*, Type*>, Dict*> m;
auto lookup_key = std::make_pair(key,value);
auto it = m.find(lookup_key);
if (it == m.end()) {
it = m.insert(std::make_pair(lookup_key, new Dict(key, value))).first;
}
return it->second;
}
void Dict::repr(instance_ptr self, ReprAccumulator& stream) {
PushReprState isNew(stream, self);
if (!isNew) {
stream << m_name << "(" << (void*)self << ")";
return;
}
stream << "{";
layout& l = **(layout**)self;
bool isFirst = true;
for (long k = 0; k < l.items_reserved;k++) {
if (l.items_populated[k]) {
if (isFirst) {
isFirst = false;
} else {
stream << ", ";
}
m_key->repr(l.items + k * m_bytes_per_key_value_pair, stream);
stream << ": ";
m_key->repr(l.items + k * m_bytes_per_key_value_pair + m_bytes_per_key, stream);
}
}
stream << "}";
}
int32_t Dict::hash32(instance_ptr left) {
throw std::logic_error(name() + " is not hashable");
}
//to make this fast(er), we do dict size comparison first, then keys, then values
bool Dict::cmp(instance_ptr left, instance_ptr right, int pyComparisonOp) {
if (pyComparisonOp != Py_NE && pyComparisonOp != Py_EQ) {
throw std::runtime_error("Ordered comparison not supported between objects of type " + name());
}
layout& l = **(layout**)left;
layout& r = **(layout**)right;
if (&l == &r) {
return cmpResultToBoolForPyOrdering(pyComparisonOp, 0);
}
if (l.hash_table_count != r.hash_table_count) {
return cmpResultToBoolForPyOrdering(pyComparisonOp, 1);
}
//check each item on the left to see if its in the right and has the same value
for (long k = 0; k < l.items_reserved; k++) {
if (l.items_populated[k]) {
instance_ptr key = l.items + m_bytes_per_key_value_pair * k;
instance_ptr value = key + m_bytes_per_key;
instance_ptr otherValue = lookupValueByKey(right, key);
if (!otherValue) {
return cmpResultToBoolForPyOrdering(pyComparisonOp, 1);
}
if (m_value->cmp(value, otherValue, Py_NE)) {
return cmpResultToBoolForPyOrdering(pyComparisonOp, 1);
}
}
}
return cmpResultToBoolForPyOrdering(pyComparisonOp, 0);
}
int64_t Dict::refcount(instance_ptr self) const {
layout& record = **(layout**)self;
return record.refcount;
}
int64_t Dict::slotCount(instance_ptr self) const {
layout& record = **(layout**)self;
return record.items_reserved;
}
bool Dict::slotPopulated(instance_ptr self, size_t slot) const {
layout& record = **(layout**)self;
return record.items_populated[slot];
}
instance_ptr Dict::keyAtSlot(instance_ptr self, size_t offset) const {
layout& record = **(layout**)self;
return record.items + m_bytes_per_key_value_pair * offset;
}
instance_ptr Dict::valueAtSlot(instance_ptr self, size_t offset) const {
layout& record = **(layout**)self;
return record.items + m_bytes_per_key_value_pair * offset + m_bytes_per_key;
}
int64_t Dict::size(instance_ptr self) const {
layout& record = **(layout**)self;
return record.hash_table_count;
}
instance_ptr Dict::lookupValueByKey(instance_ptr self, instance_ptr key) const {
layout& record = **(layout**)self;
int32_t keyHash = m_key->hash32(key);
int32_t index = record.find(m_bytes_per_key_value_pair, keyHash, [&](instance_ptr ptr) {
return m_key->cmp(key, ptr, Py_EQ);
});
if (index >= 0) {
return record.items + index * m_bytes_per_key_value_pair + m_bytes_per_key;
}
return 0;
}
bool Dict::deleteKey(instance_ptr self, instance_ptr key) const {
layout& record = **(layout**)self;
int32_t keyHash = m_key->hash32(key);
int32_t index = record.remove(m_bytes_per_key_value_pair, keyHash, [&](instance_ptr ptr) {
return m_key->cmp(key, ptr, Py_EQ);
});
if (index >= 0) {
m_key->destroy(record.items + index * m_bytes_per_key_value_pair);
m_value->destroy(record.items + index * m_bytes_per_key_value_pair + m_bytes_per_key);
return true;
}
return false;
}
instance_ptr Dict::insertKey(instance_ptr self, instance_ptr key) const {
layout& record = **(layout**)self;
int32_t keyHash = m_key->hash32(key);
int32_t slot = record.allocateNewSlot(m_bytes_per_key_value_pair);
record.add(keyHash, slot);
m_key->copy_constructor(record.items + slot * m_bytes_per_key_value_pair, key);
return record.items + slot * m_bytes_per_key_value_pair + m_bytes_per_key;
}
void Dict::constructor(instance_ptr self) {
(*(layout**)self) = (layout*)malloc(sizeof(layout));
layout& record = **(layout**)self;
new (&record) layout();
record.refcount += 1;
}
void Dict::destroy(instance_ptr self) {
layout& record = **(layout**)self;
if (record.refcount.fetch_sub(1) == 1) {
for (long k = 0; k < record.items_reserved; k++) {
if (record.items_populated[k]) {
m_key->destroy(record.items + m_bytes_per_key_value_pair * k);
m_value->destroy(record.items + m_bytes_per_key_value_pair * k + m_bytes_per_key);
}
}
free(record.items);
free(record.items_populated);
free(record.hash_table_slots);
free(record.hash_table_hashes);
free(&record);
}
}
void Dict::copy_constructor(instance_ptr self, instance_ptr other) {
(*(layout**)self) = (*(layout**)other);
(*(layout**)self)->refcount++;
}
void Dict::assign(instance_ptr self, instance_ptr other) {
layout* old = (*(layout**)self);
(*(layout**)self) = (*(layout**)other);
(*(layout**)self)->refcount++;
destroy((instance_ptr)&old);
}