Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
ndarrayobject.h305 linesDownload Raw Back to numpy
1/*
2 * DON'T INCLUDE THIS DIRECTLY.
3 */
4#ifndef NUMPY_CORE_INCLUDE_NUMPY_NDARRAYOBJECT_H_
5#define NUMPY_CORE_INCLUDE_NUMPY_NDARRAYOBJECT_H_
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11#include <Python.h>
12#include "ndarraytypes.h"
13#include "dtype_api.h"
14
15/* Includes the "function" C-API -- these are all stored in a
16   list of pointers --- one for each file
17   The two lists are concatenated into one in multiarray.
18
19   They are available as import_array()
20*/
21
22#include "__multiarray_api.h"
23
24/*
25 * Include any definitions which are defined differently for 1.x and 2.x
26 * (Symbols only available on 2.x are not there, but rather guarded.)
27 */
28#include "npy_2_compat.h"
29
30/* C-API that requires previous API to be defined */
31
32#define PyArray_DescrCheck(op) PyObject_TypeCheck(op, &PyArrayDescr_Type)
33
34#define PyArray_Check(op) PyObject_TypeCheck(op, &PyArray_Type)
35#define PyArray_CheckExact(op) (Py_TYPE((PyObject*)(op)) == &PyArray_Type)
36
37#define PyArray_HasArrayInterfaceType(op, type, context, out)                 \
38        ((((out)=PyArray_FromStructInterface(op)) != Py_NotImplemented) ||    \
39         (((out)=PyArray_FromInterface(op)) != Py_NotImplemented) ||          \
40         (((out)=PyArray_FromArrayAttr(op, type, context)) !=                 \
41          Py_NotImplemented))
42
43#define PyArray_HasArrayInterface(op, out)                                    \
44        PyArray_HasArrayInterfaceType(op, NULL, NULL, out)
45
46#define PyArray_IsZeroDim(op) (PyArray_Check(op) && \
47                               (PyArray_NDIM((PyArrayObject *)op) == 0))
48
49#define PyArray_IsScalar(obj, cls)                                            \
50        (PyObject_TypeCheck(obj, &Py##cls##ArrType_Type))
51
52#define PyArray_CheckScalar(m) (PyArray_IsScalar(m, Generic) ||               \
53                                PyArray_IsZeroDim(m))
54#define PyArray_IsPythonNumber(obj)                                           \
55        (PyFloat_Check(obj) || PyComplex_Check(obj) ||                        \
56         PyLong_Check(obj) || PyBool_Check(obj))
57#define PyArray_IsIntegerScalar(obj) (PyLong_Check(obj)                       \
58              || PyArray_IsScalar((obj), Integer))
59#define PyArray_IsPythonScalar(obj)                                           \
60        (PyArray_IsPythonNumber(obj) || PyBytes_Check(obj) ||                 \
61         PyUnicode_Check(obj))
62
63#define PyArray_IsAnyScalar(obj)                                              \
64        (PyArray_IsScalar(obj, Generic) || PyArray_IsPythonScalar(obj))
65
66#define PyArray_CheckAnyScalar(obj) (PyArray_IsPythonScalar(obj) ||           \
67                                     PyArray_CheckScalar(obj))
68
69
70#define PyArray_GETCONTIGUOUS(m) (PyArray_ISCONTIGUOUS(m) ?                   \
71                                  Py_INCREF(m), (m) :                         \
72                                  (PyArrayObject *)(PyArray_Copy(m)))
73
74#define PyArray_SAMESHAPE(a1,a2) ((PyArray_NDIM(a1) == PyArray_NDIM(a2)) &&   \
75                                  PyArray_CompareLists(PyArray_DIMS(a1),      \
76                                                       PyArray_DIMS(a2),      \
77                                                       PyArray_NDIM(a1)))
78
79#define PyArray_SIZE(m) PyArray_MultiplyList(PyArray_DIMS(m), PyArray_NDIM(m))
80#define PyArray_NBYTES(m) (PyArray_ITEMSIZE(m) * PyArray_SIZE(m))
81#define PyArray_FROM_O(m) PyArray_FromAny(m, NULL, 0, 0, 0, NULL)
82
83#define PyArray_FROM_OF(m,flags) PyArray_CheckFromAny(m, NULL, 0, 0, flags,   \
84                                                      NULL)
85
86#define PyArray_FROM_OT(m,type) PyArray_FromAny(m,                            \
87                                PyArray_DescrFromType(type), 0, 0, 0, NULL)
88
89#define PyArray_FROM_OTF(m, type, flags) \
90        PyArray_FromAny(m, PyArray_DescrFromType(type), 0, 0, \
91                        (((flags) & NPY_ARRAY_ENSURECOPY) ? \
92                         ((flags) | NPY_ARRAY_DEFAULT) : (flags)), NULL)
93
94#define PyArray_FROMANY(m, type, min, max, flags) \
95        PyArray_FromAny(m, PyArray_DescrFromType(type), min, max, \
96                        (((flags) & NPY_ARRAY_ENSURECOPY) ? \
97                         (flags) | NPY_ARRAY_DEFAULT : (flags)), NULL)
98
99#define PyArray_ZEROS(m, dims, type, is_f_order) \
100        PyArray_Zeros(m, dims, PyArray_DescrFromType(type), is_f_order)
101
102#define PyArray_EMPTY(m, dims, type, is_f_order) \
103        PyArray_Empty(m, dims, PyArray_DescrFromType(type), is_f_order)
104
105#define PyArray_FILLWBYTE(obj, val) memset(PyArray_DATA(obj), val, \
106                                           PyArray_NBYTES(obj))
107
108#define PyArray_ContiguousFromAny(op, type, min_depth, max_depth) \
109        PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
110                              max_depth, NPY_ARRAY_DEFAULT, NULL)
111
112#define PyArray_EquivArrTypes(a1, a2) \
113        PyArray_EquivTypes(PyArray_DESCR(a1), PyArray_DESCR(a2))
114
115#define PyArray_EquivByteorders(b1, b2) \
116        (((b1) == (b2)) || (PyArray_ISNBO(b1) == PyArray_ISNBO(b2)))
117
118#define PyArray_SimpleNew(nd, dims, typenum) \
119        PyArray_New(&PyArray_Type, nd, dims, typenum, NULL, NULL, 0, 0, NULL)
120
121#define PyArray_SimpleNewFromData(nd, dims, typenum, data) \
122        PyArray_New(&PyArray_Type, nd, dims, typenum, NULL, \
123                    data, 0, NPY_ARRAY_CARRAY, NULL)
124
125#define PyArray_SimpleNewFromDescr(nd, dims, descr) \
126        PyArray_NewFromDescr(&PyArray_Type, descr, nd, dims, \
127                             NULL, NULL, 0, NULL)
128
129#define PyArray_ToScalar(data, arr) \
130        PyArray_Scalar(data, PyArray_DESCR(arr), (PyObject *)arr)
131
132
133/* These might be faster without the dereferencing of obj
134   going on inside -- of course an optimizing compiler should
135   inline the constants inside a for loop making it a moot point
136*/
137
138#define PyArray_GETPTR1(obj, i) ((void *)(PyArray_BYTES(obj) + \
139                                         (i)*PyArray_STRIDES(obj)[0]))
140
141#define PyArray_GETPTR2(obj, i, j) ((void *)(PyArray_BYTES(obj) + \
142                                            (i)*PyArray_STRIDES(obj)[0] + \
143                                            (j)*PyArray_STRIDES(obj)[1]))
144
145#define PyArray_GETPTR3(obj, i, j, k) ((void *)(PyArray_BYTES(obj) + \
146                                            (i)*PyArray_STRIDES(obj)[0] + \
147                                            (j)*PyArray_STRIDES(obj)[1] + \
148                                            (k)*PyArray_STRIDES(obj)[2]))
149
150#define PyArray_GETPTR4(obj, i, j, k, l) ((void *)(PyArray_BYTES(obj) + \
151                                            (i)*PyArray_STRIDES(obj)[0] + \
152                                            (j)*PyArray_STRIDES(obj)[1] + \
153                                            (k)*PyArray_STRIDES(obj)[2] + \
154                                            (l)*PyArray_STRIDES(obj)[3]))
155
156static inline void
157PyArray_DiscardWritebackIfCopy(PyArrayObject *arr)
158{
159    PyArrayObject_fields *fa = (PyArrayObject_fields *)arr;
160    if (fa && fa->base) {
161        if (fa->flags & NPY_ARRAY_WRITEBACKIFCOPY) {
162            PyArray_ENABLEFLAGS((PyArrayObject*)fa->base, NPY_ARRAY_WRITEABLE);
163            Py_DECREF(fa->base);
164            fa->base = NULL;
165            PyArray_CLEARFLAGS(arr, NPY_ARRAY_WRITEBACKIFCOPY);
166        }
167    }
168}
169
170#define PyArray_DESCR_REPLACE(descr) do { \
171                PyArray_Descr *_new_; \
172                _new_ = PyArray_DescrNew(descr); \
173                Py_XDECREF(descr); \
174                descr = _new_; \
175        } while(0)
176
177/* Copy should always return contiguous array */
178#define PyArray_Copy(obj) PyArray_NewCopy(obj, NPY_CORDER)
179
180#define PyArray_FromObject(op, type, min_depth, max_depth) \
181        PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
182                              max_depth, NPY_ARRAY_BEHAVED | \
183                                         NPY_ARRAY_ENSUREARRAY, NULL)
184
185#define PyArray_ContiguousFromObject(op, type, min_depth, max_depth) \
186        PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
187                              max_depth, NPY_ARRAY_DEFAULT | \
188                                         NPY_ARRAY_ENSUREARRAY, NULL)
189
190#define PyArray_CopyFromObject(op, type, min_depth, max_depth) \
191        PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
192                        max_depth, NPY_ARRAY_ENSURECOPY | \
193                                   NPY_ARRAY_DEFAULT | \
194                                   NPY_ARRAY_ENSUREARRAY, NULL)
195
196#define PyArray_Cast(mp, type_num)                                            \
197        PyArray_CastToType(mp, PyArray_DescrFromType(type_num), 0)
198
199#define PyArray_Take(ap, items, axis)                                         \
200        PyArray_TakeFrom(ap, items, axis, NULL, NPY_RAISE)
201
202#define PyArray_Put(ap, items, values)                                        \
203        PyArray_PutTo(ap, items, values, NPY_RAISE)
204
205
206/*
207   Check to see if this key in the dictionary is the "title"
208   entry of the tuple (i.e. a duplicate dictionary entry in the fields
209   dict).
210*/
211
212static inline int
213NPY_TITLE_KEY_check(PyObject *key, PyObject *value)
214{
215    PyObject *title;
216    if (PyTuple_Size(value) != 3) {
217        return 0;
218    }
219    title = PyTuple_GetItem(value, 2);
220    if (key == title) {
221        return 1;
222    }
223#ifdef PYPY_VERSION
224    /*
225     * On PyPy, dictionary keys do not always preserve object identity.
226     * Fall back to comparison by value.
227     */
228    if (PyUnicode_Check(title) && PyUnicode_Check(key)) {
229        return PyUnicode_Compare(title, key) == 0 ? 1 : 0;
230    }
231#endif
232    return 0;
233}
234
235/* Macro, for backward compat with "if NPY_TITLE_KEY(key, value) { ..." */
236#define NPY_TITLE_KEY(key, value) (NPY_TITLE_KEY_check((key), (value)))
237
238#define DEPRECATE(msg) PyErr_WarnEx(PyExc_DeprecationWarning,msg,1)
239#define DEPRECATE_FUTUREWARNING(msg) PyErr_WarnEx(PyExc_FutureWarning,msg,1)
240
241
242/*
243 * These macros and functions unfortunately require runtime version checks
244 * that are only defined in `npy_2_compat.h`.  For that reasons they cannot be
245 * part of `ndarraytypes.h` which tries to be self contained.
246 */
247
248static inline npy_intp
249PyArray_ITEMSIZE(const PyArrayObject *arr)
250{
251    return PyDataType_ELSIZE(((PyArrayObject_fields *)arr)->descr);
252}
253
254#define PyDataType_HASFIELDS(obj) (PyDataType_ISLEGACY((PyArray_Descr*)(obj)) && PyDataType_NAMES((PyArray_Descr*)(obj)) != NULL)
255#define PyDataType_HASSUBARRAY(dtype) (PyDataType_ISLEGACY(dtype) && PyDataType_SUBARRAY(dtype) != NULL)
256#define PyDataType_ISUNSIZED(dtype) ((dtype)->elsize == 0 && \
257                                      !PyDataType_HASFIELDS(dtype))
258
259#define PyDataType_FLAGCHK(dtype, flag) \
260        ((PyDataType_FLAGS(dtype) & (flag)) == (flag))
261
262#define PyDataType_REFCHK(dtype) \
263        PyDataType_FLAGCHK(dtype, NPY_ITEM_REFCOUNT)
264
265#define NPY_BEGIN_THREADS_DESCR(dtype) \
266        do {if (!(PyDataType_FLAGCHK((dtype), NPY_NEEDS_PYAPI))) \
267                NPY_BEGIN_THREADS;} while (0);
268
269#define NPY_END_THREADS_DESCR(dtype) \
270        do {if (!(PyDataType_FLAGCHK((dtype), NPY_NEEDS_PYAPI))) \
271                NPY_END_THREADS; } while (0);
272
273#if !(defined(NPY_INTERNAL_BUILD) && NPY_INTERNAL_BUILD)
274/* The internal copy of this is now defined in `dtypemeta.h` */
275/*
276 * `PyArray_Scalar` is the same as this function but converts will convert
277 * most NumPy types to Python scalars.
278 */
279static inline PyObject *
280PyArray_GETITEM(const PyArrayObject *arr, const char *itemptr)
281{
282    return PyDataType_GetArrFuncs(((PyArrayObject_fields *)arr)->descr)->getitem(
283                                        (void *)itemptr, (PyArrayObject *)arr);
284}
285
286/*
287 * SETITEM should only be used if it is known that the value is a scalar
288 * and of a type understood by the arrays dtype.
289 * Use `PyArray_Pack` if the value may be of a different dtype.
290 */
291static inline int
292PyArray_SETITEM(PyArrayObject *arr, char *itemptr, PyObject *v)
293{
294    return PyDataType_GetArrFuncs(((PyArrayObject_fields *)arr)->descr)->setitem(v, itemptr, arr);
295}
296#endif  /* not internal */
297
298
299#ifdef __cplusplus
300}
301#endif
302
303
304#endif  /* NUMPY_CORE_INCLUDE_NUMPY_NDARRAYOBJECT_H_ */
305