forked from APrioriInvestments/typed_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathType.cpp
More file actions
2406 lines (1879 loc) · 62.5 KB
/
Copy pathType.cpp
File metadata and controls
2406 lines (1879 loc) · 62.5 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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Type.hpp"
void Type::repr(instance_ptr self, ReprAccumulator& out) {
assertForwardsResolved();
this->check([&](auto& subtype) {
subtype.repr(self, out);
});
}
char Type::cmp(instance_ptr left, instance_ptr right) {
assertForwardsResolved();
return this->check([&](auto& subtype) {
return subtype.cmp(left, right);
});
}
int32_t Type::hash32(instance_ptr left) {
assertForwardsResolved();
return this->check([&](auto& subtype) {
return subtype.hash32(left);
});
}
void Type::swap(instance_ptr left, instance_ptr right) {
assertForwardsResolved();
if (left == right) {
return;
}
size_t remaining = m_size;
while (remaining >= 8) {
int64_t temp = *(int64_t*)left;
*(int64_t*)left = *(int64_t*)right;
*(int64_t*)right = temp;
remaining -= 8;
left += 8;
right += 8;
}
while (remaining > 0) {
int8_t temp = *(int8_t*)left;
*(int8_t*)left = *(int8_t*)right;
*(int8_t*)right = temp;
remaining -= 1;
left += 1;
right += 1;
}
}
// static
char Type::byteCompare(uint8_t* l, uint8_t* r, size_t count) {
while (count >= 8 && *(uint64_t*)l == *(uint64_t*)r) {
l += 8;
r += 8;
count -= 8;
}
for (long k = 0; k < count; k++) {
if (l[k] < r[k]) {
return -1;
}
if (l[k] > r[k]) {
return 1;
}
}
return 0;
}
void Type::constructor(instance_ptr self) {
assertForwardsResolved();
this->check([&](auto& subtype) { subtype.constructor(self); } );
}
void Type::destroy(instance_ptr self) {
assertForwardsResolved();
this->check([&](auto& subtype) { subtype.destroy(self); } );
}
void Type::forwardTypesMayHaveChanged() {
m_references_unresolved_forwards = false;
visitReferencedTypes([&](Type* t) {
if (t->references_unresolved_forwards()) {
m_references_unresolved_forwards = true;
}
});
this->check([&](auto& subtype) {
subtype._forwardTypesMayHaveChanged();
});
if (mTypeRep) {
updateTypeRepForType(this, mTypeRep);
}
}
void Type::copy_constructor(instance_ptr self, instance_ptr other) {
assertForwardsResolved();
this->check([&](auto& subtype) { subtype.copy_constructor(self, other); } );
}
void Type::assign(instance_ptr self, instance_ptr other) {
assertForwardsResolved();
this->check([&](auto& subtype) { subtype.assign(self, other); } );
}
bool Type::isBinaryCompatibleWith(Type* other) {
if (other == this) {
return true;
}
while (other->getTypeCategory() == TypeCategory::catPythonSubclass) {
other = other->getBaseType();
}
auto it = mIsBinaryCompatible.find(other);
if (it != mIsBinaryCompatible.end()) {
return it->second != BinaryCompatibilityCategory::Incompatible;
}
//mark that we are recursing through this datastructure. we don't want to
//loop indefinitely.
mIsBinaryCompatible[other] = BinaryCompatibilityCategory::Checking;
bool isCompatible = this->check([&](auto& subtype) {
return subtype.isBinaryCompatibleWithConcrete(other);
});
mIsBinaryCompatible[other] = isCompatible ?
BinaryCompatibilityCategory::Compatible :
BinaryCompatibilityCategory::Incompatible
;
return isCompatible;
}
bool OneOf::isBinaryCompatibleWithConcrete(Type* other) {
if (other->getTypeCategory() != TypeCategory::catOneOf) {
return false;
}
OneOf* otherO = (OneOf*)other;
if (m_types.size() != otherO->m_types.size()) {
return false;
}
for (long k = 0; k < m_types.size(); k++) {
if (!m_types[k]->isBinaryCompatibleWith(otherO->m_types[k])) {
return false;
}
}
return true;
}
void OneOf::_forwardTypesMayHaveChanged() {
m_size = computeBytecount();
m_name = computeName();
m_is_default_constructible = false;
for (auto typePtr: m_types) {
if (typePtr->is_default_constructible()) {
m_is_default_constructible = true;
break;
}
}
}
std::string OneOf::computeName() const {
std::string res = "OneOf(";
bool first = true;
for (auto t: m_types) {
if (first) {
first = false;
} else {
res += ", ";
}
res += t->name();
}
res += ")";
return res;
}
void OneOf::repr(instance_ptr self, ReprAccumulator& stream) {
m_types[*((uint8_t*)self)]->repr(self+1, stream);
}
int32_t OneOf::hash32(instance_ptr left) {
Hash32Accumulator acc((int)getTypeCategory());
acc.add(*(uint8_t*)left);
acc.add(m_types[*((uint8_t*)left)]->hash32(left+1));
return acc.get();
}
char OneOf::cmp(instance_ptr left, instance_ptr right) {
if (((uint8_t*)left)[0] < ((uint8_t*)right)[0]) {
return -1;
}
if (((uint8_t*)left)[0] > ((uint8_t*)right)[0]) {
return 1;
}
return m_types[*((uint8_t*)left)]->cmp(left+1,right+1);
}
size_t OneOf::computeBytecount() const {
size_t res = 0;
for (auto t: m_types)
res = std::max(res, t->bytecount());
return res + 1;
}
void OneOf::constructor(instance_ptr self) {
if (!m_is_default_constructible) {
throw std::runtime_error(m_name + " is not default-constructible");
}
for (size_t k = 0; k < m_types.size(); k++) {
if (m_types[k]->is_default_constructible()) {
*(uint8_t*)self = k;
m_types[k]->constructor(self+1);
return;
}
}
}
void OneOf::destroy(instance_ptr self) {
uint8_t which = *(uint8_t*)(self);
m_types[which]->destroy(self+1);
}
void OneOf::copy_constructor(instance_ptr self, instance_ptr other) {
uint8_t which = *(uint8_t*)self = *(uint8_t*)other;
m_types[which]->copy_constructor(self+1, other+1);
}
void OneOf::assign(instance_ptr self, instance_ptr other) {
uint8_t which = *(uint8_t*)self;
if (which == *(uint8_t*)other) {
m_types[which]->assign(self+1,other+1);
} else {
m_types[which]->destroy(self+1);
uint8_t otherWhich = *(uint8_t*)other;
*(uint8_t*)self = otherWhich;
m_types[otherWhich]->copy_constructor(self+1,other+1);
}
}
// static
OneOf* OneOf::Make(const std::vector<Type*>& types) {
std::vector<Type*> flat_typelist;
std::set<Type*> seen;
//make sure we only get each type once and don't have any other 'OneOf' in there...
std::function<void (const std::vector<Type*>)> visit = [&](const std::vector<Type*>& subvec) {
for (auto t: subvec) {
if (t->getTypeCategory() == catOneOf) {
visit( ((OneOf*)t)->getTypes() );
} else if (seen.find(t) == seen.end()) {
flat_typelist.push_back(t);
seen.insert(t);
}
}
};
visit(types);
static std::mutex guard;
std::lock_guard<std::mutex> lock(guard);
typedef const std::vector<Type*> keytype;
static std::map<keytype, OneOf*> m;
auto it = m.find(flat_typelist);
if (it == m.end()) {
it = m.insert(std::make_pair(flat_typelist, new OneOf(flat_typelist))).first;
}
return it->second;
}
bool CompositeType::isBinaryCompatibleWithConcrete(Type* other) {
if (other->getTypeCategory() != m_typeCategory) {
return false;
}
CompositeType* otherO = (CompositeType*)other;
if (m_types.size() != otherO->m_types.size()) {
return false;
}
for (long k = 0; k < m_types.size(); k++) {
if (!m_types[k]->isBinaryCompatibleWith(otherO->m_types[k])) {
return false;
}
}
return true;
}
void CompositeType::_forwardTypesMayHaveChanged() {
m_is_default_constructible = true;
m_size = 0;
m_byte_offsets.clear();
for (auto t: m_types) {
m_byte_offsets.push_back(m_size);
m_size += t->bytecount();
}
for (auto t: m_types) {
if (!t->is_default_constructible()) {
m_is_default_constructible = false;
}
}
}
char CompositeType::cmp(instance_ptr left, instance_ptr right) {
for (long k = 0; k < m_types.size(); k++) {
char res = m_types[k]->cmp(left + m_byte_offsets[k], right + m_byte_offsets[k]);
if (res != 0) {
return res;
}
}
return 0;
}
void CompositeType::repr(instance_ptr self, ReprAccumulator& stream) {
stream << "(";
for (long k = 0; k < getTypes().size();k++) {
if (k > 0) {
stream << ", ";
}
if (k < m_names.size()) {
stream << m_names[k] << "=";
}
getTypes()[k]->repr(eltPtr(self,k),stream);
}
if (getTypes().size() == 1) {
stream << ",";
}
stream << ")";
}
int32_t CompositeType::hash32(instance_ptr left) {
Hash32Accumulator acc((int)getTypeCategory());
for (long k = 0; k < getTypes().size();k++) {
acc.add(getTypes()[k]->hash32(eltPtr(left,k)));
}
acc.add(getTypes().size());
return acc.get();
}
void CompositeType::constructor(instance_ptr self) {
if (!m_is_default_constructible) {
throw std::runtime_error(m_name + " is not default-constructible");
}
for (size_t k = 0; k < m_types.size(); k++) {
m_types[k]->constructor(self+m_byte_offsets[k]);
}
}
void CompositeType::destroy(instance_ptr self) {
for (long k = (long)m_types.size() - 1; k >= 0; k--) {
m_types[k]->destroy(self+m_byte_offsets[k]);
}
}
void CompositeType::copy_constructor(instance_ptr self, instance_ptr other) {
for (long k = (long)m_types.size() - 1; k >= 0; k--) {
m_types[k]->copy_constructor(self + m_byte_offsets[k], other+m_byte_offsets[k]);
}
}
void CompositeType::assign(instance_ptr self, instance_ptr other) {
for (long k = (long)m_types.size() - 1; k >= 0; k--) {
m_types[k]->assign(self + m_byte_offsets[k], other+m_byte_offsets[k]);
}
}
void NamedTuple::_forwardTypesMayHaveChanged() {
((CompositeType*)this)->_forwardTypesMayHaveChanged();
std::string oldName = m_name;
m_name = "NamedTuple(";
for (long k = 0; k < m_types.size();k++) {
if (k) {
m_name += ", ";
}
m_name += m_names[k] + "=" + m_types[k]->name();
}
m_name += ")";
}
void Tuple::_forwardTypesMayHaveChanged() {
((CompositeType*)this)->_forwardTypesMayHaveChanged();
m_name = "Tuple(";
for (long k = 0; k < m_types.size();k++) {
if (k) {
m_name += ", ";
}
m_name += m_types[k]->name();
}
m_name += ")";
}
bool TupleOrListOf::isBinaryCompatibleWithConcrete(Type* other) {
if (other->getTypeCategory() != m_typeCategory) {
return false;
}
TupleOf* otherO = (TupleOf*)other;
return m_element_type->isBinaryCompatibleWith(otherO->m_element_type);
}
void TupleOrListOf::repr(instance_ptr self, ReprAccumulator& stream) {
PushReprState isNew(stream, self);
if (!isNew) {
if (m_is_tuple) {
stream << m_name << "(" << (void*)self << ")";
} else {
stream << m_name << "[" << (void*)self << "]";
}
return;
}
stream << (m_is_tuple ? "(" : "[");
int32_t ct = count(self);
for (long k = 0; k < ct;k++) {
if (k > 0) {
stream << ", ";
}
m_element_type->repr(eltPtr(self,k),stream);
}
stream << (m_is_tuple ? ")" : "]");
}
int32_t TupleOrListOf::hash32(instance_ptr left) {
if (!(*(layout**)left)) {
return 0x123;
}
if ((*(layout**)left)->hash_cache == -1) {
Hash32Accumulator acc((int)getTypeCategory());
int32_t ct = count(left);
acc.add(ct);
for (long k = 0; k < ct;k++) {
acc.add(m_element_type->hash32(eltPtr(left, k)));
}
(*(layout**)left)->hash_cache = acc.get();
if ((*(layout**)left)->hash_cache == -1) {
(*(layout**)left)->hash_cache = -2;
}
}
return (*(layout**)left)->hash_cache;
}
char TupleOrListOf::cmp(instance_ptr left, instance_ptr right) {
if (!(*(layout**)left) && (*(layout**)right)) {
return -1;
}
if (!(*(layout**)right) && (*(layout**)left)) {
return 1;
}
if (!(*(layout**)right) && !(*(layout**)left)) {
return 0;
}
layout& left_layout = **(layout**)left;
layout& right_layout = **(layout**)right;
if (&left_layout == &right_layout) {
return 0;
}
size_t bytesPer = m_element_type->bytecount();
for (long k = 0; k < left_layout.count && k < right_layout.count; k++) {
char res = m_element_type->cmp(left_layout.data + bytesPer * k,
right_layout.data + bytesPer * k);
if (res != 0) {
return res;
}
}
if (left_layout.count < right_layout.count) {
return -1;
}
if (left_layout.count > right_layout.count) {
return 1;
}
return 0;
}
// static
TupleOf* TupleOf::Make(Type* elt) {
static std::mutex guard;
std::lock_guard<std::mutex> lock(guard);
static std::map<Type*, TupleOf*> m;
auto it = m.find(elt);
if (it == m.end()) {
it = m.insert(std::make_pair(elt, new TupleOf(elt))).first;
}
return it->second;
}
// static
ListOf* ListOf::Make(Type* elt) {
static std::mutex guard;
std::lock_guard<std::mutex> lock(guard);
static std::map<Type*, ListOf*> m;
auto it = m.find(elt);
if (it == m.end()) {
it = m.insert(std::make_pair(elt, new ListOf(elt))).first;
}
return it->second;
}
int64_t TupleOrListOf::count(instance_ptr self) const {
if (!(*(layout**)self)) {
return 0;
}
return (*(layout**)self)->count;
}
int64_t TupleOrListOf::refcount(instance_ptr self) const {
if (!(*(layout**)self)) {
return 0;
}
return (*(layout**)self)->refcount;
}
void TupleOrListOf::constructor(instance_ptr self) {
constructor(self, 0, [](instance_ptr i, int64_t k) {});
}
void TupleOrListOf::destroy(instance_ptr selfPtr) {
layout_ptr& self = *(layout_ptr*)selfPtr;
if (!self) {
return;
}
self->refcount--;
if (self->refcount == 0) {
m_element_type->destroy(self->count, [&](int64_t k) {return eltPtr(self,k);});
free(self->data);
free(self);
}
}
void TupleOrListOf::copy_constructor(instance_ptr self, instance_ptr other) {
(*(layout**)self) = (*(layout**)other);
if (*(layout**)self) {
(*(layout**)self)->refcount++;
}
}
void TupleOrListOf::assign(instance_ptr self, instance_ptr other) {
layout* old = (*(layout**)self);
(*(layout**)self) = (*(layout**)other);
if (*(layout**)self) {
(*(layout**)self)->refcount++;
}
destroy((instance_ptr)&old);
}
void ListOf::append(instance_ptr self, instance_ptr other) {
layout_ptr& self_layout = *(layout_ptr*)self;
if (!self_layout) {
self_layout = (layout_ptr)malloc(sizeof(layout) + getEltType()->bytecount() * 1);
self_layout->count = 1;
self_layout->refcount = 1;
self_layout->reserved = 1;
self_layout->hash_cache = -1;
getEltType()->copy_constructor(eltPtr(self, 0), other);
} else {
if (self_layout->count == self_layout->reserved) {
int64_t new_reserved = self_layout->reserved * 1.25 + 1;
self_layout->data = (uint8_t*)realloc(self_layout->data, getEltType()->bytecount() * new_reserved);
self_layout->reserved = new_reserved;
}
getEltType()->copy_constructor(eltPtr(self, self_layout->count), other);
self_layout->count++;
}
}
size_t ListOf::reserved(instance_ptr self) {
layout_ptr& self_layout = *(layout_ptr*)self;
return self_layout->reserved;
}
void ListOf::reserve(instance_ptr self, size_t target) {
layout_ptr& self_layout = *(layout_ptr*)self;
if (target < self_layout->count) {
target = self_layout->count;
}
self_layout->data = (uint8_t*)realloc(self_layout->data, getEltType()->bytecount() * target);
self_layout->reserved = target;
}
void ListOf::remove(instance_ptr self, size_t index) {
layout_ptr& self_layout = *(layout_ptr*)self;
getEltType()->destroy(eltPtr(self, index));
memmove(eltPtr(self, index), eltPtr(self, index+1), (self_layout->count - index - 1) * getEltType()->bytecount());
self_layout->count--;
}
void ListOf::resize(instance_ptr self, size_t count) {
layout_ptr& self_layout = *(layout_ptr*)self;
if (count > self_layout->reserved) {
reserve(self, count);
}
if (count < self_layout->count) {
getEltType()->destroy(self_layout->count - count, [&](int64_t k) {return eltPtr(self,k + count);});
self_layout->count = count;
}
else if (count > self_layout->count) {
getEltType()->constructor(count - self_layout->count, [&](int64_t k) {return eltPtr(self,k + self_layout->count);});
self_layout->count = count;
}
}
void ListOf::resize(instance_ptr self, size_t count, instance_ptr value) {
layout_ptr& self_layout = *(layout_ptr*)self;
if (count > self_layout->reserved) {
reserve(self, count);
}
if (count < self_layout->count) {
getEltType()->destroy(self_layout->count - count, [&](int64_t k) {return eltPtr(self,k + count);});
self_layout->count = count;
}
else if (count > self_layout->count) {
getEltType()->copy_constructor(
count - self_layout->count,
[&](int64_t k) {return eltPtr(self,k + self_layout->count);},
[&](int64_t k) {return value;}
);
self_layout->count = count;
}
}
void ConstDict::_forwardTypesMayHaveChanged() {
m_name = "ConstDict(" + 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_bytes_per_key_subtree_pair = m_key->bytecount() + this->bytecount();
m_key_value_pair_type = Tuple::Make({m_key, m_value});
}
bool ConstDict::isBinaryCompatibleWithConcrete(Type* other) {
if (other->getTypeCategory() != m_typeCategory) {
return false;
}
ConstDict* otherO = (ConstDict*)other;
return m_key->isBinaryCompatibleWith(otherO->m_key) &&
m_value->isBinaryCompatibleWith(otherO->m_value);
}
// static
ConstDict* ConstDict::Make(Type* key, Type* value) {
static std::mutex guard;
std::lock_guard<std::mutex> lock(guard);
static std::map<std::pair<Type*, Type*>, ConstDict*> 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 ConstDict(key, value))).first;
}
return it->second;
}
void ConstDict::repr(instance_ptr self, ReprAccumulator& stream) {
PushReprState isNew(stream, self);
if (!isNew) {
stream << m_name << "(" << (void*)self << ")";
return;
}
stream << "{";
int32_t ct = count(self);
for (long k = 0; k < ct;k++) {
if (k > 0) {
stream << ", ";
}
m_key->repr(kvPairPtrKey(self,k),stream);
stream << ": ";
m_value->repr(kvPairPtrValue(self,k),stream);
}
stream << "}";
}
int32_t ConstDict::hash32(instance_ptr left) {
if (size(left) == 0) {
return 0x123456;
}
if ((*(layout**)left)->hash_cache == -1) {
Hash32Accumulator acc((int)getTypeCategory());
int32_t count = size(left);
acc.add(count);
for (long k = 0; k < count;k++) {
acc.add(m_key->hash32(kvPairPtrKey(left,k)));
acc.add(m_value->hash32(kvPairPtrValue(left,k)));
}
(*(layout**)left)->hash_cache = acc.get();
if ((*(layout**)left)->hash_cache == -1) {
(*(layout**)left)->hash_cache = -2;
}
}
return (*(layout**)left)->hash_cache;
}
//to make this fast(er), we do dict size comparison first, then keys, then values
char ConstDict::cmp(instance_ptr left, instance_ptr right) {
if (size(left) < size(right)) {
return -1;
}
if (size(left) > size(right)) {
return 1;
}
if (*(layout**)left == *(layout**)right) {
return 0;
}
int ct = count(left);
for (long k = 0; k < ct; k++) {
char res = m_key->cmp(kvPairPtrKey(left,k), kvPairPtrKey(right,k));
if (res) {
return res;
}
}
for (long k = 0; k < ct; k++) {
char res = m_value->cmp(
kvPairPtrValue(left,k),
kvPairPtrValue(right,k)
);
if (res) {
return res;
}
}
return 0;
}
void ConstDict::addDicts(instance_ptr lhs, instance_ptr rhs, instance_ptr output) const {
std::vector<instance_ptr> keep;
int64_t lhsCount = count(lhs);
int64_t rhsCount = count(rhs);
for (long k = 0; k < lhsCount; k++) {
instance_ptr lhsVal = kvPairPtrKey(lhs, k);
if (!lookupValueByKey(rhs, lhsVal)) {
keep.push_back(lhsVal);
}
}
constructor(output, rhsCount + keep.size(), false);
for (long k = 0; k < rhsCount; k++) {
m_key->copy_constructor(kvPairPtrKey(output,k), kvPairPtrKey(rhs, k));
m_value->copy_constructor(kvPairPtrValue(output,k), kvPairPtrValue(rhs, k));
}
for (long k = 0; k < keep.size(); k++) {
m_key->copy_constructor(kvPairPtrKey(output,k + rhsCount), keep[k]);
m_value->copy_constructor(kvPairPtrValue(output,k + rhsCount), keep[k] + m_bytes_per_key);
}
incKvPairCount(output, keep.size() + rhsCount);
sortKvPairs(output);
}
void ConstDict::subtractTupleOfKeysFromDict(instance_ptr lhs, instance_ptr rhs, instance_ptr output) const {
TupleOf* tupleType = tupleOfKeysType();
int64_t lhsCount = count(lhs);
int64_t rhsCount = tupleType->count(rhs);
std::set<int> remove;
for (long k = 0; k < rhsCount; k++) {
int64_t index = lookupIndexByKey(lhs, tupleType->eltPtr(rhs, k));
if (index != -1) {
remove.insert(index);
}
}
constructor(output, lhsCount - remove.size(), false);
long written = 0;
for (long k = 0; k < lhsCount; k++) {
if (remove.find(k) == remove.end()) {
m_key->copy_constructor(kvPairPtrKey(output,written), kvPairPtrKey(lhs, k));
m_value->copy_constructor(kvPairPtrValue(output,written), kvPairPtrValue(lhs, k));
written++;
}
}
incKvPairCount(output, written);
}
instance_ptr ConstDict::kvPairPtrKey(instance_ptr self, int64_t i) const {
if (!(*(layout**)self)) {
return self;
}
layout& record = **(layout**)self;
return record.data + m_bytes_per_key_value_pair * i;
}
instance_ptr ConstDict::kvPairPtrValue(instance_ptr self, int64_t i) const {
if (!(*(layout**)self)) {
return self;
}
layout& record = **(layout**)self;
return record.data + m_bytes_per_key_value_pair * i + m_bytes_per_key;
}
void ConstDict::incKvPairCount(instance_ptr self, int by) const {
if (by == 0) {
return;
}
layout& record = **(layout**)self;
record.count += by;
}
void ConstDict::sortKvPairs(instance_ptr self) const {
if (!*(layout**)self) {
return;
}
layout& record = **(layout**)self;
assert(!record.subpointers);
if (record.count <= 1) {
return;
}
else if (record.count == 2) {
if (m_key->cmp(kvPairPtrKey(self, 0), kvPairPtrKey(self,1)) > 0) {
m_key->swap(kvPairPtrKey(self,0), kvPairPtrKey(self,1));
m_value->swap(kvPairPtrValue(self,0), kvPairPtrValue(self,1));
}
return;
} else {
std::vector<int> indices;
for (long k=0;k<record.count;k++) {
indices.push_back(k);
}
std::sort(indices.begin(), indices.end(), [&](int l, int r) {
char res = m_key->cmp(kvPairPtrKey(self,l),kvPairPtrKey(self,r));
return res < 0;
});
//create a temporary buffer
std::vector<uint8_t> d;
d.resize(m_bytes_per_key_value_pair * record.count);
//final_lookup contains the location of each value in the original sort
for (long k = 0; k < indices.size(); k++) {
m_key->swap(kvPairPtrKey(self, indices[k]), &d[m_bytes_per_key_value_pair*k]);
m_value->swap(kvPairPtrValue(self, indices[k]), &d[m_bytes_per_key_value_pair*k+m_bytes_per_key]);
}
//now move them back
for (long k = 0; k < indices.size(); k++) {
m_key->swap(kvPairPtrKey(self, k), &d[m_bytes_per_key_value_pair*k]);
m_value->swap(kvPairPtrValue(self, k), &d[m_bytes_per_key_value_pair*k+m_bytes_per_key]);
}
}
}
instance_ptr ConstDict::keyTreePtr(instance_ptr self, int64_t i) const {
if (!(*(layout**)self)) {
return self;
}
layout& record = **(layout**)self;
return record.data + m_bytes_per_key_subtree_pair * i;
}
bool ConstDict::instanceIsSubtrees(instance_ptr self) const {
if (!(*(layout**)self)) {
return self;
}
layout& record = **(layout**)self;
return record.subpointers != 0;
}
int64_t ConstDict::refcount(instance_ptr self) const {
if (!(*(layout**)self)) {
return 0;
}
layout& record = **(layout**)self;
return record.refcount;