1
#ifndef NDARRAYTYPES_H
2
#define NDARRAYTYPES_H
3

4
#include "npy_common.h"
5
#include "npy_endian.h"
6
#include "npy_cpu.h"
7
#include "utils.h"
8

9
#define NPY_NO_EXPORT NPY_VISIBILITY_HIDDEN
10

11
/* Only use thread if configured in config and python supports it */
12
#if defined WITH_THREAD && !NPY_NO_SMP
13
        #define NPY_ALLOW_THREADS 1
14
#else
15
        #define NPY_ALLOW_THREADS 0
16
#endif
17

18
#ifndef __has_extension
19
#define __has_extension(x) 0
20
#endif
21

22
#if !defined(_NPY_NO_DEPRECATIONS) && \
23
    ((defined(__GNUC__)&& __GNUC__ >= 6) || \
24
     __has_extension(attribute_deprecated_with_message))
25
#define NPY_ATTR_DEPRECATE(text) __attribute__ ((deprecated (text)))
26
#else
27
#define NPY_ATTR_DEPRECATE(text)
28
#endif
29

30
/*
31
 * There are several places in the code where an array of dimensions
32
 * is allocated statically.  This is the size of that static
33
 * allocation.
34
 *
35
 * The array creation itself could have arbitrary dimensions but all
36
 * the places where static allocation is used would need to be changed
37
 * to dynamic (including inside of several structures)
38
 */
39

40
#define NPY_MAXDIMS 32
41
#define NPY_MAXARGS 32
42

43
/* Used for Converter Functions "O&" code in ParseTuple */
44
#define NPY_FAIL 0
45
#define NPY_SUCCEED 1
46

47
/*
48
 * Binary compatibility version number.  This number is increased
49
 * whenever the C-API is changed such that binary compatibility is
50
 * broken, i.e. whenever a recompile of extension modules is needed.
51
 */
52
#define NPY_VERSION NPY_ABI_VERSION
53

54
/*
55
 * Minor API version.  This number is increased whenever a change is
56
 * made to the C-API -- whether it breaks binary compatibility or not.
57
 * Some changes, such as adding a function pointer to the end of the
58
 * function table, can be made without breaking binary compatibility.
59
 * In this case, only the NPY_FEATURE_VERSION (*not* NPY_VERSION)
60
 * would be increased.  Whenever binary compatibility is broken, both
61
 * NPY_VERSION and NPY_FEATURE_VERSION should be increased.
62
 */
63
#define NPY_FEATURE_VERSION NPY_API_VERSION
64

65
enum NPY_TYPES {    NPY_BOOL=0,
66
                    NPY_BYTE, NPY_UBYTE,
67
                    NPY_SHORT, NPY_USHORT,
68
                    NPY_INT, NPY_UINT,
69
                    NPY_LONG, NPY_ULONG,
70
                    NPY_LONGLONG, NPY_ULONGLONG,
71
                    NPY_FLOAT, NPY_DOUBLE, NPY_LONGDOUBLE,
72
                    NPY_CFLOAT, NPY_CDOUBLE, NPY_CLONGDOUBLE,
73
                    NPY_OBJECT=17,
74
                    NPY_STRING, NPY_UNICODE,
75
                    NPY_VOID,
76
                    /*
77
                     * New 1.6 types appended, may be integrated
78
                     * into the above in 2.0.
79
                     */
80
                    NPY_DATETIME, NPY_TIMEDELTA, NPY_HALF,
81

82
                    NPY_NTYPES,
83
                    NPY_NOTYPE,
84
                    NPY_CHAR NPY_ATTR_DEPRECATE("Use NPY_STRING"),
85
                    NPY_USERDEF=256,  /* leave room for characters */
86

87
                    /* The number of types not including the new 1.6 types */
88
                    NPY_NTYPES_ABI_COMPATIBLE=21
89
};
90
#ifdef _MSC_VER
91
#pragma deprecated(NPY_CHAR)
92
#endif
93

94
/* basetype array priority */
95
#define NPY_PRIORITY 0.0
96

97
/* default subtype priority */
98
#define NPY_SUBTYPE_PRIORITY 1.0
99

100
/* default scalar priority */
101
#define NPY_SCALAR_PRIORITY -1000000.0
102

103
/* How many floating point types are there (excluding half) */
104
#define NPY_NUM_FLOATTYPE 3
105

106
/*
107
 * These characters correspond to the array type and the struct
108
 * module
109
 */
110

111
enum NPY_TYPECHAR {
112
        NPY_BOOLLTR = '?',
113
        NPY_BYTELTR = 'b',
114
        NPY_UBYTELTR = 'B',
115
        NPY_SHORTLTR = 'h',
116
        NPY_USHORTLTR = 'H',
117
        NPY_INTLTR = 'i',
118
        NPY_UINTLTR = 'I',
119
        NPY_LONGLTR = 'l',
120
        NPY_ULONGLTR = 'L',
121
        NPY_LONGLONGLTR = 'q',
122
        NPY_ULONGLONGLTR = 'Q',
123
        NPY_HALFLTR = 'e',
124
        NPY_FLOATLTR = 'f',
125
        NPY_DOUBLELTR = 'd',
126
        NPY_LONGDOUBLELTR = 'g',
127
        NPY_CFLOATLTR = 'F',
128
        NPY_CDOUBLELTR = 'D',
129
        NPY_CLONGDOUBLELTR = 'G',
130
        NPY_OBJECTLTR = 'O',
131
        NPY_STRINGLTR = 'S',
132
        NPY_STRINGLTR2 = 'a',
133
        NPY_UNICODELTR = 'U',
134
        NPY_VOIDLTR = 'V',
135
        NPY_DATETIMELTR = 'M',
136
        NPY_TIMEDELTALTR = 'm',
137
        NPY_CHARLTR = 'c',
138

139
        /*
140
         * No Descriptor, just a define -- this let's
141
         * Python users specify an array of integers
142
         * large enough to hold a pointer on the
143
         * platform
144
         */
145
        NPY_INTPLTR = 'p',
146
        NPY_UINTPLTR = 'P',
147

148
        /*
149
         * These are for dtype 'kinds', not dtype 'typecodes'
150
         * as the above are for.
151
         */
152
        NPY_GENBOOLLTR ='b',
153
        NPY_SIGNEDLTR = 'i',
154
        NPY_UNSIGNEDLTR = 'u',
155
        NPY_FLOATINGLTR = 'f',
156
        NPY_COMPLEXLTR = 'c'
157
};
158

159
/*
160
 * Changing this may break Numpy API compatibility
161
 * due to changing offsets in PyArray_ArrFuncs, so be
162
 * careful. Here we have reused the mergesort slot for
163
 * any kind of stable sort, the actual implementation will
164
 * depend on the data type.
165
 */
166
typedef enum {
167
        NPY_QUICKSORT=0,
168
        NPY_HEAPSORT=1,
169
        NPY_MERGESORT=2,
170
        NPY_STABLESORT=2,
171
} NPY_SORTKIND;
172
#define NPY_NSORTS (NPY_STABLESORT + 1)
173

174

175
typedef enum {
176
        NPY_INTROSELECT=0
177
} NPY_SELECTKIND;
178
#define NPY_NSELECTS (NPY_INTROSELECT + 1)
179

180

181
typedef enum {
182
        NPY_SEARCHLEFT=0,
183
        NPY_SEARCHRIGHT=1
184
} NPY_SEARCHSIDE;
185
#define NPY_NSEARCHSIDES (NPY_SEARCHRIGHT + 1)
186

187

188
typedef enum {
189
        NPY_NOSCALAR=-1,
190
        NPY_BOOL_SCALAR,
191
        NPY_INTPOS_SCALAR,
192
        NPY_INTNEG_SCALAR,
193
        NPY_FLOAT_SCALAR,
194
        NPY_COMPLEX_SCALAR,
195
        NPY_OBJECT_SCALAR
196
} NPY_SCALARKIND;
197
#define NPY_NSCALARKINDS (NPY_OBJECT_SCALAR + 1)
198

199
/* For specifying array memory layout or iteration order */
200
typedef enum {
201
        /* Fortran order if inputs are all Fortran, C otherwise */
202
        NPY_ANYORDER=-1,
203
        /* C order */
204
        NPY_CORDER=0,
205
        /* Fortran order */
206
        NPY_FORTRANORDER=1,
207
        /* An order as close to the inputs as possible */
208
        NPY_KEEPORDER=2
209
} NPY_ORDER;
210

211
/* For specifying allowed casting in operations which support it */
212
typedef enum {
213
        /* Only allow identical types */
214
        NPY_NO_CASTING=0,
215
        /* Allow identical and byte swapped types */
216
        NPY_EQUIV_CASTING=1,
217
        /* Only allow safe casts */
218
        NPY_SAFE_CASTING=2,
219
        /* Allow safe casts or casts within the same kind */
220
        NPY_SAME_KIND_CASTING=3,
221
        /* Allow any casts */
222
        NPY_UNSAFE_CASTING=4
223
} NPY_CASTING;
224

225
typedef enum {
226
        NPY_CLIP=0,
227
        NPY_WRAP=1,
228
        NPY_RAISE=2
229
} NPY_CLIPMODE;
230

231
/* The special not-a-time (NaT) value */
232
#define NPY_DATETIME_NAT NPY_MIN_INT64
233

234
/*
235
 * Upper bound on the length of a DATETIME ISO 8601 string
236
 *   YEAR: 21 (64-bit year)
237
 *   MONTH: 3
238
 *   DAY: 3
239
 *   HOURS: 3
240
 *   MINUTES: 3
241
 *   SECONDS: 3
242
 *   ATTOSECONDS: 1 + 3*6
243
 *   TIMEZONE: 5
244
 *   NULL TERMINATOR: 1
245
 */
246
#define NPY_DATETIME_MAX_ISO8601_STRLEN (21 + 3*5 + 1 + 3*6 + 6 + 1)
247

248
/* The FR in the unit names stands for frequency */
249
typedef enum {
250
        /* Force signed enum type, must be -1 for code compatibility */
251
        NPY_FR_ERROR = -1,      /* error or undetermined */
252

253
        /* Start of valid units */
254
        NPY_FR_Y = 0,           /* Years */
255
        NPY_FR_M = 1,           /* Months */
256
        NPY_FR_W = 2,           /* Weeks */
257
        /* Gap where 1.6 NPY_FR_B (value 3) was */
258
        NPY_FR_D = 4,           /* Days */
259
        NPY_FR_h = 5,           /* hours */
260
        NPY_FR_m = 6,           /* minutes */
261
        NPY_FR_s = 7,           /* seconds */
262
        NPY_FR_ms = 8,          /* milliseconds */
263
        NPY_FR_us = 9,          /* microseconds */
264
        NPY_FR_ns = 10,         /* nanoseconds */
265
        NPY_FR_ps = 11,         /* picoseconds */
266
        NPY_FR_fs = 12,         /* femtoseconds */
267
        NPY_FR_as = 13,         /* attoseconds */
268
        NPY_FR_GENERIC = 14     /* unbound units, can convert to anything */
269
} NPY_DATETIMEUNIT;
270

