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_length(PyListObject
*a
)
257 list_contains(PyListObject
*a
, PyObject
*el
)
261 for (i
= 0; i
< a
->ob_size
; ++i
) {
262 int cmp
= PyObject_RichCompareBool(el
, PyList_GET_ITEM(a
, i
),
274 list_item(PyListObject
*a
, int i
)
276 if (i
< 0 || i
>= a
->ob_size
) {
277 if (indexerr
== NULL
)
278 indexerr
= PyString_FromString(
279 "list index out of range");
280 PyErr_SetObject(PyExc_IndexError
, indexerr
);
283 Py_INCREF(a
->ob_item
[i
]);
284 return a
->ob_item
[i
];
288 list_slice(PyListObject
*a
, int ilow
, int ihigh
)
294 else if (ilow
> a
->ob_size
)
298 else if (ihigh
> a
->ob_size
)
300 np
= (PyListObject
*) PyList_New(ihigh
- ilow
);
303 for (i
= ilow
; i
< ihigh
; i
++) {
304 PyObject
*v
= a
->ob_item
[i
];
306 np
->ob_item
[i
- ilow
] = v
;
308 return (PyObject
*)np
;
312 PyList_GetSlice(PyObject
*a
, int ilow
, int ihigh
)
314 if (!PyList_Check(a
)) {
315 PyErr_BadInternalCall();
318 return list_slice((PyListObject
*)a
, ilow
, ihigh
);
322 list_concat(PyListObject
*a
, PyObject
*bb
)
327 if (!PyList_Check(bb
)) {
328 PyErr_Format(PyExc_TypeError
,
329 "can only concatenate list (not \"%.200s\") to list",
330 bb
->ob_type
->tp_name
);
333 #define b ((PyListObject *)bb)
334 size
= a
->ob_size
+ b
->ob_size
;
335 np
= (PyListObject
*) PyList_New(size
);
339 for (i
= 0; i
< a
->ob_size
; i
++) {
340 PyObject
*v
= a
->ob_item
[i
];
344 for (i
= 0; i
< b
->ob_size
; i
++) {
345 PyObject
*v
= b
->ob_item
[i
];
347 np
->ob_item
[i
+ a
->ob_size
] = v
;
349 return (PyObject
*)np
;
354 list_repeat(PyListObject
*a
, int n
)
362 size
= a
->ob_size
* n
;
363 np
= (PyListObject
*) PyList_New(size
);
367 for (i
= 0; i
< n
; i
++) {
368 for (j
= 0; j
< a
->ob_size
; j
++) {
374 return (PyObject
*) np
;
378 list_ass_slice(PyListObject
*a
, int ilow
, int ihigh
, PyObject
*v
)
380 /* Because [X]DECREF can recursively invoke list operations on
381 this list, we must postpone all [X]DECREF activity until
382 after the list is back in its canonical shape. Therefore
383 we must allocate an additional array, 'recycle', into which
384 we temporarily copy the items that are deleted from the
386 PyObject
**recycle
, **p
;
388 int n
; /* Size of replacement list */
389 int d
; /* Change in size */
390 int k
; /* Loop index */
391 #define b ((PyListObject *)v)
394 else if (PyList_Check(v
)) {
397 /* Special case "a[i:j] = a" -- copy b first */
399 v
= list_slice(b
, 0, n
);
400 ret
= list_ass_slice(a
, ilow
, ihigh
, v
);
406 PyErr_Format(PyExc_TypeError
,
407 "must assign list (not \"%.200s\") to slice",
408 v
->ob_type
->tp_name
);
413 else if (ilow
> a
->ob_size
)
417 else if (ihigh
> a
->ob_size
)
420 d
= n
- (ihigh
-ilow
);
422 p
= recycle
= PyMem_NEW(PyObject
*, (ihigh
-ilow
));
425 if (d
<= 0) { /* Delete -d items; recycle ihigh-ilow items */
426 for (k
= ilow
; k
< ihigh
; k
++)
429 for (/*k = ihigh*/; k
< a
->ob_size
; k
++)
432 NRESIZE(item
, PyObject
*, a
->ob_size
); /* Can't fail */
436 else { /* Insert d items; recycle ihigh-ilow items */
437 NRESIZE(item
, PyObject
*, a
->ob_size
+ d
);
444 for (k
= a
->ob_size
; --k
>= ihigh
; )
446 for (/*k = ihigh-1*/; k
>= ilow
; --k
)
451 for (k
= 0; k
< n
; k
++, ilow
++) {
452 PyObject
*w
= b
->ob_item
[k
];
457 while (--p
>= recycle
)
466 PyList_SetSlice(PyObject
*a
, int ilow
, int ihigh
, PyObject
*v
)
468 if (!PyList_Check(a
)) {
469 PyErr_BadInternalCall();
472 return list_ass_slice((PyListObject
*)a
, ilow
, ihigh
, v
);
476 list_inplace_repeat(PyListObject
*self
, int n
)
482 size
= PyList_GET_SIZE(self
);
485 return (PyObject
*)self
;
488 items
= self
->ob_item
;
491 self
->ob_item
= NULL
;
493 for (i
= 0; i
< size
; i
++)
494 Py_XDECREF(items
[i
]);
497 return (PyObject
*)self
;
500 NRESIZE(items
, PyObject
*, size
*n
);
505 self
->ob_item
= items
;
506 for (i
= 1; i
< n
; i
++) { /* Start counting at 1, not 0 */
507 for (j
= 0; j
< size
; j
++) {
508 PyObject
*o
= PyList_GET_ITEM(self
, j
);
510 PyList_SET_ITEM(self
, self
->ob_size
++, o
);
514 return (PyObject
*)self
;
520 list_ass_item(PyListObject
*a
, int i
, PyObject
*v
)
523 if (i
< 0 || i
>= a
->ob_size
) {
524 PyErr_SetString(PyExc_IndexError
,
525 "list assignment index out of range");
529 return list_ass_slice(a
, i
, i
+1, v
);
531 old_value
= a
->ob_item
[i
];
533 Py_DECREF(old_value
);
538 ins(PyListObject
*self
, int where
, PyObject
*v
)
540 if (ins1(self
, where
, v
) != 0)
547 listinsert(PyListObject
*self
, PyObject
*args
)
551 if (!PyArg_ParseTuple(args
, "iO:insert", &i
, &v
))
553 return ins(self
, i
, v
);
556 /* Define NO_STRICT_LIST_APPEND to enable multi-argument append() */
558 #ifndef NO_STRICT_LIST_APPEND
559 #define PyArg_ParseTuple_Compat1 PyArg_ParseTuple
561 #define PyArg_ParseTuple_Compat1(args, format, ret) \
563 PyTuple_GET_SIZE(args) > 1 ? (*ret = args, 1) : \
564 PyTuple_GET_SIZE(args) == 1 ? (*ret = PyTuple_GET_ITEM(args, 0), 1) : \
565 PyArg_ParseTuple(args, format, ret) \
571 listappend(PyListObject
*self
, PyObject
*args
)
574 if (!PyArg_ParseTuple_Compat1(args
, "O:append", &v
))
576 return ins(self
, (int) self
->ob_size
, v
);
580 listextend_internal(PyListObject
*self
, PyObject
*b
)
583 int selflen
= PyList_GET_SIZE(self
);
587 if (PyObject_Size(b
) == 0) {
588 /* short circuit when b is empty */
593 if (self
== (PyListObject
*)b
) {
594 /* as in list_ass_slice() we must special case the
595 * situation: a.extend(a)
597 * XXX: I think this way ought to be faster than using
598 * list_slice() the way list_ass_slice() does.
601 b
= PyList_New(selflen
);
604 for (i
= 0; i
< selflen
; i
++) {
605 PyObject
*o
= PyList_GET_ITEM(self
, i
);
607 PyList_SET_ITEM(b
, i
, o
);
611 blen
= PyObject_Size(b
);
613 /* resize a using idiom */
614 items
= self
->ob_item
;
615 NRESIZE(items
, PyObject
*, selflen
+ blen
);
622 self
->ob_item
= items
;
624 /* populate the end of self with b's items */
625 for (i
= 0; i
< blen
; i
++) {
626 PyObject
*o
= PySequence_Fast_GET_ITEM(b
, i
);
628 PyList_SET_ITEM(self
, self
->ob_size
++, o
);
636 list_inplace_concat(PyListObject
*self
, PyObject
*other
)
638 other
= PySequence_Fast(other
, "argument to += must be a sequence");
642 if (listextend_internal(self
, other
) < 0)
646 return (PyObject
*)self
;
650 listextend(PyListObject
*self
, PyObject
*args
)
655 if (!PyArg_ParseTuple(args
, "O:extend", &b
))
658 b
= PySequence_Fast(b
, "list.extend() argument must be a sequence");
662 if (listextend_internal(self
, b
) < 0)
670 listpop(PyListObject
*self
, PyObject
*args
)
674 if (!PyArg_ParseTuple(args
, "|i:pop", &i
))
676 if (self
->ob_size
== 0) {
677 /* Special-case most common failure cause */
678 PyErr_SetString(PyExc_IndexError
, "pop from empty list");
683 if (i
< 0 || i
>= self
->ob_size
) {
684 PyErr_SetString(PyExc_IndexError
, "pop index out of range");
687 v
= self
->ob_item
[i
];
689 if (list_ass_slice(self
, i
, i
+1, (PyObject
*)NULL
) != 0) {
696 /* New quicksort implementation for arrays of object pointers.
697 Thanks to discussions with Tim Peters. */
699 /* CMPERROR is returned by our comparison function when an error
700 occurred. This is the largest negative integer (0x80000000 on a
702 #define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
704 /* Comparison function. Takes care of calling a user-supplied
705 comparison function (any callable Python object). Calls the
706 standard comparison function, PyObject_Compare(), if the user-
707 supplied function is NULL. */
710 docompare(PyObject
*x
, PyObject
*y
, PyObject
*compare
)
712 PyObject
*args
, *res
;
715 if (compare
== NULL
) {
716 /* NOTE: we rely on the fact here that the sorting algorithm
717 only ever checks whether k<0, i.e., whether x<y. So we
718 invoke the rich comparison function with Py_LT ('<'), and
719 return -1 when it returns true and 0 when it returns
721 i
= PyObject_RichCompareBool(x
, y
, Py_LT
);
728 args
= Py_BuildValue("(OO)", x
, y
);
731 res
= PyEval_CallObject(compare
, args
);
735 if (!PyInt_Check(res
)) {
737 PyErr_SetString(PyExc_TypeError
,
738 "comparison function must return int");
741 i
= PyInt_AsLong(res
);
750 /* MINSIZE is the smallest array that will get a full-blown samplesort
751 treatment; smaller arrays are sorted using binary insertion. It must
752 be at least 7 for the samplesort implementation to work. Binary
753 insertion does fewer compares, but can suffer O(N**2) data movement.
754 The more expensive compares, the larger MINSIZE should be. */
757 /* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
758 partition; smaller slices are passed to binarysort. It must be at
759 least 2, and no larger than MINSIZE. Setting it higher reduces the #
760 of compares slowly, but increases the amount of data movement quickly.
761 The value here was chosen assuming a compare costs ~25x more than
762 swapping a pair of memory-resident pointers -- but under that assumption,
763 changing the value by a few dozen more or less has aggregate effect
764 under 1%. So the value is crucial, but not touchy <wink>. */
765 #define MINPARTITIONSIZE 40
767 /* MAXMERGE is the largest number of elements we'll always merge into
768 a known-to-be sorted chunk via binary insertion, regardless of the
769 size of that chunk. Given a chunk of N sorted elements, and a group
770 of K unknowns, the largest K for which it's better to do insertion
771 (than a full-blown sort) is a complicated function of N and K mostly
772 involving the expected number of compares and data moves under each
773 approach, and the relative cost of those operations on a specific
774 architecure. The fixed value here is conservative, and should be a
775 clear win regardless of architecture or N. */
778 /* STACKSIZE is the size of our work stack. A rough estimate is that
779 this allows us to sort arrays of size N where
780 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
781 for arrays of size 2**64. Because we push the biggest partition
782 first, the worst case occurs when all subarrays are always partitioned
787 #define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
789 /* binarysort is the best method for sorting small arrays: it does
790 few compares, but can do data movement quadratic in the number of
792 [lo, hi) is a contiguous slice of a list, and is sorted via
794 On entry, must have lo <= start <= hi, and that [lo, start) is already
795 sorted (pass start == lo if you don't know!).
796 If docompare complains (returns CMPERROR) return -1, else 0.
797 Even in case of error, the output slice will be some permutation of
798 the input (nothing is lost or duplicated).
802 binarysort(PyObject
**lo
, PyObject
**hi
, PyObject
**start
, PyObject
*compare
)
803 /* compare -- comparison function object, or NULL for default */
805 /* assert lo <= start <= hi
806 assert [lo, start) is sorted */
808 register PyObject
**l
, **p
, **r
;
809 register PyObject
*pivot
;
813 for (; start
< hi
; ++start
) {
814 /* set l to where *start belongs */
819 p
= l
+ ((r
- l
) >> 1);
826 /* Pivot should go at l -- slide over to make room.
827 Caution: using memmove is much slower under MSVC 5;
828 we're not usually moving many slots. */
829 for (p
= start
; p
> l
; --p
)
839 /* samplesortslice is the sorting workhorse.
840 [lo, hi) is a contiguous slice of a list, to be sorted in place.
841 On entry, must have lo <= hi,
842 If docompare complains (returns CMPERROR) return -1, else 0.
843 Even in case of error, the output slice will be some permutation of
844 the input (nothing is lost or duplicated).
846 samplesort is basically quicksort on steroids: a power of 2 close
847 to n/ln(n) is computed, and that many elements (less 1) are picked at
848 random from the array and sorted. These 2**k - 1 elements are then
849 used as preselected pivots for an equal number of quicksort
850 partitioning steps, partitioning the slice into 2**k chunks each of
851 size about ln(n). These small final chunks are then usually handled
852 by binarysort. Note that when k=1, this is roughly the same as an
853 ordinary quicksort using a random pivot, and when k=2 this is roughly
854 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
855 this a "median of n/ln(n)" quicksort. You can also view it as a kind
856 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
858 The large number of samples makes a quadratic-time case almost
859 impossible, and asymptotically drives the average-case number of
860 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
861 3 variant) down to N lg N.
863 We also play lots of low-level tricks to cut the number of compares.
865 Very obscure: To avoid using extra memory, the PPs are stored in the
866 array and shuffled around as partitioning proceeds. At the start of a
867 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
868 adjacent (either on the left or the right!) to a chunk of X elements
869 that are to be partitioned: P X or X P. In either case we need to
870 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
871 left, followed by the PP to be used for this step (that's the middle
872 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
873 P X or X P -> Psmall pivot X Plarge
874 and the order of the PPs must not be altered. It can take a while
875 to realize this isn't trivial! It can take even longer <wink> to
876 understand why the simple code below works, using only 2**(m-1) swaps.
877 The key is that the order of the X elements isn't necessarily
878 preserved: X can end up as some cyclic permutation of its original
879 order. That's OK, because X is unsorted anyway. If the order of X
880 had to be preserved too, the simplest method I know of using O(1)
881 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
882 Since len(X) is typically several times larger than 2**(m-1), that
883 would slow things down.
886 struct SamplesortStackNode
{
887 /* Represents a slice of the array, from (& including) lo up
888 to (but excluding) hi. "extra" additional & adjacent elements
889 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
890 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
891 already sorted, but nothing is known about the other elements
892 in [lo, hi). |extra| is always one less than a power of 2.
893 When extra is 0, we're out of PPs, and the slice must be
894 sorted by some other means. */
900 /* The number of PPs we want is 2**k - 1, where 2**k is as close to
901 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
902 is undesirable, so cutoff values are canned in the "cutoff" table
903 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
905 static long cutoff
[] = {
906 43, /* smallest N such that k == 4 */
928 982548444, /* smallest N such that k == 26 */
929 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
933 samplesortslice(PyObject
**lo
, PyObject
**hi
, PyObject
*compare
)
934 /* compare -- comparison function object, or NULL for default */
936 register PyObject
**l
, **r
;
937 register PyObject
*tmp
, *pivot
;
939 int n
, extra
, top
, extraOnRight
;
940 struct SamplesortStackNode stack
[STACKSIZE
];
942 /* assert lo <= hi */
945 /* ----------------------------------------------------------
947 * --------------------------------------------------------*/
951 /* Set r to the largest value such that [lo,r) is sorted.
952 This catches the already-sorted case, the all-the-same
953 case, and the appended-a-few-elements-to-a-sorted-list case.
954 If the array is unsorted, we're very likely to get out of
955 the loop fast, so the test is cheap if it doesn't pay off.
958 for (r
= lo
+1; r
< hi
; ++r
) {
963 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
964 few unknowns, or few elements in total. */
965 if (hi
- r
<= MAXMERGE
|| n
< MINSIZE
)
966 return binarysort(lo
, hi
, r
, compare
);
968 /* Check for the array already being reverse-sorted. Typical
969 benchmark-driven silliness <wink>. */
971 for (r
= lo
+1; r
< hi
; ++r
) {
976 if (hi
- r
<= MAXMERGE
) {
977 /* Reverse the reversed prefix, then insert the tail */
978 PyObject
**originalr
= r
;
982 tmp
= *l
; *l
= *r
; *r
= tmp
;
985 return binarysort(lo
, hi
, originalr
, compare
);
988 /* ----------------------------------------------------------
989 * Normal case setup: a large array without obvious pattern.
990 * --------------------------------------------------------*/
992 /* extra := a power of 2 ~= n/ln(n), less 1.
993 First find the smallest extra s.t. n < cutoff[extra] */
995 extra
< sizeof(cutoff
) / sizeof(cutoff
[0]);
997 if (n
< cutoff
[extra
])
999 /* note that if we fall out of the loop, the value of
1000 extra still makes *sense*, but may be smaller than
1001 we would like (but the array has more than ~= 2**31
1002 elements in this case!) */
1004 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1005 have is CUTOFFBASE-1, so
1006 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1007 extra
= (1 << (extra
- 1 + CUTOFFBASE
)) - 1;
1008 /* assert extra > 0 and n >= extra */
1010 /* Swap that many values to the start of the array. The
1011 selection of elements is pseudo-random, but the same on
1012 every run (this is intentional! timing algorithm changes is
1013 a pain if timing varies across runs). */
1015 unsigned int seed
= n
/ extra
; /* arbitrary */
1017 for (i
= 0; i
< (unsigned)extra
; ++i
) {
1018 /* j := random int in [i, n) */
1020 seed
= seed
* 69069 + 7;
1021 j
= i
+ seed
% (n
- i
);
1022 tmp
= lo
[i
]; lo
[i
] = lo
[j
]; lo
[j
] = tmp
;
1026 /* Recursively sort the preselected pivots. */
1027 if (samplesortslice(lo
, lo
+ extra
, compare
) < 0)
1030 top
= 0; /* index of available stack slot */
1031 lo
+= extra
; /* point to first unknown */
1032 extraOnRight
= 0; /* the PPs are at the left end */
1034 /* ----------------------------------------------------------
1035 * Partition [lo, hi), and repeat until out of work.
1036 * --------------------------------------------------------*/
1038 /* assert lo <= hi, so n >= 0 */
1041 /* We may not want, or may not be able, to partition:
1042 If n is small, it's quicker to insert.
1043 If extra is 0, we're out of pivots, and *must* use
1046 if (n
< MINPARTITIONSIZE
|| extra
== 0) {
1048 /* assert extra == 0
1049 This is rare, since the average size
1050 of a final block is only about
1052 if (samplesortslice(lo
, hi
, compare
) < 0)
1056 /* Binary insertion should be quicker,
1057 and we can take advantage of the PPs
1058 already being sorted. */
1059 if (extraOnRight
&& extra
) {
1060 /* swap the PPs to the left end */
1069 if (binarysort(lo
- extra
, hi
, lo
,
1074 /* Find another slice to work on. */
1076 break; /* no more -- done! */
1079 extra
= stack
[top
].extra
;
1088 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1089 Then our preselected pivot is at (extra-1)/2, and we
1090 want to move the PPs before that to the left end of
1091 the slice, and the PPs after that to the right end.
1092 The following section changes extra, lo, hi, and the
1094 [lo-extra, lo) contains the smaller PPs.
1096 (lo, hi) contains the unknown elements.
1097 [hi, hi+extra) contains the larger PPs.
1099 k
= extra
>>= 1; /* num PPs to move */
1101 /* Swap the smaller PPs to the left end.
1102 Note that this loop actually moves k+1 items:
1103 the last is our PP */
1105 tmp
= *lo
; *lo
= *hi
; *hi
= tmp
;
1110 /* Swap the larger PPs to the right end. */
1113 tmp
= *lo
; *lo
= *hi
; *hi
= tmp
;
1116 --lo
; /* *lo is now our PP */
1119 /* Now an almost-ordinary quicksort partition step.
1120 Note that most of the time is spent here!
1121 Only odd thing is that we partition into < and >=,
1122 instead of the usual <= and >=. This helps when
1123 there are lots of duplicates of different values,
1124 because it eventually tends to make subfiles
1125 "pure" (all duplicates), and we special-case for
1126 duplicates later. */
1129 /* assert lo < l < r < hi (small n weeded out above) */
1132 /* slide l right, looking for key >= pivot */
1141 /* slide r left, looking for key < pivot */
1143 register PyObject
*rval
= *r
--;
1146 /* swap and advance */
1155 /* assert lo < r <= l < hi
1156 assert r == l or r+1 == l
1157 everything to the left of l is < pivot, and
1158 everything to the right of r is >= pivot */
1167 /* assert lo <= r and r+1 == l and l <= hi
1168 assert r == lo or a[r] < pivot
1169 assert a[lo] is pivot
1170 assert l == hi or a[l] >= pivot
1171 Swap the pivot into "the middle", so we can henceforth
1177 /* The following is true now, & will be preserved:
1178 All in [lo,r) are < pivot
1179 All in [r,l) == pivot (& so can be ignored)
1180 All in [l,hi) are >= pivot */
1182 /* Check for duplicates of the pivot. One compare is
1183 wasted if there are no duplicates, but can win big
1185 Tricky: we're sticking to "<" compares, so deduce
1186 equality indirectly. We know pivot <= *l, so they're
1187 equal iff not pivot < *l.
1190 /* pivot <= *l known */
1195 /* <= and not < implies == */
1199 /* assert lo <= r < l <= hi
1200 Partitions are [lo, r) and [l, hi) */
1202 /* push fattest first; remember we still have extra PPs
1203 to the left of the left chunk and to the right of
1205 /* assert top < STACKSIZE */
1206 if (r
- lo
<= hi
- l
) {
1207 /* second is bigger */
1210 stack
[top
].extra
= -extra
;
1215 /* first is bigger */
1218 stack
[top
].extra
= extra
;
1224 } /* end of partitioning loop */
1234 staticforward PyTypeObject immutable_list_type
;
1237 listsort(PyListObject
*self
, PyObject
*args
)
1240 PyObject
*compare
= NULL
;
1243 if (!PyArg_ParseTuple(args
, "|O:sort", &compare
))
1246 self
->ob_type
= &immutable_list_type
;
1247 err
= samplesortslice(self
->ob_item
,
1248 self
->ob_item
+ self
->ob_size
,
1250 self
->ob_type
= &PyList_Type
;
1258 PyList_Sort(PyObject
*v
)
1260 if (v
== NULL
|| !PyList_Check(v
)) {
1261 PyErr_BadInternalCall();
1264 v
= listsort((PyListObject
*)v
, (PyObject
*)NULL
);
1272 _listreverse(PyListObject
*self
)
1274 register PyObject
**p
, **q
;
1275 register PyObject
*tmp
;
1277 if (self
->ob_size
> 1) {
1278 for (p
= self
->ob_item
, q
= self
->ob_item
+ self
->ob_size
- 1;
1290 listreverse(PyListObject
*self
, PyObject
*args
)
1292 if (!PyArg_ParseTuple(args
, ":reverse"))
1300 PyList_Reverse(PyObject
*v
)
1302 if (v
== NULL
|| !PyList_Check(v
)) {
1303 PyErr_BadInternalCall();
1306 _listreverse((PyListObject
*)v
);
1311 PyList_AsTuple(PyObject
*v
)
1316 if (v
== NULL
|| !PyList_Check(v
)) {
1317 PyErr_BadInternalCall();
1320 n
= ((PyListObject
*)v
)->ob_size
;
1324 p
= ((PyTupleObject
*)w
)->ob_item
;
1326 (void *)((PyListObject
*)v
)->ob_item
,
1327 n
*sizeof(PyObject
*));
1336 listindex(PyListObject
*self
, PyObject
*args
)
1341 if (!PyArg_ParseTuple_Compat1(args
, "O:index", &v
))
1343 for (i
= 0; i
< self
->ob_size
; i
++) {
1344 int cmp
= PyObject_RichCompareBool(self
->ob_item
[i
], v
, Py_EQ
);
1346 return PyInt_FromLong((long)i
);
1350 PyErr_SetString(PyExc_ValueError
, "list.index(x): x not in list");
1355 listcount(PyListObject
*self
, PyObject
*args
)
1361 if (!PyArg_ParseTuple_Compat1(args
, "O:count", &v
))
1363 for (i
= 0; i
< self
->ob_size
; i
++) {
1364 int cmp
= PyObject_RichCompareBool(self
->ob_item
[i
], v
, Py_EQ
);
1370 return PyInt_FromLong((long)count
);
1374 listremove(PyListObject
*self
, PyObject
*args
)
1379 if (!PyArg_ParseTuple_Compat1(args
, "O:remove", &v
))
1381 for (i
= 0; i
< self
->ob_size
; i
++) {
1382 int cmp
= PyObject_RichCompareBool(self
->ob_item
[i
], v
, Py_EQ
);
1384 if (list_ass_slice(self
, i
, i
+1,
1385 (PyObject
*)NULL
) != 0)
1393 PyErr_SetString(PyExc_ValueError
, "list.remove(x): x not in list");
1398 list_traverse(PyListObject
*o
, visitproc visit
, void *arg
)
1403 for (i
= o
->ob_size
; --i
>= 0; ) {
1406 err
= visit(x
, arg
);
1415 list_clear(PyListObject
*lp
)
1417 (void) PyList_SetSlice((PyObject
*)lp
, 0, lp
->ob_size
, 0);
1422 list_richcompare(PyObject
*v
, PyObject
*w
, int op
)
1424 PyListObject
*vl
, *wl
;
1427 if (!PyList_Check(v
) || !PyList_Check(w
)) {
1428 Py_INCREF(Py_NotImplemented
);
1429 return Py_NotImplemented
;
1432 vl
= (PyListObject
*)v
;
1433 wl
= (PyListObject
*)w
;
1435 if (vl
->ob_size
!= wl
->ob_size
&& (op
== Py_EQ
|| op
== Py_NE
)) {
1436 /* Shortcut: if the lengths differ, the lists differ */
1446 /* Search for the first index where items are different */
1447 for (i
= 0; i
< vl
->ob_size
&& i
< wl
->ob_size
; i
++) {
1448 int k
= PyObject_RichCompareBool(vl
->ob_item
[i
],
1449 wl
->ob_item
[i
], Py_EQ
);
1456 if (i
>= vl
->ob_size
|| i
>= wl
->ob_size
) {
1457 /* No more items to compare -- compare sizes */
1458 int vs
= vl
->ob_size
;
1459 int ws
= wl
->ob_size
;
1463 case Py_LT
: cmp
= vs
< ws
; break;
1464 case Py_LE
: cmp
= ws
<= ws
; break;
1465 case Py_EQ
: cmp
= vs
== ws
; break;
1466 case Py_NE
: cmp
= vs
!= ws
; break;
1467 case Py_GT
: cmp
= vs
> ws
; break;
1468 case Py_GE
: cmp
= vs
>= ws
; break;
1469 default: return NULL
; /* cannot happen */
1479 /* We have an item that differs -- shortcuts for EQ/NE */
1481 Py_INCREF(Py_False
);
1489 /* Compare the final item again using the proper operator */
1490 return PyObject_RichCompare(vl
->ob_item
[i
], wl
->ob_item
[i
], op
);
1493 static char append_doc
[] =
1494 "L.append(object) -- append object to end";
1495 static char extend_doc
[] =
1496 "L.extend(list) -- extend list by appending list elements";
1497 static char insert_doc
[] =
1498 "L.insert(index, object) -- insert object before index";
1499 static char pop_doc
[] =
1500 "L.pop([index]) -> item -- remove and return item at index (default last)";
1501 static char remove_doc
[] =
1502 "L.remove(value) -- remove first occurrence of value";
1503 static char index_doc
[] =
1504 "L.index(value) -> integer -- return index of first occurrence of value";
1505 static char count_doc
[] =
1506 "L.count(value) -> integer -- return number of occurrences of value";
1507 static char reverse_doc
[] =
1508 "L.reverse() -- reverse *IN PLACE*";
1509 static char sort_doc
[] =
1510 "L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1512 static PyMethodDef list_methods
[] = {
1513 {"append", (PyCFunction
)listappend
, METH_VARARGS
, append_doc
},
1514 {"insert", (PyCFunction
)listinsert
, METH_VARARGS
, insert_doc
},
1515 {"extend", (PyCFunction
)listextend
, METH_VARARGS
, extend_doc
},
1516 {"pop", (PyCFunction
)listpop
, METH_VARARGS
, pop_doc
},
1517 {"remove", (PyCFunction
)listremove
, METH_VARARGS
, remove_doc
},
1518 {"index", (PyCFunction
)listindex
, METH_VARARGS
, index_doc
},
1519 {"count", (PyCFunction
)listcount
, METH_VARARGS
, count_doc
},
1520 {"reverse", (PyCFunction
)listreverse
, METH_VARARGS
, reverse_doc
},
1521 {"sort", (PyCFunction
)listsort
, METH_VARARGS
, sort_doc
},
1522 {NULL
, NULL
} /* sentinel */
1526 list_getattr(PyListObject
*f
, char *name
)
1528 return Py_FindMethod(list_methods
, (PyObject
*)f
, name
);
1531 static PySequenceMethods list_as_sequence
= {
1532 (inquiry
)list_length
, /* sq_length */
1533 (binaryfunc
)list_concat
, /* sq_concat */
1534 (intargfunc
)list_repeat
, /* sq_repeat */
1535 (intargfunc
)list_item
, /* sq_item */
1536 (intintargfunc
)list_slice
, /* sq_slice */
1537 (intobjargproc
)list_ass_item
, /* sq_ass_item */
1538 (intintobjargproc
)list_ass_slice
, /* sq_ass_slice */
1539 (objobjproc
)list_contains
, /* sq_contains */
1540 (binaryfunc
)list_inplace_concat
, /* sq_inplace_concat */
1541 (intargfunc
)list_inplace_repeat
, /* sq_inplace_repeat */
1544 PyTypeObject PyList_Type
= {
1545 PyObject_HEAD_INIT(&PyType_Type
)
1548 sizeof(PyListObject
) + PyGC_HEAD_SIZE
,
1550 (destructor
)list_dealloc
, /* tp_dealloc */
1551 (printfunc
)list_print
, /* tp_print */
1552 (getattrfunc
)list_getattr
, /* tp_getattr */
1555 (reprfunc
)list_repr
, /* tp_repr */
1556 0, /* tp_as_number */
1557 &list_as_sequence
, /* tp_as_sequence */
1558 0, /* tp_as_mapping */
1562 0, /* tp_getattro */
1563 0, /* tp_setattro */
1564 0, /* tp_as_buffer */
1565 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_GC
, /* tp_flags */
1567 (traverseproc
)list_traverse
, /* tp_traverse */
1568 (inquiry
)list_clear
, /* tp_clear */
1569 list_richcompare
, /* tp_richcompare */
1573 /* During a sort, we really can't have anyone modifying the list; it could
1574 cause core dumps. Thus, we substitute a dummy type that raises an
1575 explanatory exception when a modifying operation is used. Caveat:
1576 comparisons may behave differently; but I guess it's a bad idea anyway to
1577 compare a list that's being sorted... */
1580 immutable_list_op(void)
1582 PyErr_SetString(PyExc_TypeError
,
1583 "a list cannot be modified while it is being sorted");
1587 static PyMethodDef immutable_list_methods
[] = {
1588 {"append", (PyCFunction
)immutable_list_op
},
1589 {"insert", (PyCFunction
)immutable_list_op
},
1590 {"remove", (PyCFunction
)immutable_list_op
},
1591 {"index", (PyCFunction
)listindex
},
1592 {"count", (PyCFunction
)listcount
},
1593 {"reverse", (PyCFunction
)immutable_list_op
},
1594 {"sort", (PyCFunction
)immutable_list_op
},
1595 {NULL
, NULL
} /* sentinel */
1599 immutable_list_getattr(PyListObject
*f
, char *name
)
1601 return Py_FindMethod(immutable_list_methods
, (PyObject
*)f
, name
);
1605 immutable_list_ass(void)
1607 immutable_list_op();
1611 static PySequenceMethods immutable_list_as_sequence
= {
1612 (inquiry
)list_length
, /* sq_length */
1613 (binaryfunc
)list_concat
, /* sq_concat */
1614 (intargfunc
)list_repeat
, /* sq_repeat */
1615 (intargfunc
)list_item
, /* sq_item */
1616 (intintargfunc
)list_slice
, /* sq_slice */
1617 (intobjargproc
)immutable_list_ass
, /* sq_ass_item */
1618 (intintobjargproc
)immutable_list_ass
, /* sq_ass_slice */
1619 (objobjproc
)list_contains
, /* sq_contains */
1622 static PyTypeObject immutable_list_type
= {
1623 PyObject_HEAD_INIT(&PyType_Type
)
1625 "list (immutable, during sort)",
1626 sizeof(PyListObject
) + PyGC_HEAD_SIZE
,
1628 0, /* Cannot happen */ /* tp_dealloc */
1629 (printfunc
)list_print
, /* tp_print */
1630 (getattrfunc
)immutable_list_getattr
, /* tp_getattr */
1632 0, /* Won't be called */ /* tp_compare */
1633 (reprfunc
)list_repr
, /* tp_repr */
1634 0, /* tp_as_number */
1635 &immutable_list_as_sequence
, /* tp_as_sequence */
1636 0, /* tp_as_mapping */
1640 0, /* tp_getattro */
1641 0, /* tp_setattro */
1642 0, /* tp_as_buffer */
1643 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_GC
, /* tp_flags */
1645 (traverseproc
)list_traverse
, /* tp_traverse */
1647 list_richcompare
, /* tp_richcompare */
1648 /* NOTE: This is *not* the standard list_type struct! */