forked from numpy/numpy
/
iterators.c
1793 lines (1605 loc) · 48.3 KB
/
iterators.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "structmember.h"
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#define _MULTIARRAYMODULE
#include "numpy/arrayobject.h"
#include "numpy/arrayscalars.h"
#include "npy_config.h"
#include "npy_pycompat.h"
#include "arrayobject.h"
#include "iterators.h"
#include "ctors.h"
#include "common.h"
#include "conversion_utils.h"
#include "array_coercion.h"
#define NEWAXIS_INDEX -1
#define ELLIPSIS_INDEX -2
#define SINGLE_INDEX -3
/*
* Tries to convert 'o' into an npy_intp interpreted as an
* index. Returns 1 if it was successful, 0 otherwise. Does
* not set an exception.
*/
static int
coerce_index(PyObject *o, npy_intp *v)
{
*v = PyArray_PyIntAsIntp(o);
if ((*v) == -1 && PyErr_Occurred()) {
PyErr_Clear();
return 0;
}
return 1;
}
/*
* This function converts one element of the indexing tuple
* into a step size and a number of steps, returning the
* starting index. Non-slices are signalled in 'n_steps',
* as NEWAXIS_INDEX, ELLIPSIS_INDEX, or SINGLE_INDEX.
*/
NPY_NO_EXPORT npy_intp
parse_index_entry(PyObject *op, npy_intp *step_size,
npy_intp *n_steps, npy_intp max,
int axis, int check_index)
{
npy_intp i;
if (op == Py_None) {
*n_steps = NEWAXIS_INDEX;
i = 0;
}
else if (op == Py_Ellipsis) {
*n_steps = ELLIPSIS_INDEX;
i = 0;
}
else if (PySlice_Check(op)) {
npy_intp stop;
if (PySlice_GetIndicesEx(op, max, &i, &stop, step_size, n_steps) < 0) {
goto fail;
}
if (*n_steps <= 0) {
*n_steps = 0;
*step_size = 1;
i = 0;
}
}
else if (coerce_index(op, &i)) {
*n_steps = SINGLE_INDEX;
*step_size = 0;
if (check_index) {
if (check_and_adjust_index(&i, max, axis, NULL) < 0) {
goto fail;
}
}
}
else {
PyErr_SetString(PyExc_IndexError,
"each index entry must be either a "
"slice, an integer, Ellipsis, or "
"newaxis");
goto fail;
}
return i;
fail:
return -1;
}
/*********************** Element-wise Array Iterator ***********************/
/* Aided by Peter J. Verveer's nd_image package and numpy's arraymap ****/
/* and Python's array iterator ***/
/* get the dataptr from its current coordinates for simple iterator */
static char*
get_ptr_simple(PyArrayIterObject* iter, const npy_intp *coordinates)
{
npy_intp i;
char *ret;
ret = PyArray_DATA(iter->ao);
for(i = 0; i < PyArray_NDIM(iter->ao); ++i) {
ret += coordinates[i] * iter->strides[i];
}
return ret;
}
/*
* This is common initialization code between PyArrayIterObject and
* PyArrayNeighborhoodIterObject
*
* Steals a reference to the array object which gets removed at deallocation,
* if the iterator is allocated statically and its dealloc not called, it
* can be thought of as borrowing the reference.
*/
NPY_NO_EXPORT void
PyArray_RawIterBaseInit(PyArrayIterObject *it, PyArrayObject *ao)
{
int nd, i;
nd = PyArray_NDIM(ao);
PyArray_UpdateFlags(ao, NPY_ARRAY_C_CONTIGUOUS);
if (PyArray_ISCONTIGUOUS(ao)) {
it->contiguous = 1;
}
else {
it->contiguous = 0;
}
it->ao = ao;
it->size = PyArray_SIZE(ao);
it->nd_m1 = nd - 1;
if (nd != 0) {
it->factors[nd-1] = 1;
}
for (i = 0; i < nd; i++) {
it->dims_m1[i] = PyArray_DIMS(ao)[i] - 1;
it->strides[i] = PyArray_STRIDES(ao)[i];
it->backstrides[i] = it->strides[i] * it->dims_m1[i];
if (i > 0) {
it->factors[nd-i-1] = it->factors[nd-i] * PyArray_DIMS(ao)[nd-i];
}
it->bounds[i][0] = 0;
it->bounds[i][1] = PyArray_DIMS(ao)[i] - 1;
it->limits[i][0] = 0;
it->limits[i][1] = PyArray_DIMS(ao)[i] - 1;
it->limits_sizes[i] = it->limits[i][1] - it->limits[i][0] + 1;
}
it->translate = &get_ptr_simple;
PyArray_ITER_RESET(it);
return;
}
static void
array_iter_base_dealloc(PyArrayIterObject *it)
{
Py_XDECREF(it->ao);
}
/*NUMPY_API
* Get Iterator.
*/
NPY_NO_EXPORT PyObject *
PyArray_IterNew(PyObject *obj)
{
/*
* Note that internally PyArray_RawIterBaseInit may be called directly on a
* statically allocated PyArrayIterObject.
*/
PyArrayIterObject *it;
PyArrayObject *ao;
if (!PyArray_Check(obj)) {
PyErr_BadInternalCall();
return NULL;
}
ao = (PyArrayObject *)obj;
it = (PyArrayIterObject *)PyArray_malloc(sizeof(PyArrayIterObject));
PyObject_Init((PyObject *)it, &PyArrayIter_Type);
/* it = PyObject_New(PyArrayIterObject, &PyArrayIter_Type);*/
if (it == NULL) {
return NULL;
}
Py_INCREF(ao); /* PyArray_RawIterBaseInit steals a reference */
PyArray_RawIterBaseInit(it, ao);
return (PyObject *)it;
}
/*NUMPY_API
* Get Iterator broadcast to a particular shape
*/
NPY_NO_EXPORT PyObject *
PyArray_BroadcastToShape(PyObject *obj, npy_intp *dims, int nd)
{
PyArrayIterObject *it;
int i, diff, j, compat, k;
PyArrayObject *ao = (PyArrayObject *)obj;
if (PyArray_NDIM(ao) > nd) {
goto err;
}
compat = 1;
diff = j = nd - PyArray_NDIM(ao);
for (i = 0; i < PyArray_NDIM(ao); i++, j++) {
if (PyArray_DIMS(ao)[i] == 1) {
continue;
}
if (PyArray_DIMS(ao)[i] != dims[j]) {
compat = 0;
break;
}
}
if (!compat) {
goto err;
}
it = (PyArrayIterObject *)PyArray_malloc(sizeof(PyArrayIterObject));
if (it == NULL) {
return NULL;
}
PyObject_Init((PyObject *)it, &PyArrayIter_Type);
PyArray_UpdateFlags(ao, NPY_ARRAY_C_CONTIGUOUS);
if (PyArray_ISCONTIGUOUS(ao)) {
it->contiguous = 1;
}
else {
it->contiguous = 0;
}
Py_INCREF(ao);
it->ao = ao;
it->size = PyArray_MultiplyList(dims, nd);
it->nd_m1 = nd - 1;
if (nd != 0) {
it->factors[nd-1] = 1;
}
for (i = 0; i < nd; i++) {
it->dims_m1[i] = dims[i] - 1;
k = i - diff;
if ((k < 0) || PyArray_DIMS(ao)[k] != dims[i]) {
it->contiguous = 0;
it->strides[i] = 0;
}
else {
it->strides[i] = PyArray_STRIDES(ao)[k];
}
it->backstrides[i] = it->strides[i] * it->dims_m1[i];
if (i > 0) {
it->factors[nd-i-1] = it->factors[nd-i] * dims[nd-i];
}
}
PyArray_ITER_RESET(it);
return (PyObject *)it;
err:
PyErr_SetString(PyExc_ValueError, "array is not broadcastable to "\
"correct shape");
return NULL;
}
/*NUMPY_API
* Get Iterator that iterates over all but one axis (don't use this with
* PyArray_ITER_GOTO1D). The axis will be over-written if negative
* with the axis having the smallest stride.
*/
NPY_NO_EXPORT PyObject *
PyArray_IterAllButAxis(PyObject *obj, int *inaxis)
{
PyArrayObject *arr;
PyArrayIterObject *it;
int axis;
if (!PyArray_Check(obj)) {
PyErr_SetString(PyExc_ValueError,
"Numpy IterAllButAxis requires an ndarray");
return NULL;
}
arr = (PyArrayObject *)obj;
it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)arr);
if (it == NULL) {
return NULL;
}
if (PyArray_NDIM(arr)==0) {
return (PyObject *)it;
}
if (*inaxis < 0) {
int i, minaxis = 0;
npy_intp minstride = 0;
i = 0;
while (minstride == 0 && i < PyArray_NDIM(arr)) {
minstride = PyArray_STRIDE(arr,i);
i++;
}
for (i = 1; i < PyArray_NDIM(arr); i++) {
if (PyArray_STRIDE(arr,i) > 0 &&
PyArray_STRIDE(arr, i) < minstride) {
minaxis = i;
minstride = PyArray_STRIDE(arr,i);
}
}
*inaxis = minaxis;
}
axis = *inaxis;
/* adjust so that will not iterate over axis */
it->contiguous = 0;
if (it->size != 0) {
it->size /= PyArray_DIM(arr,axis);
}
it->dims_m1[axis] = 0;
it->backstrides[axis] = 0;
/*
* (won't fix factors so don't use
* PyArray_ITER_GOTO1D with this iterator)
*/
return (PyObject *)it;
}
/*NUMPY_API
* Adjusts previously broadcasted iterators so that the axis with
* the smallest sum of iterator strides is not iterated over.
* Returns dimension which is smallest in the range [0,multi->nd).
* A -1 is returned if multi->nd == 0.
*
* don't use with PyArray_ITER_GOTO1D because factors are not adjusted
*/
NPY_NO_EXPORT int
PyArray_RemoveSmallest(PyArrayMultiIterObject *multi)
{
PyArrayIterObject *it;
int i, j;
int axis;
npy_intp smallest;
npy_intp sumstrides[NPY_MAXDIMS];
if (multi->nd == 0) {
return -1;
}
for (i = 0; i < multi->nd; i++) {
sumstrides[i] = 0;
for (j = 0; j < multi->numiter; j++) {
sumstrides[i] += multi->iters[j]->strides[i];
}
}
axis = 0;
smallest = sumstrides[0];
/* Find longest dimension */
for (i = 1; i < multi->nd; i++) {
if (sumstrides[i] < smallest) {
axis = i;
smallest = sumstrides[i];
}
}
for(i = 0; i < multi->numiter; i++) {
it = multi->iters[i];
it->contiguous = 0;
if (it->size != 0) {
it->size /= (it->dims_m1[axis]+1);
}
it->dims_m1[axis] = 0;
it->backstrides[axis] = 0;
}
multi->size = multi->iters[0]->size;
return axis;
}
/* Returns an array scalar holding the element desired */
static PyObject *
arrayiter_next(PyArrayIterObject *it)
{
PyObject *ret;
if (it->index < it->size) {
ret = PyArray_ToScalar(it->dataptr, it->ao);
PyArray_ITER_NEXT(it);
return ret;
}
return NULL;
}
static void
arrayiter_dealloc(PyArrayIterObject *it)
{
/*
* Note that it is possible to statically allocate a PyArrayIterObject,
* which does not call this function.
*/
array_iter_base_dealloc(it);
PyArray_free(it);
}
static Py_ssize_t
iter_length(PyArrayIterObject *self)
{
return self->size;
}
static PyArrayObject *
iter_subscript_Bool(PyArrayIterObject *self, PyArrayObject *ind)
{
npy_intp counter, strides;
int itemsize;
npy_intp count = 0;
char *dptr, *optr;
PyArrayObject *ret;
int swap;
PyArray_CopySwapFunc *copyswap;
if (PyArray_NDIM(ind) != 1) {
PyErr_SetString(PyExc_ValueError,
"boolean index array should have 1 dimension");
return NULL;
}
counter = PyArray_DIMS(ind)[0];
if (counter > self->size) {
PyErr_SetString(PyExc_ValueError,
"too many boolean indices");
return NULL;
}
strides = PyArray_STRIDES(ind)[0];
dptr = PyArray_DATA(ind);
/* Get size of return array */
while (counter--) {
if (*((npy_bool *)dptr) != 0) {
count++;
}
dptr += strides;
}
itemsize = PyArray_DESCR(self->ao)->elsize;
Py_INCREF(PyArray_DESCR(self->ao));
ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self->ao),
PyArray_DESCR(self->ao), 1, &count,
NULL, NULL,
0, (PyObject *)self->ao);
if (ret == NULL) {
return NULL;
}
/* Set up loop */
optr = PyArray_DATA(ret);
counter = PyArray_DIMS(ind)[0];
dptr = PyArray_DATA(ind);
copyswap = PyArray_DESCR(self->ao)->f->copyswap;
/* Loop over Boolean array */
swap = (PyArray_ISNOTSWAPPED(self->ao) != PyArray_ISNOTSWAPPED(ret));
while (counter--) {
if (*((npy_bool *)dptr) != 0) {
copyswap(optr, self->dataptr, swap, self->ao);
optr += itemsize;
}
dptr += strides;
PyArray_ITER_NEXT(self);
}
PyArray_ITER_RESET(self);
return ret;
}
static PyObject *
iter_subscript_int(PyArrayIterObject *self, PyArrayObject *ind)
{
npy_intp num;
PyArrayObject *ret;
PyArrayIterObject *ind_it;
int itemsize;
int swap;
char *optr;
npy_intp counter;
PyArray_CopySwapFunc *copyswap;
itemsize = PyArray_DESCR(self->ao)->elsize;
if (PyArray_NDIM(ind) == 0) {
num = *((npy_intp *)PyArray_DATA(ind));
if (check_and_adjust_index(&num, self->size, -1, NULL) < 0) {
PyArray_ITER_RESET(self);
return NULL;
}
else {
PyObject *tmp;
PyArray_ITER_GOTO1D(self, num);
tmp = PyArray_ToScalar(self->dataptr, self->ao);
PyArray_ITER_RESET(self);
return tmp;
}
}
Py_INCREF(PyArray_DESCR(self->ao));
ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self->ao),
PyArray_DESCR(self->ao),
PyArray_NDIM(ind),
PyArray_DIMS(ind),
NULL, NULL,
0, (PyObject *)self->ao);
if (ret == NULL) {
return NULL;
}
optr = PyArray_DATA(ret);
ind_it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)ind);
if (ind_it == NULL) {
Py_DECREF(ret);
return NULL;
}
counter = ind_it->size;
copyswap = PyArray_DESCR(ret)->f->copyswap;
swap = (PyArray_ISNOTSWAPPED(ret) != PyArray_ISNOTSWAPPED(self->ao));
while (counter--) {
num = *((npy_intp *)(ind_it->dataptr));
if (check_and_adjust_index(&num, self->size, -1, NULL) < 0) {
Py_DECREF(ind_it);
Py_DECREF(ret);
PyArray_ITER_RESET(self);
return NULL;
}
PyArray_ITER_GOTO1D(self, num);
copyswap(optr, self->dataptr, swap, ret);
optr += itemsize;
PyArray_ITER_NEXT(ind_it);
}
Py_DECREF(ind_it);
PyArray_ITER_RESET(self);
return (PyObject *)ret;
}
/* Always returns arrays */
NPY_NO_EXPORT PyObject *
iter_subscript(PyArrayIterObject *self, PyObject *ind)
{
PyArray_Descr *indtype = NULL;
PyArray_Descr *dtype;
npy_intp start, step_size;
npy_intp n_steps;
PyArrayObject *ret;
char *dptr;
int size;
PyObject *obj = NULL;
PyObject *new;
PyArray_CopySwapFunc *copyswap;
if (ind == Py_Ellipsis) {
ind = PySlice_New(NULL, NULL, NULL);
obj = iter_subscript(self, ind);
Py_DECREF(ind);
return obj;
}
if (PyTuple_Check(ind)) {
int len;
len = PyTuple_GET_SIZE(ind);
if (len > 1) {
goto fail;
}
if (len == 0) {
Py_INCREF(self->ao);
return (PyObject *)self->ao;
}
ind = PyTuple_GET_ITEM(ind, 0);
}
/*
* Tuples >1d not accepted --- i.e. no newaxis
* Could implement this with adjusted strides and dimensions in iterator
* Check for Boolean -- this is first because Bool is a subclass of Int
*/
PyArray_ITER_RESET(self);
if (PyBool_Check(ind)) {
if (PyObject_IsTrue(ind)) {
return PyArray_ToScalar(self->dataptr, self->ao);
}
else { /* empty array */
npy_intp ii = 0;
dtype = PyArray_DESCR(self->ao);
Py_INCREF(dtype);
ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self->ao),
dtype,
1, &ii,
NULL, NULL, 0,
(PyObject *)self->ao);
return (PyObject *)ret;
}
}
/* Check for Integer or Slice */
if (PyLong_Check(ind) || PySlice_Check(ind)) {
start = parse_index_entry(ind, &step_size, &n_steps,
self->size, 0, 1);
if (start == -1) {
goto fail;
}
if (n_steps == ELLIPSIS_INDEX || n_steps == NEWAXIS_INDEX) {
PyErr_SetString(PyExc_IndexError,
"cannot use Ellipsis or newaxes here");
goto fail;
}
PyArray_ITER_GOTO1D(self, start);
if (n_steps == SINGLE_INDEX) { /* Integer */
PyObject *tmp;
tmp = PyArray_ToScalar(self->dataptr, self->ao);
PyArray_ITER_RESET(self);
return tmp;
}
size = PyArray_DESCR(self->ao)->elsize;
dtype = PyArray_DESCR(self->ao);
Py_INCREF(dtype);
ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self->ao),
dtype,
1, &n_steps,
NULL, NULL,
0, (PyObject *)self->ao);
if (ret == NULL) {
goto fail;
}
dptr = PyArray_DATA(ret);
copyswap = PyArray_DESCR(ret)->f->copyswap;
while (n_steps--) {
copyswap(dptr, self->dataptr, 0, ret);
start += step_size;
PyArray_ITER_GOTO1D(self, start);
dptr += size;
}
PyArray_ITER_RESET(self);
return (PyObject *)ret;
}
/* convert to INTP array if Integer array scalar or List */
indtype = PyArray_DescrFromType(NPY_INTP);
if (PyArray_IsScalar(ind, Integer) || PyList_Check(ind)) {
Py_INCREF(indtype);
obj = PyArray_FromAny(ind, indtype, 0, 0, NPY_ARRAY_FORCECAST, NULL);
if (obj == NULL) {
goto fail;
}
}
else {
Py_INCREF(ind);
obj = ind;
}
/* Any remaining valid input is an array or has been turned into one */
if (!PyArray_Check(obj)) {
goto fail;
}
/* Check for Boolean array */
if (PyArray_TYPE((PyArrayObject *)obj) == NPY_BOOL) {
ret = iter_subscript_Bool(self, (PyArrayObject *)obj);
Py_DECREF(indtype);
Py_DECREF(obj);
return (PyObject *)ret;
}
/* Only integer arrays left */
if (!PyArray_ISINTEGER((PyArrayObject *)obj)) {
goto fail;
}
Py_INCREF(indtype);
new = PyArray_FromAny(obj, indtype, 0, 0,
NPY_ARRAY_FORCECAST | NPY_ARRAY_ALIGNED, NULL);
if (new == NULL) {
goto fail;
}
Py_DECREF(indtype);
Py_DECREF(obj);
ret = (PyArrayObject *)iter_subscript_int(self, (PyArrayObject *)new);
Py_DECREF(new);
return (PyObject *)ret;
fail:
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_IndexError, "unsupported iterator index");
}
Py_XDECREF(indtype);
Py_XDECREF(obj);
return NULL;
}
static int
iter_ass_sub_Bool(PyArrayIterObject *self, PyArrayObject *ind,
PyArrayIterObject *val, int swap)
{
npy_intp counter, strides;
char *dptr;
PyArray_CopySwapFunc *copyswap;
if (PyArray_NDIM(ind) != 1) {
PyErr_SetString(PyExc_ValueError,
"boolean index array should have 1 dimension");
return -1;
}
counter = PyArray_DIMS(ind)[0];
if (counter > self->size) {
PyErr_SetString(PyExc_ValueError,
"boolean index array has too many values");
return -1;
}
strides = PyArray_STRIDES(ind)[0];
dptr = PyArray_DATA(ind);
PyArray_ITER_RESET(self);
/* Loop over Boolean array */
copyswap = PyArray_DESCR(self->ao)->f->copyswap;
while (counter--) {
if (*((npy_bool *)dptr) != 0) {
copyswap(self->dataptr, val->dataptr, swap, self->ao);
PyArray_ITER_NEXT(val);
if (val->index == val->size) {
PyArray_ITER_RESET(val);
}
}
dptr += strides;
PyArray_ITER_NEXT(self);
}
PyArray_ITER_RESET(self);
return 0;
}
static int
iter_ass_sub_int(PyArrayIterObject *self, PyArrayObject *ind,
PyArrayIterObject *val, int swap)
{
npy_intp num;
PyArrayIterObject *ind_it;
npy_intp counter;
PyArray_CopySwapFunc *copyswap;
copyswap = PyArray_DESCR(self->ao)->f->copyswap;
if (PyArray_NDIM(ind) == 0) {
num = *((npy_intp *)PyArray_DATA(ind));
if (check_and_adjust_index(&num, self->size, -1, NULL) < 0) {
return -1;
}
PyArray_ITER_GOTO1D(self, num);
copyswap(self->dataptr, val->dataptr, swap, self->ao);
return 0;
}
ind_it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)ind);
if (ind_it == NULL) {
return -1;
}
counter = ind_it->size;
while (counter--) {
num = *((npy_intp *)(ind_it->dataptr));
if (check_and_adjust_index(&num, self->size, -1, NULL) < 0) {
Py_DECREF(ind_it);
return -1;
}
PyArray_ITER_GOTO1D(self, num);
copyswap(self->dataptr, val->dataptr, swap, self->ao);
PyArray_ITER_NEXT(ind_it);
PyArray_ITER_NEXT(val);
if (val->index == val->size) {
PyArray_ITER_RESET(val);
}
}
Py_DECREF(ind_it);
return 0;
}
NPY_NO_EXPORT int
iter_ass_subscript(PyArrayIterObject *self, PyObject *ind, PyObject *val)
{
PyArrayObject *arrval = NULL;
PyArrayIterObject *val_it = NULL;
PyArray_Descr *type;
PyArray_Descr *indtype = NULL;
int swap, retval = -1;
npy_intp start, step_size;
npy_intp n_steps;
PyObject *obj = NULL;
PyArray_CopySwapFunc *copyswap;
if (val == NULL) {
PyErr_SetString(PyExc_TypeError,
"Cannot delete iterator elements");
return -1;
}
if (PyArray_FailUnlessWriteable(self->ao, "underlying array") < 0)
return -1;
if (ind == Py_Ellipsis) {
ind = PySlice_New(NULL, NULL, NULL);
retval = iter_ass_subscript(self, ind, val);
Py_DECREF(ind);
return retval;
}
if (PyTuple_Check(ind)) {
int len;
len = PyTuple_GET_SIZE(ind);
if (len > 1) {
goto finish;
}
ind = PyTuple_GET_ITEM(ind, 0);
}
type = PyArray_DESCR(self->ao);
/*
* Check for Boolean -- this is first because
* Bool is a subclass of Int
*/
if (PyBool_Check(ind)) {
retval = 0;
if (PyObject_IsTrue(ind)) {
retval = PyArray_Pack(PyArray_DESCR(self->ao), self->dataptr, val);
}
goto finish;
}
if (PySequence_Check(ind) || PySlice_Check(ind)) {
goto skip;
}
start = PyArray_PyIntAsIntp(ind);
if (error_converting(start)) {
PyErr_Clear();
}
else {
if (check_and_adjust_index(&start, self->size, -1, NULL) < 0) {
goto finish;
}
PyArray_ITER_GOTO1D(self, start);
retval = PyArray_Pack(PyArray_DESCR(self->ao), self->dataptr, val);
PyArray_ITER_RESET(self);
if (retval < 0) {
PyErr_SetString(PyExc_ValueError,
"Error setting single item of array.");
}
goto finish;
}
skip:
Py_INCREF(type);
arrval = (PyArrayObject *)PyArray_FromAny(val, type, 0, 0,
NPY_ARRAY_FORCECAST, NULL);
if (arrval == NULL) {
return -1;
}
val_it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)arrval);
if (val_it == NULL) {
goto finish;
}
if (val_it->size == 0) {
retval = 0;
goto finish;
}
copyswap = PyArray_DESCR(arrval)->f->copyswap;
swap = (PyArray_ISNOTSWAPPED(self->ao)!=PyArray_ISNOTSWAPPED(arrval));
/* Check Slice */
if (PySlice_Check(ind)) {
start = parse_index_entry(ind, &step_size, &n_steps, self->size, 0, 0);
if (start == -1) {
goto finish;
}
if (n_steps == ELLIPSIS_INDEX || n_steps == NEWAXIS_INDEX) {
PyErr_SetString(PyExc_IndexError,
"cannot use Ellipsis or newaxes here");
goto finish;
}
PyArray_ITER_GOTO1D(self, start);
if (n_steps == SINGLE_INDEX) {
/* Integer */
copyswap(self->dataptr, PyArray_DATA(arrval), swap, arrval);
PyArray_ITER_RESET(self);
retval = 0;
goto finish;
}
while (n_steps--) {
copyswap(self->dataptr, val_it->dataptr, swap, arrval);
start += step_size;
PyArray_ITER_GOTO1D(self, start);
PyArray_ITER_NEXT(val_it);
if (val_it->index == val_it->size) {
PyArray_ITER_RESET(val_it);
}
}
PyArray_ITER_RESET(self);
retval = 0;
goto finish;
}
/* convert to INTP array if Integer array scalar or List */
indtype = PyArray_DescrFromType(NPY_INTP);
if (PyList_Check(ind)) {
Py_INCREF(indtype);
obj = PyArray_FromAny(ind, indtype, 0, 0, NPY_ARRAY_FORCECAST, NULL);
}
else {
Py_INCREF(ind);
obj = ind;
}
if (obj != NULL && PyArray_Check(obj)) {
/* Check for Boolean object */
if (PyArray_TYPE((PyArrayObject *)obj)==NPY_BOOL) {
if (iter_ass_sub_Bool(self, (PyArrayObject *)obj,
val_it, swap) < 0) {
goto finish;
}
retval=0;
}
/* Check for integer array */
else if (PyArray_ISINTEGER((PyArrayObject *)obj)) {
PyObject *new;
Py_INCREF(indtype);
new = PyArray_CheckFromAny(obj, indtype, 0, 0,
NPY_ARRAY_FORCECAST | NPY_ARRAY_BEHAVED_NS, NULL);
Py_DECREF(obj);
obj = new;
if (new == NULL) {
goto finish;
}
if (iter_ass_sub_int(self, (PyArrayObject *)obj,
val_it, swap) < 0) {
goto finish;
}
retval = 0;
}
}
finish:
if (!PyErr_Occurred() && retval < 0) {
PyErr_SetString(PyExc_IndexError, "unsupported iterator index");
}
Py_XDECREF(indtype);
Py_XDECREF(obj);
Py_XDECREF(val_it);
Py_XDECREF(arrval);
return retval;
}
static PyMappingMethods iter_as_mapping = {
(lenfunc)iter_length, /*mp_length*/
(binaryfunc)iter_subscript, /*mp_subscript*/
(objobjargproc)iter_ass_subscript, /*mp_ass_subscript*/
};
/* Two options:
* 1) underlying array is contiguous
* -- return 1-d wrapper around it
* 2) underlying array is not contiguous
* -- make new 1-d contiguous array with updateifcopy flag set
* to copy back to the old array
*
* If underlying array is readonly, then we make the output array readonly
* and updateifcopy does not apply.
*
* Changed 2017-07-21, 1.14.0.
*
* In order to start the process of removing UPDATEIFCOPY, see gh-7054, the
* behavior is changed to always return an non-writeable copy when the base
* array is non-contiguous. Doing that will hopefully smoke out those few
* folks who assign to the result with the expectation that the base array
* will be changed. At a later date non-contiguous arrays will always return
* writeable copies.
*
* Note that the type and argument expected for the __array__ method is
* ignored.
*/
static PyArrayObject *
iter_array(PyArrayIterObject *it, PyObject *NPY_UNUSED(op))
{
PyArrayObject *ret;
npy_intp size;
size = PyArray_SIZE(it->ao);
Py_INCREF(PyArray_DESCR(it->ao));
if (PyArray_ISCONTIGUOUS(it->ao)) {
ret = (PyArrayObject *)PyArray_NewFromDescrAndBase(
&PyArray_Type, PyArray_DESCR(it->ao),