271
/*
272
 * NOTE: With the NPY_FR_B gap for 1.6 ABI compatibility, NPY_DATETIME_NUMUNITS
273
 * is technically one more than the actual number of units.
274
 */
275
#define NPY_DATETIME_NUMUNITS (NPY_FR_GENERIC + 1)
276
#define NPY_DATETIME_DEFAULTUNIT NPY_FR_GENERIC
277

278
/*
279
 * Business day conventions for mapping invalid business
280
 * days to valid business days.
281
 */
282
typedef enum {
283
    /* Go forward in time to the following business day. */
284
    NPY_BUSDAY_FORWARD,
285
    NPY_BUSDAY_FOLLOWING = NPY_BUSDAY_FORWARD,
286
    /* Go backward in time to the preceding business day. */
287
    NPY_BUSDAY_BACKWARD,
288
    NPY_BUSDAY_PRECEDING = NPY_BUSDAY_BACKWARD,
289
    /*
290
     * Go forward in time to the following business day, unless it
291
     * crosses a month boundary, in which case go backward
292
     */
293
    NPY_BUSDAY_MODIFIEDFOLLOWING,
294
    /*
295
     * Go backward in time to the preceding business day, unless it
296
     * crosses a month boundary, in which case go forward.
297
     */
298
    NPY_BUSDAY_MODIFIEDPRECEDING,
299
    /* Produce a NaT for non-business days. */
300
    NPY_BUSDAY_NAT,
301
    /* Raise an exception for non-business days. */
302
    NPY_BUSDAY_RAISE
303
} NPY_BUSDAY_ROLL;
304

305
/************************************************************
306
 * NumPy Auxiliary Data for inner loops, sort functions, etc.
307
 ************************************************************/
308

309
/*
310
 * When creating an auxiliary data struct, this should always appear
311
 * as the first member, like this:
312
 *
313
 * typedef struct {
314
 *     NpyAuxData base;
315
 *     double constant;
316
 * } constant_multiplier_aux_data;
317
 */
318
typedef struct NpyAuxData_tag NpyAuxData;
319

320
/* Function pointers for freeing or cloning auxiliary data */
321
typedef void (NpyAuxData_FreeFunc) (NpyAuxData *);
322
typedef NpyAuxData *(NpyAuxData_CloneFunc) (NpyAuxData *);
323

324
struct NpyAuxData_tag {
325
    NpyAuxData_FreeFunc *free;
326
    NpyAuxData_CloneFunc *clone;
327
    /* To allow for a bit of expansion without breaking the ABI */
328
    void *reserved[2];
329
};
330

331
/* Macros to use for freeing and cloning auxiliary data */
332
#define NPY_AUXDATA_FREE(auxdata) \
333
    do { \
334
        if ((auxdata) != NULL) { \
335
            (auxdata)->free(auxdata); \
336
        } \
337
    } while(0)
338
#define NPY_AUXDATA_CLONE(auxdata) \
339
    ((auxdata)->clone(auxdata))
340

341
#define NPY_ERR(str) fprintf(stderr, #str); fflush(stderr);
342
#define NPY_ERR2(str) fprintf(stderr, str); fflush(stderr);
343

344
  /*
345
   * Macros to define how array, and dimension/strides data is
346
   * allocated.
347
   */
348

349
  /* Data buffer - PyDataMem_NEW/FREE/RENEW are in multiarraymodule.c */
350

351
#define NPY_USE_PYMEM 1
352

353

354
#if NPY_USE_PYMEM == 1
355
/* use the Raw versions which are safe to call with the GIL released */
356
#define PyArray_malloc PyMem_RawMalloc
357
#define PyArray_free PyMem_RawFree
358
#define PyArray_realloc PyMem_RawRealloc
359
#else
360
#define PyArray_malloc malloc
361
#define PyArray_free free
362
#define PyArray_realloc realloc
363
#endif
364

365
/* Dimensions and strides */
366
#define PyDimMem_NEW(size)                                         \
367
    ((npy_intp *)PyArray_malloc(size*sizeof(npy_intp)))
368

369
#define PyDimMem_FREE(ptr) PyArray_free(ptr)
370

371
#define PyDimMem_RENEW(ptr,size)                                   \
372
        ((npy_intp *)PyArray_realloc(ptr,size*sizeof(npy_intp)))
373

374
/* forward declaration */
375
struct _PyArray_Descr;
376

377
/* These must deal with unaligned and swapped data if necessary */
378
typedef PyObject * (PyArray_GetItemFunc) (void *, void *);
379
typedef int (PyArray_SetItemFunc)(PyObject *, void *, void *);
380

381
typedef void (PyArray_CopySwapNFunc)(void *, npy_intp, void *, npy_intp,
382
                                     npy_intp, int, void *);
383

384
typedef void (PyArray_CopySwapFunc)(void *, void *, int, void *);
385
typedef npy_bool (PyArray_NonzeroFunc)(void *, void *);
386

387

388
/*
389
 * These assume aligned and notswapped data -- a buffer will be used
390
 * before or contiguous data will be obtained
391
 */
392

393
typedef int (PyArray_CompareFunc)(const void *, const void *, void *);
394
typedef int (PyArray_ArgFunc)(void*, npy_intp, npy_intp*, void *);
395

396
typedef void (PyArray_DotFunc)(void *, npy_intp, void *, npy_intp, void *,
397
                               npy_intp, void *);
398

399
typedef void (PyArray_VectorUnaryFunc)(void *, void *, npy_intp, void *,
400
                                       void *);
401

402
/*
403
 * XXX the ignore argument should be removed next time the API version
404
 * is bumped. It used to be the separator.
405
 */
406
typedef int (PyArray_ScanFunc)(FILE *fp, void *dptr,
407
                               char *ignore, struct _PyArray_Descr *);
408
typedef int (PyArray_FromStrFunc)(char *s, void *dptr, char **endptr,
409
                                  struct _PyArray_Descr *);
410

411
typedef int (PyArray_FillFunc)(void *, npy_intp, void *);
412

413
typedef int (PyArray_SortFunc)(void *, npy_intp, void *);
414
typedef int (PyArray_ArgSortFunc)(void *, npy_intp *, npy_intp, void *);
415
typedef int (PyArray_PartitionFunc)(void *, npy_intp, npy_intp,
416
                                    npy_intp *, npy_intp *,
417
                                    void *);
418
typedef int (PyArray_ArgPartitionFunc)(void *, npy_intp *, npy_intp, npy_intp,
419
                                       npy_intp *, npy_intp *,
420
                                       void *);
421

422
typedef int (PyArray_FillWithScalarFunc)(void *, npy_intp, void *, void *);
423

424
typedef int (PyArray_ScalarKindFunc)(void *);
425

426
typedef void (PyArray_FastClipFunc)(void *in, npy_intp n_in, void *min,
427
                                    void *max, void *out);
428
typedef void (PyArray_FastPutmaskFunc)(void *in, void *mask, npy_intp n_in,
429
                                       void *values, npy_intp nv);
430
typedef int  (PyArray_FastTakeFunc)(void *dest, void *src, npy_intp *indarray,
431
                                       npy_intp nindarray, npy_intp n_outer,
432
                                       npy_intp m_middle, npy_intp nelem,
433
                                       NPY_CLIPMODE clipmode);
434

435
typedef struct {
436
        npy_intp *ptr;
437
        int len;
438
} PyArray_Dims;
439

440
typedef struct {
441
        /*
442
         * Functions to cast to most other standard types
443
         * Can have some NULL entries. The types
444
         * DATETIME, TIMEDELTA, and HALF go into the castdict
445
         * even though they are built-in.
446
         */
447
        PyArray_VectorUnaryFunc *cast[NPY_NTYPES_ABI_COMPATIBLE];
448

449
        /* The next four functions *cannot* be NULL */
450

451
        /*
452
         * Functions to get and set items with standard Python types
453
         * -- not array scalars
454
         */
455
        PyArray_GetItemFunc *getitem;
456
        PyArray_SetItemFunc *setitem;
457

458
        /*
459
         * Copy and/or swap data.  Memory areas may not overlap
460
         * Use memmove first if they might
461
         */
462
        PyArray_CopySwapNFunc *copyswapn;
463
        PyArray_CopySwapFunc *copyswap;
464

465
        /*
466
         * Function to compare items
467
         * Can be NULL
468
         */
469
        PyArray_CompareFunc *compare;
470

471
        /*
472
         * Function to select largest
473
         * Can be NULL
474
         */
475
        PyArray_ArgFunc *argmax;
476

477
        /*
478
         * Function to compute dot product
479
         * Can be NULL
480
         */
481
        PyArray_DotFunc *dotfunc;
482

483
        /*
484
         * Function to scan an ASCII file and
485
         * place a single value plus possible separator
486
         * Can be NULL
487
         */
488
        PyArray_ScanFunc *scanfunc;
489

490
        /*
491
         * Function to read a single value from a string
492
         * and adjust the pointer; Can be NULL
493
         */
494
        PyArray_FromStrFunc *fromstr;
495

496
        /*
497
         * Function to determine if data is zero or not
498
         * If NULL a default version is
499
         * used at Registration time.
500
         */
501
        PyArray_NonzeroFunc *nonzero;
502

503
        /*
504
         * Used for arange. Should return 0 on success
505
         * and -1 on failure.
506
         * Can be NULL.
507
         */
508
        PyArray_FillFunc *fill;
509

510
        /*
511
         * Function to fill arrays with scalar values
512
         * Can be NULL
513
         */
514
        PyArray_FillWithScalarFunc *fillwithscalar;
515

516
        /*
517
         * Sorting functions
518
         * Can be NULL
519
         */
520
        PyArray_SortFunc *sort[NPY_NSORTS];
521
        PyArray_ArgSortFunc *argsort[NPY_NSORTS];
522

523
        /*
524
         * Dictionary of additional casting functions
525
         * PyArray_VectorUnaryFuncs
526
         * which can be populated to support casting
527
         * to other registered types. Can be NULL
528
         */
529
        PyObject *castdict;
530

531
        /*
532
         * Functions useful for generalizing
533
         * the casting rules.
534
         * Can be NULL;
535
         */
536
        PyArray_ScalarKindFunc *scalarkind;
537
        int **cancastscalarkindto;
538
        int *cancastto;
539

540
        PyArray_FastClipFunc *fastclip;
541
        PyArray_FastPutmaskFunc *fastputmask;
542
        PyArray_FastTakeFunc *fasttake;
543

544
        /*
545
         * Function to select smallest
546
         * Can be NULL
547
         */
548
        PyArray_ArgFunc *argmin;
549

550
} PyArray_ArrFuncs;
551

