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) \
49 size_t _new_size = roundupsize(nitems); \
50 if (_new_size <= ((~(size_t)0) / sizeof(type))) \
51 PyMem_RESIZE(var, type, _new_size); \
63 PyErr_BadInternalCall();
66 nbytes
= size
* sizeof(PyObject
*);
67 /* Check for overflow */
68 if (nbytes
/ sizeof(PyObject
*) != (size_t)size
) {
69 return PyErr_NoMemory();
71 op
= PyObject_GC_New(PyListObject
, &PyList_Type
);
79 op
->ob_item
= (PyObject
**) PyMem_MALLOC(nbytes
);
80 if (op
->ob_item
== NULL
) {
81 return PyErr_NoMemory();
85 for (i
= 0; i
< size
; i
++)
86 op
->ob_item
[i
] = NULL
;
87 _PyObject_GC_TRACK(op
);
88 return (PyObject
*) op
;
92 PyList_Size(PyObject
*op
)
94 if (!PyList_Check(op
)) {
95 PyErr_BadInternalCall();
99 return ((PyListObject
*)op
) -> ob_size
;
102 static PyObject
*indexerr
;
105 PyList_GetItem(PyObject
*op
, int i
)
107 if (!PyList_Check(op
)) {
108 PyErr_BadInternalCall();
111 if (i
< 0 || i
>= ((PyListObject
*)op
) -> ob_size
) {
112 if (indexerr
== NULL
)
113 indexerr
= PyString_FromString(
114 "list index out of range");
115 PyErr_SetObject(PyExc_IndexError
, indexerr
);
118 return ((PyListObject
*)op
) -> ob_item
[i
];
122 PyList_SetItem(register PyObject
*op
, register int i
,
123 register PyObject
*newitem
)
125 register PyObject
*olditem
;
126 register PyObject
**p
;
127 if (!PyList_Check(op
)) {
129 PyErr_BadInternalCall();
132 if (i
< 0 || i
>= ((PyListObject
*)op
) -> ob_size
) {
134 PyErr_SetString(PyExc_IndexError
,
135 "list assignment index out of range");
138 p
= ((PyListObject
*)op
) -> ob_item
+ i
;
146 ins1(PyListObject
*self
, int where
, PyObject
*v
)
151 PyErr_BadInternalCall();
154 if (self
->ob_size
== INT_MAX
) {
155 PyErr_SetString(PyExc_OverflowError
,
156 "cannot add more objects to list");
159 items
= self
->ob_item
;
160 NRESIZE(items
, PyObject
*, self
->ob_size
+1);
167 if (where
> self
->ob_size
)
168 where
= self
->ob_size
;
169 for (i
= self
->ob_size
; --i
>= where
; )
170 items
[i
+1] = items
[i
];
173 self
->ob_item
= items
;
179 PyList_Insert(PyObject
*op
, int where
, PyObject
*newitem
)
181 if (!PyList_Check(op
)) {
182 PyErr_BadInternalCall();
185 return ins1((PyListObject
*)op
, where
, newitem
);
189 PyList_Append(PyObject
*op
, PyObject
*newitem
)
191 if (!PyList_Check(op
)) {
192 PyErr_BadInternalCall();
195 return ins1((PyListObject
*)op
,
196 (int) ((PyListObject
*)op
)->ob_size
, newitem
);
202 list_dealloc(PyListObject
*op
)
205 PyObject_GC_UnTrack(op
);
206 Py_TRASHCAN_SAFE_BEGIN(op
)
207 if (op
->ob_item
!= NULL
) {
208 /* Do it backwards, for Christian Tismer.
209 There's a simple test case where somehow this reduces
210 thrashing when a *very* large list is created and
211 immediately deleted. */
214 Py_XDECREF(op
->ob_item
[i
]);
216 PyMem_FREE(op
->ob_item
);
218 op
->ob_type
->tp_free((PyObject
*)op
);
219 Py_TRASHCAN_SAFE_END(op
)
223 list_print(PyListObject
*op
, FILE *fp
, int flags
)
227 i
= Py_ReprEnter((PyObject
*)op
);
231 fprintf(fp
, "[...]");
235 for (i
= 0; i
< op
->ob_size
; i
++) {
238 if (PyObject_Print(op
->ob_item
[i
], fp
, 0) != 0) {
239 Py_ReprLeave((PyObject
*)op
);
244 Py_ReprLeave((PyObject
*)op
);
249 list_repr(PyListObject
*v
)
253 PyObject
*pieces
= NULL
, *result
= NULL
;
255 i
= Py_ReprEnter((PyObject
*)v
);
257 return i
> 0 ? PyString_FromString("[...]") : NULL
;
260 if (v
->ob_size
== 0) {
261 result
= PyString_FromString("[]");
265 pieces
= PyList_New(0);
269 /* Do repr() on each element. Note that this may mutate the list,
270 so must refetch the list size on each iteration. */
271 for (i
= 0; i
< v
->ob_size
; ++i
) {
273 s
= PyObject_Repr(v
->ob_item
[i
]);
276 status
= PyList_Append(pieces
, s
);
277 Py_DECREF(s
); /* append created a new ref */
282 /* Add "[]" decorations to the first and last items. */
283 assert(PyList_GET_SIZE(pieces
) > 0);
284 s
= PyString_FromString("[");
287 temp
= PyList_GET_ITEM(pieces
, 0);
288 PyString_ConcatAndDel(&s
, temp
);
289 PyList_SET_ITEM(pieces
, 0, s
);
293 s
= PyString_FromString("]");
296 temp
= PyList_GET_ITEM(pieces
, PyList_GET_SIZE(pieces
) - 1);
297 PyString_ConcatAndDel(&temp
, s
);
298 PyList_SET_ITEM(pieces
, PyList_GET_SIZE(pieces
) - 1, temp
);
302 /* Paste them all together with ", " between. */
303 s
= PyString_FromString(", ");
306 result
= _PyString_Join(s
, pieces
);
311 Py_ReprLeave((PyObject
*)v
);
316 list_length(PyListObject
*a
)
324 list_contains(PyListObject
*a
, PyObject
*el
)
328 for (i
= 0; i
< a
->ob_size
; ++i
) {
329 int cmp
= PyObject_RichCompareBool(el
, PyList_GET_ITEM(a
, i
),
341 list_item(PyListObject
*a
, int i
)
343 if (i
< 0 || i
>= a
->ob_size
) {
344 if (indexerr
== NULL
)
345 indexerr
= PyString_FromString(
346 "list index out of range");
347 PyErr_SetObject(PyExc_IndexError
, indexerr
);
350 Py_INCREF(a
->ob_item
[i
]);
351 return a
->ob_item
[i
];
355 list_slice(PyListObject
*a
, int ilow
, int ihigh
)
361 else if (ilow
> a
->ob_size
)
365 else if (ihigh
> a
->ob_size
)
367 np
= (PyListObject
*) PyList_New(ihigh
- ilow
);
370 for (i
= ilow
; i
< ihigh
; i
++) {
371 PyObject
*v
= a
->ob_item
[i
];
373 np
->ob_item
[i
- ilow
] = v
;
375 return (PyObject
*)np
;
379 PyList_GetSlice(PyObject
*a
, int ilow
, int ihigh
)
381 if (!PyList_Check(a
)) {
382 PyErr_BadInternalCall();
385 return list_slice((PyListObject
*)a
, ilow
, ihigh
);
389 list_concat(PyListObject
*a
, PyObject
*bb
)
394 if (!PyList_Check(bb
)) {
395 PyErr_Format(PyExc_TypeError
,
396 "can only concatenate list (not \"%.200s\") to list",
397 bb
->ob_type
->tp_name
);
400 #define b ((PyListObject *)bb)
401 size
= a
->ob_size
+ b
->ob_size
;
403 return PyErr_NoMemory();
404 np
= (PyListObject
*) PyList_New(size
);
408 for (i
= 0; i
< a
->ob_size
; i
++) {
409 PyObject
*v
= a
->ob_item
[i
];
413 for (i
= 0; i
< b
->ob_size
; i
++) {
414 PyObject
*v
= b
->ob_item
[i
];
416 np
->ob_item
[i
+ a
->ob_size
] = v
;
418 return (PyObject
*)np
;
423 list_repeat(PyListObject
*a
, int n
)
431 size
= a
->ob_size
* n
;
432 if (n
&& size
/n
!= a
->ob_size
)
433 return PyErr_NoMemory();
434 np
= (PyListObject
*) PyList_New(size
);
438 for (i
= 0; i
< n
; i
++) {
439 for (j
= 0; j
< a
->ob_size
; j
++) {
445 return (PyObject
*) np
;
449 list_ass_slice(PyListObject
*a
, int ilow
, int ihigh
, PyObject
*v
)
451 /* Because [X]DECREF can recursively invoke list operations on
452 this list, we must postpone all [X]DECREF activity until
453 after the list is back in its canonical shape. Therefore
454 we must allocate an additional array, 'recycle', into which
455 we temporarily copy the items that are deleted from the
457 PyObject
**recycle
, **p
;
459 int n
; /* Size of replacement list */
460 int d
; /* Change in size */
461 int k
; /* Loop index */
462 #define b ((PyListObject *)v)
465 else if (PyList_Check(v
)) {
468 /* Special case "a[i:j] = a" -- copy b first */
470 v
= list_slice(b
, 0, n
);
471 ret
= list_ass_slice(a
, ilow
, ihigh
, v
);
477 PyErr_Format(PyExc_TypeError
,
478 "must assign list (not \"%.200s\") to slice",
479 v
->ob_type
->tp_name
);
484 else if (ilow
> a
->ob_size
)
488 else if (ihigh
> a
->ob_size
)
491 d
= n
- (ihigh
-ilow
);
493 p
= recycle
= PyMem_NEW(PyObject
*, (ihigh
-ilow
));
496 if (d
<= 0) { /* Delete -d items; recycle ihigh-ilow items */
497 for (k
= ilow
; k
< ihigh
; k
++)
500 for (/*k = ihigh*/; k
< a
->ob_size
; k
++)
503 NRESIZE(item
, PyObject
*, a
->ob_size
); /* Can't fail */
507 else { /* Insert d items; recycle ihigh-ilow items */
508 NRESIZE(item
, PyObject
*, a
->ob_size
+ d
);
515 for (k
= a
->ob_size
; --k
>= ihigh
; )
517 for (/*k = ihigh-1*/; k
>= ilow
; --k
)
522 for (k
= 0; k
< n
; k
++, ilow
++) {
523 PyObject
*w
= b
->ob_item
[k
];
528 while (--p
>= recycle
)
532 if (a
->ob_size
== 0 && a
->ob_item
!= NULL
) {
533 PyMem_FREE(a
->ob_item
);
541 PyList_SetSlice(PyObject
*a
, int ilow
, int ihigh
, PyObject
*v
)
543 if (!PyList_Check(a
)) {
544 PyErr_BadInternalCall();
547 return list_ass_slice((PyListObject
*)a
, ilow
, ihigh
, v
);
551 list_inplace_repeat(PyListObject
*self
, int n
)
557 size
= PyList_GET_SIZE(self
);
560 return (PyObject
*)self
;
563 items
= self
->ob_item
;
566 self
->ob_item
= NULL
;
568 for (i
= 0; i
< size
; i
++)
569 Py_XDECREF(items
[i
]);
572 return (PyObject
*)self
;
575 NRESIZE(items
, PyObject
*, size
*n
);
580 self
->ob_item
= items
;
581 for (i
= 1; i
< n
; i
++) { /* Start counting at 1, not 0 */
582 for (j
= 0; j
< size
; j
++) {
583 PyObject
*o
= PyList_GET_ITEM(self
, j
);
585 PyList_SET_ITEM(self
, self
->ob_size
++, o
);
589 return (PyObject
*)self
;
595 list_ass_item(PyListObject
*a
, int i
, PyObject
*v
)
598 if (i
< 0 || i
>= a
->ob_size
) {
599 PyErr_SetString(PyExc_IndexError
,
600 "list assignment index out of range");
604 return list_ass_slice(a
, i
, i
+1, v
);
606 old_value
= a
->ob_item
[i
];
608 Py_DECREF(old_value
);
613 ins(PyListObject
*self
, int where
, PyObject
*v
)
615 if (ins1(self
, where
, v
) != 0)
622 listinsert(PyListObject
*self
, PyObject
*args
)
626 if (!PyArg_ParseTuple(args
, "iO:insert", &i
, &v
))
628 return ins(self
, i
, v
);
632 listappend(PyListObject
*self
, PyObject
*v
)
634 return ins(self
, (int) self
->ob_size
, v
);
638 listextend_internal(PyListObject
*self
, PyObject
*b
)
641 int selflen
= PyList_GET_SIZE(self
);
645 if (PyObject_Size(b
) == 0) {
646 /* short circuit when b is empty */
651 if (self
== (PyListObject
*)b
) {
652 /* as in list_ass_slice() we must special case the
653 * situation: a.extend(a)
655 * XXX: I think this way ought to be faster than using
656 * list_slice() the way list_ass_slice() does.
659 b
= PyList_New(selflen
);
662 for (i
= 0; i
< selflen
; i
++) {
663 PyObject
*o
= PyList_GET_ITEM(self
, i
);
665 PyList_SET_ITEM(b
, i
, o
);
669 blen
= PyObject_Size(b
);
671 /* resize a using idiom */
672 items
= self
->ob_item
;
673 NRESIZE(items
, PyObject
*, selflen
+ blen
);
680 self
->ob_item
= items
;
682 /* populate the end of self with b's items */
683 for (i
= 0; i
< blen
; i
++) {
684 PyObject
*o
= PySequence_Fast_GET_ITEM(b
, i
);
686 PyList_SET_ITEM(self
, self
->ob_size
++, o
);
694 list_inplace_concat(PyListObject
*self
, PyObject
*other
)
696 other
= PySequence_Fast(other
, "argument to += must be iterable");
700 if (listextend_internal(self
, other
) < 0)
704 return (PyObject
*)self
;
708 listextend(PyListObject
*self
, PyObject
*b
)
711 b
= PySequence_Fast(b
, "list.extend() argument must be iterable");
715 if (listextend_internal(self
, b
) < 0)
723 listpop(PyListObject
*self
, PyObject
*args
)
727 if (!PyArg_ParseTuple(args
, "|i:pop", &i
))
729 if (self
->ob_size
== 0) {
730 /* Special-case most common failure cause */
731 PyErr_SetString(PyExc_IndexError
, "pop from empty list");
736 if (i
< 0 || i
>= self
->ob_size
) {
737 PyErr_SetString(PyExc_IndexError
, "pop index out of range");
740 v
= self
->ob_item
[i
];
742 if (list_ass_slice(self
, i
, i
+1, (PyObject
*)NULL
) != 0) {
749 /* New quicksort implementation for arrays of object pointers.
750 Thanks to discussions with Tim Peters. */
752 /* CMPERROR is returned by our comparison function when an error
753 occurred. This is the largest negative integer (0x80000000 on a
755 #define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
757 /* Comparison function. Takes care of calling a user-supplied
758 comparison function (any callable Python object). Calls the
759 standard comparison function, PyObject_Compare(), if the user-
760 supplied function is NULL. */
763 docompare(PyObject
*x
, PyObject
*y
, PyObject
*compare
)
765 PyObject
*args
, *res
;
768 if (compare
== NULL
) {
769 /* NOTE: we rely on the fact here that the sorting algorithm
770 only ever checks whether k<0, i.e., whether x<y. So we
771 invoke the rich comparison function with Py_LT ('<'), and
772 return -1 when it returns true and 0 when it returns
774 i
= PyObject_RichCompareBool(x
, y
, Py_LT
);
781 args
= Py_BuildValue("(OO)", x
, y
);
784 res
= PyEval_CallObject(compare
, args
);
788 if (!PyInt_Check(res
)) {
790 PyErr_SetString(PyExc_TypeError
,
791 "comparison function must return int");
794 i
= PyInt_AsLong(res
);
803 /* MINSIZE is the smallest array that will get a full-blown samplesort
804 treatment; smaller arrays are sorted using binary insertion. It must
805 be at least 7 for the samplesort implementation to work. Binary
806 insertion does fewer compares, but can suffer O(N**2) data movement.
807 The more expensive compares, the larger MINSIZE should be. */
810 /* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
811 partition; smaller slices are passed to binarysort. It must be at
812 least 2, and no larger than MINSIZE. Setting it higher reduces the #
813 of compares slowly, but increases the amount of data movement quickly.
814 The value here was chosen assuming a compare costs ~25x more than
815 swapping a pair of memory-resident pointers -- but under that assumption,
816 changing the value by a few dozen more or less has aggregate effect
817 under 1%. So the value is crucial, but not touchy <wink>. */
818 #define MINPARTITIONSIZE 40
820 /* MAXMERGE is the largest number of elements we'll always merge into
821 a known-to-be sorted chunk via binary insertion, regardless of the
822 size of that chunk. Given a chunk of N sorted elements, and a group
823 of K unknowns, the largest K for which it's better to do insertion
824 (than a full-blown sort) is a complicated function of N and K mostly
825 involving the expected number of compares and data moves under each
826 approach, and the relative cost of those operations on a specific
827 architecure. The fixed value here is conservative, and should be a
828 clear win regardless of architecture or N. */
831 /* STACKSIZE is the size of our work stack. A rough estimate is that
832 this allows us to sort arrays of size N where
833 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
834 for arrays of size 2**64. Because we push the biggest partition
835 first, the worst case occurs when all subarrays are always partitioned
840 #define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
842 /* binarysort is the best method for sorting small arrays: it does
843 few compares, but can do data movement quadratic in the number of
845 [lo, hi) is a contiguous slice of a list, and is sorted via
847 On entry, must have lo <= start <= hi, and that [lo, start) is already
848 sorted (pass start == lo if you don't know!).
849 If docompare complains (returns CMPERROR) return -1, else 0.
850 Even in case of error, the output slice will be some permutation of
851 the input (nothing is lost or duplicated).
855 binarysort(PyObject
**lo
, PyObject
**hi
, PyObject
**start
, PyObject
*compare
)
856 /* compare -- comparison function object, or NULL for default */
858 /* assert lo <= start <= hi
859 assert [lo, start) is sorted */
861 register PyObject
**l
, **p
, **r
;
862 register PyObject
*pivot
;
866 for (; start
< hi
; ++start
) {
867 /* set l to where *start belongs */
872 p
= l
+ ((r
- l
) >> 1);
879 /* Pivot should go at l -- slide over to make room.
880 Caution: using memmove is much slower under MSVC 5;
881 we're not usually moving many slots. */
882 for (p
= start
; p
> l
; --p
)
892 /* samplesortslice is the sorting workhorse.
893 [lo, hi) is a contiguous slice of a list, to be sorted in place.
894 On entry, must have lo <= hi,
895 If docompare complains (returns CMPERROR) return -1, else 0.
896 Even in case of error, the output slice will be some permutation of
897 the input (nothing is lost or duplicated).
899 samplesort is basically quicksort on steroids: a power of 2 close
900 to n/ln(n) is computed, and that many elements (less 1) are picked at
901 random from the array and sorted. These 2**k - 1 elements are then
902 used as preselected pivots for an equal number of quicksort
903 partitioning steps, partitioning the slice into 2**k chunks each of
904 size about ln(n). These small final chunks are then usually handled
905 by binarysort. Note that when k=1, this is roughly the same as an
906 ordinary quicksort using a random pivot, and when k=2 this is roughly
907 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
908 this a "median of n/ln(n)" quicksort. You can also view it as a kind
909 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
911 The large number of samples makes a quadratic-time case almost
912 impossible, and asymptotically drives the average-case number of
913 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
914 3 variant) down to N lg N.
916 We also play lots of low-level tricks to cut the number of compares.
918 Very obscure: To avoid using extra memory, the PPs are stored in the
919 array and shuffled around as partitioning proceeds. At the start of a
920 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
921 adjacent (either on the left or the right!) to a chunk of X elements
922 that are to be partitioned: P X or X P. In either case we need to
923 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
924 left, followed by the PP to be used for this step (that's the middle
925 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
926 P X or X P -> Psmall pivot X Plarge
927 and the order of the PPs must not be altered. It can take a while
928 to realize this isn't trivial! It can take even longer <wink> to
929 understand why the simple code below works, using only 2**(m-1) swaps.
930 The key is that the order of the X elements isn't necessarily
931 preserved: X can end up as some cyclic permutation of its original
932 order. That's OK, because X is unsorted anyway. If the order of X
933 had to be preserved too, the simplest method I know of using O(1)
934 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
935 Since len(X) is typically several times larger than 2**(m-1), that
936 would slow things down.
939 struct SamplesortStackNode
{
940 /* Represents a slice of the array, from (& including) lo up
941 to (but excluding) hi. "extra" additional & adjacent elements
942 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
943 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
944 already sorted, but nothing is known about the other elements
945 in [lo, hi). |extra| is always one less than a power of 2.
946 When extra is 0, we're out of PPs, and the slice must be
947 sorted by some other means. */
953 /* The number of PPs we want is 2**k - 1, where 2**k is as close to
954 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
955 is undesirable, so cutoff values are canned in the "cutoff" table
956 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
958 static long cutoff
[] = {
959 43, /* smallest N such that k == 4 */
981 982548444, /* smallest N such that k == 26 */
982 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
986 samplesortslice(PyObject
**lo
, PyObject
**hi
, PyObject
*compare
)
987 /* compare -- comparison function object, or NULL for default */
989 register PyObject
**l
, **r
;
990 register PyObject
*tmp
, *pivot
;
992 int n
, extra
, top
, extraOnRight
;
993 struct SamplesortStackNode stack
[STACKSIZE
];
995 /* assert lo <= hi */
998 /* ----------------------------------------------------------
1000 * --------------------------------------------------------*/
1004 /* Set r to the largest value such that [lo,r) is sorted.
1005 This catches the already-sorted case, the all-the-same
1006 case, and the appended-a-few-elements-to-a-sorted-list case.
1007 If the array is unsorted, we're very likely to get out of
1008 the loop fast, so the test is cheap if it doesn't pay off.
1010 /* assert lo < hi */
1011 for (r
= lo
+1; r
< hi
; ++r
) {
1016 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
1017 few unknowns, or few elements in total. */
1018 if (hi
- r
<= MAXMERGE
|| n
< MINSIZE
)
1019 return binarysort(lo
, hi
, r
, compare
);
1021 /* Check for the array already being reverse-sorted. Typical
1022 benchmark-driven silliness <wink>. */
1023 /* assert lo < hi */
1024 for (r
= lo
+1; r
< hi
; ++r
) {
1029 if (hi
- r
<= MAXMERGE
) {
1030 /* Reverse the reversed prefix, then insert the tail */
1031 PyObject
**originalr
= r
;
1035 tmp
= *l
; *l
= *r
; *r
= tmp
;
1038 return binarysort(lo
, hi
, originalr
, compare
);
1041 /* ----------------------------------------------------------
1042 * Normal case setup: a large array without obvious pattern.
1043 * --------------------------------------------------------*/
1045 /* extra := a power of 2 ~= n/ln(n), less 1.
1046 First find the smallest extra s.t. n < cutoff[extra] */
1048 extra
< sizeof(cutoff
) / sizeof(cutoff
[0]);
1050 if (n
< cutoff
[extra
])
1052 /* note that if we fall out of the loop, the value of
1053 extra still makes *sense*, but may be smaller than
1054 we would like (but the array has more than ~= 2**31
1055 elements in this case!) */
1057 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1058 have is CUTOFFBASE-1, so
1059 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1060 extra
= (1 << (extra
- 1 + CUTOFFBASE
)) - 1;
1061 /* assert extra > 0 and n >= extra */
1063 /* Swap that many values to the start of the array. The
1064 selection of elements is pseudo-random, but the same on
1065 every run (this is intentional! timing algorithm changes is
1066 a pain if timing varies across runs). */
1068 unsigned int seed
= n
/ extra
; /* arbitrary */
1070 for (i
= 0; i
< (unsigned)extra
; ++i
) {
1071 /* j := random int in [i, n) */
1073 seed
= seed
* 69069 + 7;
1074 j
= i
+ seed
% (n
- i
);
1075 tmp
= lo
[i
]; lo
[i
] = lo
[j
]; lo
[j
] = tmp
;
1079 /* Recursively sort the preselected pivots. */
1080 if (samplesortslice(lo
, lo
+ extra
, compare
) < 0)
1083 top
= 0; /* index of available stack slot */
1084 lo
+= extra
; /* point to first unknown */
1085 extraOnRight
= 0; /* the PPs are at the left end */
1087 /* ----------------------------------------------------------
1088 * Partition [lo, hi), and repeat until out of work.
1089 * --------------------------------------------------------*/
1091 /* assert lo <= hi, so n >= 0 */
1094 /* We may not want, or may not be able, to partition:
1095 If n is small, it's quicker to insert.
1096 If extra is 0, we're out of pivots, and *must* use
1099 if (n
< MINPARTITIONSIZE
|| extra
== 0) {
1101 /* assert extra == 0
1102 This is rare, since the average size
1103 of a final block is only about
1105 if (samplesortslice(lo
, hi
, compare
) < 0)
1109 /* Binary insertion should be quicker,
1110 and we can take advantage of the PPs
1111 already being sorted. */
1112 if (extraOnRight
&& extra
) {
1113 /* swap the PPs to the left end */
1122 if (binarysort(lo
- extra
, hi
, lo
,
1127 /* Find another slice to work on. */
1129 break; /* no more -- done! */
1132 extra
= stack
[top
].extra
;
1141 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1142 Then our preselected pivot is at (extra-1)/2, and we
1143 want to move the PPs before that to the left end of
1144 the slice, and the PPs after that to the right end.
1145 The following section changes extra, lo, hi, and the
1147 [lo-extra, lo) contains the smaller PPs.
1149 (lo, hi) contains the unknown elements.
1150 [hi, hi+extra) contains the larger PPs.
1152 k
= extra
>>= 1; /* num PPs to move */
1154 /* Swap the smaller PPs to the left end.
1155 Note that this loop actually moves k+1 items:
1156 the last is our PP */
1158 tmp
= *lo
; *lo
= *hi
; *hi
= tmp
;
1163 /* Swap the larger PPs to the right end. */
1166 tmp
= *lo
; *lo
= *hi
; *hi
= tmp
;
1169 --lo
; /* *lo is now our PP */
1172 /* Now an almost-ordinary quicksort partition step.
1173 Note that most of the time is spent here!
1174 Only odd thing is that we partition into < and >=,
1175 instead of the usual <= and >=. This helps when
1176 there are lots of duplicates of different values,
1177 because it eventually tends to make subfiles
1178 "pure" (all duplicates), and we special-case for
1179 duplicates later. */
1182 /* assert lo < l < r < hi (small n weeded out above) */
1185 /* slide l right, looking for key >= pivot */
1194 /* slide r left, looking for key < pivot */
1196 register PyObject
*rval
= *r
--;
1199 /* swap and advance */
1208 /* assert lo < r <= l < hi
1209 assert r == l or r+1 == l
1210 everything to the left of l is < pivot, and
1211 everything to the right of r is >= pivot */
1220 /* assert lo <= r and r+1 == l and l <= hi
1221 assert r == lo or a[r] < pivot
1222 assert a[lo] is pivot
1223 assert l == hi or a[l] >= pivot
1224 Swap the pivot into "the middle", so we can henceforth
1230 /* The following is true now, & will be preserved:
1231 All in [lo,r) are < pivot
1232 All in [r,l) == pivot (& so can be ignored)
1233 All in [l,hi) are >= pivot */
1235 /* Check for duplicates of the pivot. One compare is
1236 wasted if there are no duplicates, but can win big
1238 Tricky: we're sticking to "<" compares, so deduce
1239 equality indirectly. We know pivot <= *l, so they're
1240 equal iff not pivot < *l.
1243 /* pivot <= *l known */
1248 /* <= and not < implies == */
1252 /* assert lo <= r < l <= hi
1253 Partitions are [lo, r) and [l, hi) */
1255 /* push fattest first; remember we still have extra PPs
1256 to the left of the left chunk and to the right of
1258 /* assert top < STACKSIZE */
1259 if (r
- lo
<= hi
- l
) {
1260 /* second is bigger */
1263 stack
[top
].extra
= -extra
;
1268 /* first is bigger */
1271 stack
[top
].extra
= extra
;
1277 } /* end of partitioning loop */
1287 staticforward PyTypeObject immutable_list_type
;
1290 listsort(PyListObject
*self
, PyObject
*args
)
1293 PyObject
*compare
= NULL
;
1294 PyTypeObject
*savetype
;
1297 if (!PyArg_ParseTuple(args
, "|O:sort", &compare
))
1300 savetype
= self
->ob_type
;
1301 self
->ob_type
= &immutable_list_type
;
1302 err
= samplesortslice(self
->ob_item
,
1303 self
->ob_item
+ self
->ob_size
,
1305 self
->ob_type
= savetype
;
1313 PyList_Sort(PyObject
*v
)
1315 if (v
== NULL
|| !PyList_Check(v
)) {
1316 PyErr_BadInternalCall();
1319 v
= listsort((PyListObject
*)v
, (PyObject
*)NULL
);
1327 _listreverse(PyListObject
*self
)
1329 register PyObject
**p
, **q
;
1330 register PyObject
*tmp
;
1332 if (self
->ob_size
> 1) {
1333 for (p
= self
->ob_item
, q
= self
->ob_item
+ self
->ob_size
- 1;
1345 listreverse(PyListObject
*self
)
1353 PyList_Reverse(PyObject
*v
)
1355 if (v
== NULL
|| !PyList_Check(v
)) {
1356 PyErr_BadInternalCall();
1359 _listreverse((PyListObject
*)v
);
1364 PyList_AsTuple(PyObject
*v
)
1369 if (v
== NULL
|| !PyList_Check(v
)) {
1370 PyErr_BadInternalCall();
1373 n
= ((PyListObject
*)v
)->ob_size
;
1377 p
= ((PyTupleObject
*)w
)->ob_item
;
1379 (void *)((PyListObject
*)v
)->ob_item
,
1380 n
*sizeof(PyObject
*));
1389 listindex(PyListObject
*self
, PyObject
*v
)
1393 for (i
= 0; i
< self
->ob_size
; i
++) {
1394 int cmp
= PyObject_RichCompareBool(self
->ob_item
[i
], v
, Py_EQ
);
1396 return PyInt_FromLong((long)i
);
1400 PyErr_SetString(PyExc_ValueError
, "list.index(x): x not in list");
1405 listcount(PyListObject
*self
, PyObject
*v
)
1410 for (i
= 0; i
< self
->ob_size
; i
++) {
1411 int cmp
= PyObject_RichCompareBool(self
->ob_item
[i
], v
, Py_EQ
);
1417 return PyInt_FromLong((long)count
);
1421 listremove(PyListObject
*self
, PyObject
*v
)
1425 for (i
= 0; i
< self
->ob_size
; i
++) {
1426 int cmp
= PyObject_RichCompareBool(self
->ob_item
[i
], v
, Py_EQ
);
1428 if (list_ass_slice(self
, i
, i
+1,
1429 (PyObject
*)NULL
) != 0)
1437 PyErr_SetString(PyExc_ValueError
, "list.remove(x): x not in list");
1442 list_traverse(PyListObject
*o
, visitproc visit
, void *arg
)
1447 for (i
= o
->ob_size
; --i
>= 0; ) {
1450 err
= visit(x
, arg
);
1459 list_clear(PyListObject
*lp
)
1461 (void) PyList_SetSlice((PyObject
*)lp
, 0, lp
->ob_size
, 0);
1466 list_richcompare(PyObject
*v
, PyObject
*w
, int op
)
1468 PyListObject
*vl
, *wl
;
1471 if (!PyList_Check(v
) || !PyList_Check(w
)) {
1472 Py_INCREF(Py_NotImplemented
);
1473 return Py_NotImplemented
;
1476 vl
= (PyListObject
*)v
;
1477 wl
= (PyListObject
*)w
;
1479 if (vl
->ob_size
!= wl
->ob_size
&& (op
== Py_EQ
|| op
== Py_NE
)) {
1480 /* Shortcut: if the lengths differ, the lists differ */
1490 /* Search for the first index where items are different */
1491 for (i
= 0; i
< vl
->ob_size
&& i
< wl
->ob_size
; i
++) {
1492 int k
= PyObject_RichCompareBool(vl
->ob_item
[i
],
1493 wl
->ob_item
[i
], Py_EQ
);
1500 if (i
>= vl
->ob_size
|| i
>= wl
->ob_size
) {
1501 /* No more items to compare -- compare sizes */
1502 int vs
= vl
->ob_size
;
1503 int ws
= wl
->ob_size
;
1507 case Py_LT
: cmp
= vs
< ws
; break;
1508 case Py_LE
: cmp
= vs
<= ws
; break;
1509 case Py_EQ
: cmp
= vs
== ws
; break;
1510 case Py_NE
: cmp
= vs
!= ws
; break;
1511 case Py_GT
: cmp
= vs
> ws
; break;
1512 case Py_GE
: cmp
= vs
>= ws
; break;
1513 default: return NULL
; /* cannot happen */
1523 /* We have an item that differs -- shortcuts for EQ/NE */
1525 Py_INCREF(Py_False
);
1533 /* Compare the final item again using the proper operator */
1534 return PyObject_RichCompare(vl
->ob_item
[i
], wl
->ob_item
[i
], op
);
1537 /* Adapted from newer code by Tim */
1539 list_fill(PyListObject
*result
, PyObject
*v
)
1541 PyObject
*it
; /* iter(v) */
1542 int n
; /* guess for result list size */
1545 n
= result
->ob_size
;
1547 /* Special-case list(a_list), for speed. */
1548 if (PyList_Check(v
)) {
1549 if (v
== (PyObject
*)result
)
1550 return 0; /* source is destination, we're done */
1551 return list_ass_slice(result
, 0, n
, v
);
1554 /* Empty previous contents */
1556 if (list_ass_slice(result
, 0, n
, (PyObject
*)NULL
) != 0)
1560 /* Get iterator. There may be some low-level efficiency to be gained
1561 * by caching the tp_iternext slot instead of using PyIter_Next()
1562 * later, but premature optimization is the root etc.
1564 it
= PyObject_GetIter(v
);
1568 /* Guess a result list size. */
1569 n
= -1; /* unknown */
1570 if (PySequence_Check(v
) &&
1571 v
->ob_type
->tp_as_sequence
->sq_length
) {
1572 n
= PySequence_Size(v
);
1577 n
= 8; /* arbitrary */
1578 NRESIZE(result
->ob_item
, PyObject
*, n
);
1579 if (result
->ob_item
== NULL
) {
1583 for (i
= 0; i
< n
; i
++)
1584 result
->ob_item
[i
] = NULL
;
1585 result
->ob_size
= n
;
1587 /* Run iterator to exhaustion. */
1588 for (i
= 0; ; i
++) {
1589 PyObject
*item
= PyIter_Next(it
);
1591 if (PyErr_Occurred())
1596 PyList_SET_ITEM(result
, i
, item
); /* steals ref */
1598 int status
= ins1(result
, result
->ob_size
, item
);
1599 Py_DECREF(item
); /* append creates a new ref */
1605 /* Cut back result list if initial guess was too large. */
1606 if (i
< n
&& result
!= NULL
) {
1607 if (list_ass_slice(result
, i
, n
, (PyObject
*)NULL
) != 0)
1619 list_init(PyListObject
*self
, PyObject
*args
, PyObject
*kw
)
1621 PyObject
*arg
= NULL
;
1622 static char *kwlist
[] = {"sequence", 0};
1624 if (!PyArg_ParseTupleAndKeywords(args
, kw
, "|O:list", kwlist
, &arg
))
1627 return list_fill(self
, arg
);
1628 if (self
->ob_size
> 0)
1629 return list_ass_slice(self
, 0, self
->ob_size
, (PyObject
*)NULL
);
1634 list_nohash(PyObject
*self
)
1636 PyErr_SetString(PyExc_TypeError
, "list objects are unhashable");
1640 static char append_doc
[] =
1641 "L.append(object) -- append object to end";
1642 static char extend_doc
[] =
1643 "L.extend(list) -- extend list by appending list elements";
1644 static char insert_doc
[] =
1645 "L.insert(index, object) -- insert object before index";
1646 static char pop_doc
[] =
1647 "L.pop([index]) -> item -- remove and return item at index (default last)";
1648 static char remove_doc
[] =
1649 "L.remove(value) -- remove first occurrence of value";
1650 static char index_doc
[] =
1651 "L.index(value) -> integer -- return index of first occurrence of value";
1652 static char count_doc
[] =
1653 "L.count(value) -> integer -- return number of occurrences of value";
1654 static char reverse_doc
[] =
1655 "L.reverse() -- reverse *IN PLACE*";
1656 static char sort_doc
[] =
1657 "L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1659 static PyMethodDef list_methods
[] = {
1660 {"append", (PyCFunction
)listappend
, METH_O
, append_doc
},
1661 {"insert", (PyCFunction
)listinsert
, METH_VARARGS
, insert_doc
},
1662 {"extend", (PyCFunction
)listextend
, METH_O
, extend_doc
},
1663 {"pop", (PyCFunction
)listpop
, METH_VARARGS
, pop_doc
},
1664 {"remove", (PyCFunction
)listremove
, METH_O
, remove_doc
},
1665 {"index", (PyCFunction
)listindex
, METH_O
, index_doc
},
1666 {"count", (PyCFunction
)listcount
, METH_O
, count_doc
},
1667 {"reverse", (PyCFunction
)listreverse
, METH_NOARGS
, reverse_doc
},
1668 {"sort", (PyCFunction
)listsort
, METH_VARARGS
, sort_doc
},
1669 {NULL
, NULL
} /* sentinel */
1672 static PySequenceMethods list_as_sequence
= {
1673 (inquiry
)list_length
, /* sq_length */
1674 (binaryfunc
)list_concat
, /* sq_concat */
1675 (intargfunc
)list_repeat
, /* sq_repeat */
1676 (intargfunc
)list_item
, /* sq_item */
1677 (intintargfunc
)list_slice
, /* sq_slice */
1678 (intobjargproc
)list_ass_item
, /* sq_ass_item */
1679 (intintobjargproc
)list_ass_slice
, /* sq_ass_slice */
1680 (objobjproc
)list_contains
, /* sq_contains */
1681 (binaryfunc
)list_inplace_concat
, /* sq_inplace_concat */
1682 (intargfunc
)list_inplace_repeat
, /* sq_inplace_repeat */
1685 static char list_doc
[] =
1686 "list() -> new list\n"
1687 "list(sequence) -> new list initialized from sequence's items";
1689 PyTypeObject PyList_Type
= {
1690 PyObject_HEAD_INIT(&PyType_Type
)
1693 sizeof(PyListObject
),
1695 (destructor
)list_dealloc
, /* tp_dealloc */
1696 (printfunc
)list_print
, /* tp_print */
1700 (reprfunc
)list_repr
, /* tp_repr */
1701 0, /* tp_as_number */
1702 &list_as_sequence
, /* tp_as_sequence */
1703 0, /* tp_as_mapping */
1704 list_nohash
, /* tp_hash */
1707 PyObject_GenericGetAttr
, /* tp_getattro */
1708 0, /* tp_setattro */
1709 0, /* tp_as_buffer */
1710 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1711 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1712 list_doc
, /* tp_doc */
1713 (traverseproc
)list_traverse
, /* tp_traverse */
1714 (inquiry
)list_clear
, /* tp_clear */
1715 list_richcompare
, /* tp_richcompare */
1716 0, /* tp_weaklistoffset */
1718 0, /* tp_iternext */
1719 list_methods
, /* tp_methods */
1724 0, /* tp_descr_get */
1725 0, /* tp_descr_set */
1726 0, /* tp_dictoffset */
1727 (initproc
)list_init
, /* tp_init */
1728 PyType_GenericAlloc
, /* tp_alloc */
1729 PyType_GenericNew
, /* tp_new */
1730 _PyObject_GC_Del
, /* tp_free */
1734 /* During a sort, we really can't have anyone modifying the list; it could
1735 cause core dumps. Thus, we substitute a dummy type that raises an
1736 explanatory exception when a modifying operation is used. Caveat:
1737 comparisons may behave differently; but I guess it's a bad idea anyway to
1738 compare a list that's being sorted... */
1741 immutable_list_op(void)
1743 PyErr_SetString(PyExc_TypeError
,
1744 "a list cannot be modified while it is being sorted");
1748 static PyMethodDef immutable_list_methods
[] = {
1749 {"append", (PyCFunction
)immutable_list_op
, METH_VARARGS
},
1750 {"insert", (PyCFunction
)immutable_list_op
, METH_VARARGS
},
1751 {"extend", (PyCFunction
)immutable_list_op
, METH_O
},
1752 {"pop", (PyCFunction
)immutable_list_op
, METH_VARARGS
},
1753 {"remove", (PyCFunction
)immutable_list_op
, METH_VARARGS
},
1754 {"index", (PyCFunction
)listindex
, METH_O
},
1755 {"count", (PyCFunction
)listcount
, METH_O
},
1756 {"reverse", (PyCFunction
)immutable_list_op
, METH_VARARGS
},
1757 {"sort", (PyCFunction
)immutable_list_op
, METH_VARARGS
},
1758 {NULL
, NULL
} /* sentinel */
1762 immutable_list_ass(void)
1764 immutable_list_op();
1768 static PySequenceMethods immutable_list_as_sequence
= {
1769 (inquiry
)list_length
, /* sq_length */
1770 (binaryfunc
)list_concat
, /* sq_concat */
1771 (intargfunc
)list_repeat
, /* sq_repeat */
1772 (intargfunc
)list_item
, /* sq_item */
1773 (intintargfunc
)list_slice
, /* sq_slice */
1774 (intobjargproc
)immutable_list_ass
, /* sq_ass_item */
1775 (intintobjargproc
)immutable_list_ass
, /* sq_ass_slice */
1776 (objobjproc
)list_contains
, /* sq_contains */
1779 static PyTypeObject immutable_list_type
= {
1780 PyObject_HEAD_INIT(&PyType_Type
)
1782 "list (immutable, during sort)",
1783 sizeof(PyListObject
),
1785 0, /* Cannot happen */ /* tp_dealloc */
1786 (printfunc
)list_print
, /* tp_print */
1789 0, /* Won't be called */ /* tp_compare */
1790 (reprfunc
)list_repr
, /* tp_repr */
1791 0, /* tp_as_number */
1792 &immutable_list_as_sequence
, /* tp_as_sequence */
1793 0, /* tp_as_mapping */
1794 list_nohash
, /* tp_hash */
1797 PyObject_GenericGetAttr
, /* tp_getattro */
1798 0, /* tp_setattro */
1799 0, /* tp_as_buffer */
1800 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
1801 list_doc
, /* tp_doc */
1802 (traverseproc
)list_traverse
, /* tp_traverse */
1804 list_richcompare
, /* tp_richcompare */
1805 0, /* tp_weaklistoffset */
1807 0, /* tp_iternext */
1808 immutable_list_methods
, /* tp_methods */
1813 0, /* tp_descr_get */
1814 0, /* tp_descr_set */
1816 /* NOTE: This is *not* the standard list_type struct! */