-
Notifications
You must be signed in to change notification settings - Fork 294
/
arraymodule.c
2241 lines (2030 loc) · 53.8 KB
/
arraymodule.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
/* Array object implementation */
/* An array is a uniform list -- all items have the same type.
The item type is restricted to simple C types like int or float */
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "structmember.h"
#ifdef STDC_HEADERS
#include <stddef.h>
#else /* !STDC_HEADERS */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h> /* For size_t */
#endif /* HAVE_SYS_TYPES_H */
#endif /* !STDC_HEADERS */
struct arrayobject; /* Forward */
/* All possible arraydescr values are defined in the vector "descriptors"
* below. That's defined later because the appropriate get and set
* functions aren't visible yet.
*/
struct arraydescr {
int typecode;
int itemsize;
PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
};
typedef struct arrayobject {
PyObject_VAR_HEAD
char *ob_item;
Py_ssize_t allocated;
struct arraydescr *ob_descr;
PyObject *weakreflist; /* List of weak references */
} arrayobject;
static PyTypeObject Arraytype;
#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
#define array_CheckExact(op) (Py_TYPE(op) == &Arraytype)
static int
array_resize(arrayobject *self, Py_ssize_t newsize)
{
char *items;
size_t _new_size;
/* Bypass realloc() when a previous overallocation is large enough
to accommodate the newsize. If the newsize is 16 smaller than the
current size, then proceed with the realloc() to shrink the list.
*/
if (self->allocated >= newsize &&
Py_SIZE(self) < newsize + 16 &&
self->ob_item != NULL) {
Py_SIZE(self) = newsize;
return 0;
}
/* This over-allocates proportional to the array size, making room
* for additional growth. The over-allocation is mild, but is
* enough to give linear-time amortized behavior over a long
* sequence of appends() in the presence of a poorly-performing
* system realloc().
* The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
* Note, the pattern starts out the same as for lists but then
* grows at a smaller rate so that larger arrays only overallocate
* by about 1/16th -- this is done because arrays are presumed to be more
* memory critical.
*/
_new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;
items = self->ob_item;
/* XXX The following multiplication and division does not optimize away
like it does for lists since the size is not known at compile time */
if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
else
items = NULL;
if (items == NULL) {
PyErr_NoMemory();
return -1;
}
self->ob_item = items;
Py_SIZE(self) = newsize;
self->allocated = _new_size;
return 0;
}
/****************************************************************************
Get and Set functions for each type.
A Get function takes an arrayobject* and an integer index, returning the
array value at that index wrapped in an appropriate PyObject*.
A Set function takes an arrayobject, integer index, and PyObject*; sets
the array value at that index to the raw C data extracted from the PyObject*,
and returns 0 if successful, else nonzero on failure (PyObject* not of an
appropriate type or value).
Note that the basic Get and Set functions do NOT check that the index is
in bounds; that's the responsibility of the caller.
****************************************************************************/
static PyObject *
c_getitem(arrayobject *ap, Py_ssize_t i)
{
return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
}
static int
c_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
char x;
if (!PyArg_Parse(v, "c;array item must be char", &x))
return -1;
if (i >= 0)
((char *)ap->ob_item)[i] = x;
return 0;
}
static PyObject *
b_getitem(arrayobject *ap, Py_ssize_t i)
{
long x = ((char *)ap->ob_item)[i];
if (x >= 128)
x -= 256;
return PyInt_FromLong(x);
}
static int
b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
short x;
/* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
must use the next size up that is signed ('h') and manually do
the overflow checking */
if (!PyArg_Parse(v, "h;array item must be integer", &x))
return -1;
else if (x < -128) {
PyErr_SetString(PyExc_OverflowError,
"signed char is less than minimum");
return -1;
}
else if (x > 127) {
PyErr_SetString(PyExc_OverflowError,
"signed char is greater than maximum");
return -1;
}
if (i >= 0)
((char *)ap->ob_item)[i] = (char)x;
return 0;
}
static PyObject *
BB_getitem(arrayobject *ap, Py_ssize_t i)
{
long x = ((unsigned char *)ap->ob_item)[i];
return PyInt_FromLong(x);
}
static int
BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
unsigned char x;
/* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
if (!PyArg_Parse(v, "b;array item must be integer", &x))
return -1;
if (i >= 0)
((char *)ap->ob_item)[i] = x;
return 0;
}
#ifdef Py_USING_UNICODE
static PyObject *
u_getitem(arrayobject *ap, Py_ssize_t i)
{
return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
}
static int
u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
Py_UNICODE *p;
Py_ssize_t len;
if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
return -1;
if (len != 1) {
PyErr_SetString(PyExc_TypeError,
"array item must be unicode character");
return -1;
}
if (i >= 0)
((Py_UNICODE *)ap->ob_item)[i] = p[0];
return 0;
}
#endif
static PyObject *
h_getitem(arrayobject *ap, Py_ssize_t i)
{
return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
}
static int
h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
short x;
/* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
if (!PyArg_Parse(v, "h;array item must be integer", &x))
return -1;
if (i >= 0)
((short *)ap->ob_item)[i] = x;
return 0;
}
static PyObject *
HH_getitem(arrayobject *ap, Py_ssize_t i)
{
return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
}
static int
HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
int x;
/* PyArg_Parse's 'h' formatter is for a signed short, therefore
must use the next size up and manually do the overflow checking */
if (!PyArg_Parse(v, "i;array item must be integer", &x))
return -1;
else if (x < 0) {
PyErr_SetString(PyExc_OverflowError,
"unsigned short is less than minimum");
return -1;
}
else if (x > USHRT_MAX) {
PyErr_SetString(PyExc_OverflowError,
"unsigned short is greater than maximum");
return -1;
}
if (i >= 0)
((short *)ap->ob_item)[i] = (short)x;
return 0;
}
static PyObject *
i_getitem(arrayobject *ap, Py_ssize_t i)
{
return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
}
static int
i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
int x;
/* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
if (!PyArg_Parse(v, "i;array item must be integer", &x))
return -1;
if (i >= 0)
((int *)ap->ob_item)[i] = x;
return 0;
}
static PyObject *
II_getitem(arrayobject *ap, Py_ssize_t i)
{
return PyLong_FromUnsignedLong(
(unsigned long) ((unsigned int *)ap->ob_item)[i]);
}
static int
II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
unsigned long x;
if (PyLong_Check(v)) {
x = PyLong_AsUnsignedLong(v);
if (x == (unsigned long) -1 && PyErr_Occurred())
return -1;
}
else {
long y;
if (!PyArg_Parse(v, "l;array item must be integer", &y))
return -1;
if (y < 0) {
PyErr_SetString(PyExc_OverflowError,
"unsigned int is less than minimum");
return -1;
}
x = (unsigned long)y;
}
if (x > UINT_MAX) {
PyErr_SetString(PyExc_OverflowError,
"unsigned int is greater than maximum");
return -1;
}
if (i >= 0)
((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
return 0;
}
static PyObject *
l_getitem(arrayobject *ap, Py_ssize_t i)
{
return PyInt_FromLong(((long *)ap->ob_item)[i]);
}
static int
l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
long x;
if (!PyArg_Parse(v, "l;array item must be integer", &x))
return -1;
if (i >= 0)
((long *)ap->ob_item)[i] = x;
return 0;
}
static PyObject *
LL_getitem(arrayobject *ap, Py_ssize_t i)
{
return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
}
static int
LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
unsigned long x;
if (PyLong_Check(v)) {
x = PyLong_AsUnsignedLong(v);
if (x == (unsigned long) -1 && PyErr_Occurred())
return -1;
}
else {
long y;
if (!PyArg_Parse(v, "l;array item must be integer", &y))
return -1;
if (y < 0) {
PyErr_SetString(PyExc_OverflowError,
"unsigned long is less than minimum");
return -1;
}
x = (unsigned long)y;
}
if (x > ULONG_MAX) {
PyErr_SetString(PyExc_OverflowError,
"unsigned long is greater than maximum");
return -1;
}
if (i >= 0)
((unsigned long *)ap->ob_item)[i] = x;
return 0;
}
static PyObject *
f_getitem(arrayobject *ap, Py_ssize_t i)
{
return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
}
static int
f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
float x;
if (!PyArg_Parse(v, "f;array item must be float", &x))
return -1;
if (i >= 0)
((float *)ap->ob_item)[i] = x;
return 0;
}
static PyObject *
d_getitem(arrayobject *ap, Py_ssize_t i)
{
return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
}
static int
d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
double x;
if (!PyArg_Parse(v, "d;array item must be float", &x))
return -1;
if (i >= 0)
((double *)ap->ob_item)[i] = x;
return 0;
}
/* Description of types */
static struct arraydescr descriptors[] = {
{'c', sizeof(char), c_getitem, c_setitem},
{'b', sizeof(char), b_getitem, b_setitem},
{'B', sizeof(char), BB_getitem, BB_setitem},
#ifdef Py_USING_UNICODE
{'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
#endif
{'h', sizeof(short), h_getitem, h_setitem},
{'H', sizeof(short), HH_getitem, HH_setitem},
{'i', sizeof(int), i_getitem, i_setitem},
{'I', sizeof(int), II_getitem, II_setitem},
{'l', sizeof(long), l_getitem, l_setitem},
{'L', sizeof(long), LL_getitem, LL_setitem},
{'f', sizeof(float), f_getitem, f_setitem},
{'d', sizeof(double), d_getitem, d_setitem},
{'\0', 0, 0, 0} /* Sentinel */
};
/****************************************************************************
Implementations of array object methods.
****************************************************************************/
static PyObject *
newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
{
arrayobject *op;
size_t nbytes;
if (size < 0) {
PyErr_BadInternalCall();
return NULL;
}
nbytes = size * descr->itemsize;
/* Check for overflow */
if (nbytes / descr->itemsize != (size_t)size) {
return PyErr_NoMemory();
}
op = (arrayobject *) type->tp_alloc(type, 0);
if (op == NULL) {
return NULL;
}
op->ob_descr = descr;
op->allocated = size;
op->weakreflist = NULL;
Py_SIZE(op) = size;
if (size <= 0) {
op->ob_item = NULL;
}
else {
op->ob_item = PyMem_NEW(char, nbytes);
if (op->ob_item == NULL) {
Py_DECREF(op);
return PyErr_NoMemory();
}
}
return (PyObject *) op;
}
static PyObject *
getarrayitem(PyObject *op, Py_ssize_t i)
{
register arrayobject *ap;
assert(array_Check(op));
ap = (arrayobject *)op;
assert(i>=0 && i<Py_SIZE(ap));
return (*ap->ob_descr->getitem)(ap, i);
}
static int
ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
{
char *items;
Py_ssize_t n = Py_SIZE(self);
if (v == NULL) {
PyErr_BadInternalCall();
return -1;
}
if ((*self->ob_descr->setitem)(self, -1, v) < 0)
return -1;
if (array_resize(self, n+1) == -1)
return -1;
items = self->ob_item;
if (where < 0) {
where += n;
if (where < 0)
where = 0;
}
if (where > n)
where = n;
/* appends don't need to call memmove() */
if (where != n)
memmove(items + (where+1)*self->ob_descr->itemsize,
items + where*self->ob_descr->itemsize,
(n-where)*self->ob_descr->itemsize);
return (*self->ob_descr->setitem)(self, where, v);
}
/* Methods */
static void
array_dealloc(arrayobject *op)
{
if (op->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) op);
if (op->ob_item != NULL)
PyMem_DEL(op->ob_item);
Py_TYPE(op)->tp_free((PyObject *)op);
}
static PyObject *
array_richcompare(PyObject *v, PyObject *w, int op)
{
arrayobject *va, *wa;
PyObject *vi = NULL;
PyObject *wi = NULL;
Py_ssize_t i, k;
PyObject *res;
if (!array_Check(v) || !array_Check(w)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
va = (arrayobject *)v;
wa = (arrayobject *)w;
if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
/* Shortcut: if the lengths differ, the arrays differ */
if (op == Py_EQ)
res = Py_False;
else
res = Py_True;
Py_INCREF(res);
return res;
}
/* Search for the first index where items are different */
k = 1;
for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
vi = getarrayitem(v, i);
wi = getarrayitem(w, i);
if (vi == NULL || wi == NULL) {
Py_XDECREF(vi);
Py_XDECREF(wi);
return NULL;
}
k = PyObject_RichCompareBool(vi, wi, Py_EQ);
if (k == 0)
break; /* Keeping vi and wi alive! */
Py_DECREF(vi);
Py_DECREF(wi);
if (k < 0)
return NULL;
}
if (k) {
/* No more items to compare -- compare sizes */
Py_ssize_t vs = Py_SIZE(va);
Py_ssize_t ws = Py_SIZE(wa);
int cmp;
switch (op) {
case Py_LT: cmp = vs < ws; break;
case Py_LE: cmp = vs <= ws; break;
case Py_EQ: cmp = vs == ws; break;
case Py_NE: cmp = vs != ws; break;
case Py_GT: cmp = vs > ws; break;
case Py_GE: cmp = vs >= ws; break;
default: return NULL; /* cannot happen */
}
if (cmp)
res = Py_True;
else
res = Py_False;
Py_INCREF(res);
return res;
}
/* We have an item that differs. First, shortcuts for EQ/NE */
if (op == Py_EQ) {
Py_INCREF(Py_False);
res = Py_False;
}
else if (op == Py_NE) {
Py_INCREF(Py_True);
res = Py_True;
}
else {
/* Compare the final item again using the proper operator */
res = PyObject_RichCompare(vi, wi, op);
}
Py_DECREF(vi);
Py_DECREF(wi);
return res;
}
static Py_ssize_t
array_length(arrayobject *a)
{
return Py_SIZE(a);
}
static PyObject *
array_item(arrayobject *a, Py_ssize_t i)
{
if (i < 0 || i >= Py_SIZE(a)) {
PyErr_SetString(PyExc_IndexError, "array index out of range");
return NULL;
}
return getarrayitem((PyObject *)a, i);
}
static PyObject *
array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
{
arrayobject *np;
if (ilow < 0)
ilow = 0;
else if (ilow > Py_SIZE(a))
ilow = Py_SIZE(a);
if (ihigh < 0)
ihigh = 0;
if (ihigh < ilow)
ihigh = ilow;
else if (ihigh > Py_SIZE(a))
ihigh = Py_SIZE(a);
np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
if (np == NULL)
return NULL;
memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
(ihigh-ilow) * a->ob_descr->itemsize);
return (PyObject *)np;
}
static PyObject *
array_copy(arrayobject *a, PyObject *unused)
{
return array_slice(a, 0, Py_SIZE(a));
}
PyDoc_STRVAR(copy_doc,
"copy(array)\n\
\n\
Return a copy of the array.");
static PyObject *
array_concat(arrayobject *a, PyObject *bb)
{
Py_ssize_t size;
arrayobject *np;
if (!array_Check(bb)) {
PyErr_Format(PyExc_TypeError,
"can only append array (not \"%.200s\") to array",
Py_TYPE(bb)->tp_name);
return NULL;
}
#define b ((arrayobject *)bb)
if (a->ob_descr != b->ob_descr) {
PyErr_BadArgument();
return NULL;
}
if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
return PyErr_NoMemory();
}
size = Py_SIZE(a) + Py_SIZE(b);
np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
if (np == NULL) {
return NULL;
}
memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
return (PyObject *)np;
#undef b
}
static PyObject *
array_repeat(arrayobject *a, Py_ssize_t n)
{
Py_ssize_t i;
Py_ssize_t size;
arrayobject *np;
char *p;
Py_ssize_t nbytes;
if (n < 0)
n = 0;
if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
return PyErr_NoMemory();
}
size = Py_SIZE(a) * n;
np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
if (np == NULL)
return NULL;
p = np->ob_item;
nbytes = Py_SIZE(a) * a->ob_descr->itemsize;
for (i = 0; i < n; i++) {
memcpy(p, a->ob_item, nbytes);
p += nbytes;
}
return (PyObject *) np;
}
static int
array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
{
char *item;
Py_ssize_t n; /* Size of replacement array */
Py_ssize_t d; /* Change in size */
#define b ((arrayobject *)v)
if (v == NULL)
n = 0;
else if (array_Check(v)) {
n = Py_SIZE(b);
if (a == b) {
/* Special case "a[i:j] = a" -- copy b first */
int ret;
v = array_slice(b, 0, n);
if (!v)
return -1;
ret = array_ass_slice(a, ilow, ihigh, v);
Py_DECREF(v);
return ret;
}
if (b->ob_descr != a->ob_descr) {
PyErr_BadArgument();
return -1;
}
}
else {
PyErr_Format(PyExc_TypeError,
"can only assign array (not \"%.200s\") to array slice",
Py_TYPE(v)->tp_name);
return -1;
}
if (ilow < 0)
ilow = 0;
else if (ilow > Py_SIZE(a))
ilow = Py_SIZE(a);
if (ihigh < 0)
ihigh = 0;
if (ihigh < ilow)
ihigh = ilow;
else if (ihigh > Py_SIZE(a))
ihigh = Py_SIZE(a);
item = a->ob_item;
d = n - (ihigh-ilow);
if (d < 0) { /* Delete -d items */
memmove(item + (ihigh+d)*a->ob_descr->itemsize,
item + ihigh*a->ob_descr->itemsize,
(Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
Py_SIZE(a) += d;
PyMem_RESIZE(item, char, Py_SIZE(a)*a->ob_descr->itemsize);
/* Can't fail */
a->ob_item = item;
a->allocated = Py_SIZE(a);
}
else if (d > 0) { /* Insert d items */
PyMem_RESIZE(item, char,
(Py_SIZE(a) + d)*a->ob_descr->itemsize);
if (item == NULL) {
PyErr_NoMemory();
return -1;
}
memmove(item + (ihigh+d)*a->ob_descr->itemsize,
item + ihigh*a->ob_descr->itemsize,
(Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
a->ob_item = item;
Py_SIZE(a) += d;
a->allocated = Py_SIZE(a);
}
if (n > 0)
memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
n*b->ob_descr->itemsize);
return 0;
#undef b
}
static int
array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
{
if (i < 0 || i >= Py_SIZE(a)) {
PyErr_SetString(PyExc_IndexError,
"array assignment index out of range");
return -1;
}
if (v == NULL)
return array_ass_slice(a, i, i+1, v);
return (*a->ob_descr->setitem)(a, i, v);
}
static int
setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
{
assert(array_Check(a));
return array_ass_item((arrayobject *)a, i, v);
}
static int
array_iter_extend(arrayobject *self, PyObject *bb)
{
PyObject *it, *v;
it = PyObject_GetIter(bb);
if (it == NULL)
return -1;
while ((v = PyIter_Next(it)) != NULL) {
if (ins1(self, (int) Py_SIZE(self), v) != 0) {
Py_DECREF(v);
Py_DECREF(it);
return -1;
}
Py_DECREF(v);
}
Py_DECREF(it);
if (PyErr_Occurred())
return -1;
return 0;
}
static int
array_do_extend(arrayobject *self, PyObject *bb)
{
Py_ssize_t size;
char *old_item;
if (!array_Check(bb))
return array_iter_extend(self, bb);
#define b ((arrayobject *)bb)
if (self->ob_descr != b->ob_descr) {
PyErr_SetString(PyExc_TypeError,
"can only extend with array of same kind");
return -1;
}
if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
PyErr_NoMemory();
return -1;
}
size = Py_SIZE(self) + Py_SIZE(b);
old_item = self->ob_item;
PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
if (self->ob_item == NULL) {
self->ob_item = old_item;
PyErr_NoMemory();
return -1;
}
memcpy(self->ob_item + Py_SIZE(self)*self->ob_descr->itemsize,
b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
Py_SIZE(self) = size;
self->allocated = size;
return 0;
#undef b
}
static PyObject *
array_inplace_concat(arrayobject *self, PyObject *bb)
{
if (!array_Check(bb)) {
PyErr_Format(PyExc_TypeError,
"can only extend array with array (not \"%.200s\")",
Py_TYPE(bb)->tp_name);
return NULL;
}
if (array_do_extend(self, bb) == -1)
return NULL;
Py_INCREF(self);
return (PyObject *)self;
}
static PyObject *
array_inplace_repeat(arrayobject *self, Py_ssize_t n)
{
char *items, *p;
Py_ssize_t size, i;
if (Py_SIZE(self) > 0) {
if (n < 0)
n = 0;
items = self->ob_item;
if ((self->ob_descr->itemsize != 0) &&
(Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
return PyErr_NoMemory();
}
size = Py_SIZE(self) * self->ob_descr->itemsize;
if (n == 0) {
PyMem_FREE(items);
self->ob_item = NULL;
Py_SIZE(self) = 0;
self->allocated = 0;
}
else {
if (size > PY_SSIZE_T_MAX / n) {
return PyErr_NoMemory();
}
PyMem_RESIZE(items, char, n * size);
if (items == NULL)
return PyErr_NoMemory();
p = items;
for (i = 1; i < n; i++) {
p += size;
memcpy(p, items, size);
}
self->ob_item = items;
Py_SIZE(self) *= n;
self->allocated = Py_SIZE(self);
}
}
Py_INCREF(self);
return (PyObject *)self;
}
static PyObject *
ins(arrayobject *self, Py_ssize_t where, PyObject *v)
{
if (ins1(self, where, v) != 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
array_count(arrayobject *self, PyObject *v)
{
Py_ssize_t count = 0;
Py_ssize_t i;
for (i = 0; i < Py_SIZE(self); i++) {
PyObject *selfi = getarrayitem((PyObject *)self, i);
int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Py_DECREF(selfi);
if (cmp > 0)
count++;
else if (cmp < 0)
return NULL;
}
return PyInt_FromSsize_t(count);
}
PyDoc_STRVAR(count_doc,
"count(x)\n\
\n\
Return number of occurences of x in the array.");
static PyObject *
array_index(arrayobject *self, PyObject *v)
{
Py_ssize_t i;
for (i = 0; i < Py_SIZE(self); i++) {
PyObject *selfi = getarrayitem((PyObject *)self, i);
int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Py_DECREF(selfi);
if (cmp > 0) {
return PyInt_FromLong((long)i);
}
else if (cmp < 0)
return NULL;
}
PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
return NULL;
}
PyDoc_STRVAR(index_doc,
"index(x)\n\
\n\
Return index of first occurence of x in the array.");
static int
array_contains(arrayobject *self, PyObject *v)
{
Py_ssize_t i;
int cmp;
for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
PyObject *selfi = getarrayitem((PyObject *)self, i);
cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Py_DECREF(selfi);
}
return cmp;
}
static PyObject *
array_remove(arrayobject *self, PyObject *v)
{
int i;
for (i = 0; i < Py_SIZE(self); i++) {
PyObject *selfi = getarrayitem((PyObject *)self,i);
int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Py_DECREF(selfi);
if (cmp > 0) {
if (array_ass_slice(self, i, i+1,
(PyObject *)NULL) != 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
else if (cmp < 0)
return NULL;
}
PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
return NULL;
}