552
/* The item must be reference counted when it is inserted or extracted. */
553
#define NPY_ITEM_REFCOUNT   0x01
554
/* Same as needing REFCOUNT */
555
#define NPY_ITEM_HASOBJECT  0x01
556
/* Convert to list for pickling */
557
#define NPY_LIST_PICKLE     0x02
558
/* The item is a POINTER  */
559
#define NPY_ITEM_IS_POINTER 0x04
560
/* memory needs to be initialized for this data-type */
561
#define NPY_NEEDS_INIT      0x08
562
/* operations need Python C-API so don't give-up thread. */
563
#define NPY_NEEDS_PYAPI     0x10
564
/* Use f.getitem when extracting elements of this data-type */
565
#define NPY_USE_GETITEM     0x20
566
/* Use f.setitem when setting creating 0-d array from this data-type.*/
567
#define NPY_USE_SETITEM     0x40
568
/* A sticky flag specifically for structured arrays */
569
#define NPY_ALIGNED_STRUCT  0x80
570

571
/*
572
 *These are inherited for global data-type if any data-types in the
573
 * field have them
574
 */
575
#define NPY_FROM_FIELDS    (NPY_NEEDS_INIT | NPY_LIST_PICKLE | \
576
                            NPY_ITEM_REFCOUNT | NPY_NEEDS_PYAPI)
577

578
#define NPY_OBJECT_DTYPE_FLAGS (NPY_LIST_PICKLE | NPY_USE_GETITEM | \
579
                                NPY_ITEM_IS_POINTER | NPY_ITEM_REFCOUNT | \
580
                                NPY_NEEDS_INIT | NPY_NEEDS_PYAPI)
581

582
#define PyDataType_FLAGCHK(dtype, flag) \
583
        (((dtype)->flags & (flag)) == (flag))
584

585
#define PyDataType_REFCHK(dtype) \
586
        PyDataType_FLAGCHK(dtype, NPY_ITEM_REFCOUNT)
587

588
typedef struct _PyArray_Descr {
589
        PyObject_HEAD
590
        /*
591
         * the type object representing an
592
         * instance of this type -- should not
593
         * be two type_numbers with the same type
594
         * object.
595
         */
596
        PyTypeObject *typeobj;
597
        /* kind for this type */
598
        char kind;
599
        /* unique-character representing this type */
600
        char type;
601
        /*
602
         * '>' (big), '<' (little), '|'
603
         * (not-applicable), or '=' (native).
604
         */
605
        char byteorder;
606
        /* flags describing data type */
607
        char flags;
608
        /* number representing this type */
609
        int type_num;
610
        /* element size (itemsize) for this type */
611
        int elsize;
612
        /* alignment needed for this type */
613
        int alignment;
614
        /*
615
         * Non-NULL if this type is
616
         * is an array (C-contiguous)
617
         * of some other type
618
         */
619
        struct _arr_descr *subarray;
620
        /*
621
         * The fields dictionary for this type
622
         * For statically defined descr this
623
         * is always Py_None
624
         */
625
        PyObject *fields;
626
        /*
627
         * An ordered tuple of field names or NULL
628
         * if no fields are defined
629
         */
630
        PyObject *names;
631
        /*
632
         * a table of functions specific for each
633
         * basic data descriptor
634
         */
635
        PyArray_ArrFuncs *f;
636
        /* Metadata about this dtype */
637
        PyObject *metadata;
638
        /*
639
         * Metadata specific to the C implementation
640
         * of the particular dtype. This was added
641
         * for NumPy 1.7.0.
642
         */
643
        NpyAuxData *c_metadata;
644
        /* Cached hash value (-1 if not yet computed).
645
         * This was added for NumPy 2.0.0.
646
         */
647
        npy_hash_t hash;
648
} PyArray_Descr;
649

650
typedef struct _arr_descr {
651
        PyArray_Descr *base;
652
        PyObject *shape;       /* a tuple */
653
} PyArray_ArrayDescr;
654

655
/*
656
 * The main array object structure.
657
 *
658
 * It has been recommended to use the inline functions defined below
659
 * (PyArray_DATA and friends) to access fields here for a number of
660
 * releases. Direct access to the members themselves is deprecated.
661
 * To ensure that your code does not use deprecated access,
662
 * #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
663
 * (or NPY_1_8_API_VERSION or higher as required).
664
 */
665
/* This struct will be moved to a private header in a future release */
666
typedef struct tagPyArrayObject_fields {
667
    PyObject_HEAD
668
    /* Pointer to the raw data buffer */
669
    char *data;
670
    /* The number of dimensions, also called 'ndim' */
671
    int nd;
672
    /* The size in each dimension, also called 'shape' */
673
    npy_intp *dimensions;
674
    /*
675
     * Number of bytes to jump to get to the
676
     * next element in each dimension
677
     */
678
    npy_intp *strides;
679
    /*
680
     * This object is decref'd upon
681
     * deletion of array. Except in the
682
     * case of WRITEBACKIFCOPY which has
683
     * special handling.
684
     *
685
     * For views it points to the original
686
     * array, collapsed so no chains of
687
     * views occur.
688
     *
689
     * For creation from buffer object it
690
     * points to an object that should be
691
     * decref'd on deletion
692
     *
693
     * For WRITEBACKIFCOPY flag this is an
694
     * array to-be-updated upon calling
695
     * PyArray_ResolveWritebackIfCopy
696
     */
697
    PyObject *base;
698
    /* Pointer to type structure */
699
    PyArray_Descr *descr;
700
    /* Flags describing array -- see below */
701
    int flags;
702
    /* For weak references */
703
    PyObject *weakreflist;
704
} PyArrayObject_fields;
705

706
/*
707
 * To hide the implementation details, we only expose
708
 * the Python struct HEAD.
709
 */
710
#if !defined(NPY_NO_DEPRECATED_API) || \
711
    (NPY_NO_DEPRECATED_API < NPY_1_7_API_VERSION)
712
/*
713
 * Can't put this in npy_deprecated_api.h like the others.
714
 * PyArrayObject field access is deprecated as of NumPy 1.7.
715
 */
716
typedef PyArrayObject_fields PyArrayObject;
717
#else
718
typedef struct tagPyArrayObject {
719
        PyObject_HEAD
720
} PyArrayObject;
721
#endif
722

723
#define NPY_SIZEOF_PYARRAYOBJECT (sizeof(PyArrayObject_fields))
724

725
/* Array Flags Object */
726
typedef struct PyArrayFlagsObject {
727
        PyObject_HEAD
728
        PyObject *arr;
729
        int flags;
730
} PyArrayFlagsObject;
731

732
/* Mirrors buffer object to ptr */
733

734
typedef struct {
735
        PyObject_HEAD
736
        PyObject *base;
737
        void *ptr;
738
        npy_intp len;
739
        int flags;
740
} PyArray_Chunk;
741

742
typedef struct {
743
    NPY_DATETIMEUNIT base;
744
    int num;
745
} PyArray_DatetimeMetaData;
746

747
typedef struct {
748
    NpyAuxData base;
749
    PyArray_DatetimeMetaData meta;
750
} PyArray_DatetimeDTypeMetaData;
751

752
/*
753
 * This structure contains an exploded view of a date-time value.
754
 * NaT is represented by year == NPY_DATETIME_NAT.
755
 */
756
typedef struct {
757
        npy_int64 year;
758
        npy_int32 month, day, hour, min, sec, us, ps, as;
759
} npy_datetimestruct;
760

761
/* This is not used internally. */
762
typedef struct {
763
        npy_int64 day;
764
        npy_int32 sec, us, ps, as;
765
} npy_timedeltastruct;
766

767
typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *);
768

769
/*
770
 * Means c-style contiguous (last index varies the fastest). The data
771
 * elements right after each other.
772
 *
773
 * This flag may be requested in constructor functions.
774
 * This flag may be tested for in PyArray_FLAGS(arr).
775
 */
776
#define NPY_ARRAY_C_CONTIGUOUS    0x0001
777

778
/*
779
 * Set if array is a contiguous Fortran array: the first index varies
780
 * the fastest in memory (strides array is reverse of C-contiguous
781
 * array)
782
 *
783
 * This flag may be requested in constructor functions.
784
 * This flag may be tested for in PyArray_FLAGS(arr).
785
 */
786
#define NPY_ARRAY_F_CONTIGUOUS    0x0002
787

788
/*
789
 * Note: all 0-d arrays are C_CONTIGUOUS and F_CONTIGUOUS. If a
790
 * 1-d array is C_CONTIGUOUS it is also F_CONTIGUOUS. Arrays with
791
 * more then one dimension can be C_CONTIGUOUS and F_CONTIGUOUS
792
 * at the same time if they have either zero or one element.
793
 * If NPY_RELAXED_STRIDES_CHECKING is set, a higher dimensional
794
 * array is always C_CONTIGUOUS and F_CONTIGUOUS if it has zero elements
795
 * and the array is contiguous if ndarray.squeeze() is contiguous.
796
 * I.e. dimensions for which `ndarray.shape[dimension] == 1` are
797
 * ignored.
798
 */
799

800
/*
801
 * If set, the array owns the data: it will be free'd when the array
802
 * is deleted.
803
 *
804
 * This flag may be tested for in PyArray_FLAGS(arr).
805
 */
806
#define NPY_ARRAY_OWNDATA         0x0004
807

808
/*
809
 * An array never has the next four set; they're only used as parameter
810
 * flags to the various FromAny functions
811
 *
812
 * This flag may be requested in constructor functions.
813
 */
814

815
/* Cause a cast to occur regardless of whether or not it is safe. */
816
#define NPY_ARRAY_FORCECAST       0x0010
817

818
/*
819
 * Always copy the array. Returned arrays are always CONTIGUOUS,
820
 * ALIGNED, and WRITEABLE.
821
 *
822
 * This flag may be requested in constructor functions.
823
 */
824
#define NPY_ARRAY_ENSURECOPY      0x0020
825

826
/*
827
 * Make sure the returned array is a base-class ndarray
828
 *
829
 * This flag may be requested in constructor functions.
830
 */
831
#define NPY_ARRAY_ENSUREARRAY     0x0040
832

833
/*
834
 * Make sure that the strides are in units of the element size Needed
835
 * for some operations with record-arrays.
836
 *
837
 * This flag may be requested in constructor functions.
838
 */
839
#define NPY_ARRAY_ELEMENTSTRIDES  0x0080
840

841
/*
842
 * Array data is aligned on the appropriate memory address for the type
843
 * stored according to how the compiler would align things (e.g., an
844
 * array of integers (4 bytes each) starts on a memory address that's
845
 * a multiple of 4)
846
 *
847
 * This flag may be requested in constructor functions.
848
 * This flag may be tested for in PyArray_FLAGS(arr).
849
 */
850
#define NPY_ARRAY_ALIGNED         0x0100
851

852
/*
853
 * Array data has the native endianness
854
 *
855
 * This flag may be requested in constructor functions.
856
 */
857
#define NPY_ARRAY_NOTSWAPPED      0x0200
858

859
/*
860
 * Array data is writeable
861
 *
862
 * This flag may be requested in constructor functions.
863
 * This flag may be tested for in PyArray_FLAGS(arr).
864
 */
865
#define NPY_ARRAY_WRITEABLE       0x0400
866

