1 /* List object implementation */
8 #include <sys/types.h> /* For size_t */
11 /* Ensure ob_item has room for at least newsize elements, and set
12 * ob_size to newsize. If newsize > ob_size on entry, the content
13 * of the new slots at exit is undefined heap trash; it's the caller's
14 * responsiblity to overwrite them with sane values.
15 * The number of allocated elements may grow, shrink, or stay the same.
16 * Failure is impossible if newsize <= self.allocated on entry, although
17 * that partly relies on an assumption that the system realloc() never
18 * fails when passed a number of bytes <= the number of bytes last
19 * allocated (the C standard doesn't guarantee this, but it's hard to
20 * imagine a realloc implementation where it wouldn't be true).
21 * Note that self->ob_item may change, and even if newsize is less
22 * than ob_size on entry.
25 list_resize(PyListObject
*self
, Py_ssize_t newsize
)
29 Py_ssize_t allocated
= self
->allocated
;
31 /* Bypass realloc() when a previous overallocation is large enough
32 to accommodate the newsize. If the newsize falls lower than half
33 the allocated size, then proceed with the realloc() to shrink the list.
35 if (allocated
>= newsize
&& newsize
>= (allocated
>> 1)) {
36 assert(self
->ob_item
!= NULL
|| newsize
== 0);
37 Py_SIZE(self
) = newsize
;
41 /* This over-allocates proportional to the list size, making room
42 * for additional growth. The over-allocation is mild, but is
43 * enough to give linear-time amortized behavior over a long
44 * sequence of appends() in the presence of a poorly-performing
46 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
48 new_allocated
= (newsize
>> 3) + (newsize
< 9 ? 3 : 6);
50 /* check for integer overflow */
51 if (new_allocated
> PY_SIZE_MAX
- newsize
) {
55 new_allocated
+= newsize
;
60 items
= self
->ob_item
;
61 if (new_allocated
<= ((~(size_t)0) / sizeof(PyObject
*)))
62 PyMem_RESIZE(items
, PyObject
*, new_allocated
);
69 self
->ob_item
= items
;
70 Py_SIZE(self
) = newsize
;
71 self
->allocated
= new_allocated
;
75 /* Debug statistic to compare allocations with reuse through the free list */
76 #undef SHOW_ALLOC_COUNT
77 #ifdef SHOW_ALLOC_COUNT
78 static size_t count_alloc
= 0;
79 static size_t count_reuse
= 0;
84 fprintf(stderr
, "List allocations: %" PY_FORMAT_SIZE_T
"d\n",
86 fprintf(stderr
, "List reuse through freelist: %" PY_FORMAT_SIZE_T
88 fprintf(stderr
, "%.2f%% reuse rate\n\n",
89 (100.0*count_reuse
/(count_alloc
+count_reuse
)));
93 /* Empty list reuse scheme to save calls to malloc and free */
94 #ifndef PyList_MAXFREELIST
95 #define PyList_MAXFREELIST 80
97 static PyListObject
*free_list
[PyList_MAXFREELIST
];
98 static int numfree
= 0;
106 op
= free_list
[--numfree
];
107 assert(PyList_CheckExact(op
));
113 PyList_New(Py_ssize_t size
)
117 #ifdef SHOW_ALLOC_COUNT
118 static int initialized
= 0;
120 Py_AtExit(show_alloc
);
126 PyErr_BadInternalCall();
129 nbytes
= size
* sizeof(PyObject
*);
130 /* Check for overflow without an actual overflow,
131 * which can cause compiler to optimise out */
132 if (size
> PY_SIZE_MAX
/ sizeof(PyObject
*))
133 return PyErr_NoMemory();
136 op
= free_list
[numfree
];
137 _Py_NewReference((PyObject
*)op
);
138 #ifdef SHOW_ALLOC_COUNT
142 op
= PyObject_GC_New(PyListObject
, &PyList_Type
);
145 #ifdef SHOW_ALLOC_COUNT
152 op
->ob_item
= (PyObject
**) PyMem_MALLOC(nbytes
);
153 if (op
->ob_item
== NULL
) {
155 return PyErr_NoMemory();
157 memset(op
->ob_item
, 0, nbytes
);
160 op
->allocated
= size
;
161 _PyObject_GC_TRACK(op
);
162 return (PyObject
*) op
;
166 PyList_Size(PyObject
*op
)
168 if (!PyList_Check(op
)) {
169 PyErr_BadInternalCall();
176 static PyObject
*indexerr
= NULL
;
179 PyList_GetItem(PyObject
*op
, Py_ssize_t i
)
181 if (!PyList_Check(op
)) {
182 PyErr_BadInternalCall();
185 if (i
< 0 || i
>= Py_SIZE(op
)) {
186 if (indexerr
== NULL
)
187 indexerr
= PyString_FromString(
188 "list index out of range");
189 PyErr_SetObject(PyExc_IndexError
, indexerr
);
192 return ((PyListObject
*)op
) -> ob_item
[i
];
196 PyList_SetItem(register PyObject
*op
, register Py_ssize_t i
,
197 register PyObject
*newitem
)
199 register PyObject
*olditem
;
200 register PyObject
**p
;
201 if (!PyList_Check(op
)) {
203 PyErr_BadInternalCall();
206 if (i
< 0 || i
>= Py_SIZE(op
)) {
208 PyErr_SetString(PyExc_IndexError
,
209 "list assignment index out of range");
212 p
= ((PyListObject
*)op
) -> ob_item
+ i
;
220 ins1(PyListObject
*self
, Py_ssize_t where
, PyObject
*v
)
222 Py_ssize_t i
, n
= Py_SIZE(self
);
225 PyErr_BadInternalCall();
228 if (n
== PY_SSIZE_T_MAX
) {
229 PyErr_SetString(PyExc_OverflowError
,
230 "cannot add more objects to list");
234 if (list_resize(self
, n
+1) == -1)
244 items
= self
->ob_item
;
245 for (i
= n
; --i
>= where
; )
246 items
[i
+1] = items
[i
];
253 PyList_Insert(PyObject
*op
, Py_ssize_t where
, PyObject
*newitem
)
255 if (!PyList_Check(op
)) {
256 PyErr_BadInternalCall();
259 return ins1((PyListObject
*)op
, where
, newitem
);
263 app1(PyListObject
*self
, PyObject
*v
)
265 Py_ssize_t n
= PyList_GET_SIZE(self
);
268 if (n
== PY_SSIZE_T_MAX
) {
269 PyErr_SetString(PyExc_OverflowError
,
270 "cannot add more objects to list");
274 if (list_resize(self
, n
+1) == -1)
278 PyList_SET_ITEM(self
, n
, v
);
283 PyList_Append(PyObject
*op
, PyObject
*newitem
)
285 if (PyList_Check(op
) && (newitem
!= NULL
))
286 return app1((PyListObject
*)op
, newitem
);
287 PyErr_BadInternalCall();
294 list_dealloc(PyListObject
*op
)
297 PyObject_GC_UnTrack(op
);
298 Py_TRASHCAN_SAFE_BEGIN(op
)
299 if (op
->ob_item
!= NULL
) {
300 /* Do it backwards, for Christian Tismer.
301 There's a simple test case where somehow this reduces
302 thrashing when a *very* large list is created and
303 immediately deleted. */
306 Py_XDECREF(op
->ob_item
[i
]);
308 PyMem_FREE(op
->ob_item
);
310 if (numfree
< PyList_MAXFREELIST
&& PyList_CheckExact(op
))
311 free_list
[numfree
++] = op
;
313 Py_TYPE(op
)->tp_free((PyObject
*)op
);
314 Py_TRASHCAN_SAFE_END(op
)
318 list_print(PyListObject
*op
, FILE *fp
, int flags
)
323 rc
= Py_ReprEnter((PyObject
*)op
);
327 Py_BEGIN_ALLOW_THREADS
328 fprintf(fp
, "[...]");
332 Py_BEGIN_ALLOW_THREADS
335 for (i
= 0; i
< Py_SIZE(op
); i
++) {
337 Py_BEGIN_ALLOW_THREADS
341 if (PyObject_Print(op
->ob_item
[i
], fp
, 0) != 0) {
342 Py_ReprLeave((PyObject
*)op
);
346 Py_BEGIN_ALLOW_THREADS
349 Py_ReprLeave((PyObject
*)op
);
354 list_repr(PyListObject
*v
)
358 PyObject
*pieces
= NULL
, *result
= NULL
;
360 i
= Py_ReprEnter((PyObject
*)v
);
362 return i
> 0 ? PyString_FromString("[...]") : NULL
;
365 if (Py_SIZE(v
) == 0) {
366 result
= PyString_FromString("[]");
370 pieces
= PyList_New(0);
374 /* Do repr() on each element. Note that this may mutate the list,
375 so must refetch the list size on each iteration. */
376 for (i
= 0; i
< Py_SIZE(v
); ++i
) {
378 if (Py_EnterRecursiveCall(" while getting the repr of a list"))
380 s
= PyObject_Repr(v
->ob_item
[i
]);
381 Py_LeaveRecursiveCall();
384 status
= PyList_Append(pieces
, s
);
385 Py_DECREF(s
); /* append created a new ref */
390 /* Add "[]" decorations to the first and last items. */
391 assert(PyList_GET_SIZE(pieces
) > 0);
392 s
= PyString_FromString("[");
395 temp
= PyList_GET_ITEM(pieces
, 0);
396 PyString_ConcatAndDel(&s
, temp
);
397 PyList_SET_ITEM(pieces
, 0, s
);
401 s
= PyString_FromString("]");
404 temp
= PyList_GET_ITEM(pieces
, PyList_GET_SIZE(pieces
) - 1);
405 PyString_ConcatAndDel(&temp
, s
);
406 PyList_SET_ITEM(pieces
, PyList_GET_SIZE(pieces
) - 1, temp
);
410 /* Paste them all together with ", " between. */
411 s
= PyString_FromString(", ");
414 result
= _PyString_Join(s
, pieces
);
419 Py_ReprLeave((PyObject
*)v
);
424 list_length(PyListObject
*a
)
430 list_contains(PyListObject
*a
, PyObject
*el
)
435 for (i
= 0, cmp
= 0 ; cmp
== 0 && i
< Py_SIZE(a
); ++i
)
436 cmp
= PyObject_RichCompareBool(el
, PyList_GET_ITEM(a
, i
),
442 list_item(PyListObject
*a
, Py_ssize_t i
)
444 if (i
< 0 || i
>= Py_SIZE(a
)) {
445 if (indexerr
== NULL
)
446 indexerr
= PyString_FromString(
447 "list index out of range");
448 PyErr_SetObject(PyExc_IndexError
, indexerr
);
451 Py_INCREF(a
->ob_item
[i
]);
452 return a
->ob_item
[i
];
456 list_slice(PyListObject
*a
, Py_ssize_t ilow
, Py_ssize_t ihigh
)
459 PyObject
**src
, **dest
;
463 else if (ilow
> Py_SIZE(a
))
467 else if (ihigh
> Py_SIZE(a
))
470 np
= (PyListObject
*) PyList_New(len
);
474 src
= a
->ob_item
+ ilow
;
476 for (i
= 0; i
< len
; i
++) {
477 PyObject
*v
= src
[i
];
481 return (PyObject
*)np
;
485 PyList_GetSlice(PyObject
*a
, Py_ssize_t ilow
, Py_ssize_t ihigh
)
487 if (!PyList_Check(a
)) {
488 PyErr_BadInternalCall();
491 return list_slice((PyListObject
*)a
, ilow
, ihigh
);
495 list_concat(PyListObject
*a
, PyObject
*bb
)
499 PyObject
**src
, **dest
;
501 if (!PyList_Check(bb
)) {
502 PyErr_Format(PyExc_TypeError
,
503 "can only concatenate list (not \"%.200s\") to list",
504 bb
->ob_type
->tp_name
);
507 #define b ((PyListObject *)bb)
508 size
= Py_SIZE(a
) + Py_SIZE(b
);
510 return PyErr_NoMemory();
511 np
= (PyListObject
*) PyList_New(size
);
517 for (i
= 0; i
< Py_SIZE(a
); i
++) {
518 PyObject
*v
= src
[i
];
523 dest
= np
->ob_item
+ Py_SIZE(a
);
524 for (i
= 0; i
< Py_SIZE(b
); i
++) {
525 PyObject
*v
= src
[i
];
529 return (PyObject
*)np
;
534 list_repeat(PyListObject
*a
, Py_ssize_t n
)
539 PyObject
**p
, **items
;
543 size
= Py_SIZE(a
) * n
;
544 if (n
&& size
/n
!= Py_SIZE(a
))
545 return PyErr_NoMemory();
547 return PyList_New(0);
548 np
= (PyListObject
*) PyList_New(size
);
553 if (Py_SIZE(a
) == 1) {
554 elem
= a
->ob_item
[0];
555 for (i
= 0; i
< n
; i
++) {
559 return (PyObject
*) np
;
563 for (i
= 0; i
< n
; i
++) {
564 for (j
= 0; j
< Py_SIZE(a
); j
++) {
570 return (PyObject
*) np
;
574 list_clear(PyListObject
*a
)
577 PyObject
**item
= a
->ob_item
;
579 /* Because XDECREF can recursively invoke operations on
580 this list, we make it empty first. */
590 /* Never fails; the return value can be ignored.
591 Note that there is no guarantee that the list is actually empty
592 at this point, because XDECREF may have populated it again! */
596 /* a[ilow:ihigh] = v if v != NULL.
597 * del a[ilow:ihigh] if v == NULL.
599 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
600 * guaranteed the call cannot fail.
603 list_ass_slice(PyListObject
*a
, Py_ssize_t ilow
, Py_ssize_t ihigh
, PyObject
*v
)
605 /* Because [X]DECREF can recursively invoke list operations on
606 this list, we must postpone all [X]DECREF activity until
607 after the list is back in its canonical shape. Therefore
608 we must allocate an additional array, 'recycle', into which
609 we temporarily copy the items that are deleted from the
611 PyObject
*recycle_on_stack
[8];
612 PyObject
**recycle
= recycle_on_stack
; /* will allocate more if needed */
614 PyObject
**vitem
= NULL
;
615 PyObject
*v_as_SF
= NULL
; /* PySequence_Fast(v) */
616 Py_ssize_t n
; /* # of elements in replacement list */
617 Py_ssize_t norig
; /* # of elements in list getting replaced */
618 Py_ssize_t d
; /* Change in size */
621 int result
= -1; /* guilty until proved innocent */
622 #define b ((PyListObject *)v)
627 /* Special case "a[i:j] = a" -- copy b first */
628 v
= list_slice(b
, 0, Py_SIZE(b
));
631 result
= list_ass_slice(a
, ilow
, ihigh
, v
);
635 v_as_SF
= PySequence_Fast(v
, "can only assign an iterable");
638 n
= PySequence_Fast_GET_SIZE(v_as_SF
);
639 vitem
= PySequence_Fast_ITEMS(v_as_SF
);
643 else if (ilow
> Py_SIZE(a
))
648 else if (ihigh
> Py_SIZE(a
))
651 norig
= ihigh
- ilow
;
654 if (Py_SIZE(a
) + d
== 0) {
656 return list_clear(a
);
659 /* recycle the items that we are about to remove */
660 s
= norig
* sizeof(PyObject
*);
661 if (s
> sizeof(recycle_on_stack
)) {
662 recycle
= (PyObject
**)PyMem_MALLOC(s
);
663 if (recycle
== NULL
) {
668 memcpy(recycle
, &item
[ilow
], s
);
670 if (d
< 0) { /* Delete -d items */
671 memmove(&item
[ihigh
+d
], &item
[ihigh
],
672 (Py_SIZE(a
) - ihigh
)*sizeof(PyObject
*));
673 list_resize(a
, Py_SIZE(a
) + d
);
676 else if (d
> 0) { /* Insert d items */
678 if (list_resize(a
, k
+d
) < 0)
681 memmove(&item
[ihigh
+d
], &item
[ihigh
],
682 (k
- ihigh
)*sizeof(PyObject
*));
684 for (k
= 0; k
< n
; k
++, ilow
++) {
685 PyObject
*w
= vitem
[k
];
689 for (k
= norig
- 1; k
>= 0; --k
)
690 Py_XDECREF(recycle
[k
]);
693 if (recycle
!= recycle_on_stack
)
701 PyList_SetSlice(PyObject
*a
, Py_ssize_t ilow
, Py_ssize_t ihigh
, PyObject
*v
)
703 if (!PyList_Check(a
)) {
704 PyErr_BadInternalCall();
707 return list_ass_slice((PyListObject
*)a
, ilow
, ihigh
, v
);
711 list_inplace_repeat(PyListObject
*self
, Py_ssize_t n
)
714 Py_ssize_t size
, i
, j
, p
;
717 size
= PyList_GET_SIZE(self
);
718 if (size
== 0 || n
== 1) {
720 return (PyObject
*)self
;
724 (void)list_clear(self
);
726 return (PyObject
*)self
;
729 if (size
> PY_SSIZE_T_MAX
/ n
) {
730 return PyErr_NoMemory();
733 if (list_resize(self
, size
*n
) == -1)
737 items
= self
->ob_item
;
738 for (i
= 1; i
< n
; i
++) { /* Start counting at 1, not 0 */
739 for (j
= 0; j
< size
; j
++) {
740 PyObject
*o
= items
[j
];
746 return (PyObject
*)self
;
750 list_ass_item(PyListObject
*a
, Py_ssize_t i
, PyObject
*v
)
753 if (i
< 0 || i
>= Py_SIZE(a
)) {
754 PyErr_SetString(PyExc_IndexError
,
755 "list assignment index out of range");
759 return list_ass_slice(a
, i
, i
+1, v
);
761 old_value
= a
->ob_item
[i
];
763 Py_DECREF(old_value
);
768 listinsert(PyListObject
*self
, PyObject
*args
)
772 if (!PyArg_ParseTuple(args
, "nO:insert", &i
, &v
))
774 if (ins1(self
, i
, v
) == 0)
780 listappend(PyListObject
*self
, PyObject
*v
)
782 if (app1(self
, v
) == 0)
788 listextend(PyListObject
*self
, PyObject
*b
)
790 PyObject
*it
; /* iter(v) */
791 Py_ssize_t m
; /* size of self */
792 Py_ssize_t n
; /* guess for size of b */
793 Py_ssize_t mn
; /* m + n */
795 PyObject
*(*iternext
)(PyObject
*);
798 1) lists and tuples which can use PySequence_Fast ops
799 2) extending self to self requires making a copy first
801 if (PyList_CheckExact(b
) || PyTuple_CheckExact(b
) || (PyObject
*)self
== b
) {
802 PyObject
**src
, **dest
;
803 b
= PySequence_Fast(b
, "argument must be iterable");
806 n
= PySequence_Fast_GET_SIZE(b
);
808 /* short circuit when b is empty */
813 if (list_resize(self
, m
+ n
) == -1) {
817 /* note that we may still have self == b here for the
818 * situation a.extend(a), but the following code works
819 * in that case too. Just make sure to resize self
820 * before calling PySequence_Fast_ITEMS.
822 /* populate the end of self with b's items */
823 src
= PySequence_Fast_ITEMS(b
);
824 dest
= self
->ob_item
+ m
;
825 for (i
= 0; i
< n
; i
++) {
826 PyObject
*o
= src
[i
];
834 it
= PyObject_GetIter(b
);
837 iternext
= *it
->ob_type
->tp_iternext
;
839 /* Guess a result list size. */
840 n
= _PyObject_LengthHint(b
, 8);
845 if (list_resize(self
, mn
) == -1)
847 /* Make the list sane again. */
850 /* Else m + n overflowed; on the chance that n lied, and there really
851 * is enough room, ignore it. If n was telling the truth, we'll
852 * eventually run out of memory during the loop.
855 /* Run iterator to exhaustion. */
857 PyObject
*item
= iternext(it
);
859 if (PyErr_Occurred()) {
860 if (PyErr_ExceptionMatches(PyExc_StopIteration
))
867 if (Py_SIZE(self
) < self
->allocated
) {
869 PyList_SET_ITEM(self
, Py_SIZE(self
), item
);
873 int status
= app1(self
, item
);
874 Py_DECREF(item
); /* append creates a new ref */
880 /* Cut back result list if initial guess was too large. */
881 if (Py_SIZE(self
) < self
->allocated
)
882 list_resize(self
, Py_SIZE(self
)); /* shrinking can't fail */
893 _PyList_Extend(PyListObject
*self
, PyObject
*b
)
895 return listextend(self
, b
);
899 list_inplace_concat(PyListObject
*self
, PyObject
*other
)
903 result
= listextend(self
, other
);
908 return (PyObject
*)self
;
912 listpop(PyListObject
*self
, PyObject
*args
)
918 if (!PyArg_ParseTuple(args
, "|n:pop", &i
))
921 if (Py_SIZE(self
) == 0) {
922 /* Special-case most common failure cause */
923 PyErr_SetString(PyExc_IndexError
, "pop from empty list");
928 if (i
< 0 || i
>= Py_SIZE(self
)) {
929 PyErr_SetString(PyExc_IndexError
, "pop index out of range");
932 v
= self
->ob_item
[i
];
933 if (i
== Py_SIZE(self
) - 1) {
934 status
= list_resize(self
, Py_SIZE(self
) - 1);
936 return v
; /* and v now owns the reference the list had */
939 status
= list_ass_slice(self
, i
, i
+1, (PyObject
*)NULL
);
941 /* Use status, so that in a release build compilers don't
942 * complain about the unused name.
949 /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
951 reverse_slice(PyObject
**lo
, PyObject
**hi
)
965 /* Lots of code for an adaptive, stable, natural mergesort. There are many
966 * pieces to this algorithm; read listsort.txt for overviews and details.
969 /* Comparison function. Takes care of calling a user-supplied
970 * comparison function (any callable Python object), which must not be
971 * NULL (use the ISLT macro if you don't know, or call PyObject_RichCompareBool
972 * with Py_LT if you know it's NULL).
973 * Returns -1 on error, 1 if x < y, 0 if x >= y.
976 islt(PyObject
*x
, PyObject
*y
, PyObject
*compare
)
982 assert(compare
!= NULL
);
983 /* Call the user's comparison function and translate the 3-way
984 * result into true or false (or error).
986 args
= PyTuple_New(2);
991 PyTuple_SET_ITEM(args
, 0, x
);
992 PyTuple_SET_ITEM(args
, 1, y
);
993 res
= PyObject_Call(compare
, args
, NULL
);
997 if (!PyInt_Check(res
)) {
998 PyErr_Format(PyExc_TypeError
,
999 "comparison function must return int, not %.200s",
1000 res
->ob_type
->tp_name
);
1004 i
= PyInt_AsLong(res
);
1009 /* If COMPARE is NULL, calls PyObject_RichCompareBool with Py_LT, else calls
1010 * islt. This avoids a layer of function call in the usual case, and
1011 * sorting does many comparisons.
1012 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1014 #define ISLT(X, Y, COMPARE) ((COMPARE) == NULL ? \
1015 PyObject_RichCompareBool(X, Y, Py_LT) : \
1016 islt(X, Y, COMPARE))
1018 /* Compare X to Y via "<". Goto "fail" if the comparison raises an
1019 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1020 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1022 #define IFLT(X, Y) if ((k = ISLT(X, Y, compare)) < 0) goto fail; \
1025 /* binarysort is the best method for sorting small arrays: it does
1026 few compares, but can do data movement quadratic in the number of
1028 [lo, hi) is a contiguous slice of a list, and is sorted via
1029 binary insertion. This sort is stable.
1030 On entry, must have lo <= start <= hi, and that [lo, start) is already
1031 sorted (pass start == lo if you don't know!).
1032 If islt() complains return -1, else 0.
1033 Even in case of error, the output slice will be some permutation of
1034 the input (nothing is lost or duplicated).
1037 binarysort(PyObject
**lo
, PyObject
**hi
, PyObject
**start
, PyObject
*compare
)
1038 /* compare -- comparison function object, or NULL for default */
1040 register Py_ssize_t k
;
1041 register PyObject
**l
, **p
, **r
;
1042 register PyObject
*pivot
;
1044 assert(lo
<= start
&& start
<= hi
);
1045 /* assert [lo, start) is sorted */
1048 for (; start
< hi
; ++start
) {
1049 /* set l to where *start belongs */
1054 * pivot >= all in [lo, l).
1055 * pivot < all in [r, start).
1056 * The second is vacuously true at the start.
1060 p
= l
+ ((r
- l
) >> 1);
1067 /* The invariants still hold, so pivot >= all in [lo, l) and
1068 pivot < all in [l, start), so pivot belongs at l. Note
1069 that if there are elements equal to pivot, l points to the
1070 first slot after them -- that's why this sort is stable.
1071 Slide over to make room.
1072 Caution: using memmove is much slower under MSVC 5;
1073 we're not usually moving many slots. */
1074 for (p
= start
; p
> l
; --p
)
1085 Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1086 is required on entry. "A run" is the longest ascending sequence, with
1088 lo[0] <= lo[1] <= lo[2] <= ...
1090 or the longest descending sequence, with
1092 lo[0] > lo[1] > lo[2] > ...
1094 Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1095 For its intended use in a stable mergesort, the strictness of the defn of
1096 "descending" is needed so that the caller can safely reverse a descending
1097 sequence without violating stability (strict > ensures there are no equal
1098 elements to get out of order).
1100 Returns -1 in case of error.
1103 count_run(PyObject
**lo
, PyObject
**hi
, PyObject
*compare
, int *descending
)
1115 IFLT(*lo
, *(lo
-1)) {
1117 for (lo
= lo
+1; lo
< hi
; ++lo
, ++n
) {
1125 for (lo
= lo
+1; lo
< hi
; ++lo
, ++n
) {
1137 Locate the proper position of key in a sorted vector; if the vector contains
1138 an element equal to key, return the position immediately to the left of
1139 the leftmost equal element. [gallop_right() does the same except returns
1140 the position to the right of the rightmost equal element (if any).]
1142 "a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
1144 "hint" is an index at which to begin the search, 0 <= hint < n. The closer
1145 hint is to the final result, the faster this runs.
1147 The return value is the int k in 0..n such that
1149 a[k-1] < key <= a[k]
1151 pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1152 key belongs at index k; or, IOW, the first k elements of a should precede
1153 key, and the last n-k should follow key.
1155 Returns -1 on error. See listsort.txt for info on the method.
1158 gallop_left(PyObject
*key
, PyObject
**a
, Py_ssize_t n
, Py_ssize_t hint
, PyObject
*compare
)
1164 assert(key
&& a
&& n
> 0 && hint
>= 0 && hint
< n
);
1170 /* a[hint] < key -- gallop right, until
1171 * a[hint + lastofs] < key <= a[hint + ofs]
1173 const Py_ssize_t maxofs
= n
- hint
; /* &a[n-1] is highest */
1174 while (ofs
< maxofs
) {
1177 ofs
= (ofs
<< 1) + 1;
1178 if (ofs
<= 0) /* int overflow */
1181 else /* key <= a[hint + ofs] */
1186 /* Translate back to offsets relative to &a[0]. */
1191 /* key <= a[hint] -- gallop left, until
1192 * a[hint - ofs] < key <= a[hint - lastofs]
1194 const Py_ssize_t maxofs
= hint
+ 1; /* &a[0] is lowest */
1195 while (ofs
< maxofs
) {
1198 /* key <= a[hint - ofs] */
1200 ofs
= (ofs
<< 1) + 1;
1201 if (ofs
<= 0) /* int overflow */
1206 /* Translate back to positive offsets relative to &a[0]. */
1208 lastofs
= hint
- ofs
;
1213 assert(-1 <= lastofs
&& lastofs
< ofs
&& ofs
<= n
);
1214 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1215 * right of lastofs but no farther right than ofs. Do a binary
1216 * search, with invariant a[lastofs-1] < key <= a[ofs].
1219 while (lastofs
< ofs
) {
1220 Py_ssize_t m
= lastofs
+ ((ofs
- lastofs
) >> 1);
1223 lastofs
= m
+1; /* a[m] < key */
1225 ofs
= m
; /* key <= a[m] */
1227 assert(lastofs
== ofs
); /* so a[ofs-1] < key <= a[ofs] */
1235 Exactly like gallop_left(), except that if key already exists in a[0:n],
1236 finds the position immediately to the right of the rightmost equal value.
1238 The return value is the int k in 0..n such that
1240 a[k-1] <= key < a[k]
1244 The code duplication is massive, but this is enough different given that
1245 we're sticking to "<" comparisons that it's much harder to follow if
1246 written as one routine with yet another "left or right?" flag.
1249 gallop_right(PyObject
*key
, PyObject
**a
, Py_ssize_t n
, Py_ssize_t hint
, PyObject
*compare
)
1255 assert(key
&& a
&& n
> 0 && hint
>= 0 && hint
< n
);
1261 /* key < a[hint] -- gallop left, until
1262 * a[hint - ofs] <= key < a[hint - lastofs]
1264 const Py_ssize_t maxofs
= hint
+ 1; /* &a[0] is lowest */
1265 while (ofs
< maxofs
) {
1266 IFLT(key
, *(a
-ofs
)) {
1268 ofs
= (ofs
<< 1) + 1;
1269 if (ofs
<= 0) /* int overflow */
1272 else /* a[hint - ofs] <= key */
1277 /* Translate back to positive offsets relative to &a[0]. */
1279 lastofs
= hint
- ofs
;
1283 /* a[hint] <= key -- gallop right, until
1284 * a[hint + lastofs] <= key < a[hint + ofs]
1286 const Py_ssize_t maxofs
= n
- hint
; /* &a[n-1] is highest */
1287 while (ofs
< maxofs
) {
1290 /* a[hint + ofs] <= key */
1292 ofs
= (ofs
<< 1) + 1;
1293 if (ofs
<= 0) /* int overflow */
1298 /* Translate back to offsets relative to &a[0]. */
1304 assert(-1 <= lastofs
&& lastofs
< ofs
&& ofs
<= n
);
1305 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1306 * right of lastofs but no farther right than ofs. Do a binary
1307 * search, with invariant a[lastofs-1] <= key < a[ofs].
1310 while (lastofs
< ofs
) {
1311 Py_ssize_t m
= lastofs
+ ((ofs
- lastofs
) >> 1);
1314 ofs
= m
; /* key < a[m] */
1316 lastofs
= m
+1; /* a[m] <= key */
1318 assert(lastofs
== ofs
); /* so a[ofs-1] <= key < a[ofs] */
1325 /* The maximum number of entries in a MergeState's pending-runs stack.
1326 * This is enough to sort arrays of size up to about
1327 * 32 * phi ** MAX_MERGE_PENDING
1328 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1329 * with 2**64 elements.
1331 #define MAX_MERGE_PENDING 85
1333 /* When we get into galloping mode, we stay there until both runs win less
1334 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1336 #define MIN_GALLOP 7
1338 /* Avoid malloc for small temp arrays. */
1339 #define MERGESTATE_TEMP_SIZE 256
1341 /* One MergeState exists on the stack per invocation of mergesort. It's just
1342 * a convenient way to pass state around among the helper functions.
1349 typedef struct s_MergeState
{
1350 /* The user-supplied comparison function. or NULL if none given. */
1353 /* This controls when we get *into* galloping mode. It's initialized
1354 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1355 * random data, and lower for highly structured data.
1357 Py_ssize_t min_gallop
;
1359 /* 'a' is temp storage to help with merges. It contains room for
1362 PyObject
**a
; /* may point to temparray below */
1365 /* A stack of n pending runs yet to be merged. Run #i starts at
1366 * address base[i] and extends for len[i] elements. It's always
1367 * true (so long as the indices are in bounds) that
1369 * pending[i].base + pending[i].len == pending[i+1].base
1371 * so we could cut the storage for this, but it's a minor amount,
1372 * and keeping all the info explicit simplifies the code.
1375 struct s_slice pending
[MAX_MERGE_PENDING
];
1377 /* 'a' points to this when possible, rather than muck with malloc. */
1378 PyObject
*temparray
[MERGESTATE_TEMP_SIZE
];
1381 /* Conceptually a MergeState's constructor. */
1383 merge_init(MergeState
*ms
, PyObject
*compare
)
1386 ms
->compare
= compare
;
1387 ms
->a
= ms
->temparray
;
1388 ms
->alloced
= MERGESTATE_TEMP_SIZE
;
1390 ms
->min_gallop
= MIN_GALLOP
;
1393 /* Free all the temp memory owned by the MergeState. This must be called
1394 * when you're done with a MergeState, and may be called before then if
1395 * you want to free the temp memory early.
1398 merge_freemem(MergeState
*ms
)
1401 if (ms
->a
!= ms
->temparray
)
1403 ms
->a
= ms
->temparray
;
1404 ms
->alloced
= MERGESTATE_TEMP_SIZE
;
1407 /* Ensure enough temp memory for 'need' array slots is available.
1408 * Returns 0 on success and -1 if the memory can't be gotten.
1411 merge_getmem(MergeState
*ms
, Py_ssize_t need
)
1414 if (need
<= ms
->alloced
)
1416 /* Don't realloc! That can cost cycles to copy the old data, but
1417 * we don't care what's in the block.
1420 if (need
> PY_SSIZE_T_MAX
/ sizeof(PyObject
*)) {
1424 ms
->a
= (PyObject
**)PyMem_Malloc(need
* sizeof(PyObject
*));
1430 merge_freemem(ms
); /* reset to sane state */
1433 #define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1434 merge_getmem(MS, NEED))
1436 /* Merge the na elements starting at pa with the nb elements starting at pb
1437 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
1438 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
1439 * merge, and should have na <= nb. See listsort.txt for more info.
1440 * Return 0 if successful, -1 if error.
1443 merge_lo(MergeState
*ms
, PyObject
**pa
, Py_ssize_t na
,
1444 PyObject
**pb
, Py_ssize_t nb
)
1449 int result
= -1; /* guilty until proved innocent */
1450 Py_ssize_t min_gallop
;
1452 assert(ms
&& pa
&& pb
&& na
> 0 && nb
> 0 && pa
+ na
== pb
);
1453 if (MERGE_GETMEM(ms
, na
) < 0)
1455 memcpy(ms
->a
, pa
, na
* sizeof(PyObject
*));
1466 min_gallop
= ms
->min_gallop
;
1467 compare
= ms
->compare
;
1469 Py_ssize_t acount
= 0; /* # of times A won in a row */
1470 Py_ssize_t bcount
= 0; /* # of times B won in a row */
1472 /* Do the straightforward thing until (if ever) one run
1473 * appears to win consistently.
1476 assert(na
> 1 && nb
> 0);
1477 k
= ISLT(*pb
, *pa
, compare
);
1487 if (bcount
>= min_gallop
)
1497 if (acount
>= min_gallop
)
1502 /* One run is winning so consistently that galloping may
1503 * be a huge win. So try that, and continue galloping until
1504 * (if ever) neither run appears to be winning consistently
1509 assert(na
> 1 && nb
> 0);
1510 min_gallop
-= min_gallop
> 1;
1511 ms
->min_gallop
= min_gallop
;
1512 k
= gallop_right(*pb
, pa
, na
, 0, compare
);
1517 memcpy(dest
, pa
, k
* sizeof(PyObject
*));
1523 /* na==0 is impossible now if the comparison
1524 * function is consistent, but we can't assume
1535 k
= gallop_left(*pa
, pb
, nb
, 0, compare
);
1540 memmove(dest
, pb
, k
* sizeof(PyObject
*));
1551 } while (acount
>= MIN_GALLOP
|| bcount
>= MIN_GALLOP
);
1552 ++min_gallop
; /* penalize it for leaving galloping mode */
1553 ms
->min_gallop
= min_gallop
;
1559 memcpy(dest
, pa
, na
* sizeof(PyObject
*));
1562 assert(na
== 1 && nb
> 0);
1563 /* The last element of pa belongs at the end of the merge. */
1564 memmove(dest
, pb
, nb
* sizeof(PyObject
*));
1569 /* Merge the na elements starting at pa with the nb elements starting at pb
1570 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
1571 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
1572 * merge, and should have na >= nb. See listsort.txt for more info.
1573 * Return 0 if successful, -1 if error.
1576 merge_hi(MergeState
*ms
, PyObject
**pa
, Py_ssize_t na
, PyObject
**pb
, Py_ssize_t nb
)
1581 int result
= -1; /* guilty until proved innocent */
1584 Py_ssize_t min_gallop
;
1586 assert(ms
&& pa
&& pb
&& na
> 0 && nb
> 0 && pa
+ na
== pb
);
1587 if (MERGE_GETMEM(ms
, nb
) < 0)
1590 memcpy(ms
->a
, pb
, nb
* sizeof(PyObject
*));
1593 pb
= ms
->a
+ nb
- 1;
1603 min_gallop
= ms
->min_gallop
;
1604 compare
= ms
->compare
;
1606 Py_ssize_t acount
= 0; /* # of times A won in a row */
1607 Py_ssize_t bcount
= 0; /* # of times B won in a row */
1609 /* Do the straightforward thing until (if ever) one run
1610 * appears to win consistently.
1613 assert(na
> 0 && nb
> 1);
1614 k
= ISLT(*pb
, *pa
, compare
);
1624 if (acount
>= min_gallop
)
1634 if (bcount
>= min_gallop
)
1639 /* One run is winning so consistently that galloping may
1640 * be a huge win. So try that, and continue galloping until
1641 * (if ever) neither run appears to be winning consistently
1646 assert(na
> 0 && nb
> 1);
1647 min_gallop
-= min_gallop
> 1;
1648 ms
->min_gallop
= min_gallop
;
1649 k
= gallop_right(*pb
, basea
, na
, na
-1, compare
);
1657 memmove(dest
+1, pa
+1, k
* sizeof(PyObject
*));
1667 k
= gallop_left(*pa
, baseb
, nb
, nb
-1, compare
);
1675 memcpy(dest
+1, pb
+1, k
* sizeof(PyObject
*));
1679 /* nb==0 is impossible now if the comparison
1680 * function is consistent, but we can't assume
1690 } while (acount
>= MIN_GALLOP
|| bcount
>= MIN_GALLOP
);
1691 ++min_gallop
; /* penalize it for leaving galloping mode */
1692 ms
->min_gallop
= min_gallop
;
1698 memcpy(dest
-(nb
-1), baseb
, nb
* sizeof(PyObject
*));
1701 assert(nb
== 1 && na
> 0);
1702 /* The first element of pb belongs at the front of the merge. */
1705 memmove(dest
+1, pa
+1, na
* sizeof(PyObject
*));
1710 /* Merge the two runs at stack indices i and i+1.
1711 * Returns 0 on success, -1 on error.
1714 merge_at(MergeState
*ms
, Py_ssize_t i
)
1716 PyObject
**pa
, **pb
;
1724 assert(i
== ms
->n
- 2 || i
== ms
->n
- 3);
1726 pa
= ms
->pending
[i
].base
;
1727 na
= ms
->pending
[i
].len
;
1728 pb
= ms
->pending
[i
+1].base
;
1729 nb
= ms
->pending
[i
+1].len
;
1730 assert(na
> 0 && nb
> 0);
1731 assert(pa
+ na
== pb
);
1733 /* Record the length of the combined runs; if i is the 3rd-last
1734 * run now, also slide over the last run (which isn't involved
1735 * in this merge). The current run i+1 goes away in any case.
1737 ms
->pending
[i
].len
= na
+ nb
;
1739 ms
->pending
[i
+1] = ms
->pending
[i
+2];
1742 /* Where does b start in a? Elements in a before that can be
1743 * ignored (already in place).
1745 compare
= ms
->compare
;
1746 k
= gallop_right(*pb
, pa
, na
, 0, compare
);
1754 /* Where does a end in b? Elements in b after that can be
1755 * ignored (already in place).
1757 nb
= gallop_left(pa
[na
-1], pb
, nb
, nb
-1, compare
);
1761 /* Merge what remains of the runs, using a temp array with
1762 * min(na, nb) elements.
1765 return merge_lo(ms
, pa
, na
, pb
, nb
);
1767 return merge_hi(ms
, pa
, na
, pb
, nb
);
1770 /* Examine the stack of runs waiting to be merged, merging adjacent runs
1771 * until the stack invariants are re-established:
1773 * 1. len[-3] > len[-2] + len[-1]
1774 * 2. len[-2] > len[-1]
1776 * See listsort.txt for more info.
1778 * Returns 0 on success, -1 on error.
1781 merge_collapse(MergeState
*ms
)
1783 struct s_slice
*p
= ms
->pending
;
1787 Py_ssize_t n
= ms
->n
- 2;
1788 if (n
> 0 && p
[n
-1].len
<= p
[n
].len
+ p
[n
+1].len
) {
1789 if (p
[n
-1].len
< p
[n
+1].len
)
1791 if (merge_at(ms
, n
) < 0)
1794 else if (p
[n
].len
<= p
[n
+1].len
) {
1795 if (merge_at(ms
, n
) < 0)
1804 /* Regardless of invariants, merge all runs on the stack until only one
1805 * remains. This is used at the end of the mergesort.
1807 * Returns 0 on success, -1 on error.
1810 merge_force_collapse(MergeState
*ms
)
1812 struct s_slice
*p
= ms
->pending
;
1816 Py_ssize_t n
= ms
->n
- 2;
1817 if (n
> 0 && p
[n
-1].len
< p
[n
+1].len
)
1819 if (merge_at(ms
, n
) < 0)
1825 /* Compute a good value for the minimum run length; natural runs shorter
1826 * than this are boosted artificially via binary insertion.
1828 * If n < 64, return n (it's too small to bother with fancy stuff).
1829 * Else if n is an exact power of 2, return 32.
1830 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1831 * strictly less than, an exact power of 2.
1833 * See listsort.txt for more info.
1836 merge_compute_minrun(Py_ssize_t n
)
1838 Py_ssize_t r
= 0; /* becomes 1 if any 1 bits are shifted off */
1848 /* Special wrapper to support stable sorting using the decorate-sort-undecorate
1849 pattern. Holds a key which is used for comparisons and the original record
1850 which is returned during the undecorate phase. By exposing only the key
1851 during comparisons, the underlying sort stability characteristics are left
1852 unchanged. Also, if a custom comparison function is used, it will only see
1853 the key instead of a full record. */
1859 } sortwrapperobject
;
1861 PyDoc_STRVAR(sortwrapper_doc
, "Object wrapper with a custom sort key.");
1863 sortwrapper_richcompare(sortwrapperobject
*, sortwrapperobject
*, int);
1865 sortwrapper_dealloc(sortwrapperobject
*);
1867 static PyTypeObject sortwrapper_type
= {
1868 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
1869 "sortwrapper", /* tp_name */
1870 sizeof(sortwrapperobject
), /* tp_basicsize */
1871 0, /* tp_itemsize */
1873 (destructor
)sortwrapper_dealloc
, /* tp_dealloc */
1879 0, /* tp_as_number */
1880 0, /* tp_as_sequence */
1881 0, /* tp_as_mapping */
1885 PyObject_GenericGetAttr
, /* tp_getattro */
1886 0, /* tp_setattro */
1887 0, /* tp_as_buffer */
1888 Py_TPFLAGS_DEFAULT
|
1889 Py_TPFLAGS_HAVE_RICHCOMPARE
, /* tp_flags */
1890 sortwrapper_doc
, /* tp_doc */
1891 0, /* tp_traverse */
1893 (richcmpfunc
)sortwrapper_richcompare
, /* tp_richcompare */
1898 sortwrapper_richcompare(sortwrapperobject
*a
, sortwrapperobject
*b
, int op
)
1900 if (!PyObject_TypeCheck(b
, &sortwrapper_type
)) {
1901 PyErr_SetString(PyExc_TypeError
,
1902 "expected a sortwrapperobject");
1905 return PyObject_RichCompare(a
->key
, b
->key
, op
);
1909 sortwrapper_dealloc(sortwrapperobject
*so
)
1911 Py_XDECREF(so
->key
);
1912 Py_XDECREF(so
->value
);
1916 /* Returns a new reference to a sortwrapper.
1917 Consumes the references to the two underlying objects. */
1920 build_sortwrapper(PyObject
*key
, PyObject
*value
)
1922 sortwrapperobject
*so
;
1924 so
= PyObject_New(sortwrapperobject
, &sortwrapper_type
);
1929 return (PyObject
*)so
;
1932 /* Returns a new reference to the value underlying the wrapper. */
1934 sortwrapper_getvalue(PyObject
*so
)
1938 if (!PyObject_TypeCheck(so
, &sortwrapper_type
)) {
1939 PyErr_SetString(PyExc_TypeError
,
1940 "expected a sortwrapperobject");
1943 value
= ((sortwrapperobject
*)so
)->value
;
1948 /* Wrapper for user specified cmp functions in combination with a
1949 specified key function. Makes sure the cmp function is presented
1950 with the actual key instead of the sortwrapper */
1958 cmpwrapper_dealloc(cmpwrapperobject
*co
)
1960 Py_XDECREF(co
->func
);
1965 cmpwrapper_call(cmpwrapperobject
*co
, PyObject
*args
, PyObject
*kwds
)
1967 PyObject
*x
, *y
, *xx
, *yy
;
1969 if (!PyArg_UnpackTuple(args
, "", 2, 2, &x
, &y
))
1971 if (!PyObject_TypeCheck(x
, &sortwrapper_type
) ||
1972 !PyObject_TypeCheck(y
, &sortwrapper_type
)) {
1973 PyErr_SetString(PyExc_TypeError
,
1974 "expected a sortwrapperobject");
1977 xx
= ((sortwrapperobject
*)x
)->key
;
1978 yy
= ((sortwrapperobject
*)y
)->key
;
1979 return PyObject_CallFunctionObjArgs(co
->func
, xx
, yy
, NULL
);
1982 PyDoc_STRVAR(cmpwrapper_doc
, "cmp() wrapper for sort with custom keys.");
1984 static PyTypeObject cmpwrapper_type
= {
1985 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
1986 "cmpwrapper", /* tp_name */
1987 sizeof(cmpwrapperobject
), /* tp_basicsize */
1988 0, /* tp_itemsize */
1990 (destructor
)cmpwrapper_dealloc
, /* tp_dealloc */
1996 0, /* tp_as_number */
1997 0, /* tp_as_sequence */
1998 0, /* tp_as_mapping */
2000 (ternaryfunc
)cmpwrapper_call
, /* tp_call */
2002 PyObject_GenericGetAttr
, /* tp_getattro */
2003 0, /* tp_setattro */
2004 0, /* tp_as_buffer */
2005 Py_TPFLAGS_DEFAULT
, /* tp_flags */
2006 cmpwrapper_doc
, /* tp_doc */
2010 build_cmpwrapper(PyObject
*cmpfunc
)
2012 cmpwrapperobject
*co
;
2014 co
= PyObject_New(cmpwrapperobject
, &cmpwrapper_type
);
2019 return (PyObject
*)co
;
2022 /* An adaptive, stable, natural mergesort. See listsort.txt.
2023 * Returns Py_None on success, NULL on error. Even in case of error, the
2024 * list will be some permutation of its input state (nothing is lost or
2028 listsort(PyListObject
*self
, PyObject
*args
, PyObject
*kwds
)
2031 PyObject
**lo
, **hi
;
2032 Py_ssize_t nremaining
;
2034 Py_ssize_t saved_ob_size
, saved_allocated
;
2035 PyObject
**saved_ob_item
;
2036 PyObject
**final_ob_item
;
2037 PyObject
*compare
= NULL
;
2038 PyObject
*result
= NULL
; /* guilty until proved innocent */
2040 PyObject
*keyfunc
= NULL
;
2042 PyObject
*key
, *value
, *kvpair
;
2043 static char *kwlist
[] = {"cmp", "key", "reverse", 0};
2045 assert(self
!= NULL
);
2046 assert (PyList_Check(self
));
2048 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "|OOi:sort",
2049 kwlist
, &compare
, &keyfunc
, &reverse
))
2052 if (compare
== Py_None
)
2054 if (compare
!= NULL
&&
2055 PyErr_WarnPy3k("the cmp argument is not supported in 3.x", 1) < 0)
2057 if (keyfunc
== Py_None
)
2059 if (compare
!= NULL
&& keyfunc
!= NULL
) {
2060 compare
= build_cmpwrapper(compare
);
2061 if (compare
== NULL
)
2064 Py_XINCREF(compare
);
2066 /* The list is temporarily made empty, so that mutations performed
2067 * by comparison functions can't affect the slice of memory we're
2068 * sorting (allowing mutations during sorting is a core-dump
2069 * factory, since ob_item may change).
2071 saved_ob_size
= Py_SIZE(self
);
2072 saved_ob_item
= self
->ob_item
;
2073 saved_allocated
= self
->allocated
;
2075 self
->ob_item
= NULL
;
2076 self
->allocated
= -1; /* any operation will reset it to >= 0 */
2078 if (keyfunc
!= NULL
) {
2079 for (i
=0 ; i
< saved_ob_size
; i
++) {
2080 value
= saved_ob_item
[i
];
2081 key
= PyObject_CallFunctionObjArgs(keyfunc
, value
,
2084 for (i
=i
-1 ; i
>=0 ; i
--) {
2085 kvpair
= saved_ob_item
[i
];
2086 value
= sortwrapper_getvalue(kvpair
);
2087 saved_ob_item
[i
] = value
;
2092 kvpair
= build_sortwrapper(key
, value
);
2095 saved_ob_item
[i
] = kvpair
;
2099 /* Reverse sort stability achieved by initially reversing the list,
2100 applying a stable forward sort, then reversing the final result. */
2101 if (reverse
&& saved_ob_size
> 1)
2102 reverse_slice(saved_ob_item
, saved_ob_item
+ saved_ob_size
);
2104 merge_init(&ms
, compare
);
2106 nremaining
= saved_ob_size
;
2110 /* March over the array once, left to right, finding natural runs,
2111 * and extending short natural runs to minrun elements.
2114 hi
= lo
+ nremaining
;
2115 minrun
= merge_compute_minrun(nremaining
);
2120 /* Identify next run. */
2121 n
= count_run(lo
, hi
, compare
, &descending
);
2125 reverse_slice(lo
, lo
+ n
);
2126 /* If short, extend to min(minrun, nremaining). */
2128 const Py_ssize_t force
= nremaining
<= minrun
?
2129 nremaining
: minrun
;
2130 if (binarysort(lo
, lo
+ force
, lo
+ n
, compare
) < 0)
2134 /* Push run onto pending-runs stack, and maybe merge. */
2135 assert(ms
.n
< MAX_MERGE_PENDING
);
2136 ms
.pending
[ms
.n
].base
= lo
;
2137 ms
.pending
[ms
.n
].len
= n
;
2139 if (merge_collapse(&ms
) < 0)
2141 /* Advance to find next run. */
2144 } while (nremaining
);
2147 if (merge_force_collapse(&ms
) < 0)
2150 assert(ms
.pending
[0].base
== saved_ob_item
);
2151 assert(ms
.pending
[0].len
== saved_ob_size
);
2156 if (keyfunc
!= NULL
) {
2157 for (i
=0 ; i
< saved_ob_size
; i
++) {
2158 kvpair
= saved_ob_item
[i
];
2159 value
= sortwrapper_getvalue(kvpair
);
2160 saved_ob_item
[i
] = value
;
2165 if (self
->allocated
!= -1 && result
!= NULL
) {
2166 /* The user mucked with the list during the sort,
2167 * and we don't already have another error to report.
2169 PyErr_SetString(PyExc_ValueError
, "list modified during sort");
2173 if (reverse
&& saved_ob_size
> 1)
2174 reverse_slice(saved_ob_item
, saved_ob_item
+ saved_ob_size
);
2179 final_ob_item
= self
->ob_item
;
2181 Py_SIZE(self
) = saved_ob_size
;
2182 self
->ob_item
= saved_ob_item
;
2183 self
->allocated
= saved_allocated
;
2184 if (final_ob_item
!= NULL
) {
2185 /* we cannot use list_clear() for this because it does not
2186 guarantee that the list is really empty when it returns */
2188 Py_XDECREF(final_ob_item
[i
]);
2190 PyMem_FREE(final_ob_item
);
2192 Py_XDECREF(compare
);
2200 PyList_Sort(PyObject
*v
)
2202 if (v
== NULL
|| !PyList_Check(v
)) {
2203 PyErr_BadInternalCall();
2206 v
= listsort((PyListObject
*)v
, (PyObject
*)NULL
, (PyObject
*)NULL
);
2214 listreverse(PyListObject
*self
)
2216 if (Py_SIZE(self
) > 1)
2217 reverse_slice(self
->ob_item
, self
->ob_item
+ Py_SIZE(self
));
2222 PyList_Reverse(PyObject
*v
)
2224 PyListObject
*self
= (PyListObject
*)v
;
2226 if (v
== NULL
|| !PyList_Check(v
)) {
2227 PyErr_BadInternalCall();
2230 if (Py_SIZE(self
) > 1)
2231 reverse_slice(self
->ob_item
, self
->ob_item
+ Py_SIZE(self
));
2236 PyList_AsTuple(PyObject
*v
)
2241 if (v
== NULL
|| !PyList_Check(v
)) {
2242 PyErr_BadInternalCall();
2249 p
= ((PyTupleObject
*)w
)->ob_item
;
2250 q
= ((PyListObject
*)v
)->ob_item
;
2261 listindex(PyListObject
*self
, PyObject
*args
)
2263 Py_ssize_t i
, start
=0, stop
=Py_SIZE(self
);
2266 if (!PyArg_ParseTuple(args
, "O|O&O&:index", &v
,
2267 _PyEval_SliceIndex
, &start
,
2268 _PyEval_SliceIndex
, &stop
))
2271 start
+= Py_SIZE(self
);
2276 stop
+= Py_SIZE(self
);
2280 for (i
= start
; i
< stop
&& i
< Py_SIZE(self
); i
++) {
2281 int cmp
= PyObject_RichCompareBool(self
->ob_item
[i
], v
, Py_EQ
);
2283 return PyInt_FromSsize_t(i
);
2287 PyErr_SetString(PyExc_ValueError
, "list.index(x): x not in list");
2292 listcount(PyListObject
*self
, PyObject
*v
)
2294 Py_ssize_t count
= 0;
2297 for (i
= 0; i
< Py_SIZE(self
); i
++) {
2298 int cmp
= PyObject_RichCompareBool(self
->ob_item
[i
], v
, Py_EQ
);
2304 return PyInt_FromSsize_t(count
);
2308 listremove(PyListObject
*self
, PyObject
*v
)
2312 for (i
= 0; i
< Py_SIZE(self
); i
++) {
2313 int cmp
= PyObject_RichCompareBool(self
->ob_item
[i
], v
, Py_EQ
);
2315 if (list_ass_slice(self
, i
, i
+1,
2316 (PyObject
*)NULL
) == 0)
2323 PyErr_SetString(PyExc_ValueError
, "list.remove(x): x not in list");
2328 list_traverse(PyListObject
*o
, visitproc visit
, void *arg
)
2332 for (i
= Py_SIZE(o
); --i
>= 0; )
2333 Py_VISIT(o
->ob_item
[i
]);
2338 list_richcompare(PyObject
*v
, PyObject
*w
, int op
)
2340 PyListObject
*vl
, *wl
;
2343 if (!PyList_Check(v
) || !PyList_Check(w
)) {
2344 Py_INCREF(Py_NotImplemented
);
2345 return Py_NotImplemented
;
2348 vl
= (PyListObject
*)v
;
2349 wl
= (PyListObject
*)w
;
2351 if (Py_SIZE(vl
) != Py_SIZE(wl
) && (op
== Py_EQ
|| op
== Py_NE
)) {
2352 /* Shortcut: if the lengths differ, the lists differ */
2362 /* Search for the first index where items are different */
2363 for (i
= 0; i
< Py_SIZE(vl
) && i
< Py_SIZE(wl
); i
++) {
2364 int k
= PyObject_RichCompareBool(vl
->ob_item
[i
],
2365 wl
->ob_item
[i
], Py_EQ
);
2372 if (i
>= Py_SIZE(vl
) || i
>= Py_SIZE(wl
)) {
2373 /* No more items to compare -- compare sizes */
2374 Py_ssize_t vs
= Py_SIZE(vl
);
2375 Py_ssize_t ws
= Py_SIZE(wl
);
2379 case Py_LT
: cmp
= vs
< ws
; break;
2380 case Py_LE
: cmp
= vs
<= ws
; break;
2381 case Py_EQ
: cmp
= vs
== ws
; break;
2382 case Py_NE
: cmp
= vs
!= ws
; break;
2383 case Py_GT
: cmp
= vs
> ws
; break;
2384 case Py_GE
: cmp
= vs
>= ws
; break;
2385 default: return NULL
; /* cannot happen */
2395 /* We have an item that differs -- shortcuts for EQ/NE */
2397 Py_INCREF(Py_False
);
2405 /* Compare the final item again using the proper operator */
2406 return PyObject_RichCompare(vl
->ob_item
[i
], wl
->ob_item
[i
], op
);
2410 list_init(PyListObject
*self
, PyObject
*args
, PyObject
*kw
)
2412 PyObject
*arg
= NULL
;
2413 static char *kwlist
[] = {"sequence", 0};
2415 if (!PyArg_ParseTupleAndKeywords(args
, kw
, "|O:list", kwlist
, &arg
))
2418 /* Verify list invariants established by PyType_GenericAlloc() */
2419 assert(0 <= Py_SIZE(self
));
2420 assert(Py_SIZE(self
) <= self
->allocated
|| self
->allocated
== -1);
2421 assert(self
->ob_item
!= NULL
||
2422 self
->allocated
== 0 || self
->allocated
== -1);
2424 /* Empty previous contents */
2425 if (self
->ob_item
!= NULL
) {
2426 (void)list_clear(self
);
2429 PyObject
*rv
= listextend(self
, arg
);
2438 list_sizeof(PyListObject
*self
)
2442 res
= sizeof(PyListObject
) + self
->allocated
* sizeof(void*);
2443 return PyInt_FromSsize_t(res
);
2446 static PyObject
*list_iter(PyObject
*seq
);
2447 static PyObject
*list_reversed(PyListObject
* seq
, PyObject
* unused
);
2449 PyDoc_STRVAR(getitem_doc
,
2450 "x.__getitem__(y) <==> x[y]");
2451 PyDoc_STRVAR(reversed_doc
,
2452 "L.__reversed__() -- return a reverse iterator over the list");
2453 PyDoc_STRVAR(sizeof_doc
,
2454 "L.__sizeof__() -- size of L in memory, in bytes");
2455 PyDoc_STRVAR(append_doc
,
2456 "L.append(object) -- append object to end");
2457 PyDoc_STRVAR(extend_doc
,
2458 "L.extend(iterable) -- extend list by appending elements from the iterable");
2459 PyDoc_STRVAR(insert_doc
,
2460 "L.insert(index, object) -- insert object before index");
2461 PyDoc_STRVAR(pop_doc
,
2462 "L.pop([index]) -> item -- remove and return item at index (default last).\n"
2463 "Raises IndexError if list is empty or index is out of range.");
2464 PyDoc_STRVAR(remove_doc
,
2465 "L.remove(value) -- remove first occurrence of value.\n"
2466 "Raises ValueError if the value is not present.");
2467 PyDoc_STRVAR(index_doc
,
2468 "L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2469 "Raises ValueError if the value is not present.");
2470 PyDoc_STRVAR(count_doc
,
2471 "L.count(value) -> integer -- return number of occurrences of value");
2472 PyDoc_STRVAR(reverse_doc
,
2473 "L.reverse() -- reverse *IN PLACE*");
2474 PyDoc_STRVAR(sort_doc
,
2475 "L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\n\
2476 cmp(x, y) -> -1, 0, 1");
2478 static PyObject
*list_subscript(PyListObject
*, PyObject
*);
2480 static PyMethodDef list_methods
[] = {
2481 {"__getitem__", (PyCFunction
)list_subscript
, METH_O
|METH_COEXIST
, getitem_doc
},
2482 {"__reversed__",(PyCFunction
)list_reversed
, METH_NOARGS
, reversed_doc
},
2483 {"__sizeof__", (PyCFunction
)list_sizeof
, METH_NOARGS
, sizeof_doc
},
2484 {"append", (PyCFunction
)listappend
, METH_O
, append_doc
},
2485 {"insert", (PyCFunction
)listinsert
, METH_VARARGS
, insert_doc
},
2486 {"extend", (PyCFunction
)listextend
, METH_O
, extend_doc
},
2487 {"pop", (PyCFunction
)listpop
, METH_VARARGS
, pop_doc
},
2488 {"remove", (PyCFunction
)listremove
, METH_O
, remove_doc
},
2489 {"index", (PyCFunction
)listindex
, METH_VARARGS
, index_doc
},
2490 {"count", (PyCFunction
)listcount
, METH_O
, count_doc
},
2491 {"reverse", (PyCFunction
)listreverse
, METH_NOARGS
, reverse_doc
},
2492 {"sort", (PyCFunction
)listsort
, METH_VARARGS
| METH_KEYWORDS
, sort_doc
},
2493 {NULL
, NULL
} /* sentinel */
2496 static PySequenceMethods list_as_sequence
= {
2497 (lenfunc
)list_length
, /* sq_length */
2498 (binaryfunc
)list_concat
, /* sq_concat */
2499 (ssizeargfunc
)list_repeat
, /* sq_repeat */
2500 (ssizeargfunc
)list_item
, /* sq_item */
2501 (ssizessizeargfunc
)list_slice
, /* sq_slice */
2502 (ssizeobjargproc
)list_ass_item
, /* sq_ass_item */
2503 (ssizessizeobjargproc
)list_ass_slice
, /* sq_ass_slice */
2504 (objobjproc
)list_contains
, /* sq_contains */
2505 (binaryfunc
)list_inplace_concat
, /* sq_inplace_concat */
2506 (ssizeargfunc
)list_inplace_repeat
, /* sq_inplace_repeat */
2509 PyDoc_STRVAR(list_doc
,
2510 "list() -> new list\n"
2511 "list(sequence) -> new list initialized from sequence's items");
2515 list_subscript(PyListObject
* self
, PyObject
* item
)
2517 if (PyIndex_Check(item
)) {
2519 i
= PyNumber_AsSsize_t(item
, PyExc_IndexError
);
2520 if (i
== -1 && PyErr_Occurred())
2523 i
+= PyList_GET_SIZE(self
);
2524 return list_item(self
, i
);
2526 else if (PySlice_Check(item
)) {
2527 Py_ssize_t start
, stop
, step
, slicelength
, cur
, i
;
2530 PyObject
**src
, **dest
;
2532 if (PySlice_GetIndicesEx((PySliceObject
*)item
, Py_SIZE(self
),
2533 &start
, &stop
, &step
, &slicelength
) < 0) {
2537 if (slicelength
<= 0) {
2538 return PyList_New(0);
2540 else if (step
== 1) {
2541 return list_slice(self
, start
, stop
);
2544 result
= PyList_New(slicelength
);
2545 if (!result
) return NULL
;
2547 src
= self
->ob_item
;
2548 dest
= ((PyListObject
*)result
)->ob_item
;
2549 for (cur
= start
, i
= 0; i
< slicelength
;
2560 PyErr_Format(PyExc_TypeError
,
2561 "list indices must be integers, not %.200s",
2562 item
->ob_type
->tp_name
);
2568 list_ass_subscript(PyListObject
* self
, PyObject
* item
, PyObject
* value
)
2570 if (PyIndex_Check(item
)) {
2571 Py_ssize_t i
= PyNumber_AsSsize_t(item
, PyExc_IndexError
);
2572 if (i
== -1 && PyErr_Occurred())
2575 i
+= PyList_GET_SIZE(self
);
2576 return list_ass_item(self
, i
, value
);
2578 else if (PySlice_Check(item
)) {
2579 Py_ssize_t start
, stop
, step
, slicelength
;
2581 if (PySlice_GetIndicesEx((PySliceObject
*)item
, Py_SIZE(self
),
2582 &start
, &stop
, &step
, &slicelength
) < 0) {
2587 return list_ass_slice(self
, start
, stop
, value
);
2589 /* Make sure s[5:2] = [..] inserts at the right place:
2590 before 5, not before 2. */
2591 if ((step
< 0 && start
< stop
) ||
2592 (step
> 0 && start
> stop
))
2595 if (value
== NULL
) {
2600 if (slicelength
<= 0)
2605 start
= stop
+ step
*(slicelength
- 1) - 1;
2609 assert(slicelength
<= PY_SIZE_MAX
/ sizeof(PyObject
*));
2611 garbage
= (PyObject
**)
2612 PyMem_MALLOC(slicelength
*sizeof(PyObject
*));
2618 /* drawing pictures might help understand these for
2619 loops. Basically, we memmove the parts of the
2620 list that are *not* part of the slice: step-1
2621 items for each item that is part of the slice,
2622 and then tail end of the list that was not
2623 covered by the slice */
2624 for (cur
= start
, i
= 0;
2627 Py_ssize_t lim
= step
- 1;
2629 garbage
[i
] = PyList_GET_ITEM(self
, cur
);
2631 if (cur
+ step
>= Py_SIZE(self
)) {
2632 lim
= Py_SIZE(self
) - cur
- 1;
2635 memmove(self
->ob_item
+ cur
- i
,
2636 self
->ob_item
+ cur
+ 1,
2637 lim
* sizeof(PyObject
*));
2639 cur
= start
+ slicelength
*step
;
2640 if (cur
< Py_SIZE(self
)) {
2641 memmove(self
->ob_item
+ cur
- slicelength
,
2642 self
->ob_item
+ cur
,
2643 (Py_SIZE(self
) - cur
) *
2644 sizeof(PyObject
*));
2647 Py_SIZE(self
) -= slicelength
;
2648 list_resize(self
, Py_SIZE(self
));
2650 for (i
= 0; i
< slicelength
; i
++) {
2651 Py_DECREF(garbage
[i
]);
2653 PyMem_FREE(garbage
);
2659 PyObject
*ins
, *seq
;
2660 PyObject
**garbage
, **seqitems
, **selfitems
;
2663 /* protect against a[::-1] = a */
2664 if (self
== (PyListObject
*)value
) {
2665 seq
= list_slice((PyListObject
*)value
, 0,
2666 PyList_GET_SIZE(value
));
2669 seq
= PySequence_Fast(value
,
2670 "must assign iterable "
2671 "to extended slice");
2676 if (PySequence_Fast_GET_SIZE(seq
) != slicelength
) {
2677 PyErr_Format(PyExc_ValueError
,
2678 "attempt to assign sequence of "
2679 "size %zd to extended slice of "
2681 PySequence_Fast_GET_SIZE(seq
),
2692 garbage
= (PyObject
**)
2693 PyMem_MALLOC(slicelength
*sizeof(PyObject
*));
2700 selfitems
= self
->ob_item
;
2701 seqitems
= PySequence_Fast_ITEMS(seq
);
2702 for (cur
= start
, i
= 0; i
< slicelength
;
2704 garbage
[i
] = selfitems
[cur
];
2707 selfitems
[cur
] = ins
;
2710 for (i
= 0; i
< slicelength
; i
++) {
2711 Py_DECREF(garbage
[i
]);
2714 PyMem_FREE(garbage
);
2721 PyErr_Format(PyExc_TypeError
,
2722 "list indices must be integers, not %.200s",
2723 item
->ob_type
->tp_name
);
2728 static PyMappingMethods list_as_mapping
= {
2729 (lenfunc
)list_length
,
2730 (binaryfunc
)list_subscript
,
2731 (objobjargproc
)list_ass_subscript
2734 PyTypeObject PyList_Type
= {
2735 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
2737 sizeof(PyListObject
),
2739 (destructor
)list_dealloc
, /* tp_dealloc */
2740 (printfunc
)list_print
, /* tp_print */
2744 (reprfunc
)list_repr
, /* tp_repr */
2745 0, /* tp_as_number */
2746 &list_as_sequence
, /* tp_as_sequence */
2747 &list_as_mapping
, /* tp_as_mapping */
2748 (hashfunc
)PyObject_HashNotImplemented
, /* tp_hash */
2751 PyObject_GenericGetAttr
, /* tp_getattro */
2752 0, /* tp_setattro */
2753 0, /* tp_as_buffer */
2754 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
2755 Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_LIST_SUBCLASS
, /* tp_flags */
2756 list_doc
, /* tp_doc */
2757 (traverseproc
)list_traverse
, /* tp_traverse */
2758 (inquiry
)list_clear
, /* tp_clear */
2759 list_richcompare
, /* tp_richcompare */
2760 0, /* tp_weaklistoffset */
2761 list_iter
, /* tp_iter */
2762 0, /* tp_iternext */
2763 list_methods
, /* tp_methods */
2768 0, /* tp_descr_get */
2769 0, /* tp_descr_set */
2770 0, /* tp_dictoffset */
2771 (initproc
)list_init
, /* tp_init */
2772 PyType_GenericAlloc
, /* tp_alloc */
2773 PyType_GenericNew
, /* tp_new */
2774 PyObject_GC_Del
, /* tp_free */
2778 /*********************** List Iterator **************************/
2783 PyListObject
*it_seq
; /* Set to NULL when iterator is exhausted */
2786 static PyObject
*list_iter(PyObject
*);
2787 static void listiter_dealloc(listiterobject
*);
2788 static int listiter_traverse(listiterobject
*, visitproc
, void *);
2789 static PyObject
*listiter_next(listiterobject
*);
2790 static PyObject
*listiter_len(listiterobject
*);
2792 PyDoc_STRVAR(length_hint_doc
, "Private method returning an estimate of len(list(it)).");
2794 static PyMethodDef listiter_methods
[] = {
2795 {"__length_hint__", (PyCFunction
)listiter_len
, METH_NOARGS
, length_hint_doc
},
2796 {NULL
, NULL
} /* sentinel */
2799 PyTypeObject PyListIter_Type
= {
2800 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
2801 "listiterator", /* tp_name */
2802 sizeof(listiterobject
), /* tp_basicsize */
2803 0, /* tp_itemsize */
2805 (destructor
)listiter_dealloc
, /* tp_dealloc */
2811 0, /* tp_as_number */
2812 0, /* tp_as_sequence */
2813 0, /* tp_as_mapping */
2817 PyObject_GenericGetAttr
, /* tp_getattro */
2818 0, /* tp_setattro */
2819 0, /* tp_as_buffer */
2820 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
2822 (traverseproc
)listiter_traverse
, /* tp_traverse */
2824 0, /* tp_richcompare */
2825 0, /* tp_weaklistoffset */
2826 PyObject_SelfIter
, /* tp_iter */
2827 (iternextfunc
)listiter_next
, /* tp_iternext */
2828 listiter_methods
, /* tp_methods */
2834 list_iter(PyObject
*seq
)
2838 if (!PyList_Check(seq
)) {
2839 PyErr_BadInternalCall();
2842 it
= PyObject_GC_New(listiterobject
, &PyListIter_Type
);
2847 it
->it_seq
= (PyListObject
*)seq
;
2848 _PyObject_GC_TRACK(it
);
2849 return (PyObject
*)it
;
2853 listiter_dealloc(listiterobject
*it
)
2855 _PyObject_GC_UNTRACK(it
);
2856 Py_XDECREF(it
->it_seq
);
2857 PyObject_GC_Del(it
);
2861 listiter_traverse(listiterobject
*it
, visitproc visit
, void *arg
)
2863 Py_VISIT(it
->it_seq
);
2868 listiter_next(listiterobject
*it
)
2877 assert(PyList_Check(seq
));
2879 if (it
->it_index
< PyList_GET_SIZE(seq
)) {
2880 item
= PyList_GET_ITEM(seq
, it
->it_index
);
2892 listiter_len(listiterobject
*it
)
2896 len
= PyList_GET_SIZE(it
->it_seq
) - it
->it_index
;
2898 return PyInt_FromSsize_t(len
);
2900 return PyInt_FromLong(0);
2902 /*********************** List Reverse Iterator **************************/
2906 Py_ssize_t it_index
;
2907 PyListObject
*it_seq
; /* Set to NULL when iterator is exhausted */
2908 } listreviterobject
;
2910 static PyObject
*list_reversed(PyListObject
*, PyObject
*);
2911 static void listreviter_dealloc(listreviterobject
*);
2912 static int listreviter_traverse(listreviterobject
*, visitproc
, void *);
2913 static PyObject
*listreviter_next(listreviterobject
*);
2914 static PyObject
*listreviter_len(listreviterobject
*);
2916 static PyMethodDef listreviter_methods
[] = {
2917 {"__length_hint__", (PyCFunction
)listreviter_len
, METH_NOARGS
, length_hint_doc
},
2918 {NULL
, NULL
} /* sentinel */
2921 PyTypeObject PyListRevIter_Type
= {
2922 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
2923 "listreverseiterator", /* tp_name */
2924 sizeof(listreviterobject
), /* tp_basicsize */
2925 0, /* tp_itemsize */
2927 (destructor
)listreviter_dealloc
, /* tp_dealloc */
2933 0, /* tp_as_number */
2934 0, /* tp_as_sequence */
2935 0, /* tp_as_mapping */
2939 PyObject_GenericGetAttr
, /* tp_getattro */
2940 0, /* tp_setattro */
2941 0, /* tp_as_buffer */
2942 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
2944 (traverseproc
)listreviter_traverse
, /* tp_traverse */
2946 0, /* tp_richcompare */
2947 0, /* tp_weaklistoffset */
2948 PyObject_SelfIter
, /* tp_iter */
2949 (iternextfunc
)listreviter_next
, /* tp_iternext */
2950 listreviter_methods
, /* tp_methods */
2955 list_reversed(PyListObject
*seq
, PyObject
*unused
)
2957 listreviterobject
*it
;
2959 it
= PyObject_GC_New(listreviterobject
, &PyListRevIter_Type
);
2962 assert(PyList_Check(seq
));
2963 it
->it_index
= PyList_GET_SIZE(seq
) - 1;
2966 PyObject_GC_Track(it
);
2967 return (PyObject
*)it
;
2971 listreviter_dealloc(listreviterobject
*it
)
2973 PyObject_GC_UnTrack(it
);
2974 Py_XDECREF(it
->it_seq
);
2975 PyObject_GC_Del(it
);
2979 listreviter_traverse(listreviterobject
*it
, visitproc visit
, void *arg
)
2981 Py_VISIT(it
->it_seq
);
2986 listreviter_next(listreviterobject
*it
)
2989 Py_ssize_t index
= it
->it_index
;
2990 PyListObject
*seq
= it
->it_seq
;
2992 if (index
>=0 && index
< PyList_GET_SIZE(seq
)) {
2993 item
= PyList_GET_ITEM(seq
, index
);
3007 listreviter_len(listreviterobject
*it
)
3009 Py_ssize_t len
= it
->it_index
+ 1;
3010 if (it
->it_seq
== NULL
|| PyList_GET_SIZE(it
->it_seq
) < len
)
3012 return PyLong_FromSsize_t(len
);