Applying patches backported from 3.1, by Gregor Lingl.
[python.git] / Objects / listobject.c
blob98d7e473549e0a2c557a0754fc0ea64ac38fbf3a
1 /* List object implementation */
3 #include "Python.h"
5 #ifdef STDC_HEADERS
6 #include <stddef.h>
7 #else
8 #include <sys/types.h> /* For size_t */
9 #endif
11 /* Ensure ob_item has room for at least newsize elements, and set
12 * ob_size to newsize. If newsize > ob_size on entry, the content
13 * of the new slots at exit is undefined heap trash; it's the caller's
14 * responsiblity to overwrite them with sane values.
15 * The number of allocated elements may grow, shrink, or stay the same.
16 * Failure is impossible if newsize <= self.allocated on entry, although
17 * that partly relies on an assumption that the system realloc() never
18 * fails when passed a number of bytes <= the number of bytes last
19 * allocated (the C standard doesn't guarantee this, but it's hard to
20 * imagine a realloc implementation where it wouldn't be true).
21 * Note that self->ob_item may change, and even if newsize is less
22 * than ob_size on entry.
24 static int
25 list_resize(PyListObject *self, Py_ssize_t newsize)
27 PyObject **items;
28 size_t new_allocated;
29 Py_ssize_t allocated = self->allocated;
31 /* Bypass realloc() when a previous overallocation is large enough
32 to accommodate the newsize. If the newsize falls lower than half
33 the allocated size, then proceed with the realloc() to shrink the list.
35 if (allocated >= newsize && newsize >= (allocated >> 1)) {
36 assert(self->ob_item != NULL || newsize == 0);
37 Py_SIZE(self) = newsize;
38 return 0;
41 /* This over-allocates proportional to the list size, making room
42 * for additional growth. The over-allocation is mild, but is
43 * enough to give linear-time amortized behavior over a long
44 * sequence of appends() in the presence of a poorly-performing
45 * system realloc().
46 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
48 new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
50 /* check for integer overflow */
51 if (new_allocated > PY_SIZE_MAX - newsize) {
52 PyErr_NoMemory();
53 return -1;
54 } else {
55 new_allocated += newsize;
58 if (newsize == 0)
59 new_allocated = 0;
60 items = self->ob_item;
61 if (new_allocated <= ((~(size_t)0) / sizeof(PyObject *)))
62 PyMem_RESIZE(items, PyObject *, new_allocated);
63 else
64 items = NULL;
65 if (items == NULL) {
66 PyErr_NoMemory();
67 return -1;
69 self->ob_item = items;
70 Py_SIZE(self) = newsize;
71 self->allocated = new_allocated;
72 return 0;
75 /* Debug statistic to compare allocations with reuse through the free list */
76 #undef SHOW_ALLOC_COUNT
77 #ifdef SHOW_ALLOC_COUNT
78 static size_t count_alloc = 0;
79 static size_t count_reuse = 0;
81 static void
82 show_alloc(void)
84 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
85 count_alloc);
86 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
87 "d\n", count_reuse);
88 fprintf(stderr, "%.2f%% reuse rate\n\n",
89 (100.0*count_reuse/(count_alloc+count_reuse)));
91 #endif
93 /* Empty list reuse scheme to save calls to malloc and free */
94 #ifndef PyList_MAXFREELIST
95 #define PyList_MAXFREELIST 80
96 #endif
97 static PyListObject *free_list[PyList_MAXFREELIST];
98 static int numfree = 0;
100 void
101 PyList_Fini(void)
103 PyListObject *op;
105 while (numfree) {
106 op = free_list[--numfree];
107 assert(PyList_CheckExact(op));
108 PyObject_GC_Del(op);
112 PyObject *
113 PyList_New(Py_ssize_t size)
115 PyListObject *op;
116 size_t nbytes;
117 #ifdef SHOW_ALLOC_COUNT
118 static int initialized = 0;
119 if (!initialized) {
120 Py_AtExit(show_alloc);
121 initialized = 1;
123 #endif
125 if (size < 0) {
126 PyErr_BadInternalCall();
127 return NULL;
129 nbytes = size * sizeof(PyObject *);
130 /* Check for overflow without an actual overflow,
131 * which can cause compiler to optimise out */
132 if (size > PY_SIZE_MAX / sizeof(PyObject *))
133 return PyErr_NoMemory();
134 if (numfree) {
135 numfree--;
136 op = free_list[numfree];
137 _Py_NewReference((PyObject *)op);
138 #ifdef SHOW_ALLOC_COUNT
139 count_reuse++;
140 #endif
141 } else {
142 op = PyObject_GC_New(PyListObject, &PyList_Type);
143 if (op == NULL)
144 return NULL;
145 #ifdef SHOW_ALLOC_COUNT
146 count_alloc++;
147 #endif
149 if (size <= 0)
150 op->ob_item = NULL;
151 else {
152 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
153 if (op->ob_item == NULL) {
154 Py_DECREF(op);
155 return PyErr_NoMemory();
157 memset(op->ob_item, 0, nbytes);
159 Py_SIZE(op) = size;
160 op->allocated = size;
161 _PyObject_GC_TRACK(op);
162 return (PyObject *) op;
165 Py_ssize_t
166 PyList_Size(PyObject *op)
168 if (!PyList_Check(op)) {
169 PyErr_BadInternalCall();
170 return -1;
172 else
173 return Py_SIZE(op);
176 static PyObject *indexerr = NULL;
178 PyObject *
179 PyList_GetItem(PyObject *op, Py_ssize_t i)
181 if (!PyList_Check(op)) {
182 PyErr_BadInternalCall();
183 return NULL;
185 if (i < 0 || i >= Py_SIZE(op)) {
186 if (indexerr == NULL)
187 indexerr = PyString_FromString(
188 "list index out of range");
189 PyErr_SetObject(PyExc_IndexError, indexerr);
190 return NULL;
192 return ((PyListObject *)op) -> ob_item[i];
196 PyList_SetItem(register PyObject *op, register Py_ssize_t i,
197 register PyObject *newitem)
199 register PyObject *olditem;
200 register PyObject **p;
201 if (!PyList_Check(op)) {
202 Py_XDECREF(newitem);
203 PyErr_BadInternalCall();
204 return -1;
206 if (i < 0 || i >= Py_SIZE(op)) {
207 Py_XDECREF(newitem);
208 PyErr_SetString(PyExc_IndexError,
209 "list assignment index out of range");
210 return -1;
212 p = ((PyListObject *)op) -> ob_item + i;
213 olditem = *p;
214 *p = newitem;
215 Py_XDECREF(olditem);
216 return 0;
219 static int
220 ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
222 Py_ssize_t i, n = Py_SIZE(self);
223 PyObject **items;
224 if (v == NULL) {
225 PyErr_BadInternalCall();
226 return -1;
228 if (n == PY_SSIZE_T_MAX) {
229 PyErr_SetString(PyExc_OverflowError,
230 "cannot add more objects to list");
231 return -1;
234 if (list_resize(self, n+1) == -1)
235 return -1;
237 if (where < 0) {
238 where += n;
239 if (where < 0)
240 where = 0;
242 if (where > n)
243 where = n;
244 items = self->ob_item;
245 for (i = n; --i >= where; )
246 items[i+1] = items[i];
247 Py_INCREF(v);
248 items[where] = v;
249 return 0;
253 PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
255 if (!PyList_Check(op)) {
256 PyErr_BadInternalCall();
257 return -1;
259 return ins1((PyListObject *)op, where, newitem);
262 static int
263 app1(PyListObject *self, PyObject *v)
265 Py_ssize_t n = PyList_GET_SIZE(self);
267 assert (v != NULL);
268 if (n == PY_SSIZE_T_MAX) {
269 PyErr_SetString(PyExc_OverflowError,
270 "cannot add more objects to list");
271 return -1;
274 if (list_resize(self, n+1) == -1)
275 return -1;
277 Py_INCREF(v);
278 PyList_SET_ITEM(self, n, v);
279 return 0;
283 PyList_Append(PyObject *op, PyObject *newitem)
285 if (PyList_Check(op) && (newitem != NULL))
286 return app1((PyListObject *)op, newitem);
287 PyErr_BadInternalCall();
288 return -1;
291 /* Methods */
293 static void
294 list_dealloc(PyListObject *op)
296 Py_ssize_t i;
297 PyObject_GC_UnTrack(op);
298 Py_TRASHCAN_SAFE_BEGIN(op)
299 if (op->ob_item != NULL) {
300 /* Do it backwards, for Christian Tismer.
301 There's a simple test case where somehow this reduces
302 thrashing when a *very* large list is created and
303 immediately deleted. */
304 i = Py_SIZE(op);
305 while (--i >= 0) {
306 Py_XDECREF(op->ob_item[i]);
308 PyMem_FREE(op->ob_item);
310 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
311 free_list[numfree++] = op;
312 else
313 Py_TYPE(op)->tp_free((PyObject *)op);
314 Py_TRASHCAN_SAFE_END(op)
317 static int
318 list_print(PyListObject *op, FILE *fp, int flags)
320 int rc;
321 Py_ssize_t i;
323 rc = Py_ReprEnter((PyObject*)op);
324 if (rc != 0) {
325 if (rc < 0)
326 return rc;
327 Py_BEGIN_ALLOW_THREADS
328 fprintf(fp, "[...]");
329 Py_END_ALLOW_THREADS
330 return 0;
332 Py_BEGIN_ALLOW_THREADS
333 fprintf(fp, "[");
334 Py_END_ALLOW_THREADS
335 for (i = 0; i < Py_SIZE(op); i++) {
336 if (i > 0) {
337 Py_BEGIN_ALLOW_THREADS
338 fprintf(fp, ", ");
339 Py_END_ALLOW_THREADS
341 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
342 Py_ReprLeave((PyObject *)op);
343 return -1;
346 Py_BEGIN_ALLOW_THREADS
347 fprintf(fp, "]");
348 Py_END_ALLOW_THREADS
349 Py_ReprLeave((PyObject *)op);
350 return 0;
353 static PyObject *
354 list_repr(PyListObject *v)
356 Py_ssize_t i;
357 PyObject *s, *temp;
358 PyObject *pieces = NULL, *result = NULL;
360 i = Py_ReprEnter((PyObject*)v);
361 if (i != 0) {
362 return i > 0 ? PyString_FromString("[...]") : NULL;
365 if (Py_SIZE(v) == 0) {
366 result = PyString_FromString("[]");
367 goto Done;
370 pieces = PyList_New(0);
371 if (pieces == NULL)
372 goto Done;
374 /* Do repr() on each element. Note that this may mutate the list,
375 so must refetch the list size on each iteration. */
376 for (i = 0; i < Py_SIZE(v); ++i) {
377 int status;
378 if (Py_EnterRecursiveCall(" while getting the repr of a list"))
379 goto Done;
380 s = PyObject_Repr(v->ob_item[i]);
381 Py_LeaveRecursiveCall();
382 if (s == NULL)
383 goto Done;
384 status = PyList_Append(pieces, s);
385 Py_DECREF(s); /* append created a new ref */
386 if (status < 0)
387 goto Done;
390 /* Add "[]" decorations to the first and last items. */
391 assert(PyList_GET_SIZE(pieces) > 0);
392 s = PyString_FromString("[");
393 if (s == NULL)
394 goto Done;
395 temp = PyList_GET_ITEM(pieces, 0);
396 PyString_ConcatAndDel(&s, temp);
397 PyList_SET_ITEM(pieces, 0, s);
398 if (s == NULL)
399 goto Done;
401 s = PyString_FromString("]");
402 if (s == NULL)
403 goto Done;
404 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
405 PyString_ConcatAndDel(&temp, s);
406 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
407 if (temp == NULL)
408 goto Done;
410 /* Paste them all together with ", " between. */
411 s = PyString_FromString(", ");
412 if (s == NULL)
413 goto Done;
414 result = _PyString_Join(s, pieces);
415 Py_DECREF(s);
417 Done:
418 Py_XDECREF(pieces);
419 Py_ReprLeave((PyObject *)v);
420 return result;
423 static Py_ssize_t
424 list_length(PyListObject *a)
426 return Py_SIZE(a);
429 static int
430 list_contains(PyListObject *a, PyObject *el)
432 Py_ssize_t i;
433 int cmp;
435 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
436 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
437 Py_EQ);
438 return cmp;
441 static PyObject *
442 list_item(PyListObject *a, Py_ssize_t i)
444 if (i < 0 || i >= Py_SIZE(a)) {
445 if (indexerr == NULL)
446 indexerr = PyString_FromString(
447 "list index out of range");
448 PyErr_SetObject(PyExc_IndexError, indexerr);
449 return NULL;
451 Py_INCREF(a->ob_item[i]);
452 return a->ob_item[i];
455 static PyObject *
456 list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
458 PyListObject *np;
459 PyObject **src, **dest;
460 Py_ssize_t i, len;
461 if (ilow < 0)
462 ilow = 0;
463 else if (ilow > Py_SIZE(a))
464 ilow = Py_SIZE(a);
465 if (ihigh < ilow)
466 ihigh = ilow;
467 else if (ihigh > Py_SIZE(a))
468 ihigh = Py_SIZE(a);
469 len = ihigh - ilow;
470 np = (PyListObject *) PyList_New(len);
471 if (np == NULL)
472 return NULL;
474 src = a->ob_item + ilow;
475 dest = np->ob_item;
476 for (i = 0; i < len; i++) {
477 PyObject *v = src[i];
478 Py_INCREF(v);
479 dest[i] = v;
481 return (PyObject *)np;
484 PyObject *
485 PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
487 if (!PyList_Check(a)) {
488 PyErr_BadInternalCall();
489 return NULL;
491 return list_slice((PyListObject *)a, ilow, ihigh);
494 static PyObject *
495 list_concat(PyListObject *a, PyObject *bb)
497 Py_ssize_t size;
498 Py_ssize_t i;
499 PyObject **src, **dest;
500 PyListObject *np;
501 if (!PyList_Check(bb)) {
502 PyErr_Format(PyExc_TypeError,
503 "can only concatenate list (not \"%.200s\") to list",
504 bb->ob_type->tp_name);
505 return NULL;
507 #define b ((PyListObject *)bb)
508 size = Py_SIZE(a) + Py_SIZE(b);
509 if (size < 0)
510 return PyErr_NoMemory();
511 np = (PyListObject *) PyList_New(size);
512 if (np == NULL) {
513 return NULL;
515 src = a->ob_item;
516 dest = np->ob_item;
517 for (i = 0; i < Py_SIZE(a); i++) {
518 PyObject *v = src[i];
519 Py_INCREF(v);
520 dest[i] = v;
522 src = b->ob_item;
523 dest = np->ob_item + Py_SIZE(a);
524 for (i = 0; i < Py_SIZE(b); i++) {
525 PyObject *v = src[i];
526 Py_INCREF(v);
527 dest[i] = v;
529 return (PyObject *)np;
530 #undef b
533 static PyObject *
534 list_repeat(PyListObject *a, Py_ssize_t n)
536 Py_ssize_t i, j;
537 Py_ssize_t size;
538 PyListObject *np;
539 PyObject **p, **items;
540 PyObject *elem;
541 if (n < 0)
542 n = 0;
543 size = Py_SIZE(a) * n;
544 if (n && size/n != Py_SIZE(a))
545 return PyErr_NoMemory();
546 if (size == 0)
547 return PyList_New(0);
548 np = (PyListObject *) PyList_New(size);
549 if (np == NULL)
550 return NULL;
552 items = np->ob_item;
553 if (Py_SIZE(a) == 1) {
554 elem = a->ob_item[0];
555 for (i = 0; i < n; i++) {
556 items[i] = elem;
557 Py_INCREF(elem);
559 return (PyObject *) np;
561 p = np->ob_item;
562 items = a->ob_item;
563 for (i = 0; i < n; i++) {
564 for (j = 0; j < Py_SIZE(a); j++) {
565 *p = items[j];
566 Py_INCREF(*p);
567 p++;
570 return (PyObject *) np;
573 static int
574 list_clear(PyListObject *a)
576 Py_ssize_t i;
577 PyObject **item = a->ob_item;
578 if (item != NULL) {
579 /* Because XDECREF can recursively invoke operations on
580 this list, we make it empty first. */
581 i = Py_SIZE(a);
582 Py_SIZE(a) = 0;
583 a->ob_item = NULL;
584 a->allocated = 0;
585 while (--i >= 0) {
586 Py_XDECREF(item[i]);
588 PyMem_FREE(item);
590 /* Never fails; the return value can be ignored.
591 Note that there is no guarantee that the list is actually empty
592 at this point, because XDECREF may have populated it again! */
593 return 0;
596 /* a[ilow:ihigh] = v if v != NULL.
597 * del a[ilow:ihigh] if v == NULL.
599 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
600 * guaranteed the call cannot fail.
602 static int
603 list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
605 /* Because [X]DECREF can recursively invoke list operations on
606 this list, we must postpone all [X]DECREF activity until
607 after the list is back in its canonical shape. Therefore
608 we must allocate an additional array, 'recycle', into which
609 we temporarily copy the items that are deleted from the
610 list. :-( */
611 PyObject *recycle_on_stack[8];
612 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
613 PyObject **item;
614 PyObject **vitem = NULL;
615 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
616 Py_ssize_t n; /* # of elements in replacement list */
617 Py_ssize_t norig; /* # of elements in list getting replaced */
618 Py_ssize_t d; /* Change in size */
619 Py_ssize_t k;
620 size_t s;
621 int result = -1; /* guilty until proved innocent */
622 #define b ((PyListObject *)v)
623 if (v == NULL)
624 n = 0;
625 else {
626 if (a == b) {
627 /* Special case "a[i:j] = a" -- copy b first */
628 v = list_slice(b, 0, Py_SIZE(b));
629 if (v == NULL)
630 return result;
631 result = list_ass_slice(a, ilow, ihigh, v);
632 Py_DECREF(v);
633 return result;
635 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
636 if(v_as_SF == NULL)
637 goto Error;
638 n = PySequence_Fast_GET_SIZE(v_as_SF);
639 vitem = PySequence_Fast_ITEMS(v_as_SF);
641 if (ilow < 0)
642 ilow = 0;
643 else if (ilow > Py_SIZE(a))
644 ilow = Py_SIZE(a);
646 if (ihigh < ilow)
647 ihigh = ilow;
648 else if (ihigh > Py_SIZE(a))
649 ihigh = Py_SIZE(a);
651 norig = ihigh - ilow;
652 assert(norig >= 0);
653 d = n - norig;
654 if (Py_SIZE(a) + d == 0) {
655 Py_XDECREF(v_as_SF);
656 return list_clear(a);
658 item = a->ob_item;
659 /* recycle the items that we are about to remove */
660 s = norig * sizeof(PyObject *);
661 if (s > sizeof(recycle_on_stack)) {
662 recycle = (PyObject **)PyMem_MALLOC(s);
663 if (recycle == NULL) {
664 PyErr_NoMemory();
665 goto Error;
668 memcpy(recycle, &item[ilow], s);
670 if (d < 0) { /* Delete -d items */
671 memmove(&item[ihigh+d], &item[ihigh],
672 (Py_SIZE(a) - ihigh)*sizeof(PyObject *));
673 list_resize(a, Py_SIZE(a) + d);
674 item = a->ob_item;
676 else if (d > 0) { /* Insert d items */
677 k = Py_SIZE(a);
678 if (list_resize(a, k+d) < 0)
679 goto Error;
680 item = a->ob_item;
681 memmove(&item[ihigh+d], &item[ihigh],
682 (k - ihigh)*sizeof(PyObject *));
684 for (k = 0; k < n; k++, ilow++) {
685 PyObject *w = vitem[k];
686 Py_XINCREF(w);
687 item[ilow] = w;
689 for (k = norig - 1; k >= 0; --k)
690 Py_XDECREF(recycle[k]);
691 result = 0;
692 Error:
693 if (recycle != recycle_on_stack)
694 PyMem_FREE(recycle);
695 Py_XDECREF(v_as_SF);
696 return result;
697 #undef b
701 PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
703 if (!PyList_Check(a)) {
704 PyErr_BadInternalCall();
705 return -1;
707 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
710 static PyObject *
711 list_inplace_repeat(PyListObject *self, Py_ssize_t n)
713 PyObject **items;
714 Py_ssize_t size, i, j, p;
717 size = PyList_GET_SIZE(self);
718 if (size == 0 || n == 1) {
719 Py_INCREF(self);
720 return (PyObject *)self;
723 if (n < 1) {
724 (void)list_clear(self);
725 Py_INCREF(self);
726 return (PyObject *)self;
729 if (size > PY_SSIZE_T_MAX / n) {
730 return PyErr_NoMemory();
733 if (list_resize(self, size*n) == -1)
734 return NULL;
736 p = size;
737 items = self->ob_item;
738 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
739 for (j = 0; j < size; j++) {
740 PyObject *o = items[j];
741 Py_INCREF(o);
742 items[p++] = o;
745 Py_INCREF(self);
746 return (PyObject *)self;
749 static int
750 list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
752 PyObject *old_value;
753 if (i < 0 || i >= Py_SIZE(a)) {
754 PyErr_SetString(PyExc_IndexError,
755 "list assignment index out of range");
756 return -1;
758 if (v == NULL)
759 return list_ass_slice(a, i, i+1, v);
760 Py_INCREF(v);
761 old_value = a->ob_item[i];
762 a->ob_item[i] = v;
763 Py_DECREF(old_value);
764 return 0;
767 static PyObject *
768 listinsert(PyListObject *self, PyObject *args)
770 Py_ssize_t i;
771 PyObject *v;
772 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
773 return NULL;
774 if (ins1(self, i, v) == 0)
775 Py_RETURN_NONE;
776 return NULL;
779 static PyObject *
780 listappend(PyListObject *self, PyObject *v)
782 if (app1(self, v) == 0)
783 Py_RETURN_NONE;
784 return NULL;
787 static PyObject *
788 listextend(PyListObject *self, PyObject *b)
790 PyObject *it; /* iter(v) */
791 Py_ssize_t m; /* size of self */
792 Py_ssize_t n; /* guess for size of b */
793 Py_ssize_t mn; /* m + n */
794 Py_ssize_t i;
795 PyObject *(*iternext)(PyObject *);
797 /* Special cases:
798 1) lists and tuples which can use PySequence_Fast ops
799 2) extending self to self requires making a copy first
801 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
802 PyObject **src, **dest;
803 b = PySequence_Fast(b, "argument must be iterable");
804 if (!b)
805 return NULL;
806 n = PySequence_Fast_GET_SIZE(b);
807 if (n == 0) {
808 /* short circuit when b is empty */
809 Py_DECREF(b);
810 Py_RETURN_NONE;
812 m = Py_SIZE(self);
813 if (list_resize(self, m + n) == -1) {
814 Py_DECREF(b);
815 return NULL;
817 /* note that we may still have self == b here for the
818 * situation a.extend(a), but the following code works
819 * in that case too. Just make sure to resize self
820 * before calling PySequence_Fast_ITEMS.
822 /* populate the end of self with b's items */
823 src = PySequence_Fast_ITEMS(b);
824 dest = self->ob_item + m;
825 for (i = 0; i < n; i++) {
826 PyObject *o = src[i];
827 Py_INCREF(o);
828 dest[i] = o;
830 Py_DECREF(b);
831 Py_RETURN_NONE;
834 it = PyObject_GetIter(b);
835 if (it == NULL)
836 return NULL;
837 iternext = *it->ob_type->tp_iternext;
839 /* Guess a result list size. */
840 n = _PyObject_LengthHint(b, 8);
841 if (n == -1) {
842 Py_DECREF(it);
843 return NULL;
845 m = Py_SIZE(self);
846 mn = m + n;
847 if (mn >= m) {
848 /* Make room. */
849 if (list_resize(self, mn) == -1)
850 goto error;
851 /* Make the list sane again. */
852 Py_SIZE(self) = m;
854 /* Else m + n overflowed; on the chance that n lied, and there really
855 * is enough room, ignore it. If n was telling the truth, we'll
856 * eventually run out of memory during the loop.
859 /* Run iterator to exhaustion. */
860 for (;;) {
861 PyObject *item = iternext(it);
862 if (item == NULL) {
863 if (PyErr_Occurred()) {
864 if (PyErr_ExceptionMatches(PyExc_StopIteration))
865 PyErr_Clear();
866 else
867 goto error;
869 break;
871 if (Py_SIZE(self) < self->allocated) {
872 /* steals ref */
873 PyList_SET_ITEM(self, Py_SIZE(self), item);
874 ++Py_SIZE(self);
876 else {
877 int status = app1(self, item);
878 Py_DECREF(item); /* append creates a new ref */
879 if (status < 0)
880 goto error;
884 /* Cut back result list if initial guess was too large. */
885 if (Py_SIZE(self) < self->allocated)
886 list_resize(self, Py_SIZE(self)); /* shrinking can't fail */
888 Py_DECREF(it);
889 Py_RETURN_NONE;
891 error:
892 Py_DECREF(it);
893 return NULL;
896 PyObject *
897 _PyList_Extend(PyListObject *self, PyObject *b)
899 return listextend(self, b);
902 static PyObject *
903 list_inplace_concat(PyListObject *self, PyObject *other)
905 PyObject *result;
907 result = listextend(self, other);
908 if (result == NULL)
909 return result;
910 Py_DECREF(result);
911 Py_INCREF(self);
912 return (PyObject *)self;
915 static PyObject *
916 listpop(PyListObject *self, PyObject *args)
918 Py_ssize_t i = -1;
919 PyObject *v;
920 int status;
922 if (!PyArg_ParseTuple(args, "|n:pop", &i))
923 return NULL;
925 if (Py_SIZE(self) == 0) {
926 /* Special-case most common failure cause */
927 PyErr_SetString(PyExc_IndexError, "pop from empty list");
928 return NULL;
930 if (i < 0)
931 i += Py_SIZE(self);
932 if (i < 0 || i >= Py_SIZE(self)) {
933 PyErr_SetString(PyExc_IndexError, "pop index out of range");
934 return NULL;
936 v = self->ob_item[i];
937 if (i == Py_SIZE(self) - 1) {
938 status = list_resize(self, Py_SIZE(self) - 1);
939 assert(status >= 0);
940 return v; /* and v now owns the reference the list had */
942 Py_INCREF(v);
943 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
944 assert(status >= 0);
945 /* Use status, so that in a release build compilers don't
946 * complain about the unused name.
948 (void) status;
950 return v;
953 /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
954 static void
955 reverse_slice(PyObject **lo, PyObject **hi)
957 assert(lo && hi);
959 --hi;
960 while (lo < hi) {
961 PyObject *t = *lo;
962 *lo = *hi;
963 *hi = t;
964 ++lo;
965 --hi;
969 /* Lots of code for an adaptive, stable, natural mergesort. There are many
970 * pieces to this algorithm; read listsort.txt for overviews and details.
973 /* Comparison function. Takes care of calling a user-supplied
974 * comparison function (any callable Python object), which must not be
975 * NULL (use the ISLT macro if you don't know, or call PyObject_RichCompareBool
976 * with Py_LT if you know it's NULL).
977 * Returns -1 on error, 1 if x < y, 0 if x >= y.
979 static int
980 islt(PyObject *x, PyObject *y, PyObject *compare)
982 PyObject *res;
983 PyObject *args;
984 Py_ssize_t i;
986 assert(compare != NULL);
987 /* Call the user's comparison function and translate the 3-way
988 * result into true or false (or error).
990 args = PyTuple_New(2);
991 if (args == NULL)
992 return -1;
993 Py_INCREF(x);
994 Py_INCREF(y);
995 PyTuple_SET_ITEM(args, 0, x);
996 PyTuple_SET_ITEM(args, 1, y);
997 res = PyObject_Call(compare, args, NULL);
998 Py_DECREF(args);
999 if (res == NULL)
1000 return -1;
1001 if (!PyInt_Check(res)) {
1002 PyErr_Format(PyExc_TypeError,
1003 "comparison function must return int, not %.200s",
1004 res->ob_type->tp_name);
1005 Py_DECREF(res);
1006 return -1;
1008 i = PyInt_AsLong(res);
1009 Py_DECREF(res);
1010 return i < 0;
1013 /* If COMPARE is NULL, calls PyObject_RichCompareBool with Py_LT, else calls
1014 * islt. This avoids a layer of function call in the usual case, and
1015 * sorting does many comparisons.
1016 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1018 #define ISLT(X, Y, COMPARE) ((COMPARE) == NULL ? \
1019 PyObject_RichCompareBool(X, Y, Py_LT) : \
1020 islt(X, Y, COMPARE))
1022 /* Compare X to Y via "<". Goto "fail" if the comparison raises an
1023 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1024 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1026 #define IFLT(X, Y) if ((k = ISLT(X, Y, compare)) < 0) goto fail; \
1027 if (k)
1029 /* binarysort is the best method for sorting small arrays: it does
1030 few compares, but can do data movement quadratic in the number of
1031 elements.
1032 [lo, hi) is a contiguous slice of a list, and is sorted via
1033 binary insertion. This sort is stable.
1034 On entry, must have lo <= start <= hi, and that [lo, start) is already
1035 sorted (pass start == lo if you don't know!).
1036 If islt() complains return -1, else 0.
1037 Even in case of error, the output slice will be some permutation of
1038 the input (nothing is lost or duplicated).
1040 static int
1041 binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
1042 /* compare -- comparison function object, or NULL for default */
1044 register Py_ssize_t k;
1045 register PyObject **l, **p, **r;
1046 register PyObject *pivot;
1048 assert(lo <= start && start <= hi);
1049 /* assert [lo, start) is sorted */
1050 if (lo == start)
1051 ++start;
1052 for (; start < hi; ++start) {
1053 /* set l to where *start belongs */
1054 l = lo;
1055 r = start;
1056 pivot = *r;
1057 /* Invariants:
1058 * pivot >= all in [lo, l).
1059 * pivot < all in [r, start).
1060 * The second is vacuously true at the start.
1062 assert(l < r);
1063 do {
1064 p = l + ((r - l) >> 1);
1065 IFLT(pivot, *p)
1066 r = p;
1067 else
1068 l = p+1;
1069 } while (l < r);
1070 assert(l == r);
1071 /* The invariants still hold, so pivot >= all in [lo, l) and
1072 pivot < all in [l, start), so pivot belongs at l. Note
1073 that if there are elements equal to pivot, l points to the
1074 first slot after them -- that's why this sort is stable.
1075 Slide over to make room.
1076 Caution: using memmove is much slower under MSVC 5;
1077 we're not usually moving many slots. */
1078 for (p = start; p > l; --p)
1079 *p = *(p-1);
1080 *l = pivot;
1082 return 0;
1084 fail:
1085 return -1;
1089 Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1090 is required on entry. "A run" is the longest ascending sequence, with
1092 lo[0] <= lo[1] <= lo[2] <= ...
1094 or the longest descending sequence, with
1096 lo[0] > lo[1] > lo[2] > ...
1098 Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1099 For its intended use in a stable mergesort, the strictness of the defn of
1100 "descending" is needed so that the caller can safely reverse a descending
1101 sequence without violating stability (strict > ensures there are no equal
1102 elements to get out of order).
1104 Returns -1 in case of error.
1106 static Py_ssize_t
1107 count_run(PyObject **lo, PyObject **hi, PyObject *compare, int *descending)
1109 Py_ssize_t k;
1110 Py_ssize_t n;
1112 assert(lo < hi);
1113 *descending = 0;
1114 ++lo;
1115 if (lo == hi)
1116 return 1;
1118 n = 2;
1119 IFLT(*lo, *(lo-1)) {
1120 *descending = 1;
1121 for (lo = lo+1; lo < hi; ++lo, ++n) {
1122 IFLT(*lo, *(lo-1))
1124 else
1125 break;
1128 else {
1129 for (lo = lo+1; lo < hi; ++lo, ++n) {
1130 IFLT(*lo, *(lo-1))
1131 break;
1135 return n;
1136 fail:
1137 return -1;
1141 Locate the proper position of key in a sorted vector; if the vector contains
1142 an element equal to key, return the position immediately to the left of
1143 the leftmost equal element. [gallop_right() does the same except returns
1144 the position to the right of the rightmost equal element (if any).]
1146 "a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
1148 "hint" is an index at which to begin the search, 0 <= hint < n. The closer
1149 hint is to the final result, the faster this runs.
1151 The return value is the int k in 0..n such that
1153 a[k-1] < key <= a[k]
1155 pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1156 key belongs at index k; or, IOW, the first k elements of a should precede
1157 key, and the last n-k should follow key.
1159 Returns -1 on error. See listsort.txt for info on the method.
1161 static Py_ssize_t
1162 gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare)
1164 Py_ssize_t ofs;
1165 Py_ssize_t lastofs;
1166 Py_ssize_t k;
1168 assert(key && a && n > 0 && hint >= 0 && hint < n);
1170 a += hint;
1171 lastofs = 0;
1172 ofs = 1;
1173 IFLT(*a, key) {
1174 /* a[hint] < key -- gallop right, until
1175 * a[hint + lastofs] < key <= a[hint + ofs]
1177 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1178 while (ofs < maxofs) {
1179 IFLT(a[ofs], key) {
1180 lastofs = ofs;
1181 ofs = (ofs << 1) + 1;
1182 if (ofs <= 0) /* int overflow */
1183 ofs = maxofs;
1185 else /* key <= a[hint + ofs] */
1186 break;
1188 if (ofs > maxofs)
1189 ofs = maxofs;
1190 /* Translate back to offsets relative to &a[0]. */
1191 lastofs += hint;
1192 ofs += hint;
1194 else {
1195 /* key <= a[hint] -- gallop left, until
1196 * a[hint - ofs] < key <= a[hint - lastofs]
1198 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1199 while (ofs < maxofs) {
1200 IFLT(*(a-ofs), key)
1201 break;
1202 /* key <= a[hint - ofs] */
1203 lastofs = ofs;
1204 ofs = (ofs << 1) + 1;
1205 if (ofs <= 0) /* int overflow */
1206 ofs = maxofs;
1208 if (ofs > maxofs)
1209 ofs = maxofs;
1210 /* Translate back to positive offsets relative to &a[0]. */
1211 k = lastofs;
1212 lastofs = hint - ofs;
1213 ofs = hint - k;
1215 a -= hint;
1217 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1218 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1219 * right of lastofs but no farther right than ofs. Do a binary
1220 * search, with invariant a[lastofs-1] < key <= a[ofs].
1222 ++lastofs;
1223 while (lastofs < ofs) {
1224 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
1226 IFLT(a[m], key)
1227 lastofs = m+1; /* a[m] < key */
1228 else
1229 ofs = m; /* key <= a[m] */
1231 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1232 return ofs;
1234 fail:
1235 return -1;
1239 Exactly like gallop_left(), except that if key already exists in a[0:n],
1240 finds the position immediately to the right of the rightmost equal value.
1242 The return value is the int k in 0..n such that
1244 a[k-1] <= key < a[k]
1246 or -1 if error.
1248 The code duplication is massive, but this is enough different given that
1249 we're sticking to "<" comparisons that it's much harder to follow if
1250 written as one routine with yet another "left or right?" flag.
1252 static Py_ssize_t
1253 gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare)
1255 Py_ssize_t ofs;
1256 Py_ssize_t lastofs;
1257 Py_ssize_t k;
1259 assert(key && a && n > 0 && hint >= 0 && hint < n);
1261 a += hint;
1262 lastofs = 0;
1263 ofs = 1;
1264 IFLT(key, *a) {
1265 /* key < a[hint] -- gallop left, until
1266 * a[hint - ofs] <= key < a[hint - lastofs]
1268 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1269 while (ofs < maxofs) {
1270 IFLT(key, *(a-ofs)) {
1271 lastofs = ofs;
1272 ofs = (ofs << 1) + 1;
1273 if (ofs <= 0) /* int overflow */
1274 ofs = maxofs;
1276 else /* a[hint - ofs] <= key */
1277 break;
1279 if (ofs > maxofs)
1280 ofs = maxofs;
1281 /* Translate back to positive offsets relative to &a[0]. */
1282 k = lastofs;
1283 lastofs = hint - ofs;
1284 ofs = hint - k;
1286 else {
1287 /* a[hint] <= key -- gallop right, until
1288 * a[hint + lastofs] <= key < a[hint + ofs]
1290 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1291 while (ofs < maxofs) {
1292 IFLT(key, a[ofs])
1293 break;
1294 /* a[hint + ofs] <= key */
1295 lastofs = ofs;
1296 ofs = (ofs << 1) + 1;
1297 if (ofs <= 0) /* int overflow */
1298 ofs = maxofs;
1300 if (ofs > maxofs)
1301 ofs = maxofs;
1302 /* Translate back to offsets relative to &a[0]. */
1303 lastofs += hint;
1304 ofs += hint;
1306 a -= hint;
1308 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1309 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1310 * right of lastofs but no farther right than ofs. Do a binary
1311 * search, with invariant a[lastofs-1] <= key < a[ofs].
1313 ++lastofs;
1314 while (lastofs < ofs) {
1315 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
1317 IFLT(key, a[m])
1318 ofs = m; /* key < a[m] */
1319 else
1320 lastofs = m+1; /* a[m] <= key */
1322 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1323 return ofs;
1325 fail:
1326 return -1;
1329 /* The maximum number of entries in a MergeState's pending-runs stack.
1330 * This is enough to sort arrays of size up to about
1331 * 32 * phi ** MAX_MERGE_PENDING
1332 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1333 * with 2**64 elements.
1335 #define MAX_MERGE_PENDING 85
1337 /* When we get into galloping mode, we stay there until both runs win less
1338 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1340 #define MIN_GALLOP 7
1342 /* Avoid malloc for small temp arrays. */
1343 #define MERGESTATE_TEMP_SIZE 256
1345 /* One MergeState exists on the stack per invocation of mergesort. It's just
1346 * a convenient way to pass state around among the helper functions.
1348 struct s_slice {
1349 PyObject **base;
1350 Py_ssize_t len;
1353 typedef struct s_MergeState {
1354 /* The user-supplied comparison function. or NULL if none given. */
1355 PyObject *compare;
1357 /* This controls when we get *into* galloping mode. It's initialized
1358 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1359 * random data, and lower for highly structured data.
1361 Py_ssize_t min_gallop;
1363 /* 'a' is temp storage to help with merges. It contains room for
1364 * alloced entries.
1366 PyObject **a; /* may point to temparray below */
1367 Py_ssize_t alloced;
1369 /* A stack of n pending runs yet to be merged. Run #i starts at
1370 * address base[i] and extends for len[i] elements. It's always
1371 * true (so long as the indices are in bounds) that
1373 * pending[i].base + pending[i].len == pending[i+1].base
1375 * so we could cut the storage for this, but it's a minor amount,
1376 * and keeping all the info explicit simplifies the code.
1378 int n;
1379 struct s_slice pending[MAX_MERGE_PENDING];
1381 /* 'a' points to this when possible, rather than muck with malloc. */
1382 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1383 } MergeState;
1385 /* Conceptually a MergeState's constructor. */
1386 static void
1387 merge_init(MergeState *ms, PyObject *compare)
1389 assert(ms != NULL);
1390 ms->compare = compare;
1391 ms->a = ms->temparray;
1392 ms->alloced = MERGESTATE_TEMP_SIZE;
1393 ms->n = 0;
1394 ms->min_gallop = MIN_GALLOP;
1397 /* Free all the temp memory owned by the MergeState. This must be called
1398 * when you're done with a MergeState, and may be called before then if
1399 * you want to free the temp memory early.
1401 static void
1402 merge_freemem(MergeState *ms)
1404 assert(ms != NULL);
1405 if (ms->a != ms->temparray)
1406 PyMem_Free(ms->a);
1407 ms->a = ms->temparray;
1408 ms->alloced = MERGESTATE_TEMP_SIZE;
1411 /* Ensure enough temp memory for 'need' array slots is available.
1412 * Returns 0 on success and -1 if the memory can't be gotten.
1414 static int
1415 merge_getmem(MergeState *ms, Py_ssize_t need)
1417 assert(ms != NULL);
1418 if (need <= ms->alloced)
1419 return 0;
1420 /* Don't realloc! That can cost cycles to copy the old data, but
1421 * we don't care what's in the block.
1423 merge_freemem(ms);
1424 if (need > PY_SSIZE_T_MAX / sizeof(PyObject*)) {
1425 PyErr_NoMemory();
1426 return -1;
1428 ms->a = (PyObject **)PyMem_Malloc(need * sizeof(PyObject*));
1429 if (ms->a) {
1430 ms->alloced = need;
1431 return 0;
1433 PyErr_NoMemory();
1434 merge_freemem(ms); /* reset to sane state */
1435 return -1;
1437 #define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1438 merge_getmem(MS, NEED))
1440 /* Merge the na elements starting at pa with the nb elements starting at pb
1441 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
1442 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
1443 * merge, and should have na <= nb. See listsort.txt for more info.
1444 * Return 0 if successful, -1 if error.
1446 static Py_ssize_t
1447 merge_lo(MergeState *ms, PyObject **pa, Py_ssize_t na,
1448 PyObject **pb, Py_ssize_t nb)
1450 Py_ssize_t k;
1451 PyObject *compare;
1452 PyObject **dest;
1453 int result = -1; /* guilty until proved innocent */
1454 Py_ssize_t min_gallop;
1456 assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
1457 if (MERGE_GETMEM(ms, na) < 0)
1458 return -1;
1459 memcpy(ms->a, pa, na * sizeof(PyObject*));
1460 dest = pa;
1461 pa = ms->a;
1463 *dest++ = *pb++;
1464 --nb;
1465 if (nb == 0)
1466 goto Succeed;
1467 if (na == 1)
1468 goto CopyB;
1470 min_gallop = ms->min_gallop;
1471 compare = ms->compare;
1472 for (;;) {
1473 Py_ssize_t acount = 0; /* # of times A won in a row */
1474 Py_ssize_t bcount = 0; /* # of times B won in a row */
1476 /* Do the straightforward thing until (if ever) one run
1477 * appears to win consistently.
1479 for (;;) {
1480 assert(na > 1 && nb > 0);
1481 k = ISLT(*pb, *pa, compare);
1482 if (k) {
1483 if (k < 0)
1484 goto Fail;
1485 *dest++ = *pb++;
1486 ++bcount;
1487 acount = 0;
1488 --nb;
1489 if (nb == 0)
1490 goto Succeed;
1491 if (bcount >= min_gallop)
1492 break;
1494 else {
1495 *dest++ = *pa++;
1496 ++acount;
1497 bcount = 0;
1498 --na;
1499 if (na == 1)
1500 goto CopyB;
1501 if (acount >= min_gallop)
1502 break;
1506 /* One run is winning so consistently that galloping may
1507 * be a huge win. So try that, and continue galloping until
1508 * (if ever) neither run appears to be winning consistently
1509 * anymore.
1511 ++min_gallop;
1512 do {
1513 assert(na > 1 && nb > 0);
1514 min_gallop -= min_gallop > 1;
1515 ms->min_gallop = min_gallop;
1516 k = gallop_right(*pb, pa, na, 0, compare);
1517 acount = k;
1518 if (k) {
1519 if (k < 0)
1520 goto Fail;
1521 memcpy(dest, pa, k * sizeof(PyObject *));
1522 dest += k;
1523 pa += k;
1524 na -= k;
1525 if (na == 1)
1526 goto CopyB;
1527 /* na==0 is impossible now if the comparison
1528 * function is consistent, but we can't assume
1529 * that it is.
1531 if (na == 0)
1532 goto Succeed;
1534 *dest++ = *pb++;
1535 --nb;
1536 if (nb == 0)
1537 goto Succeed;
1539 k = gallop_left(*pa, pb, nb, 0, compare);
1540 bcount = k;
1541 if (k) {
1542 if (k < 0)
1543 goto Fail;
1544 memmove(dest, pb, k * sizeof(PyObject *));
1545 dest += k;
1546 pb += k;
1547 nb -= k;
1548 if (nb == 0)
1549 goto Succeed;
1551 *dest++ = *pa++;
1552 --na;
1553 if (na == 1)
1554 goto CopyB;
1555 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1556 ++min_gallop; /* penalize it for leaving galloping mode */
1557 ms->min_gallop = min_gallop;
1559 Succeed:
1560 result = 0;
1561 Fail:
1562 if (na)
1563 memcpy(dest, pa, na * sizeof(PyObject*));
1564 return result;
1565 CopyB:
1566 assert(na == 1 && nb > 0);
1567 /* The last element of pa belongs at the end of the merge. */
1568 memmove(dest, pb, nb * sizeof(PyObject *));
1569 dest[nb] = *pa;
1570 return 0;
1573 /* Merge the na elements starting at pa with the nb elements starting at pb
1574 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
1575 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
1576 * merge, and should have na >= nb. See listsort.txt for more info.
1577 * Return 0 if successful, -1 if error.
1579 static Py_ssize_t
1580 merge_hi(MergeState *ms, PyObject **pa, Py_ssize_t na, PyObject **pb, Py_ssize_t nb)
1582 Py_ssize_t k;
1583 PyObject *compare;
1584 PyObject **dest;
1585 int result = -1; /* guilty until proved innocent */
1586 PyObject **basea;
1587 PyObject **baseb;
1588 Py_ssize_t min_gallop;
1590 assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
1591 if (MERGE_GETMEM(ms, nb) < 0)
1592 return -1;
1593 dest = pb + nb - 1;
1594 memcpy(ms->a, pb, nb * sizeof(PyObject*));
1595 basea = pa;
1596 baseb = ms->a;
1597 pb = ms->a + nb - 1;
1598 pa += na - 1;
1600 *dest-- = *pa--;
1601 --na;
1602 if (na == 0)
1603 goto Succeed;
1604 if (nb == 1)
1605 goto CopyA;
1607 min_gallop = ms->min_gallop;
1608 compare = ms->compare;
1609 for (;;) {
1610 Py_ssize_t acount = 0; /* # of times A won in a row */
1611 Py_ssize_t bcount = 0; /* # of times B won in a row */
1613 /* Do the straightforward thing until (if ever) one run
1614 * appears to win consistently.
1616 for (;;) {
1617 assert(na > 0 && nb > 1);
1618 k = ISLT(*pb, *pa, compare);
1619 if (k) {
1620 if (k < 0)
1621 goto Fail;
1622 *dest-- = *pa--;
1623 ++acount;
1624 bcount = 0;
1625 --na;
1626 if (na == 0)
1627 goto Succeed;
1628 if (acount >= min_gallop)
1629 break;
1631 else {
1632 *dest-- = *pb--;
1633 ++bcount;
1634 acount = 0;
1635 --nb;
1636 if (nb == 1)
1637 goto CopyA;
1638 if (bcount >= min_gallop)
1639 break;
1643 /* One run is winning so consistently that galloping may
1644 * be a huge win. So try that, and continue galloping until
1645 * (if ever) neither run appears to be winning consistently
1646 * anymore.
1648 ++min_gallop;
1649 do {
1650 assert(na > 0 && nb > 1);
1651 min_gallop -= min_gallop > 1;
1652 ms->min_gallop = min_gallop;
1653 k = gallop_right(*pb, basea, na, na-1, compare);
1654 if (k < 0)
1655 goto Fail;
1656 k = na - k;
1657 acount = k;
1658 if (k) {
1659 dest -= k;
1660 pa -= k;
1661 memmove(dest+1, pa+1, k * sizeof(PyObject *));
1662 na -= k;
1663 if (na == 0)
1664 goto Succeed;
1666 *dest-- = *pb--;
1667 --nb;
1668 if (nb == 1)
1669 goto CopyA;
1671 k = gallop_left(*pa, baseb, nb, nb-1, compare);
1672 if (k < 0)
1673 goto Fail;
1674 k = nb - k;
1675 bcount = k;
1676 if (k) {
1677 dest -= k;
1678 pb -= k;
1679 memcpy(dest+1, pb+1, k * sizeof(PyObject *));
1680 nb -= k;
1681 if (nb == 1)
1682 goto CopyA;
1683 /* nb==0 is impossible now if the comparison
1684 * function is consistent, but we can't assume
1685 * that it is.
1687 if (nb == 0)
1688 goto Succeed;
1690 *dest-- = *pa--;
1691 --na;
1692 if (na == 0)
1693 goto Succeed;
1694 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1695 ++min_gallop; /* penalize it for leaving galloping mode */
1696 ms->min_gallop = min_gallop;
1698 Succeed:
1699 result = 0;
1700 Fail:
1701 if (nb)
1702 memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*));
1703 return result;
1704 CopyA:
1705 assert(nb == 1 && na > 0);
1706 /* The first element of pb belongs at the front of the merge. */
1707 dest -= na;
1708 pa -= na;
1709 memmove(dest+1, pa+1, na * sizeof(PyObject *));
1710 *dest = *pb;
1711 return 0;
1714 /* Merge the two runs at stack indices i and i+1.
1715 * Returns 0 on success, -1 on error.
1717 static Py_ssize_t
1718 merge_at(MergeState *ms, Py_ssize_t i)
1720 PyObject **pa, **pb;
1721 Py_ssize_t na, nb;
1722 Py_ssize_t k;
1723 PyObject *compare;
1725 assert(ms != NULL);
1726 assert(ms->n >= 2);
1727 assert(i >= 0);
1728 assert(i == ms->n - 2 || i == ms->n - 3);
1730 pa = ms->pending[i].base;
1731 na = ms->pending[i].len;
1732 pb = ms->pending[i+1].base;
1733 nb = ms->pending[i+1].len;
1734 assert(na > 0 && nb > 0);
1735 assert(pa + na == pb);
1737 /* Record the length of the combined runs; if i is the 3rd-last
1738 * run now, also slide over the last run (which isn't involved
1739 * in this merge). The current run i+1 goes away in any case.
1741 ms->pending[i].len = na + nb;
1742 if (i == ms->n - 3)
1743 ms->pending[i+1] = ms->pending[i+2];
1744 --ms->n;
1746 /* Where does b start in a? Elements in a before that can be
1747 * ignored (already in place).
1749 compare = ms->compare;
1750 k = gallop_right(*pb, pa, na, 0, compare);
1751 if (k < 0)
1752 return -1;
1753 pa += k;
1754 na -= k;
1755 if (na == 0)
1756 return 0;
1758 /* Where does a end in b? Elements in b after that can be
1759 * ignored (already in place).
1761 nb = gallop_left(pa[na-1], pb, nb, nb-1, compare);
1762 if (nb <= 0)
1763 return nb;
1765 /* Merge what remains of the runs, using a temp array with
1766 * min(na, nb) elements.
1768 if (na <= nb)
1769 return merge_lo(ms, pa, na, pb, nb);
1770 else
1771 return merge_hi(ms, pa, na, pb, nb);
1774 /* Examine the stack of runs waiting to be merged, merging adjacent runs
1775 * until the stack invariants are re-established:
1777 * 1. len[-3] > len[-2] + len[-1]
1778 * 2. len[-2] > len[-1]
1780 * See listsort.txt for more info.
1782 * Returns 0 on success, -1 on error.
1784 static int
1785 merge_collapse(MergeState *ms)
1787 struct s_slice *p = ms->pending;
1789 assert(ms);
1790 while (ms->n > 1) {
1791 Py_ssize_t n = ms->n - 2;
1792 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
1793 if (p[n-1].len < p[n+1].len)
1794 --n;
1795 if (merge_at(ms, n) < 0)
1796 return -1;
1798 else if (p[n].len <= p[n+1].len) {
1799 if (merge_at(ms, n) < 0)
1800 return -1;
1802 else
1803 break;
1805 return 0;
1808 /* Regardless of invariants, merge all runs on the stack until only one
1809 * remains. This is used at the end of the mergesort.
1811 * Returns 0 on success, -1 on error.
1813 static int
1814 merge_force_collapse(MergeState *ms)
1816 struct s_slice *p = ms->pending;
1818 assert(ms);
1819 while (ms->n > 1) {
1820 Py_ssize_t n = ms->n - 2;
1821 if (n > 0 && p[n-1].len < p[n+1].len)
1822 --n;
1823 if (merge_at(ms, n) < 0)
1824 return -1;
1826 return 0;
1829 /* Compute a good value for the minimum run length; natural runs shorter
1830 * than this are boosted artificially via binary insertion.
1832 * If n < 64, return n (it's too small to bother with fancy stuff).
1833 * Else if n is an exact power of 2, return 32.
1834 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1835 * strictly less than, an exact power of 2.
1837 * See listsort.txt for more info.
1839 static Py_ssize_t
1840 merge_compute_minrun(Py_ssize_t n)
1842 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
1844 assert(n >= 0);
1845 while (n >= 64) {
1846 r |= n & 1;
1847 n >>= 1;
1849 return n + r;
1852 /* Special wrapper to support stable sorting using the decorate-sort-undecorate
1853 pattern. Holds a key which is used for comparisons and the original record
1854 which is returned during the undecorate phase. By exposing only the key
1855 during comparisons, the underlying sort stability characteristics are left
1856 unchanged. Also, if a custom comparison function is used, it will only see
1857 the key instead of a full record. */
1859 typedef struct {
1860 PyObject_HEAD
1861 PyObject *key;
1862 PyObject *value;
1863 } sortwrapperobject;
1865 PyDoc_STRVAR(sortwrapper_doc, "Object wrapper with a custom sort key.");
1866 static PyObject *
1867 sortwrapper_richcompare(sortwrapperobject *, sortwrapperobject *, int);
1868 static void
1869 sortwrapper_dealloc(sortwrapperobject *);
1871 static PyTypeObject sortwrapper_type = {
1872 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1873 "sortwrapper", /* tp_name */
1874 sizeof(sortwrapperobject), /* tp_basicsize */
1875 0, /* tp_itemsize */
1876 /* methods */
1877 (destructor)sortwrapper_dealloc, /* tp_dealloc */
1878 0, /* tp_print */
1879 0, /* tp_getattr */
1880 0, /* tp_setattr */
1881 0, /* tp_compare */
1882 0, /* tp_repr */
1883 0, /* tp_as_number */
1884 0, /* tp_as_sequence */
1885 0, /* tp_as_mapping */
1886 0, /* tp_hash */
1887 0, /* tp_call */
1888 0, /* tp_str */
1889 PyObject_GenericGetAttr, /* tp_getattro */
1890 0, /* tp_setattro */
1891 0, /* tp_as_buffer */
1892 Py_TPFLAGS_DEFAULT |
1893 Py_TPFLAGS_HAVE_RICHCOMPARE, /* tp_flags */
1894 sortwrapper_doc, /* tp_doc */
1895 0, /* tp_traverse */
1896 0, /* tp_clear */
1897 (richcmpfunc)sortwrapper_richcompare, /* tp_richcompare */
1901 static PyObject *
1902 sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, int op)
1904 if (!PyObject_TypeCheck(b, &sortwrapper_type)) {
1905 PyErr_SetString(PyExc_TypeError,
1906 "expected a sortwrapperobject");
1907 return NULL;
1909 return PyObject_RichCompare(a->key, b->key, op);
1912 static void
1913 sortwrapper_dealloc(sortwrapperobject *so)
1915 Py_XDECREF(so->key);
1916 Py_XDECREF(so->value);
1917 PyObject_Del(so);
1920 /* Returns a new reference to a sortwrapper.
1921 Consumes the references to the two underlying objects. */
1923 static PyObject *
1924 build_sortwrapper(PyObject *key, PyObject *value)
1926 sortwrapperobject *so;
1928 so = PyObject_New(sortwrapperobject, &sortwrapper_type);
1929 if (so == NULL)
1930 return NULL;
1931 so->key = key;
1932 so->value = value;
1933 return (PyObject *)so;
1936 /* Returns a new reference to the value underlying the wrapper. */
1937 static PyObject *
1938 sortwrapper_getvalue(PyObject *so)
1940 PyObject *value;
1942 if (!PyObject_TypeCheck(so, &sortwrapper_type)) {
1943 PyErr_SetString(PyExc_TypeError,
1944 "expected a sortwrapperobject");
1945 return NULL;
1947 value = ((sortwrapperobject *)so)->value;
1948 Py_INCREF(value);
1949 return value;
1952 /* Wrapper for user specified cmp functions in combination with a
1953 specified key function. Makes sure the cmp function is presented
1954 with the actual key instead of the sortwrapper */
1956 typedef struct {
1957 PyObject_HEAD
1958 PyObject *func;
1959 } cmpwrapperobject;
1961 static void
1962 cmpwrapper_dealloc(cmpwrapperobject *co)
1964 Py_XDECREF(co->func);
1965 PyObject_Del(co);
1968 static PyObject *
1969 cmpwrapper_call(cmpwrapperobject *co, PyObject *args, PyObject *kwds)
1971 PyObject *x, *y, *xx, *yy;
1973 if (!PyArg_UnpackTuple(args, "", 2, 2, &x, &y))
1974 return NULL;
1975 if (!PyObject_TypeCheck(x, &sortwrapper_type) ||
1976 !PyObject_TypeCheck(y, &sortwrapper_type)) {
1977 PyErr_SetString(PyExc_TypeError,
1978 "expected a sortwrapperobject");
1979 return NULL;
1981 xx = ((sortwrapperobject *)x)->key;
1982 yy = ((sortwrapperobject *)y)->key;
1983 return PyObject_CallFunctionObjArgs(co->func, xx, yy, NULL);
1986 PyDoc_STRVAR(cmpwrapper_doc, "cmp() wrapper for sort with custom keys.");
1988 static PyTypeObject cmpwrapper_type = {
1989 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1990 "cmpwrapper", /* tp_name */
1991 sizeof(cmpwrapperobject), /* tp_basicsize */
1992 0, /* tp_itemsize */
1993 /* methods */
1994 (destructor)cmpwrapper_dealloc, /* tp_dealloc */
1995 0, /* tp_print */
1996 0, /* tp_getattr */
1997 0, /* tp_setattr */
1998 0, /* tp_compare */
1999 0, /* tp_repr */
2000 0, /* tp_as_number */
2001 0, /* tp_as_sequence */
2002 0, /* tp_as_mapping */
2003 0, /* tp_hash */
2004 (ternaryfunc)cmpwrapper_call, /* tp_call */
2005 0, /* tp_str */
2006 PyObject_GenericGetAttr, /* tp_getattro */
2007 0, /* tp_setattro */
2008 0, /* tp_as_buffer */
2009 Py_TPFLAGS_DEFAULT, /* tp_flags */
2010 cmpwrapper_doc, /* tp_doc */
2013 static PyObject *
2014 build_cmpwrapper(PyObject *cmpfunc)
2016 cmpwrapperobject *co;
2018 co = PyObject_New(cmpwrapperobject, &cmpwrapper_type);
2019 if (co == NULL)
2020 return NULL;
2021 Py_INCREF(cmpfunc);
2022 co->func = cmpfunc;
2023 return (PyObject *)co;
2026 /* An adaptive, stable, natural mergesort. See listsort.txt.
2027 * Returns Py_None on success, NULL on error. Even in case of error, the
2028 * list will be some permutation of its input state (nothing is lost or
2029 * duplicated).
2031 static PyObject *
2032 listsort(PyListObject *self, PyObject *args, PyObject *kwds)
2034 MergeState ms;
2035 PyObject **lo, **hi;
2036 Py_ssize_t nremaining;
2037 Py_ssize_t minrun;
2038 Py_ssize_t saved_ob_size, saved_allocated;
2039 PyObject **saved_ob_item;
2040 PyObject **final_ob_item;
2041 PyObject *compare = NULL;
2042 PyObject *result = NULL; /* guilty until proved innocent */
2043 int reverse = 0;
2044 PyObject *keyfunc = NULL;
2045 Py_ssize_t i;
2046 PyObject *key, *value, *kvpair;
2047 static char *kwlist[] = {"cmp", "key", "reverse", 0};
2049 assert(self != NULL);
2050 assert (PyList_Check(self));
2051 if (args != NULL) {
2052 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi:sort",
2053 kwlist, &compare, &keyfunc, &reverse))
2054 return NULL;
2056 if (compare == Py_None)
2057 compare = NULL;
2058 if (compare != NULL &&
2059 PyErr_WarnPy3k("the cmp argument is not supported in 3.x", 1) < 0)
2060 return NULL;
2061 if (keyfunc == Py_None)
2062 keyfunc = NULL;
2063 if (compare != NULL && keyfunc != NULL) {
2064 compare = build_cmpwrapper(compare);
2065 if (compare == NULL)
2066 return NULL;
2067 } else
2068 Py_XINCREF(compare);
2070 /* The list is temporarily made empty, so that mutations performed
2071 * by comparison functions can't affect the slice of memory we're
2072 * sorting (allowing mutations during sorting is a core-dump
2073 * factory, since ob_item may change).
2075 saved_ob_size = Py_SIZE(self);
2076 saved_ob_item = self->ob_item;
2077 saved_allocated = self->allocated;
2078 Py_SIZE(self) = 0;
2079 self->ob_item = NULL;
2080 self->allocated = -1; /* any operation will reset it to >= 0 */
2082 if (keyfunc != NULL) {
2083 for (i=0 ; i < saved_ob_size ; i++) {
2084 value = saved_ob_item[i];
2085 key = PyObject_CallFunctionObjArgs(keyfunc, value,
2086 NULL);
2087 if (key == NULL) {
2088 for (i=i-1 ; i>=0 ; i--) {
2089 kvpair = saved_ob_item[i];
2090 value = sortwrapper_getvalue(kvpair);
2091 saved_ob_item[i] = value;
2092 Py_DECREF(kvpair);
2094 goto dsu_fail;
2096 kvpair = build_sortwrapper(key, value);
2097 if (kvpair == NULL)
2098 goto dsu_fail;
2099 saved_ob_item[i] = kvpair;
2103 /* Reverse sort stability achieved by initially reversing the list,
2104 applying a stable forward sort, then reversing the final result. */
2105 if (reverse && saved_ob_size > 1)
2106 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
2108 merge_init(&ms, compare);
2110 nremaining = saved_ob_size;
2111 if (nremaining < 2)
2112 goto succeed;
2114 /* March over the array once, left to right, finding natural runs,
2115 * and extending short natural runs to minrun elements.
2117 lo = saved_ob_item;
2118 hi = lo + nremaining;
2119 minrun = merge_compute_minrun(nremaining);
2120 do {
2121 int descending;
2122 Py_ssize_t n;
2124 /* Identify next run. */
2125 n = count_run(lo, hi, compare, &descending);
2126 if (n < 0)
2127 goto fail;
2128 if (descending)
2129 reverse_slice(lo, lo + n);
2130 /* If short, extend to min(minrun, nremaining). */
2131 if (n < minrun) {
2132 const Py_ssize_t force = nremaining <= minrun ?
2133 nremaining : minrun;
2134 if (binarysort(lo, lo + force, lo + n, compare) < 0)
2135 goto fail;
2136 n = force;
2138 /* Push run onto pending-runs stack, and maybe merge. */
2139 assert(ms.n < MAX_MERGE_PENDING);
2140 ms.pending[ms.n].base = lo;
2141 ms.pending[ms.n].len = n;
2142 ++ms.n;
2143 if (merge_collapse(&ms) < 0)
2144 goto fail;
2145 /* Advance to find next run. */
2146 lo += n;
2147 nremaining -= n;
2148 } while (nremaining);
2149 assert(lo == hi);
2151 if (merge_force_collapse(&ms) < 0)
2152 goto fail;
2153 assert(ms.n == 1);
2154 assert(ms.pending[0].base == saved_ob_item);
2155 assert(ms.pending[0].len == saved_ob_size);
2157 succeed:
2158 result = Py_None;
2159 fail:
2160 if (keyfunc != NULL) {
2161 for (i=0 ; i < saved_ob_size ; i++) {
2162 kvpair = saved_ob_item[i];
2163 value = sortwrapper_getvalue(kvpair);
2164 saved_ob_item[i] = value;
2165 Py_DECREF(kvpair);
2169 if (self->allocated != -1 && result != NULL) {
2170 /* The user mucked with the list during the sort,
2171 * and we don't already have another error to report.
2173 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2174 result = NULL;
2177 if (reverse && saved_ob_size > 1)
2178 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
2180 merge_freemem(&ms);
2182 dsu_fail:
2183 final_ob_item = self->ob_item;
2184 i = Py_SIZE(self);
2185 Py_SIZE(self) = saved_ob_size;
2186 self->ob_item = saved_ob_item;
2187 self->allocated = saved_allocated;
2188 if (final_ob_item != NULL) {
2189 /* we cannot use list_clear() for this because it does not
2190 guarantee that the list is really empty when it returns */
2191 while (--i >= 0) {
2192 Py_XDECREF(final_ob_item[i]);
2194 PyMem_FREE(final_ob_item);
2196 Py_XDECREF(compare);
2197 Py_XINCREF(result);
2198 return result;
2200 #undef IFLT
2201 #undef ISLT
2204 PyList_Sort(PyObject *v)
2206 if (v == NULL || !PyList_Check(v)) {
2207 PyErr_BadInternalCall();
2208 return -1;
2210 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
2211 if (v == NULL)
2212 return -1;
2213 Py_DECREF(v);
2214 return 0;
2217 static PyObject *
2218 listreverse(PyListObject *self)
2220 if (Py_SIZE(self) > 1)
2221 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2222 Py_RETURN_NONE;
2226 PyList_Reverse(PyObject *v)
2228 PyListObject *self = (PyListObject *)v;
2230 if (v == NULL || !PyList_Check(v)) {
2231 PyErr_BadInternalCall();
2232 return -1;
2234 if (Py_SIZE(self) > 1)
2235 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2236 return 0;
2239 PyObject *
2240 PyList_AsTuple(PyObject *v)
2242 PyObject *w;
2243 PyObject **p, **q;
2244 Py_ssize_t n;
2245 if (v == NULL || !PyList_Check(v)) {
2246 PyErr_BadInternalCall();
2247 return NULL;
2249 n = Py_SIZE(v);
2250 w = PyTuple_New(n);
2251 if (w == NULL)
2252 return NULL;
2253 p = ((PyTupleObject *)w)->ob_item;
2254 q = ((PyListObject *)v)->ob_item;
2255 while (--n >= 0) {
2256 Py_INCREF(*q);
2257 *p = *q;
2258 p++;
2259 q++;
2261 return w;
2264 static PyObject *
2265 listindex(PyListObject *self, PyObject *args)
2267 Py_ssize_t i, start=0, stop=Py_SIZE(self);
2268 PyObject *v;
2270 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2271 _PyEval_SliceIndex, &start,
2272 _PyEval_SliceIndex, &stop))
2273 return NULL;
2274 if (start < 0) {
2275 start += Py_SIZE(self);
2276 if (start < 0)
2277 start = 0;
2279 if (stop < 0) {
2280 stop += Py_SIZE(self);
2281 if (stop < 0)
2282 stop = 0;
2284 for (i = start; i < stop && i < Py_SIZE(self); i++) {
2285 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2286 if (cmp > 0)
2287 return PyInt_FromSsize_t(i);
2288 else if (cmp < 0)
2289 return NULL;
2291 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
2292 return NULL;
2295 static PyObject *
2296 listcount(PyListObject *self, PyObject *v)
2298 Py_ssize_t count = 0;
2299 Py_ssize_t i;
2301 for (i = 0; i < Py_SIZE(self); i++) {
2302 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2303 if (cmp > 0)
2304 count++;
2305 else if (cmp < 0)
2306 return NULL;
2308 return PyInt_FromSsize_t(count);
2311 static PyObject *
2312 listremove(PyListObject *self, PyObject *v)
2314 Py_ssize_t i;
2316 for (i = 0; i < Py_SIZE(self); i++) {
2317 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2318 if (cmp > 0) {
2319 if (list_ass_slice(self, i, i+1,
2320 (PyObject *)NULL) == 0)
2321 Py_RETURN_NONE;
2322 return NULL;
2324 else if (cmp < 0)
2325 return NULL;
2327 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2328 return NULL;
2331 static int
2332 list_traverse(PyListObject *o, visitproc visit, void *arg)
2334 Py_ssize_t i;
2336 for (i = Py_SIZE(o); --i >= 0; )
2337 Py_VISIT(o->ob_item[i]);
2338 return 0;
2341 static PyObject *
2342 list_richcompare(PyObject *v, PyObject *w, int op)
2344 PyListObject *vl, *wl;
2345 Py_ssize_t i;
2347 if (!PyList_Check(v) || !PyList_Check(w)) {
2348 Py_INCREF(Py_NotImplemented);
2349 return Py_NotImplemented;
2352 vl = (PyListObject *)v;
2353 wl = (PyListObject *)w;
2355 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2356 /* Shortcut: if the lengths differ, the lists differ */
2357 PyObject *res;
2358 if (op == Py_EQ)
2359 res = Py_False;
2360 else
2361 res = Py_True;
2362 Py_INCREF(res);
2363 return res;
2366 /* Search for the first index where items are different */
2367 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2368 int k = PyObject_RichCompareBool(vl->ob_item[i],
2369 wl->ob_item[i], Py_EQ);
2370 if (k < 0)
2371 return NULL;
2372 if (!k)
2373 break;
2376 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2377 /* No more items to compare -- compare sizes */
2378 Py_ssize_t vs = Py_SIZE(vl);
2379 Py_ssize_t ws = Py_SIZE(wl);
2380 int cmp;
2381 PyObject *res;
2382 switch (op) {
2383 case Py_LT: cmp = vs < ws; break;
2384 case Py_LE: cmp = vs <= ws; break;
2385 case Py_EQ: cmp = vs == ws; break;
2386 case Py_NE: cmp = vs != ws; break;
2387 case Py_GT: cmp = vs > ws; break;
2388 case Py_GE: cmp = vs >= ws; break;
2389 default: return NULL; /* cannot happen */
2391 if (cmp)
2392 res = Py_True;
2393 else
2394 res = Py_False;
2395 Py_INCREF(res);
2396 return res;
2399 /* We have an item that differs -- shortcuts for EQ/NE */
2400 if (op == Py_EQ) {
2401 Py_INCREF(Py_False);
2402 return Py_False;
2404 if (op == Py_NE) {
2405 Py_INCREF(Py_True);
2406 return Py_True;
2409 /* Compare the final item again using the proper operator */
2410 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
2413 static int
2414 list_init(PyListObject *self, PyObject *args, PyObject *kw)
2416 PyObject *arg = NULL;
2417 static char *kwlist[] = {"sequence", 0};
2419 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2420 return -1;
2422 /* Verify list invariants established by PyType_GenericAlloc() */
2423 assert(0 <= Py_SIZE(self));
2424 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2425 assert(self->ob_item != NULL ||
2426 self->allocated == 0 || self->allocated == -1);
2428 /* Empty previous contents */
2429 if (self->ob_item != NULL) {
2430 (void)list_clear(self);
2432 if (arg != NULL) {
2433 PyObject *rv = listextend(self, arg);
2434 if (rv == NULL)
2435 return -1;
2436 Py_DECREF(rv);
2438 return 0;
2441 static PyObject *
2442 list_sizeof(PyListObject *self)
2444 Py_ssize_t res;
2446 res = sizeof(PyListObject) + self->allocated * sizeof(void*);
2447 return PyInt_FromSsize_t(res);
2450 static PyObject *list_iter(PyObject *seq);
2451 static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2453 PyDoc_STRVAR(getitem_doc,
2454 "x.__getitem__(y) <==> x[y]");
2455 PyDoc_STRVAR(reversed_doc,
2456 "L.__reversed__() -- return a reverse iterator over the list");
2457 PyDoc_STRVAR(sizeof_doc,
2458 "L.__sizeof__() -- size of L in memory, in bytes");
2459 PyDoc_STRVAR(append_doc,
2460 "L.append(object) -- append object to end");
2461 PyDoc_STRVAR(extend_doc,
2462 "L.extend(iterable) -- extend list by appending elements from the iterable");
2463 PyDoc_STRVAR(insert_doc,
2464 "L.insert(index, object) -- insert object before index");
2465 PyDoc_STRVAR(pop_doc,
2466 "L.pop([index]) -> item -- remove and return item at index (default last).\n"
2467 "Raises IndexError if list is empty or index is out of range.");
2468 PyDoc_STRVAR(remove_doc,
2469 "L.remove(value) -- remove first occurrence of value.\n"
2470 "Raises ValueError if the value is not present.");
2471 PyDoc_STRVAR(index_doc,
2472 "L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2473 "Raises ValueError if the value is not present.");
2474 PyDoc_STRVAR(count_doc,
2475 "L.count(value) -> integer -- return number of occurrences of value");
2476 PyDoc_STRVAR(reverse_doc,
2477 "L.reverse() -- reverse *IN PLACE*");
2478 PyDoc_STRVAR(sort_doc,
2479 "L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\n\
2480 cmp(x, y) -> -1, 0, 1");
2482 static PyObject *list_subscript(PyListObject*, PyObject*);
2484 static PyMethodDef list_methods[] = {
2485 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
2486 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
2487 {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
2488 {"append", (PyCFunction)listappend, METH_O, append_doc},
2489 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
2490 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
2491 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
2492 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
2493 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
2494 {"count", (PyCFunction)listcount, METH_O, count_doc},
2495 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
2496 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
2497 {NULL, NULL} /* sentinel */
2500 static PySequenceMethods list_as_sequence = {
2501 (lenfunc)list_length, /* sq_length */
2502 (binaryfunc)list_concat, /* sq_concat */
2503 (ssizeargfunc)list_repeat, /* sq_repeat */
2504 (ssizeargfunc)list_item, /* sq_item */
2505 (ssizessizeargfunc)list_slice, /* sq_slice */
2506 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2507 (ssizessizeobjargproc)list_ass_slice, /* sq_ass_slice */
2508 (objobjproc)list_contains, /* sq_contains */
2509 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2510 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
2513 PyDoc_STRVAR(list_doc,
2514 "list() -> new list\n"
2515 "list(sequence) -> new list initialized from sequence's items");
2518 static PyObject *
2519 list_subscript(PyListObject* self, PyObject* item)
2521 if (PyIndex_Check(item)) {
2522 Py_ssize_t i;
2523 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2524 if (i == -1 && PyErr_Occurred())
2525 return NULL;
2526 if (i < 0)
2527 i += PyList_GET_SIZE(self);
2528 return list_item(self, i);
2530 else if (PySlice_Check(item)) {
2531 Py_ssize_t start, stop, step, slicelength, cur, i;
2532 PyObject* result;
2533 PyObject* it;
2534 PyObject **src, **dest;
2536 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
2537 &start, &stop, &step, &slicelength) < 0) {
2538 return NULL;
2541 if (slicelength <= 0) {
2542 return PyList_New(0);
2544 else if (step == 1) {
2545 return list_slice(self, start, stop);
2547 else {
2548 result = PyList_New(slicelength);
2549 if (!result) return NULL;
2551 src = self->ob_item;
2552 dest = ((PyListObject *)result)->ob_item;
2553 for (cur = start, i = 0; i < slicelength;
2554 cur += step, i++) {
2555 it = src[cur];
2556 Py_INCREF(it);
2557 dest[i] = it;
2560 return result;
2563 else {
2564 PyErr_Format(PyExc_TypeError,
2565 "list indices must be integers, not %.200s",
2566 item->ob_type->tp_name);
2567 return NULL;
2571 static int
2572 list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2574 if (PyIndex_Check(item)) {
2575 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2576 if (i == -1 && PyErr_Occurred())
2577 return -1;
2578 if (i < 0)
2579 i += PyList_GET_SIZE(self);
2580 return list_ass_item(self, i, value);
2582 else if (PySlice_Check(item)) {
2583 Py_ssize_t start, stop, step, slicelength;
2585 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
2586 &start, &stop, &step, &slicelength) < 0) {
2587 return -1;
2590 if (step == 1)
2591 return list_ass_slice(self, start, stop, value);
2593 /* Make sure s[5:2] = [..] inserts at the right place:
2594 before 5, not before 2. */
2595 if ((step < 0 && start < stop) ||
2596 (step > 0 && start > stop))
2597 stop = start;
2599 if (value == NULL) {
2600 /* delete slice */
2601 PyObject **garbage;
2602 Py_ssize_t cur, i;
2604 if (slicelength <= 0)
2605 return 0;
2607 if (step < 0) {
2608 stop = start + 1;
2609 start = stop + step*(slicelength - 1) - 1;
2610 step = -step;
2613 assert(slicelength <= PY_SIZE_MAX / sizeof(PyObject*));
2615 garbage = (PyObject**)
2616 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2617 if (!garbage) {
2618 PyErr_NoMemory();
2619 return -1;
2622 /* drawing pictures might help understand these for
2623 loops. Basically, we memmove the parts of the
2624 list that are *not* part of the slice: step-1
2625 items for each item that is part of the slice,
2626 and then tail end of the list that was not
2627 covered by the slice */
2628 for (cur = start, i = 0;
2629 cur < stop;
2630 cur += step, i++) {
2631 Py_ssize_t lim = step - 1;
2633 garbage[i] = PyList_GET_ITEM(self, cur);
2635 if (cur + step >= Py_SIZE(self)) {
2636 lim = Py_SIZE(self) - cur - 1;
2639 memmove(self->ob_item + cur - i,
2640 self->ob_item + cur + 1,
2641 lim * sizeof(PyObject *));
2643 cur = start + slicelength*step;
2644 if (cur < Py_SIZE(self)) {
2645 memmove(self->ob_item + cur - slicelength,
2646 self->ob_item + cur,
2647 (Py_SIZE(self) - cur) *
2648 sizeof(PyObject *));
2651 Py_SIZE(self) -= slicelength;
2652 list_resize(self, Py_SIZE(self));
2654 for (i = 0; i < slicelength; i++) {
2655 Py_DECREF(garbage[i]);
2657 PyMem_FREE(garbage);
2659 return 0;
2661 else {
2662 /* assign slice */
2663 PyObject *ins, *seq;
2664 PyObject **garbage, **seqitems, **selfitems;
2665 Py_ssize_t cur, i;
2667 /* protect against a[::-1] = a */
2668 if (self == (PyListObject*)value) {
2669 seq = list_slice((PyListObject*)value, 0,
2670 PyList_GET_SIZE(value));
2672 else {
2673 seq = PySequence_Fast(value,
2674 "must assign iterable "
2675 "to extended slice");
2677 if (!seq)
2678 return -1;
2680 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2681 PyErr_Format(PyExc_ValueError,
2682 "attempt to assign sequence of "
2683 "size %zd to extended slice of "
2684 "size %zd",
2685 PySequence_Fast_GET_SIZE(seq),
2686 slicelength);
2687 Py_DECREF(seq);
2688 return -1;
2691 if (!slicelength) {
2692 Py_DECREF(seq);
2693 return 0;
2696 garbage = (PyObject**)
2697 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2698 if (!garbage) {
2699 Py_DECREF(seq);
2700 PyErr_NoMemory();
2701 return -1;
2704 selfitems = self->ob_item;
2705 seqitems = PySequence_Fast_ITEMS(seq);
2706 for (cur = start, i = 0; i < slicelength;
2707 cur += step, i++) {
2708 garbage[i] = selfitems[cur];
2709 ins = seqitems[i];
2710 Py_INCREF(ins);
2711 selfitems[cur] = ins;
2714 for (i = 0; i < slicelength; i++) {
2715 Py_DECREF(garbage[i]);
2718 PyMem_FREE(garbage);
2719 Py_DECREF(seq);
2721 return 0;
2724 else {
2725 PyErr_Format(PyExc_TypeError,
2726 "list indices must be integers, not %.200s",
2727 item->ob_type->tp_name);
2728 return -1;
2732 static PyMappingMethods list_as_mapping = {
2733 (lenfunc)list_length,
2734 (binaryfunc)list_subscript,
2735 (objobjargproc)list_ass_subscript
2738 PyTypeObject PyList_Type = {
2739 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2740 "list",
2741 sizeof(PyListObject),
2743 (destructor)list_dealloc, /* tp_dealloc */
2744 (printfunc)list_print, /* tp_print */
2745 0, /* tp_getattr */
2746 0, /* tp_setattr */
2747 0, /* tp_compare */
2748 (reprfunc)list_repr, /* tp_repr */
2749 0, /* tp_as_number */
2750 &list_as_sequence, /* tp_as_sequence */
2751 &list_as_mapping, /* tp_as_mapping */
2752 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
2753 0, /* tp_call */
2754 0, /* tp_str */
2755 PyObject_GenericGetAttr, /* tp_getattro */
2756 0, /* tp_setattro */
2757 0, /* tp_as_buffer */
2758 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2759 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2760 list_doc, /* tp_doc */
2761 (traverseproc)list_traverse, /* tp_traverse */
2762 (inquiry)list_clear, /* tp_clear */
2763 list_richcompare, /* tp_richcompare */
2764 0, /* tp_weaklistoffset */
2765 list_iter, /* tp_iter */
2766 0, /* tp_iternext */
2767 list_methods, /* tp_methods */
2768 0, /* tp_members */
2769 0, /* tp_getset */
2770 0, /* tp_base */
2771 0, /* tp_dict */
2772 0, /* tp_descr_get */
2773 0, /* tp_descr_set */
2774 0, /* tp_dictoffset */
2775 (initproc)list_init, /* tp_init */
2776 PyType_GenericAlloc, /* tp_alloc */
2777 PyType_GenericNew, /* tp_new */
2778 PyObject_GC_Del, /* tp_free */
2782 /*********************** List Iterator **************************/
2784 typedef struct {
2785 PyObject_HEAD
2786 long it_index;
2787 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
2788 } listiterobject;
2790 static PyObject *list_iter(PyObject *);
2791 static void listiter_dealloc(listiterobject *);
2792 static int listiter_traverse(listiterobject *, visitproc, void *);
2793 static PyObject *listiter_next(listiterobject *);
2794 static PyObject *listiter_len(listiterobject *);
2796 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
2798 static PyMethodDef listiter_methods[] = {
2799 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
2800 {NULL, NULL} /* sentinel */
2803 PyTypeObject PyListIter_Type = {
2804 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2805 "listiterator", /* tp_name */
2806 sizeof(listiterobject), /* tp_basicsize */
2807 0, /* tp_itemsize */
2808 /* methods */
2809 (destructor)listiter_dealloc, /* tp_dealloc */
2810 0, /* tp_print */
2811 0, /* tp_getattr */
2812 0, /* tp_setattr */
2813 0, /* tp_compare */
2814 0, /* tp_repr */
2815 0, /* tp_as_number */
2816 0, /* tp_as_sequence */
2817 0, /* tp_as_mapping */
2818 0, /* tp_hash */
2819 0, /* tp_call */
2820 0, /* tp_str */
2821 PyObject_GenericGetAttr, /* tp_getattro */
2822 0, /* tp_setattro */
2823 0, /* tp_as_buffer */
2824 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2825 0, /* tp_doc */
2826 (traverseproc)listiter_traverse, /* tp_traverse */
2827 0, /* tp_clear */
2828 0, /* tp_richcompare */
2829 0, /* tp_weaklistoffset */
2830 PyObject_SelfIter, /* tp_iter */
2831 (iternextfunc)listiter_next, /* tp_iternext */
2832 listiter_methods, /* tp_methods */
2833 0, /* tp_members */
2837 static PyObject *
2838 list_iter(PyObject *seq)
2840 listiterobject *it;
2842 if (!PyList_Check(seq)) {
2843 PyErr_BadInternalCall();
2844 return NULL;
2846 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2847 if (it == NULL)
2848 return NULL;
2849 it->it_index = 0;
2850 Py_INCREF(seq);
2851 it->it_seq = (PyListObject *)seq;
2852 _PyObject_GC_TRACK(it);
2853 return (PyObject *)it;
2856 static void
2857 listiter_dealloc(listiterobject *it)
2859 _PyObject_GC_UNTRACK(it);
2860 Py_XDECREF(it->it_seq);
2861 PyObject_GC_Del(it);
2864 static int
2865 listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2867 Py_VISIT(it->it_seq);
2868 return 0;
2871 static PyObject *
2872 listiter_next(listiterobject *it)
2874 PyListObject *seq;
2875 PyObject *item;
2877 assert(it != NULL);
2878 seq = it->it_seq;
2879 if (seq == NULL)
2880 return NULL;
2881 assert(PyList_Check(seq));
2883 if (it->it_index < PyList_GET_SIZE(seq)) {
2884 item = PyList_GET_ITEM(seq, it->it_index);
2885 ++it->it_index;
2886 Py_INCREF(item);
2887 return item;
2890 Py_DECREF(seq);
2891 it->it_seq = NULL;
2892 return NULL;
2895 static PyObject *
2896 listiter_len(listiterobject *it)
2898 Py_ssize_t len;
2899 if (it->it_seq) {
2900 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2901 if (len >= 0)
2902 return PyInt_FromSsize_t(len);
2904 return PyInt_FromLong(0);
2906 /*********************** List Reverse Iterator **************************/
2908 typedef struct {
2909 PyObject_HEAD
2910 Py_ssize_t it_index;
2911 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
2912 } listreviterobject;
2914 static PyObject *list_reversed(PyListObject *, PyObject *);
2915 static void listreviter_dealloc(listreviterobject *);
2916 static int listreviter_traverse(listreviterobject *, visitproc, void *);
2917 static PyObject *listreviter_next(listreviterobject *);
2918 static PyObject *listreviter_len(listreviterobject *);
2920 static PyMethodDef listreviter_methods[] = {
2921 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
2922 {NULL, NULL} /* sentinel */
2925 PyTypeObject PyListRevIter_Type = {
2926 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2927 "listreverseiterator", /* tp_name */
2928 sizeof(listreviterobject), /* tp_basicsize */
2929 0, /* tp_itemsize */
2930 /* methods */
2931 (destructor)listreviter_dealloc, /* tp_dealloc */
2932 0, /* tp_print */
2933 0, /* tp_getattr */
2934 0, /* tp_setattr */
2935 0, /* tp_compare */
2936 0, /* tp_repr */
2937 0, /* tp_as_number */
2938 0, /* tp_as_sequence */
2939 0, /* tp_as_mapping */
2940 0, /* tp_hash */
2941 0, /* tp_call */
2942 0, /* tp_str */
2943 PyObject_GenericGetAttr, /* tp_getattro */
2944 0, /* tp_setattro */
2945 0, /* tp_as_buffer */
2946 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2947 0, /* tp_doc */
2948 (traverseproc)listreviter_traverse, /* tp_traverse */
2949 0, /* tp_clear */
2950 0, /* tp_richcompare */
2951 0, /* tp_weaklistoffset */
2952 PyObject_SelfIter, /* tp_iter */
2953 (iternextfunc)listreviter_next, /* tp_iternext */
2954 listreviter_methods, /* tp_methods */
2958 static PyObject *
2959 list_reversed(PyListObject *seq, PyObject *unused)
2961 listreviterobject *it;
2963 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2964 if (it == NULL)
2965 return NULL;
2966 assert(PyList_Check(seq));
2967 it->it_index = PyList_GET_SIZE(seq) - 1;
2968 Py_INCREF(seq);
2969 it->it_seq = seq;
2970 PyObject_GC_Track(it);
2971 return (PyObject *)it;
2974 static void
2975 listreviter_dealloc(listreviterobject *it)
2977 PyObject_GC_UnTrack(it);
2978 Py_XDECREF(it->it_seq);
2979 PyObject_GC_Del(it);
2982 static int
2983 listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2985 Py_VISIT(it->it_seq);
2986 return 0;
2989 static PyObject *
2990 listreviter_next(listreviterobject *it)
2992 PyObject *item;
2993 Py_ssize_t index = it->it_index;
2994 PyListObject *seq = it->it_seq;
2996 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2997 item = PyList_GET_ITEM(seq, index);
2998 it->it_index--;
2999 Py_INCREF(item);
3000 return item;
3002 it->it_index = -1;
3003 if (seq != NULL) {
3004 it->it_seq = NULL;
3005 Py_DECREF(seq);
3007 return NULL;
3010 static PyObject *
3011 listreviter_len(listreviterobject *it)
3013 Py_ssize_t len = it->it_index + 1;
3014 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3015 len = 0;
3016 return PyLong_FromSsize_t(len);