867
/*
868
 * If this flag is set, then base contains a pointer to an array of
869
 * the same size that should be updated with the current contents of
870
 * this array when PyArray_ResolveWritebackIfCopy is called.
871
 *
872
 * This flag may be requested in constructor functions.
873
 * This flag may be tested for in PyArray_FLAGS(arr).
874
 */
875
#define NPY_ARRAY_UPDATEIFCOPY    0x1000 /* Deprecated in 1.14 */
876
#define NPY_ARRAY_WRITEBACKIFCOPY 0x2000
877

878
/*
879
 * NOTE: there are also internal flags defined in multiarray/arrayobject.h,
880
 * which start at bit 31 and work down.
881
 */
882

883
#define NPY_ARRAY_BEHAVED      (NPY_ARRAY_ALIGNED | \
884
                                NPY_ARRAY_WRITEABLE)
885
#define NPY_ARRAY_BEHAVED_NS   (NPY_ARRAY_ALIGNED | \
886
                                NPY_ARRAY_WRITEABLE | \
887
                                NPY_ARRAY_NOTSWAPPED)
888
#define NPY_ARRAY_CARRAY       (NPY_ARRAY_C_CONTIGUOUS | \
889
                                NPY_ARRAY_BEHAVED)
890
#define NPY_ARRAY_CARRAY_RO    (NPY_ARRAY_C_CONTIGUOUS | \
891
                                NPY_ARRAY_ALIGNED)
892
#define NPY_ARRAY_FARRAY       (NPY_ARRAY_F_CONTIGUOUS | \
893
                                NPY_ARRAY_BEHAVED)
894
#define NPY_ARRAY_FARRAY_RO    (NPY_ARRAY_F_CONTIGUOUS | \
895
                                NPY_ARRAY_ALIGNED)
896
#define NPY_ARRAY_DEFAULT      (NPY_ARRAY_CARRAY)
897
#define NPY_ARRAY_IN_ARRAY     (NPY_ARRAY_CARRAY_RO)
898
#define NPY_ARRAY_OUT_ARRAY    (NPY_ARRAY_CARRAY)
899
#define NPY_ARRAY_INOUT_ARRAY  (NPY_ARRAY_CARRAY | \
900
                                NPY_ARRAY_UPDATEIFCOPY)
901
#define NPY_ARRAY_INOUT_ARRAY2 (NPY_ARRAY_CARRAY | \
902
                                NPY_ARRAY_WRITEBACKIFCOPY)
903
#define NPY_ARRAY_IN_FARRAY    (NPY_ARRAY_FARRAY_RO)
904
#define NPY_ARRAY_OUT_FARRAY   (NPY_ARRAY_FARRAY)
905
#define NPY_ARRAY_INOUT_FARRAY (NPY_ARRAY_FARRAY | \
906
                                NPY_ARRAY_UPDATEIFCOPY)
907
#define NPY_ARRAY_INOUT_FARRAY2 (NPY_ARRAY_FARRAY | \
908
                                NPY_ARRAY_WRITEBACKIFCOPY)
909

910
#define NPY_ARRAY_UPDATE_ALL   (NPY_ARRAY_C_CONTIGUOUS | \
911
                                NPY_ARRAY_F_CONTIGUOUS | \
912
                                NPY_ARRAY_ALIGNED)
913

914
/* This flag is for the array interface, not PyArrayObject */
915
#define NPY_ARR_HAS_DESCR  0x0800
916

917

918

919

920
/*
921
 * Size of internal buffers used for alignment Make BUFSIZE a multiple
922
 * of sizeof(npy_cdouble) -- usually 16 so that ufunc buffers are aligned
923
 */
924
#define NPY_MIN_BUFSIZE ((int)sizeof(npy_cdouble))
925
#define NPY_MAX_BUFSIZE (((int)sizeof(npy_cdouble))*1000000)
926
#define NPY_BUFSIZE 8192
927
/* buffer stress test size: */
928
/*#define NPY_BUFSIZE 17*/
929

930
#define PyArray_MAX(a,b) (((a)>(b))?(a):(b))
931
#define PyArray_MIN(a,b) (((a)<(b))?(a):(b))
932
#define PyArray_CLT(p,q) ((((p).real==(q).real) ? ((p).imag < (q).imag) : \
933
                               ((p).real < (q).real)))
934
#define PyArray_CGT(p,q) ((((p).real==(q).real) ? ((p).imag > (q).imag) : \
935
                               ((p).real > (q).real)))
936
#define PyArray_CLE(p,q) ((((p).real==(q).real) ? ((p).imag <= (q).imag) : \
937
                               ((p).real <= (q).real)))
938
#define PyArray_CGE(p,q) ((((p).real==(q).real) ? ((p).imag >= (q).imag) : \
939
                               ((p).real >= (q).real)))
940
#define PyArray_CEQ(p,q) (((p).real==(q).real) && ((p).imag == (q).imag))
941
#define PyArray_CNE(p,q) (((p).real!=(q).real) || ((p).imag != (q).imag))
942

943
/*
944
 * C API: consists of Macros and functions.  The MACROS are defined
945
 * here.
946
 */
947

948

949
#define PyArray_ISCONTIGUOUS(m) PyArray_CHKFLAGS((m), NPY_ARRAY_C_CONTIGUOUS)
950
#define PyArray_ISWRITEABLE(m) PyArray_CHKFLAGS((m), NPY_ARRAY_WRITEABLE)
951
#define PyArray_ISALIGNED(m) PyArray_CHKFLAGS((m), NPY_ARRAY_ALIGNED)
952

953
#define PyArray_IS_C_CONTIGUOUS(m) PyArray_CHKFLAGS((m), NPY_ARRAY_C_CONTIGUOUS)
954
#define PyArray_IS_F_CONTIGUOUS(m) PyArray_CHKFLAGS((m), NPY_ARRAY_F_CONTIGUOUS)
955

956
/* the variable is used in some places, so always define it */
957
#define NPY_BEGIN_THREADS_DEF PyThreadState *_save=NULL;
958
#if NPY_ALLOW_THREADS
959
#define NPY_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
960
#define NPY_END_ALLOW_THREADS Py_END_ALLOW_THREADS
961
#define NPY_BEGIN_THREADS do {_save = PyEval_SaveThread();} while (0);
962
#define NPY_END_THREADS   do { if (_save) \
963
                { PyEval_RestoreThread(_save); _save = NULL;} } while (0);
964
#define NPY_BEGIN_THREADS_THRESHOLDED(loop_size) do { if ((loop_size) > 500) \
965
                { _save = PyEval_SaveThread();} } while (0);
966

967
#define NPY_BEGIN_THREADS_DESCR(dtype) \
968
        do {if (!(PyDataType_FLAGCHK((dtype), NPY_NEEDS_PYAPI))) \
969
                NPY_BEGIN_THREADS;} while (0);
970

971
#define NPY_END_THREADS_DESCR(dtype) \
972
        do {if (!(PyDataType_FLAGCHK((dtype), NPY_NEEDS_PYAPI))) \
973
                NPY_END_THREADS; } while (0);
974

975
#define NPY_ALLOW_C_API_DEF  PyGILState_STATE __save__;
976
#define NPY_ALLOW_C_API      do {__save__ = PyGILState_Ensure();} while (0);
977
#define NPY_DISABLE_C_API    do {PyGILState_Release(__save__);} while (0);
978
#else
979
#define NPY_BEGIN_ALLOW_THREADS
980
#define NPY_END_ALLOW_THREADS
981
#define NPY_BEGIN_THREADS
982
#define NPY_END_THREADS
983
#define NPY_BEGIN_THREADS_THRESHOLDED(loop_size)
984
#define NPY_BEGIN_THREADS_DESCR(dtype)
985
#define NPY_END_THREADS_DESCR(dtype)
986
#define NPY_ALLOW_C_API_DEF
987
#define NPY_ALLOW_C_API
988
#define NPY_DISABLE_C_API
989
#endif
990

991
/**********************************
992
 * The nditer object, added in 1.6
993
 **********************************/
994

995
/* The actual structure of the iterator is an internal detail */
996
typedef struct NpyIter_InternalOnly NpyIter;
997

998
/* Iterator function pointers that may be specialized */
999
typedef int (NpyIter_IterNextFunc)(NpyIter *iter);
1000
typedef void (NpyIter_GetMultiIndexFunc)(NpyIter *iter,
1001
                                      npy_intp *outcoords);
1002

1003
/*** Global flags that may be passed to the iterator constructors ***/
1004

1005
/* Track an index representing C order */
1006
#define NPY_ITER_C_INDEX                    0x00000001
1007
/* Track an index representing Fortran order */
1008
#define NPY_ITER_F_INDEX                    0x00000002
1009
/* Track a multi-index */
1010
#define NPY_ITER_MULTI_INDEX                0x00000004
1011
/* User code external to the iterator does the 1-dimensional innermost loop */
1012
#define NPY_ITER_EXTERNAL_LOOP              0x00000008
1013
/* Convert all the operands to a common data type */
1014
#define NPY_ITER_COMMON_DTYPE               0x00000010
1015
/* Operands may hold references, requiring API access during iteration */
1016
#define NPY_ITER_REFS_OK                    0x00000020
1017
/* Zero-sized operands should be permitted, iteration checks IterSize for 0 */
1018
#define NPY_ITER_ZEROSIZE_OK                0x00000040
1019
/* Permits reductions (size-0 stride with dimension size > 1) */
1020
#define NPY_ITER_REDUCE_OK                  0x00000080
1021
/* Enables sub-range iteration */
1022
#define NPY_ITER_RANGED                     0x00000100
1023
/* Enables buffering */
1024
#define NPY_ITER_BUFFERED                   0x00000200
1025
/* When buffering is enabled, grows the inner loop if possible */
1026
#define NPY_ITER_GROWINNER                  0x00000400
1027
/* Delay allocation of buffers until first Reset* call */
1028
#define NPY_ITER_DELAY_BUFALLOC             0x00000800
1029
/* When NPY_KEEPORDER is specified, disable reversing negative-stride axes */
1030
#define NPY_ITER_DONT_NEGATE_STRIDES        0x00001000
1031
/*
1032
 * If output operands overlap with other operands (based on heuristics that
1033
 * has false positives but no false negatives), make temporary copies to
1034
 * eliminate overlap.
1035
 */
1036
#define NPY_ITER_COPY_IF_OVERLAP            0x00002000
1037

1038
/*** Per-operand flags that may be passed to the iterator constructors ***/
1039

