blob: 4d4399c2ed5ccac18a66a0377433c4c1b748f195 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* List object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
4
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00005#ifdef STDC_HEADERS
6#include <stddef.h>
7#else
8#include <sys/types.h> /* For size_t */
9#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010
Tim Peters8d9eb102004-07-31 02:24:20 +000011/* 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.
23 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000024static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000025list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000026{
Raymond Hettinger4bb95402004-02-13 11:36:39 +000027 PyObject **items;
Raymond Hettingera84f3ab2004-09-12 19:53:07 +000028 size_t new_allocated;
Martin v. Löwis18e16552006-02-15 17:27:45 +000029 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000030
Raymond Hettinger4bb95402004-02-13 11:36:39 +000031 /* Bypass realloc() when a previous overallocation is large enough
Raymond Hettingera84f3ab2004-09-12 19:53:07 +000032 to accommodate the newsize. If the newsize falls lower than half
33 the allocated size, then proceed with the realloc() to shrink the list.
Raymond Hettinger4bb95402004-02-13 11:36:39 +000034 */
Raymond Hettingera84f3ab2004-09-12 19:53:07 +000035 if (allocated >= newsize && newsize >= (allocated >> 1)) {
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +000036 assert(self->ob_item != NULL || newsize == 0);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000037 Py_Size(self) = newsize;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000038 return 0;
39 }
40
41 /* This over-allocates proportional to the list size, making room
Tim Peters65b8b842001-05-26 05:28:40 +000042 * 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
Raymond Hettingerab517d22004-02-14 18:34:46 +000045 * system realloc().
46 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
Tim Peters65b8b842001-05-26 05:28:40 +000047 */
Raymond Hettingera84f3ab2004-09-12 19:53:07 +000048 new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize;
49 if (newsize == 0)
50 new_allocated = 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000051 items = self->ob_item;
Raymond Hettingera84f3ab2004-09-12 19:53:07 +000052 if (new_allocated <= ((~(size_t)0) / sizeof(PyObject *)))
53 PyMem_RESIZE(items, PyObject *, new_allocated);
Raymond Hettinger4bb95402004-02-13 11:36:39 +000054 else
55 items = NULL;
56 if (items == NULL) {
57 PyErr_NoMemory();
58 return -1;
59 }
60 self->ob_item = items;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000061 Py_Size(self) = newsize;
Raymond Hettingera84f3ab2004-09-12 19:53:07 +000062 self->allocated = new_allocated;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000063 return 0;
64}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000065
Raymond Hettinger0468e412004-05-05 05:37:53 +000066/* Empty list reuse scheme to save calls to malloc and free */
67#define MAXFREELISTS 80
68static PyListObject *free_lists[MAXFREELISTS];
69static int num_free_lists = 0;
70
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +000071void
72PyList_Fini(void)
73{
74 PyListObject *op;
75
76 while (num_free_lists) {
77 num_free_lists--;
78 op = free_lists[num_free_lists];
79 assert(PyList_CheckExact(op));
80 PyObject_GC_Del(op);
81 }
82}
83
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000085PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 PyListObject *op;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000088 size_t nbytes;
Tim Peters3986d4e2004-07-29 02:28:42 +000089
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 if (size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092 return NULL;
93 }
Tim Peters7049d812004-01-18 20:31:02 +000094 nbytes = size * sizeof(PyObject *);
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000095 /* Check for overflow */
Raymond Hettingerfdfe6182004-05-05 06:28:16 +000096 if (nbytes / sizeof(PyObject *) != (size_t)size)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097 return PyErr_NoMemory();
Raymond Hettinger0468e412004-05-05 05:37:53 +000098 if (num_free_lists) {
99 num_free_lists--;
100 op = free_lists[num_free_lists];
101 _Py_NewReference((PyObject *)op);
102 } else {
103 op = PyObject_GC_New(PyListObject, &PyList_Type);
104 if (op == NULL)
105 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106 }
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000107 if (size <= 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108 op->ob_item = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000110 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000111 if (op->ob_item == NULL) {
112 Py_DECREF(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113 return PyErr_NoMemory();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000114 }
Tim Peters3986d4e2004-07-29 02:28:42 +0000115 memset(op->ob_item, 0, nbytes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000117 Py_Size(op) = size;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000118 op->allocated = size;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000119 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121}
122
Martin v. Löwis18e16552006-02-15 17:27:45 +0000123Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000124PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000126 if (!PyList_Check(op)) {
127 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128 return -1;
129 }
130 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000131 return Py_Size(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132}
133
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000134static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000135
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000136PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000137PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000139 if (!PyList_Check(op)) {
140 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141 return NULL;
142 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000143 if (i < 0 || i >= Py_Size(op)) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000144 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145 indexerr = PyString_FromString(
146 "list index out of range");
147 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148 return NULL;
149 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151}
152
153int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000154PyList_SetItem(register PyObject *op, register Py_ssize_t i,
Fred Drakea2f55112000-07-09 15:16:51 +0000155 register PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157 register PyObject *olditem;
158 register PyObject **p;
159 if (!PyList_Check(op)) {
160 Py_XDECREF(newitem);
161 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000162 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000164 if (i < 0 || i >= Py_Size(op)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 Py_XDECREF(newitem);
166 PyErr_SetString(PyExc_IndexError,
167 "list assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000168 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000170 p = ((PyListObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000171 olditem = *p;
172 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000173 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174 return 0;
175}
176
177static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000178ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000180 Py_ssize_t i, n = Py_Size(self);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000181 PyObject **items;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000182 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000184 return -1;
185 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000186 if (n == PY_SSIZE_T_MAX) {
Trent Micka5846642000-08-13 22:47:45 +0000187 PyErr_SetString(PyExc_OverflowError,
188 "cannot add more objects to list");
189 return -1;
190 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000191
Raymond Hettingerd4ff7412004-03-15 09:01:31 +0000192 if (list_resize(self, n+1) == -1)
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000193 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000194
Guido van Rossum3a3cca52003-04-14 20:58:14 +0000195 if (where < 0) {
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000196 where += n;
Guido van Rossum3a3cca52003-04-14 20:58:14 +0000197 if (where < 0)
198 where = 0;
199 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000200 if (where > n)
201 where = n;
202 items = self->ob_item;
203 for (i = n; --i >= where; )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204 items[i+1] = items[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206 items[where] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207 return 0;
208}
209
210int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000211PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000213 if (!PyList_Check(op)) {
214 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000215 return -1;
216 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000217 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218}
219
Raymond Hettinger40a03822004-04-12 13:05:09 +0000220static int
221app1(PyListObject *self, PyObject *v)
222{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000223 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000224
225 assert (v != NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000226 if (n == PY_SSIZE_T_MAX) {
Raymond Hettinger40a03822004-04-12 13:05:09 +0000227 PyErr_SetString(PyExc_OverflowError,
228 "cannot add more objects to list");
229 return -1;
230 }
231
232 if (list_resize(self, n+1) == -1)
233 return -1;
234
235 Py_INCREF(v);
236 PyList_SET_ITEM(self, n, v);
237 return 0;
238}
239
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240int
Fred Drakea2f55112000-07-09 15:16:51 +0000241PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242{
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000243 if (PyList_Check(op) && (newitem != NULL))
244 return app1((PyListObject *)op, newitem);
245 PyErr_BadInternalCall();
246 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247}
248
249/* Methods */
250
251static void
Fred Drakea2f55112000-07-09 15:16:51 +0000252list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000254 Py_ssize_t i;
Guido van Rossumff413af2002-03-28 20:34:59 +0000255 PyObject_GC_UnTrack(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000256 Py_TRASHCAN_SAFE_BEGIN(op)
Jack Jansen7874d1f1995-01-19 12:09:27 +0000257 if (op->ob_item != NULL) {
Guido van Rossumfa717011999-06-09 15:19:34 +0000258 /* Do it backwards, for Christian Tismer.
259 There's a simple test case where somehow this reduces
260 thrashing when a *very* large list is created and
261 immediately deleted. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000262 i = Py_Size(op);
Guido van Rossumfa717011999-06-09 15:19:34 +0000263 while (--i >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000264 Py_XDECREF(op->ob_item[i]);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000265 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000266 PyMem_FREE(op->ob_item);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000267 }
Raymond Hettinger0468e412004-05-05 05:37:53 +0000268 if (num_free_lists < MAXFREELISTS && PyList_CheckExact(op))
269 free_lists[num_free_lists++] = op;
Tim Petersb38e2b62004-07-29 02:29:26 +0000270 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000271 Py_Type(op)->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000272 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273}
274
Guido van Rossum90933611991-06-07 16:10:43 +0000275static int
Fred Drakea2f55112000-07-09 15:16:51 +0000276list_print(PyListObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000278 int rc;
279 Py_ssize_t i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000280
Martin v. Löwis18e16552006-02-15 17:27:45 +0000281 rc = Py_ReprEnter((PyObject*)op);
282 if (rc != 0) {
283 if (rc < 0)
284 return rc;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000285 fprintf(fp, "[...]");
286 return 0;
287 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288 fprintf(fp, "[");
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000289 for (i = 0; i < Py_Size(op); i++) {
Guido van Rossum90933611991-06-07 16:10:43 +0000290 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291 fprintf(fp, ", ");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000292 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
293 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000294 return -1;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000295 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296 }
297 fprintf(fp, "]");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000298 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000299 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300}
301
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000302static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000303list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000305 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000306 PyObject *s, *temp;
307 PyObject *pieces = NULL, *result = NULL;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000308
309 i = Py_ReprEnter((PyObject*)v);
310 if (i != 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000311 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000312 }
Tim Petersa7259592001-06-16 05:11:17 +0000313
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000314 if (Py_Size(v) == 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000315 result = PyUnicode_FromString("[]");
Tim Petersa7259592001-06-16 05:11:17 +0000316 goto Done;
317 }
318
319 pieces = PyList_New(0);
320 if (pieces == NULL)
321 goto Done;
322
323 /* Do repr() on each element. Note that this may mutate the list,
324 so must refetch the list size on each iteration. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000325 for (i = 0; i < Py_Size(v); ++i) {
Tim Petersa7259592001-06-16 05:11:17 +0000326 int status;
327 s = PyObject_Repr(v->ob_item[i]);
328 if (s == NULL)
329 goto Done;
330 status = PyList_Append(pieces, s);
331 Py_DECREF(s); /* append created a new ref */
332 if (status < 0)
333 goto Done;
334 }
335
336 /* Add "[]" decorations to the first and last items. */
337 assert(PyList_GET_SIZE(pieces) > 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000338 s = PyUnicode_FromString("[");
Tim Petersa7259592001-06-16 05:11:17 +0000339 if (s == NULL)
340 goto Done;
341 temp = PyList_GET_ITEM(pieces, 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000342 PyUnicode_AppendAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +0000343 PyList_SET_ITEM(pieces, 0, s);
344 if (s == NULL)
345 goto Done;
346
Walter Dörwald1ab83302007-05-18 17:15:44 +0000347 s = PyUnicode_FromString("]");
Tim Petersa7259592001-06-16 05:11:17 +0000348 if (s == NULL)
349 goto Done;
350 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000351 PyUnicode_AppendAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +0000352 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
353 if (temp == NULL)
354 goto Done;
355
356 /* Paste them all together with ", " between. */
Walter Dörwald1ab83302007-05-18 17:15:44 +0000357 s = PyUnicode_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +0000358 if (s == NULL)
359 goto Done;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000360 result = PyUnicode_Join(s, pieces);
Tim Peters3b01a122002-07-19 02:35:45 +0000361 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000362
363Done:
364 Py_XDECREF(pieces);
Guido van Rossumfb376de1998-04-10 22:47:27 +0000365 Py_ReprLeave((PyObject *)v);
Tim Petersa7259592001-06-16 05:11:17 +0000366 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367}
368
Martin v. Löwis18e16552006-02-15 17:27:45 +0000369static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000370list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000372 return Py_Size(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373}
374
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000375static int
Fred Drakea2f55112000-07-09 15:16:51 +0000376list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000377{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000378 Py_ssize_t i;
379 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000380
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000381 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_Size(a); ++i)
Raymond Hettingeraae59992002-09-05 14:23:49 +0000382 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
Guido van Rossum65e1cea2001-01-17 22:11:59 +0000383 Py_EQ);
Neal Norwitzbb9c5f52002-09-05 21:32:55 +0000384 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000385}
386
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000387static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000388list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000390 if (i < 0 || i >= Py_Size(a)) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000391 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000392 indexerr = PyString_FromString(
393 "list index out of range");
394 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395 return NULL;
396 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398 return a->ob_item[i];
399}
400
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000401static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000402list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000404 PyListObject *np;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000405 PyObject **src, **dest;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000406 Py_ssize_t i, len;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407 if (ilow < 0)
408 ilow = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000409 else if (ilow > Py_Size(a))
410 ilow = Py_Size(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411 if (ihigh < ilow)
412 ihigh = ilow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000413 else if (ihigh > Py_Size(a))
414 ihigh = Py_Size(a);
Raymond Hettinger99842b62004-03-08 05:56:15 +0000415 len = ihigh - ilow;
416 np = (PyListObject *) PyList_New(len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000417 if (np == NULL)
418 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000419
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000420 src = a->ob_item + ilow;
421 dest = np->ob_item;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000422 for (i = 0; i < len; i++) {
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000423 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000424 Py_INCREF(v);
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000425 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000427 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428}
429
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000430PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000431PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000432{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000433 if (!PyList_Check(a)) {
434 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000435 return NULL;
436 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000437 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000438}
439
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000440static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000441list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000443 Py_ssize_t size;
444 Py_ssize_t i;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000445 PyObject **src, **dest;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446 PyListObject *np;
447 if (!PyList_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000448 PyErr_Format(PyExc_TypeError,
Fred Drake914a2ed2000-06-01 14:31:03 +0000449 "can only concatenate list (not \"%.200s\") to list",
450 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451 return NULL;
452 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000453#define b ((PyListObject *)bb)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000454 size = Py_Size(a) + Py_Size(b);
Guido van Rossuma5c0e6d2002-10-11 21:05:56 +0000455 if (size < 0)
456 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000459 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000461 src = a->ob_item;
462 dest = np->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000463 for (i = 0; i < Py_Size(a); i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000464 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000466 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000467 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000468 src = b->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000469 dest = np->ob_item + Py_Size(a);
470 for (i = 0; i < Py_Size(b); i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000471 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000472 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000473 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476#undef b
477}
478
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000479static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000480list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000481{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000482 Py_ssize_t i, j;
483 Py_ssize_t size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000484 PyListObject *np;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000485 PyObject **p, **items;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000486 PyObject *elem;
Guido van Rossumed98d481991-03-06 13:07:53 +0000487 if (n < 0)
488 n = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000489 size = Py_Size(a) * n;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000490 if (size == 0)
491 return PyList_New(0);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000492 if (n && size/n != Py_Size(a))
Guido van Rossuma5c0e6d2002-10-11 21:05:56 +0000493 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000494 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000495 if (np == NULL)
496 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000497
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000498 items = np->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000499 if (Py_Size(a) == 1) {
Raymond Hettinger6624e682003-05-21 05:58:46 +0000500 elem = a->ob_item[0];
501 for (i = 0; i < n; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000502 items[i] = elem;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000503 Py_INCREF(elem);
504 }
505 return (PyObject *) np;
506 }
Guido van Rossumed98d481991-03-06 13:07:53 +0000507 p = np->ob_item;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000508 items = a->ob_item;
Guido van Rossumed98d481991-03-06 13:07:53 +0000509 for (i = 0; i < n; i++) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000510 for (j = 0; j < Py_Size(a); j++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000511 *p = items[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000512 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000513 p++;
514 }
515 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000516 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000517}
518
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000519static int
Armin Rigo93677f02004-07-29 12:40:23 +0000520list_clear(PyListObject *a)
521{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000522 Py_ssize_t i;
Armin Rigo93677f02004-07-29 12:40:23 +0000523 PyObject **item = a->ob_item;
524 if (item != NULL) {
525 /* Because XDECREF can recursively invoke operations on
526 this list, we make it empty first. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000527 i = Py_Size(a);
528 Py_Size(a) = 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000529 a->ob_item = NULL;
530 a->allocated = 0;
531 while (--i >= 0) {
532 Py_XDECREF(item[i]);
533 }
534 PyMem_FREE(item);
535 }
536 /* Never fails; the return value can be ignored.
537 Note that there is no guarantee that the list is actually empty
538 at this point, because XDECREF may have populated it again! */
539 return 0;
540}
541
Tim Peters8fc4a912004-07-31 21:53:19 +0000542/* a[ilow:ihigh] = v if v != NULL.
543 * del a[ilow:ihigh] if v == NULL.
544 *
545 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
546 * guaranteed the call cannot fail.
547 */
Armin Rigo93677f02004-07-29 12:40:23 +0000548static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000549list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000551 /* Because [X]DECREF can recursively invoke list operations on
552 this list, we must postpone all [X]DECREF activity until
553 after the list is back in its canonical shape. Therefore
554 we must allocate an additional array, 'recycle', into which
555 we temporarily copy the items that are deleted from the
556 list. :-( */
Tim Peters73572222004-07-31 02:54:42 +0000557 PyObject *recycle_on_stack[8];
558 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000559 PyObject **item;
Raymond Hettingerf889e102004-03-09 08:04:33 +0000560 PyObject **vitem = NULL;
Michael W. Hudson5da854f2002-11-05 17:38:05 +0000561 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000562 Py_ssize_t n; /* # of elements in replacement list */
563 Py_ssize_t norig; /* # of elements in list getting replaced */
564 Py_ssize_t d; /* Change in size */
565 Py_ssize_t k;
Tim Peters8d9eb102004-07-31 02:24:20 +0000566 size_t s;
567 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569 if (v == NULL)
570 n = 0;
Michael W. Hudson5da854f2002-11-05 17:38:05 +0000571 else {
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000572 if (a == b) {
573 /* Special case "a[i:j] = a" -- copy b first */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000574 v = list_slice(b, 0, Py_Size(b));
Martin v. Löwiscd12bfc2003-05-03 10:53:08 +0000575 if (v == NULL)
Tim Peters8d9eb102004-07-31 02:24:20 +0000576 return result;
577 result = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578 Py_DECREF(v);
Tim Peters8d9eb102004-07-31 02:24:20 +0000579 return result;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000580 }
Raymond Hettingerf889e102004-03-09 08:04:33 +0000581 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
Michael W. Hudsonb4f49382003-08-14 17:04:28 +0000582 if(v_as_SF == NULL)
Tim Peters8d9eb102004-07-31 02:24:20 +0000583 goto Error;
Michael W. Hudsonb4f49382003-08-14 17:04:28 +0000584 n = PySequence_Fast_GET_SIZE(v_as_SF);
Raymond Hettinger42bec932004-03-12 16:38:17 +0000585 vitem = PySequence_Fast_ITEMS(v_as_SF);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000586 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587 if (ilow < 0)
588 ilow = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000589 else if (ilow > Py_Size(a))
590 ilow = Py_Size(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000591
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000592 if (ihigh < ilow)
593 ihigh = ilow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000594 else if (ihigh > Py_Size(a))
595 ihigh = Py_Size(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000596
Tim Peters8d9eb102004-07-31 02:24:20 +0000597 norig = ihigh - ilow;
598 assert(norig >= 0);
599 d = n - norig;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000600 if (Py_Size(a) + d == 0) {
Armin Rigo93677f02004-07-29 12:40:23 +0000601 Py_XDECREF(v_as_SF);
602 return list_clear(a);
603 }
604 item = a->ob_item;
Tim Peters8d9eb102004-07-31 02:24:20 +0000605 /* recycle the items that we are about to remove */
606 s = norig * sizeof(PyObject *);
Tim Peters73572222004-07-31 02:54:42 +0000607 if (s > sizeof(recycle_on_stack)) {
Armin Rigo1dd04a02004-07-30 11:38:22 +0000608 recycle = (PyObject **)PyMem_MALLOC(s);
Martin v. Löwiscd12bfc2003-05-03 10:53:08 +0000609 if (recycle == NULL) {
610 PyErr_NoMemory();
Tim Peters8d9eb102004-07-31 02:24:20 +0000611 goto Error;
Martin v. Löwiscd12bfc2003-05-03 10:53:08 +0000612 }
613 }
Armin Rigo1dd04a02004-07-30 11:38:22 +0000614 memcpy(recycle, &item[ilow], s);
Tim Peters8d9eb102004-07-31 02:24:20 +0000615
Armin Rigo1dd04a02004-07-30 11:38:22 +0000616 if (d < 0) { /* Delete -d items */
617 memmove(&item[ihigh+d], &item[ihigh],
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000618 (Py_Size(a) - ihigh)*sizeof(PyObject *));
619 list_resize(a, Py_Size(a) + d);
Armin Rigo1dd04a02004-07-30 11:38:22 +0000620 item = a->ob_item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000621 }
Armin Rigo1dd04a02004-07-30 11:38:22 +0000622 else if (d > 0) { /* Insert d items */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000623 k = Py_Size(a);
Tim Peters73572222004-07-31 02:54:42 +0000624 if (list_resize(a, k+d) < 0)
Tim Peters8d9eb102004-07-31 02:24:20 +0000625 goto Error;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000626 item = a->ob_item;
Raymond Hettingerf889e102004-03-09 08:04:33 +0000627 memmove(&item[ihigh+d], &item[ihigh],
Tim Peters73572222004-07-31 02:54:42 +0000628 (k - ihigh)*sizeof(PyObject *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000629 }
630 for (k = 0; k < n; k++, ilow++) {
Raymond Hettingerf889e102004-03-09 08:04:33 +0000631 PyObject *w = vitem[k];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000633 item[ilow] = w;
634 }
Tim Peters73572222004-07-31 02:54:42 +0000635 for (k = norig - 1; k >= 0; --k)
636 Py_XDECREF(recycle[k]);
Tim Peters8d9eb102004-07-31 02:24:20 +0000637 result = 0;
638 Error:
Tim Peters73572222004-07-31 02:54:42 +0000639 if (recycle != recycle_on_stack)
Armin Rigo1dd04a02004-07-30 11:38:22 +0000640 PyMem_FREE(recycle);
Michael W. Hudson5da854f2002-11-05 17:38:05 +0000641 Py_XDECREF(v_as_SF);
Tim Peters8d9eb102004-07-31 02:24:20 +0000642 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643#undef b
644}
645
Guido van Rossum234f9421993-06-17 12:35:49 +0000646int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000647PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000648{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649 if (!PyList_Check(a)) {
650 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000651 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000652 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000654}
655
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000656static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000657list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000658{
659 PyObject **items;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000660 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000661
662
663 size = PyList_GET_SIZE(self);
664 if (size == 0) {
665 Py_INCREF(self);
666 return (PyObject *)self;
667 }
668
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000669 if (n < 1) {
Armin Rigo93677f02004-07-29 12:40:23 +0000670 (void)list_clear(self);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000671 Py_INCREF(self);
672 return (PyObject *)self;
673 }
674
Tim Petersb38e2b62004-07-29 02:29:26 +0000675 if (list_resize(self, size*n) == -1)
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000676 return NULL;
677
678 p = size;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000679 items = self->ob_item;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000680 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
681 for (j = 0; j < size; j++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000682 PyObject *o = items[j];
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000683 Py_INCREF(o);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000684 items[p++] = o;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000685 }
686 }
687 Py_INCREF(self);
688 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000689}
690
Guido van Rossum4a450d01991-04-03 19:05:18 +0000691static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000692list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000693{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 PyObject *old_value;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000695 if (i < 0 || i >= Py_Size(a)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696 PyErr_SetString(PyExc_IndexError,
697 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000698 return -1;
699 }
700 if (v == NULL)
701 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000702 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000703 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000704 a->ob_item[i] = v;
Tim Peters3b01a122002-07-19 02:35:45 +0000705 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000706 return 0;
707}
708
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000709static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000710listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000711{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000712 Py_ssize_t i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000713 PyObject *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000714 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715 return NULL;
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +0000716 if (ins1(self, i, v) == 0)
717 Py_RETURN_NONE;
Raymond Hettinger501f02c2004-04-12 14:01:16 +0000718 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719}
720
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000721static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000722listappend(PyListObject *self, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000723{
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +0000724 if (app1(self, v) == 0)
725 Py_RETURN_NONE;
Raymond Hettinger501f02c2004-04-12 14:01:16 +0000726 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727}
728
Barry Warsawdedf6d61998-10-09 16:37:25 +0000729static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000730listextend(PyListObject *self, PyObject *b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000731{
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000732 PyObject *it; /* iter(v) */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000733 Py_ssize_t m; /* size of self */
734 Py_ssize_t n; /* guess for size of b */
735 Py_ssize_t mn; /* m + n */
736 Py_ssize_t i;
Raymond Hettinger57c45422004-03-11 09:48:18 +0000737 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000738
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000739 /* Special cases:
Tim Petersb38e2b62004-07-29 02:29:26 +0000740 1) lists and tuples which can use PySequence_Fast ops
741 2) extending self to self requires making a copy first
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000742 */
743 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
Armin Rigo70d172d2004-03-20 22:19:23 +0000744 PyObject **src, **dest;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000745 b = PySequence_Fast(b, "argument must be iterable");
746 if (!b)
747 return NULL;
Armin Rigo70d172d2004-03-20 22:19:23 +0000748 n = PySequence_Fast_GET_SIZE(b);
749 if (n == 0) {
750 /* short circuit when b is empty */
751 Py_DECREF(b);
752 Py_RETURN_NONE;
753 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000754 m = Py_Size(self);
Armin Rigo70d172d2004-03-20 22:19:23 +0000755 if (list_resize(self, m + n) == -1) {
756 Py_DECREF(b);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000757 return NULL;
Armin Rigo70d172d2004-03-20 22:19:23 +0000758 }
759 /* note that we may still have self == b here for the
760 * situation a.extend(a), but the following code works
761 * in that case too. Just make sure to resize self
762 * before calling PySequence_Fast_ITEMS.
763 */
764 /* populate the end of self with b's items */
765 src = PySequence_Fast_ITEMS(b);
766 dest = self->ob_item + m;
767 for (i = 0; i < n; i++) {
768 PyObject *o = src[i];
769 Py_INCREF(o);
770 dest[i] = o;
771 }
772 Py_DECREF(b);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000773 Py_RETURN_NONE;
774 }
775
776 it = PyObject_GetIter(b);
777 if (it == NULL)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000778 return NULL;
Raymond Hettinger57c45422004-03-11 09:48:18 +0000779 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000780
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000781 /* Guess a result list size. */
Armin Rigof5b3e362006-02-11 21:32:43 +0000782 n = _PyObject_LengthHint(b);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000783 if (n < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000784 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
785 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
786 Py_DECREF(it);
787 return NULL;
788 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000789 PyErr_Clear();
790 n = 8; /* arbitrary */
791 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000792 m = Py_Size(self);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000793 mn = m + n;
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000794 if (mn >= m) {
795 /* Make room. */
796 if (list_resize(self, mn) == -1)
797 goto error;
798 /* Make the list sane again. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000799 Py_Size(self) = m;
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000800 }
801 /* Else m + n overflowed; on the chance that n lied, and there really
802 * is enough room, ignore it. If n was telling the truth, we'll
803 * eventually run out of memory during the loop.
804 */
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000805
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000806 /* Run iterator to exhaustion. */
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000807 for (;;) {
Raymond Hettinger57c45422004-03-11 09:48:18 +0000808 PyObject *item = iternext(it);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000809 if (item == NULL) {
Raymond Hettinger57c45422004-03-11 09:48:18 +0000810 if (PyErr_Occurred()) {
811 if (PyErr_ExceptionMatches(PyExc_StopIteration))
812 PyErr_Clear();
813 else
814 goto error;
815 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000816 break;
817 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000818 if (Py_Size(self) < self->allocated) {
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000819 /* steals ref */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000820 PyList_SET_ITEM(self, Py_Size(self), item);
821 ++Py_Size(self);
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000822 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000823 else {
Raymond Hettinger40a03822004-04-12 13:05:09 +0000824 int status = app1(self, item);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000825 Py_DECREF(item); /* append creates a new ref */
826 if (status < 0)
827 goto error;
828 }
829 }
830
831 /* Cut back result list if initial guess was too large. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000832 if (Py_Size(self) < self->allocated)
833 list_resize(self, Py_Size(self)); /* shrinking can't fail */
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000834
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000835 Py_DECREF(it);
836 Py_RETURN_NONE;
837
838 error:
839 Py_DECREF(it);
840 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000841}
842
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000843PyObject *
844_PyList_Extend(PyListObject *self, PyObject *b)
845{
846 return listextend(self, b);
847}
848
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000849static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000850list_inplace_concat(PyListObject *self, PyObject *other)
851{
852 PyObject *result;
853
854 result = listextend(self, other);
855 if (result == NULL)
856 return result;
857 Py_DECREF(result);
858 Py_INCREF(self);
859 return (PyObject *)self;
860}
861
862static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000863listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000864{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000865 Py_ssize_t i = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000866 PyObject *v;
Tim Peters8fc4a912004-07-31 21:53:19 +0000867 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000868
Thomas Wouters89f507f2006-12-13 04:49:30 +0000869 if (!PyArg_ParseTuple(args, "|n:pop", &i))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000870 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000871
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000872 if (Py_Size(self) == 0) {
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000873 /* Special-case most common failure cause */
874 PyErr_SetString(PyExc_IndexError, "pop from empty list");
875 return NULL;
876 }
877 if (i < 0)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000878 i += Py_Size(self);
879 if (i < 0 || i >= Py_Size(self)) {
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000880 PyErr_SetString(PyExc_IndexError, "pop index out of range");
881 return NULL;
882 }
883 v = self->ob_item[i];
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000884 if (i == Py_Size(self) - 1) {
885 status = list_resize(self, Py_Size(self) - 1);
Tim Peters8fc4a912004-07-31 21:53:19 +0000886 assert(status >= 0);
887 return v; /* and v now owns the reference the list had */
Raymond Hettingercb3e5802004-02-13 18:36:31 +0000888 }
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000889 Py_INCREF(v);
Tim Peters8fc4a912004-07-31 21:53:19 +0000890 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
891 assert(status >= 0);
892 /* Use status, so that in a release build compilers don't
893 * complain about the unused name.
894 */
Brett Cannon651dd522004-08-08 21:21:18 +0000895 (void) status;
896
897 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000898}
899
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000900/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
901static void
902reverse_slice(PyObject **lo, PyObject **hi)
903{
904 assert(lo && hi);
905
906 --hi;
907 while (lo < hi) {
908 PyObject *t = *lo;
909 *lo = *hi;
910 *hi = t;
911 ++lo;
912 --hi;
913 }
914}
915
Tim Petersa64dc242002-08-01 02:13:36 +0000916/* Lots of code for an adaptive, stable, natural mergesort. There are many
917 * pieces to this algorithm; read listsort.txt for overviews and details.
918 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000919
Guido van Rossum3f236de1996-12-10 23:55:39 +0000920/* Comparison function. Takes care of calling a user-supplied
Tim Peters66860f62002-08-04 17:47:26 +0000921 * comparison function (any callable Python object), which must not be
922 * NULL (use the ISLT macro if you don't know, or call PyObject_RichCompareBool
923 * with Py_LT if you know it's NULL).
Tim Petersa64dc242002-08-01 02:13:36 +0000924 * Returns -1 on error, 1 if x < y, 0 if x >= y.
925 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000926static int
Tim Petersa8c974c2002-07-19 03:30:57 +0000927islt(PyObject *x, PyObject *y, PyObject *compare)
Guido van Rossum3f236de1996-12-10 23:55:39 +0000928{
Tim Petersf2a04732002-07-11 21:46:16 +0000929 PyObject *res;
930 PyObject *args;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000931 Py_ssize_t i;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000932
Tim Peters66860f62002-08-04 17:47:26 +0000933 assert(compare != NULL);
Tim Petersa8c974c2002-07-19 03:30:57 +0000934 /* Call the user's comparison function and translate the 3-way
935 * result into true or false (or error).
936 */
Tim Petersf2a04732002-07-11 21:46:16 +0000937 args = PyTuple_New(2);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000938 if (args == NULL)
Tim Petersa8c974c2002-07-19 03:30:57 +0000939 return -1;
Tim Petersf2a04732002-07-11 21:46:16 +0000940 Py_INCREF(x);
941 Py_INCREF(y);
942 PyTuple_SET_ITEM(args, 0, x);
943 PyTuple_SET_ITEM(args, 1, y);
Tim Peters58cf3612002-07-15 05:16:13 +0000944 res = PyObject_Call(compare, args, NULL);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000945 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000946 if (res == NULL)
Tim Petersa8c974c2002-07-19 03:30:57 +0000947 return -1;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000948 if (!PyInt_CheckExact(res)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000949 PyErr_Format(PyExc_TypeError,
950 "comparison function must return int, not %.200s",
951 res->ob_type->tp_name);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000952 Py_DECREF(res);
Tim Petersa8c974c2002-07-19 03:30:57 +0000953 return -1;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000954 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000955 i = PyInt_AsLong(res);
956 Py_DECREF(res);
Tim Petersa8c974c2002-07-19 03:30:57 +0000957 return i < 0;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000958}
959
Tim Peters66860f62002-08-04 17:47:26 +0000960/* If COMPARE is NULL, calls PyObject_RichCompareBool with Py_LT, else calls
961 * islt. This avoids a layer of function call in the usual case, and
962 * sorting does many comparisons.
963 * Returns -1 on error, 1 if x < y, 0 if x >= y.
964 */
965#define ISLT(X, Y, COMPARE) ((COMPARE) == NULL ? \
966 PyObject_RichCompareBool(X, Y, Py_LT) : \
967 islt(X, Y, COMPARE))
968
969/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +0000970 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
971 started. It makes more sense in context <wink>. X and Y are PyObject*s.
972*/
Tim Peters66860f62002-08-04 17:47:26 +0000973#define IFLT(X, Y) if ((k = ISLT(X, Y, compare)) < 0) goto fail; \
Tim Petersa8c974c2002-07-19 03:30:57 +0000974 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000975
976/* binarysort is the best method for sorting small arrays: it does
977 few compares, but can do data movement quadratic in the number of
978 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000979 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +0000980 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000981 On entry, must have lo <= start <= hi, and that [lo, start) is already
982 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +0000983 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000984 Even in case of error, the output slice will be some permutation of
985 the input (nothing is lost or duplicated).
986*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000987static int
Fred Drakea2f55112000-07-09 15:16:51 +0000988binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
989 /* compare -- comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000990{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000991 register Py_ssize_t k;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000992 register PyObject **l, **p, **r;
993 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000994
Tim Petersa8c974c2002-07-19 03:30:57 +0000995 assert(lo <= start && start <= hi);
996 /* assert [lo, start) is sorted */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000997 if (lo == start)
998 ++start;
999 for (; start < hi; ++start) {
1000 /* set l to where *start belongs */
1001 l = lo;
1002 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001003 pivot = *r;
Tim Peters0fe977c2002-07-19 06:12:32 +00001004 /* Invariants:
1005 * pivot >= all in [lo, l).
1006 * pivot < all in [r, start).
1007 * The second is vacuously true at the start.
1008 */
1009 assert(l < r);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001010 do {
1011 p = l + ((r - l) >> 1);
Tim Petersa8c974c2002-07-19 03:30:57 +00001012 IFLT(pivot, *p)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001013 r = p;
1014 else
Tim Peters0fe977c2002-07-19 06:12:32 +00001015 l = p+1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001016 } while (l < r);
Tim Peters0fe977c2002-07-19 06:12:32 +00001017 assert(l == r);
1018 /* The invariants still hold, so pivot >= all in [lo, l) and
1019 pivot < all in [l, start), so pivot belongs at l. Note
1020 that if there are elements equal to pivot, l points to the
1021 first slot after them -- that's why this sort is stable.
1022 Slide over to make room.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001023 Caution: using memmove is much slower under MSVC 5;
1024 we're not usually moving many slots. */
1025 for (p = start; p > l; --p)
1026 *p = *(p-1);
1027 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001028 }
Guido van Rossum3f236de1996-12-10 23:55:39 +00001029 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001030
1031 fail:
1032 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001033}
1034
Tim Petersa64dc242002-08-01 02:13:36 +00001035/*
1036Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1037is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001038
Tim Petersa64dc242002-08-01 02:13:36 +00001039 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001040
Tim Petersa64dc242002-08-01 02:13:36 +00001041or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001042
Tim Petersa64dc242002-08-01 02:13:36 +00001043 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001044
Tim Petersa64dc242002-08-01 02:13:36 +00001045Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1046For its intended use in a stable mergesort, the strictness of the defn of
1047"descending" is needed so that the caller can safely reverse a descending
1048sequence without violating stability (strict > ensures there are no equal
1049elements to get out of order).
1050
1051Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001052*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001053static Py_ssize_t
Tim Petersa64dc242002-08-01 02:13:36 +00001054count_run(PyObject **lo, PyObject **hi, PyObject *compare, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001055{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001056 Py_ssize_t k;
1057 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001058
Tim Petersa64dc242002-08-01 02:13:36 +00001059 assert(lo < hi);
1060 *descending = 0;
1061 ++lo;
1062 if (lo == hi)
1063 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001064
Tim Petersa64dc242002-08-01 02:13:36 +00001065 n = 2;
1066 IFLT(*lo, *(lo-1)) {
1067 *descending = 1;
1068 for (lo = lo+1; lo < hi; ++lo, ++n) {
1069 IFLT(*lo, *(lo-1))
1070 ;
1071 else
1072 break;
1073 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001074 }
Tim Petersa64dc242002-08-01 02:13:36 +00001075 else {
1076 for (lo = lo+1; lo < hi; ++lo, ++n) {
1077 IFLT(*lo, *(lo-1))
1078 break;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001079 }
1080 }
1081
Tim Petersa64dc242002-08-01 02:13:36 +00001082 return n;
1083fail:
1084 return -1;
1085}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001086
Tim Petersa64dc242002-08-01 02:13:36 +00001087/*
1088Locate the proper position of key in a sorted vector; if the vector contains
1089an element equal to key, return the position immediately to the left of
1090the leftmost equal element. [gallop_right() does the same except returns
1091the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001092
Tim Petersa64dc242002-08-01 02:13:36 +00001093"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001094
Tim Petersa64dc242002-08-01 02:13:36 +00001095"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1096hint is to the final result, the faster this runs.
1097
1098The return value is the int k in 0..n such that
1099
1100 a[k-1] < key <= a[k]
1101
1102pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1103key belongs at index k; or, IOW, the first k elements of a should precede
1104key, and the last n-k should follow key.
1105
1106Returns -1 on error. See listsort.txt for info on the method.
1107*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001108static Py_ssize_t
1109gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare)
Tim Petersa64dc242002-08-01 02:13:36 +00001110{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001111 Py_ssize_t ofs;
1112 Py_ssize_t lastofs;
1113 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001114
1115 assert(key && a && n > 0 && hint >= 0 && hint < n);
1116
1117 a += hint;
1118 lastofs = 0;
1119 ofs = 1;
1120 IFLT(*a, key) {
1121 /* a[hint] < key -- gallop right, until
1122 * a[hint + lastofs] < key <= a[hint + ofs]
1123 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001124 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
Tim Petersa64dc242002-08-01 02:13:36 +00001125 while (ofs < maxofs) {
1126 IFLT(a[ofs], key) {
1127 lastofs = ofs;
1128 ofs = (ofs << 1) + 1;
1129 if (ofs <= 0) /* int overflow */
1130 ofs = maxofs;
1131 }
1132 else /* key <= a[hint + ofs] */
1133 break;
1134 }
1135 if (ofs > maxofs)
1136 ofs = maxofs;
1137 /* Translate back to offsets relative to &a[0]. */
1138 lastofs += hint;
1139 ofs += hint;
1140 }
1141 else {
1142 /* key <= a[hint] -- gallop left, until
1143 * a[hint - ofs] < key <= a[hint - lastofs]
1144 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001145 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
Tim Petersa64dc242002-08-01 02:13:36 +00001146 while (ofs < maxofs) {
1147 IFLT(*(a-ofs), key)
1148 break;
1149 /* key <= a[hint - ofs] */
1150 lastofs = ofs;
1151 ofs = (ofs << 1) + 1;
1152 if (ofs <= 0) /* int overflow */
1153 ofs = maxofs;
1154 }
1155 if (ofs > maxofs)
1156 ofs = maxofs;
1157 /* Translate back to positive offsets relative to &a[0]. */
1158 k = lastofs;
1159 lastofs = hint - ofs;
1160 ofs = hint - k;
1161 }
1162 a -= hint;
1163
1164 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1165 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1166 * right of lastofs but no farther right than ofs. Do a binary
1167 * search, with invariant a[lastofs-1] < key <= a[ofs].
1168 */
1169 ++lastofs;
1170 while (lastofs < ofs) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001171 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001172
1173 IFLT(a[m], key)
1174 lastofs = m+1; /* a[m] < key */
1175 else
1176 ofs = m; /* key <= a[m] */
1177 }
1178 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1179 return ofs;
1180
1181fail:
1182 return -1;
1183}
1184
1185/*
1186Exactly like gallop_left(), except that if key already exists in a[0:n],
1187finds the position immediately to the right of the rightmost equal value.
1188
1189The return value is the int k in 0..n such that
1190
1191 a[k-1] <= key < a[k]
1192
1193or -1 if error.
1194
1195The code duplication is massive, but this is enough different given that
1196we're sticking to "<" comparisons that it's much harder to follow if
1197written as one routine with yet another "left or right?" flag.
1198*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001199static Py_ssize_t
1200gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare)
Tim Petersa64dc242002-08-01 02:13:36 +00001201{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001202 Py_ssize_t ofs;
1203 Py_ssize_t lastofs;
1204 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001205
1206 assert(key && a && n > 0 && hint >= 0 && hint < n);
1207
1208 a += hint;
1209 lastofs = 0;
1210 ofs = 1;
1211 IFLT(key, *a) {
1212 /* key < a[hint] -- gallop left, until
1213 * a[hint - ofs] <= key < a[hint - lastofs]
1214 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001215 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
Tim Petersa64dc242002-08-01 02:13:36 +00001216 while (ofs < maxofs) {
1217 IFLT(key, *(a-ofs)) {
1218 lastofs = ofs;
1219 ofs = (ofs << 1) + 1;
1220 if (ofs <= 0) /* int overflow */
1221 ofs = maxofs;
1222 }
1223 else /* a[hint - ofs] <= key */
1224 break;
1225 }
1226 if (ofs > maxofs)
1227 ofs = maxofs;
1228 /* Translate back to positive offsets relative to &a[0]. */
1229 k = lastofs;
1230 lastofs = hint - ofs;
1231 ofs = hint - k;
1232 }
1233 else {
1234 /* a[hint] <= key -- gallop right, until
1235 * a[hint + lastofs] <= key < a[hint + ofs]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001236 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001237 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
Tim Petersa64dc242002-08-01 02:13:36 +00001238 while (ofs < maxofs) {
1239 IFLT(key, a[ofs])
1240 break;
1241 /* a[hint + ofs] <= key */
1242 lastofs = ofs;
1243 ofs = (ofs << 1) + 1;
1244 if (ofs <= 0) /* int overflow */
1245 ofs = maxofs;
1246 }
1247 if (ofs > maxofs)
1248 ofs = maxofs;
1249 /* Translate back to offsets relative to &a[0]. */
1250 lastofs += hint;
1251 ofs += hint;
1252 }
1253 a -= hint;
1254
1255 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1256 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1257 * right of lastofs but no farther right than ofs. Do a binary
1258 * search, with invariant a[lastofs-1] <= key < a[ofs].
1259 */
1260 ++lastofs;
1261 while (lastofs < ofs) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001262 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001263
1264 IFLT(key, a[m])
1265 ofs = m; /* key < a[m] */
1266 else
1267 lastofs = m+1; /* a[m] <= key */
1268 }
1269 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1270 return ofs;
1271
1272fail:
1273 return -1;
1274}
1275
1276/* The maximum number of entries in a MergeState's pending-runs stack.
1277 * This is enough to sort arrays of size up to about
1278 * 32 * phi ** MAX_MERGE_PENDING
1279 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1280 * with 2**64 elements.
1281 */
1282#define MAX_MERGE_PENDING 85
1283
Tim Peterse05f65a2002-08-10 05:21:15 +00001284/* When we get into galloping mode, we stay there until both runs win less
1285 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
Tim Petersa64dc242002-08-01 02:13:36 +00001286 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001287#define MIN_GALLOP 7
Tim Petersa64dc242002-08-01 02:13:36 +00001288
1289/* Avoid malloc for small temp arrays. */
1290#define MERGESTATE_TEMP_SIZE 256
1291
1292/* One MergeState exists on the stack per invocation of mergesort. It's just
1293 * a convenient way to pass state around among the helper functions.
1294 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001295struct s_slice {
1296 PyObject **base;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001297 Py_ssize_t len;
Tim Peterse05f65a2002-08-10 05:21:15 +00001298};
1299
Tim Petersa64dc242002-08-01 02:13:36 +00001300typedef struct s_MergeState {
1301 /* The user-supplied comparison function. or NULL if none given. */
1302 PyObject *compare;
1303
Tim Peterse05f65a2002-08-10 05:21:15 +00001304 /* This controls when we get *into* galloping mode. It's initialized
1305 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1306 * random data, and lower for highly structured data.
1307 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001308 Py_ssize_t min_gallop;
Tim Peterse05f65a2002-08-10 05:21:15 +00001309
Tim Petersa64dc242002-08-01 02:13:36 +00001310 /* 'a' is temp storage to help with merges. It contains room for
1311 * alloced entries.
1312 */
1313 PyObject **a; /* may point to temparray below */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001314 Py_ssize_t alloced;
Tim Petersa64dc242002-08-01 02:13:36 +00001315
1316 /* A stack of n pending runs yet to be merged. Run #i starts at
1317 * address base[i] and extends for len[i] elements. It's always
1318 * true (so long as the indices are in bounds) that
1319 *
Tim Peterse05f65a2002-08-10 05:21:15 +00001320 * pending[i].base + pending[i].len == pending[i+1].base
Tim Petersa64dc242002-08-01 02:13:36 +00001321 *
1322 * so we could cut the storage for this, but it's a minor amount,
1323 * and keeping all the info explicit simplifies the code.
1324 */
1325 int n;
Tim Peterse05f65a2002-08-10 05:21:15 +00001326 struct s_slice pending[MAX_MERGE_PENDING];
Tim Petersa64dc242002-08-01 02:13:36 +00001327
1328 /* 'a' points to this when possible, rather than muck with malloc. */
1329 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1330} MergeState;
1331
1332/* Conceptually a MergeState's constructor. */
1333static void
1334merge_init(MergeState *ms, PyObject *compare)
1335{
1336 assert(ms != NULL);
1337 ms->compare = compare;
1338 ms->a = ms->temparray;
1339 ms->alloced = MERGESTATE_TEMP_SIZE;
1340 ms->n = 0;
Tim Peterse05f65a2002-08-10 05:21:15 +00001341 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001342}
1343
1344/* Free all the temp memory owned by the MergeState. This must be called
1345 * when you're done with a MergeState, and may be called before then if
1346 * you want to free the temp memory early.
1347 */
1348static void
1349merge_freemem(MergeState *ms)
1350{
1351 assert(ms != NULL);
1352 if (ms->a != ms->temparray)
1353 PyMem_Free(ms->a);
1354 ms->a = ms->temparray;
1355 ms->alloced = MERGESTATE_TEMP_SIZE;
1356}
1357
1358/* Ensure enough temp memory for 'need' array slots is available.
1359 * Returns 0 on success and -1 if the memory can't be gotten.
1360 */
1361static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001362merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001363{
1364 assert(ms != NULL);
1365 if (need <= ms->alloced)
1366 return 0;
1367 /* Don't realloc! That can cost cycles to copy the old data, but
1368 * we don't care what's in the block.
1369 */
1370 merge_freemem(ms);
1371 ms->a = (PyObject **)PyMem_Malloc(need * sizeof(PyObject*));
1372 if (ms->a) {
1373 ms->alloced = need;
1374 return 0;
1375 }
1376 PyErr_NoMemory();
1377 merge_freemem(ms); /* reset to sane state */
1378 return -1;
1379}
1380#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1381 merge_getmem(MS, NEED))
1382
1383/* Merge the na elements starting at pa with the nb elements starting at pb
1384 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
1385 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
1386 * merge, and should have na <= nb. See listsort.txt for more info.
1387 * Return 0 if successful, -1 if error.
1388 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001389static Py_ssize_t
1390merge_lo(MergeState *ms, PyObject **pa, Py_ssize_t na,
1391 PyObject **pb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001392{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001393 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001394 PyObject *compare;
1395 PyObject **dest;
1396 int result = -1; /* guilty until proved innocent */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001397 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001398
1399 assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
1400 if (MERGE_GETMEM(ms, na) < 0)
1401 return -1;
1402 memcpy(ms->a, pa, na * sizeof(PyObject*));
1403 dest = pa;
1404 pa = ms->a;
1405
1406 *dest++ = *pb++;
1407 --nb;
1408 if (nb == 0)
1409 goto Succeed;
1410 if (na == 1)
1411 goto CopyB;
1412
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001413 min_gallop = ms->min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001414 compare = ms->compare;
1415 for (;;) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001416 Py_ssize_t acount = 0; /* # of times A won in a row */
1417 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001418
1419 /* Do the straightforward thing until (if ever) one run
1420 * appears to win consistently.
1421 */
1422 for (;;) {
Tim Peterse05f65a2002-08-10 05:21:15 +00001423 assert(na > 1 && nb > 0);
Tim Peters66860f62002-08-04 17:47:26 +00001424 k = ISLT(*pb, *pa, compare);
Tim Petersa64dc242002-08-01 02:13:36 +00001425 if (k) {
1426 if (k < 0)
1427 goto Fail;
1428 *dest++ = *pb++;
1429 ++bcount;
1430 acount = 0;
1431 --nb;
1432 if (nb == 0)
1433 goto Succeed;
Tim Peterse05f65a2002-08-10 05:21:15 +00001434 if (bcount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001435 break;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001436 }
1437 else {
Tim Petersa64dc242002-08-01 02:13:36 +00001438 *dest++ = *pa++;
1439 ++acount;
1440 bcount = 0;
1441 --na;
1442 if (na == 1)
1443 goto CopyB;
Tim Peterse05f65a2002-08-10 05:21:15 +00001444 if (acount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001445 break;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001446 }
Tim Petersa64dc242002-08-01 02:13:36 +00001447 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001448
Tim Petersa64dc242002-08-01 02:13:36 +00001449 /* One run is winning so consistently that galloping may
1450 * be a huge win. So try that, and continue galloping until
1451 * (if ever) neither run appears to be winning consistently
1452 * anymore.
1453 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001454 ++min_gallop;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001455 do {
Tim Peterse05f65a2002-08-10 05:21:15 +00001456 assert(na > 1 && nb > 0);
1457 min_gallop -= min_gallop > 1;
1458 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001459 k = gallop_right(*pb, pa, na, 0, compare);
1460 acount = k;
1461 if (k) {
1462 if (k < 0)
1463 goto Fail;
1464 memcpy(dest, pa, k * sizeof(PyObject *));
1465 dest += k;
1466 pa += k;
1467 na -= k;
1468 if (na == 1)
1469 goto CopyB;
1470 /* na==0 is impossible now if the comparison
1471 * function is consistent, but we can't assume
1472 * that it is.
1473 */
1474 if (na == 0)
1475 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001476 }
Tim Petersa64dc242002-08-01 02:13:36 +00001477 *dest++ = *pb++;
1478 --nb;
1479 if (nb == 0)
1480 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001481
Tim Petersa64dc242002-08-01 02:13:36 +00001482 k = gallop_left(*pa, pb, nb, 0, compare);
1483 bcount = k;
1484 if (k) {
1485 if (k < 0)
1486 goto Fail;
1487 memmove(dest, pb, k * sizeof(PyObject *));
1488 dest += k;
1489 pb += k;
1490 nb -= k;
1491 if (nb == 0)
1492 goto Succeed;
1493 }
1494 *dest++ = *pa++;
1495 --na;
1496 if (na == 1)
1497 goto CopyB;
1498 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
Tim Peterse05f65a2002-08-10 05:21:15 +00001499 ++min_gallop; /* penalize it for leaving galloping mode */
1500 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001501 }
1502Succeed:
1503 result = 0;
1504Fail:
1505 if (na)
1506 memcpy(dest, pa, na * sizeof(PyObject*));
1507 return result;
1508CopyB:
1509 assert(na == 1 && nb > 0);
1510 /* The last element of pa belongs at the end of the merge. */
1511 memmove(dest, pb, nb * sizeof(PyObject *));
1512 dest[nb] = *pa;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001513 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001514}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001515
Tim Petersa64dc242002-08-01 02:13:36 +00001516/* Merge the na elements starting at pa with the nb elements starting at pb
1517 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
1518 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
1519 * merge, and should have na >= nb. See listsort.txt for more info.
1520 * Return 0 if successful, -1 if error.
1521 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001522static Py_ssize_t
1523merge_hi(MergeState *ms, PyObject **pa, Py_ssize_t na, PyObject **pb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001524{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001525 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001526 PyObject *compare;
1527 PyObject **dest;
1528 int result = -1; /* guilty until proved innocent */
1529 PyObject **basea;
1530 PyObject **baseb;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001531 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001532
1533 assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
1534 if (MERGE_GETMEM(ms, nb) < 0)
1535 return -1;
1536 dest = pb + nb - 1;
1537 memcpy(ms->a, pb, nb * sizeof(PyObject*));
1538 basea = pa;
1539 baseb = ms->a;
1540 pb = ms->a + nb - 1;
1541 pa += na - 1;
1542
1543 *dest-- = *pa--;
1544 --na;
1545 if (na == 0)
1546 goto Succeed;
1547 if (nb == 1)
1548 goto CopyA;
1549
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001550 min_gallop = ms->min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001551 compare = ms->compare;
1552 for (;;) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001553 Py_ssize_t acount = 0; /* # of times A won in a row */
1554 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001555
1556 /* Do the straightforward thing until (if ever) one run
1557 * appears to win consistently.
1558 */
1559 for (;;) {
Tim Peterse05f65a2002-08-10 05:21:15 +00001560 assert(na > 0 && nb > 1);
Tim Peters66860f62002-08-04 17:47:26 +00001561 k = ISLT(*pb, *pa, compare);
Tim Petersa64dc242002-08-01 02:13:36 +00001562 if (k) {
1563 if (k < 0)
1564 goto Fail;
1565 *dest-- = *pa--;
1566 ++acount;
1567 bcount = 0;
1568 --na;
1569 if (na == 0)
1570 goto Succeed;
Tim Peterse05f65a2002-08-10 05:21:15 +00001571 if (acount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001572 break;
1573 }
1574 else {
1575 *dest-- = *pb--;
1576 ++bcount;
1577 acount = 0;
1578 --nb;
1579 if (nb == 1)
1580 goto CopyA;
Tim Peterse05f65a2002-08-10 05:21:15 +00001581 if (bcount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001582 break;
1583 }
1584 }
1585
1586 /* One run is winning so consistently that galloping may
1587 * be a huge win. So try that, and continue galloping until
1588 * (if ever) neither run appears to be winning consistently
1589 * anymore.
1590 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001591 ++min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001592 do {
Tim Peterse05f65a2002-08-10 05:21:15 +00001593 assert(na > 0 && nb > 1);
1594 min_gallop -= min_gallop > 1;
1595 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001596 k = gallop_right(*pb, basea, na, na-1, compare);
1597 if (k < 0)
1598 goto Fail;
1599 k = na - k;
1600 acount = k;
1601 if (k) {
1602 dest -= k;
1603 pa -= k;
1604 memmove(dest+1, pa+1, k * sizeof(PyObject *));
1605 na -= k;
1606 if (na == 0)
1607 goto Succeed;
1608 }
1609 *dest-- = *pb--;
1610 --nb;
1611 if (nb == 1)
1612 goto CopyA;
1613
1614 k = gallop_left(*pa, baseb, nb, nb-1, compare);
1615 if (k < 0)
1616 goto Fail;
1617 k = nb - k;
1618 bcount = k;
1619 if (k) {
1620 dest -= k;
1621 pb -= k;
1622 memcpy(dest+1, pb+1, k * sizeof(PyObject *));
1623 nb -= k;
1624 if (nb == 1)
1625 goto CopyA;
1626 /* nb==0 is impossible now if the comparison
1627 * function is consistent, but we can't assume
1628 * that it is.
1629 */
1630 if (nb == 0)
1631 goto Succeed;
1632 }
1633 *dest-- = *pa--;
1634 --na;
1635 if (na == 0)
1636 goto Succeed;
1637 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
Tim Peterse05f65a2002-08-10 05:21:15 +00001638 ++min_gallop; /* penalize it for leaving galloping mode */
1639 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001640 }
1641Succeed:
1642 result = 0;
1643Fail:
1644 if (nb)
1645 memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*));
1646 return result;
1647CopyA:
1648 assert(nb == 1 && na > 0);
1649 /* The first element of pb belongs at the front of the merge. */
1650 dest -= na;
1651 pa -= na;
1652 memmove(dest+1, pa+1, na * sizeof(PyObject *));
1653 *dest = *pb;
1654 return 0;
1655}
1656
1657/* Merge the two runs at stack indices i and i+1.
1658 * Returns 0 on success, -1 on error.
1659 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001660static Py_ssize_t
1661merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001662{
1663 PyObject **pa, **pb;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001664 Py_ssize_t na, nb;
1665 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001666 PyObject *compare;
1667
1668 assert(ms != NULL);
1669 assert(ms->n >= 2);
1670 assert(i >= 0);
1671 assert(i == ms->n - 2 || i == ms->n - 3);
1672
Tim Peterse05f65a2002-08-10 05:21:15 +00001673 pa = ms->pending[i].base;
1674 na = ms->pending[i].len;
1675 pb = ms->pending[i+1].base;
1676 nb = ms->pending[i+1].len;
Tim Petersa64dc242002-08-01 02:13:36 +00001677 assert(na > 0 && nb > 0);
1678 assert(pa + na == pb);
1679
1680 /* Record the length of the combined runs; if i is the 3rd-last
1681 * run now, also slide over the last run (which isn't involved
1682 * in this merge). The current run i+1 goes away in any case.
1683 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001684 ms->pending[i].len = na + nb;
1685 if (i == ms->n - 3)
1686 ms->pending[i+1] = ms->pending[i+2];
Tim Petersa64dc242002-08-01 02:13:36 +00001687 --ms->n;
1688
1689 /* Where does b start in a? Elements in a before that can be
1690 * ignored (already in place).
1691 */
1692 compare = ms->compare;
1693 k = gallop_right(*pb, pa, na, 0, compare);
1694 if (k < 0)
1695 return -1;
1696 pa += k;
1697 na -= k;
1698 if (na == 0)
1699 return 0;
1700
1701 /* Where does a end in b? Elements in b after that can be
1702 * ignored (already in place).
1703 */
1704 nb = gallop_left(pa[na-1], pb, nb, nb-1, compare);
1705 if (nb <= 0)
1706 return nb;
1707
1708 /* Merge what remains of the runs, using a temp array with
1709 * min(na, nb) elements.
1710 */
1711 if (na <= nb)
1712 return merge_lo(ms, pa, na, pb, nb);
1713 else
1714 return merge_hi(ms, pa, na, pb, nb);
1715}
1716
1717/* Examine the stack of runs waiting to be merged, merging adjacent runs
1718 * until the stack invariants are re-established:
1719 *
1720 * 1. len[-3] > len[-2] + len[-1]
1721 * 2. len[-2] > len[-1]
1722 *
1723 * See listsort.txt for more info.
1724 *
1725 * Returns 0 on success, -1 on error.
1726 */
1727static int
1728merge_collapse(MergeState *ms)
1729{
Tim Peterse05f65a2002-08-10 05:21:15 +00001730 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001731
1732 assert(ms);
1733 while (ms->n > 1) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001734 Py_ssize_t n = ms->n - 2;
Tim Peterse05f65a2002-08-10 05:21:15 +00001735 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
1736 if (p[n-1].len < p[n+1].len)
Tim Petersa64dc242002-08-01 02:13:36 +00001737 --n;
1738 if (merge_at(ms, n) < 0)
1739 return -1;
1740 }
Tim Peterse05f65a2002-08-10 05:21:15 +00001741 else if (p[n].len <= p[n+1].len) {
Tim Petersa64dc242002-08-01 02:13:36 +00001742 if (merge_at(ms, n) < 0)
1743 return -1;
1744 }
1745 else
1746 break;
1747 }
1748 return 0;
1749}
1750
1751/* Regardless of invariants, merge all runs on the stack until only one
1752 * remains. This is used at the end of the mergesort.
1753 *
1754 * Returns 0 on success, -1 on error.
1755 */
1756static int
1757merge_force_collapse(MergeState *ms)
1758{
Tim Peterse05f65a2002-08-10 05:21:15 +00001759 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001760
1761 assert(ms);
1762 while (ms->n > 1) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001763 Py_ssize_t n = ms->n - 2;
Tim Peterse05f65a2002-08-10 05:21:15 +00001764 if (n > 0 && p[n-1].len < p[n+1].len)
Tim Petersa64dc242002-08-01 02:13:36 +00001765 --n;
1766 if (merge_at(ms, n) < 0)
1767 return -1;
1768 }
1769 return 0;
1770}
1771
1772/* Compute a good value for the minimum run length; natural runs shorter
1773 * than this are boosted artificially via binary insertion.
1774 *
1775 * If n < 64, return n (it's too small to bother with fancy stuff).
1776 * Else if n is an exact power of 2, return 32.
1777 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1778 * strictly less than, an exact power of 2.
1779 *
1780 * See listsort.txt for more info.
1781 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001782static Py_ssize_t
1783merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001784{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001785 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001786
1787 assert(n >= 0);
1788 while (n >= 64) {
1789 r |= n & 1;
1790 n >>= 1;
1791 }
1792 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001793}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001794
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001795/* Special wrapper to support stable sorting using the decorate-sort-undecorate
Andrew M. Kuchling55be9ea2004-09-10 12:59:54 +00001796 pattern. Holds a key which is used for comparisons and the original record
Tim Petersb38e2b62004-07-29 02:29:26 +00001797 which is returned during the undecorate phase. By exposing only the key
1798 during comparisons, the underlying sort stability characteristics are left
1799 unchanged. Also, if a custom comparison function is used, it will only see
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001800 the key instead of a full record. */
1801
1802typedef struct {
1803 PyObject_HEAD
1804 PyObject *key;
1805 PyObject *value;
1806} sortwrapperobject;
1807
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001808PyDoc_STRVAR(sortwrapper_doc, "Object wrapper with a custom sort key.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001809static PyObject *
1810sortwrapper_richcompare(sortwrapperobject *, sortwrapperobject *, int);
1811static void
1812sortwrapper_dealloc(sortwrapperobject *);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001813
1814static PyTypeObject sortwrapper_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001815 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001816 "sortwrapper", /* tp_name */
1817 sizeof(sortwrapperobject), /* tp_basicsize */
1818 0, /* tp_itemsize */
1819 /* methods */
1820 (destructor)sortwrapper_dealloc, /* tp_dealloc */
1821 0, /* tp_print */
1822 0, /* tp_getattr */
1823 0, /* tp_setattr */
1824 0, /* tp_compare */
1825 0, /* tp_repr */
1826 0, /* tp_as_number */
1827 0, /* tp_as_sequence */
1828 0, /* tp_as_mapping */
1829 0, /* tp_hash */
1830 0, /* tp_call */
1831 0, /* tp_str */
1832 PyObject_GenericGetAttr, /* tp_getattro */
1833 0, /* tp_setattro */
1834 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001835 Py_TPFLAGS_DEFAULT, /* tp_flags */
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001836 sortwrapper_doc, /* tp_doc */
1837 0, /* tp_traverse */
1838 0, /* tp_clear */
1839 (richcmpfunc)sortwrapper_richcompare, /* tp_richcompare */
1840};
1841
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001842
1843static PyObject *
1844sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, int op)
1845{
1846 if (!PyObject_TypeCheck(b, &sortwrapper_type)) {
1847 PyErr_SetString(PyExc_TypeError,
1848 "expected a sortwrapperobject");
1849 return NULL;
1850 }
1851 return PyObject_RichCompare(a->key, b->key, op);
1852}
1853
1854static void
1855sortwrapper_dealloc(sortwrapperobject *so)
1856{
1857 Py_XDECREF(so->key);
1858 Py_XDECREF(so->value);
1859 PyObject_Del(so);
1860}
1861
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001862/* Returns a new reference to a sortwrapper.
1863 Consumes the references to the two underlying objects. */
1864
1865static PyObject *
1866build_sortwrapper(PyObject *key, PyObject *value)
1867{
1868 sortwrapperobject *so;
Tim Petersb38e2b62004-07-29 02:29:26 +00001869
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001870 so = PyObject_New(sortwrapperobject, &sortwrapper_type);
1871 if (so == NULL)
1872 return NULL;
1873 so->key = key;
1874 so->value = value;
1875 return (PyObject *)so;
1876}
1877
1878/* Returns a new reference to the value underlying the wrapper. */
1879static PyObject *
1880sortwrapper_getvalue(PyObject *so)
1881{
1882 PyObject *value;
1883
1884 if (!PyObject_TypeCheck(so, &sortwrapper_type)) {
Tim Petersb38e2b62004-07-29 02:29:26 +00001885 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001886 "expected a sortwrapperobject");
1887 return NULL;
1888 }
1889 value = ((sortwrapperobject *)so)->value;
1890 Py_INCREF(value);
1891 return value;
1892}
1893
1894/* Wrapper for user specified cmp functions in combination with a
1895 specified key function. Makes sure the cmp function is presented
1896 with the actual key instead of the sortwrapper */
1897
1898typedef struct {
1899 PyObject_HEAD
1900 PyObject *func;
1901} cmpwrapperobject;
1902
1903static void
1904cmpwrapper_dealloc(cmpwrapperobject *co)
1905{
1906 Py_XDECREF(co->func);
1907 PyObject_Del(co);
1908}
1909
1910static PyObject *
1911cmpwrapper_call(cmpwrapperobject *co, PyObject *args, PyObject *kwds)
1912{
1913 PyObject *x, *y, *xx, *yy;
1914
1915 if (!PyArg_UnpackTuple(args, "", 2, 2, &x, &y))
1916 return NULL;
1917 if (!PyObject_TypeCheck(x, &sortwrapper_type) ||
Raymond Hettingerae4a2992003-10-16 17:16:30 +00001918 !PyObject_TypeCheck(y, &sortwrapper_type)) {
Tim Petersb38e2b62004-07-29 02:29:26 +00001919 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001920 "expected a sortwrapperobject");
1921 return NULL;
1922 }
1923 xx = ((sortwrapperobject *)x)->key;
1924 yy = ((sortwrapperobject *)y)->key;
1925 return PyObject_CallFunctionObjArgs(co->func, xx, yy, NULL);
1926}
1927
1928PyDoc_STRVAR(cmpwrapper_doc, "cmp() wrapper for sort with custom keys.");
1929
1930static PyTypeObject cmpwrapper_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001931 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001932 "cmpwrapper", /* tp_name */
1933 sizeof(cmpwrapperobject), /* tp_basicsize */
1934 0, /* tp_itemsize */
1935 /* methods */
1936 (destructor)cmpwrapper_dealloc, /* tp_dealloc */
1937 0, /* tp_print */
1938 0, /* tp_getattr */
1939 0, /* tp_setattr */
1940 0, /* tp_compare */
1941 0, /* tp_repr */
1942 0, /* tp_as_number */
1943 0, /* tp_as_sequence */
1944 0, /* tp_as_mapping */
1945 0, /* tp_hash */
1946 (ternaryfunc)cmpwrapper_call, /* tp_call */
1947 0, /* tp_str */
1948 PyObject_GenericGetAttr, /* tp_getattro */
1949 0, /* tp_setattro */
1950 0, /* tp_as_buffer */
1951 Py_TPFLAGS_DEFAULT, /* tp_flags */
1952 cmpwrapper_doc, /* tp_doc */
1953};
1954
1955static PyObject *
1956build_cmpwrapper(PyObject *cmpfunc)
1957{
1958 cmpwrapperobject *co;
Tim Petersb38e2b62004-07-29 02:29:26 +00001959
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001960 co = PyObject_New(cmpwrapperobject, &cmpwrapper_type);
1961 if (co == NULL)
1962 return NULL;
1963 Py_INCREF(cmpfunc);
1964 co->func = cmpfunc;
1965 return (PyObject *)co;
1966}
1967
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001968static int
1969is_default_cmp(PyObject *cmpfunc)
1970{
1971 PyCFunctionObject *f;
1972 if (cmpfunc == NULL || cmpfunc == Py_None)
1973 return 1;
1974 if (!PyCFunction_Check(cmpfunc))
1975 return 0;
Guido van Rossumf1624cd2006-08-24 23:43:52 +00001976 f = (PyCFunctionObject *)cmpfunc;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001977 if (f->m_self != NULL)
1978 return 0;
1979 if (!PyString_Check(f->m_module))
1980 return 0;
1981 if (strcmp(PyString_AS_STRING(f->m_module), "__builtin__") != 0)
1982 return 0;
1983 if (strcmp(f->m_ml->ml_name, "cmp") != 0)
1984 return 0;
1985 return 1;
1986}
1987
Tim Petersa64dc242002-08-01 02:13:36 +00001988/* An adaptive, stable, natural mergesort. See listsort.txt.
1989 * Returns Py_None on success, NULL on error. Even in case of error, the
1990 * list will be some permutation of its input state (nothing is lost or
1991 * duplicated).
1992 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001993static PyObject *
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001994listsort(PyListObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001995{
Tim Petersa64dc242002-08-01 02:13:36 +00001996 MergeState ms;
1997 PyObject **lo, **hi;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001998 Py_ssize_t nremaining;
1999 Py_ssize_t minrun;
2000 Py_ssize_t saved_ob_size, saved_allocated;
Tim Petersb9099c32002-11-12 22:08:10 +00002001 PyObject **saved_ob_item;
Armin Rigo93677f02004-07-29 12:40:23 +00002002 PyObject **final_ob_item;
Tim Petersa64dc242002-08-01 02:13:36 +00002003 PyObject *compare = NULL;
2004 PyObject *result = NULL; /* guilty until proved innocent */
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002005 int reverse = 0;
2006 PyObject *keyfunc = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002007 Py_ssize_t i;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002008 PyObject *key, *value, *kvpair;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002009 static char *kwlist[] = {"cmp", "key", "reverse", 0};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002010
Tim Petersa64dc242002-08-01 02:13:36 +00002011 assert(self != NULL);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002012 assert (PyList_Check(self));
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002013 if (args != NULL) {
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002014 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi:sort",
2015 kwlist, &compare, &keyfunc, &reverse))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002016 return NULL;
2017 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002018 if (is_default_cmp(compare))
Skip Montanaro4abd5f02003-01-02 20:51:08 +00002019 compare = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002020 if (keyfunc == Py_None)
2021 keyfunc = NULL;
2022 if (compare != NULL && keyfunc != NULL) {
2023 compare = build_cmpwrapper(compare);
2024 if (compare == NULL)
Hye-Shik Chang19cb1932003-12-10 07:31:08 +00002025 return NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002026 } else
2027 Py_XINCREF(compare);
2028
Tim Petersb9099c32002-11-12 22:08:10 +00002029 /* The list is temporarily made empty, so that mutations performed
2030 * by comparison functions can't affect the slice of memory we're
2031 * sorting (allowing mutations during sorting is a core-dump
2032 * factory, since ob_item may change).
2033 */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002034 saved_ob_size = Py_Size(self);
Tim Petersb9099c32002-11-12 22:08:10 +00002035 saved_ob_item = self->ob_item;
Raymond Hettinger4bb95402004-02-13 11:36:39 +00002036 saved_allocated = self->allocated;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002037 Py_Size(self) = 0;
Tim Peters51b4ade2004-07-29 04:07:15 +00002038 self->ob_item = NULL;
Armin Rigo93677f02004-07-29 12:40:23 +00002039 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002040
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002041 if (keyfunc != NULL) {
2042 for (i=0 ; i < saved_ob_size ; i++) {
2043 value = saved_ob_item[i];
Tim Petersb38e2b62004-07-29 02:29:26 +00002044 key = PyObject_CallFunctionObjArgs(keyfunc, value,
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002045 NULL);
2046 if (key == NULL) {
2047 for (i=i-1 ; i>=0 ; i--) {
2048 kvpair = saved_ob_item[i];
2049 value = sortwrapper_getvalue(kvpair);
2050 saved_ob_item[i] = value;
2051 Py_DECREF(kvpair);
2052 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002053 goto dsu_fail;
2054 }
2055 kvpair = build_sortwrapper(key, value);
2056 if (kvpair == NULL)
2057 goto dsu_fail;
2058 saved_ob_item[i] = kvpair;
2059 }
2060 }
2061
2062 /* Reverse sort stability achieved by initially reversing the list,
2063 applying a stable forward sort, then reversing the final result. */
2064 if (reverse && saved_ob_size > 1)
2065 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
2066
2067 merge_init(&ms, compare);
2068
Tim Petersb9099c32002-11-12 22:08:10 +00002069 nremaining = saved_ob_size;
Tim Petersa64dc242002-08-01 02:13:36 +00002070 if (nremaining < 2)
2071 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002072
Tim Petersa64dc242002-08-01 02:13:36 +00002073 /* March over the array once, left to right, finding natural runs,
2074 * and extending short natural runs to minrun elements.
2075 */
Tim Petersb9099c32002-11-12 22:08:10 +00002076 lo = saved_ob_item;
Tim Petersa64dc242002-08-01 02:13:36 +00002077 hi = lo + nremaining;
2078 minrun = merge_compute_minrun(nremaining);
2079 do {
2080 int descending;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002081 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002082
Tim Petersa64dc242002-08-01 02:13:36 +00002083 /* Identify next run. */
2084 n = count_run(lo, hi, compare, &descending);
2085 if (n < 0)
2086 goto fail;
2087 if (descending)
2088 reverse_slice(lo, lo + n);
2089 /* If short, extend to min(minrun, nremaining). */
2090 if (n < minrun) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002091 const Py_ssize_t force = nremaining <= minrun ?
Tim Petersa64dc242002-08-01 02:13:36 +00002092 nremaining : minrun;
2093 if (binarysort(lo, lo + force, lo + n, compare) < 0)
2094 goto fail;
2095 n = force;
2096 }
2097 /* Push run onto pending-runs stack, and maybe merge. */
2098 assert(ms.n < MAX_MERGE_PENDING);
Tim Peterse05f65a2002-08-10 05:21:15 +00002099 ms.pending[ms.n].base = lo;
2100 ms.pending[ms.n].len = n;
Tim Petersa64dc242002-08-01 02:13:36 +00002101 ++ms.n;
2102 if (merge_collapse(&ms) < 0)
2103 goto fail;
2104 /* Advance to find next run. */
2105 lo += n;
2106 nremaining -= n;
2107 } while (nremaining);
2108 assert(lo == hi);
Tim Peters330f9e92002-07-19 07:05:44 +00002109
Tim Petersa64dc242002-08-01 02:13:36 +00002110 if (merge_force_collapse(&ms) < 0)
2111 goto fail;
2112 assert(ms.n == 1);
Tim Petersb9099c32002-11-12 22:08:10 +00002113 assert(ms.pending[0].base == saved_ob_item);
2114 assert(ms.pending[0].len == saved_ob_size);
Tim Petersa64dc242002-08-01 02:13:36 +00002115
2116succeed:
2117 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002118fail:
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002119 if (keyfunc != NULL) {
2120 for (i=0 ; i < saved_ob_size ; i++) {
2121 kvpair = saved_ob_item[i];
2122 value = sortwrapper_getvalue(kvpair);
2123 saved_ob_item[i] = value;
2124 Py_DECREF(kvpair);
2125 }
2126 }
2127
Armin Rigo93677f02004-07-29 12:40:23 +00002128 if (self->allocated != -1 && result != NULL) {
Tim Peters51b4ade2004-07-29 04:07:15 +00002129 /* The user mucked with the list during the sort,
2130 * and we don't already have another error to report.
2131 */
2132 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2133 result = NULL;
Tim Petersb9099c32002-11-12 22:08:10 +00002134 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002135
2136 if (reverse && saved_ob_size > 1)
2137 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
2138
2139 merge_freemem(&ms);
2140
2141dsu_fail:
Armin Rigo93677f02004-07-29 12:40:23 +00002142 final_ob_item = self->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002143 i = Py_Size(self);
2144 Py_Size(self) = saved_ob_size;
Tim Petersb9099c32002-11-12 22:08:10 +00002145 self->ob_item = saved_ob_item;
Raymond Hettinger4bb95402004-02-13 11:36:39 +00002146 self->allocated = saved_allocated;
Armin Rigo93677f02004-07-29 12:40:23 +00002147 if (final_ob_item != NULL) {
2148 /* we cannot use list_clear() for this because it does not
2149 guarantee that the list is really empty when it returns */
2150 while (--i >= 0) {
2151 Py_XDECREF(final_ob_item[i]);
2152 }
2153 PyMem_FREE(final_ob_item);
2154 }
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002155 Py_XDECREF(compare);
Tim Petersa64dc242002-08-01 02:13:36 +00002156 Py_XINCREF(result);
2157 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002158}
Tim Peters330f9e92002-07-19 07:05:44 +00002159#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002160#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002161
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002162int
Fred Drakea2f55112000-07-09 15:16:51 +00002163PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002164{
2165 if (v == NULL || !PyList_Check(v)) {
2166 PyErr_BadInternalCall();
2167 return -1;
2168 }
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002169 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002170 if (v == NULL)
2171 return -1;
2172 Py_DECREF(v);
2173 return 0;
2174}
2175
Guido van Rossumb86c5492001-02-12 22:06:02 +00002176static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002177listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002178{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002179 if (Py_Size(self) > 1)
2180 reverse_slice(self->ob_item, self->ob_item + Py_Size(self));
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +00002181 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002182}
2183
Guido van Rossum84c76f51990-10-30 13:32:20 +00002184int
Fred Drakea2f55112000-07-09 15:16:51 +00002185PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002186{
Tim Peters6063e262002-08-08 01:06:39 +00002187 PyListObject *self = (PyListObject *)v;
2188
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002189 if (v == NULL || !PyList_Check(v)) {
2190 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002191 return -1;
2192 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002193 if (Py_Size(self) > 1)
2194 reverse_slice(self->ob_item, self->ob_item + Py_Size(self));
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002195 return 0;
2196}
2197
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002198PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002199PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002200{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002201 PyObject *w;
2202 PyObject **p;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002203 Py_ssize_t n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002204 if (v == NULL || !PyList_Check(v)) {
2205 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002206 return NULL;
2207 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002208 n = Py_Size(v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002209 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002210 if (w == NULL)
2211 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002212 p = ((PyTupleObject *)w)->ob_item;
Thomas Wouters334fb892000-07-25 12:56:38 +00002213 memcpy((void *)p,
2214 (void *)((PyListObject *)v)->ob_item,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002215 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002216 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002217 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002218 p++;
2219 }
2220 return w;
2221}
2222
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002223static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002224listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002225{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002226 Py_ssize_t i, start=0, stop=Py_Size(self);
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002227 PyObject *v;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002228
Walter Dörwalde8049bef2003-06-17 19:27:39 +00002229 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2230 _PyEval_SliceIndex, &start,
2231 _PyEval_SliceIndex, &stop))
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002232 return NULL;
Guido van Rossum2743d872003-06-17 14:25:14 +00002233 if (start < 0) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002234 start += Py_Size(self);
Guido van Rossum2743d872003-06-17 14:25:14 +00002235 if (start < 0)
2236 start = 0;
2237 }
2238 if (stop < 0) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002239 stop += Py_Size(self);
Guido van Rossum2743d872003-06-17 14:25:14 +00002240 if (stop < 0)
2241 stop = 0;
2242 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002243 for (i = start; i < stop && i < Py_Size(self); i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002244 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2245 if (cmp > 0)
Martin v. Löwis18e16552006-02-15 17:27:45 +00002246 return PyInt_FromSsize_t(i);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002247 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002248 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002249 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002250 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00002251 return NULL;
2252}
2253
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002254static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002255listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002256{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002257 Py_ssize_t count = 0;
2258 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002259
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002260 for (i = 0; i < Py_Size(self); i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002261 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2262 if (cmp > 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002263 count++;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002264 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002265 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00002266 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002267 return PyInt_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002268}
2269
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002270static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002271listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002272{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002273 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002274
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002275 for (i = 0; i < Py_Size(self); i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002276 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2277 if (cmp > 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002278 if (list_ass_slice(self, i, i+1,
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +00002279 (PyObject *)NULL) == 0)
2280 Py_RETURN_NONE;
2281 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002282 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002283 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002284 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002285 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002286 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00002287 return NULL;
2288}
2289
Jeremy Hylton8caad492000-06-23 14:18:11 +00002290static int
2291list_traverse(PyListObject *o, visitproc visit, void *arg)
2292{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002293 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002294
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002295 for (i = Py_Size(o); --i >= 0; )
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002296 Py_VISIT(o->ob_item[i]);
Jeremy Hylton8caad492000-06-23 14:18:11 +00002297 return 0;
2298}
2299
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002300static PyObject *
2301list_richcompare(PyObject *v, PyObject *w, int op)
2302{
2303 PyListObject *vl, *wl;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002304 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002305
2306 if (!PyList_Check(v) || !PyList_Check(w)) {
2307 Py_INCREF(Py_NotImplemented);
2308 return Py_NotImplemented;
2309 }
2310
2311 vl = (PyListObject *)v;
2312 wl = (PyListObject *)w;
2313
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002314 if (Py_Size(vl) != Py_Size(wl) && (op == Py_EQ || op == Py_NE)) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002315 /* Shortcut: if the lengths differ, the lists differ */
2316 PyObject *res;
2317 if (op == Py_EQ)
2318 res = Py_False;
2319 else
2320 res = Py_True;
2321 Py_INCREF(res);
2322 return res;
2323 }
2324
2325 /* Search for the first index where items are different */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002326 for (i = 0; i < Py_Size(vl) && i < Py_Size(wl); i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002327 int k = PyObject_RichCompareBool(vl->ob_item[i],
2328 wl->ob_item[i], Py_EQ);
2329 if (k < 0)
2330 return NULL;
2331 if (!k)
2332 break;
2333 }
2334
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002335 if (i >= Py_Size(vl) || i >= Py_Size(wl)) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002336 /* No more items to compare -- compare sizes */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002337 Py_ssize_t vs = Py_Size(vl);
2338 Py_ssize_t ws = Py_Size(wl);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002339 int cmp;
2340 PyObject *res;
2341 switch (op) {
2342 case Py_LT: cmp = vs < ws; break;
Tim Peters6ee42342001-07-06 17:45:43 +00002343 case Py_LE: cmp = vs <= ws; break;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002344 case Py_EQ: cmp = vs == ws; break;
2345 case Py_NE: cmp = vs != ws; break;
2346 case Py_GT: cmp = vs > ws; break;
2347 case Py_GE: cmp = vs >= ws; break;
2348 default: return NULL; /* cannot happen */
2349 }
2350 if (cmp)
2351 res = Py_True;
2352 else
2353 res = Py_False;
2354 Py_INCREF(res);
2355 return res;
2356 }
2357
2358 /* We have an item that differs -- shortcuts for EQ/NE */
2359 if (op == Py_EQ) {
2360 Py_INCREF(Py_False);
2361 return Py_False;
2362 }
2363 if (op == Py_NE) {
2364 Py_INCREF(Py_True);
2365 return Py_True;
2366 }
2367
2368 /* Compare the final item again using the proper operator */
2369 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
2370}
2371
Tim Peters6d6c1a32001-08-02 04:15:00 +00002372static int
2373list_init(PyListObject *self, PyObject *args, PyObject *kw)
2374{
2375 PyObject *arg = NULL;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002376 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002377
2378 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2379 return -1;
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002380
2381 /* Verify list invariants established by PyType_GenericAlloc() */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002382 assert(0 <= Py_Size(self));
2383 assert(Py_Size(self) <= self->allocated || self->allocated == -1);
Armin Rigoa37bbf22004-07-30 11:20:18 +00002384 assert(self->ob_item != NULL ||
2385 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002386
Raymond Hettinger90a39bf2004-02-15 03:57:00 +00002387 /* Empty previous contents */
Armin Rigo93677f02004-07-29 12:40:23 +00002388 if (self->ob_item != NULL) {
2389 (void)list_clear(self);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +00002390 }
2391 if (arg != NULL) {
2392 PyObject *rv = listextend(self, arg);
2393 if (rv == NULL)
2394 return -1;
2395 Py_DECREF(rv);
2396 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002397 return 0;
2398}
2399
Raymond Hettinger1021c442003-11-07 15:38:09 +00002400static PyObject *list_iter(PyObject *seq);
2401static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2402
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002403PyDoc_STRVAR(getitem_doc,
2404"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002405PyDoc_STRVAR(reversed_doc,
2406"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002407PyDoc_STRVAR(append_doc,
2408"L.append(object) -- append object to end");
2409PyDoc_STRVAR(extend_doc,
Raymond Hettingerf8bcfb12002-12-29 05:49:09 +00002410"L.extend(iterable) -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002411PyDoc_STRVAR(insert_doc,
2412"L.insert(index, object) -- insert object before index");
2413PyDoc_STRVAR(pop_doc,
2414"L.pop([index]) -> item -- remove and return item at index (default last)");
2415PyDoc_STRVAR(remove_doc,
2416"L.remove(value) -- remove first occurrence of value");
2417PyDoc_STRVAR(index_doc,
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002418"L.index(value, [start, [stop]]) -> integer -- return first index of value");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002419PyDoc_STRVAR(count_doc,
2420"L.count(value) -> integer -- return number of occurrences of value");
2421PyDoc_STRVAR(reverse_doc,
2422"L.reverse() -- reverse *IN PLACE*");
2423PyDoc_STRVAR(sort_doc,
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002424"L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\n\
2425cmp(x, y) -> -1, 0, 1");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002426
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002427static PyObject *list_subscript(PyListObject*, PyObject*);
2428
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002429static PyMethodDef list_methods[] = {
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002430 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
Raymond Hettinger1021c442003-11-07 15:38:09 +00002431 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002432 {"append", (PyCFunction)listappend, METH_O, append_doc},
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002433 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002434 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002435 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002436 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002437 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002438 {"count", (PyCFunction)listcount, METH_O, count_doc},
2439 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002440 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
Tim Petersa64dc242002-08-01 02:13:36 +00002441 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002442};
2443
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002444static PySequenceMethods list_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002445 (lenfunc)list_length, /* sq_length */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002446 (binaryfunc)list_concat, /* sq_concat */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002447 (ssizeargfunc)list_repeat, /* sq_repeat */
2448 (ssizeargfunc)list_item, /* sq_item */
2449 (ssizessizeargfunc)list_slice, /* sq_slice */
2450 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2451 (ssizessizeobjargproc)list_ass_slice, /* sq_ass_slice */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002452 (objobjproc)list_contains, /* sq_contains */
2453 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002454 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002455};
2456
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002457PyDoc_STRVAR(list_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002458"list() -> new list\n"
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002459"list(sequence) -> new list initialized from sequence's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002460
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002461static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002462list_subscript(PyListObject* self, PyObject* item)
2463{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002464 if (PyIndex_Check(item)) {
2465 Py_ssize_t i;
2466 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002467 if (i == -1 && PyErr_Occurred())
2468 return NULL;
2469 if (i < 0)
2470 i += PyList_GET_SIZE(self);
2471 return list_item(self, i);
2472 }
2473 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002474 Py_ssize_t start, stop, step, slicelength, cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002475 PyObject* result;
2476 PyObject* it;
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002477 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002478
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002479 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_Size(self),
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002480 &start, &stop, &step, &slicelength) < 0) {
2481 return NULL;
2482 }
2483
2484 if (slicelength <= 0) {
2485 return PyList_New(0);
2486 }
2487 else {
2488 result = PyList_New(slicelength);
2489 if (!result) return NULL;
2490
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002491 src = self->ob_item;
2492 dest = ((PyListObject *)result)->ob_item;
Tim Peters3b01a122002-07-19 02:35:45 +00002493 for (cur = start, i = 0; i < slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002494 cur += step, i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002495 it = src[cur];
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002496 Py_INCREF(it);
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002497 dest[i] = it;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002498 }
Tim Peters3b01a122002-07-19 02:35:45 +00002499
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002500 return result;
2501 }
2502 }
2503 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002504 PyErr_Format(PyExc_TypeError,
2505 "list indices must be integers, not %.200s",
2506 item->ob_type->tp_name);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002507 return NULL;
2508 }
2509}
2510
Tim Peters3b01a122002-07-19 02:35:45 +00002511static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002512list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2513{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002514 if (PyIndex_Check(item)) {
2515 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002516 if (i == -1 && PyErr_Occurred())
2517 return -1;
2518 if (i < 0)
2519 i += PyList_GET_SIZE(self);
2520 return list_ass_item(self, i, value);
2521 }
2522 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002523 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002524
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002525 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_Size(self),
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002526 &start, &stop, &step, &slicelength) < 0) {
2527 return -1;
2528 }
2529
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002530 /* treat L[slice(a,b)] = v _exactly_ like L[a:b] = v */
2531 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
2532 return list_ass_slice(self, start, stop, value);
2533
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002534 if (value == NULL) {
2535 /* delete slice */
Raymond Hettinger4bb95402004-02-13 11:36:39 +00002536 PyObject **garbage;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002537 Py_ssize_t cur, i;
Tim Peters3b01a122002-07-19 02:35:45 +00002538
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002539 if (slicelength <= 0)
2540 return 0;
2541
2542 if (step < 0) {
2543 stop = start + 1;
2544 start = stop + step*(slicelength - 1) - 1;
2545 step = -step;
2546 }
2547
2548 garbage = (PyObject**)
2549 PyMem_MALLOC(slicelength*sizeof(PyObject*));
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002550 if (!garbage) {
2551 PyErr_NoMemory();
2552 return -1;
2553 }
Tim Peters3b01a122002-07-19 02:35:45 +00002554
2555 /* drawing pictures might help
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002556 understand these for loops */
Guido van Rossum75a20b12002-06-11 12:22:28 +00002557 for (cur = start, i = 0;
2558 cur < stop;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002559 cur += step, i++) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002560 Py_ssize_t lim = step;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002561
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002562 garbage[i] = PyList_GET_ITEM(self, cur);
2563
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002564 if (cur + step >= Py_Size(self)) {
2565 lim = Py_Size(self) - cur - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002566 }
2567
Tim Petersb38e2b62004-07-29 02:29:26 +00002568 memmove(self->ob_item + cur - i,
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002569 self->ob_item + cur + 1,
2570 lim * sizeof(PyObject *));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002571 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002572
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002573 for (cur = start + slicelength*step + 1;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002574 cur < Py_Size(self); cur++) {
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002575 PyList_SET_ITEM(self, cur - slicelength,
2576 PyList_GET_ITEM(self, cur));
2577 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002578
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002579 Py_Size(self) -= slicelength;
2580 list_resize(self, Py_Size(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002581
2582 for (i = 0; i < slicelength; i++) {
2583 Py_DECREF(garbage[i]);
2584 }
2585 PyMem_FREE(garbage);
2586
2587 return 0;
2588 }
2589 else {
2590 /* assign slice */
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002591 PyObject **garbage, *ins, *seq, **seqitems, **selfitems;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002592 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002593
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002594 /* protect against a[::-1] = a */
Tim Peters3b01a122002-07-19 02:35:45 +00002595 if (self == (PyListObject*)value) {
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002596 seq = list_slice((PyListObject*)value, 0,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002597 PyList_GET_SIZE(value));
Tim Peters3b01a122002-07-19 02:35:45 +00002598 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002599 else {
Tim Petersb38e2b62004-07-29 02:29:26 +00002600 seq = PySequence_Fast(value,
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002601 "must assign iterable to extended slice");
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002602 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002603 if (!seq)
2604 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002605
2606 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2607 PyErr_Format(PyExc_ValueError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00002608 "attempt to assign sequence of size %zd to extended slice of size %zd",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00002609 PySequence_Fast_GET_SIZE(seq),
2610 slicelength);
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002611 Py_DECREF(seq);
2612 return -1;
2613 }
2614
2615 if (!slicelength) {
2616 Py_DECREF(seq);
2617 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002618 }
2619
2620 garbage = (PyObject**)
2621 PyMem_MALLOC(slicelength*sizeof(PyObject*));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002622 if (!garbage) {
2623 Py_DECREF(seq);
2624 PyErr_NoMemory();
2625 return -1;
2626 }
Tim Peters3b01a122002-07-19 02:35:45 +00002627
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002628 selfitems = self->ob_item;
Raymond Hettinger42bec932004-03-12 16:38:17 +00002629 seqitems = PySequence_Fast_ITEMS(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002630 for (cur = start, i = 0; i < slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002631 cur += step, i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002632 garbage[i] = selfitems[cur];
2633 ins = seqitems[i];
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002634 Py_INCREF(ins);
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002635 selfitems[cur] = ins;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002636 }
2637
2638 for (i = 0; i < slicelength; i++) {
2639 Py_DECREF(garbage[i]);
2640 }
Tim Peters3b01a122002-07-19 02:35:45 +00002641
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002642 PyMem_FREE(garbage);
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002643 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002644
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002645 return 0;
2646 }
Tim Peters3b01a122002-07-19 02:35:45 +00002647 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002648 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002649 PyErr_Format(PyExc_TypeError,
2650 "list indices must be integers, not %.200s",
2651 item->ob_type->tp_name);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002652 return -1;
2653 }
2654}
2655
2656static PyMappingMethods list_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002657 (lenfunc)list_length,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002658 (binaryfunc)list_subscript,
2659 (objobjargproc)list_ass_subscript
2660};
2661
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002662PyTypeObject PyList_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002663 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002664 "list",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002665 sizeof(PyListObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002666 0,
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002667 (destructor)list_dealloc, /* tp_dealloc */
2668 (printfunc)list_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002669 0, /* tp_getattr */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002670 0, /* tp_setattr */
2671 0, /* tp_compare */
2672 (reprfunc)list_repr, /* tp_repr */
2673 0, /* tp_as_number */
2674 &list_as_sequence, /* tp_as_sequence */
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002675 &list_as_mapping, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002676 0, /* tp_hash */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002677 0, /* tp_call */
2678 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002679 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002680 0, /* tp_setattro */
2681 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002682 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00002683 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002684 list_doc, /* tp_doc */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002685 (traverseproc)list_traverse, /* tp_traverse */
2686 (inquiry)list_clear, /* tp_clear */
2687 list_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002688 0, /* tp_weaklistoffset */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002689 list_iter, /* tp_iter */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002690 0, /* tp_iternext */
2691 list_methods, /* tp_methods */
2692 0, /* tp_members */
2693 0, /* tp_getset */
2694 0, /* tp_base */
2695 0, /* tp_dict */
2696 0, /* tp_descr_get */
2697 0, /* tp_descr_set */
2698 0, /* tp_dictoffset */
2699 (initproc)list_init, /* tp_init */
2700 PyType_GenericAlloc, /* tp_alloc */
2701 PyType_GenericNew, /* tp_new */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002702 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002703};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002704
2705
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002706/*********************** List Iterator **************************/
2707
2708typedef struct {
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002709 PyObject_HEAD
2710 long it_index;
Guido van Rossum86103ae2002-07-16 20:07:32 +00002711 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002712} listiterobject;
2713
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002714static PyObject *list_iter(PyObject *);
2715static void listiter_dealloc(listiterobject *);
2716static int listiter_traverse(listiterobject *, visitproc, void *);
2717static PyObject *listiter_next(listiterobject *);
2718static PyObject *listiter_len(listiterobject *);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002719
Armin Rigof5b3e362006-02-11 21:32:43 +00002720PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002721
2722static PyMethodDef listiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002723 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002724 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002725};
2726
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002727PyTypeObject PyListIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002728 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002729 "listiterator", /* tp_name */
2730 sizeof(listiterobject), /* tp_basicsize */
2731 0, /* tp_itemsize */
2732 /* methods */
2733 (destructor)listiter_dealloc, /* tp_dealloc */
2734 0, /* tp_print */
2735 0, /* tp_getattr */
2736 0, /* tp_setattr */
2737 0, /* tp_compare */
2738 0, /* tp_repr */
2739 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002740 0, /* tp_as_sequence */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002741 0, /* tp_as_mapping */
2742 0, /* tp_hash */
2743 0, /* tp_call */
2744 0, /* tp_str */
2745 PyObject_GenericGetAttr, /* tp_getattro */
2746 0, /* tp_setattro */
2747 0, /* tp_as_buffer */
2748 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2749 0, /* tp_doc */
2750 (traverseproc)listiter_traverse, /* tp_traverse */
2751 0, /* tp_clear */
2752 0, /* tp_richcompare */
2753 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002754 PyObject_SelfIter, /* tp_iter */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002755 (iternextfunc)listiter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002756 listiter_methods, /* tp_methods */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002757 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002758};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002759
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002760
2761static PyObject *
2762list_iter(PyObject *seq)
2763{
2764 listiterobject *it;
2765
2766 if (!PyList_Check(seq)) {
2767 PyErr_BadInternalCall();
2768 return NULL;
2769 }
2770 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2771 if (it == NULL)
2772 return NULL;
2773 it->it_index = 0;
2774 Py_INCREF(seq);
2775 it->it_seq = (PyListObject *)seq;
2776 _PyObject_GC_TRACK(it);
2777 return (PyObject *)it;
2778}
2779
2780static void
2781listiter_dealloc(listiterobject *it)
2782{
2783 _PyObject_GC_UNTRACK(it);
2784 Py_XDECREF(it->it_seq);
2785 PyObject_GC_Del(it);
2786}
2787
2788static int
2789listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2790{
2791 Py_VISIT(it->it_seq);
2792 return 0;
2793}
2794
2795static PyObject *
2796listiter_next(listiterobject *it)
2797{
2798 PyListObject *seq;
2799 PyObject *item;
2800
2801 assert(it != NULL);
2802 seq = it->it_seq;
2803 if (seq == NULL)
2804 return NULL;
2805 assert(PyList_Check(seq));
2806
2807 if (it->it_index < PyList_GET_SIZE(seq)) {
2808 item = PyList_GET_ITEM(seq, it->it_index);
2809 ++it->it_index;
2810 Py_INCREF(item);
2811 return item;
2812 }
2813
2814 Py_DECREF(seq);
2815 it->it_seq = NULL;
2816 return NULL;
2817}
2818
2819static PyObject *
2820listiter_len(listiterobject *it)
2821{
2822 Py_ssize_t len;
2823 if (it->it_seq) {
2824 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2825 if (len >= 0)
2826 return PyInt_FromSsize_t(len);
2827 }
2828 return PyInt_FromLong(0);
2829}
Raymond Hettinger1021c442003-11-07 15:38:09 +00002830/*********************** List Reverse Iterator **************************/
2831
2832typedef struct {
2833 PyObject_HEAD
Martin v. Löwiseb079f12006-02-16 14:32:27 +00002834 Py_ssize_t it_index;
Raymond Hettinger001f2282003-11-08 11:58:44 +00002835 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002836} listreviterobject;
2837
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002838static PyObject *list_reversed(PyListObject *, PyObject *);
2839static void listreviter_dealloc(listreviterobject *);
2840static int listreviter_traverse(listreviterobject *, visitproc, void *);
2841static PyObject *listreviter_next(listreviterobject *);
2842static Py_ssize_t listreviter_len(listreviterobject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002843
2844static PySequenceMethods listreviter_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002845 (lenfunc)listreviter_len, /* sq_length */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002846 0, /* sq_concat */
2847};
2848
Raymond Hettinger1021c442003-11-07 15:38:09 +00002849PyTypeObject PyListRevIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002850 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger1021c442003-11-07 15:38:09 +00002851 "listreverseiterator", /* tp_name */
2852 sizeof(listreviterobject), /* tp_basicsize */
2853 0, /* tp_itemsize */
2854 /* methods */
2855 (destructor)listreviter_dealloc, /* tp_dealloc */
2856 0, /* tp_print */
2857 0, /* tp_getattr */
2858 0, /* tp_setattr */
2859 0, /* tp_compare */
2860 0, /* tp_repr */
2861 0, /* tp_as_number */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002862 &listreviter_as_sequence, /* tp_as_sequence */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002863 0, /* tp_as_mapping */
2864 0, /* tp_hash */
2865 0, /* tp_call */
2866 0, /* tp_str */
2867 PyObject_GenericGetAttr, /* tp_getattro */
2868 0, /* tp_setattro */
2869 0, /* tp_as_buffer */
2870 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2871 0, /* tp_doc */
2872 (traverseproc)listreviter_traverse, /* tp_traverse */
2873 0, /* tp_clear */
2874 0, /* tp_richcompare */
2875 0, /* tp_weaklistoffset */
2876 PyObject_SelfIter, /* tp_iter */
2877 (iternextfunc)listreviter_next, /* tp_iternext */
2878 0,
2879};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002880
2881static PyObject *
2882list_reversed(PyListObject *seq, PyObject *unused)
2883{
2884 listreviterobject *it;
2885
2886 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2887 if (it == NULL)
2888 return NULL;
2889 assert(PyList_Check(seq));
2890 it->it_index = PyList_GET_SIZE(seq) - 1;
2891 Py_INCREF(seq);
2892 it->it_seq = seq;
2893 PyObject_GC_Track(it);
2894 return (PyObject *)it;
2895}
2896
2897static void
2898listreviter_dealloc(listreviterobject *it)
2899{
2900 PyObject_GC_UnTrack(it);
2901 Py_XDECREF(it->it_seq);
2902 PyObject_GC_Del(it);
2903}
2904
2905static int
2906listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2907{
2908 Py_VISIT(it->it_seq);
2909 return 0;
2910}
2911
2912static PyObject *
2913listreviter_next(listreviterobject *it)
2914{
2915 PyObject *item;
2916 Py_ssize_t index = it->it_index;
2917 PyListObject *seq = it->it_seq;
2918
2919 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2920 item = PyList_GET_ITEM(seq, index);
2921 it->it_index--;
2922 Py_INCREF(item);
2923 return item;
2924 }
2925 it->it_index = -1;
2926 if (seq != NULL) {
2927 it->it_seq = NULL;
2928 Py_DECREF(seq);
2929 }
2930 return NULL;
2931}
2932
2933static Py_ssize_t
2934listreviter_len(listreviterobject *it)
2935{
2936 Py_ssize_t len = it->it_index + 1;
2937 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2938 return 0;
2939 return len;
2940}
2941