arraykit 1.2.0__cp314-cp314-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
arraykit/tri_map.h ADDED
@@ -0,0 +1,8 @@
1
+ # ifndef ARRAYKIT_SRC_TRI_MAP_H_
2
+ # define ARRAYKIT_SRC_TRI_MAP_H_
3
+
4
+ # include "Python.h"
5
+
6
+ extern PyTypeObject TriMapType;
7
+
8
+ # endif /* ARRAYKIT_SRC_TRI_MAP_H_ */
arraykit/utilities.h ADDED
@@ -0,0 +1,392 @@
1
+ #ifndef ARRAYKIT_SRC_UTILITIES_H_
2
+ #define ARRAYKIT_SRC_UTILITIES_H_
3
+
4
+ # include "Python.h"
5
+ # include "stdbool.h"
6
+
7
+ # define NO_IMPORT_ARRAY
8
+ # define PY_ARRAY_UNIQUE_SYMBOL AK_ARRAY_API
9
+ # define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
10
+
11
+ # include "numpy/arrayobject.h"
12
+ # include "numpy/arrayscalars.h"
13
+
14
+ static const size_t UCS4_SIZE = sizeof(Py_UCS4);
15
+
16
+ //------------------------------------------------------------------------------
17
+ // Macros
18
+ //------------------------------------------------------------------------------
19
+
20
+ // Given a PyObject, raise if not an array.
21
+ # define AK_CHECK_NUMPY_ARRAY(O) \
22
+ if (!PyArray_Check(O)) { \
23
+ return PyErr_Format(PyExc_TypeError, \
24
+ "Expected NumPy array, not %s.", \
25
+ Py_TYPE(O)->tp_name); \
26
+ }
27
+
28
+ // Given a PyObject, raise if not an array or is not one or two dimensional.
29
+ # define AK_CHECK_NUMPY_ARRAY_1D_2D(O) \
30
+ do { \
31
+ AK_CHECK_NUMPY_ARRAY(O) \
32
+ int ndim = PyArray_NDIM((PyArrayObject *)O); \
33
+ if (ndim != 1 && ndim != 2) { \
34
+ return PyErr_Format(PyExc_NotImplementedError,\
35
+ "Expected 1D or 2D array, not %i.", \
36
+ ndim); \
37
+ } \
38
+ } while (0)
39
+
40
+ // Given a PyObject, raise if not an array or is not two dimensional.
41
+ # define AK_CHECK_NUMPY_ARRAY_2D(O) \
42
+ do { \
43
+ AK_CHECK_NUMPY_ARRAY(O) \
44
+ int ndim = PyArray_NDIM((PyArrayObject *)O); \
45
+ if (ndim != 2) { \
46
+ return PyErr_Format(PyExc_NotImplementedError,\
47
+ "Expected a 2D array, not %i.", \
48
+ ndim); \
49
+ } \
50
+ } while (0)
51
+
52
+ # if defined __GNUC__ || defined __clang__
53
+ # define AK_LIKELY(X) __builtin_expect(!!(X), 1)
54
+ # define AK_UNLIKELY(X) __builtin_expect(!!(X), 0)
55
+ # else
56
+ # define AK_LIKELY(X) (!!(X))
57
+ # define AK_UNLIKELY(X) (!!(X))
58
+ # endif
59
+
60
+ // Placeholder of not implemented pathways / debugging.
61
+ # define AK_NOT_IMPLEMENTED(msg) \
62
+ do { \
63
+ PyErr_SetString(PyExc_NotImplementedError, msg);\
64
+ return NULL; \
65
+ } while (0)
66
+
67
+ # define _AK_DEBUG_BEGIN() \
68
+ do { \
69
+ fprintf(stderr, "--- %s: %i: %s: ", __FILE__, __LINE__, __FUNCTION__);
70
+
71
+ # define _AK_DEBUG_END() \
72
+ fprintf(stderr, "\n"); \
73
+ fflush(stderr); \
74
+ } while (0)
75
+
76
+ # define AK_DEBUG(msg) \
77
+ _AK_DEBUG_BEGIN(); \
78
+ fprintf(stderr, #msg); \
79
+ _AK_DEBUG_END()
80
+
81
+ # define AK_DEBUG_MSG_OBJ(msg, obj) \
82
+ _AK_DEBUG_BEGIN(); \
83
+ fprintf(stderr, #msg " "); \
84
+ PyObject_Print(obj, stderr, 0); \
85
+ _AK_DEBUG_END()
86
+
87
+ # define AK_DEBUG_MSG_REFCNT(msg, obj) \
88
+ _AK_DEBUG_BEGIN(); \
89
+ fprintf(stderr, #msg " ref count: "); \
90
+ PyObject_Print(PyLong_FromSsize_t(Py_REFCNT((PyObject*)obj)), stderr, 0); \
91
+ _AK_DEBUG_END()
92
+
93
+ # define AK_DEBUG_OBJ(obj) \
94
+ _AK_DEBUG_BEGIN(); \
95
+ fprintf(stderr, #obj " = "); \
96
+ PyObject_Print(obj, stderr, 0); \
97
+ _AK_DEBUG_END()
98
+
99
+ // Takes and returns a PyArrayObject, optionally copying a mutable array and setting it as immutable
100
+ static inline PyArrayObject *
101
+ AK_immutable_filter(PyArrayObject *a)
102
+ {
103
+ // https://numpy.org/devdocs/reference/c-api/array.html#array-flags
104
+ if (PyArray_FLAGS(a) & NPY_ARRAY_WRITEABLE) {
105
+ if ((a = (PyArrayObject *)PyArray_NewCopy(a, NPY_ANYORDER))) {
106
+ PyArray_CLEARFLAGS(a, NPY_ARRAY_WRITEABLE);
107
+ }
108
+ return a;
109
+ }
110
+ Py_INCREF(a);
111
+ return a;
112
+ }
113
+
114
+ // Returns NULL on error.
115
+ static inline PyArray_Descr *
116
+ AK_resolve_dtype(PyArray_Descr *d1, PyArray_Descr *d2)
117
+ {
118
+ if (PyArray_EquivTypes(d1, d2)) {
119
+ Py_INCREF(d1);
120
+ return d1;
121
+ }
122
+ if (PyDataType_ISOBJECT(d1) || PyDataType_ISOBJECT(d2)
123
+ || PyDataType_ISBOOL(d1) || PyDataType_ISBOOL(d2)
124
+ || (PyDataType_ISSTRING(d1) != PyDataType_ISSTRING(d2))
125
+ || ((PyDataType_ISDATETIME(d1) || PyDataType_ISDATETIME(d2))
126
+ // PyDataType_ISDATETIME matches NPY_DATETIME or NPY_TIMEDELTA, so
127
+ // we need to make sure we didn't get one of each:
128
+ && !PyArray_EquivTypenums(d1->type_num, d2->type_num)))
129
+ {
130
+ return PyArray_DescrFromType(NPY_OBJECT);
131
+ }
132
+ PyArray_Descr *d = PyArray_PromoteTypes(d1, d2);
133
+ if (!d) {
134
+ PyErr_Clear();
135
+ return PyArray_DescrFromType(NPY_OBJECT);
136
+ }
137
+ return d;
138
+ }
139
+
140
+ // Returns NULL on error. Returns a new reference.
141
+ static inline PyObject *
142
+ AK_build_pair_ssize_t(Py_ssize_t a, Py_ssize_t b)
143
+ {
144
+ PyObject* t = PyTuple_New(2);
145
+ if (t == NULL) {
146
+ return NULL;
147
+ }
148
+ PyObject* py_a = PyLong_FromSsize_t(a);
149
+ if (py_a == NULL) {
150
+ Py_DECREF(t);
151
+ return NULL;
152
+ }
153
+ PyObject* py_b = PyLong_FromSsize_t(b);
154
+ if (py_b == NULL) {
155
+ Py_DECREF(t);
156
+ Py_DECREF(py_a);
157
+ return NULL;
158
+ }
159
+ // steals refs
160
+ PyTuple_SET_ITEM(t, 0, py_a);
161
+ PyTuple_SET_ITEM(t, 1, py_b);
162
+ return t;
163
+ }
164
+
165
+ // Returns a new ref; returns NULL on error. Any start or stop less than 0 will be set to NULL.
166
+ static inline PyObject *
167
+ AK_build_slice(Py_ssize_t start, Py_ssize_t stop, Py_ssize_t step)
168
+ {
169
+ PyObject* py_start = NULL;
170
+ PyObject* py_stop = NULL;
171
+ PyObject* py_step = NULL;
172
+
173
+ if (start >= 0) {
174
+ py_start = PyLong_FromSsize_t(start);
175
+ if (py_start == NULL) {return NULL;}
176
+ }
177
+ if (stop >= 0) {
178
+ py_stop = PyLong_FromSsize_t(stop);
179
+ if (py_stop == NULL) {return NULL;}
180
+ }
181
+ // do not set a step if not necessary
182
+ if (step != 0 && step != 1) {
183
+ py_step = PyLong_FromSsize_t(step);
184
+ if (py_step == NULL) {return NULL;}
185
+ }
186
+
187
+ // might be NULL, let return
188
+ PyObject* new = PySlice_New(py_start, py_stop, py_step);
189
+
190
+ Py_XDECREF(py_start);
191
+ Py_XDECREF(py_stop);
192
+ Py_XDECREF(py_step);
193
+
194
+ return new;
195
+ }
196
+
197
+ // Utility function for converting slices; returns NULL on error; returns a new reference.
198
+ static inline PyObject *
199
+ AK_slice_to_ascending_slice(PyObject* slice, Py_ssize_t size)
200
+ {
201
+ Py_ssize_t step_count = -1;
202
+ Py_ssize_t start = 0;
203
+ Py_ssize_t stop = 0;
204
+ Py_ssize_t step = 0;
205
+
206
+ if (PySlice_Unpack(slice, &start, &stop, &step)) {
207
+ return NULL;
208
+ }
209
+ if (step > 0) {
210
+ Py_INCREF(slice);
211
+ return slice;
212
+ }
213
+ step_count = PySlice_AdjustIndices(
214
+ size,
215
+ &start,
216
+ &stop,
217
+ step);
218
+
219
+ // step will be negative; shift original start value down to find new start
220
+ return AK_build_slice(
221
+ start + (step * (step_count - 1)),
222
+ start + 1,
223
+ -step);
224
+ }
225
+
226
+
227
+ static inline NPY_DATETIMEUNIT
228
+ AK_dt_unit_from_array(PyArrayObject* a) {
229
+ // This is based on get_datetime_metadata_from_dtype in the NumPy source, but that function is private. This does not check that the dtype is of the appropriate type.
230
+ PyArray_Descr* dt = PyArray_DESCR(a); // borrowed ref
231
+ PyArray_DatetimeMetaData* dma = &(((PyArray_DatetimeDTypeMetaData *)PyDataType_C_METADATA(dt))->meta);
232
+ return dma->base;
233
+ }
234
+
235
+ // Given a dt64 array, determine if it can be cast to a object without data loss. Returns -1 on error. NOTE: if we use dt_year, must incref first
236
+ static inline int
237
+ AK_is_objectable_dt64(PyArrayObject* a, PyObject* dt_year)
238
+ {
239
+ NPY_DATETIMEUNIT unit = AK_dt_unit_from_array(a);
240
+ switch (unit) {
241
+ case NPY_FR_ERROR:
242
+ case NPY_FR_Y:
243
+ case NPY_FR_M:
244
+ case NPY_FR_W:
245
+ return false;
246
+ case NPY_FR_D:
247
+ case NPY_FR_h:
248
+ case NPY_FR_m:
249
+ case NPY_FR_s:
250
+ case NPY_FR_ms:
251
+ case NPY_FR_us:
252
+ break;
253
+ case NPY_FR_ns:
254
+ case NPY_FR_ps:
255
+ case NPY_FR_fs:
256
+ case NPY_FR_as:
257
+ case NPY_FR_GENERIC:
258
+ return false;
259
+ }
260
+
261
+ Py_INCREF(dt_year);
262
+ PyObject* a_year = PyArray_CastToType(a, (PyArray_Descr*)dt_year, 0);
263
+ if (!a_year) {
264
+ Py_DECREF(dt_year);
265
+ return -1;
266
+ }
267
+
268
+ npy_int64* data = (npy_int64*)PyArray_DATA((PyArrayObject*)a_year);
269
+ npy_intp size = PyArray_SIZE((PyArrayObject*)a_year);
270
+
271
+ for (npy_intp i = 0; i < size; ++i) {
272
+ npy_int64 v = data[i];
273
+ if (v == NPY_DATETIME_NAT) {
274
+ continue;
275
+ }
276
+ // offset: 1-1970, 9999-1970
277
+ if (v < -1969 || v > 8029) {
278
+ Py_DECREF(a_year);
279
+ return 0;
280
+ }
281
+ }
282
+ Py_DECREF(a_year);
283
+ return 1;
284
+ }
285
+
286
+
287
+
288
+
289
+ // Given a Boolean, contiguous 1D array, return the index positions in an int64 array. Through experimentation it has been verified that doing full-size allocation of memory provides the best performance at all scales. Using NpyIter, or using, bit masks does not improve performance over pointer arithmetic. Prescanning for all empty is very effective. Note that NumPy benefits from first counting the nonzeros, then allocating only enough data for the expexted number of indices.
290
+ static inline PyObject *
291
+ AK_nonzero_1d(PyArrayObject* array) {
292
+ PyObject* final;
293
+ npy_intp count_max = PyArray_SIZE(array);
294
+
295
+ if (count_max == 0) { // return empty array
296
+ npy_intp dims = {count_max};
297
+ final = PyArray_SimpleNew(1, &dims, NPY_INT64);
298
+ PyArray_CLEARFLAGS((PyArrayObject*)final, NPY_ARRAY_WRITEABLE);
299
+ return final;
300
+ }
301
+ lldiv_t size_div = lldiv((long long)count_max, 8); // quot, rem
302
+
303
+ Py_ssize_t count = 0;
304
+ // the maximum number of collected integers is equal to or less than count_max
305
+ Py_ssize_t capacity = count_max;
306
+ npy_int64* indices = (npy_int64*)malloc(sizeof(npy_int64) * capacity);
307
+
308
+ NPY_BEGIN_THREADS_DEF;
309
+ NPY_BEGIN_THREADS;
310
+
311
+ if (PyArray_IS_C_CONTIGUOUS(array)) {
312
+ npy_bool* p_start = (npy_bool*)PyArray_DATA(array);
313
+ npy_bool* p = p_start;
314
+ npy_bool* p_end = p + count_max;
315
+ npy_bool* p_end_roll = p_end - size_div.rem;
316
+
317
+ while (p < p_end_roll) {
318
+ if (*(npy_uint64*)p == 0) {
319
+ p += 8; // no true within this 8 byte roll region
320
+ continue;
321
+ }
322
+ if (*p) {indices[count++] = p - p_start;}
323
+ p++;
324
+ if (*p) {indices[count++] = p - p_start;}
325
+ p++;
326
+ if (*p) {indices[count++] = p - p_start;}
327
+ p++;
328
+ if (*p) {indices[count++] = p - p_start;}
329
+ p++;
330
+ if (*p) {indices[count++] = p - p_start;}
331
+ p++;
332
+ if (*p) {indices[count++] = p - p_start;}
333
+ p++;
334
+ if (*p) {indices[count++] = p - p_start;}
335
+ p++;
336
+ if (*p) {indices[count++] = p - p_start;}
337
+ p++;
338
+ }
339
+ while (p < p_end) {
340
+ if (*p) {indices[count++] = p - p_start;}
341
+ p++;
342
+ }
343
+ }
344
+ else {
345
+ npy_intp i = 0; // position within Boolean array
346
+ npy_intp i_end = count_max;
347
+ npy_intp i_end_roll = count_max - size_div.rem;
348
+ while (i < i_end_roll) {
349
+ if (*(npy_bool*)PyArray_GETPTR1(array, i)) {indices[count++] = i;}
350
+ i++;
351
+ if (*(npy_bool*)PyArray_GETPTR1(array, i)) {indices[count++] = i;}
352
+ i++;
353
+ if (*(npy_bool*)PyArray_GETPTR1(array, i)) {indices[count++] = i;}
354
+ i++;
355
+ if (*(npy_bool*)PyArray_GETPTR1(array, i)) {indices[count++] = i;}
356
+ i++;
357
+ if (*(npy_bool*)PyArray_GETPTR1(array, i)) {indices[count++] = i;}
358
+ i++;
359
+ if (*(npy_bool*)PyArray_GETPTR1(array, i)) {indices[count++] = i;}
360
+ i++;
361
+ if (*(npy_bool*)PyArray_GETPTR1(array, i)) {indices[count++] = i;}
362
+ i++;
363
+ if (*(npy_bool*)PyArray_GETPTR1(array, i)) {indices[count++] = i;}
364
+ i++;
365
+ }
366
+ while (i < i_end) {
367
+ if (*(npy_bool*)PyArray_GETPTR1(array, i)) {indices[count++] = i;}
368
+ i++;
369
+ }
370
+ }
371
+ NPY_END_THREADS;
372
+
373
+ npy_intp dims = {count};
374
+ final = PyArray_SimpleNewFromData(1, &dims, NPY_INT64, (void*)indices);
375
+ if (!final) {
376
+ free(indices);
377
+ return NULL;
378
+ }
379
+ // This ensures that the array frees the indices array; this has been tested by calling free(indices) and observing segfault
380
+ PyArray_ENABLEFLAGS((PyArrayObject*)final, NPY_ARRAY_OWNDATA);
381
+ PyArray_CLEARFLAGS((PyArrayObject*)final, NPY_ARRAY_WRITEABLE);
382
+ return final;
383
+ }
384
+
385
+ static inline NPY_DATETIMEUNIT
386
+ AK_dt_unit_from_scalar(PyDatetimeScalarObject* dts) {
387
+ // Based on convert_pyobject_to_datetime and related usage in datetime.c
388
+ PyArray_DatetimeMetaData* dma = &(dts->obmeta);
389
+ return dma->base;
390
+ }
391
+
392
+ #endif /* ARRAYKIT_SRC_UTILITIES_H_ */