1040
/* The operand will be read from and written to */
1041
#define NPY_ITER_READWRITE                  0x00010000
1042
/* The operand will only be read from */
1043
#define NPY_ITER_READONLY                   0x00020000
1044
/* The operand will only be written to */
1045
#define NPY_ITER_WRITEONLY                  0x00040000
1046
/* The operand's data must be in native byte order */
1047
#define NPY_ITER_NBO                        0x00080000
1048
/* The operand's data must be aligned */
1049
#define NPY_ITER_ALIGNED                    0x00100000
1050
/* The operand's data must be contiguous (within the inner loop) */
1051
#define NPY_ITER_CONTIG                     0x00200000
1052
/* The operand may be copied to satisfy requirements */
1053
#define NPY_ITER_COPY                       0x00400000
1054
/* The operand may be copied with WRITEBACKIFCOPY to satisfy requirements */
1055
#define NPY_ITER_UPDATEIFCOPY               0x00800000
1056
/* Allocate the operand if it is NULL */
1057
#define NPY_ITER_ALLOCATE                   0x01000000
1058
/* If an operand is allocated, don't use any subtype */
1059
#define NPY_ITER_NO_SUBTYPE                 0x02000000
1060
/* This is a virtual array slot, operand is NULL but temporary data is there */
1061
#define NPY_ITER_VIRTUAL                    0x04000000
1062
/* Require that the dimension match the iterator dimensions exactly */
1063
#define NPY_ITER_NO_BROADCAST               0x08000000
1064
/* A mask is being used on this array, affects buffer -> array copy */
1065
#define NPY_ITER_WRITEMASKED                0x10000000
1066
/* This array is the mask for all WRITEMASKED operands */
1067
#define NPY_ITER_ARRAYMASK                  0x20000000
1068
/* Assume iterator order data access for COPY_IF_OVERLAP */
1069
#define NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE 0x40000000
1070

1071
#define NPY_ITER_GLOBAL_FLAGS               0x0000ffff
1072
#define NPY_ITER_PER_OP_FLAGS               0xffff0000
1073

1074

1075
/*****************************
1076
 * Basic iterator object
1077
 *****************************/
1078

1079
/* FWD declaration */
1080
typedef struct PyArrayIterObject_tag PyArrayIterObject;
1081

1082
/*
1083
 * type of the function which translates a set of coordinates to a
1084
 * pointer to the data
1085
 */
1086
typedef char* (*npy_iter_get_dataptr_t)(
1087
        PyArrayIterObject* iter, const npy_intp*);
1088

1089
struct PyArrayIterObject_tag {
1090
        PyObject_HEAD
1091
        int               nd_m1;            /* number of dimensions - 1 */
1092
        npy_intp          index, size;
1093
        npy_intp          coordinates[NPY_MAXDIMS];/* N-dimensional loop */
1094
        npy_intp          dims_m1[NPY_MAXDIMS];    /* ao->dimensions - 1 */
1095
        npy_intp          strides[NPY_MAXDIMS];    /* ao->strides or fake */
1096
        npy_intp          backstrides[NPY_MAXDIMS];/* how far to jump back */
1097
        npy_intp          factors[NPY_MAXDIMS];     /* shape factors */
1098
        PyArrayObject     *ao;
1099
        char              *dataptr;        /* pointer to current item*/
1100
        npy_bool          contiguous;
1101

1102
        npy_intp          bounds[NPY_MAXDIMS][2];
1103
        npy_intp          limits[NPY_MAXDIMS][2];
1104
        npy_intp          limits_sizes[NPY_MAXDIMS];
1105
        npy_iter_get_dataptr_t translate;
1106
} ;
1107

1108

1109
/* Iterator API */
1110
#define PyArrayIter_Check(op) PyObject_TypeCheck((op), &PyArrayIter_Type)
1111

1112
#define _PyAIT(it) ((PyArrayIterObject *)(it))
1113
#define PyArray_ITER_RESET(it) do { \
1114
        _PyAIT(it)->index = 0; \
1115
        _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao); \
1116
        memset(_PyAIT(it)->coordinates, 0, \
1117
               (_PyAIT(it)->nd_m1+1)*sizeof(npy_intp)); \
1118
} while (0)
1119

1120
#define _PyArray_ITER_NEXT1(it) do { \
1121
        (it)->dataptr += _PyAIT(it)->strides[0]; \
1122
        (it)->coordinates[0]++; \
1123
} while (0)
1124

1125
#define _PyArray_ITER_NEXT2(it) do { \
1126
        if ((it)->coordinates[1] < (it)->dims_m1[1]) { \
1127
                (it)->coordinates[1]++; \
1128
                (it)->dataptr += (it)->strides[1]; \
1129
        } \
1130
        else { \
1131
                (it)->coordinates[1] = 0; \
1132
                (it)->coordinates[0]++; \
1133
                (it)->dataptr += (it)->strides[0] - \
1134
                        (it)->backstrides[1]; \
1135
        } \
1136
} while (0)
1137

1138
#define PyArray_ITER_NEXT(it) do { \
1139
        _PyAIT(it)->index++; \
1140
        if (_PyAIT(it)->nd_m1 == 0) { \
1141
                _PyArray_ITER_NEXT1(_PyAIT(it)); \
1142
        } \
1143
        else if (_PyAIT(it)->contiguous) \
1144
                _PyAIT(it)->dataptr += PyArray_DESCR(_PyAIT(it)->ao)->elsize; \
1145
        else if (_PyAIT(it)->nd_m1 == 1) { \
1146
                _PyArray_ITER_NEXT2(_PyAIT(it)); \
1147
        } \
1148
        else { \
1149
                int __npy_i; \
1150
                for (__npy_i=_PyAIT(it)->nd_m1; __npy_i >= 0; __npy_i--) { \
1151
                        if (_PyAIT(it)->coordinates[__npy_i] < \
1152
                            _PyAIT(it)->dims_m1[__npy_i]) { \
1153
                                _PyAIT(it)->coordinates[__npy_i]++; \
1154
                                _PyAIT(it)->dataptr += \
1155
                                        _PyAIT(it)->strides[__npy_i]; \
1156
                                break; \
1157
                        } \
1158
                        else { \
1159
                                _PyAIT(it)->coordinates[__npy_i] = 0; \
1160
                                _PyAIT(it)->dataptr -= \
1161
                                        _PyAIT(it)->backstrides[__npy_i]; \
1162
                        } \
1163
                } \
1164
        } \
1165
} while (0)
1166

1167
#define PyArray_ITER_GOTO(it, destination) do { \
1168
        int __npy_i; \
1169
        _PyAIT(it)->index = 0; \
1170
        _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao); \
1171
        for (__npy_i = _PyAIT(it)->nd_m1; __npy_i>=0; __npy_i--) { \
1172
                if (destination[__npy_i] < 0) { \
1173
                        destination[__npy_i] += \
1174
                                _PyAIT(it)->dims_m1[__npy_i]+1; \
1175
                } \
1176
                _PyAIT(it)->dataptr += destination[__npy_i] * \
1177
                        _PyAIT(it)->strides[__npy_i]; \
1178
                _PyAIT(it)->coordinates[__npy_i] = \
1179
                        destination[__npy_i]; \
1180
                _PyAIT(it)->index += destination[__npy_i] * \
1181
                        ( __npy_i==_PyAIT(it)->nd_m1 ? 1 : \
1182
                          _PyAIT(it)->dims_m1[__npy_i+1]+1) ; \
1183
        } \
1184
} while (0)
1185

1186
#define PyArray_ITER_GOTO1D(it, ind) do { \
1187
        int __npy_i; \
1188
        npy_intp __npy_ind = (npy_intp)(ind); \
1189
        if (__npy_ind < 0) __npy_ind += _PyAIT(it)->size; \
1190
        _PyAIT(it)->index = __npy_ind; \
1191
        if (_PyAIT(it)->nd_m1 == 0) { \
1192
                _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao) + \
1193
                        __npy_ind * _PyAIT(it)->strides[0]; \
1194
        } \
1195
        else if (_PyAIT(it)->contiguous) \
1196
                _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao) + \
1197
                        __npy_ind * PyArray_DESCR(_PyAIT(it)->ao)->elsize; \
1198
        else { \
1199
                _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao); \
1200
                for (__npy_i = 0; __npy_i<=_PyAIT(it)->nd_m1; \
1201
                     __npy_i++) { \
1202
                        _PyAIT(it)->dataptr += \
1203
                                (__npy_ind / _PyAIT(it)->factors[__npy_i]) \
1204
                                * _PyAIT(it)->strides[__npy_i]; \
1205
                        __npy_ind %= _PyAIT(it)->factors[__npy_i]; \
1206
                } \
1207
        } \
1208
} while (0)
1209

1210
#define PyArray_ITER_DATA(it) ((void *)(_PyAIT(it)->dataptr))
1211

1212
#define PyArray_ITER_NOTDONE(it) (_PyAIT(it)->index < _PyAIT(it)->size)
1213

1214

1215
/*
1216
 * Any object passed to PyArray_Broadcast must be binary compatible
1217
 * with this structure.
1218
 */
1219

1220
typedef struct {
1221
        PyObject_HEAD
1222
        int                  numiter;                 /* number of iters */
1223
        npy_intp             size;                    /* broadcasted size */
1224
        npy_intp             index;                   /* current index */
1225
        int                  nd;                      /* number of dims */
1226
        npy_intp             dimensions[NPY_MAXDIMS]; /* dimensions */
1227
        PyArrayIterObject    *iters[NPY_MAXARGS];     /* iterators */
1228
} PyArrayMultiIterObject;
1229

1230
#define _PyMIT(m) ((PyArrayMultiIterObject *)(m))
1231
#define PyArray_MultiIter_RESET(multi) do {                                   \
1232
        int __npy_mi;                                                         \
1233
        _PyMIT(multi)->index = 0;                                             \
1234
        for (__npy_mi=0; __npy_mi < _PyMIT(multi)->numiter;  __npy_mi++) {    \
1235
                PyArray_ITER_RESET(_PyMIT(multi)->iters[__npy_mi]);           \
1236
        }                                                                     \
1237
} while (0)
1238

1239
#define PyArray_MultiIter_NEXT(multi) do {                                    \
1240
        int __npy_mi;                                                         \
1241
        _PyMIT(multi)->index++;                                               \
1242
        for (__npy_mi=0; __npy_mi < _PyMIT(multi)->numiter;   __npy_mi++) {   \
1243
                PyArray_ITER_NEXT(_PyMIT(multi)->iters[__npy_mi]);            \
1244
        }                                                                     \
1245
} while (0)
1246

1247
#define PyArray_MultiIter_GOTO(multi, dest) do {                            \
1248
        int __npy_mi;                                                       \
1249
        for (__npy_mi=0; __npy_mi < _PyMIT(multi)->numiter; __npy_mi++) {   \
1250
                PyArray_ITER_GOTO(_PyMIT(multi)->iters[__npy_mi], dest);    \
1251
        }                                                                   \
1252
        _PyMIT(multi)->index = _PyMIT(multi)->iters[0]->index;              \
1253
} while (0)
1254

1255
#define PyArray_MultiIter_GOTO1D(multi, ind) do {                          \
1256
        int __npy_mi;                                                      \
1257
        for (__npy_mi=0; __npy_mi < _PyMIT(multi)->numiter; __npy_mi++) {  \
1258
                PyArray_ITER_GOTO1D(_PyMIT(multi)->iters[__npy_mi], ind);  \
1259
        }                                                                  \
1260
        _PyMIT(multi)->index = _PyMIT(multi)->iters[0]->index;             \
1261
} while (0)
1262

