This commit was manufactured by cvs2svn to create tag 'r221'.
[python/dscho.git] / Objects / listobject.c
blobf3821f86fac03cecf3c25a9516c4509f4ac55861
2 /* List object implementation */
4 #include "Python.h"
6 #ifdef STDC_HEADERS
7 #include <stddef.h>
8 #else
9 #include <sys/types.h> /* For size_t */
10 #endif
12 static int
13 roundupsize(int n)
15 unsigned int nbits = 0;
16 unsigned int n2 = (unsigned int)n >> 5;
18 /* Round up:
19 * If n < 256, to a multiple of 8.
20 * If n < 2048, to a multiple of 64.
21 * If n < 16384, to a multiple of 512.
22 * If n < 131072, to a multiple of 4096.
23 * If n < 1048576, to a multiple of 32768.
24 * If n < 8388608, to a multiple of 262144.
25 * If n < 67108864, to a multiple of 2097152.
26 * If n < 536870912, to a multiple of 16777216.
27 * ...
28 * If n < 2**(5+3*i), to a multiple of 2**(3*i).
30 * This over-allocates proportional to the list size, making room
31 * for additional growth. The over-allocation is mild, but is
32 * enough to give linear-time amortized behavior over a long
33 * sequence of appends() in the presence of a poorly-performing
34 * system realloc() (which is a reality, e.g., across all flavors
35 * of Windows, with Win9x behavior being particularly bad -- and
36 * we've still got address space fragmentation problems on Win9x
37 * even with this scheme, although it requires much longer lists to
38 * provoke them than it used to).
40 do {
41 n2 >>= 3;
42 nbits += 3;
43 } while (n2);
44 return ((n >> nbits) + 1) << nbits;
47 #define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems))
49 PyObject *
50 PyList_New(int size)
52 int i;
53 PyListObject *op;
54 size_t nbytes;
55 if (size < 0) {
56 PyErr_BadInternalCall();
57 return NULL;
59 nbytes = size * sizeof(PyObject *);
60 /* Check for overflow */
61 if (nbytes / sizeof(PyObject *) != (size_t)size) {
62 return PyErr_NoMemory();
64 op = PyObject_GC_New(PyListObject, &PyList_Type);
65 if (op == NULL) {
66 return NULL;
68 if (size <= 0) {
69 op->ob_item = NULL;
71 else {
72 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
73 if (op->ob_item == NULL) {
74 return PyErr_NoMemory();
77 op->ob_size = size;
78 for (i = 0; i < size; i++)
79 op->ob_item[i] = NULL;
80 _PyObject_GC_TRACK(op);
81 return (PyObject *) op;
84 int
85 PyList_Size(PyObject *op)
87 if (!PyList_Check(op)) {
88 PyErr_BadInternalCall();
89 return -1;
91 else
92 return ((PyListObject *)op) -> ob_size;
95 static PyObject *indexerr;
97 PyObject *
98 PyList_GetItem(PyObject *op, int i)
100 if (!PyList_Check(op)) {
101 PyErr_BadInternalCall();
102 return NULL;
104 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
105 if (indexerr == NULL)
106 indexerr = PyString_FromString(
107 "list index out of range");
108 PyErr_SetObject(PyExc_IndexError, indexerr);
109 return NULL;
111 return ((PyListObject *)op) -> ob_item[i];
115 PyList_SetItem(register PyObject *op, register int i,
116 register PyObject *newitem)
118 register PyObject *olditem;
119 register PyObject **p;
120 if (!PyList_Check(op)) {
121 Py_XDECREF(newitem);
122 PyErr_BadInternalCall();
123 return -1;
125 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
126 Py_XDECREF(newitem);
127 PyErr_SetString(PyExc_IndexError,
128 "list assignment index out of range");
129 return -1;
131 p = ((PyListObject *)op) -> ob_item + i;
132 olditem = *p;
133 *p = newitem;
134 Py_XDECREF(olditem);
135 return 0;
138 static int
139 ins1(PyListObject *self, int where, PyObject *v)
141 int i;
142 PyObject **items;
143 if (v == NULL) {
144 PyErr_BadInternalCall();
145 return -1;
147 if (self->ob_size == INT_MAX) {
148 PyErr_SetString(PyExc_OverflowError,
149 "cannot add more objects to list");
150 return -1;
152 items = self->ob_item;
153 NRESIZE(items, PyObject *, self->ob_size+1);
154 if (items == NULL) {
155 PyErr_NoMemory();
156 return -1;
158 if (where < 0)
159 where = 0;
160 if (where > self->ob_size)
161 where = self->ob_size;
162 for (i = self->ob_size; --i >= where; )
163 items[i+1] = items[i];
164 Py_INCREF(v);
165 items[where] = v;
166 self->ob_item = items;
167 self->ob_size++;
168 return 0;
172 PyList_Insert(PyObject *op, int where, PyObject *newitem)
174 if (!PyList_Check(op)) {
175 PyErr_BadInternalCall();
176 return -1;
178 return ins1((PyListObject *)op, where, newitem);
182 PyList_Append(PyObject *op, PyObject *newitem)
184 if (!PyList_Check(op)) {
185 PyErr_BadInternalCall();
186 return -1;
188 return ins1((PyListObject *)op,
189 (int) ((PyListObject *)op)->ob_size, newitem);
192 /* Methods */
194 static void
195 list_dealloc(PyListObject *op)
197 int i;
198 PyObject_GC_UnTrack(op);
199 Py_TRASHCAN_SAFE_BEGIN(op)
200 if (op->ob_item != NULL) {
201 /* Do it backwards, for Christian Tismer.
202 There's a simple test case where somehow this reduces
203 thrashing when a *very* large list is created and
204 immediately deleted. */
205 i = op->ob_size;
206 while (--i >= 0) {
207 Py_XDECREF(op->ob_item[i]);
209 PyMem_FREE(op->ob_item);
211 op->ob_type->tp_free((PyObject *)op);
212 Py_TRASHCAN_SAFE_END(op)
215 static int
216 list_print(PyListObject *op, FILE *fp, int flags)
218 int i;
220 i = Py_ReprEnter((PyObject*)op);
221 if (i != 0) {
222 if (i < 0)
223 return i;
224 fprintf(fp, "[...]");
225 return 0;
227 fprintf(fp, "[");
228 for (i = 0; i < op->ob_size; i++) {
229 if (i > 0)
230 fprintf(fp, ", ");
231 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
232 Py_ReprLeave((PyObject *)op);
233 return -1;
236 fprintf(fp, "]");
237 Py_ReprLeave((PyObject *)op);
238 return 0;
241 static PyObject *
242 list_repr(PyListObject *v)
244 int i;
245 PyObject *s, *temp;
246 PyObject *pieces = NULL, *result = NULL;
248 i = Py_ReprEnter((PyObject*)v);
249 if (i != 0) {
250 return i > 0 ? PyString_FromString("[...]") : NULL;
253 if (v->ob_size == 0) {
254 result = PyString_FromString("[]");
255 goto Done;
258 pieces = PyList_New(0);
259 if (pieces == NULL)
260 goto Done;
262 /* Do repr() on each element. Note that this may mutate the list,
263 so must refetch the list size on each iteration. */
264 for (i = 0; i < v->ob_size; ++i) {
265 int status;
266 s = PyObject_Repr(v->ob_item[i]);
267 if (s == NULL)
268 goto Done;
269 status = PyList_Append(pieces, s);
270 Py_DECREF(s); /* append created a new ref */
271 if (status < 0)
272 goto Done;
275 /* Add "[]" decorations to the first and last items. */
276 assert(PyList_GET_SIZE(pieces) > 0);
277 s = PyString_FromString("[");
278 if (s == NULL)
279 goto Done;
280 temp = PyList_GET_ITEM(pieces, 0);
281 PyString_ConcatAndDel(&s, temp);
282 PyList_SET_ITEM(pieces, 0, s);
283 if (s == NULL)
284 goto Done;
286 s = PyString_FromString("]");
287 if (s == NULL)
288 goto Done;
289 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
290 PyString_ConcatAndDel(&temp, s);
291 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
292 if (temp == NULL)
293 goto Done;
295 /* Paste them all together with ", " between. */
296 s = PyString_FromString(", ");
297 if (s == NULL)
298 goto Done;
299 result = _PyString_Join(s, pieces);
300 Py_DECREF(s);
302 Done:
303 Py_XDECREF(pieces);
304 Py_ReprLeave((PyObject *)v);
305 return result;
308 static int
309 list_length(PyListObject *a)
311 return a->ob_size;
316 static int
317 list_contains(PyListObject *a, PyObject *el)
319 int i;
321 for (i = 0; i < a->ob_size; ++i) {
322 int cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
323 Py_EQ);
324 if (cmp > 0)
325 return 1;
326 else if (cmp < 0)
327 return -1;
329 return 0;
333 static PyObject *
334 list_item(PyListObject *a, int i)
336 if (i < 0 || i >= a->ob_size) {
337 if (indexerr == NULL)
338 indexerr = PyString_FromString(
339 "list index out of range");
340 PyErr_SetObject(PyExc_IndexError, indexerr);
341 return NULL;
343 Py_INCREF(a->ob_item[i]);
344 return a->ob_item[i];
347 static PyObject *
348 list_slice(PyListObject *a, int ilow, int ihigh)
350 PyListObject *np;
351 int i;
352 if (ilow < 0)
353 ilow = 0;
354 else if (ilow > a->ob_size)
355 ilow = a->ob_size;
356 if (ihigh < ilow)
357 ihigh = ilow;
358 else if (ihigh > a->ob_size)
359 ihigh = a->ob_size;
360 np = (PyListObject *) PyList_New(ihigh - ilow);
361 if (np == NULL)
362 return NULL;
363 for (i = ilow; i < ihigh; i++) {
364 PyObject *v = a->ob_item[i];
365 Py_INCREF(v);
366 np->ob_item[i - ilow] = v;
368 return (PyObject *)np;
371 PyObject *
372 PyList_GetSlice(PyObject *a, int ilow, int ihigh)
374 if (!PyList_Check(a)) {
375 PyErr_BadInternalCall();
376 return NULL;
378 return list_slice((PyListObject *)a, ilow, ihigh);
381 static PyObject *
382 list_concat(PyListObject *a, PyObject *bb)
384 int size;
385 int i;
386 PyListObject *np;
387 if (!PyList_Check(bb)) {
388 PyErr_Format(PyExc_TypeError,
389 "can only concatenate list (not \"%.200s\") to list",
390 bb->ob_type->tp_name);
391 return NULL;
393 #define b ((PyListObject *)bb)
394 size = a->ob_size + b->ob_size;
395 np = (PyListObject *) PyList_New(size);
396 if (np == NULL) {
397 return NULL;
399 for (i = 0; i < a->ob_size; i++) {
400 PyObject *v = a->ob_item[i];
401 Py_INCREF(v);
402 np->ob_item[i] = v;
404 for (i = 0; i < b->ob_size; i++) {
405 PyObject *v = b->ob_item[i];
406 Py_INCREF(v);
407 np->ob_item[i + a->ob_size] = v;
409 return (PyObject *)np;
410 #undef b
413 static PyObject *
414 list_repeat(PyListObject *a, int n)
416 int i, j;
417 int size;
418 PyListObject *np;
419 PyObject **p;
420 if (n < 0)
421 n = 0;
422 size = a->ob_size * n;
423 np = (PyListObject *) PyList_New(size);
424 if (np == NULL)
425 return NULL;
426 p = np->ob_item;
427 for (i = 0; i < n; i++) {
428 for (j = 0; j < a->ob_size; j++) {
429 *p = a->ob_item[j];
430 Py_INCREF(*p);
431 p++;
434 return (PyObject *) np;
437 static int
438 list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
440 /* Because [X]DECREF can recursively invoke list operations on
441 this list, we must postpone all [X]DECREF activity until
442 after the list is back in its canonical shape. Therefore
443 we must allocate an additional array, 'recycle', into which
444 we temporarily copy the items that are deleted from the
445 list. :-( */
446 PyObject **recycle, **p;
447 PyObject **item;
448 int n; /* Size of replacement list */
449 int d; /* Change in size */
450 int k; /* Loop index */
451 #define b ((PyListObject *)v)
452 if (v == NULL)
453 n = 0;
454 else if (PyList_Check(v)) {
455 n = b->ob_size;
456 if (a == b) {
457 /* Special case "a[i:j] = a" -- copy b first */
458 int ret;
459 v = list_slice(b, 0, n);
460 ret = list_ass_slice(a, ilow, ihigh, v);
461 Py_DECREF(v);
462 return ret;
465 else {
466 PyErr_Format(PyExc_TypeError,
467 "must assign list (not \"%.200s\") to slice",
468 v->ob_type->tp_name);
469 return -1;
471 if (ilow < 0)
472 ilow = 0;
473 else if (ilow > a->ob_size)
474 ilow = a->ob_size;
475 if (ihigh < ilow)
476 ihigh = ilow;
477 else if (ihigh > a->ob_size)
478 ihigh = a->ob_size;
479 item = a->ob_item;
480 d = n - (ihigh-ilow);
481 if (ihigh > ilow)
482 p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
483 else
484 p = recycle = NULL;
485 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
486 for (k = ilow; k < ihigh; k++)
487 *p++ = item[k];
488 if (d < 0) {
489 for (/*k = ihigh*/; k < a->ob_size; k++)
490 item[k+d] = item[k];
491 a->ob_size += d;
492 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
493 a->ob_item = item;
496 else { /* Insert d items; recycle ihigh-ilow items */
497 NRESIZE(item, PyObject *, a->ob_size + d);
498 if (item == NULL) {
499 if (recycle != NULL)
500 PyMem_DEL(recycle);
501 PyErr_NoMemory();
502 return -1;
504 for (k = a->ob_size; --k >= ihigh; )
505 item[k+d] = item[k];
506 for (/*k = ihigh-1*/; k >= ilow; --k)
507 *p++ = item[k];
508 a->ob_item = item;
509 a->ob_size += d;
511 for (k = 0; k < n; k++, ilow++) {
512 PyObject *w = b->ob_item[k];
513 Py_XINCREF(w);
514 item[ilow] = w;
516 if (recycle) {
517 while (--p >= recycle)
518 Py_XDECREF(*p);
519 PyMem_DEL(recycle);
521 if (a->ob_size == 0 && a->ob_item != NULL) {
522 PyMem_FREE(a->ob_item);
523 a->ob_item = NULL;
525 return 0;
526 #undef b
530 PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v)
532 if (!PyList_Check(a)) {
533 PyErr_BadInternalCall();
534 return -1;
536 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
539 static PyObject *
540 list_inplace_repeat(PyListObject *self, int n)
542 PyObject **items;
543 int size, i, j;
546 size = PyList_GET_SIZE(self);
547 if (size == 0) {
548 Py_INCREF(self);
549 return (PyObject *)self;
552 items = self->ob_item;
554 if (n < 1) {
555 self->ob_item = NULL;
556 self->ob_size = 0;
557 for (i = 0; i < size; i++)
558 Py_XDECREF(items[i]);
559 PyMem_DEL(items);
560 Py_INCREF(self);
561 return (PyObject *)self;
564 NRESIZE(items, PyObject*, size*n);
565 if (items == NULL) {
566 PyErr_NoMemory();
567 goto finally;
569 self->ob_item = items;
570 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
571 for (j = 0; j < size; j++) {
572 PyObject *o = PyList_GET_ITEM(self, j);
573 Py_INCREF(o);
574 PyList_SET_ITEM(self, self->ob_size++, o);
577 Py_INCREF(self);
578 return (PyObject *)self;
579 finally:
580 return NULL;
583 static int
584 list_ass_item(PyListObject *a, int i, PyObject *v)
586 PyObject *old_value;
587 if (i < 0 || i >= a->ob_size) {
588 PyErr_SetString(PyExc_IndexError,
589 "list assignment index out of range");
590 return -1;
592 if (v == NULL)
593 return list_ass_slice(a, i, i+1, v);
594 Py_INCREF(v);
595 old_value = a->ob_item[i];
596 a->ob_item[i] = v;
597 Py_DECREF(old_value);
598 return 0;
601 static PyObject *
602 ins(PyListObject *self, int where, PyObject *v)
604 if (ins1(self, where, v) != 0)
605 return NULL;
606 Py_INCREF(Py_None);
607 return Py_None;
610 static PyObject *
611 listinsert(PyListObject *self, PyObject *args)
613 int i;
614 PyObject *v;
615 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
616 return NULL;
617 return ins(self, i, v);
620 static PyObject *
621 listappend(PyListObject *self, PyObject *v)
623 return ins(self, (int) self->ob_size, v);
626 static int
627 listextend_internal(PyListObject *self, PyObject *b)
629 PyObject **items;
630 int selflen = PyList_GET_SIZE(self);
631 int blen;
632 register int i;
634 if (PyObject_Size(b) == 0) {
635 /* short circuit when b is empty */
636 Py_DECREF(b);
637 return 0;
640 if (self == (PyListObject*)b) {
641 /* as in list_ass_slice() we must special case the
642 * situation: a.extend(a)
644 * XXX: I think this way ought to be faster than using
645 * list_slice() the way list_ass_slice() does.
647 Py_DECREF(b);
648 b = PyList_New(selflen);
649 if (!b)
650 return -1;
651 for (i = 0; i < selflen; i++) {
652 PyObject *o = PyList_GET_ITEM(self, i);
653 Py_INCREF(o);
654 PyList_SET_ITEM(b, i, o);
658 blen = PyObject_Size(b);
660 /* resize a using idiom */
661 items = self->ob_item;
662 NRESIZE(items, PyObject*, selflen + blen);
663 if (items == NULL) {
664 PyErr_NoMemory();
665 Py_DECREF(b);
666 return -1;
669 self->ob_item = items;
671 /* populate the end of self with b's items */
672 for (i = 0; i < blen; i++) {
673 PyObject *o = PySequence_Fast_GET_ITEM(b, i);
674 Py_INCREF(o);
675 PyList_SET_ITEM(self, self->ob_size++, o);
677 Py_DECREF(b);
678 return 0;
682 static PyObject *
683 list_inplace_concat(PyListObject *self, PyObject *other)
685 other = PySequence_Fast(other, "argument to += must be iterable");
686 if (!other)
687 return NULL;
689 if (listextend_internal(self, other) < 0)
690 return NULL;
692 Py_INCREF(self);
693 return (PyObject *)self;
696 static PyObject *
697 listextend(PyListObject *self, PyObject *b)
700 b = PySequence_Fast(b, "list.extend() argument must be iterable");
701 if (!b)
702 return NULL;
704 if (listextend_internal(self, b) < 0)
705 return NULL;
707 Py_INCREF(Py_None);
708 return Py_None;
711 static PyObject *
712 listpop(PyListObject *self, PyObject *args)
714 int i = -1;
715 PyObject *v;
716 if (!PyArg_ParseTuple(args, "|i:pop", &i))
717 return NULL;
718 if (self->ob_size == 0) {
719 /* Special-case most common failure cause */
720 PyErr_SetString(PyExc_IndexError, "pop from empty list");
721 return NULL;
723 if (i < 0)
724 i += self->ob_size;
725 if (i < 0 || i >= self->ob_size) {
726 PyErr_SetString(PyExc_IndexError, "pop index out of range");
727 return NULL;
729 v = self->ob_item[i];
730 Py_INCREF(v);
731 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
732 Py_DECREF(v);
733 return NULL;
735 return v;
738 /* New quicksort implementation for arrays of object pointers.
739 Thanks to discussions with Tim Peters. */
741 /* CMPERROR is returned by our comparison function when an error
742 occurred. This is the largest negative integer (0x80000000 on a
743 32-bit system). */
744 #define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
746 /* Comparison function. Takes care of calling a user-supplied
747 comparison function (any callable Python object). Calls the
748 standard comparison function, PyObject_Compare(), if the user-
749 supplied function is NULL. */
751 static int
752 docompare(PyObject *x, PyObject *y, PyObject *compare)
754 PyObject *args, *res;
755 int i;
757 if (compare == NULL) {
758 /* NOTE: we rely on the fact here that the sorting algorithm
759 only ever checks whether k<0, i.e., whether x<y. So we
760 invoke the rich comparison function with Py_LT ('<'), and
761 return -1 when it returns true and 0 when it returns
762 false. */
763 i = PyObject_RichCompareBool(x, y, Py_LT);
764 if (i < 0)
765 return CMPERROR;
766 else
767 return -i;
770 args = Py_BuildValue("(OO)", x, y);
771 if (args == NULL)
772 return CMPERROR;
773 res = PyEval_CallObject(compare, args);
774 Py_DECREF(args);
775 if (res == NULL)
776 return CMPERROR;
777 if (!PyInt_Check(res)) {
778 Py_DECREF(res);
779 PyErr_SetString(PyExc_TypeError,
780 "comparison function must return int");
781 return CMPERROR;
783 i = PyInt_AsLong(res);
784 Py_DECREF(res);
785 if (i < 0)
786 return -1;
787 if (i > 0)
788 return 1;
789 return 0;
792 /* MINSIZE is the smallest array that will get a full-blown samplesort
793 treatment; smaller arrays are sorted using binary insertion. It must
794 be at least 7 for the samplesort implementation to work. Binary
795 insertion does fewer compares, but can suffer O(N**2) data movement.
796 The more expensive compares, the larger MINSIZE should be. */
797 #define MINSIZE 100
799 /* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
800 partition; smaller slices are passed to binarysort. It must be at
801 least 2, and no larger than MINSIZE. Setting it higher reduces the #
802 of compares slowly, but increases the amount of data movement quickly.
803 The value here was chosen assuming a compare costs ~25x more than
804 swapping a pair of memory-resident pointers -- but under that assumption,
805 changing the value by a few dozen more or less has aggregate effect
806 under 1%. So the value is crucial, but not touchy <wink>. */
807 #define MINPARTITIONSIZE 40
809 /* MAXMERGE is the largest number of elements we'll always merge into
810 a known-to-be sorted chunk via binary insertion, regardless of the
811 size of that chunk. Given a chunk of N sorted elements, and a group
812 of K unknowns, the largest K for which it's better to do insertion
813 (than a full-blown sort) is a complicated function of N and K mostly
814 involving the expected number of compares and data moves under each
815 approach, and the relative cost of those operations on a specific
816 architecure. The fixed value here is conservative, and should be a
817 clear win regardless of architecture or N. */
818 #define MAXMERGE 15
820 /* STACKSIZE is the size of our work stack. A rough estimate is that
821 this allows us to sort arrays of size N where
822 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
823 for arrays of size 2**64. Because we push the biggest partition
824 first, the worst case occurs when all subarrays are always partitioned
825 exactly in two. */
826 #define STACKSIZE 60
829 #define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
831 /* binarysort is the best method for sorting small arrays: it does
832 few compares, but can do data movement quadratic in the number of
833 elements.
834 [lo, hi) is a contiguous slice of a list, and is sorted via
835 binary insertion.
836 On entry, must have lo <= start <= hi, and that [lo, start) is already
837 sorted (pass start == lo if you don't know!).
838 If docompare complains (returns CMPERROR) return -1, else 0.
839 Even in case of error, the output slice will be some permutation of
840 the input (nothing is lost or duplicated).
843 static int
844 binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
845 /* compare -- comparison function object, or NULL for default */
847 /* assert lo <= start <= hi
848 assert [lo, start) is sorted */
849 register int k;
850 register PyObject **l, **p, **r;
851 register PyObject *pivot;
853 if (lo == start)
854 ++start;
855 for (; start < hi; ++start) {
856 /* set l to where *start belongs */
857 l = lo;
858 r = start;
859 pivot = *r;
860 do {
861 p = l + ((r - l) >> 1);
862 SETK(pivot, *p);
863 if (k < 0)
864 r = p;
865 else
866 l = p + 1;
867 } while (l < r);
868 /* Pivot should go at l -- slide over to make room.
869 Caution: using memmove is much slower under MSVC 5;
870 we're not usually moving many slots. */
871 for (p = start; p > l; --p)
872 *p = *(p-1);
873 *l = pivot;
875 return 0;
877 fail:
878 return -1;
881 /* samplesortslice is the sorting workhorse.
882 [lo, hi) is a contiguous slice of a list, to be sorted in place.
883 On entry, must have lo <= hi,
884 If docompare complains (returns CMPERROR) return -1, else 0.
885 Even in case of error, the output slice will be some permutation of
886 the input (nothing is lost or duplicated).
888 samplesort is basically quicksort on steroids: a power of 2 close
889 to n/ln(n) is computed, and that many elements (less 1) are picked at
890 random from the array and sorted. These 2**k - 1 elements are then
891 used as preselected pivots for an equal number of quicksort
892 partitioning steps, partitioning the slice into 2**k chunks each of
893 size about ln(n). These small final chunks are then usually handled
894 by binarysort. Note that when k=1, this is roughly the same as an
895 ordinary quicksort using a random pivot, and when k=2 this is roughly
896 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
897 this a "median of n/ln(n)" quicksort. You can also view it as a kind
898 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
900 The large number of samples makes a quadratic-time case almost
901 impossible, and asymptotically drives the average-case number of
902 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
903 3 variant) down to N lg N.
905 We also play lots of low-level tricks to cut the number of compares.
907 Very obscure: To avoid using extra memory, the PPs are stored in the
908 array and shuffled around as partitioning proceeds. At the start of a
909 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
910 adjacent (either on the left or the right!) to a chunk of X elements
911 that are to be partitioned: P X or X P. In either case we need to
912 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
913 left, followed by the PP to be used for this step (that's the middle
914 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
915 P X or X P -> Psmall pivot X Plarge
916 and the order of the PPs must not be altered. It can take a while
917 to realize this isn't trivial! It can take even longer <wink> to
918 understand why the simple code below works, using only 2**(m-1) swaps.
919 The key is that the order of the X elements isn't necessarily
920 preserved: X can end up as some cyclic permutation of its original
921 order. That's OK, because X is unsorted anyway. If the order of X
922 had to be preserved too, the simplest method I know of using O(1)
923 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
924 Since len(X) is typically several times larger than 2**(m-1), that
925 would slow things down.
928 struct SamplesortStackNode {
929 /* Represents a slice of the array, from (& including) lo up
930 to (but excluding) hi. "extra" additional & adjacent elements
931 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
932 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
933 already sorted, but nothing is known about the other elements
934 in [lo, hi). |extra| is always one less than a power of 2.
935 When extra is 0, we're out of PPs, and the slice must be
936 sorted by some other means. */
937 PyObject **lo;
938 PyObject **hi;
939 int extra;
942 /* The number of PPs we want is 2**k - 1, where 2**k is as close to
943 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
944 is undesirable, so cutoff values are canned in the "cutoff" table
945 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
946 #define CUTOFFBASE 4
947 static long cutoff[] = {
948 43, /* smallest N such that k == 4 */
949 106, /* etc */
950 250,
951 576,
952 1298,
953 2885,
954 6339,
955 13805,
956 29843,
957 64116,
958 137030,
959 291554,
960 617916,
961 1305130,
962 2748295,
963 5771662,
964 12091672,
965 25276798,
966 52734615,
967 109820537,
968 228324027,
969 473977813,
970 982548444, /* smallest N such that k == 26 */
971 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
974 static int
975 samplesortslice(PyObject **lo, PyObject **hi, PyObject *compare)
976 /* compare -- comparison function object, or NULL for default */
978 register PyObject **l, **r;
979 register PyObject *tmp, *pivot;
980 register int k;
981 int n, extra, top, extraOnRight;
982 struct SamplesortStackNode stack[STACKSIZE];
984 /* assert lo <= hi */
985 n = hi - lo;
987 /* ----------------------------------------------------------
988 * Special cases
989 * --------------------------------------------------------*/
990 if (n < 2)
991 return 0;
993 /* Set r to the largest value such that [lo,r) is sorted.
994 This catches the already-sorted case, the all-the-same
995 case, and the appended-a-few-elements-to-a-sorted-list case.
996 If the array is unsorted, we're very likely to get out of
997 the loop fast, so the test is cheap if it doesn't pay off.
999 /* assert lo < hi */
1000 for (r = lo+1; r < hi; ++r) {
1001 SETK(*r, *(r-1));
1002 if (k < 0)
1003 break;
1005 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
1006 few unknowns, or few elements in total. */
1007 if (hi - r <= MAXMERGE || n < MINSIZE)
1008 return binarysort(lo, hi, r, compare);
1010 /* Check for the array already being reverse-sorted. Typical
1011 benchmark-driven silliness <wink>. */
1012 /* assert lo < hi */
1013 for (r = lo+1; r < hi; ++r) {
1014 SETK(*(r-1), *r);
1015 if (k < 0)
1016 break;
1018 if (hi - r <= MAXMERGE) {
1019 /* Reverse the reversed prefix, then insert the tail */
1020 PyObject **originalr = r;
1021 l = lo;
1022 do {
1023 --r;
1024 tmp = *l; *l = *r; *r = tmp;
1025 ++l;
1026 } while (l < r);
1027 return binarysort(lo, hi, originalr, compare);
1030 /* ----------------------------------------------------------
1031 * Normal case setup: a large array without obvious pattern.
1032 * --------------------------------------------------------*/
1034 /* extra := a power of 2 ~= n/ln(n), less 1.
1035 First find the smallest extra s.t. n < cutoff[extra] */
1036 for (extra = 0;
1037 extra < sizeof(cutoff) / sizeof(cutoff[0]);
1038 ++extra) {
1039 if (n < cutoff[extra])
1040 break;
1041 /* note that if we fall out of the loop, the value of
1042 extra still makes *sense*, but may be smaller than
1043 we would like (but the array has more than ~= 2**31
1044 elements in this case!) */
1046 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1047 have is CUTOFFBASE-1, so
1048 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1049 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
1050 /* assert extra > 0 and n >= extra */
1052 /* Swap that many values to the start of the array. The
1053 selection of elements is pseudo-random, but the same on
1054 every run (this is intentional! timing algorithm changes is
1055 a pain if timing varies across runs). */
1057 unsigned int seed = n / extra; /* arbitrary */
1058 unsigned int i;
1059 for (i = 0; i < (unsigned)extra; ++i) {
1060 /* j := random int in [i, n) */
1061 unsigned int j;
1062 seed = seed * 69069 + 7;
1063 j = i + seed % (n - i);
1064 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
1068 /* Recursively sort the preselected pivots. */
1069 if (samplesortslice(lo, lo + extra, compare) < 0)
1070 goto fail;
1072 top = 0; /* index of available stack slot */
1073 lo += extra; /* point to first unknown */
1074 extraOnRight = 0; /* the PPs are at the left end */
1076 /* ----------------------------------------------------------
1077 * Partition [lo, hi), and repeat until out of work.
1078 * --------------------------------------------------------*/
1079 for (;;) {
1080 /* assert lo <= hi, so n >= 0 */
1081 n = hi - lo;
1083 /* We may not want, or may not be able, to partition:
1084 If n is small, it's quicker to insert.
1085 If extra is 0, we're out of pivots, and *must* use
1086 another method.
1088 if (n < MINPARTITIONSIZE || extra == 0) {
1089 if (n >= MINSIZE) {
1090 /* assert extra == 0
1091 This is rare, since the average size
1092 of a final block is only about
1093 ln(original n). */
1094 if (samplesortslice(lo, hi, compare) < 0)
1095 goto fail;
1097 else {
1098 /* Binary insertion should be quicker,
1099 and we can take advantage of the PPs
1100 already being sorted. */
1101 if (extraOnRight && extra) {
1102 /* swap the PPs to the left end */
1103 k = extra;
1104 do {
1105 tmp = *lo;
1106 *lo = *hi;
1107 *hi = tmp;
1108 ++lo; ++hi;
1109 } while (--k);
1111 if (binarysort(lo - extra, hi, lo,
1112 compare) < 0)
1113 goto fail;
1116 /* Find another slice to work on. */
1117 if (--top < 0)
1118 break; /* no more -- done! */
1119 lo = stack[top].lo;
1120 hi = stack[top].hi;
1121 extra = stack[top].extra;
1122 extraOnRight = 0;
1123 if (extra < 0) {
1124 extraOnRight = 1;
1125 extra = -extra;
1127 continue;
1130 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1131 Then our preselected pivot is at (extra-1)/2, and we
1132 want to move the PPs before that to the left end of
1133 the slice, and the PPs after that to the right end.
1134 The following section changes extra, lo, hi, and the
1135 slice such that:
1136 [lo-extra, lo) contains the smaller PPs.
1137 *lo == our PP.
1138 (lo, hi) contains the unknown elements.
1139 [hi, hi+extra) contains the larger PPs.
1141 k = extra >>= 1; /* num PPs to move */
1142 if (extraOnRight) {
1143 /* Swap the smaller PPs to the left end.
1144 Note that this loop actually moves k+1 items:
1145 the last is our PP */
1146 do {
1147 tmp = *lo; *lo = *hi; *hi = tmp;
1148 ++lo; ++hi;
1149 } while (k--);
1151 else {
1152 /* Swap the larger PPs to the right end. */
1153 while (k--) {
1154 --lo; --hi;
1155 tmp = *lo; *lo = *hi; *hi = tmp;
1158 --lo; /* *lo is now our PP */
1159 pivot = *lo;
1161 /* Now an almost-ordinary quicksort partition step.
1162 Note that most of the time is spent here!
1163 Only odd thing is that we partition into < and >=,
1164 instead of the usual <= and >=. This helps when
1165 there are lots of duplicates of different values,
1166 because it eventually tends to make subfiles
1167 "pure" (all duplicates), and we special-case for
1168 duplicates later. */
1169 l = lo + 1;
1170 r = hi - 1;
1171 /* assert lo < l < r < hi (small n weeded out above) */
1173 do {
1174 /* slide l right, looking for key >= pivot */
1175 do {
1176 SETK(*l, pivot);
1177 if (k < 0)
1178 ++l;
1179 else
1180 break;
1181 } while (l < r);
1183 /* slide r left, looking for key < pivot */
1184 while (l < r) {
1185 register PyObject *rval = *r--;
1186 SETK(rval, pivot);
1187 if (k < 0) {
1188 /* swap and advance */
1189 r[1] = *l;
1190 *l++ = rval;
1191 break;
1195 } while (l < r);
1197 /* assert lo < r <= l < hi
1198 assert r == l or r+1 == l
1199 everything to the left of l is < pivot, and
1200 everything to the right of r is >= pivot */
1202 if (l == r) {
1203 SETK(*r, pivot);
1204 if (k < 0)
1205 ++l;
1206 else
1207 --r;
1209 /* assert lo <= r and r+1 == l and l <= hi
1210 assert r == lo or a[r] < pivot
1211 assert a[lo] is pivot
1212 assert l == hi or a[l] >= pivot
1213 Swap the pivot into "the middle", so we can henceforth
1214 ignore it.
1216 *lo = *r;
1217 *r = pivot;
1219 /* The following is true now, & will be preserved:
1220 All in [lo,r) are < pivot
1221 All in [r,l) == pivot (& so can be ignored)
1222 All in [l,hi) are >= pivot */
1224 /* Check for duplicates of the pivot. One compare is
1225 wasted if there are no duplicates, but can win big
1226 when there are.
1227 Tricky: we're sticking to "<" compares, so deduce
1228 equality indirectly. We know pivot <= *l, so they're
1229 equal iff not pivot < *l.
1231 while (l < hi) {
1232 /* pivot <= *l known */
1233 SETK(pivot, *l);
1234 if (k < 0)
1235 break;
1236 else
1237 /* <= and not < implies == */
1238 ++l;
1241 /* assert lo <= r < l <= hi
1242 Partitions are [lo, r) and [l, hi) */
1244 /* push fattest first; remember we still have extra PPs
1245 to the left of the left chunk and to the right of
1246 the right chunk! */
1247 /* assert top < STACKSIZE */
1248 if (r - lo <= hi - l) {
1249 /* second is bigger */
1250 stack[top].lo = l;
1251 stack[top].hi = hi;
1252 stack[top].extra = -extra;
1253 hi = r;
1254 extraOnRight = 0;
1256 else {
1257 /* first is bigger */
1258 stack[top].lo = lo;
1259 stack[top].hi = r;
1260 stack[top].extra = extra;
1261 lo = l;
1262 extraOnRight = 1;
1264 ++top;
1266 } /* end of partitioning loop */
1268 return 0;
1270 fail:
1271 return -1;
1274 #undef SETK
1276 staticforward PyTypeObject immutable_list_type;
1278 static PyObject *
1279 listsort(PyListObject *self, PyObject *args)
1281 int err;
1282 PyObject *compare = NULL;
1283 PyTypeObject *savetype;
1285 if (args != NULL) {
1286 if (!PyArg_ParseTuple(args, "|O:sort", &compare))
1287 return NULL;
1289 savetype = self->ob_type;
1290 self->ob_type = &immutable_list_type;
1291 err = samplesortslice(self->ob_item,
1292 self->ob_item + self->ob_size,
1293 compare);
1294 self->ob_type = savetype;
1295 if (err < 0)
1296 return NULL;
1297 Py_INCREF(Py_None);
1298 return Py_None;
1302 PyList_Sort(PyObject *v)
1304 if (v == NULL || !PyList_Check(v)) {
1305 PyErr_BadInternalCall();
1306 return -1;
1308 v = listsort((PyListObject *)v, (PyObject *)NULL);
1309 if (v == NULL)
1310 return -1;
1311 Py_DECREF(v);
1312 return 0;
1315 static void
1316 _listreverse(PyListObject *self)
1318 register PyObject **p, **q;
1319 register PyObject *tmp;
1321 if (self->ob_size > 1) {
1322 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
1323 p < q;
1324 p++, q--)
1326 tmp = *p;
1327 *p = *q;
1328 *q = tmp;
1333 static PyObject *
1334 listreverse(PyListObject *self)
1336 _listreverse(self);
1337 Py_INCREF(Py_None);
1338 return Py_None;
1342 PyList_Reverse(PyObject *v)
1344 if (v == NULL || !PyList_Check(v)) {
1345 PyErr_BadInternalCall();
1346 return -1;
1348 _listreverse((PyListObject *)v);
1349 return 0;
1352 PyObject *
1353 PyList_AsTuple(PyObject *v)
1355 PyObject *w;
1356 PyObject **p;
1357 int n;
1358 if (v == NULL || !PyList_Check(v)) {
1359 PyErr_BadInternalCall();
1360 return NULL;
1362 n = ((PyListObject *)v)->ob_size;
1363 w = PyTuple_New(n);
1364 if (w == NULL)
1365 return NULL;
1366 p = ((PyTupleObject *)w)->ob_item;
1367 memcpy((void *)p,
1368 (void *)((PyListObject *)v)->ob_item,
1369 n*sizeof(PyObject *));
1370 while (--n >= 0) {
1371 Py_INCREF(*p);
1372 p++;
1374 return w;
1377 static PyObject *
1378 listindex(PyListObject *self, PyObject *v)
1380 int i;
1382 for (i = 0; i < self->ob_size; i++) {
1383 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
1384 if (cmp > 0)
1385 return PyInt_FromLong((long)i);
1386 else if (cmp < 0)
1387 return NULL;
1389 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
1390 return NULL;
1393 static PyObject *
1394 listcount(PyListObject *self, PyObject *v)
1396 int count = 0;
1397 int i;
1399 for (i = 0; i < self->ob_size; i++) {
1400 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
1401 if (cmp > 0)
1402 count++;
1403 else if (cmp < 0)
1404 return NULL;
1406 return PyInt_FromLong((long)count);
1409 static PyObject *
1410 listremove(PyListObject *self, PyObject *v)
1412 int i;
1414 for (i = 0; i < self->ob_size; i++) {
1415 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
1416 if (cmp > 0) {
1417 if (list_ass_slice(self, i, i+1,
1418 (PyObject *)NULL) != 0)
1419 return NULL;
1420 Py_INCREF(Py_None);
1421 return Py_None;
1423 else if (cmp < 0)
1424 return NULL;
1426 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
1427 return NULL;
1430 static int
1431 list_traverse(PyListObject *o, visitproc visit, void *arg)
1433 int i, err;
1434 PyObject *x;
1436 for (i = o->ob_size; --i >= 0; ) {
1437 x = o->ob_item[i];
1438 if (x != NULL) {
1439 err = visit(x, arg);
1440 if (err)
1441 return err;
1444 return 0;
1447 static int
1448 list_clear(PyListObject *lp)
1450 (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0);
1451 return 0;
1454 static PyObject *
1455 list_richcompare(PyObject *v, PyObject *w, int op)
1457 PyListObject *vl, *wl;
1458 int i;
1460 if (!PyList_Check(v) || !PyList_Check(w)) {
1461 Py_INCREF(Py_NotImplemented);
1462 return Py_NotImplemented;
1465 vl = (PyListObject *)v;
1466 wl = (PyListObject *)w;
1468 if (vl->ob_size != wl->ob_size && (op == Py_EQ || op == Py_NE)) {
1469 /* Shortcut: if the lengths differ, the lists differ */
1470 PyObject *res;
1471 if (op == Py_EQ)
1472 res = Py_False;
1473 else
1474 res = Py_True;
1475 Py_INCREF(res);
1476 return res;
1479 /* Search for the first index where items are different */
1480 for (i = 0; i < vl->ob_size && i < wl->ob_size; i++) {
1481 int k = PyObject_RichCompareBool(vl->ob_item[i],
1482 wl->ob_item[i], Py_EQ);
1483 if (k < 0)
1484 return NULL;
1485 if (!k)
1486 break;
1489 if (i >= vl->ob_size || i >= wl->ob_size) {
1490 /* No more items to compare -- compare sizes */
1491 int vs = vl->ob_size;
1492 int ws = wl->ob_size;
1493 int cmp;
1494 PyObject *res;
1495 switch (op) {
1496 case Py_LT: cmp = vs < ws; break;
1497 case Py_LE: cmp = vs <= ws; break;
1498 case Py_EQ: cmp = vs == ws; break;
1499 case Py_NE: cmp = vs != ws; break;
1500 case Py_GT: cmp = vs > ws; break;
1501 case Py_GE: cmp = vs >= ws; break;
1502 default: return NULL; /* cannot happen */
1504 if (cmp)
1505 res = Py_True;
1506 else
1507 res = Py_False;
1508 Py_INCREF(res);
1509 return res;
1512 /* We have an item that differs -- shortcuts for EQ/NE */
1513 if (op == Py_EQ) {
1514 Py_INCREF(Py_False);
1515 return Py_False;
1517 if (op == Py_NE) {
1518 Py_INCREF(Py_True);
1519 return Py_True;
1522 /* Compare the final item again using the proper operator */
1523 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
1526 /* Adapted from newer code by Tim */
1527 static int
1528 list_fill(PyListObject *result, PyObject *v)
1530 PyObject *it; /* iter(v) */
1531 int n; /* guess for result list size */
1532 int i;
1534 n = result->ob_size;
1536 /* Special-case list(a_list), for speed. */
1537 if (PyList_Check(v)) {
1538 if (v == (PyObject *)result)
1539 return 0; /* source is destination, we're done */
1540 return list_ass_slice(result, 0, n, v);
1543 /* Empty previous contents */
1544 if (n != 0) {
1545 if (list_ass_slice(result, 0, n, (PyObject *)NULL) != 0)
1546 return -1;
1549 /* Get iterator. There may be some low-level efficiency to be gained
1550 * by caching the tp_iternext slot instead of using PyIter_Next()
1551 * later, but premature optimization is the root etc.
1553 it = PyObject_GetIter(v);
1554 if (it == NULL)
1555 return -1;
1557 /* Guess a result list size. */
1558 n = -1; /* unknown */
1559 if (PySequence_Check(v) &&
1560 v->ob_type->tp_as_sequence->sq_length) {
1561 n = PySequence_Size(v);
1562 if (n < 0)
1563 PyErr_Clear();
1565 if (n < 0)
1566 n = 8; /* arbitrary */
1567 NRESIZE(result->ob_item, PyObject*, n);
1568 if (result->ob_item == NULL)
1569 goto error;
1570 for (i = 0; i < n; i++)
1571 result->ob_item[i] = NULL;
1572 result->ob_size = n;
1574 /* Run iterator to exhaustion. */
1575 for (i = 0; ; i++) {
1576 PyObject *item = PyIter_Next(it);
1577 if (item == NULL) {
1578 if (PyErr_Occurred())
1579 goto error;
1580 break;
1582 if (i < n)
1583 PyList_SET_ITEM(result, i, item); /* steals ref */
1584 else {
1585 int status = ins1(result, result->ob_size, item);
1586 Py_DECREF(item); /* append creates a new ref */
1587 if (status < 0)
1588 goto error;
1592 /* Cut back result list if initial guess was too large. */
1593 if (i < n && result != NULL) {
1594 if (list_ass_slice(result, i, n, (PyObject *)NULL) != 0)
1595 goto error;
1597 Py_DECREF(it);
1598 return 0;
1600 error:
1601 Py_DECREF(it);
1602 return -1;
1605 static int
1606 list_init(PyListObject *self, PyObject *args, PyObject *kw)
1608 PyObject *arg = NULL;
1609 static char *kwlist[] = {"sequence", 0};
1611 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
1612 return -1;
1613 if (arg != NULL)
1614 return list_fill(self, arg);
1615 if (self->ob_size > 0)
1616 return list_ass_slice(self, 0, self->ob_size, (PyObject*)NULL);
1617 return 0;
1620 static long
1621 list_nohash(PyObject *self)
1623 PyErr_SetString(PyExc_TypeError, "list objects are unhashable");
1624 return -1;
1627 static char append_doc[] =
1628 "L.append(object) -- append object to end";
1629 static char extend_doc[] =
1630 "L.extend(list) -- extend list by appending list elements";
1631 static char insert_doc[] =
1632 "L.insert(index, object) -- insert object before index";
1633 static char pop_doc[] =
1634 "L.pop([index]) -> item -- remove and return item at index (default last)";
1635 static char remove_doc[] =
1636 "L.remove(value) -- remove first occurrence of value";
1637 static char index_doc[] =
1638 "L.index(value) -> integer -- return index of first occurrence of value";
1639 static char count_doc[] =
1640 "L.count(value) -> integer -- return number of occurrences of value";
1641 static char reverse_doc[] =
1642 "L.reverse() -- reverse *IN PLACE*";
1643 static char sort_doc[] =
1644 "L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1646 static PyMethodDef list_methods[] = {
1647 {"append", (PyCFunction)listappend, METH_O, append_doc},
1648 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
1649 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
1650 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
1651 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
1652 {"index", (PyCFunction)listindex, METH_O, index_doc},
1653 {"count", (PyCFunction)listcount, METH_O, count_doc},
1654 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
1655 {"sort", (PyCFunction)listsort, METH_VARARGS, sort_doc},
1656 {NULL, NULL} /* sentinel */
1659 static PySequenceMethods list_as_sequence = {
1660 (inquiry)list_length, /* sq_length */
1661 (binaryfunc)list_concat, /* sq_concat */
1662 (intargfunc)list_repeat, /* sq_repeat */
1663 (intargfunc)list_item, /* sq_item */
1664 (intintargfunc)list_slice, /* sq_slice */
1665 (intobjargproc)list_ass_item, /* sq_ass_item */
1666 (intintobjargproc)list_ass_slice, /* sq_ass_slice */
1667 (objobjproc)list_contains, /* sq_contains */
1668 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
1669 (intargfunc)list_inplace_repeat, /* sq_inplace_repeat */
1672 static char list_doc[] =
1673 "list() -> new list\n"
1674 "list(sequence) -> new list initialized from sequence's items";
1676 PyTypeObject PyList_Type = {
1677 PyObject_HEAD_INIT(&PyType_Type)
1679 "list",
1680 sizeof(PyListObject),
1682 (destructor)list_dealloc, /* tp_dealloc */
1683 (printfunc)list_print, /* tp_print */
1684 0, /* tp_getattr */
1685 0, /* tp_setattr */
1686 0, /* tp_compare */
1687 (reprfunc)list_repr, /* tp_repr */
1688 0, /* tp_as_number */
1689 &list_as_sequence, /* tp_as_sequence */
1690 0, /* tp_as_mapping */
1691 list_nohash, /* tp_hash */
1692 0, /* tp_call */
1693 0, /* tp_str */
1694 PyObject_GenericGetAttr, /* tp_getattro */
1695 0, /* tp_setattro */
1696 0, /* tp_as_buffer */
1697 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1698 Py_TPFLAGS_BASETYPE, /* tp_flags */
1699 list_doc, /* tp_doc */
1700 (traverseproc)list_traverse, /* tp_traverse */
1701 (inquiry)list_clear, /* tp_clear */
1702 list_richcompare, /* tp_richcompare */
1703 0, /* tp_weaklistoffset */
1704 0, /* tp_iter */
1705 0, /* tp_iternext */
1706 list_methods, /* tp_methods */
1707 0, /* tp_members */
1708 0, /* tp_getset */
1709 0, /* tp_base */
1710 0, /* tp_dict */
1711 0, /* tp_descr_get */
1712 0, /* tp_descr_set */
1713 0, /* tp_dictoffset */
1714 (initproc)list_init, /* tp_init */
1715 PyType_GenericAlloc, /* tp_alloc */
1716 PyType_GenericNew, /* tp_new */
1717 _PyObject_GC_Del, /* tp_free */
1721 /* During a sort, we really can't have anyone modifying the list; it could
1722 cause core dumps. Thus, we substitute a dummy type that raises an
1723 explanatory exception when a modifying operation is used. Caveat:
1724 comparisons may behave differently; but I guess it's a bad idea anyway to
1725 compare a list that's being sorted... */
1727 static PyObject *
1728 immutable_list_op(void)
1730 PyErr_SetString(PyExc_TypeError,
1731 "a list cannot be modified while it is being sorted");
1732 return NULL;
1735 static PyMethodDef immutable_list_methods[] = {
1736 {"append", (PyCFunction)immutable_list_op, METH_VARARGS},
1737 {"insert", (PyCFunction)immutable_list_op, METH_VARARGS},
1738 {"extend", (PyCFunction)immutable_list_op, METH_O},
1739 {"pop", (PyCFunction)immutable_list_op, METH_VARARGS},
1740 {"remove", (PyCFunction)immutable_list_op, METH_VARARGS},
1741 {"index", (PyCFunction)listindex, METH_O},
1742 {"count", (PyCFunction)listcount, METH_O},
1743 {"reverse", (PyCFunction)immutable_list_op, METH_VARARGS},
1744 {"sort", (PyCFunction)immutable_list_op, METH_VARARGS},
1745 {NULL, NULL} /* sentinel */
1748 static int
1749 immutable_list_ass(void)
1751 immutable_list_op();
1752 return -1;
1755 static PySequenceMethods immutable_list_as_sequence = {
1756 (inquiry)list_length, /* sq_length */
1757 (binaryfunc)list_concat, /* sq_concat */
1758 (intargfunc)list_repeat, /* sq_repeat */
1759 (intargfunc)list_item, /* sq_item */
1760 (intintargfunc)list_slice, /* sq_slice */
1761 (intobjargproc)immutable_list_ass, /* sq_ass_item */
1762 (intintobjargproc)immutable_list_ass, /* sq_ass_slice */
1763 (objobjproc)list_contains, /* sq_contains */
1766 static PyTypeObject immutable_list_type = {
1767 PyObject_HEAD_INIT(&PyType_Type)
1769 "list (immutable, during sort)",
1770 sizeof(PyListObject),
1772 0, /* Cannot happen */ /* tp_dealloc */
1773 (printfunc)list_print, /* tp_print */
1774 0, /* tp_getattr */
1775 0, /* tp_setattr */
1776 0, /* Won't be called */ /* tp_compare */
1777 (reprfunc)list_repr, /* tp_repr */
1778 0, /* tp_as_number */
1779 &immutable_list_as_sequence, /* tp_as_sequence */
1780 0, /* tp_as_mapping */
1781 list_nohash, /* tp_hash */
1782 0, /* tp_call */
1783 0, /* tp_str */
1784 PyObject_GenericGetAttr, /* tp_getattro */
1785 0, /* tp_setattro */
1786 0, /* tp_as_buffer */
1787 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1788 list_doc, /* tp_doc */
1789 (traverseproc)list_traverse, /* tp_traverse */
1790 0, /* tp_clear */
1791 list_richcompare, /* tp_richcompare */
1792 0, /* tp_weaklistoffset */
1793 0, /* tp_iter */
1794 0, /* tp_iternext */
1795 immutable_list_methods, /* tp_methods */
1796 0, /* tp_members */
1797 0, /* tp_getset */
1798 0, /* tp_base */
1799 0, /* tp_dict */
1800 0, /* tp_descr_get */
1801 0, /* tp_descr_set */
1802 0, /* tp_init */
1803 /* NOTE: This is *not* the standard list_type struct! */