-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharraymap.c
2756 lines (2490 loc) · 88.7 KB
/
arraymap.c
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"
# define PY_SSIZE_T_CLEAN
# include "Python.h"
# define PY_ARRAY_UNIQUE_SYMBOL AM_ARRAY_API
# define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
# include "numpy/arrayobject.h"
# include "numpy/arrayscalars.h"
# include "numpy/halffloat.h"
# define DEBUG_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
static PyTypeObject AMType;
static PyTypeObject FAMIType;
static PyTypeObject FAMVType;
static PyTypeObject FAMType;
static PyObject *NonUniqueError;
// The main storage "table" is an array of TableElement
typedef struct TableElement{
Py_ssize_t keys_pos;
Py_hash_t hash;
} TableElement;
// Table configuration; experimentation shows that these values work well:
# define LOAD 0.9
# define SCAN 16
const static size_t UCS4_SIZE = sizeof(Py_UCS4);
// Partial, two-argument version of PyUnicode_FromKindAndData for consistent templating with bytes version.
static inline PyObject*
PyUnicode_FromUCS4AndData(const void *buffer, Py_ssize_t size) {
return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, size);
}
typedef enum KeysArrayType{
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);
return dma->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);
return dma->base;
}
KeysArrayType
at_to_kat(int array_t, PyArrayObject* a) {
switch (array_t) {
case NPY_INT64:
return KAT_INT64;
case NPY_INT32:
return KAT_INT32;
case NPY_INT16:
return KAT_INT16;
case NPY_INT8:
return KAT_INT8;
case NPY_UINT64:
return KAT_UINT64;
case NPY_UINT32:
return KAT_UINT32;
case NPY_UINT16:
return KAT_UINT16;
case NPY_UINT8:
return KAT_UINT8;
case NPY_FLOAT64:
return KAT_FLOAT64;
case NPY_FLOAT32:
return KAT_FLOAT32;
case NPY_FLOAT16:
return KAT_FLOAT16;
case NPY_UNICODE:
return KAT_UNICODE;
case NPY_STRING:
return KAT_STRING;
case NPY_DATETIME: {
NPY_DATETIMEUNIT dtu = dt_unit_from_array(a);
switch (dtu) {
case NPY_FR_Y:
return KAT_DTY;
case NPY_FR_M:
return KAT_DTM;
case NPY_FR_W:
return KAT_DTW;
case NPY_FR_D:
return KAT_DTD;
case NPY_FR_h:
return KAT_DTh;
case NPY_FR_m:
return KAT_DTm;
case NPY_FR_s:
return KAT_DTs;
case NPY_FR_ms:
return KAT_DTms;
case NPY_FR_us:
return KAT_DTus;
case NPY_FR_ns:
return KAT_DTns;
case NPY_FR_ps:
return KAT_DTps;
case NPY_FR_fs:
return KAT_DTfs;
case NPY_FR_as:
return KAT_DTas;
case NPY_FR_ERROR:
case NPY_FR_GENERIC:
return KAT_LIST; // fall back to list
}
}
default:
return KAT_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(KeysArrayType kat, char kind) {
switch (kat) {
case KAT_INT64:
// case KAT_INT32:
// case KAT_INT16:
// case KAT_INT8:
return kind == 'i';
case KAT_UINT64:
// case KAT_UINT32:
// case KAT_UINT16:
// case KAT_UINT8:
return kind == 'u';
case KAT_FLOAT64:
// case KAT_FLOAT32:
// case KAT_FLOAT16:
return kind == 'f';
case KAT_UNICODE:
return kind == 'U';
case KAT_STRING:
return kind == 'S';
case KAT_DTY:
case KAT_DTM:
case KAT_DTW:
case KAT_DTD:
case KAT_DTh:
case KAT_DTm:
case KAT_DTs:
case KAT_DTms:
case KAT_DTus:
case KAT_DTns:
case KAT_DTps:
case KAT_DTfs:
case KAT_DTas:
return kind == 'M';
default:
return 0;
}
}
// Given a KAT, determine if it matches a NumPy dt64 unit.
bool
kat_is_datetime_unit(KeysArrayType kat, NPY_DATETIMEUNIT unit) {
switch (kat) {
case KAT_DTY:
if (unit == NPY_FR_Y ) {return true;}
break;
case KAT_DTM:
if (unit == NPY_FR_M ) {return true;}
break;
case KAT_DTW:
if (unit == NPY_FR_W ) {return true;}
break;
case KAT_DTD:
if (unit == NPY_FR_D ) {return true;}
break;
case KAT_DTh:
if (unit == NPY_FR_h ) {return true;}
break;
case KAT_DTm:
if (unit == NPY_FR_m ) {return true;}
break;
case KAT_DTs:
if (unit == NPY_FR_s ) {return true;}
break;
case KAT_DTms:
if (unit == NPY_FR_ms) {return true;}
break;
case KAT_DTus:
if (unit == NPY_FR_us) {return true;}
break;
case KAT_DTns:
if (unit == NPY_FR_ns) {return true;}
break;
case KAT_DTps:
if (unit == NPY_FR_ps) {return true;}
break;
case KAT_DTfs:
if (unit == NPY_FR_fs) {return true;}
break;
case KAT_DTas:
if (unit == NPY_FR_as) {return true;}
break;
default: // non dt64 KATs
return false;
}
return false;
}
typedef struct FAMObject{
PyObject_HEAD
Py_ssize_t table_size;
TableElement *table; // an array of TableElement structs
PyObject *keys;
KeysArrayType keys_array_type;
Py_ssize_t keys_size;
Py_UCS4* key_buffer;
} FAMObject;
typedef enum ViewKind{
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.
static inline Py_UCS4*
ucs4_get_end_p(Py_UCS4* p_start, Py_ssize_t dt_size) {
for (Py_UCS4* p = p_start + dt_size - 1; p >= p_start; p--) {
if (*p != '\0') {
return p + 1; // 1 after first non-null
}
}
return p_start;
}
static inline char*
char_get_end_p(char* p_start, Py_ssize_t dt_size) {
for (char* p = p_start + dt_size - 1; p >= p_start; p--) {
if (*p != '\0') {
return p + 1; // 1 after first non-null
}
}
return p_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.
static inline Py_hash_t
uint_to_hash(npy_uint64 v) {
Py_hash_t hash = (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;
}
return hash;
}
static inline Py_hash_t
int_to_hash(npy_int64 v) {
Py_hash_t hash = (Py_hash_t)v;
if (hash == -1) {
return -2;
}
return hash;
}
// This is a adapted from https://github.com/python/cpython/blob/ba65a065cf07a7a9f53be61057a090f7311a5ad7/Python/pyhash.c#L92
#define HASH_MODULUS (((size_t)1 << 61) - 1)
#define HASH_BITS 61
static inline Py_hash_t
double_to_hash(double v)
{
int e, sign;
double m;
Py_uhash_t x, y;
if (isinf(v)) {
return v > 0 ? 314159 : -314159;
}
if (isnan(v)) {
return 0;
}
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.
static inline Py_hash_t
unicode_to_hash(Py_UCS4 *str, Py_ssize_t len) {
Py_UCS4* p = str;
Py_UCS4* p_end = str + len;
Py_hash_t hash = 5381;
while (p < p_end) {
hash = ((hash << 5) + hash) + *p++;
}
if (hash == -1) {
return -2;
}
return hash;
}
static inline Py_hash_t
string_to_hash(char *str, Py_ssize_t len) {
char* p = str;
char* p_end = str + len;
Py_hash_t hash = 5381;
while (p < p_end) {
hash = ((hash << 5) + hash) + *p++;
}
if (hash == -1) {
return -2;
}
return hash;
}
//------------------------------------------------------------------------------
// the global int_cache is shared among all instances
static PyObject *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
static npy_int64 key_count_global = 0;
// Fill the int_cache up to size_needed with PyObject ints; `size` is not the key_count_global.
static int
int_cache_fill(Py_ssize_t size_needed)
{
PyObject *item;
if (!int_cache) {
int_cache = PyList_New(0);
if (!int_cache) {
return -1;
}
}
for (Py_ssize_t i = 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);
}
return 0;
}
// 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_t key_count)
{
if (!key_count) {
Py_CLEAR(int_cache);
}
else if (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
typedef struct FAMIObject {
PyObject_HEAD
FAMObject *fam;
PyArrayObject* keys_array;
ViewKind kind;
bool reversed;
Py_ssize_t index; // current index state, mutated in-place
} FAMIObject;
static void
fami_dealloc(FAMIObject *self)
{
Py_DECREF(self->fam);
PyObject_Del((PyObject *)self);
}
static FAMIObject *
fami_iter(FAMIObject *self)
{
Py_INCREF(self);
return self;
}
// 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.
static PyObject *
fami_iternext(FAMIObject *self)
{
Py_ssize_t index;
if (self->reversed) {
index = self->fam->keys_size - ++self->index;
if (index < 0) {
return NULL;
}
}
else {
index = self->index++;
}
if (self->fam->keys_size <= index) {
return NULL;
}
switch (self->kind) {
case ITEMS: {
if (self->fam->keys_array_type) {
return PyTuple_Pack(
2,
PyArray_ToScalar(PyArray_GETPTR1(self->keys_array, index), self->keys_array),
PyList_GET_ITEM(int_cache, index)
);
}
else {
return PyTuple_Pack(
2,
PyList_GET_ITEM(self->fam->keys, index),
PyList_GET_ITEM(int_cache, index)
);
}
}
case KEYS: {
if (self->fam->keys_array_type) {
return PyArray_ToScalar(PyArray_GETPTR1(self->keys_array, index), self->keys_array);
}
else {
PyObject* yield = PyList_GET_ITEM(self->fam->keys, index);
Py_INCREF(yield);
return yield;
}
}
case VALUES: {
PyObject *yield = PyList_GET_ITEM(int_cache, index);
Py_INCREF(yield);
return yield;
}
}
Py_UNREACHABLE();
}
static PyObject *
fami_length_hint(FAMIObject *self)
{
Py_ssize_t len = Py_MAX(0, self->fam->keys_size - self->index);
return PyLong_FromSsize_t(len);
}
static PyObject *fami_new(FAMObject *, ViewKind, bool);
static PyObject *
fami_reversed(FAMIObject *self)
{
return fami_new(self->fam, self->kind, !self->reversed);
}
static PyMethodDef fami_methods[] = {
{"__length_hint__", (PyCFunction)fami_length_hint, METH_NOARGS, NULL},
{"__reversed__", (PyCFunction)fami_reversed, METH_NOARGS, NULL},
{NULL},
};
static PyTypeObject FAMIType = {
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",
};
static PyObject *
fami_new(FAMObject *fam, ViewKind kind, bool reversed)
{
FAMIObject *fami = PyObject_New(FAMIObject, &FAMIType);
if (!fami) {
return NULL;
}
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
typedef struct FAMVObject{
PyObject_HEAD
FAMObject *fam;
ViewKind kind;
} FAMVObject;
# define FAMV_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
static PyNumberMethods famv_as_number = {
.nb_and = (binaryfunc) famv_and,
.nb_or = (binaryfunc) famv_or,
.nb_subtract = (binaryfunc) famv_subtract,
.nb_xor = (binaryfunc) famv_xor,
};
static int fam_contains(FAMObject *, PyObject *);
static PyObject *famv_fami_new(FAMVObject *);
static int
famv_contains(FAMVObject *self, PyObject *other)
{
if (self->kind == KEYS) {
return fam_contains(self->fam, other);
}
PyObject *iterator = famv_fami_new(self);
if (!iterator) {
return -1;
}
int result = PySequence_Contains(iterator, other);
Py_DECREF(iterator);
return result;
}
static PySequenceMethods famv_as_sequence = {
.sq_contains = (objobjproc) famv_contains,
};
static void
famv_dealloc(FAMVObject *self)
{
Py_DECREF(self->fam);
PyObject_Del((PyObject *)self);
}
static PyObject *
famv_fami_new(FAMVObject *self)
{
return fami_new(self->fam, self->kind, false);
}
static PyObject *
famv_length_hint(FAMVObject *self)
{
return PyLong_FromSsize_t(self->fam->keys_size);
}
static PyObject *
famv_reversed(FAMVObject *self)
{
return fami_new(self->fam, self->kind, true);
}
static PyObject *
famv_isdisjoint(FAMVObject *self, PyObject *other)
{
PyObject *intersection = famv_and((PyObject *)self, other);
if (!intersection) {
return NULL;
}
Py_ssize_t result = PySet_GET_SIZE(intersection);
Py_DECREF(intersection);
return PyBool_FromLong(result);
}
static PyObject *
famv_richcompare(FAMVObject *self, PyObject *other, int op)
{
PyObject *left = PySet_New((PyObject *)self);
if (!left) {
return NULL;
}
PyObject *right = PySet_New(other);
if (!right) {
Py_DECREF(left);
return NULL;
}
PyObject *result = PyObject_RichCompare(left, right, op);
Py_DECREF(left);
Py_DECREF(right);
return result;
}
static PyMethodDef famv_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},
};
static PyTypeObject FAMVType = {
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,
};
static PyObject *
famv_new(FAMObject *fam, ViewKind kind)
{
FAMVObject *famv = (FAMVObject *)PyObject_New(FAMVObject, &FAMVType);
if (!famv) {
return NULL;
}
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.
static Py_ssize_t
lookup_hash_obj(FAMObject *self, PyObject *key, Py_hash_t hash)
{
TableElement *table = self->table;
Py_ssize_t mask = self->table_size - 1;
Py_hash_t mixin = Py_ABS(hash);
Py_ssize_t table_pos = hash & mask;
PyObject *guess = NULL;
PyObject *keys = self->keys;
int result = -1;
Py_hash_t h = 0;
while (1) {
for (Py_ssize_t i = 0; i < SCAN; i++) {
h = table[table_pos].hash;
if (h == -1) { // Miss. Found a position that can be used for insertion.
return table_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
return table_pos;
}
result = PyObject_RichCompareBool(guess, key, Py_EQ);
if (result < 0) { // Error.
return -1;
}
if (result) { // Hit.
return table_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.
static Py_ssize_t
lookup_hash_int(FAMObject *self, npy_int64 key, Py_hash_t hash, KeysArrayType kat)
{
TableElement *table = self->table;
Py_ssize_t mask = self->table_size - 1;
Py_hash_t mixin = Py_ABS(hash);
Py_ssize_t table_pos = hash & mask; // taking the modulo
PyArrayObject *a = (PyArrayObject *)self->keys;
npy_int64 k = 0;
Py_hash_t h = 0;
while (1) {
for (Py_ssize_t i = 0; i < SCAN; i++) {
h = table[table_pos].hash;
if (h == -1) { // Miss. Position that can be used for insertion.
return table_pos;
}
if (h != hash) {
table_pos++;
continue;
}
switch (kat) {
case KAT_INT64:
k = *(npy_int64*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
case KAT_INT32:
k = *(npy_int32*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
case KAT_INT16:
k = *(npy_int16*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
case KAT_INT8:
k = *(npy_int8*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
default:
return -1;
}
if (key == k) {
return table_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.
static Py_ssize_t
lookup_hash_uint(FAMObject *self, npy_uint64 key, Py_hash_t hash, KeysArrayType kat)
{
TableElement *table = self->table;
Py_ssize_t mask = self->table_size - 1;
Py_hash_t mixin = Py_ABS(hash);
Py_ssize_t table_pos = hash & mask;
PyArrayObject *a = (PyArrayObject *)self->keys;
npy_uint64 k = 0;
Py_hash_t h = 0;
while (1) {
for (Py_ssize_t i = 0; i < SCAN; i++) {
h = table[table_pos].hash;
if (h == -1) {
return table_pos;
}
if (h != hash) {
table_pos++;
continue;
}
switch (kat) {
case KAT_UINT64:
k = *(npy_uint64*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
case KAT_UINT32:
k = *(npy_uint32*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
case KAT_UINT16:
k = *(npy_uint16*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
case KAT_UINT8:
k = *(npy_uint8*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
default:
return -1;
}
if (key == k) {
return table_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
static Py_ssize_t
lookup_hash_double(FAMObject *self, npy_double key, Py_hash_t hash, KeysArrayType kat)
{
TableElement *table = self->table;
Py_ssize_t mask = self->table_size - 1;
Py_hash_t mixin = Py_ABS(hash);
Py_ssize_t table_pos = hash & mask;
PyArrayObject *a = (PyArrayObject *)self->keys;
npy_double k = 0;
Py_hash_t h = 0;
while (1) {
for (Py_ssize_t i = 0; i < SCAN; i++) {
h = table[table_pos].hash;
if (h == -1) {
return table_pos;
}
if (h != hash) {
table_pos++;
continue;
}
switch (kat) {
case KAT_FLOAT64:
k = *(npy_double*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
case KAT_FLOAT32:
k = *(npy_float*)PyArray_GETPTR1(a, table[table_pos].keys_pos);
break;
case KAT_FLOAT16:
k = npy_half_to_double(*(npy_half*)PyArray_GETPTR1(a, table[table_pos].keys_pos));
break;
default:
return -1;
}
if (key == k) {
return table_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.
static Py_ssize_t
lookup_hash_unicode(
FAMObject *self,
Py_UCS4* key,
Py_ssize_t key_size,
Py_hash_t hash)
{
TableElement *table = self->table;
Py_ssize_t mask = self->table_size - 1;
Py_hash_t mixin = Py_ABS(hash);
Py_ssize_t table_pos = hash & mask;
PyArrayObject *a = (PyArrayObject *)self->keys;
Py_ssize_t dt_size = PyArray_ITEMSIZE(a) / UCS4_SIZE;
Py_ssize_t cmp_bytes = Py_MIN(key_size, dt_size) * UCS4_SIZE;
Py_hash_t h = 0;
Py_UCS4* p_start = NULL;
while (1) {
for (Py_ssize_t i = 0; i < SCAN; i++) {
h = table[table_pos].hash;
if (h == -1) {
return table_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)) {
return table_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.
static Py_ssize_t
lookup_hash_string(
FAMObject *self,
char* key,
Py_ssize_t key_size,
Py_hash_t hash)
{
TableElement *table = self->table;
Py_ssize_t mask = self->table_size - 1;
Py_hash_t mixin = Py_ABS(hash);
Py_ssize_t table_pos = hash & mask;
PyArrayObject *a = (PyArrayObject *)self->keys;
Py_ssize_t dt_size = PyArray_ITEMSIZE(a);
Py_ssize_t cmp_bytes = Py_MIN(key_size, dt_size);
Py_hash_t h = 0;
char* p_start = NULL;
while (1) {
for (Py_ssize_t i = 0; i < SCAN; i++) {
h = table[table_pos].hash;
if (h == -1) {
return table_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)) {
return table_pos;
}
table_pos++;
}
table_pos = (5 * (table_pos - SCAN) + (mixin >>= 1) + 1) & mask;
}
}
static Py_ssize_t
lookup_int(FAMObject *self, PyObject* key) {