1263
#define PyArray_MultiIter_DATA(multi, i)                \
1264
        ((void *)(_PyMIT(multi)->iters[i]->dataptr))
1265

1266
#define PyArray_MultiIter_NEXTi(multi, i)               \
1267
        PyArray_ITER_NEXT(_PyMIT(multi)->iters[i])
1268

1269
#define PyArray_MultiIter_NOTDONE(multi)                \
1270
        (_PyMIT(multi)->index < _PyMIT(multi)->size)
1271

1272

1273
/*
1274
 * Store the information needed for fancy-indexing over an array. The
1275
 * fields are slightly unordered to keep consec, dataptr and subspace
1276
 * where they were originally.
1277
 */
1278
typedef struct {
1279
        PyObject_HEAD
1280
        /*
1281
         * Multi-iterator portion --- needs to be present in this
1282
         * order to work with PyArray_Broadcast
1283
         */
1284

1285
        int                   numiter;                 /* number of index-array
1286
                                                          iterators */
1287
        npy_intp              size;                    /* size of broadcasted
1288
                                                          result */
1289
        npy_intp              index;                   /* current index */
1290
        int                   nd;                      /* number of dims */
1291
        npy_intp              dimensions[NPY_MAXDIMS]; /* dimensions */
1292
        NpyIter               *outer;                  /* index objects
1293
                                                          iterator */
1294
        void                  *unused[NPY_MAXDIMS - 2];
1295
        PyArrayObject         *array;
1296
        /* Flat iterator for the indexed array. For compatibility solely. */
1297
        PyArrayIterObject     *ait;
1298

1299
        /*
1300
         * Subspace array. For binary compatibility (was an iterator,
1301
         * but only the check for NULL should be used).
1302
         */
1303
        PyArrayObject         *subspace;
1304

1305
        /*
1306
         * if subspace iteration, then this is the array of axes in
1307
         * the underlying array represented by the index objects
1308
         */
1309
        int                   iteraxes[NPY_MAXDIMS];
1310
        npy_intp              fancy_strides[NPY_MAXDIMS];
1311

1312
        /* pointer when all fancy indices are 0 */
1313
        char                  *baseoffset;
1314

1315
        /*
1316
         * after binding consec denotes at which axis the fancy axes
1317
         * are inserted.
1318
         */
1319
        int                   consec;
1320
        char                  *dataptr;
1321

1322
        int                   nd_fancy;
1323
        npy_intp              fancy_dims[NPY_MAXDIMS];
1324

1325
        /* Whether the iterator (any of the iterators) requires API */
1326
        int                   needs_api;
1327

1328
        /*
1329
         * Extra op information.
1330
         */
1331
        PyArrayObject         *extra_op;
1332
        PyArray_Descr         *extra_op_dtype;         /* desired dtype */
1333
        npy_uint32            *extra_op_flags;         /* Iterator flags */
1334

1335
        NpyIter               *extra_op_iter;
1336
        NpyIter_IterNextFunc  *extra_op_next;
1337
        char                  **extra_op_ptrs;
1338

1339
        /*
1340
         * Information about the iteration state.
1341
         */
1342
        NpyIter_IterNextFunc  *outer_next;
1343
        char                  **outer_ptrs;
1344
        npy_intp              *outer_strides;
1345

1346
        /*
1347
         * Information about the subspace iterator.
1348
         */
1349
        NpyIter               *subspace_iter;
1350
        NpyIter_IterNextFunc  *subspace_next;
1351
        char                  **subspace_ptrs;
1352
        npy_intp              *subspace_strides;
1353

1354
        /* Count for the external loop (which ever it is) for API iteration */
1355
        npy_intp              iter_count;
1356

1357
} PyArrayMapIterObject;
1358

1359
enum {
1360
    NPY_NEIGHBORHOOD_ITER_ZERO_PADDING,
1361
    NPY_NEIGHBORHOOD_ITER_ONE_PADDING,
1362
    NPY_NEIGHBORHOOD_ITER_CONSTANT_PADDING,
1363
    NPY_NEIGHBORHOOD_ITER_CIRCULAR_PADDING,
1364
    NPY_NEIGHBORHOOD_ITER_MIRROR_PADDING
1365
};
1366

1367
typedef struct {
1368
    PyObject_HEAD
1369

1370
    /*
1371
     * PyArrayIterObject part: keep this in this exact order
1372
     */
1373
    int               nd_m1;            /* number of dimensions - 1 */
1374
    npy_intp          index, size;
1375
    npy_intp          coordinates[NPY_MAXDIMS];/* N-dimensional loop */
1376
    npy_intp          dims_m1[NPY_MAXDIMS];    /* ao->dimensions - 1 */
1377
    npy_intp          strides[NPY_MAXDIMS];    /* ao->strides or fake */
1378
    npy_intp          backstrides[NPY_MAXDIMS];/* how far to jump back */
1379
    npy_intp          factors[NPY_MAXDIMS];     /* shape factors */
1380
    PyArrayObject     *ao;
1381
    char              *dataptr;        /* pointer to current item*/
1382
    npy_bool          contiguous;
1383

1384
    npy_intp          bounds[NPY_MAXDIMS][2];
1385
    npy_intp          limits[NPY_MAXDIMS][2];
1386
    npy_intp          limits_sizes[NPY_MAXDIMS];
1387
    npy_iter_get_dataptr_t translate;
1388

1389
    /*
1390
     * New members
1391
     */
1392
    npy_intp nd;
1393

1394
    /* Dimensions is the dimension of the array */
1395
    npy_intp dimensions[NPY_MAXDIMS];
1396

1397
    /*
1398
     * Neighborhood points coordinates are computed relatively to the
1399
     * point pointed by _internal_iter
1400
     */
1401
    PyArrayIterObject* _internal_iter;
1402
    /*
1403
     * To keep a reference to the representation of the constant value
1404
     * for constant padding
1405
     */
1406
    char* constant;
1407

1408
    int mode;
1409
} PyArrayNeighborhoodIterObject;
1410

1411
/*
1412
 * Neighborhood iterator API
1413
 */
1414

1415
/* General: those work for any mode */
1416
static NPY_INLINE int
1417
PyArrayNeighborhoodIter_Reset(PyArrayNeighborhoodIterObject* iter);
1418
static NPY_INLINE int
1419
PyArrayNeighborhoodIter_Next(PyArrayNeighborhoodIterObject* iter);
1420
#if 0
1421
static NPY_INLINE int
1422
PyArrayNeighborhoodIter_Next2D(PyArrayNeighborhoodIterObject* iter);
1423
#endif
1424

1425
/*
1426
 * Include inline implementations - functions defined there are not
1427
 * considered public API
1428
 */
1429
#define _NPY_INCLUDE_NEIGHBORHOOD_IMP
1430
#include "_neighborhood_iterator_imp.h"
1431
#undef _NPY_INCLUDE_NEIGHBORHOOD_IMP
1432

1433
/* The default array type */
1434
#define NPY_DEFAULT_TYPE NPY_DOUBLE
1435

1436
/*
1437
 * All sorts of useful ways to look into a PyArrayObject. It is recommended
1438
 * to use PyArrayObject * objects instead of always casting from PyObject *,
1439
 * for improved type checking.
1440
 *
1441
 * In many cases here the macro versions of the accessors are deprecated,
1442
 * but can't be immediately changed to inline functions because the
1443
 * preexisting macros accept PyObject * and do automatic casts. Inline
1444
 * functions accepting PyArrayObject * provides for some compile-time
1445
 * checking of correctness when working with these objects in C.
1446
 */
1447

1448
#define PyArray_ISONESEGMENT(m) (PyArray_CHKFLAGS(m, NPY_ARRAY_C_CONTIGUOUS) || \
1449
                                 PyArray_CHKFLAGS(m, NPY_ARRAY_F_CONTIGUOUS))
1450

1451
#define PyArray_ISFORTRAN(m) (PyArray_CHKFLAGS(m, NPY_ARRAY_F_CONTIGUOUS) && \
1452
                             (!PyArray_CHKFLAGS(m, NPY_ARRAY_C_CONTIGUOUS)))
1453

1454
#define PyArray_FORTRAN_IF(m) ((PyArray_CHKFLAGS(m, NPY_ARRAY_F_CONTIGUOUS) ? \
1455
                               NPY_ARRAY_F_CONTIGUOUS : 0))
1456

1457
#if (defined(NPY_NO_DEPRECATED_API) && (NPY_1_7_API_VERSION <= NPY_NO_DEPRECATED_API))
1458
/*
1459
 * Changing access macros into functions, to allow for future hiding
1460
 * of the internal memory layout. This later hiding will allow the 2.x series
1461
 * to change the internal representation of arrays without affecting
1462
 * ABI compatibility.
1463
 */
1464

1465
static NPY_INLINE int
1466
PyArray_NDIM(const PyArrayObject *arr)
1467
{
1468 1
    return ((PyArrayObject_fields *)arr)->nd;
1469
}
1470

1471
static NPY_INLINE void *
1472
PyArray_DATA(PyArrayObject *arr)
1473
{
1474 1
    return ((PyArrayObject_fields *)arr)->data;
1475
}
1476

1477
static NPY_INLINE char *
1478
PyArray_BYTES(PyArrayObject *arr)
1479
{
1480 1
    return ((PyArrayObject_fields *)arr)->data;
1481
}
1482

1483
static NPY_INLINE npy_intp *
1484
PyArray_DIMS(PyArrayObject *arr)
1485
{
1486 1
    return ((PyArrayObject_fields *)arr)->dimensions;
1487
}
1488

1489
static NPY_INLINE npy_intp *
1490
PyArray_STRIDES(PyArrayObject *arr)
1491
{
1492 1
    return ((PyArrayObject_fields *)arr)->strides;
1493
}
1494

1495
static NPY_INLINE npy_intp
1496
PyArray_DIM(const PyArrayObject *arr, int idim)
1497
{
1498 1
    return ((PyArrayObject_fields *)arr)->dimensions[idim];
1499
}
1500

1501
static NPY_INLINE npy_intp
1502
PyArray_STRIDE(const PyArrayObject *arr, int istride)
1503
{
1504 1
    return ((PyArrayObject_fields *)arr)->strides[istride];
1505
}
1506

1507
static NPY_INLINE NPY_RETURNS_BORROWED_REF PyObject *
1508
PyArray_BASE(PyArrayObject *arr)
1509
{
1510 1
    return ((PyArrayObject_fields *)arr)->base;
1511
}
1512

1513
static NPY_INLINE NPY_RETURNS_BORROWED_REF PyArray_Descr *
1514
PyArray_DESCR(PyArrayObject *arr)
1515
{
1516 1
    return ((PyArrayObject_fields *)arr)->descr;
1517
}
1518

1519
static NPY_INLINE int
1520
PyArray_FLAGS(const PyArrayObject *arr)
1521
{
1522 1
    return ((PyArrayObject_fields *)arr)->flags;
1523
}
1524

1525
static NPY_INLINE npy_intp
1526
PyArray_ITEMSIZE(const PyArrayObject *arr)
1527
{
1528 1
    return ((PyArrayObject_fields *)arr)->descr->elsize;
1529
}
1530

