Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Jun 27, 2025. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraymap.c
More file actions
Latest commit
2756 lines (2490 loc) · 88.7 KB
/
Copy patharraymap.c
File metadata and controls
2756 lines (2490 loc) · 88.7 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
// For background on the hashtable design first implemented in AutoMap, see the following:
// https://github.com/brandtbucher/automap/blob/b787199d38d6bfa1b55484e5ea1e89b31cc1fa72/automap.c#L12
# include<math.h>
# include"stdbool.h"
# definePY_SSIZE_T_CLEAN
# include"Python.h"
# definePY_ARRAY_UNIQUE_SYMBOL AM_ARRAY_API
# defineNPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
# include"numpy/arrayobject.h"
# include"numpy/arrayscalars.h"
# include"numpy/halffloat.h"
# defineDEBUG_MSG_OBJ(msg, obj) \
fprintf(stderr, "--- %s: %i: %s: ", __FILE__, __LINE__, __FUNCTION__); \
fprintf(stderr, #msg " "); \
PyObject_Print(obj, stderr, 0); \
fprintf(stderr, "\n"); \
fflush(stderr); \
//------------------------------------------------------------------------------
// Common
staticPyTypeObjectAMType;
staticPyTypeObjectFAMIType;
staticPyTypeObjectFAMVType;
staticPyTypeObjectFAMType;
staticPyObject*NonUniqueError;
// The main storage "table" is an array of TableElement
typedefstructTableElement{
Py_ssize_tkeys_pos;
Py_hash_thash;
} TableElement;
// Table configuration; experimentation shows that these values work well:
# defineLOAD 0.9
# defineSCAN 16
conststaticsize_tUCS4_SIZE=sizeof(Py_UCS4);
// Partial, two-argument version of PyUnicode_FromKindAndData for consistent templating with bytes version.
staticinlinePyObject*
PyUnicode_FromUCS4AndData(constvoid*buffer, Py_ssize_tsize) {
returnPyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, size);
}
typedefenumKeysArrayType{
KAT_LIST=0, // must be falsy
KAT_INT8, // order matters as ranges of size are used in selection
KAT_INT16,
KAT_INT32,
KAT_INT64,
KAT_UINT8,
KAT_UINT16,
KAT_UINT32,
KAT_UINT64,
KAT_FLOAT16,
KAT_FLOAT32,
KAT_FLOAT64,
KAT_UNICODE,
KAT_STRING,
KAT_DTY,
KAT_DTM,
KAT_DTW,
KAT_DTD,
KAT_DTh,
KAT_DTm,
KAT_DTs,
KAT_DTms,
KAT_DTus,
KAT_DTns,
KAT_DTps,
KAT_DTfs,
KAT_DTas,
} KeysArrayType;
NPY_DATETIMEUNIT
dt_unit_from_array(PyArrayObject*a) {
// This is based on get_datetime_metadata_from_dtype in the NumPy source, but that function is private. This does not check that the dytpe is of the appropriate type.
PyArray_Descr*dt=PyArray_DESCR(a); // borrowed ref
PyArray_DatetimeMetaData*dma=&(((PyArray_DatetimeDTypeMetaData*)PyDataType_C_METADATA(dt))->meta);
returndma->base;
}
NPY_DATETIMEUNIT
dt_unit_from_scalar(PyDatetimeScalarObject*dts) {
// Based on convert_pyobject_to_datetime and related usage in datetime.c
PyArray_DatetimeMetaData*dma=&(dts->obmeta);
returndma->base;
}
KeysArrayType
at_to_kat(intarray_t, PyArrayObject*a) {
switch (array_t) {
caseNPY_INT64:
returnKAT_INT64;
caseNPY_INT32:
returnKAT_INT32;
caseNPY_INT16:
returnKAT_INT16;
caseNPY_INT8:
returnKAT_INT8;
caseNPY_UINT64:
returnKAT_UINT64;
caseNPY_UINT32:
returnKAT_UINT32;
caseNPY_UINT16:
returnKAT_UINT16;
caseNPY_UINT8:
returnKAT_UINT8;
caseNPY_FLOAT64:
returnKAT_FLOAT64;
caseNPY_FLOAT32:
returnKAT_FLOAT32;
caseNPY_FLOAT16:
returnKAT_FLOAT16;
caseNPY_UNICODE:
returnKAT_UNICODE;
caseNPY_STRING:
returnKAT_STRING;
caseNPY_DATETIME: {
NPY_DATETIMEUNITdtu=dt_unit_from_array(a);
switch (dtu) {
caseNPY_FR_Y:
returnKAT_DTY;
caseNPY_FR_M:
returnKAT_DTM;
caseNPY_FR_W:
returnKAT_DTW;
caseNPY_FR_D:
returnKAT_DTD;
caseNPY_FR_h:
returnKAT_DTh;
caseNPY_FR_m:
returnKAT_DTm;
caseNPY_FR_s:
returnKAT_DTs;
caseNPY_FR_ms:
returnKAT_DTms;
caseNPY_FR_us:
returnKAT_DTus;
caseNPY_FR_ns:
returnKAT_DTns;
caseNPY_FR_ps:
returnKAT_DTps;
caseNPY_FR_fs:
returnKAT_DTfs;
caseNPY_FR_as:
returnKAT_DTas;
caseNPY_FR_ERROR:
caseNPY_FR_GENERIC:
returnKAT_LIST; // fall back to list
}
}
default:
returnKAT_LIST;
}
}
// To determine when we can use direct array lookups, this function return 1 if we match, 0 if we do not match. Given a keys array type and the kind of lookup key, return true only for the largest KAT types.
int
kat_is_kind(KeysArrayTypekat, charkind) {
switch (kat) {
caseKAT_INT64:
// case KAT_INT32:
// case KAT_INT16:
// case KAT_INT8:
returnkind=='i';
caseKAT_UINT64:
// case KAT_UINT32:
// case KAT_UINT16:
// case KAT_UINT8:
returnkind=='u';
caseKAT_FLOAT64:
// case KAT_FLOAT32:
// case KAT_FLOAT16:
returnkind=='f';
caseKAT_UNICODE:
returnkind=='U';
caseKAT_STRING:
returnkind=='S';
caseKAT_DTY:
caseKAT_DTM:
caseKAT_DTW:
caseKAT_DTD:
caseKAT_DTh:
caseKAT_DTm:
caseKAT_DTs:
caseKAT_DTms:
caseKAT_DTus:
caseKAT_DTns:
caseKAT_DTps:
caseKAT_DTfs:
caseKAT_DTas:
returnkind=='M';
default:
return0;
}
}
// Given a KAT, determine if it matches a NumPy dt64 unit.
bool
kat_is_datetime_unit(KeysArrayTypekat, NPY_DATETIMEUNITunit) {
switch (kat) {
caseKAT_DTY:
if (unit==NPY_FR_Y ) {return true;}
break;
caseKAT_DTM:
if (unit==NPY_FR_M ) {return true;}
break;
caseKAT_DTW:
if (unit==NPY_FR_W ) {return true;}
break;
caseKAT_DTD:
if (unit==NPY_FR_D ) {return true;}
break;
caseKAT_DTh:
if (unit==NPY_FR_h ) {return true;}
break;
caseKAT_DTm:
if (unit==NPY_FR_m ) {return true;}
break;
caseKAT_DTs:
if (unit==NPY_FR_s ) {return true;}
break;
caseKAT_DTms:
if (unit==NPY_FR_ms) {return true;}
break;
caseKAT_DTus:
if (unit==NPY_FR_us) {return true;}
break;
caseKAT_DTns:
if (unit==NPY_FR_ns) {return true;}
break;
caseKAT_DTps:
if (unit==NPY_FR_ps) {return true;}
break;
caseKAT_DTfs:
if (unit==NPY_FR_fs) {return true;}
break;
caseKAT_DTas:
if (unit==NPY_FR_as) {return true;}
break;
default: // non dt64 KATs
return false;
}
return false;
}
typedefstructFAMObject{
PyObject_HEAD
Py_ssize_ttable_size;
TableElement*table; // an array of TableElement structs
PyObject*keys;
KeysArrayTypekeys_array_type;
Py_ssize_tkeys_size;
Py_UCS4*key_buffer;
} FAMObject;
typedefenumViewKind{
ITEMS,
KEYS,
VALUES,
} ViewKind;
// Return the end pointer, or the pointer to the location after the last valid character. The end pointer minus the start pointer is the number of characters. For an empty string, all characters are NULL, and the start pointer and end pointer should be equal. NOTE: would like to use strchr(str, '\0') instead of this routine, but some buffers might not have a null terminator and stread by full to the the dt_size.
staticinlinePy_UCS4*
ucs4_get_end_p(Py_UCS4*p_start, Py_ssize_tdt_size) {
for (Py_UCS4*p=p_start+dt_size-1; p >= p_start; p--) {
if (*p!='\0') {
returnp+1; // 1 after first non-null
}
}
returnp_start;
}
staticinlinechar*
char_get_end_p(char*p_start, Py_ssize_tdt_size) {
for (char*p=p_start+dt_size-1; p >= p_start; p--) {
if (*p!='\0') {
returnp+1; // 1 after first non-null
}
}
returnp_start;
}
// This masks the input with INT64_MAX, which removes the MSB; we then cast to an int64; the range is now between 0 and INT64_MAX. We then use the MSB of the original value; if set, we negate the number, producing negative values for the upper half of the uint64 range. Note that we only need to check for hash -1 in this branch.
staticinlinePy_hash_t
uint_to_hash(npy_uint64v) {
Py_hash_thash= (Py_hash_t)(v&INT64_MAX);
if (v >> 63) {
hash=-hash;
}
if (hash==-1) { // might happen due to overflow on 32 bit systems
return-2;
}
returnhash;
}
staticinlinePy_hash_t
int_to_hash(npy_int64v) {
Py_hash_thash= (Py_hash_t)v;
if (hash==-1) {
return-2;
}
returnhash;
}
// This is a adapted from https://github.com/python/cpython/blob/ba65a065cf07a7a9f53be61057a090f7311a5ad7/Python/pyhash.c#L92
#defineHASH_MODULUS (((size_t)1 << 61) - 1)
#defineHASH_BITS 61
staticinlinePy_hash_t
double_to_hash(doublev)
{
inte, sign;
doublem;
Py_uhash_tx, y;
if (isinf(v)) {
returnv>0 ? 314159 : -314159;
}
if (isnan(v)) {
return0;
}
m=frexp(v, &e);
sign=1;
if (m<0) {
sign=-1;
m=-m;
}
x=0;
while (m) {
x= ((x << 28) &HASH_MODULUS) | x >> (HASH_BITS-28);
m *= 268435456.0; /* 2**28 */
e-=28;
y= (Py_uhash_t)m; /* pull out integer part */
m-=y;
x+=y;
if (x >= HASH_MODULUS)
x-=HASH_MODULUS;
}
e=e >= 0 ? e % HASH_BITS : HASH_BITS-1-((-1-e) % HASH_BITS);
x= ((x << e) &HASH_MODULUS) | x >> (HASH_BITS-e);
x=x*sign;
if (x== (Py_uhash_t)-1)
x= (Py_uhash_t)-2;
return (Py_hash_t)x;
}
// The `str` arg is a pointer to a C-array of Py_UCS4; we will only read `len` characters from this. This is a "djb2" hash algorithm.
staticinlinePy_hash_t
unicode_to_hash(Py_UCS4*str, Py_ssize_tlen) {
Py_UCS4*p=str;
Py_UCS4*p_end=str+len;
Py_hash_thash=5381;
while (p<p_end) {
hash= ((hash << 5) +hash) +*p++;
}
if (hash==-1) {
return-2;
}
returnhash;
}
staticinlinePy_hash_t
string_to_hash(char*str, Py_ssize_tlen) {
char*p=str;
char*p_end=str+len;
Py_hash_thash=5381;
while (p<p_end) {
hash= ((hash << 5) +hash) +*p++;
}
if (hash==-1) {
return-2;
}
returnhash;
}
//------------------------------------------------------------------------------
// the global int_cache is shared among all instances
staticPyObject*int_cache=NULL;
// NOTE: this used to be a Py_ssize_t, which can be 32 bits on some machines and might easily overflow with a few very large indices. Using an explicit 64-bit int seems safer
staticnpy_int64key_count_global=0;
// Fill the int_cache up to size_needed with PyObject ints; `size` is not the key_count_global.
staticint
int_cache_fill(Py_ssize_tsize_needed)
{
PyObject*item;
if (!int_cache) {
int_cache=PyList_New(0);
if (!int_cache) {
return-1;
}
}
for (Py_ssize_ti=PyList_GET_SIZE(int_cache); i<size_needed; i++) {
item=PyLong_FromSsize_t(i);
if (!item) {
return-1;
}
if (PyList_Append(int_cache, item)) {
Py_DECREF(item);
return-1;
}
Py_DECREF(item);
}
return0;
}
// Given the current key_count_global, remove cache elements only if the key_count is less than the the current size of the int_cache.
void
int_cache_remove(Py_ssize_tkey_count)
{
if (!key_count) {
Py_CLEAR(int_cache);
}
elseif (key_count<PyList_GET_SIZE(int_cache)) {
// del int_cache[key_count:]
PyList_SetSlice(int_cache, key_count, PyList_GET_SIZE(int_cache), NULL);
}
}
//------------------------------------------------------------------------------
// FrozenAutoMapIterator functions
typedefstructFAMIObject {
PyObject_HEAD
FAMObject*fam;
PyArrayObject*keys_array;
ViewKindkind;
boolreversed;
Py_ssize_tindex; // current index state, mutated in-place
} FAMIObject;
staticvoid
fami_dealloc(FAMIObject*self)
{
Py_DECREF(self->fam);
PyObject_Del((PyObject*)self);
}
staticFAMIObject*
fami_iter(FAMIObject*self)
{
Py_INCREF(self);
returnself;
}
// For a FAMI, Return appropriate PyObject for items, keys, and values. When values are needed they are retrieved from the int_cache. For consistency with NumPy array iteration, arrays use PyArray_ToScalar instead of PyArray_GETITEM.
staticPyObject*
fami_iternext(FAMIObject*self)
{
Py_ssize_tindex;
if (self->reversed) {
index=self->fam->keys_size-++self->index;
if (index<0) {
returnNULL;
}
}
else {
index=self->index++;
}
if (self->fam->keys_size <= index) {
returnNULL;
}
switch (self->kind) {
caseITEMS: {
if (self->fam->keys_array_type) {
returnPyTuple_Pack(
2,
PyArray_ToScalar(PyArray_GETPTR1(self->keys_array, index), self->keys_array),
PyList_GET_ITEM(int_cache, index)
);
}
else {
returnPyTuple_Pack(
2,
PyList_GET_ITEM(self->fam->keys, index),
PyList_GET_ITEM(int_cache, index)
);
}
}
caseKEYS: {
if (self->fam->keys_array_type) {
returnPyArray_ToScalar(PyArray_GETPTR1(self->keys_array, index), self->keys_array);
}
else {
PyObject*yield=PyList_GET_ITEM(self->fam->keys, index);
Py_INCREF(yield);
returnyield;
}
}
caseVALUES: {
PyObject*yield=PyList_GET_ITEM(int_cache, index);
Py_INCREF(yield);
returnyield;
}
}
Py_UNREACHABLE();
}
staticPyObject*
fami_length_hint(FAMIObject*self)
{
Py_ssize_tlen=Py_MAX(0, self->fam->keys_size-self->index);
returnPyLong_FromSsize_t(len);
}
staticPyObject*fami_new(FAMObject*, ViewKind, bool);
staticPyObject*
fami_reversed(FAMIObject*self)
{
returnfami_new(self->fam, self->kind, !self->reversed);
}
staticPyMethodDeffami_methods[] = {
{"__length_hint__", (PyCFunction)fami_length_hint, METH_NOARGS, NULL},
{"__reversed__", (PyCFunction)fami_reversed, METH_NOARGS, NULL},
{NULL},
};
staticPyTypeObjectFAMIType= {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_basicsize=sizeof(FAMIObject),
.tp_dealloc= (destructor) fami_dealloc,
.tp_iter= (getiterfunc) fami_iter,
.tp_iternext= (iternextfunc) fami_iternext,
.tp_methods=fami_methods,
.tp_name="arraymap.FrozenAutoMapIterator",
};
staticPyObject*
fami_new(FAMObject*fam, ViewKindkind, boolreversed)
{
FAMIObject*fami=PyObject_New(FAMIObject, &FAMIType);
if (!fami) {
returnNULL;
}
Py_INCREF(fam);
fami->fam=fam;
if (fam->keys_array_type) {
fami->keys_array= (PyArrayObject*)fam->keys;
}
else {
fami->keys_array=NULL;
}
fami->kind=kind;
fami->reversed=reversed;
fami->index=0;
return (PyObject*)fami;
}
//------------------------------------------------------------------------------
// FrozenAutoMapView functions
// A FAMVObject contains a reference to the FAM from which it was derived
typedefstructFAMVObject{
PyObject_HEAD
FAMObject*fam;
ViewKindkind;
} FAMVObject;
# defineFAMV_SET_OP(name, op) \
static PyObject * \
name(PyObject *left, PyObject *right) \
{ \
left = PySet_New(left); \
if (!left) { \
return NULL; \
} \
right = PySet_New(right); \
if (!right) { \
Py_DECREF(left); \
return NULL; \
} \
PyObject *result = PyNumber_InPlace##op(left, right); \
Py_DECREF(left); \
Py_DECREF(right); \
return result; \
}
FAMV_SET_OP(famv_and, And)
FAMV_SET_OP(famv_or, Or)
FAMV_SET_OP(famv_subtract, Subtract)
FAMV_SET_OP(famv_xor, Xor)
# undef FAMV_SET_OP
staticPyNumberMethodsfamv_as_number= {
.nb_and= (binaryfunc) famv_and,
.nb_or= (binaryfunc) famv_or,
.nb_subtract= (binaryfunc) famv_subtract,
.nb_xor= (binaryfunc) famv_xor,
};
staticintfam_contains(FAMObject*, PyObject*);
staticPyObject*famv_fami_new(FAMVObject*);
staticint
famv_contains(FAMVObject*self, PyObject*other)
{
if (self->kind==KEYS) {
returnfam_contains(self->fam, other);
}
PyObject*iterator=famv_fami_new(self);
if (!iterator) {
return-1;
}
intresult=PySequence_Contains(iterator, other);
Py_DECREF(iterator);
returnresult;
}
staticPySequenceMethodsfamv_as_sequence= {
.sq_contains= (objobjproc) famv_contains,
};
staticvoid
famv_dealloc(FAMVObject*self)
{
Py_DECREF(self->fam);
PyObject_Del((PyObject*)self);
}
staticPyObject*
famv_fami_new(FAMVObject*self)
{
returnfami_new(self->fam, self->kind, false);
}
staticPyObject*
famv_length_hint(FAMVObject*self)
{
returnPyLong_FromSsize_t(self->fam->keys_size);
}
staticPyObject*
famv_reversed(FAMVObject*self)
{
returnfami_new(self->fam, self->kind, true);
}
staticPyObject*
famv_isdisjoint(FAMVObject*self, PyObject*other)
{
PyObject*intersection=famv_and((PyObject*)self, other);
if (!intersection) {
returnNULL;
}
Py_ssize_tresult=PySet_GET_SIZE(intersection);
Py_DECREF(intersection);
returnPyBool_FromLong(result);
}
staticPyObject*
famv_richcompare(FAMVObject*self, PyObject*other, intop)
{
PyObject*left=PySet_New((PyObject*)self);
if (!left) {
returnNULL;
}
PyObject*right=PySet_New(other);
if (!right) {
Py_DECREF(left);
returnNULL;
}
PyObject*result=PyObject_RichCompare(left, right, op);
Py_DECREF(left);
Py_DECREF(right);
returnresult;
}
staticPyMethodDeffamv_methods[] = {
{"__length_hint__", (PyCFunction) famv_length_hint, METH_NOARGS, NULL},
{"__reversed__", (PyCFunction) famv_reversed, METH_NOARGS, NULL},
{"isdisjoint", (PyCFunction) famv_isdisjoint, METH_O, NULL},
{NULL},
};
staticPyTypeObjectFAMVType= {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_as_number=&famv_as_number,
.tp_as_sequence=&famv_as_sequence,
.tp_basicsize=sizeof(FAMVObject),
.tp_dealloc= (destructor) famv_dealloc,
.tp_iter= (getiterfunc) famv_fami_new,
.tp_methods=famv_methods,
.tp_name="arraymap.FrozenAutoMapView",
.tp_richcompare= (richcmpfunc) famv_richcompare,
};
staticPyObject*
famv_new(FAMObject*fam, ViewKindkind)
{
FAMVObject*famv= (FAMVObject*)PyObject_New(FAMVObject, &FAMVType);
if (!famv) {
returnNULL;
}
famv->kind=kind;
famv->fam=fam;
Py_INCREF(fam);
return (PyObject*)famv;
}
//------------------------------------------------------------------------------
// FrozenAutoMap functions
// Given a key and a computed hash, return the table_pos if that hash and key are found, or if not, the first table position that has not been assigned. Return -1 on error.
staticPy_ssize_t
lookup_hash_obj(FAMObject*self, PyObject*key, Py_hash_thash)
{
TableElement*table=self->table;
Py_ssize_tmask=self->table_size-1;
Py_hash_tmixin=Py_ABS(hash);
Py_ssize_ttable_pos=hash&mask;
PyObject*guess=NULL;
PyObject*keys=self->keys;
intresult=-1;
Py_hash_th=0;
while (1) {
for (Py_ssize_ti=0; i<SCAN; i++) {
h=table[table_pos].hash;
if (h==-1) { // Miss. Found a position that can be used for insertion.
returntable_pos;
}
if (h!=hash) { // Collision.
table_pos++;
continue;
}
guess=PyList_GET_ITEM(keys, table[table_pos].keys_pos);
if (guess==key) { // Hit. Object ID comparison
returntable_pos;
}
result=PyObject_RichCompareBool(guess, key, Py_EQ);
if (result<0) { // Error.
return-1;
}
if (result) { // Hit.
returntable_pos;
}
table_pos++;
}
table_pos= (5* (table_pos-SCAN) + (mixin >>= 1) +1) &mask;
}
}
// Used for both integer and datetime types; for this reason kat is passed in separately.
staticPy_ssize_t
lookup_hash_int(FAMObject*self, npy_int64key, Py_hash_thash, KeysArrayTypekat)
{
TableElement*table=self->table;
Py_ssize_tmask=self->table_size-1;
Py_hash_tmixin=Py_ABS(hash);
Py_ssize_ttable_pos=hash&mask; // taking the modulo
PyArrayObject*a= (PyArrayObject*)self->keys;
npy_int64k=0;
Py_hash_th=0;
while (1) {
for (Py_ssize_ti=0; i<SCAN; i++) {
h=table[table_pos].hash;
if (h==-1) { // Miss. Position that can be used for insertion.
returntable_pos;
}
if (h!=hash) {
table_pos++;
continue;
}
switch (kat) {
caseKAT_INT64:
k=*(npy_int64*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
caseKAT_INT32:
k=*(npy_int32*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
caseKAT_INT16:
k=*(npy_int16*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
caseKAT_INT8:
k=*(npy_int8*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
default:
return-1;
}
if (key==k) {
returntable_pos;
}
table_pos++;
}
table_pos= (5* (table_pos-SCAN) + (mixin >>= 1) +1) &mask;
}
}
// NOTE: kat is passed in separately to match the interface of lookup_hash_int.
staticPy_ssize_t
lookup_hash_uint(FAMObject*self, npy_uint64key, Py_hash_thash, KeysArrayTypekat)
{
TableElement*table=self->table;
Py_ssize_tmask=self->table_size-1;
Py_hash_tmixin=Py_ABS(hash);
Py_ssize_ttable_pos=hash&mask;
PyArrayObject*a= (PyArrayObject*)self->keys;
npy_uint64k=0;
Py_hash_th=0;
while (1) {
for (Py_ssize_ti=0; i<SCAN; i++) {
h=table[table_pos].hash;
if (h==-1) {
returntable_pos;
}
if (h!=hash) {
table_pos++;
continue;
}
switch (kat) {
caseKAT_UINT64:
k=*(npy_uint64*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
caseKAT_UINT32:
k=*(npy_uint32*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
caseKAT_UINT16:
k=*(npy_uint16*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
caseKAT_UINT8:
k=*(npy_uint8*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
default:
return-1;
}
if (key==k) {
returntable_pos;
}
table_pos++;
}
table_pos= (5* (table_pos-SCAN) + (mixin >>= 1) +1) &mask;
}
}
// NOTE: kat is passed in separately to match the interface of lookup_hash_int
staticPy_ssize_t
lookup_hash_double(FAMObject*self, npy_doublekey, Py_hash_thash, KeysArrayTypekat)
{
TableElement*table=self->table;
Py_ssize_tmask=self->table_size-1;
Py_hash_tmixin=Py_ABS(hash);
Py_ssize_ttable_pos=hash&mask;
PyArrayObject*a= (PyArrayObject*)self->keys;
npy_doublek=0;
Py_hash_th=0;
while (1) {
for (Py_ssize_ti=0; i<SCAN; i++) {
h=table[table_pos].hash;
if (h==-1) {
returntable_pos;
}
if (h!=hash) {
table_pos++;
continue;
}
switch (kat) {
caseKAT_FLOAT64:
k=*(npy_double*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
caseKAT_FLOAT32:
k=*(npy_float*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
caseKAT_FLOAT16:
k=npy_half_to_double(*(npy_half*)PyArray_GETPTR1(a, table[table_pos].keys_pos));
break;
default:
return-1;
}
if (key==k) {
returntable_pos;
}
table_pos++;
}
table_pos= (5* (table_pos-SCAN) + (mixin >>= 1) +1) &mask;
}
}
// Compare a passed Py_UCS4 array to stored keys. This does not use any dynamic memory. Returns -1 on error.
staticPy_ssize_t
lookup_hash_unicode(
FAMObject*self,
Py_UCS4*key,
Py_ssize_tkey_size,
Py_hash_thash)
{
TableElement*table=self->table;
Py_ssize_tmask=self->table_size-1;
Py_hash_tmixin=Py_ABS(hash);
Py_ssize_ttable_pos=hash&mask;
PyArrayObject*a= (PyArrayObject*)self->keys;
Py_ssize_tdt_size=PyArray_ITEMSIZE(a) / UCS4_SIZE;
Py_ssize_tcmp_bytes=Py_MIN(key_size, dt_size) *UCS4_SIZE;
Py_hash_th=0;
Py_UCS4*p_start=NULL;
while (1) {
for (Py_ssize_ti=0; i<SCAN; i++) {
h=table[table_pos].hash;
if (h==-1) {
returntable_pos;
}
if (h!=hash) {
table_pos++;
continue;
}
p_start= (Py_UCS4*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
// memcmp returns 0 on match
if (!memcmp(p_start, key, cmp_bytes)) {
returntable_pos;
}
table_pos++;
}
table_pos= (5* (table_pos-SCAN) + (mixin >>= 1) +1) &mask;
}
}
// Compare a passed char array to stored keys. This does not use any dynamic memory. Returns -1 on error.
staticPy_ssize_t
lookup_hash_string(
FAMObject*self,
char*key,
Py_ssize_tkey_size,
Py_hash_thash)
{
TableElement*table=self->table;
Py_ssize_tmask=self->table_size-1;
Py_hash_tmixin=Py_ABS(hash);
Py_ssize_ttable_pos=hash&mask;
PyArrayObject*a= (PyArrayObject*)self->keys;
Py_ssize_tdt_size=PyArray_ITEMSIZE(a);
Py_ssize_tcmp_bytes=Py_MIN(key_size, dt_size);
Py_hash_th=0;
char*p_start=NULL;
while (1) {
for (Py_ssize_ti=0; i<SCAN; i++) {
h=table[table_pos].hash;
if (h==-1) {
returntable_pos;
}
if (h!=hash) {
table_pos++;
continue;
}
p_start= (char*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
// memcmp returns 0 on match
if (!memcmp(p_start, key, cmp_bytes)) {
returntable_pos;
}
table_pos++;
}
table_pos= (5* (table_pos-SCAN) + (mixin >>= 1) +1) &mask;
}
}
staticPy_ssize_t
lookup_int(FAMObject*self, PyObject*key) {