2 /* List object implementation */
9 #include <sys/types.h> /* For size_t */
12 #define ROUNDUP(n, PyTryBlock) \
13 ((((n)+(PyTryBlock)-1)/(PyTryBlock))*(PyTryBlock))
19 return ROUNDUP(n
, 10);
21 return ROUNDUP(n
, 100);
24 #define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems))
33 PyErr_BadInternalCall();
36 nbytes
= size
* sizeof(PyObject
*);
37 /* Check for overflow */
38 if (nbytes
/ sizeof(PyObject
*) != (size_t)size
) {
39 return PyErr_NoMemory();
41 /* PyObject_NewVar is inlined */
42 op
= (PyListObject
*) PyObject_MALLOC(sizeof(PyListObject
)
45 return PyErr_NoMemory();
47 op
= (PyListObject
*) PyObject_FROM_GC(op
);
52 op
->ob_item
= (PyObject
**) PyMem_MALLOC(nbytes
);
53 if (op
->ob_item
== NULL
) {
54 PyObject_FREE(PyObject_AS_GC(op
));
55 return PyErr_NoMemory();
58 PyObject_INIT_VAR(op
, &PyList_Type
, size
);
59 for (i
= 0; i
< size
; i
++)
60 op
->ob_item
[i
] = NULL
;
62 return (PyObject
*) op
;
66 PyList_Size(PyObject
*op
)
68 if (!PyList_Check(op
)) {
69 PyErr_BadInternalCall();
73 return ((PyListObject
*)op
) -> ob_size
;
76 static PyObject
*indexerr
;
79 PyList_GetItem(PyObject
*op
, int i
)
81 if (!PyList_Check(op
)) {
82 PyErr_BadInternalCall();
85 if (i
< 0 || i
>= ((PyListObject
*)op
) -> ob_size
) {
87 indexerr
= PyString_FromString(
88 "list index out of range");
89 PyErr_SetObject(PyExc_IndexError
, indexerr
);
92 return ((PyListObject
*)op
) -> ob_item
[i
];
96 PyList_SetItem(register PyObject
*op
, register int i
,
97 register PyObject
*newitem
)
99 register PyObject
*olditem
;
100 register PyObject
**p
;
101 if (!PyList_Check(op
)) {
103 PyErr_BadInternalCall();
106 if (i
< 0 || i
>= ((PyListObject
*)op
) -> ob_size
) {
108 PyErr_SetString(PyExc_IndexError
,
109 "list assignment index out of range");
112 p
= ((PyListObject
*)op
) -> ob_item
+ i
;
120 ins1(PyListObject
*self
, int where
, PyObject
*v
)
125 PyErr_BadInternalCall();
128 if (self
->ob_size
== INT_MAX
) {
129 PyErr_SetString(PyExc_OverflowError
,
130 "cannot add more objects to list");
133 items
= self
->ob_item
;
134 NRESIZE(items
, PyObject
*, self
->ob_size
+1);
141 if (where
> self
->ob_size
)
142 where
= self
->ob_size
;
143 for (i
= self
->ob_size
; --i
>= where
; )
144 items
[i
+1] = items
[i
];
147 self
->ob_item
= items
;
153 PyList_Insert(PyObject
*op
, int where
, PyObject
*newitem
)
155 if (!PyList_Check(op
)) {
156 PyErr_BadInternalCall();
159 return ins1((PyListObject
*)op
, where
, newitem
);
163 PyList_Append(PyObject
*op
, PyObject
*newitem
)
165 if (!PyList_Check(op
)) {
166 PyErr_BadInternalCall();
169 return ins1((PyListObject
*)op
,
170 (int) ((PyListObject
*)op
)->ob_size
, newitem
);
176 list_dealloc(PyListObject
*op
)
179 Py_TRASHCAN_SAFE_BEGIN(op
)
180 PyObject_GC_Fini(op
);
181 if (op
->ob_item
!= NULL
) {
182 /* Do it backwards, for Christian Tismer.
183 There's a simple test case where somehow this reduces
184 thrashing when a *very* large list is created and
185 immediately deleted. */
188 Py_XDECREF(op
->ob_item
[i
]);
190 PyMem_FREE(op
->ob_item
);
192 op
= (PyListObject
*) PyObject_AS_GC(op
);
194 Py_TRASHCAN_SAFE_END(op
)
198 list_print(PyListObject
*op
, FILE *fp
, int flags
)
202 i
= Py_ReprEnter((PyObject
*)op
);
206 fprintf(fp
, "[...]");
210 for (i
= 0; i
< op
->ob_size
; i
++) {
213 if (PyObject_Print(op
->ob_item
[i
], fp
, 0) != 0) {
214 Py_ReprLeave((PyObject
*)op
);
219 Py_ReprLeave((PyObject
*)op
);
224 list_repr(PyListObject
*v
)
229 i
= Py_ReprEnter((PyObject
*)v
);
232 return PyString_FromString("[...]");
235 s
= PyString_FromString("[");
236 comma
= PyString_FromString(", ");
237 for (i
= 0; i
< v
->ob_size
&& s
!= NULL
; i
++) {
239 PyString_Concat(&s
, comma
);
240 PyString_ConcatAndDel(&s
, PyObject_Repr(v
->ob_item
[i
]));
243 PyString_ConcatAndDel(&s
, PyString_FromString("]"));
244 Py_ReprLeave((PyObject
*)v
);
249 list_compare(PyListObject
*v
, PyListObject
*w
)
253 for (i
= 0; i
< v
->ob_size
&& i
< w
->ob_size
; i
++) {
254 int cmp
= PyObject_Compare(v
->ob_item
[i
], w
->ob_item
[i
]);
258 return v
->ob_size
- w
->ob_size
;
262 list_length(PyListObject
*a
)
270 list_contains(PyListObject
*a
, PyObject
*el
)
274 for (i
= 0; i
< a
->ob_size
; ++i
) {
275 cmp
= PyObject_Compare(el
, PyList_GET_ITEM(a
, i
));
278 if (PyErr_Occurred())
286 list_item(PyListObject
*a
, int i
)
288 if (i
< 0 || i
>= a
->ob_size
) {
289 if (indexerr
== NULL
)
290 indexerr
= PyString_FromString(
291 "list index out of range");
292 PyErr_SetObject(PyExc_IndexError
, indexerr
);
295 Py_INCREF(a
->ob_item
[i
]);
296 return a
->ob_item
[i
];
300 list_slice(PyListObject
*a
, int ilow
, int ihigh
)
306 else if (ilow
> a
->ob_size
)
310 else if (ihigh
> a
->ob_size
)
312 np
= (PyListObject
*) PyList_New(ihigh
- ilow
);
315 for (i
= ilow
; i
< ihigh
; i
++) {
316 PyObject
*v
= a
->ob_item
[i
];
318 np
->ob_item
[i
- ilow
] = v
;
320 return (PyObject
*)np
;
324 PyList_GetSlice(PyObject
*a
, int ilow
, int ihigh
)
326 if (!PyList_Check(a
)) {
327 PyErr_BadInternalCall();
330 return list_slice((PyListObject
*)a
, ilow
, ihigh
);
334 list_concat(PyListObject
*a
, PyObject
*bb
)
339 if (!PyList_Check(bb
)) {
340 PyErr_Format(PyExc_TypeError
,
341 "can only concatenate list (not \"%.200s\") to list",
342 bb
->ob_type
->tp_name
);
345 #define b ((PyListObject *)bb)
346 size
= a
->ob_size
+ b
->ob_size
;
347 np
= (PyListObject
*) PyList_New(size
);
351 for (i
= 0; i
< a
->ob_size
; i
++) {
352 PyObject
*v
= a
->ob_item
[i
];
356 for (i
= 0; i
< b
->ob_size
; i
++) {
357 PyObject
*v
= b
->ob_item
[i
];
359 np
->ob_item
[i
+ a
->ob_size
] = v
;
361 return (PyObject
*)np
;
366 list_repeat(PyListObject
*a
, int n
)
374 size
= a
->ob_size
* n
;
375 np
= (PyListObject
*) PyList_New(size
);
379 for (i
= 0; i
< n
; i
++) {
380 for (j
= 0; j
< a
->ob_size
; j
++) {
386 return (PyObject
*) np
;
390 list_ass_slice(PyListObject
*a
, int ilow
, int ihigh
, PyObject
*v
)
392 /* Because [X]DECREF can recursively invoke list operations on
393 this list, we must postpone all [X]DECREF activity until
394 after the list is back in its canonical shape. Therefore
395 we must allocate an additional array, 'recycle', into which
396 we temporarily copy the items that are deleted from the
398 PyObject
**recycle
, **p
;
400 int n
; /* Size of replacement list */
401 int d
; /* Change in size */
402 int k
; /* Loop index */
403 #define b ((PyListObject *)v)
406 else if (PyList_Check(v
)) {
409 /* Special case "a[i:j] = a" -- copy b first */
411 v
= list_slice(b
, 0, n
);
412 ret
= list_ass_slice(a
, ilow
, ihigh
, v
);
418 PyErr_Format(PyExc_TypeError
,
419 "must assign list (not \"%.200s\") to slice",
420 v
->ob_type
->tp_name
);
425 else if (ilow
> a
->ob_size
)
429 else if (ihigh
> a
->ob_size
)
432 d
= n
- (ihigh
-ilow
);
434 p
= recycle
= PyMem_NEW(PyObject
*, (ihigh
-ilow
));
437 if (d
<= 0) { /* Delete -d items; recycle ihigh-ilow items */
438 for (k
= ilow
; k
< ihigh
; k
++)
441 for (/*k = ihigh*/; k
< a
->ob_size
; k
++)
444 NRESIZE(item
, PyObject
*, a
->ob_size
); /* Can't fail */
448 else { /* Insert d items; recycle ihigh-ilow items */
449 NRESIZE(item
, PyObject
*, a
->ob_size
+ d
);
456 for (k
= a
->ob_size
; --k
>= ihigh
; )
458 for (/*k = ihigh-1*/; k
>= ilow
; --k
)
463 for (k
= 0; k
< n
; k
++, ilow
++) {
464 PyObject
*w
= b
->ob_item
[k
];
469 while (--p
>= recycle
)
478 PyList_SetSlice(PyObject
*a
, int ilow
, int ihigh
, PyObject
*v
)
480 if (!PyList_Check(a
)) {
481 PyErr_BadInternalCall();
484 return list_ass_slice((PyListObject
*)a
, ilow
, ihigh
, v
);
488 list_inplace_repeat(PyListObject
*self
, int n
)
494 size
= PyList_GET_SIZE(self
);
497 return (PyObject
*)self
;
500 items
= self
->ob_item
;
503 self
->ob_item
= NULL
;
505 for (i
= 0; i
< size
; i
++)
506 Py_XDECREF(items
[i
]);
509 return (PyObject
*)self
;
512 NRESIZE(items
, PyObject
*, size
*n
);
517 self
->ob_item
= items
;
518 for (i
= 1; i
< n
; i
++) { /* Start counting at 1, not 0 */
519 for (j
= 0; j
< size
; j
++) {
520 PyObject
*o
= PyList_GET_ITEM(self
, j
);
522 PyList_SET_ITEM(self
, self
->ob_size
++, o
);
526 return (PyObject
*)self
;
532 list_ass_item(PyListObject
*a
, int i
, PyObject
*v
)
535 if (i
< 0 || i
>= a
->ob_size
) {
536 PyErr_SetString(PyExc_IndexError
,
537 "list assignment index out of range");
541 return list_ass_slice(a
, i
, i
+1, v
);
543 old_value
= a
->ob_item
[i
];
545 Py_DECREF(old_value
);
550 ins(PyListObject
*self
, int where
, PyObject
*v
)
552 if (ins1(self
, where
, v
) != 0)
559 listinsert(PyListObject
*self
, PyObject
*args
)
563 if (!PyArg_ParseTuple(args
, "iO:insert", &i
, &v
))
565 return ins(self
, i
, v
);
568 /* Define NO_STRICT_LIST_APPEND to enable multi-argument append() */
570 #ifndef NO_STRICT_LIST_APPEND
571 #define PyArg_ParseTuple_Compat1 PyArg_ParseTuple
573 #define PyArg_ParseTuple_Compat1(args, format, ret) \
575 PyTuple_GET_SIZE(args) > 1 ? (*ret = args, 1) : \
576 PyTuple_GET_SIZE(args) == 1 ? (*ret = PyTuple_GET_ITEM(args, 0), 1) : \
577 PyArg_ParseTuple(args, format, ret) \
583 listappend(PyListObject
*self
, PyObject
*args
)
586 if (!PyArg_ParseTuple_Compat1(args
, "O:append", &v
))
588 return ins(self
, (int) self
->ob_size
, v
);
592 listextend_internal(PyListObject
*self
, PyObject
*b
)
595 int selflen
= PyList_GET_SIZE(self
);
599 if (PyObject_Size(b
) == 0)
600 /* short circuit when b is empty */
603 if (self
== (PyListObject
*)b
) {
604 /* as in list_ass_slice() we must special case the
605 * situation: a.extend(a)
607 * XXX: I think this way ought to be faster than using
608 * list_slice() the way list_ass_slice() does.
611 b
= PyList_New(selflen
);
614 for (i
= 0; i
< selflen
; i
++) {
615 PyObject
*o
= PyList_GET_ITEM(self
, i
);
617 PyList_SET_ITEM(b
, i
, o
);
621 blen
= PyObject_Size(b
);
623 /* resize a using idiom */
624 items
= self
->ob_item
;
625 NRESIZE(items
, PyObject
*, selflen
+ blen
);
632 self
->ob_item
= items
;
634 /* populate the end of self with b's items */
635 for (i
= 0; i
< blen
; i
++) {
636 PyObject
*o
= PySequence_Fast_GET_ITEM(b
, i
);
638 PyList_SET_ITEM(self
, self
->ob_size
++, o
);
646 list_inplace_concat(PyListObject
*self
, PyObject
*other
)
648 other
= PySequence_Fast(other
, "argument to += must be a sequence");
652 if (listextend_internal(self
, other
) < 0)
656 return (PyObject
*)self
;
660 listextend(PyListObject
*self
, PyObject
*args
)
665 if (!PyArg_ParseTuple(args
, "O:extend", &b
))
668 b
= PySequence_Fast(b
, "list.extend() argument must be a sequence");
672 if (listextend_internal(self
, b
) < 0)
680 listpop(PyListObject
*self
, PyObject
*args
)
684 if (!PyArg_ParseTuple(args
, "|i:pop", &i
))
686 if (self
->ob_size
== 0) {
687 /* Special-case most common failure cause */
688 PyErr_SetString(PyExc_IndexError
, "pop from empty list");
693 if (i
< 0 || i
>= self
->ob_size
) {
694 PyErr_SetString(PyExc_IndexError
, "pop index out of range");
697 v
= self
->ob_item
[i
];
699 if (list_ass_slice(self
, i
, i
+1, (PyObject
*)NULL
) != 0) {
706 /* New quicksort implementation for arrays of object pointers.
707 Thanks to discussions with Tim Peters. */
709 /* CMPERROR is returned by our comparison function when an error
710 occurred. This is the largest negative integer (0x80000000 on a
712 #define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
714 /* Comparison function. Takes care of calling a user-supplied
715 comparison function (any callable Python object). Calls the
716 standard comparison function, PyObject_Compare(), if the user-
717 supplied function is NULL. */
720 docompare(PyObject
*x
, PyObject
*y
, PyObject
*compare
)
722 PyObject
*args
, *res
;
725 if (compare
== NULL
) {
726 i
= PyObject_Compare(x
, y
);
727 if (i
&& PyErr_Occurred())
732 args
= Py_BuildValue("(OO)", x
, y
);
735 res
= PyEval_CallObject(compare
, args
);
739 if (!PyInt_Check(res
)) {
741 PyErr_SetString(PyExc_TypeError
,
742 "comparison function must return int");
745 i
= PyInt_AsLong(res
);
754 /* MINSIZE is the smallest array that will get a full-blown samplesort
755 treatment; smaller arrays are sorted using binary insertion. It must
756 be at least 7 for the samplesort implementation to work. Binary
757 insertion does fewer compares, but can suffer O(N**2) data movement.
758 The more expensive compares, the larger MINSIZE should be. */
761 /* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
762 partition; smaller slices are passed to binarysort. It must be at
763 least 2, and no larger than MINSIZE. Setting it higher reduces the #
764 of compares slowly, but increases the amount of data movement quickly.
765 The value here was chosen assuming a compare costs ~25x more than
766 swapping a pair of memory-resident pointers -- but under that assumption,
767 changing the value by a few dozen more or less has aggregate effect
768 under 1%. So the value is crucial, but not touchy <wink>. */
769 #define MINPARTITIONSIZE 40
771 /* MAXMERGE is the largest number of elements we'll always merge into
772 a known-to-be sorted chunk via binary insertion, regardless of the
773 size of that chunk. Given a chunk of N sorted elements, and a group
774 of K unknowns, the largest K for which it's better to do insertion
775 (than a full-blown sort) is a complicated function of N and K mostly
776 involving the expected number of compares and data moves under each
777 approach, and the relative cost of those operations on a specific
778 architecure. The fixed value here is conservative, and should be a
779 clear win regardless of architecture or N. */
782 /* STACKSIZE is the size of our work stack. A rough estimate is that
783 this allows us to sort arrays of size N where
784 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
785 for arrays of size 2**64. Because we push the biggest partition
786 first, the worst case occurs when all subarrays are always partitioned
791 #define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
793 /* binarysort is the best method for sorting small arrays: it does
794 few compares, but can do data movement quadratic in the number of
796 [lo, hi) is a contiguous slice of a list, and is sorted via
798 On entry, must have lo <= start <= hi, and that [lo, start) is already
799 sorted (pass start == lo if you don't know!).
800 If docompare complains (returns CMPERROR) return -1, else 0.
801 Even in case of error, the output slice will be some permutation of
802 the input (nothing is lost or duplicated).
806 binarysort(PyObject
**lo
, PyObject
**hi
, PyObject
**start
, PyObject
*compare
)
807 /* compare -- comparison function object, or NULL for default */
809 /* assert lo <= start <= hi
810 assert [lo, start) is sorted */
812 register PyObject
**l
, **p
, **r
;
813 register PyObject
*pivot
;
817 for (; start
< hi
; ++start
) {
818 /* set l to where *start belongs */
823 p
= l
+ ((r
- l
) >> 1);
830 /* Pivot should go at l -- slide over to make room.
831 Caution: using memmove is much slower under MSVC 5;
832 we're not usually moving many slots. */
833 for (p
= start
; p
> l
; --p
)
843 /* samplesortslice is the sorting workhorse.
844 [lo, hi) is a contiguous slice of a list, to be sorted in place.
845 On entry, must have lo <= hi,
846 If docompare complains (returns CMPERROR) return -1, else 0.
847 Even in case of error, the output slice will be some permutation of
848 the input (nothing is lost or duplicated).
850 samplesort is basically quicksort on steroids: a power of 2 close
851 to n/ln(n) is computed, and that many elements (less 1) are picked at
852 random from the array and sorted. These 2**k - 1 elements are then
853 used as preselected pivots for an equal number of quicksort
854 partitioning steps, partitioning the slice into 2**k chunks each of
855 size about ln(n). These small final chunks are then usually handled
856 by binarysort. Note that when k=1, this is roughly the same as an
857 ordinary quicksort using a random pivot, and when k=2 this is roughly
858 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
859 this a "median of n/ln(n)" quicksort. You can also view it as a kind
860 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
862 The large number of samples makes a quadratic-time case almost
863 impossible, and asymptotically drives the average-case number of
864 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
865 3 variant) down to N lg N.
867 We also play lots of low-level tricks to cut the number of compares.
869 Very obscure: To avoid using extra memory, the PPs are stored in the
870 array and shuffled around as partitioning proceeds. At the start of a
871 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
872 adjacent (either on the left or the right!) to a chunk of X elements
873 that are to be partitioned: P X or X P. In either case we need to
874 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
875 left, followed by the PP to be used for this step (that's the middle
876 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
877 P X or X P -> Psmall pivot X Plarge
878 and the order of the PPs must not be altered. It can take a while
879 to realize this isn't trivial! It can take even longer <wink> to
880 understand why the simple code below works, using only 2**(m-1) swaps.
881 The key is that the order of the X elements isn't necessarily
882 preserved: X can end up as some cyclic permutation of its original
883 order. That's OK, because X is unsorted anyway. If the order of X
884 had to be preserved too, the simplest method I know of using O(1)
885 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
886 Since len(X) is typically several times larger than 2**(m-1), that
887 would slow things down.
890 struct SamplesortStackNode
{
891 /* Represents a slice of the array, from (& including) lo up
892 to (but excluding) hi. "extra" additional & adjacent elements
893 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
894 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
895 already sorted, but nothing is known about the other elements
896 in [lo, hi). |extra| is always one less than a power of 2.
897 When extra is 0, we're out of PPs, and the slice must be
898 sorted by some other means. */
904 /* The number of PPs we want is 2**k - 1, where 2**k is as close to
905 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
906 is undesirable, so cutoff values are canned in the "cutoff" table
907 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
909 static long cutoff
[] = {
910 43, /* smallest N such that k == 4 */
932 982548444, /* smallest N such that k == 26 */
933 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
937 samplesortslice(PyObject
**lo
, PyObject
**hi
, PyObject
*compare
)
938 /* compare -- comparison function object, or NULL for default */
940 register PyObject
**l
, **r
;
941 register PyObject
*tmp
, *pivot
;
943 int n
, extra
, top
, extraOnRight
;
944 struct SamplesortStackNode stack
[STACKSIZE
];
946 /* assert lo <= hi */
949 /* ----------------------------------------------------------
951 * --------------------------------------------------------*/
955 /* Set r to the largest value such that [lo,r) is sorted.
956 This catches the already-sorted case, the all-the-same
957 case, and the appended-a-few-elements-to-a-sorted-list case.
958 If the array is unsorted, we're very likely to get out of
959 the loop fast, so the test is cheap if it doesn't pay off.
962 for (r
= lo
+1; r
< hi
; ++r
) {
967 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
968 few unknowns, or few elements in total. */
969 if (hi
- r
<= MAXMERGE
|| n
< MINSIZE
)
970 return binarysort(lo
, hi
, r
, compare
);
972 /* Check for the array already being reverse-sorted. Typical
973 benchmark-driven silliness <wink>. */
975 for (r
= lo
+1; r
< hi
; ++r
) {
980 if (hi
- r
<= MAXMERGE
) {
981 /* Reverse the reversed prefix, then insert the tail */
982 PyObject
**originalr
= r
;
986 tmp
= *l
; *l
= *r
; *r
= tmp
;
989 return binarysort(lo
, hi
, originalr
, compare
);
992 /* ----------------------------------------------------------
993 * Normal case setup: a large array without obvious pattern.
994 * --------------------------------------------------------*/
996 /* extra := a power of 2 ~= n/ln(n), less 1.
997 First find the smallest extra s.t. n < cutoff[extra] */
999 extra
< sizeof(cutoff
) / sizeof(cutoff
[0]);
1001 if (n
< cutoff
[extra
])
1003 /* note that if we fall out of the loop, the value of
1004 extra still makes *sense*, but may be smaller than
1005 we would like (but the array has more than ~= 2**31
1006 elements in this case!) */
1008 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1009 have is CUTOFFBASE-1, so
1010 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1011 extra
= (1 << (extra
- 1 + CUTOFFBASE
)) - 1;
1012 /* assert extra > 0 and n >= extra */
1014 /* Swap that many values to the start of the array. The
1015 selection of elements is pseudo-random, but the same on
1016 every run (this is intentional! timing algorithm changes is
1017 a pain if timing varies across runs). */
1019 unsigned int seed
= n
/ extra
; /* arbitrary */
1021 for (i
= 0; i
< (unsigned)extra
; ++i
) {
1022 /* j := random int in [i, n) */
1024 seed
= seed
* 69069 + 7;
1025 j
= i
+ seed
% (n
- i
);
1026 tmp
= lo
[i
]; lo
[i
] = lo
[j
]; lo
[j
] = tmp
;
1030 /* Recursively sort the preselected pivots. */
1031 if (samplesortslice(lo
, lo
+ extra
, compare
) < 0)
1034 top
= 0; /* index of available stack slot */
1035 lo
+= extra
; /* point to first unknown */
1036 extraOnRight
= 0; /* the PPs are at the left end */
1038 /* ----------------------------------------------------------
1039 * Partition [lo, hi), and repeat until out of work.
1040 * --------------------------------------------------------*/
1042 /* assert lo <= hi, so n >= 0 */
1045 /* We may not want, or may not be able, to partition:
1046 If n is small, it's quicker to insert.
1047 If extra is 0, we're out of pivots, and *must* use
1050 if (n
< MINPARTITIONSIZE
|| extra
== 0) {
1052 /* assert extra == 0
1053 This is rare, since the average size
1054 of a final block is only about
1056 if (samplesortslice(lo
, hi
, compare
) < 0)
1060 /* Binary insertion should be quicker,
1061 and we can take advantage of the PPs
1062 already being sorted. */
1063 if (extraOnRight
&& extra
) {
1064 /* swap the PPs to the left end */
1073 if (binarysort(lo
- extra
, hi
, lo
,
1078 /* Find another slice to work on. */
1080 break; /* no more -- done! */
1083 extra
= stack
[top
].extra
;
1092 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1093 Then our preselected pivot is at (extra-1)/2, and we
1094 want to move the PPs before that to the left end of
1095 the slice, and the PPs after that to the right end.
1096 The following section changes extra, lo, hi, and the
1098 [lo-extra, lo) contains the smaller PPs.
1100 (lo, hi) contains the unknown elements.
1101 [hi, hi+extra) contains the larger PPs.
1103 k
= extra
>>= 1; /* num PPs to move */
1105 /* Swap the smaller PPs to the left end.
1106 Note that this loop actually moves k+1 items:
1107 the last is our PP */
1109 tmp
= *lo
; *lo
= *hi
; *hi
= tmp
;
1114 /* Swap the larger PPs to the right end. */
1117 tmp
= *lo
; *lo
= *hi
; *hi
= tmp
;
1120 --lo
; /* *lo is now our PP */
1123 /* Now an almost-ordinary quicksort partition step.
1124 Note that most of the time is spent here!
1125 Only odd thing is that we partition into < and >=,
1126 instead of the usual <= and >=. This helps when
1127 there are lots of duplicates of different values,
1128 because it eventually tends to make subfiles
1129 "pure" (all duplicates), and we special-case for
1130 duplicates later. */
1133 /* assert lo < l < r < hi (small n weeded out above) */
1136 /* slide l right, looking for key >= pivot */
1145 /* slide r left, looking for key < pivot */
1147 register PyObject
*rval
= *r
--;
1150 /* swap and advance */
1159 /* assert lo < r <= l < hi
1160 assert r == l or r+1 == l
1161 everything to the left of l is < pivot, and
1162 everything to the right of r is >= pivot */
1171 /* assert lo <= r and r+1 == l and l <= hi
1172 assert r == lo or a[r] < pivot
1173 assert a[lo] is pivot
1174 assert l == hi or a[l] >= pivot
1175 Swap the pivot into "the middle", so we can henceforth
1181 /* The following is true now, & will be preserved:
1182 All in [lo,r) are < pivot
1183 All in [r,l) == pivot (& so can be ignored)
1184 All in [l,hi) are >= pivot */
1186 /* Check for duplicates of the pivot. One compare is
1187 wasted if there are no duplicates, but can win big
1189 Tricky: we're sticking to "<" compares, so deduce
1190 equality indirectly. We know pivot <= *l, so they're
1191 equal iff not pivot < *l.
1194 /* pivot <= *l known */
1199 /* <= and not < implies == */
1203 /* assert lo <= r < l <= hi
1204 Partitions are [lo, r) and [l, hi) */
1206 /* push fattest first; remember we still have extra PPs
1207 to the left of the left chunk and to the right of
1209 /* assert top < STACKSIZE */
1210 if (r
- lo
<= hi
- l
) {
1211 /* second is bigger */
1214 stack
[top
].extra
= -extra
;
1219 /* first is bigger */
1222 stack
[top
].extra
= extra
;
1228 } /* end of partitioning loop */
1238 staticforward PyTypeObject immutable_list_type
;
1241 listsort(PyListObject
*self
, PyObject
*args
)
1244 PyObject
*compare
= NULL
;
1247 if (!PyArg_ParseTuple(args
, "|O:sort", &compare
))
1250 self
->ob_type
= &immutable_list_type
;
1251 err
= samplesortslice(self
->ob_item
,
1252 self
->ob_item
+ self
->ob_size
,
1254 self
->ob_type
= &PyList_Type
;
1262 PyList_Sort(PyObject
*v
)
1264 if (v
== NULL
|| !PyList_Check(v
)) {
1265 PyErr_BadInternalCall();
1268 v
= listsort((PyListObject
*)v
, (PyObject
*)NULL
);
1276 _listreverse(PyListObject
*self
)
1278 register PyObject
**p
, **q
;
1279 register PyObject
*tmp
;
1281 if (self
->ob_size
> 1) {
1282 for (p
= self
->ob_item
, q
= self
->ob_item
+ self
->ob_size
- 1;
1294 listreverse(PyListObject
*self
, PyObject
*args
)
1296 if (!PyArg_ParseTuple(args
, ":reverse"))
1304 PyList_Reverse(PyObject
*v
)
1306 if (v
== NULL
|| !PyList_Check(v
)) {
1307 PyErr_BadInternalCall();
1310 _listreverse((PyListObject
*)v
);
1315 PyList_AsTuple(PyObject
*v
)
1320 if (v
== NULL
|| !PyList_Check(v
)) {
1321 PyErr_BadInternalCall();
1324 n
= ((PyListObject
*)v
)->ob_size
;
1328 p
= ((PyTupleObject
*)w
)->ob_item
;
1330 (void *)((PyListObject
*)v
)->ob_item
,
1331 n
*sizeof(PyObject
*));
1340 listindex(PyListObject
*self
, PyObject
*args
)
1345 if (!PyArg_ParseTuple_Compat1(args
, "O:index", &v
))
1347 for (i
= 0; i
< self
->ob_size
; i
++) {
1348 if (PyObject_Compare(self
->ob_item
[i
], v
) == 0)
1349 return PyInt_FromLong((long)i
);
1350 if (PyErr_Occurred())
1353 PyErr_SetString(PyExc_ValueError
, "list.index(x): x not in list");
1358 listcount(PyListObject
*self
, PyObject
*args
)
1364 if (!PyArg_ParseTuple_Compat1(args
, "O:count", &v
))
1366 for (i
= 0; i
< self
->ob_size
; i
++) {
1367 if (PyObject_Compare(self
->ob_item
[i
], v
) == 0)
1369 if (PyErr_Occurred())
1372 return PyInt_FromLong((long)count
);
1376 listremove(PyListObject
*self
, PyObject
*args
)
1381 if (!PyArg_ParseTuple_Compat1(args
, "O:remove", &v
))
1383 for (i
= 0; i
< self
->ob_size
; i
++) {
1384 if (PyObject_Compare(self
->ob_item
[i
], v
) == 0) {
1385 if (list_ass_slice(self
, i
, i
+1,
1386 (PyObject
*)NULL
) != 0)
1391 if (PyErr_Occurred())
1394 PyErr_SetString(PyExc_ValueError
, "list.remove(x): x not in list");
1399 list_traverse(PyListObject
*o
, visitproc visit
, void *arg
)
1404 for (i
= o
->ob_size
; --i
>= 0; ) {
1407 err
= visit(x
, arg
);
1416 list_clear(PyListObject
*lp
)
1418 (void) PyList_SetSlice((PyObject
*)lp
, 0, lp
->ob_size
, 0);
1422 static char append_doc
[] =
1423 "L.append(object) -- append object to end";
1424 static char extend_doc
[] =
1425 "L.extend(list) -- extend list by appending list elements";
1426 static char insert_doc
[] =
1427 "L.insert(index, object) -- insert object before index";
1428 static char pop_doc
[] =
1429 "L.pop([index]) -> item -- remove and return item at index (default last)";
1430 static char remove_doc
[] =
1431 "L.remove(value) -- remove first occurrence of value";
1432 static char index_doc
[] =
1433 "L.index(value) -> integer -- return index of first occurrence of value";
1434 static char count_doc
[] =
1435 "L.count(value) -> integer -- return number of occurrences of value";
1436 static char reverse_doc
[] =
1437 "L.reverse() -- reverse *IN PLACE*";
1438 static char sort_doc
[] =
1439 "L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1441 static PyMethodDef list_methods
[] = {
1442 {"append", (PyCFunction
)listappend
, 1, append_doc
},
1443 {"insert", (PyCFunction
)listinsert
, 1, insert_doc
},
1444 {"extend", (PyCFunction
)listextend
, 1, extend_doc
},
1445 {"pop", (PyCFunction
)listpop
, 1, pop_doc
},
1446 {"remove", (PyCFunction
)listremove
, 1, remove_doc
},
1447 {"index", (PyCFunction
)listindex
, 1, index_doc
},
1448 {"count", (PyCFunction
)listcount
, 1, count_doc
},
1449 {"reverse", (PyCFunction
)listreverse
, 1, reverse_doc
},
1450 {"sort", (PyCFunction
)listsort
, 1, sort_doc
},
1451 {NULL
, NULL
} /* sentinel */
1455 list_getattr(PyListObject
*f
, char *name
)
1457 return Py_FindMethod(list_methods
, (PyObject
*)f
, name
);
1460 static PySequenceMethods list_as_sequence
= {
1461 (inquiry
)list_length
, /*sq_length*/
1462 (binaryfunc
)list_concat
, /*sq_concat*/
1463 (intargfunc
)list_repeat
, /*sq_repeat*/
1464 (intargfunc
)list_item
, /*sq_item*/
1465 (intintargfunc
)list_slice
, /*sq_slice*/
1466 (intobjargproc
)list_ass_item
, /*sq_ass_item*/
1467 (intintobjargproc
)list_ass_slice
, /*sq_ass_slice*/
1468 (objobjproc
)list_contains
, /*sq_contains*/
1469 (binaryfunc
)list_inplace_concat
, /*sq_inplace_concat*/
1470 (intargfunc
)list_inplace_repeat
, /*sq_inplace_repeat*/
1473 PyTypeObject PyList_Type
= {
1474 PyObject_HEAD_INIT(&PyType_Type
)
1477 sizeof(PyListObject
) + PyGC_HEAD_SIZE
,
1479 (destructor
)list_dealloc
, /*tp_dealloc*/
1480 (printfunc
)list_print
, /*tp_print*/
1481 (getattrfunc
)list_getattr
, /*tp_getattr*/
1483 (cmpfunc
)list_compare
, /*tp_compare*/
1484 (reprfunc
)list_repr
, /*tp_repr*/
1486 &list_as_sequence
, /*tp_as_sequence*/
1487 0, /*tp_as_mapping*/
1494 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_GC
, /*tp_flags*/
1496 (traverseproc
)list_traverse
, /* tp_traverse */
1497 (inquiry
)list_clear
, /* tp_clear */
1501 /* During a sort, we really can't have anyone modifying the list; it could
1502 cause core dumps. Thus, we substitute a dummy type that raises an
1503 explanatory exception when a modifying operation is used. Caveat:
1504 comparisons may behave differently; but I guess it's a bad idea anyway to
1505 compare a list that's being sorted... */
1508 immutable_list_op(void)
1510 PyErr_SetString(PyExc_TypeError
,
1511 "a list cannot be modified while it is being sorted");
1515 static PyMethodDef immutable_list_methods
[] = {
1516 {"append", (PyCFunction
)immutable_list_op
},
1517 {"insert", (PyCFunction
)immutable_list_op
},
1518 {"remove", (PyCFunction
)immutable_list_op
},
1519 {"index", (PyCFunction
)listindex
},
1520 {"count", (PyCFunction
)listcount
},
1521 {"reverse", (PyCFunction
)immutable_list_op
},
1522 {"sort", (PyCFunction
)immutable_list_op
},
1523 {NULL
, NULL
} /* sentinel */
1527 immutable_list_getattr(PyListObject
*f
, char *name
)
1529 return Py_FindMethod(immutable_list_methods
, (PyObject
*)f
, name
);
1533 immutable_list_ass(void)
1535 immutable_list_op();
1539 static PySequenceMethods immutable_list_as_sequence
= {
1540 (inquiry
)list_length
, /*sq_length*/
1541 (binaryfunc
)list_concat
, /*sq_concat*/
1542 (intargfunc
)list_repeat
, /*sq_repeat*/
1543 (intargfunc
)list_item
, /*sq_item*/
1544 (intintargfunc
)list_slice
, /*sq_slice*/
1545 (intobjargproc
)immutable_list_ass
, /*sq_ass_item*/
1546 (intintobjargproc
)immutable_list_ass
, /*sq_ass_slice*/
1547 (objobjproc
)list_contains
, /*sq_contains*/
1550 static PyTypeObject immutable_list_type
= {
1551 PyObject_HEAD_INIT(&PyType_Type
)
1553 "list (immutable, during sort)",
1554 sizeof(PyListObject
) + PyGC_HEAD_SIZE
,
1556 0, /*tp_dealloc*/ /* Cannot happen */
1557 (printfunc
)list_print
, /*tp_print*/
1558 (getattrfunc
)immutable_list_getattr
, /*tp_getattr*/
1560 0, /*tp_compare*/ /* Won't be called */
1561 (reprfunc
)list_repr
, /*tp_repr*/
1563 &immutable_list_as_sequence
, /*tp_as_sequence*/
1564 0, /*tp_as_mapping*/
1571 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_GC
, /*tp_flags*/
1573 (traverseproc
)list_traverse
, /* tp_traverse */