1531
static NPY_INLINE int
1532
PyArray_TYPE(const PyArrayObject *arr)
1533
{
1534 1
    return ((PyArrayObject_fields *)arr)->descr->type_num;
1535
}
1536

1537
static NPY_INLINE int
1538
PyArray_CHKFLAGS(const PyArrayObject *arr, int flags)
1539
{
1540 1
    return (PyArray_FLAGS(arr) & flags) == flags;
1541
}
1542

1543
static NPY_INLINE PyObject *
1544
PyArray_GETITEM(const PyArrayObject *arr, const char *itemptr)
1545
{
1546 1
    return ((PyArrayObject_fields *)arr)->descr->f->getitem(
1547
                                        (void *)itemptr, (PyArrayObject *)arr);
1548
}
1549

1550
/*
1551
 * SETITEM should only be used if it is known that the value is a scalar
1552
 * and of a type understood by the arrays dtype.
1553
 * Use `PyArray_Pack` if the value may be of a different dtype.
1554
 */
1555
static NPY_INLINE int
1556
PyArray_SETITEM(PyArrayObject *arr, char *itemptr, PyObject *v)
1557
{
1558 1
    return ((PyArrayObject_fields *)arr)->descr->f->setitem(v, itemptr, arr);
1559
}
1560

1561
#else
1562

1563
/* These macros are deprecated as of NumPy 1.7. */
1564
#define PyArray_NDIM(obj) (((PyArrayObject_fields *)(obj))->nd)
1565
#define PyArray_BYTES(obj) (((PyArrayObject_fields *)(obj))->data)
1566
#define PyArray_DATA(obj) ((void *)((PyArrayObject_fields *)(obj))->data)
1567
#define PyArray_DIMS(obj) (((PyArrayObject_fields *)(obj))->dimensions)
1568
#define PyArray_STRIDES(obj) (((PyArrayObject_fields *)(obj))->strides)
1569
#define PyArray_DIM(obj,n) (PyArray_DIMS(obj)[n])
1570
#define PyArray_STRIDE(obj,n) (PyArray_STRIDES(obj)[n])
1571
#define PyArray_BASE(obj) (((PyArrayObject_fields *)(obj))->base)
1572
#define PyArray_DESCR(obj) (((PyArrayObject_fields *)(obj))->descr)
1573
#define PyArray_FLAGS(obj) (((PyArrayObject_fields *)(obj))->flags)
1574
#define PyArray_CHKFLAGS(m, FLAGS) \
1575
        ((((PyArrayObject_fields *)(m))->flags & (FLAGS)) == (FLAGS))
1576
#define PyArray_ITEMSIZE(obj) \
1577
                    (((PyArrayObject_fields *)(obj))->descr->elsize)
1578
#define PyArray_TYPE(obj) \
1579
                    (((PyArrayObject_fields *)(obj))->descr->type_num)
1580
#define PyArray_GETITEM(obj,itemptr) \
1581
        PyArray_DESCR(obj)->f->getitem((char *)(itemptr), \
1582
                                     (PyArrayObject *)(obj))
1583

1584
#define PyArray_SETITEM(obj,itemptr,v) \
1585
        PyArray_DESCR(obj)->f->setitem((PyObject *)(v), \
1586
                                     (char *)(itemptr), \
1587
                                     (PyArrayObject *)(obj))
1588
#endif
1589

1590
static NPY_INLINE PyArray_Descr *
1591
PyArray_DTYPE(PyArrayObject *arr)
1592
{
1593 1
    return ((PyArrayObject_fields *)arr)->descr;
1594
}
1595

1596
static NPY_INLINE npy_intp *
1597
PyArray_SHAPE(PyArrayObject *arr)
1598
{
1599 1
    return ((PyArrayObject_fields *)arr)->dimensions;
1600
}
1601

1602
/*
1603
 * Enables the specified array flags. Does no checking,
1604
 * assumes you know what you're doing.
1605
 */
1606
static NPY_INLINE void
1607
PyArray_ENABLEFLAGS(PyArrayObject *arr, int flags)
1608
{
1609 1
    ((PyArrayObject_fields *)arr)->flags |= flags;
1610
}
1611

1612
/*
1613
 * Clears the specified array flags. Does no checking,
1614
 * assumes you know what you're doing.
1615
 */
1616
static NPY_INLINE void
1617
PyArray_CLEARFLAGS(PyArrayObject *arr, int flags)
1618
{
1619 1
    ((PyArrayObject_fields *)arr)->flags &= ~flags;
1620
}
1621

1622
#define PyTypeNum_ISBOOL(type) ((type) == NPY_BOOL)
1623

1624
#define PyTypeNum_ISUNSIGNED(type) (((type) == NPY_UBYTE) ||   \
1625
                                 ((type) == NPY_USHORT) ||     \
1626
                                 ((type) == NPY_UINT) ||       \
1627
                                 ((type) == NPY_ULONG) ||      \
1628
                                 ((type) == NPY_ULONGLONG))
1629

1630
#define PyTypeNum_ISSIGNED(type) (((type) == NPY_BYTE) ||      \
1631
                               ((type) == NPY_SHORT) ||        \
1632
                               ((type) == NPY_INT) ||          \
1633
                               ((type) == NPY_LONG) ||         \
1634
                               ((type) == NPY_LONGLONG))
1635

1636
#define PyTypeNum_ISINTEGER(type) (((type) >= NPY_BYTE) &&     \
1637
                                ((type) <= NPY_ULONGLONG))
1638

1639
#define PyTypeNum_ISFLOAT(type) ((((type) >= NPY_FLOAT) && \
1640
                              ((type) <= NPY_LONGDOUBLE)) || \
1641
                              ((type) == NPY_HALF))
1642

1643
#define PyTypeNum_ISNUMBER(type) (((type) <= NPY_CLONGDOUBLE) || \
1644
                                  ((type) == NPY_HALF))
1645

1646
#define PyTypeNum_ISSTRING(type) (((type) == NPY_STRING) ||    \
1647
                                  ((type) == NPY_UNICODE))
1648

1649
#define PyTypeNum_ISCOMPLEX(type) (((type) >= NPY_CFLOAT) &&   \
1650
                                ((type) <= NPY_CLONGDOUBLE))
1651

1652
#define PyTypeNum_ISPYTHON(type) (((type) == NPY_LONG) ||      \
1653
                                  ((type) == NPY_DOUBLE) ||    \
1654
                                  ((type) == NPY_CDOUBLE) ||   \
1655
                                  ((type) == NPY_BOOL) ||      \
1656
                                  ((type) == NPY_OBJECT ))
1657

1658
#define PyTypeNum_ISFLEXIBLE(type) (((type) >=NPY_STRING) &&  \
1659
                                    ((type) <=NPY_VOID))
1660

1661
#define PyTypeNum_ISDATETIME(type) (((type) >=NPY_DATETIME) &&  \
1662
                                    ((type) <=NPY_TIMEDELTA))
1663

1664
#define PyTypeNum_ISUSERDEF(type) (((type) >= NPY_USERDEF) && \
1665
                                   ((type) < NPY_USERDEF+     \
1666
                                    NPY_NUMUSERTYPES))
1667

1668
#define PyTypeNum_ISEXTENDED(type) (PyTypeNum_ISFLEXIBLE(type) ||  \
1669
                                    PyTypeNum_ISUSERDEF(type))
1670

1671
#define PyTypeNum_ISOBJECT(type) ((type) == NPY_OBJECT)
1672

1673

1674
#define PyDataType_ISBOOL(obj) PyTypeNum_ISBOOL(((PyArray_Descr*)(obj))->type_num)
1675
#define PyDataType_ISUNSIGNED(obj) PyTypeNum_ISUNSIGNED(((PyArray_Descr*)(obj))->type_num)
1676
#define PyDataType_ISSIGNED(obj) PyTypeNum_ISSIGNED(((PyArray_Descr*)(obj))->type_num)
1677
#define PyDataType_ISINTEGER(obj) PyTypeNum_ISINTEGER(((PyArray_Descr*)(obj))->type_num )
1678
#define PyDataType_ISFLOAT(obj) PyTypeNum_ISFLOAT(((PyArray_Descr*)(obj))->type_num)
1679
#define PyDataType_ISNUMBER(obj) PyTypeNum_ISNUMBER(((PyArray_Descr*)(obj))->type_num)
1680
#define PyDataType_ISSTRING(obj) PyTypeNum_ISSTRING(((PyArray_Descr*)(obj))->type_num)
1681
#define PyDataType_ISCOMPLEX(obj) PyTypeNum_ISCOMPLEX(((PyArray_Descr*)(obj))->type_num)
1682
#define PyDataType_ISPYTHON(obj) PyTypeNum_ISPYTHON(((PyArray_Descr*)(obj))->type_num)
1683
#define PyDataType_ISFLEXIBLE(obj) PyTypeNum_ISFLEXIBLE(((PyArray_Descr*)(obj))->type_num)
1684
#define PyDataType_ISDATETIME(obj) PyTypeNum_ISDATETIME(((PyArray_Descr*)(obj))->type_num)
1685
#define PyDataType_ISUSERDEF(obj) PyTypeNum_ISUSERDEF(((PyArray_Descr*)(obj))->type_num)
1686
#define PyDataType_ISEXTENDED(obj) PyTypeNum_ISEXTENDED(((PyArray_Descr*)(obj))->type_num)
1687
#define PyDataType_ISOBJECT(obj) PyTypeNum_ISOBJECT(((PyArray_Descr*)(obj))->type_num)
1688
#define PyDataType_HASFIELDS(obj) (((PyArray_Descr *)(obj))->names != NULL)
1689
#define PyDataType_HASSUBARRAY(dtype) ((dtype)->subarray != NULL)
1690
#define PyDataType_ISUNSIZED(dtype) ((dtype)->elsize == 0 && \
1691
                                      !PyDataType_HASFIELDS(dtype))
1692
#define PyDataType_MAKEUNSIZED(dtype) ((dtype)->elsize = 0)
1693

1694
#define PyArray_ISBOOL(obj) PyTypeNum_ISBOOL(PyArray_TYPE(obj))
1695
#define PyArray_ISUNSIGNED(obj) PyTypeNum_ISUNSIGNED(PyArray_TYPE(obj))
1696
#define PyArray_ISSIGNED(obj) PyTypeNum_ISSIGNED(PyArray_TYPE(obj))
1697
#define PyArray_ISINTEGER(obj) PyTypeNum_ISINTEGER(PyArray_TYPE(obj))
1698
#define PyArray_ISFLOAT(obj) PyTypeNum_ISFLOAT(PyArray_TYPE(obj))
1699
#define PyArray_ISNUMBER(obj) PyTypeNum_ISNUMBER(PyArray_TYPE(obj))
1700
#define PyArray_ISSTRING(obj) PyTypeNum_ISSTRING(PyArray_TYPE(obj))
1701
#define PyArray_ISCOMPLEX(obj) PyTypeNum_ISCOMPLEX(PyArray_TYPE(obj))
1702
#define PyArray_ISPYTHON(obj) PyTypeNum_ISPYTHON(PyArray_TYPE(obj))
1703
#define PyArray_ISFLEXIBLE(obj) PyTypeNum_ISFLEXIBLE(PyArray_TYPE(obj))
1704
#define PyArray_ISDATETIME(obj) PyTypeNum_ISDATETIME(PyArray_TYPE(obj))
1705
#define PyArray_ISUSERDEF(obj) PyTypeNum_ISUSERDEF(PyArray_TYPE(obj))
1706
#define PyArray_ISEXTENDED(obj) PyTypeNum_ISEXTENDED(PyArray_TYPE(obj))
1707
#define PyArray_ISOBJECT(obj) PyTypeNum_ISOBJECT(PyArray_TYPE(obj))
1708
#define PyArray_HASFIELDS(obj) PyDataType_HASFIELDS(PyArray_DESCR(obj))
1709

