-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtri_map.c
More file actions
1386 lines (1301 loc) · 49.6 KB
/
tri_map.c
File metadata and controls
1386 lines (1301 loc) · 49.6 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 "Python.h"
# include "stdbool.h"
# define NO_IMPORT_ARRAY
# define PY_ARRAY_UNIQUE_SYMBOL AK_ARRAY_API
# define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
# include "numpy/arrayobject.h"
# include "numpy/arrayscalars.h"
# include "tri_map.h"
# include "utilities.h"
typedef struct TriMapOne {
Py_ssize_t from; // signed
Py_ssize_t to;
} TriMapOne;
typedef struct TriMapManyTo {
Py_ssize_t start;
Py_ssize_t stop;
} TriMapManyTo;
typedef struct TriMapManyFrom {
npy_intp src;
PyArrayObject* dst;
} TriMapManyFrom;
typedef struct TriMapObject {
PyObject_HEAD
Py_ssize_t src_len;
Py_ssize_t dst_len;
Py_ssize_t len;
bool is_many;
bool finalized;
PyObject* src_match; // array object
npy_bool* src_match_data; // contiguous C array
PyObject* dst_match; // array object
npy_bool* dst_match_data; // contiguous C array
PyObject* final_src_fill; // array object
PyObject* final_dst_fill; // array object
// register one
TriMapOne* src_one;
Py_ssize_t src_one_count;
Py_ssize_t src_one_capacity;
TriMapOne* dst_one;
Py_ssize_t dst_one_count;
Py_ssize_t dst_one_capacity;
// register_many
TriMapManyTo* many_to; // two integers for contiguous assignment region
TriMapManyFrom* many_from; // int and array, for src and dst (together)
Py_ssize_t many_count;
Py_ssize_t many_capacity;
} TriMapObject;
PyObject *
TriMap_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs) {
TriMapObject *self = (TriMapObject *)cls->tp_alloc(cls, 0);
if (!self) {
return NULL;
}
return (PyObject *)self;
}
PyDoc_STRVAR(
TriMap_doc,
"\n"
"A utilty for three-way join mappings."
);
// Returns 0 on success, -1 on error.
int
TriMap_init(PyObject *self, PyObject *args, PyObject *kwargs) {
Py_ssize_t src_len;
Py_ssize_t dst_len;
if (!PyArg_ParseTuple(args,
"nn:__init__",
&src_len,
&dst_len)) {
return -1;
}
TriMapObject* tm = (TriMapObject*)self;
// handle all C types
tm->src_len = src_len;
tm->dst_len = dst_len;
tm->is_many = false;
tm->finalized = false;
tm->len = 0;
// we create arrays, and also pre-extract pointers to array data for fast insertion; we keep the array for optimal summing routines
npy_intp dims_src_len[] = {src_len};
tm->src_match = PyArray_ZEROS(1, dims_src_len, NPY_BOOL, 0);
if (tm->src_match == NULL) {
return -1;
}
tm->src_match_data = (npy_bool*)PyArray_DATA((PyArrayObject*)tm->src_match);
npy_intp dims_dst_len[] = {dst_len};
tm->dst_match = PyArray_ZEROS(1, dims_dst_len, NPY_BOOL, 0);
if (tm->dst_match == NULL) {
return -1;
}
tm->dst_match_data = (npy_bool*)PyArray_DATA((PyArrayObject*)tm->dst_match);
// register one
tm->src_one_count = 0;
tm->src_one_capacity = 16;
tm->src_one = (TriMapOne*)PyMem_Malloc(
sizeof(TriMapOne) * tm->src_one_capacity);
if (tm->src_one == NULL) {
PyErr_SetNone(PyExc_MemoryError);
return -1;
}
tm->dst_one_count = 0;
tm->dst_one_capacity = 16;
tm->dst_one = (TriMapOne*)PyMem_Malloc(
sizeof(TriMapOne) * tm->dst_one_capacity);
if (tm->dst_one == NULL) {
PyErr_SetNone(PyExc_MemoryError);
return -1;
}
// register many
tm->many_count = 0;
tm->many_capacity = 16;
tm->many_to = (TriMapManyTo*)PyMem_Malloc(
sizeof(TriMapManyTo) * tm->many_capacity);
if (tm->many_to == NULL) {
PyErr_SetNone(PyExc_MemoryError);
return -1;
}
tm->many_from = (TriMapManyFrom*)PyMem_Malloc(
sizeof(TriMapManyFrom) * tm->many_capacity);
if (tm->many_from == NULL) {
PyErr_SetNone(PyExc_MemoryError);
return -1;
}
return 0;
}
void
TriMap_dealloc(TriMapObject *self) {
// NOTE: we use XDECREF incase init fails before these objects get allocated
Py_XDECREF(self->src_match);
Py_XDECREF(self->dst_match);
Py_XDECREF(self->final_src_fill);
Py_XDECREF(self->final_dst_fill);
if (self->src_one != NULL) {
PyMem_Free(self->src_one);
}
if (self->dst_one != NULL) {
PyMem_Free(self->dst_one);
}
if (self->many_to != NULL) {
PyMem_Free(self->many_to);
}
if (self->many_from != NULL) {
// decref all arrays before freeing
for (Py_ssize_t i = 0; i < self->many_count; i++) {
// NOTE: using dot to get to pointer?
Py_DECREF((PyObject*)self->many_from[i].dst);
}
PyMem_Free(self->many_from);
}
Py_TYPE(self)->tp_free((PyObject *)self);
}
PyObject *
TriMap_repr(TriMapObject *self) {
const char *is_many = self->is_many ? "true" : "false";
const char *is_finalized = self->finalized ? "true" : "false";
npy_intp src_fill;
npy_intp dst_fill;
if (self->finalized) {
src_fill = PyArray_SIZE((PyArrayObject*)self->final_src_fill);
dst_fill = PyArray_SIZE((PyArrayObject*)self->final_dst_fill);
}
else {
src_fill = -1;
dst_fill = -1;
}
return PyUnicode_FromFormat("<%s(len: %i, src_fill: %i, dst_fill: %i, is_many: %s, is_finalized: %s)>",
Py_TYPE(self)->tp_name,
self->len,
src_fill,
dst_fill,
is_many,
is_finalized);
}
// Provide the integer positions connecting the `src` to the `dst`. If there is no match to `src` or `dst`, the unmatched position can be provided with -1. From each side, a connection is documented to the current `len`. Each time this is called `len` is incremented, indicating the inrease in position in the `final`. Return NULL on error.
// Inner function for calling from C; returns 0 on success, -1 on error. Exceptions will be set on error.
static inline int
AK_TM_register_one(TriMapObject* tm, Py_ssize_t src_from, Py_ssize_t dst_from) {
bool src_matched = src_from >= 0;
bool dst_matched = dst_from >= 0;
if (src_from >= tm->src_len || dst_from >= tm->dst_len) {
PyErr_SetString(PyExc_ValueError, "Out of bounds locator");
return -1;
}
if (src_matched) {
if (AK_UNLIKELY(tm->src_one_count == tm->src_one_capacity)) {
tm->src_one_capacity <<= 1; // get 2x the capacity
tm->src_one = PyMem_Realloc(tm->src_one,
sizeof(TriMapOne) * tm->src_one_capacity);
if (tm->src_one == NULL) {
PyErr_SetNone(PyExc_MemoryError);
return -1;
}
}
tm->src_one[tm->src_one_count] = (TriMapOne){src_from, tm->len};
tm->src_one_count += 1;
}
if (dst_matched) {
if (AK_UNLIKELY(tm->dst_one_count == tm->dst_one_capacity)) {
tm->dst_one_capacity <<= 1; // get 2x the capacity
tm->dst_one = PyMem_Realloc(tm->dst_one,
sizeof(TriMapOne) * tm->dst_one_capacity);
if (tm->dst_one == NULL) {
PyErr_SetNone(PyExc_MemoryError);
return -1;
}
}
tm->dst_one[tm->dst_one_count] = (TriMapOne){dst_from, tm->len};
tm->dst_one_count += 1;
}
if (src_matched && dst_matched) {
if (!tm->is_many) {
// if we have seen this connection before, we have a many
if (tm->src_match_data[src_from] || tm->dst_match_data[dst_from]) {
tm->is_many = true;
}
}
tm->src_match_data[src_from] = NPY_TRUE;
tm->dst_match_data[dst_from] = NPY_TRUE;
}
tm->len += 1;
return 0;
}
// Public function for calling from Python.
PyObject *
TriMap_register_one(TriMapObject *self, PyObject *args) {
Py_ssize_t src_from;
Py_ssize_t dst_from;
if (!PyArg_ParseTuple(args,
"nn:register_one",
&src_from,
&dst_from)) {
return NULL;
}
if (self->finalized) {
PyErr_SetString(PyExc_RuntimeError, "Cannot register post finalization");
return NULL;
}
if (AK_TM_register_one(self, src_from, dst_from)) {
return NULL;
}
Py_RETURN_NONE;
}
PyObject *
TriMap_register_unmatched_dst(TriMapObject *self) {
if (self->finalized) {
PyErr_SetString(PyExc_RuntimeError, "Cannot register post finalization");
return NULL;
}
PyArrayObject* dst_match_array = (PyArrayObject *)self->dst_match;
PyObject* sum_scalar = PyArray_Sum(
dst_match_array,
0,
NPY_INT64, // this converts before sum; not sure this is necessary
NULL);
if (sum_scalar == NULL) {
return NULL;
}
// for a 1D array PyArray_SUM returns a scalar
npy_int64 sum = PyArrayScalar_VAL(sum_scalar, Int64);
Py_DECREF(sum_scalar);
if (sum < self->dst_len) {
PyArrayObject* dst_unmatched = (PyArrayObject *)PyObject_CallMethod(
self->dst_match, // PyObject
"__invert__",
NULL);
if (dst_unmatched == NULL) {
return NULL;
}
// derive indices for unmatched locations, call each with register_one
PyArrayObject* indices = (PyArrayObject*)AK_nonzero_1d(dst_unmatched);
if (indices == NULL) {
Py_DECREF((PyObject*)dst_unmatched);
return NULL;
}
// borrow ref to array in 1-element tuple
npy_int64 *index_data = (npy_int64 *)PyArray_DATA(indices);
npy_intp index_len = PyArray_SIZE(indices);
for (npy_intp i = 0; i < index_len; i++) {
if (AK_TM_register_one(self, -1, index_data[i])) {
Py_DECREF((PyObject*)dst_unmatched);
Py_DECREF((PyObject*)indices);
return NULL;
}
}
Py_DECREF((PyObject*)dst_unmatched);
Py_DECREF((PyObject*)indices);
}
Py_RETURN_NONE;
}
// Given an integer (for the src) and an array of integers (for the dst), store mappings from src to final and dst to final.
PyObject *
TriMap_register_many(TriMapObject *self, PyObject *args) {
Py_ssize_t src_from;
PyArrayObject* dst_from;
if (!PyArg_ParseTuple(args,
"nO!:register_many",
&src_from,
&PyArray_Type, &dst_from)) {
return NULL;
}
if (self->finalized) {
PyErr_SetString(PyExc_RuntimeError, "Cannot register post finalization");
return NULL;
}
int dst_from_type = PyArray_TYPE(dst_from);
if (dst_from_type != NPY_INT64) {
PyErr_SetString(PyExc_ValueError, "`dst_from` must be a 64 bit integer array");
return NULL;
}
npy_intp increment = PyArray_SIZE(dst_from);
if (AK_UNLIKELY(self->many_count == self->many_capacity)) {
self->many_capacity <<= 1; // get 2x the capacity
self->many_to = PyMem_Realloc(self->many_to,
sizeof(TriMapManyTo) * self->many_capacity);
if (self->many_to == NULL) {
PyErr_SetNone(PyExc_MemoryError);
return NULL;
}
self->many_from = PyMem_Realloc(self->many_from,
sizeof(TriMapManyFrom) * self->many_capacity);
if (self->many_from == NULL) {
PyErr_SetNone(PyExc_MemoryError);
return NULL;
}
}
// define contiguous region in final to map to
self->many_to[self->many_count] = (TriMapManyTo){self->len, self->len + increment};
Py_INCREF((PyObject*)dst_from); // decrefs on dealloc
self->many_from[self->many_count] = (TriMapManyFrom){src_from, dst_from};
self->many_count += 1;
self->src_match_data[src_from] = NPY_TRUE;
// iterate over dst_from and set values to True; cannot assume that dst_from is contiguous; dst_match_data is contiguous
for (Py_ssize_t i = 0; i < increment; i++){
npy_int64 pos = *(npy_int64*)PyArray_GETPTR1(dst_from, i); // always int64
self->dst_match_data[pos] = NPY_TRUE;
}
self->len += increment;
self->is_many = true;
Py_RETURN_NONE;
}
//------------------------------------------------------------------------------
// Determine, for src and dst, which indices will need fill values, and store those indices as an integer array in final_src_fill, final_dst_fill
PyObject *
TriMap_finalize(TriMapObject *self, PyObject *Py_UNUSED(unused)) {
TriMapObject* tm = (TriMapObject*)self;
if (self->finalized) {
PyErr_SetString(PyExc_RuntimeError, "Cannot call finalize twice");
return NULL;
}
// predefine all PyObjects to use goto error
PyObject* final_src_match = NULL;
PyObject* final_dst_match = NULL;
PyObject* final_src_unmatched = NULL;
PyObject* final_dst_unmatched = NULL;
npy_intp dims[] = {tm->len};
// initialize all to False
final_src_match = PyArray_ZEROS(1, dims, NPY_BOOL, 0);
if (final_src_match == NULL) {
goto error;
}
final_dst_match = PyArray_ZEROS(1, dims, NPY_BOOL, 0);
if (final_dst_match == NULL) {
goto error;
}
npy_bool* final_src_match_data = (npy_bool*)PyArray_DATA(
(PyArrayObject*)final_src_match);
npy_bool* final_dst_match_data = (npy_bool*)PyArray_DATA(
(PyArrayObject*)final_dst_match);
TriMapOne* o;
TriMapOne* o_end;
o = tm->src_one;
o_end = o + tm->src_one_count;
for (; o < o_end; o++) {
final_src_match_data[o->to] = NPY_TRUE;
}
o = tm->dst_one;
o_end = o + tm->dst_one_count;
for (; o < o_end; o++) {
final_dst_match_data[o->to] = NPY_TRUE;
}
// many assign from src and dst into the same final positions
npy_bool* s;
npy_bool* d;
npy_bool* end;
TriMapManyTo* m = tm->many_to;
TriMapManyTo* m_end = m + tm->many_count;
for (; m < m_end; m++) {
d = final_dst_match_data + m->start;
s = final_src_match_data + m->start;
end = final_src_match_data + m->stop;
while (s < end) {
*s++ = NPY_TRUE;
*d++ = NPY_TRUE;
}
}
// NOTE: could sum first to see if nonzero call is necessary; would skip invert and nonzero calls
final_src_unmatched = PyObject_CallMethod(
final_src_match, // PyObject
"__invert__",
NULL);
if (final_src_unmatched == NULL) {
goto error;
}
final_dst_unmatched = PyObject_CallMethod(
final_dst_match, // PyObject
"__invert__",
NULL);
if (final_dst_unmatched == NULL) {
goto error;
}
tm->final_src_fill = AK_nonzero_1d((PyArrayObject*)final_src_unmatched);
if (tm->final_src_fill == NULL) {
goto error;
}
tm->final_dst_fill = AK_nonzero_1d((PyArrayObject*)final_dst_unmatched);
if (tm->final_dst_fill == NULL) {
goto error;
}
Py_DECREF(final_src_match);
Py_DECREF(final_dst_match);
Py_DECREF(final_src_unmatched);
Py_DECREF(final_dst_unmatched);
tm->finalized = true;
Py_RETURN_NONE;
error: // all PyObject initialized to NULL, no more than 1 ref
Py_XDECREF(final_src_match);
Py_XDECREF(final_dst_match);
Py_XDECREF(final_src_unmatched);
Py_XDECREF(final_dst_unmatched);
return NULL;
}
PyObject *
TriMap_is_many(TriMapObject *self, PyObject *Py_UNUSED(unused)) {
if (!self->finalized) {
PyErr_SetString(PyExc_RuntimeError, "Finalization is required");
return NULL;
}
if (self->is_many) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
// Return True if the `src` will not need a fill. This is only correct of `src` is binding to a left join or an inner join.
PyObject *
TriMap_src_no_fill(TriMapObject *self, PyObject *Py_UNUSED(unused)) {
if (!self->finalized) {
PyErr_SetString(PyExc_RuntimeError, "Finalization is required");
return NULL;
}
if (PyArray_SIZE((PyArrayObject*)self->final_src_fill) == 0) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
// Return True if the `dst` will not need a fill. This is only correct of `dst` is binding to a left join or an inner join.
PyObject *
TriMap_dst_no_fill(TriMapObject *self, PyObject *Py_UNUSED(unused)) {
if (!self->finalized) {
PyErr_SetString(PyExc_RuntimeError, "Finalization is required");
return NULL;
}
if (PyArray_SIZE((PyArrayObject*)self->final_dst_fill) == 0) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
# define AK_TM_TRANSFER_SCALAR(npy_type_to, npy_type_from) do { \
npy_type_to* array_to_data = (npy_type_to*)PyArray_DATA(array_to); \
TriMapOne* o = one_pairs; \
TriMapOne* o_end = o + one_count; \
for (; o < o_end; o++) { \
array_to_data[o->to] = (npy_type_to) \
*(npy_type_from*)PyArray_GETPTR1( \
array_from, o->from); \
} \
npy_type_to* t; \
npy_type_to* t_end; \
npy_type_to f; \
npy_int64 f_pos; \
npy_intp dst_pos; \
PyArrayObject* dst; \
for (Py_ssize_t i = 0; i < tm->many_count; i++) { \
t = array_to_data + tm->many_to[i].start; \
t_end = array_to_data + tm->many_to[i].stop; \
if (from_src) { \
f = (npy_type_to)*(npy_type_from*)PyArray_GETPTR1( \
array_from, tm->many_from[i].src); \
while (t < t_end) { \
*t++ = f; \
} \
} \
else { \
dst_pos = 0; \
dst = tm->many_from[i].dst; \
while (t < t_end) { \
f_pos = *(npy_int64*)PyArray_GETPTR1(dst, dst_pos); \
*t++ = (npy_type_to) \
*(npy_type_from*)PyArray_GETPTR1( \
array_from, f_pos); \
dst_pos++; \
} \
} \
} \
} while (0) \
// Based on `tm` state, transfer from src or from dst (depending on `from_src`) to a `array_to`, a newly created contiguous array that is compatible with the values in `array_from`. Returns -1 on error. This only needs to match to / from type combinations that are possible from `resolve_dtype`, i.e., bool never goes to integer.
static inline int
AK_TM_transfer_scalar(TriMapObject* tm,
bool from_src,
PyArrayObject* array_from,
PyArrayObject* array_to) {
Py_ssize_t one_count = from_src ? tm->src_one_count : tm->dst_one_count;
TriMapOne* one_pairs = from_src ? tm->src_one : tm->dst_one;
switch(PyArray_TYPE(array_to)){
case NPY_BOOL:
AK_TM_TRANSFER_SCALAR(npy_bool, npy_bool);
return 0;
case NPY_INT64:
switch (PyArray_TYPE(array_from)) {
case NPY_INT64:
AK_TM_TRANSFER_SCALAR(npy_int64, npy_int64);
return 0;
case NPY_INT32:
AK_TM_TRANSFER_SCALAR(npy_int64, npy_int32);
return 0;
case NPY_INT16:
AK_TM_TRANSFER_SCALAR(npy_int64, npy_int16);
return 0;
case NPY_INT8:
AK_TM_TRANSFER_SCALAR(npy_int64, npy_int8);
return 0;
case NPY_UINT32:
AK_TM_TRANSFER_SCALAR(npy_int64, npy_uint32);
return 0;
case NPY_UINT16:
AK_TM_TRANSFER_SCALAR(npy_int64, npy_uint16);
return 0;
case NPY_UINT8:
AK_TM_TRANSFER_SCALAR(npy_int64, npy_uint8);
return 0;
}
break;
case NPY_INT32:
switch (PyArray_TYPE(array_from)) {
case NPY_INT32:
AK_TM_TRANSFER_SCALAR(npy_int32, npy_int32);
return 0;
case NPY_INT16:
AK_TM_TRANSFER_SCALAR(npy_int32, npy_int16);
return 0;
case NPY_INT8:
AK_TM_TRANSFER_SCALAR(npy_int32, npy_int8);
return 0;
case NPY_UINT16:
AK_TM_TRANSFER_SCALAR(npy_int32, npy_uint16);
return 0;
case NPY_UINT8:
AK_TM_TRANSFER_SCALAR(npy_int32, npy_uint8);
return 0;
}
break;
case NPY_INT16:
switch (PyArray_TYPE(array_from)) {
case NPY_INT16:
AK_TM_TRANSFER_SCALAR(npy_int16, npy_int16);
return 0;
case NPY_INT8:
AK_TM_TRANSFER_SCALAR(npy_int16, npy_int8);
return 0;
case NPY_UINT8:
AK_TM_TRANSFER_SCALAR(npy_int16, npy_uint8);
return 0;
}
break;
case NPY_INT8:
AK_TM_TRANSFER_SCALAR(npy_int8, npy_int8);
return 0;
case NPY_UINT64:
switch (PyArray_TYPE(array_from)) {
case NPY_UINT64:
AK_TM_TRANSFER_SCALAR(npy_uint64, npy_uint64);
return 0;
case NPY_UINT32:
AK_TM_TRANSFER_SCALAR(npy_uint64, npy_uint32);
return 0;
case NPY_UINT16:
AK_TM_TRANSFER_SCALAR(npy_uint64, npy_uint16);
return 0;
case NPY_UINT8:
AK_TM_TRANSFER_SCALAR(npy_uint64, npy_uint8);
return 0;
}
break;
case NPY_UINT32:
switch (PyArray_TYPE(array_from)) {
case NPY_UINT32:
AK_TM_TRANSFER_SCALAR(npy_uint32, npy_uint32);
return 0;
case NPY_UINT16:
AK_TM_TRANSFER_SCALAR(npy_uint32, npy_uint16);
return 0;
case NPY_UINT8:
AK_TM_TRANSFER_SCALAR(npy_uint32, npy_uint8);
return 0;
}
break;
case NPY_UINT16:
switch (PyArray_TYPE(array_from)) {
case NPY_UINT16:
AK_TM_TRANSFER_SCALAR(npy_uint16, npy_uint16);
return 0;
case NPY_UINT8:
AK_TM_TRANSFER_SCALAR(npy_uint16, npy_uint8);
return 0;
}
break;
case NPY_UINT8:
AK_TM_TRANSFER_SCALAR(npy_uint8, npy_uint8);
return 0;
case NPY_FLOAT64:
switch (PyArray_TYPE(array_from)) {
case NPY_FLOAT64:
AK_TM_TRANSFER_SCALAR(npy_float64, npy_float64);
return 0;
case NPY_FLOAT32:
AK_TM_TRANSFER_SCALAR(npy_float64, npy_float32);
return 0;
case NPY_FLOAT16:
AK_TM_TRANSFER_SCALAR(npy_float64, npy_float16);
return 0;
case NPY_INT64:
AK_TM_TRANSFER_SCALAR(npy_float64, npy_int64);
return 0;
case NPY_INT32:
AK_TM_TRANSFER_SCALAR(npy_float64, npy_int32);
return 0;
case NPY_INT16:
AK_TM_TRANSFER_SCALAR(npy_float64, npy_int16);
return 0;
case NPY_INT8:
AK_TM_TRANSFER_SCALAR(npy_float64, npy_int8);
return 0;
case NPY_UINT64:
AK_TM_TRANSFER_SCALAR(npy_float64, npy_uint64);
return 0;
case NPY_UINT32:
AK_TM_TRANSFER_SCALAR(npy_float64, npy_uint32);
return 0;
case NPY_UINT16:
AK_TM_TRANSFER_SCALAR(npy_float64, npy_uint16);
return 0;
case NPY_UINT8:
AK_TM_TRANSFER_SCALAR(npy_float64, npy_uint8);
return 0;
}
break;
case NPY_FLOAT32:
switch (PyArray_TYPE(array_from)) {
case NPY_FLOAT32:
AK_TM_TRANSFER_SCALAR(npy_float32, npy_float32);
return 0;
case NPY_FLOAT16:
AK_TM_TRANSFER_SCALAR(npy_float32, npy_float16);
return 0;
case NPY_INT16:
AK_TM_TRANSFER_SCALAR(npy_float32, npy_int16);
return 0;
case NPY_INT8:
AK_TM_TRANSFER_SCALAR(npy_float32, npy_int8);
return 0;
case NPY_UINT16:
AK_TM_TRANSFER_SCALAR(npy_float32, npy_uint16);
return 0;
case NPY_UINT8:
AK_TM_TRANSFER_SCALAR(npy_float32, npy_uint8);
return 0;
}
break;
case NPY_FLOAT16:
switch (PyArray_TYPE(array_from)) {
case NPY_FLOAT16:
AK_TM_TRANSFER_SCALAR(npy_float16, npy_float16);
return 0;
case NPY_INT8:
AK_TM_TRANSFER_SCALAR(npy_float16, npy_int8);
return 0;
case NPY_UINT16:
AK_TM_TRANSFER_SCALAR(npy_float16, npy_uint16);
return 0;
case NPY_UINT8:
AK_TM_TRANSFER_SCALAR(npy_float16, npy_uint8);
return 0;
}
break;
case NPY_DATETIME: {
AK_TM_TRANSFER_SCALAR(npy_int64, npy_int64);
return 0;
}
}
PyErr_SetString(PyExc_TypeError, "No handling for types");
return -1;
}
#undef AK_TM_TRANSFER_SCALAR
// Returns -1 on error. Specialized transfer from any type of an array to an object array.
static inline int
AK_TM_transfer_object(TriMapObject* tm,
bool from_src,
PyArrayObject* array_from,
PyArrayObject* array_to
) {
Py_ssize_t one_count = from_src ? tm->src_one_count : tm->dst_one_count;
TriMapOne* one_pairs = from_src ? tm->src_one : tm->dst_one;
// NOTE: could use PyArray_Scalar instead of PyArray_GETITEM if we wanted to store scalars instead of Python objects; however, that is pretty uncommon for object arrays to store PyArray_Scalars
bool f_is_obj = PyArray_TYPE(array_from) == NPY_OBJECT;
// the passed in object array is contiguous and have NULL (not None) in each position
PyObject** array_to_data = (PyObject**)PyArray_DATA(array_to);
PyObject* pyo;
void* f;
TriMapOne* o = one_pairs;
TriMapOne* o_end = o + one_count;
for (; o < o_end; o++) {
f = PyArray_GETPTR1(array_from, o->from);
if (f_is_obj) {
pyo = *(PyObject**)f;
Py_INCREF(pyo);
}
else { // will convert any value to an object
pyo = PyArray_GETITEM(array_from, f);
}
array_to_data[o->to] = pyo;
}
PyObject** t;
PyObject** t_end;
npy_intp dst_pos;
npy_int64 f_pos;
PyArrayObject* dst;
for (Py_ssize_t i = 0; i < tm->many_count; i++) {
t = array_to_data + tm->many_to[i].start;
t_end = array_to_data + tm->many_to[i].stop;
if (from_src) {
f = PyArray_GETPTR1(array_from, tm->many_from[i].src);
if (f_is_obj) {
pyo = *(PyObject**)f;
Py_INCREF(pyo); // pre add new ref so equal to PyArray_GETITEM
}
else {
pyo = PyArray_GETITEM(array_from, f); // given a new ref
}
while (t < t_end) {
Py_INCREF(pyo); // one more than we need
*t++ = pyo;
}
Py_DECREF(pyo); // remove the extra ref
}
else { // from_dst, dst is an array
dst_pos = 0;
dst = tm->many_from[i].dst;
while (t < t_end) {
f_pos = *(npy_int64*)PyArray_GETPTR1(dst, dst_pos);
f = PyArray_GETPTR1(array_from, f_pos);
if (f_is_obj) {
pyo = *(PyObject**)f;
Py_INCREF(pyo);
}
else {
pyo = PyArray_GETITEM(array_from, f);
}
*t++ = pyo;
dst_pos++;
}
}
}
return 0;
}
// Returns -1 on error. Specialized transfer from any type of an array to an object array. For usage with merge, Will only transfer if the destination is not NULL.
static inline int
AK_TM_transfer_object_if_null(TriMapObject* tm,
bool from_src,
PyArrayObject* array_from,
PyArrayObject* array_to
) {
Py_ssize_t one_count = from_src ? tm->src_one_count : tm->dst_one_count;
TriMapOne* one_pairs = from_src ? tm->src_one : tm->dst_one;
// NOTE: could use PyArray_Scalar instead of PyArray_GETITEM if we wanted to store scalars instead of Python objects; however, that is pretty uncommon for object arrays to store PyArray_Scalars
bool f_is_obj = PyArray_TYPE(array_from) == NPY_OBJECT;
// the passed in object array is contiguous and have NULL (not None) in each position
PyObject** array_to_data = (PyObject**)PyArray_DATA(array_to);
PyObject* pyo;
void* f;
TriMapOne* o = one_pairs;
TriMapOne* o_end = o + one_count;
for (; o < o_end; o++) {
if (array_to_data[o->to] == NULL) {
f = PyArray_GETPTR1(array_from, o->from);
if (f_is_obj) {
pyo = *(PyObject**)f;
Py_INCREF(pyo);
}
else { // will convert any value to an object
pyo = PyArray_GETITEM(array_from, f);
}
array_to_data[o->to] = pyo;
}
}
PyObject** t;
PyObject** t_end;
npy_intp dst_pos;
npy_int64 f_pos;
PyArrayObject* dst;
for (Py_ssize_t i = 0; i < tm->many_count; i++) {
t = array_to_data + tm->many_to[i].start;
t_end = array_to_data + tm->many_to[i].stop;
if (from_src) {
while (t < t_end) {
if (*t == NULL) {
f = PyArray_GETPTR1(array_from, tm->many_from[i].src);
if (f_is_obj) {
pyo = *(PyObject**)f;
Py_INCREF(pyo);
}
else {
pyo = PyArray_GETITEM(array_from, f); // given a new ref
}
*t++ = pyo;
}
else {
t++;
}
}
}
else { // from_dst, dst is an array
dst_pos = 0;
dst = tm->many_from[i].dst;
while (t < t_end) {
if (*t == NULL) {
f_pos = *(npy_int64*)PyArray_GETPTR1(dst, dst_pos);
f = PyArray_GETPTR1(array_from, f_pos);
if (f_is_obj) {
pyo = *(PyObject**)f;
Py_INCREF(pyo);
}
else {
pyo = PyArray_GETITEM(array_from, f);
}
*t++ = pyo;
dst_pos++;
}
else {
t++;
dst_pos++;
}
}
}
}
return 0;
}
// Returns -1 on error.
static inline int
AK_TM_fill_object(TriMapObject* tm,
bool from_src,
PyArrayObject* array_to,
PyObject* fill_value) {
PyArrayObject* final_fill = (PyArrayObject*)(from_src
? tm->final_src_fill : tm->final_dst_fill);
PyObject** array_to_data = (PyObject**)PyArray_DATA(array_to);
npy_int64* p = (npy_int64*)PyArray_DATA(final_fill);
npy_int64* p_end = p + PyArray_SIZE(final_fill);
PyObject** target;
while (p < p_end) {
target = array_to_data + *p++;
Py_INCREF(fill_value);
*target = fill_value;
}
return 0;
}
#define AK_TM_TRANSFER_FLEXIBLE(c_type, from_src, array_from, array_to) do {\
Py_ssize_t one_count = from_src ? tm->src_one_count : tm->dst_one_count;\
TriMapOne* one_pairs = from_src ? tm->src_one : tm->dst_one; \
npy_intp t_element_size = PyArray_ITEMSIZE(array_to); \
npy_intp t_element_cp = t_element_size / sizeof(c_type); \
npy_intp f_element_size = PyArray_ITEMSIZE(array_from); \
c_type* array_to_data = (c_type*)PyArray_DATA(array_to); \
c_type* f; \
c_type* t; \
c_type* t_end; \
npy_intp dst_pos; \
npy_int64 f_pos; \
PyArrayObject* dst; \
TriMapOne* o = one_pairs; \
TriMapOne* o_end = o + one_count; \
for (; o < o_end; o++) { \
f = (c_type*)PyArray_GETPTR1(array_from, o->from); \
t = array_to_data + t_element_cp * o->to; \
memcpy(t, f, f_element_size); \
} \
for (Py_ssize_t i = 0; i < tm->many_count; i++) { \
t = array_to_data + t_element_cp * tm->many_to[i].start; \
t_end = array_to_data + t_element_cp * tm->many_to[i].stop; \
if (from_src) { \
f = (c_type*)PyArray_GETPTR1(array_from, tm->many_from[i].src);\
for (; t < t_end; t += t_element_cp) { \
memcpy(t, f, f_element_size); \
} \
} \
else { \
dst_pos = 0; \
dst = tm->many_from[i].dst; \
for (; t < t_end; t += t_element_cp) { \
f_pos = *(npy_int64*)PyArray_GETPTR1(dst, dst_pos); \
f = (c_type*)PyArray_GETPTR1(array_from, f_pos); \
memcpy(t, f, f_element_size); \
dst_pos++; \
} \
} \
} \
} while (0) \
// Returns -1 on error.
static inline int
AK_TM_fill_unicode(TriMapObject* tm,
bool from_src,
PyArrayObject* array_to,
PyObject* fill_value) {
PyArrayObject* final_fill = (PyArrayObject*)(from_src
? tm->final_src_fill : tm->final_dst_fill);
Py_UCS4* array_to_data = (Py_UCS4*)PyArray_DATA(array_to);
// code points per element
npy_intp cp = PyArray_ITEMSIZE(array_to) / UCS4_SIZE;
bool decref_fill_value = false;
if (PyBytes_Check(fill_value)) {
fill_value = PyUnicode_FromEncodedObject(fill_value, "utf-8", NULL);
if (fill_value == NULL) {
return -1;
}
decref_fill_value = true;
}
else if (!PyUnicode_Check(fill_value)) {
return -1;