2 /* List object implementation */
9 #include <sys/types.h> /* For size_t */
15 unsigned int nbits
= 0;
16 unsigned int n2
= (unsigned int)n
>> 5;
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.
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).
44 return ((n
>> nbits
) + 1) << nbits
;
47 #define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems))
56 PyErr_BadInternalCall();
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
);
72 op
->ob_item
= (PyObject
**) PyMem_MALLOC(nbytes
);
73 if (op
->ob_item
== NULL
) {
74 return PyErr_NoMemory();
78 for (i
= 0; i
< size
; i
++)
79 op
->ob_item
[i
] = NULL
;
80 _PyObject_GC_TRACK(op
);
81 return (PyObject
*) op
;
85 PyList_Size(PyObject
*op
)
87 if (!PyList_Check(op
)) {
88 PyErr_BadInternalCall();
92 return ((PyListObject
*)op
) -> ob_size
;
95 static PyObject
*indexerr
;
98 PyList_GetItem(PyObject
*op
, int i
)
100 if (!PyList_Check(op
)) {
101 PyErr_BadInternalCall();
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
);
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
)) {
122 PyErr_BadInternalCall();
125 if (i
< 0 || i
>= ((PyListObject
*)op
) -> ob_size
) {
127 PyErr_SetString(PyExc_IndexError
,
128 "list assignment index out of range");
131 p
= ((PyListObject
*)op
) -> ob_item
+ i
;
139 ins1(PyListObject
*self
, int where
, PyObject
*v
)
144 PyErr_BadInternalCall();
147 if (self
->ob_size
== INT_MAX
) {
148 PyErr_SetString(PyExc_OverflowError
,
149 "cannot add more objects to list");
152 items
= self
->ob_item
;
153 NRESIZE(items
, PyObject
*, self
->ob_size
+1);
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
];
166 self
->ob_item
= items
;
172 PyList_Insert(PyObject
*op
, int where
, PyObject
*newitem
)
174 if (!PyList_Check(op
)) {
175 PyErr_BadInternalCall();
178 return ins1((PyListObject
*)op
, where
, newitem
);
182 PyList_Append(PyObject
*op
, PyObject
*newitem
)
184 if (!PyList_Check(op
)) {
185 PyErr_BadInternalCall();
188 return ins1((PyListObject
*)op
,
189 (int) ((PyListObject
*)op
)->ob_size
, newitem
);
195 list_dealloc(PyListObject
*op
)
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. */
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
)
216 list_print(PyListObject
*op
, FILE *fp
, int flags
)
220 i
= Py_ReprEnter((PyObject
*)op
);
224 fprintf(fp
, "[...]");
228 for (i
= 0; i
< op
->ob_size
; i
++) {
231 if (PyObject_Print(op
->ob_item
[i
], fp
, 0) != 0) {
232 Py_ReprLeave((PyObject
*)op
);
237 Py_ReprLeave((PyObject
*)op
);
242 list_repr(PyListObject
*v
)
246 PyObject
*pieces
= NULL
, *result
= NULL
;
248 i
= Py_ReprEnter((PyObject
*)v
);
250 return i
> 0 ? PyString_FromString("[...]") : NULL
;
253 if (v
->ob_size
== 0) {
254 result
= PyString_FromString("[]");
258 pieces
= PyList_New(0);
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
) {
266 s
= PyObject_Repr(v
->ob_item
[i
]);
269 status
= PyList_Append(pieces
, s
);
270 Py_DECREF(s
); /* append created a new ref */
275 /* Add "[]" decorations to the first and last items. */
276 assert(PyList_GET_SIZE(pieces
) > 0);
277 s
= PyString_FromString("[");
280 temp
= PyList_GET_ITEM(pieces
, 0);
281 PyString_ConcatAndDel(&s
, temp
);
282 PyList_SET_ITEM(pieces
, 0, s
);
286 s
= PyString_FromString("]");
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
);
295 /* Paste them all together with ", " between. */
296 s
= PyString_FromString(", ");
299 result
= _PyString_Join(s
, pieces
);
304 Py_ReprLeave((PyObject
*)v
);
309 list_length(PyListObject
*a
)
317 list_contains(PyListObject
*a
, PyObject
*el
)
321 for (i
= 0; i
< a
->ob_size
; ++i
) {
322 int cmp
= PyObject_RichCompareBool(el
, PyList_GET_ITEM(a
, i
),
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
);
343 Py_INCREF(a
->ob_item
[i
]);
344 return a
->ob_item
[i
];
348 list_slice(PyListObject
*a
, int ilow
, int ihigh
)
354 else if (ilow
> a
->ob_size
)
358 else if (ihigh
> a
->ob_size
)
360 np
= (PyListObject
*) PyList_New(ihigh
- ilow
);
363 for (i
= ilow
; i
< ihigh
; i
++) {
364 PyObject
*v
= a
->ob_item
[i
];
366 np
->ob_item
[i
- ilow
] = v
;
368 return (PyObject
*)np
;
372 PyList_GetSlice(PyObject
*a
, int ilow
, int ihigh
)
374 if (!PyList_Check(a
)) {
375 PyErr_BadInternalCall();
378 return list_slice((PyListObject
*)a
, ilow
, ihigh
);
382 list_concat(PyListObject
*a
, PyObject
*bb
)
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
);
393 #define b ((PyListObject *)bb)
394 size
= a
->ob_size
+ b
->ob_size
;
395 np
= (PyListObject
*) PyList_New(size
);
399 for (i
= 0; i
< a
->ob_size
; i
++) {
400 PyObject
*v
= a
->ob_item
[i
];
404 for (i
= 0; i
< b
->ob_size
; i
++) {
405 PyObject
*v
= b
->ob_item
[i
];
407 np
->ob_item
[i
+ a
->ob_size
] = v
;
409 return (PyObject
*)np
;
414 list_repeat(PyListObject
*a
, int n
)
422 size
= a
->ob_size
* n
;
423 np
= (PyListObject
*) PyList_New(size
);
427 for (i
= 0; i
< n
; i
++) {
428 for (j
= 0; j
< a
->ob_size
; j
++) {
434 return (PyObject
*) np
;
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
446 PyObject
**recycle
, **p
;
448 int n
; /* Size of replacement list */
449 int d
; /* Change in size */
450 int k
; /* Loop index */
451 #define b ((PyListObject *)v)
454 else if (PyList_Check(v
)) {
457 /* Special case "a[i:j] = a" -- copy b first */
459 v
= list_slice(b
, 0, n
);
460 ret
= list_ass_slice(a
, ilow
, ihigh
, v
);
466 PyErr_Format(PyExc_TypeError
,
467 "must assign list (not \"%.200s\") to slice",
468 v
->ob_type
->tp_name
);
473 else if (ilow
> a
->ob_size
)
477 else if (ihigh
> a
->ob_size
)
480 d
= n
- (ihigh
-ilow
);
482 p
= recycle
= PyMem_NEW(PyObject
*, (ihigh
-ilow
));
485 if (d
<= 0) { /* Delete -d items; recycle ihigh-ilow items */
486 for (k
= ilow
; k
< ihigh
; k
++)
489 for (/*k = ihigh*/; k
< a
->ob_size
; k
++)
492 NRESIZE(item
, PyObject
*, a
->ob_size
); /* Can't fail */
496 else { /* Insert d items; recycle ihigh-ilow items */
497 NRESIZE(item
, PyObject
*, a
->ob_size
+ d
);
504 for (k
= a
->ob_size
; --k
>= ihigh
; )
506 for (/*k = ihigh-1*/; k
>= ilow
; --k
)
511 for (k
= 0; k
< n
; k
++, ilow
++) {
512 PyObject
*w
= b
->ob_item
[k
];
517 while (--p
>= recycle
)
521 if (a
->ob_size
== 0 && a
->ob_item
!= NULL
) {
522 PyMem_FREE(a
->ob_item
);
530 PyList_SetSlice(PyObject
*a
, int ilow
, int ihigh
, PyObject
*v
)
532 if (!PyList_Check(a
)) {
533 PyErr_BadInternalCall();
536 return list_ass_slice((PyListObject
*)a
, ilow
, ihigh
, v
);
540 list_inplace_repeat(PyListObject
*self
, int n
)
546 size
= PyList_GET_SIZE(self
);
549 return (PyObject
*)self
;
552 items
= self
->ob_item
;
555 self
->ob_item
= NULL
;
557 for (i
= 0; i
< size
; i
++)
558 Py_XDECREF(items
[i
]);
561 return (PyObject
*)self
;
564 NRESIZE(items
, PyObject
*, size
*n
);
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
);
574 PyList_SET_ITEM(self
, self
->ob_size
++, o
);
578 return (PyObject
*)self
;
584 list_ass_item(PyListObject
*a
, int i
, PyObject
*v
)
587 if (i
< 0 || i
>= a
->ob_size
) {
588 PyErr_SetString(PyExc_IndexError
,
589 "list assignment index out of range");
593 return list_ass_slice(a
, i
, i
+1, v
);
595 old_value
= a
->ob_item
[i
];
597 Py_DECREF(old_value
);
602 ins(PyListObject
*self
, int where
, PyObject
*v
)
604 if (ins1(self
, where
, v
) != 0)
611 listinsert(PyListObject
*self
, PyObject
*args
)
615 if (!PyArg_ParseTuple(args
, "iO:insert", &i
, &v
))
617 return ins(self
, i
, v
);
621 listappend(PyListObject
*self
, PyObject
*v
)
623 return ins(self
, (int) self
->ob_size
, v
);
627 listextend_internal(PyListObject
*self
, PyObject
*b
)
630 int selflen
= PyList_GET_SIZE(self
);
634 if (PyObject_Size(b
) == 0) {
635 /* short circuit when b is empty */
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.
648 b
= PyList_New(selflen
);
651 for (i
= 0; i
< selflen
; i
++) {
652 PyObject
*o
= PyList_GET_ITEM(self
, i
);
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
);
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
);
675 PyList_SET_ITEM(self
, self
->ob_size
++, o
);
683 list_inplace_concat(PyListObject
*self
, PyObject
*other
)
685 other
= PySequence_Fast(other
, "argument to += must be iterable");
689 if (listextend_internal(self
, other
) < 0)
693 return (PyObject
*)self
;
697 listextend(PyListObject
*self
, PyObject
*b
)
700 b
= PySequence_Fast(b
, "list.extend() argument must be iterable");
704 if (listextend_internal(self
, b
) < 0)
712 listpop(PyListObject
*self
, PyObject
*args
)
716 if (!PyArg_ParseTuple(args
, "|i:pop", &i
))
718 if (self
->ob_size
== 0) {
719 /* Special-case most common failure cause */
720 PyErr_SetString(PyExc_IndexError
, "pop from empty list");
725 if (i
< 0 || i
>= self
->ob_size
) {
726 PyErr_SetString(PyExc_IndexError
, "pop index out of range");
729 v
= self
->ob_item
[i
];
731 if (list_ass_slice(self
, i
, i
+1, (PyObject
*)NULL
) != 0) {
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
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. */
752 docompare(PyObject
*x
, PyObject
*y
, PyObject
*compare
)
754 PyObject
*args
, *res
;
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
763 i
= PyObject_RichCompareBool(x
, y
, Py_LT
);
770 args
= Py_BuildValue("(OO)", x
, y
);
773 res
= PyEval_CallObject(compare
, args
);
777 if (!PyInt_Check(res
)) {
779 PyErr_SetString(PyExc_TypeError
,
780 "comparison function must return int");
783 i
= PyInt_AsLong(res
);
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. */
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. */
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
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
834 [lo, hi) is a contiguous slice of a list, and is sorted via
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).
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 */
850 register PyObject
**l
, **p
, **r
;
851 register PyObject
*pivot
;
855 for (; start
< hi
; ++start
) {
856 /* set l to where *start belongs */
861 p
= l
+ ((r
- l
) >> 1);
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
)
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. */
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. */
947 static long cutoff
[] = {
948 43, /* smallest N such that k == 4 */
970 982548444, /* smallest N such that k == 26 */
971 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
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
;
981 int n
, extra
, top
, extraOnRight
;
982 struct SamplesortStackNode stack
[STACKSIZE
];
984 /* assert lo <= hi */
987 /* ----------------------------------------------------------
989 * --------------------------------------------------------*/
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.
1000 for (r
= lo
+1; r
< hi
; ++r
) {
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
) {
1018 if (hi
- r
<= MAXMERGE
) {
1019 /* Reverse the reversed prefix, then insert the tail */
1020 PyObject
**originalr
= r
;
1024 tmp
= *l
; *l
= *r
; *r
= tmp
;
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] */
1037 extra
< sizeof(cutoff
) / sizeof(cutoff
[0]);
1039 if (n
< cutoff
[extra
])
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 */
1059 for (i
= 0; i
< (unsigned)extra
; ++i
) {
1060 /* j := random int in [i, n) */
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)
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 * --------------------------------------------------------*/
1080 /* assert lo <= hi, so n >= 0 */
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
1088 if (n
< MINPARTITIONSIZE
|| extra
== 0) {
1090 /* assert extra == 0
1091 This is rare, since the average size
1092 of a final block is only about
1094 if (samplesortslice(lo
, hi
, compare
) < 0)
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 */
1111 if (binarysort(lo
- extra
, hi
, lo
,
1116 /* Find another slice to work on. */
1118 break; /* no more -- done! */
1121 extra
= stack
[top
].extra
;
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
1136 [lo-extra, lo) contains the smaller PPs.
1138 (lo, hi) contains the unknown elements.
1139 [hi, hi+extra) contains the larger PPs.
1141 k
= extra
>>= 1; /* num PPs to move */
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 */
1147 tmp
= *lo
; *lo
= *hi
; *hi
= tmp
;
1152 /* Swap the larger PPs to the right end. */
1155 tmp
= *lo
; *lo
= *hi
; *hi
= tmp
;
1158 --lo
; /* *lo is now our PP */
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. */
1171 /* assert lo < l < r < hi (small n weeded out above) */
1174 /* slide l right, looking for key >= pivot */
1183 /* slide r left, looking for key < pivot */
1185 register PyObject
*rval
= *r
--;
1188 /* swap and advance */
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 */
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
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
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.
1232 /* pivot <= *l known */
1237 /* <= and not < implies == */
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
1247 /* assert top < STACKSIZE */
1248 if (r
- lo
<= hi
- l
) {
1249 /* second is bigger */
1252 stack
[top
].extra
= -extra
;
1257 /* first is bigger */
1260 stack
[top
].extra
= extra
;
1266 } /* end of partitioning loop */
1276 staticforward PyTypeObject immutable_list_type
;
1279 listsort(PyListObject
*self
, PyObject
*args
)
1282 PyObject
*compare
= NULL
;
1283 PyTypeObject
*savetype
;
1286 if (!PyArg_ParseTuple(args
, "|O:sort", &compare
))
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
,
1294 self
->ob_type
= savetype
;
1302 PyList_Sort(PyObject
*v
)
1304 if (v
== NULL
|| !PyList_Check(v
)) {
1305 PyErr_BadInternalCall();
1308 v
= listsort((PyListObject
*)v
, (PyObject
*)NULL
);
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;
1334 listreverse(PyListObject
*self
)
1342 PyList_Reverse(PyObject
*v
)
1344 if (v
== NULL
|| !PyList_Check(v
)) {
1345 PyErr_BadInternalCall();
1348 _listreverse((PyListObject
*)v
);
1353 PyList_AsTuple(PyObject
*v
)
1358 if (v
== NULL
|| !PyList_Check(v
)) {
1359 PyErr_BadInternalCall();
1362 n
= ((PyListObject
*)v
)->ob_size
;
1366 p
= ((PyTupleObject
*)w
)->ob_item
;
1368 (void *)((PyListObject
*)v
)->ob_item
,
1369 n
*sizeof(PyObject
*));
1378 listindex(PyListObject
*self
, PyObject
*v
)
1382 for (i
= 0; i
< self
->ob_size
; i
++) {
1383 int cmp
= PyObject_RichCompareBool(self
->ob_item
[i
], v
, Py_EQ
);
1385 return PyInt_FromLong((long)i
);
1389 PyErr_SetString(PyExc_ValueError
, "list.index(x): x not in list");
1394 listcount(PyListObject
*self
, PyObject
*v
)
1399 for (i
= 0; i
< self
->ob_size
; i
++) {
1400 int cmp
= PyObject_RichCompareBool(self
->ob_item
[i
], v
, Py_EQ
);
1406 return PyInt_FromLong((long)count
);
1410 listremove(PyListObject
*self
, PyObject
*v
)
1414 for (i
= 0; i
< self
->ob_size
; i
++) {
1415 int cmp
= PyObject_RichCompareBool(self
->ob_item
[i
], v
, Py_EQ
);
1417 if (list_ass_slice(self
, i
, i
+1,
1418 (PyObject
*)NULL
) != 0)
1426 PyErr_SetString(PyExc_ValueError
, "list.remove(x): x not in list");
1431 list_traverse(PyListObject
*o
, visitproc visit
, void *arg
)
1436 for (i
= o
->ob_size
; --i
>= 0; ) {
1439 err
= visit(x
, arg
);
1448 list_clear(PyListObject
*lp
)
1450 (void) PyList_SetSlice((PyObject
*)lp
, 0, lp
->ob_size
, 0);
1455 list_richcompare(PyObject
*v
, PyObject
*w
, int op
)
1457 PyListObject
*vl
, *wl
;
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 */
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
);
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
;
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 */
1512 /* We have an item that differs -- shortcuts for EQ/NE */
1514 Py_INCREF(Py_False
);
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 */
1528 list_fill(PyListObject
*result
, PyObject
*v
)
1530 PyObject
*it
; /* iter(v) */
1531 int n
; /* guess for result list size */
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 */
1545 if (list_ass_slice(result
, 0, n
, (PyObject
*)NULL
) != 0)
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
);
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
);
1566 n
= 8; /* arbitrary */
1567 NRESIZE(result
->ob_item
, PyObject
*, n
);
1568 if (result
->ob_item
== NULL
)
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
);
1578 if (PyErr_Occurred())
1583 PyList_SET_ITEM(result
, i
, item
); /* steals ref */
1585 int status
= ins1(result
, result
->ob_size
, item
);
1586 Py_DECREF(item
); /* append creates a new ref */
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)
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
))
1614 return list_fill(self
, arg
);
1615 if (self
->ob_size
> 0)
1616 return list_ass_slice(self
, 0, self
->ob_size
, (PyObject
*)NULL
);
1621 list_nohash(PyObject
*self
)
1623 PyErr_SetString(PyExc_TypeError
, "list objects are unhashable");
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
)
1680 sizeof(PyListObject
),
1682 (destructor
)list_dealloc
, /* tp_dealloc */
1683 (printfunc
)list_print
, /* tp_print */
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 */
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 */
1705 0, /* tp_iternext */
1706 list_methods
, /* tp_methods */
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... */
1728 immutable_list_op(void)
1730 PyErr_SetString(PyExc_TypeError
,
1731 "a list cannot be modified while it is being sorted");
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 */
1749 immutable_list_ass(void)
1751 immutable_list_op();
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 */
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 */
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 */
1791 list_richcompare
, /* tp_richcompare */
1792 0, /* tp_weaklistoffset */
1794 0, /* tp_iternext */
1795 immutable_list_methods
, /* tp_methods */
1800 0, /* tp_descr_get */
1801 0, /* tp_descr_set */
1803 /* NOTE: This is *not* the standard list_type struct! */