1710
    /*
1711
     * FIXME: This should check for a flag on the data-type that
1712
     * states whether or not it is variable length.  Because the
1713
     * ISFLEXIBLE check is hard-coded to the built-in data-types.
1714
     */
1715
#define PyArray_ISVARIABLE(obj) PyTypeNum_ISFLEXIBLE(PyArray_TYPE(obj))
1716

1717
#define PyArray_SAFEALIGNEDCOPY(obj) (PyArray_ISALIGNED(obj) && !PyArray_ISVARIABLE(obj))
1718

1719

1720
#define NPY_LITTLE '<'
1721
#define NPY_BIG '>'
1722
#define NPY_NATIVE '='
1723
#define NPY_SWAP 's'
1724
#define NPY_IGNORE '|'
1725

1726
#if NPY_BYTE_ORDER == NPY_BIG_ENDIAN
1727
#define NPY_NATBYTE NPY_BIG
1728
#define NPY_OPPBYTE NPY_LITTLE
1729
#else
1730
#define NPY_NATBYTE NPY_LITTLE
1731
#define NPY_OPPBYTE NPY_BIG
1732
#endif
1733

1734
#define PyArray_ISNBO(arg) ((arg) != NPY_OPPBYTE)
1735
#define PyArray_IsNativeByteOrder PyArray_ISNBO
1736
#define PyArray_ISNOTSWAPPED(m) PyArray_ISNBO(PyArray_DESCR(m)->byteorder)
1737
#define PyArray_ISBYTESWAPPED(m) (!PyArray_ISNOTSWAPPED(m))
1738

1739
#define PyArray_FLAGSWAP(m, flags) (PyArray_CHKFLAGS(m, flags) &&       \
1740
                                    PyArray_ISNOTSWAPPED(m))
1741

1742
#define PyArray_ISCARRAY(m) PyArray_FLAGSWAP(m, NPY_ARRAY_CARRAY)
1743
#define PyArray_ISCARRAY_RO(m) PyArray_FLAGSWAP(m, NPY_ARRAY_CARRAY_RO)
1744
#define PyArray_ISFARRAY(m) PyArray_FLAGSWAP(m, NPY_ARRAY_FARRAY)
1745
#define PyArray_ISFARRAY_RO(m) PyArray_FLAGSWAP(m, NPY_ARRAY_FARRAY_RO)
1746
#define PyArray_ISBEHAVED(m) PyArray_FLAGSWAP(m, NPY_ARRAY_BEHAVED)
1747
#define PyArray_ISBEHAVED_RO(m) PyArray_FLAGSWAP(m, NPY_ARRAY_ALIGNED)
1748

1749

1750
#define PyDataType_ISNOTSWAPPED(d) PyArray_ISNBO(((PyArray_Descr *)(d))->byteorder)
1751
#define PyDataType_ISBYTESWAPPED(d) (!PyDataType_ISNOTSWAPPED(d))
1752

1753
/************************************************************
1754
 * A struct used by PyArray_CreateSortedStridePerm, new in 1.7.
1755
 ************************************************************/
1756

1757
typedef struct {
1758
    npy_intp perm, stride;
1759
} npy_stride_sort_item;
1760

1761
/************************************************************
1762
 * This is the form of the struct that's stored in the
1763
 * PyCapsule returned by an array's __array_struct__ attribute. See
1764
 * https://docs.scipy.org/doc/numpy/reference/arrays.interface.html for the full
1765
 * documentation.
1766
 ************************************************************/
1767
typedef struct {
1768
    int two;              /*
1769
                           * contains the integer 2 as a sanity
1770
                           * check
1771
                           */
1772

1773
    int nd;               /* number of dimensions */
1774

1775
    char typekind;        /*
1776
                           * kind in array --- character code of
1777
                           * typestr
1778
                           */
1779

1780
    int itemsize;         /* size of each element */
1781

1782
    int flags;            /*
1783
                           * how should be data interpreted. Valid
1784
                           * flags are CONTIGUOUS (1), F_CONTIGUOUS (2),
1785
                           * ALIGNED (0x100), NOTSWAPPED (0x200), and
1786
                           * WRITEABLE (0x400).  ARR_HAS_DESCR (0x800)
1787
                           * states that arrdescr field is present in
1788
                           * structure
1789
                           */
1790

1791
    npy_intp *shape;       /*
1792
                            * A length-nd array of shape
1793
                            * information
1794
                            */
1795

1796
    npy_intp *strides;    /* A length-nd array of stride information */
1797

1798
    void *data;           /* A pointer to the first element of the array */
1799

1800
    PyObject *descr;      /*
1801
                           * A list of fields or NULL (ignored if flags
1802
                           * does not have ARR_HAS_DESCR flag set)
1803
                           */
1804
} PyArrayInterface;
1805

1806
/*
1807
 * This is a function for hooking into the PyDataMem_NEW/FREE/RENEW functions.
1808
 * See the documentation for PyDataMem_SetEventHook.
1809
 */
1810
typedef void (PyDataMem_EventHookFunc)(void *inp, void *outp, size_t size,
1811
                                       void *user_data);
1812

1813

1814
/*
1815
 * PyArray_DTypeMeta related definitions.
1816
 *
1817
 * As of now, this API is preliminary and will be extended as necessary.
1818
 */
1819
#if defined(NPY_INTERNAL_BUILD) && NPY_INTERNAL_BUILD
1820
    /*
1821
     * The Structures defined in this block are considered private API and
1822
     * may change without warning!
1823
     */
1824
    /* TODO: Make this definition public in the API, as soon as its settled */
1825
    NPY_NO_EXPORT extern PyTypeObject PyArrayDTypeMeta_Type;
1826

1827
    typedef struct PyArray_DTypeMeta_tag PyArray_DTypeMeta;
1828

1829
    typedef PyArray_Descr *(discover_descr_from_pyobject_function)(
1830
            PyArray_DTypeMeta *cls, PyObject *obj);
1831

1832
    /*
1833
     * Before making this public, we should decide whether it should pass
1834
     * the type, or allow looking at the object. A possible use-case:
1835
     * `np.array(np.array([0]), dtype=np.ndarray)`
1836
     * Could consider arrays that are not `dtype=ndarray` "scalars".
1837
     */
1838
    typedef int (is_known_scalar_type_function)(
1839
            PyArray_DTypeMeta *cls, PyTypeObject *obj);
1840

1841
    typedef PyArray_Descr *(default_descr_function)(PyArray_DTypeMeta *cls);
1842

1843
    /*
1844
     * While NumPy DTypes would not need to be heap types the plan is to
1845
     * make DTypes available in Python at which point they will be heap types.
1846
     * Since we also wish to add fields to the DType class, this looks like
1847
     * a typical instance definition, but with PyHeapTypeObject instead of
1848
     * only the PyObject_HEAD.
1849
     * This must only be exposed very extremely careful consideration, since
1850
     * it is a fairly complex construct which may be better to allow
1851
     * refactoring of.
1852
     */
1853
    struct PyArray_DTypeMeta_tag {
1854
        PyHeapTypeObject super;
1855

1856
        /*
1857
         * Most DTypes will have a singleton default instance, for the
1858
         * parametric legacy DTypes (bytes, string, void, datetime) this
1859
         * may be a pointer to the *prototype* instance?
1860
         */
1861
        PyArray_Descr *singleton;
1862
        /*
1863
         * Is this DType created using the old API? This exists mainly to
1864
         * allow for assertions in paths specific to wrapping legacy types.
1865
         */
1866
        npy_bool legacy;
1867
        /* The values stored by a parametric datatype depend on its instance */
1868
        npy_bool parametric;
1869
        /* whether the DType can be instantiated (i.e. np.dtype cannot) */
1870
        npy_bool abstract;
1871

1872
        /*
1873
         * The following fields replicate the most important dtype information.
1874
         * In the legacy implementation most of these are stored in the
1875
         * PyArray_Descr struct.
1876
         */
1877
        /* The type object of the scalar instances (may be NULL?) */
1878
        PyTypeObject *scalar_type;
1879
        /* kind for this type */
1880
        char kind;
1881
        /* unique-character representing this type */
1882
        char type;
1883
        /* flags describing data type */
1884
        char flags;
1885
        /* number representing this type */
1886
        int type_num;
1887
        /*
1888
         * Point to the original ArrFuncs.
1889
         * NOTE: We could make a copy to detect changes to `f`.
1890
         */
1891
        PyArray_ArrFuncs *f;
1892

1893
        /* DType methods, these could be moved into its own struct */
1894
        discover_descr_from_pyobject_function *discover_descr_from_pyobject;
1895
        is_known_scalar_type_function *is_known_scalar_type;
1896
        default_descr_function *default_descr;
1897
    };
1898

1899
#endif  /* NPY_INTERNAL_BUILD */
1900

1901

1902
/*
1903
 * Use the keyword NPY_DEPRECATED_INCLUDES to ensure that the header files
1904
 * npy_*_*_deprecated_api.h are only included from here and nowhere else.
1905
 */
1906
#ifdef NPY_DEPRECATED_INCLUDES
1907
#error "Do not use the reserved keyword NPY_DEPRECATED_INCLUDES."
1908
#endif
1909
#define NPY_DEPRECATED_INCLUDES
1910
#if !defined(NPY_NO_DEPRECATED_API) || \
1911
    (NPY_NO_DEPRECATED_API < NPY_1_7_API_VERSION)
1912
#include "npy_1_7_deprecated_api.h"
1913
#endif
1914
/*
1915
 * There is no file npy_1_8_deprecated_api.h since there are no additional
1916
 * deprecated API features in NumPy 1.8.
1917
 *
1918
 * Note to maintainers: insert code like the following in future NumPy
1919
 * versions.
1920
 *
1921
 * #if !defined(NPY_NO_DEPRECATED_API) || \
1922
 *     (NPY_NO_DEPRECATED_API < NPY_1_9_API_VERSION)
1923
 * #include "npy_1_9_deprecated_api.h"
1924
 * #endif
1925
 */
1926
#undef NPY_DEPRECATED_INCLUDES
1927

1928
#endif /* NPY_ARRAYTYPES_H */

Read our documentation on viewing source code .

Loading