blob: 8ba317a0e12f020f856852b5c4b8d0fbf4a729c9 [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
Raymond Hettingerd4ff7412004-03-15 09:01:31 +000025list_resize(PyListObject *self, int 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;
29 int 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);
Raymond Hettinger4bb95402004-02-13 11:36:39 +000037 self->ob_size = newsize;
38 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;
61 self->ob_size = 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 *
Fred Drakea2f55112000-07-09 15:16:51 +000085PyList_New(int 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);
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000111 if (op->ob_item == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112 return PyErr_NoMemory();
Tim Peters3986d4e2004-07-29 02:28:42 +0000113 memset(op->ob_item, 0, nbytes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114 }
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000115 op->ob_size = size;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000116 op->allocated = size;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000117 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000118 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119}
120
121int
Fred Drakea2f55112000-07-09 15:16:51 +0000122PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000124 if (!PyList_Check(op)) {
125 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 return -1;
127 }
128 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129 return ((PyListObject *)op) -> ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130}
131
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000132static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000133
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000135PyList_GetItem(PyObject *op, int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000137 if (!PyList_Check(op)) {
138 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139 return NULL;
140 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000141 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000142 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000143 indexerr = PyString_FromString(
144 "list index out of range");
145 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146 return NULL;
147 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149}
150
151int
Fred Drakea2f55112000-07-09 15:16:51 +0000152PyList_SetItem(register PyObject *op, register int i,
153 register PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000155 register PyObject *olditem;
156 register PyObject **p;
157 if (!PyList_Check(op)) {
158 Py_XDECREF(newitem);
159 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000160 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
163 Py_XDECREF(newitem);
164 PyErr_SetString(PyExc_IndexError,
165 "list assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000166 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000168 p = ((PyListObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000169 olditem = *p;
170 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000171 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172 return 0;
173}
174
175static int
Fred Drakea2f55112000-07-09 15:16:51 +0000176ins1(PyListObject *self, int where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177{
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000178 int i, n = self->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179 PyObject **items;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000180 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000181 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000182 return -1;
183 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000184 if (n == INT_MAX) {
Trent Micka5846642000-08-13 22:47:45 +0000185 PyErr_SetString(PyExc_OverflowError,
186 "cannot add more objects to list");
187 return -1;
188 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000189
Raymond Hettingerd4ff7412004-03-15 09:01:31 +0000190 if (list_resize(self, n+1) == -1)
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000191 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000192
Guido van Rossum3a3cca52003-04-14 20:58:14 +0000193 if (where < 0) {
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000194 where += n;
Guido van Rossum3a3cca52003-04-14 20:58:14 +0000195 if (where < 0)
196 where = 0;
197 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000198 if (where > n)
199 where = n;
200 items = self->ob_item;
201 for (i = n; --i >= where; )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202 items[i+1] = items[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204 items[where] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205 return 0;
206}
207
208int
Fred Drakea2f55112000-07-09 15:16:51 +0000209PyList_Insert(PyObject *op, int where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211 if (!PyList_Check(op)) {
212 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000213 return -1;
214 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216}
217
Raymond Hettinger40a03822004-04-12 13:05:09 +0000218static int
219app1(PyListObject *self, PyObject *v)
220{
221 int n = PyList_GET_SIZE(self);
222
223 assert (v != NULL);
224 if (n == INT_MAX) {
225 PyErr_SetString(PyExc_OverflowError,
226 "cannot add more objects to list");
227 return -1;
228 }
229
230 if (list_resize(self, n+1) == -1)
231 return -1;
232
233 Py_INCREF(v);
234 PyList_SET_ITEM(self, n, v);
235 return 0;
236}
237
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238int
Fred Drakea2f55112000-07-09 15:16:51 +0000239PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240{
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000241 if (PyList_Check(op) && (newitem != NULL))
242 return app1((PyListObject *)op, newitem);
243 PyErr_BadInternalCall();
244 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245}
246
247/* Methods */
248
249static void
Fred Drakea2f55112000-07-09 15:16:51 +0000250list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251{
252 int i;
Guido van Rossumff413af2002-03-28 20:34:59 +0000253 PyObject_GC_UnTrack(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000254 Py_TRASHCAN_SAFE_BEGIN(op)
Jack Jansen7874d1f1995-01-19 12:09:27 +0000255 if (op->ob_item != NULL) {
Guido van Rossumfa717011999-06-09 15:19:34 +0000256 /* Do it backwards, for Christian Tismer.
257 There's a simple test case where somehow this reduces
258 thrashing when a *very* large list is created and
259 immediately deleted. */
260 i = op->ob_size;
261 while (--i >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000262 Py_XDECREF(op->ob_item[i]);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000263 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000264 PyMem_FREE(op->ob_item);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000265 }
Raymond Hettinger0468e412004-05-05 05:37:53 +0000266 if (num_free_lists < MAXFREELISTS && PyList_CheckExact(op))
267 free_lists[num_free_lists++] = op;
Tim Petersb38e2b62004-07-29 02:29:26 +0000268 else
269 op->ob_type->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000270 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271}
272
Guido van Rossum90933611991-06-07 16:10:43 +0000273static int
Fred Drakea2f55112000-07-09 15:16:51 +0000274list_print(PyListObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275{
276 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000277
278 i = Py_ReprEnter((PyObject*)op);
279 if (i != 0) {
280 if (i < 0)
281 return i;
282 fprintf(fp, "[...]");
283 return 0;
284 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285 fprintf(fp, "[");
Guido van Rossum90933611991-06-07 16:10:43 +0000286 for (i = 0; i < op->ob_size; i++) {
287 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288 fprintf(fp, ", ");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000289 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
290 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000291 return -1;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000292 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000293 }
294 fprintf(fp, "]");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000295 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000296 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297}
298
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000299static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000300list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000301{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302 int i;
Tim Petersa7259592001-06-16 05:11:17 +0000303 PyObject *s, *temp;
304 PyObject *pieces = NULL, *result = NULL;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000305
306 i = Py_ReprEnter((PyObject*)v);
307 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000308 return i > 0 ? PyString_FromString("[...]") : NULL;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000309 }
Tim Petersa7259592001-06-16 05:11:17 +0000310
311 if (v->ob_size == 0) {
312 result = PyString_FromString("[]");
313 goto Done;
314 }
315
316 pieces = PyList_New(0);
317 if (pieces == NULL)
318 goto Done;
319
320 /* Do repr() on each element. Note that this may mutate the list,
321 so must refetch the list size on each iteration. */
322 for (i = 0; i < v->ob_size; ++i) {
323 int status;
324 s = PyObject_Repr(v->ob_item[i]);
325 if (s == NULL)
326 goto Done;
327 status = PyList_Append(pieces, s);
328 Py_DECREF(s); /* append created a new ref */
329 if (status < 0)
330 goto Done;
331 }
332
333 /* Add "[]" decorations to the first and last items. */
334 assert(PyList_GET_SIZE(pieces) > 0);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000335 s = PyString_FromString("[");
Tim Petersa7259592001-06-16 05:11:17 +0000336 if (s == NULL)
337 goto Done;
338 temp = PyList_GET_ITEM(pieces, 0);
339 PyString_ConcatAndDel(&s, temp);
340 PyList_SET_ITEM(pieces, 0, s);
341 if (s == NULL)
342 goto Done;
343
344 s = PyString_FromString("]");
345 if (s == NULL)
346 goto Done;
347 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
348 PyString_ConcatAndDel(&temp, s);
349 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
350 if (temp == NULL)
351 goto Done;
352
353 /* Paste them all together with ", " between. */
354 s = PyString_FromString(", ");
355 if (s == NULL)
356 goto Done;
357 result = _PyString_Join(s, pieces);
Tim Peters3b01a122002-07-19 02:35:45 +0000358 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000359
360Done:
361 Py_XDECREF(pieces);
Guido van Rossumfb376de1998-04-10 22:47:27 +0000362 Py_ReprLeave((PyObject *)v);
Tim Petersa7259592001-06-16 05:11:17 +0000363 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364}
365
366static int
Fred Drakea2f55112000-07-09 15:16:51 +0000367list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000368{
369 return a->ob_size;
370}
371
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000372static int
Fred Drakea2f55112000-07-09 15:16:51 +0000373list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000374{
Raymond Hettingeraae59992002-09-05 14:23:49 +0000375 int i, cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000376
Raymond Hettingeraae59992002-09-05 14:23:49 +0000377 for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i)
378 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
Guido van Rossum65e1cea2001-01-17 22:11:59 +0000379 Py_EQ);
Neal Norwitzbb9c5f52002-09-05 21:32:55 +0000380 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000381}
382
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000384list_item(PyListObject *a, int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385{
386 if (i < 0 || i >= a->ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000387 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000388 indexerr = PyString_FromString(
389 "list index out of range");
390 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391 return NULL;
392 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394 return a->ob_item[i];
395}
396
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000398list_slice(PyListObject *a, int ilow, int ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400 PyListObject *np;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000401 PyObject **src, **dest;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000402 int i, len;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403 if (ilow < 0)
404 ilow = 0;
405 else if (ilow > a->ob_size)
406 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407 if (ihigh < ilow)
408 ihigh = ilow;
409 else if (ihigh > a->ob_size)
410 ihigh = a->ob_size;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000411 len = ihigh - ilow;
412 np = (PyListObject *) PyList_New(len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413 if (np == NULL)
414 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000415
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000416 src = a->ob_item + ilow;
417 dest = np->ob_item;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000418 for (i = 0; i < len; i++) {
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000419 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000420 Py_INCREF(v);
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000421 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000422 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000423 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424}
425
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000426PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000427PyList_GetSlice(PyObject *a, int ilow, int ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000428{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000429 if (!PyList_Check(a)) {
430 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000431 return NULL;
432 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000433 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000434}
435
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000436static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000437list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438{
439 int size;
440 int i;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000441 PyObject **src, **dest;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442 PyListObject *np;
443 if (!PyList_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000444 PyErr_Format(PyExc_TypeError,
Fred Drake914a2ed2000-06-01 14:31:03 +0000445 "can only concatenate list (not \"%.200s\") to list",
446 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447 return NULL;
448 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000449#define b ((PyListObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450 size = a->ob_size + b->ob_size;
Guido van Rossuma5c0e6d2002-10-11 21:05:56 +0000451 if (size < 0)
452 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000453 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000454 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000455 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000457 src = a->ob_item;
458 dest = np->ob_item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459 for (i = 0; i < a->ob_size; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000460 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000462 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000464 src = b->ob_item;
465 dest = np->ob_item + a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466 for (i = 0; i < b->ob_size; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000467 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000468 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000469 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000470 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472#undef b
473}
474
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000476list_repeat(PyListObject *a, int n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000477{
478 int i, j;
479 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000480 PyListObject *np;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000481 PyObject **p, **items;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000482 PyObject *elem;
Guido van Rossumed98d481991-03-06 13:07:53 +0000483 if (n < 0)
484 n = 0;
485 size = a->ob_size * n;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000486 if (size == 0)
487 return PyList_New(0);
Guido van Rossumbfa5a142002-10-11 23:39:35 +0000488 if (n && size/n != a->ob_size)
Guido van Rossuma5c0e6d2002-10-11 21:05:56 +0000489 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000490 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000491 if (np == NULL)
492 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000493
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000494 items = np->ob_item;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000495 if (a->ob_size == 1) {
496 elem = a->ob_item[0];
497 for (i = 0; i < n; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000498 items[i] = elem;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000499 Py_INCREF(elem);
500 }
501 return (PyObject *) np;
502 }
Guido van Rossumed98d481991-03-06 13:07:53 +0000503 p = np->ob_item;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000504 items = a->ob_item;
Guido van Rossumed98d481991-03-06 13:07:53 +0000505 for (i = 0; i < n; i++) {
506 for (j = 0; j < a->ob_size; j++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000507 *p = items[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000508 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000509 p++;
510 }
511 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000512 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000513}
514
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000515static int
Armin Rigo93677f02004-07-29 12:40:23 +0000516list_clear(PyListObject *a)
517{
518 int i;
519 PyObject **item = a->ob_item;
520 if (item != NULL) {
521 /* Because XDECREF can recursively invoke operations on
522 this list, we make it empty first. */
523 i = a->ob_size;
524 a->ob_size = 0;
525 a->ob_item = NULL;
526 a->allocated = 0;
527 while (--i >= 0) {
528 Py_XDECREF(item[i]);
529 }
530 PyMem_FREE(item);
531 }
532 /* Never fails; the return value can be ignored.
533 Note that there is no guarantee that the list is actually empty
534 at this point, because XDECREF may have populated it again! */
535 return 0;
536}
537
Tim Peters8fc4a912004-07-31 21:53:19 +0000538/* a[ilow:ihigh] = v if v != NULL.
539 * del a[ilow:ihigh] if v == NULL.
540 *
541 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
542 * guaranteed the call cannot fail.
543 */
Armin Rigo93677f02004-07-29 12:40:23 +0000544static int
Fred Drakea2f55112000-07-09 15:16:51 +0000545list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000547 /* Because [X]DECREF can recursively invoke list operations on
548 this list, we must postpone all [X]DECREF activity until
549 after the list is back in its canonical shape. Therefore
550 we must allocate an additional array, 'recycle', into which
551 we temporarily copy the items that are deleted from the
552 list. :-( */
Tim Peters73572222004-07-31 02:54:42 +0000553 PyObject *recycle_on_stack[8];
554 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000555 PyObject **item;
Raymond Hettingerf889e102004-03-09 08:04:33 +0000556 PyObject **vitem = NULL;
Michael W. Hudson5da854f2002-11-05 17:38:05 +0000557 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
Tim Peters8d9eb102004-07-31 02:24:20 +0000558 int n; /* # of elements in replacement list */
559 int norig; /* # of elements in list getting replaced */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560 int d; /* Change in size */
Tim Peters73572222004-07-31 02:54:42 +0000561 int k;
Tim Peters8d9eb102004-07-31 02:24:20 +0000562 size_t s;
563 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565 if (v == NULL)
566 n = 0;
Michael W. Hudson5da854f2002-11-05 17:38:05 +0000567 else {
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000568 if (a == b) {
569 /* Special case "a[i:j] = a" -- copy b first */
Michael W. Hudsonda0a0672003-08-15 12:06:41 +0000570 v = list_slice(b, 0, b->ob_size);
Martin v. Löwiscd12bfc2003-05-03 10:53:08 +0000571 if (v == NULL)
Tim Peters8d9eb102004-07-31 02:24:20 +0000572 return result;
573 result = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000574 Py_DECREF(v);
Tim Peters8d9eb102004-07-31 02:24:20 +0000575 return result;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000576 }
Raymond Hettingerf889e102004-03-09 08:04:33 +0000577 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
Michael W. Hudsonb4f49382003-08-14 17:04:28 +0000578 if(v_as_SF == NULL)
Tim Peters8d9eb102004-07-31 02:24:20 +0000579 goto Error;
Michael W. Hudsonb4f49382003-08-14 17:04:28 +0000580 n = PySequence_Fast_GET_SIZE(v_as_SF);
Raymond Hettinger42bec932004-03-12 16:38:17 +0000581 vitem = PySequence_Fast_ITEMS(v_as_SF);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000582 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000583 if (ilow < 0)
584 ilow = 0;
585 else if (ilow > a->ob_size)
586 ilow = a->ob_size;
Tim Peters8d9eb102004-07-31 02:24:20 +0000587
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588 if (ihigh < ilow)
589 ihigh = ilow;
590 else if (ihigh > a->ob_size)
591 ihigh = a->ob_size;
Armin Rigo93677f02004-07-29 12:40:23 +0000592
Tim Peters8d9eb102004-07-31 02:24:20 +0000593 norig = ihigh - ilow;
594 assert(norig >= 0);
595 d = n - norig;
Armin Rigo93677f02004-07-29 12:40:23 +0000596 if (a->ob_size + d == 0) {
597 Py_XDECREF(v_as_SF);
598 return list_clear(a);
599 }
600 item = a->ob_item;
Tim Peters8d9eb102004-07-31 02:24:20 +0000601 /* recycle the items that we are about to remove */
602 s = norig * sizeof(PyObject *);
Tim Peters73572222004-07-31 02:54:42 +0000603 if (s > sizeof(recycle_on_stack)) {
Armin Rigo1dd04a02004-07-30 11:38:22 +0000604 recycle = (PyObject **)PyMem_MALLOC(s);
Martin v. Löwiscd12bfc2003-05-03 10:53:08 +0000605 if (recycle == NULL) {
606 PyErr_NoMemory();
Tim Peters8d9eb102004-07-31 02:24:20 +0000607 goto Error;
Martin v. Löwiscd12bfc2003-05-03 10:53:08 +0000608 }
609 }
Armin Rigo1dd04a02004-07-30 11:38:22 +0000610 memcpy(recycle, &item[ilow], s);
Tim Peters8d9eb102004-07-31 02:24:20 +0000611
Armin Rigo1dd04a02004-07-30 11:38:22 +0000612 if (d < 0) { /* Delete -d items */
613 memmove(&item[ihigh+d], &item[ihigh],
614 (a->ob_size - ihigh)*sizeof(PyObject *));
615 list_resize(a, a->ob_size + d);
616 item = a->ob_item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617 }
Armin Rigo1dd04a02004-07-30 11:38:22 +0000618 else if (d > 0) { /* Insert d items */
Tim Peters73572222004-07-31 02:54:42 +0000619 k = a->ob_size;
620 if (list_resize(a, k+d) < 0)
Tim Peters8d9eb102004-07-31 02:24:20 +0000621 goto Error;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000622 item = a->ob_item;
Raymond Hettingerf889e102004-03-09 08:04:33 +0000623 memmove(&item[ihigh+d], &item[ihigh],
Tim Peters73572222004-07-31 02:54:42 +0000624 (k - ihigh)*sizeof(PyObject *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625 }
626 for (k = 0; k < n; k++, ilow++) {
Raymond Hettingerf889e102004-03-09 08:04:33 +0000627 PyObject *w = vitem[k];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000629 item[ilow] = w;
630 }
Tim Peters73572222004-07-31 02:54:42 +0000631 for (k = norig - 1; k >= 0; --k)
632 Py_XDECREF(recycle[k]);
Tim Peters8d9eb102004-07-31 02:24:20 +0000633 result = 0;
634 Error:
Tim Peters73572222004-07-31 02:54:42 +0000635 if (recycle != recycle_on_stack)
Armin Rigo1dd04a02004-07-30 11:38:22 +0000636 PyMem_FREE(recycle);
Michael W. Hudson5da854f2002-11-05 17:38:05 +0000637 Py_XDECREF(v_as_SF);
Tim Peters8d9eb102004-07-31 02:24:20 +0000638 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639#undef b
640}
641
Guido van Rossum234f9421993-06-17 12:35:49 +0000642int
Fred Drakea2f55112000-07-09 15:16:51 +0000643PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000644{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645 if (!PyList_Check(a)) {
646 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000647 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000648 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000650}
651
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000652static PyObject *
653list_inplace_repeat(PyListObject *self, int n)
654{
655 PyObject **items;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000656 int size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000657
658
659 size = PyList_GET_SIZE(self);
660 if (size == 0) {
661 Py_INCREF(self);
662 return (PyObject *)self;
663 }
664
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000665 if (n < 1) {
Armin Rigo93677f02004-07-29 12:40:23 +0000666 (void)list_clear(self);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000667 Py_INCREF(self);
668 return (PyObject *)self;
669 }
670
Tim Petersb38e2b62004-07-29 02:29:26 +0000671 if (list_resize(self, size*n) == -1)
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000672 return NULL;
673
674 p = size;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000675 items = self->ob_item;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000676 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
677 for (j = 0; j < size; j++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000678 PyObject *o = items[j];
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000679 Py_INCREF(o);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000680 items[p++] = o;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000681 }
682 }
683 Py_INCREF(self);
684 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000685}
686
Guido van Rossum4a450d01991-04-03 19:05:18 +0000687static int
Fred Drakea2f55112000-07-09 15:16:51 +0000688list_ass_item(PyListObject *a, int i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000689{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690 PyObject *old_value;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000691 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000692 PyErr_SetString(PyExc_IndexError,
693 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000694 return -1;
695 }
696 if (v == NULL)
697 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000699 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000700 a->ob_item[i] = v;
Tim Peters3b01a122002-07-19 02:35:45 +0000701 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000702 return 0;
703}
704
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000705static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000706listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000707{
708 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000709 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000710 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000711 return NULL;
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +0000712 if (ins1(self, i, v) == 0)
713 Py_RETURN_NONE;
Raymond Hettinger501f02c2004-04-12 14:01:16 +0000714 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715}
716
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000717static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000718listappend(PyListObject *self, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719{
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +0000720 if (app1(self, v) == 0)
721 Py_RETURN_NONE;
Raymond Hettinger501f02c2004-04-12 14:01:16 +0000722 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000723}
724
Barry Warsawdedf6d61998-10-09 16:37:25 +0000725static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000726listextend(PyListObject *self, PyObject *b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000727{
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000728 PyObject *it; /* iter(v) */
729 int m; /* size of self */
730 int n; /* guess for size of b */
731 int mn; /* m + n */
732 int i;
Raymond Hettinger57c45422004-03-11 09:48:18 +0000733 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000734
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000735 /* Special cases:
Tim Petersb38e2b62004-07-29 02:29:26 +0000736 1) lists and tuples which can use PySequence_Fast ops
737 2) extending self to self requires making a copy first
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000738 */
739 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
Armin Rigo70d172d2004-03-20 22:19:23 +0000740 PyObject **src, **dest;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000741 b = PySequence_Fast(b, "argument must be iterable");
742 if (!b)
743 return NULL;
Armin Rigo70d172d2004-03-20 22:19:23 +0000744 n = PySequence_Fast_GET_SIZE(b);
745 if (n == 0) {
746 /* short circuit when b is empty */
747 Py_DECREF(b);
748 Py_RETURN_NONE;
749 }
750 m = self->ob_size;
751 if (list_resize(self, m + n) == -1) {
752 Py_DECREF(b);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000753 return NULL;
Armin Rigo70d172d2004-03-20 22:19:23 +0000754 }
755 /* note that we may still have self == b here for the
756 * situation a.extend(a), but the following code works
757 * in that case too. Just make sure to resize self
758 * before calling PySequence_Fast_ITEMS.
759 */
760 /* populate the end of self with b's items */
761 src = PySequence_Fast_ITEMS(b);
762 dest = self->ob_item + m;
763 for (i = 0; i < n; i++) {
764 PyObject *o = src[i];
765 Py_INCREF(o);
766 dest[i] = o;
767 }
768 Py_DECREF(b);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000769 Py_RETURN_NONE;
770 }
771
772 it = PyObject_GetIter(b);
773 if (it == NULL)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000774 return NULL;
Raymond Hettinger57c45422004-03-11 09:48:18 +0000775 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000776
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000777 /* Guess a result list size. */
Armin Rigof5b3e362006-02-11 21:32:43 +0000778 n = _PyObject_LengthHint(b);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000779 if (n < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000780 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
781 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
782 Py_DECREF(it);
783 return NULL;
784 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000785 PyErr_Clear();
786 n = 8; /* arbitrary */
787 }
788 m = self->ob_size;
789 mn = m + n;
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000790 if (mn >= m) {
791 /* Make room. */
792 if (list_resize(self, mn) == -1)
793 goto error;
794 /* Make the list sane again. */
795 self->ob_size = m;
796 }
797 /* Else m + n overflowed; on the chance that n lied, and there really
798 * is enough room, ignore it. If n was telling the truth, we'll
799 * eventually run out of memory during the loop.
800 */
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000801
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000802 /* Run iterator to exhaustion. */
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000803 for (;;) {
Raymond Hettinger57c45422004-03-11 09:48:18 +0000804 PyObject *item = iternext(it);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000805 if (item == NULL) {
Raymond Hettinger57c45422004-03-11 09:48:18 +0000806 if (PyErr_Occurred()) {
807 if (PyErr_ExceptionMatches(PyExc_StopIteration))
808 PyErr_Clear();
809 else
810 goto error;
811 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000812 break;
813 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000814 if (self->ob_size < self->allocated) {
815 /* steals ref */
816 PyList_SET_ITEM(self, self->ob_size, item);
817 ++self->ob_size;
818 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000819 else {
Raymond Hettinger40a03822004-04-12 13:05:09 +0000820 int status = app1(self, item);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000821 Py_DECREF(item); /* append creates a new ref */
822 if (status < 0)
823 goto error;
824 }
825 }
826
827 /* Cut back result list if initial guess was too large. */
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000828 if (self->ob_size < self->allocated)
829 list_resize(self, self->ob_size); /* shrinking can't fail */
830
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000831 Py_DECREF(it);
832 Py_RETURN_NONE;
833
834 error:
835 Py_DECREF(it);
836 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000837}
838
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000839PyObject *
840_PyList_Extend(PyListObject *self, PyObject *b)
841{
842 return listextend(self, b);
843}
844
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000845static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000846list_inplace_concat(PyListObject *self, PyObject *other)
847{
848 PyObject *result;
849
850 result = listextend(self, other);
851 if (result == NULL)
852 return result;
853 Py_DECREF(result);
854 Py_INCREF(self);
855 return (PyObject *)self;
856}
857
858static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000859listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000860{
861 int i = -1;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000862 PyObject *v, *arg = NULL;
Tim Peters8fc4a912004-07-31 21:53:19 +0000863 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000864
865 if (!PyArg_UnpackTuple(args, "pop", 0, 1, &arg))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000866 return NULL;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000867 if (arg != NULL) {
868 if (PyInt_Check(arg))
869 i = (int)(PyInt_AS_LONG((PyIntObject*) arg));
Raymond Hettingerfa6c6f82004-02-19 06:12:06 +0000870 else if (!PyArg_ParseTuple(args, "|i:pop", &i))
871 return NULL;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000872 }
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000873 if (self->ob_size == 0) {
874 /* Special-case most common failure cause */
875 PyErr_SetString(PyExc_IndexError, "pop from empty list");
876 return NULL;
877 }
878 if (i < 0)
879 i += self->ob_size;
880 if (i < 0 || i >= self->ob_size) {
881 PyErr_SetString(PyExc_IndexError, "pop index out of range");
882 return NULL;
883 }
884 v = self->ob_item[i];
Raymond Hettingercb3e5802004-02-13 18:36:31 +0000885 if (i == self->ob_size - 1) {
Tim Peters8fc4a912004-07-31 21:53:19 +0000886 status = list_resize(self, self->ob_size - 1);
887 assert(status >= 0);
888 return v; /* and v now owns the reference the list had */
Raymond Hettingercb3e5802004-02-13 18:36:31 +0000889 }
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000890 Py_INCREF(v);
Tim Peters8fc4a912004-07-31 21:53:19 +0000891 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
892 assert(status >= 0);
893 /* Use status, so that in a release build compilers don't
894 * complain about the unused name.
895 */
Brett Cannon651dd522004-08-08 21:21:18 +0000896 (void) status;
897
898 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000899}
900
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000901/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
902static void
903reverse_slice(PyObject **lo, PyObject **hi)
904{
905 assert(lo && hi);
906
907 --hi;
908 while (lo < hi) {
909 PyObject *t = *lo;
910 *lo = *hi;
911 *hi = t;
912 ++lo;
913 --hi;
914 }
915}
916
Tim Petersa64dc242002-08-01 02:13:36 +0000917/* Lots of code for an adaptive, stable, natural mergesort. There are many
918 * pieces to this algorithm; read listsort.txt for overviews and details.
919 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000920
Guido van Rossum3f236de1996-12-10 23:55:39 +0000921/* Comparison function. Takes care of calling a user-supplied
Tim Peters66860f62002-08-04 17:47:26 +0000922 * comparison function (any callable Python object), which must not be
923 * NULL (use the ISLT macro if you don't know, or call PyObject_RichCompareBool
924 * with Py_LT if you know it's NULL).
Tim Petersa64dc242002-08-01 02:13:36 +0000925 * Returns -1 on error, 1 if x < y, 0 if x >= y.
926 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000927static int
Tim Petersa8c974c2002-07-19 03:30:57 +0000928islt(PyObject *x, PyObject *y, PyObject *compare)
Guido van Rossum3f236de1996-12-10 23:55:39 +0000929{
Tim Petersf2a04732002-07-11 21:46:16 +0000930 PyObject *res;
931 PyObject *args;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000932 int i;
933
Tim Peters66860f62002-08-04 17:47:26 +0000934 assert(compare != NULL);
Tim Petersa8c974c2002-07-19 03:30:57 +0000935 /* Call the user's comparison function and translate the 3-way
936 * result into true or false (or error).
937 */
Tim Petersf2a04732002-07-11 21:46:16 +0000938 args = PyTuple_New(2);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000939 if (args == NULL)
Tim Petersa8c974c2002-07-19 03:30:57 +0000940 return -1;
Tim Petersf2a04732002-07-11 21:46:16 +0000941 Py_INCREF(x);
942 Py_INCREF(y);
943 PyTuple_SET_ITEM(args, 0, x);
944 PyTuple_SET_ITEM(args, 1, y);
Tim Peters58cf3612002-07-15 05:16:13 +0000945 res = PyObject_Call(compare, args, NULL);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000946 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000947 if (res == NULL)
Tim Petersa8c974c2002-07-19 03:30:57 +0000948 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000949 if (!PyInt_Check(res)) {
950 Py_DECREF(res);
951 PyErr_SetString(PyExc_TypeError,
Guido van Rossum9bcd1d71999-04-19 17:44:39 +0000952 "comparison function must return int");
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{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000991 register int k;
992 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*/
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001053static int
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{
Tim Petersa64dc242002-08-01 02:13:36 +00001056 int k;
1057 int 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*/
1108static int
1109gallop_left(PyObject *key, PyObject **a, int n, int hint, PyObject *compare)
1110{
1111 int ofs;
1112 int lastofs;
1113 int k;
1114
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 */
1124 const int maxofs = n - hint; /* &a[n-1] is highest */
1125 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 */
1145 const int maxofs = hint + 1; /* &a[0] is lowest */
1146 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) {
1171 int m = lastofs + ((ofs - lastofs) >> 1);
1172
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*/
1199static int
1200gallop_right(PyObject *key, PyObject **a, int n, int hint, PyObject *compare)
1201{
1202 int ofs;
1203 int lastofs;
1204 int k;
1205
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 */
1215 const int maxofs = hint + 1; /* &a[0] is lowest */
1216 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 */
Tim Petersa64dc242002-08-01 02:13:36 +00001237 const int maxofs = n - hint; /* &a[n-1] is highest */
1238 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) {
1262 int m = lastofs + ((ofs - lastofs) >> 1);
1263
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;
1297 int len;
1298};
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 */
1308 int min_gallop;
1309
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 */
1314 int alloced;
1315
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
1362merge_getmem(MergeState *ms, int need)
1363{
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 */
1389static int
1390merge_lo(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb)
1391{
1392 int k;
1393 PyObject *compare;
1394 PyObject **dest;
1395 int result = -1; /* guilty until proved innocent */
Tim Peterse05f65a2002-08-10 05:21:15 +00001396 int min_gallop = ms->min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001397
1398 assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
1399 if (MERGE_GETMEM(ms, na) < 0)
1400 return -1;
1401 memcpy(ms->a, pa, na * sizeof(PyObject*));
1402 dest = pa;
1403 pa = ms->a;
1404
1405 *dest++ = *pb++;
1406 --nb;
1407 if (nb == 0)
1408 goto Succeed;
1409 if (na == 1)
1410 goto CopyB;
1411
1412 compare = ms->compare;
1413 for (;;) {
1414 int acount = 0; /* # of times A won in a row */
1415 int bcount = 0; /* # of times B won in a row */
1416
1417 /* Do the straightforward thing until (if ever) one run
1418 * appears to win consistently.
1419 */
1420 for (;;) {
Tim Peterse05f65a2002-08-10 05:21:15 +00001421 assert(na > 1 && nb > 0);
Tim Peters66860f62002-08-04 17:47:26 +00001422 k = ISLT(*pb, *pa, compare);
Tim Petersa64dc242002-08-01 02:13:36 +00001423 if (k) {
1424 if (k < 0)
1425 goto Fail;
1426 *dest++ = *pb++;
1427 ++bcount;
1428 acount = 0;
1429 --nb;
1430 if (nb == 0)
1431 goto Succeed;
Tim Peterse05f65a2002-08-10 05:21:15 +00001432 if (bcount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001433 break;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001434 }
1435 else {
Tim Petersa64dc242002-08-01 02:13:36 +00001436 *dest++ = *pa++;
1437 ++acount;
1438 bcount = 0;
1439 --na;
1440 if (na == 1)
1441 goto CopyB;
Tim Peterse05f65a2002-08-10 05:21:15 +00001442 if (acount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001443 break;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001444 }
Tim Petersa64dc242002-08-01 02:13:36 +00001445 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001446
Tim Petersa64dc242002-08-01 02:13:36 +00001447 /* One run is winning so consistently that galloping may
1448 * be a huge win. So try that, and continue galloping until
1449 * (if ever) neither run appears to be winning consistently
1450 * anymore.
1451 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001452 ++min_gallop;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001453 do {
Tim Peterse05f65a2002-08-10 05:21:15 +00001454 assert(na > 1 && nb > 0);
1455 min_gallop -= min_gallop > 1;
1456 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001457 k = gallop_right(*pb, pa, na, 0, compare);
1458 acount = k;
1459 if (k) {
1460 if (k < 0)
1461 goto Fail;
1462 memcpy(dest, pa, k * sizeof(PyObject *));
1463 dest += k;
1464 pa += k;
1465 na -= k;
1466 if (na == 1)
1467 goto CopyB;
1468 /* na==0 is impossible now if the comparison
1469 * function is consistent, but we can't assume
1470 * that it is.
1471 */
1472 if (na == 0)
1473 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001474 }
Tim Petersa64dc242002-08-01 02:13:36 +00001475 *dest++ = *pb++;
1476 --nb;
1477 if (nb == 0)
1478 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001479
Tim Petersa64dc242002-08-01 02:13:36 +00001480 k = gallop_left(*pa, pb, nb, 0, compare);
1481 bcount = k;
1482 if (k) {
1483 if (k < 0)
1484 goto Fail;
1485 memmove(dest, pb, k * sizeof(PyObject *));
1486 dest += k;
1487 pb += k;
1488 nb -= k;
1489 if (nb == 0)
1490 goto Succeed;
1491 }
1492 *dest++ = *pa++;
1493 --na;
1494 if (na == 1)
1495 goto CopyB;
1496 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
Tim Peterse05f65a2002-08-10 05:21:15 +00001497 ++min_gallop; /* penalize it for leaving galloping mode */
1498 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001499 }
1500Succeed:
1501 result = 0;
1502Fail:
1503 if (na)
1504 memcpy(dest, pa, na * sizeof(PyObject*));
1505 return result;
1506CopyB:
1507 assert(na == 1 && nb > 0);
1508 /* The last element of pa belongs at the end of the merge. */
1509 memmove(dest, pb, nb * sizeof(PyObject *));
1510 dest[nb] = *pa;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001511 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001512}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001513
Tim Petersa64dc242002-08-01 02:13:36 +00001514/* Merge the na elements starting at pa with the nb elements starting at pb
1515 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
1516 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
1517 * merge, and should have na >= nb. See listsort.txt for more info.
1518 * Return 0 if successful, -1 if error.
1519 */
1520static int
1521merge_hi(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb)
1522{
1523 int k;
1524 PyObject *compare;
1525 PyObject **dest;
1526 int result = -1; /* guilty until proved innocent */
1527 PyObject **basea;
1528 PyObject **baseb;
Tim Peterse05f65a2002-08-10 05:21:15 +00001529 int min_gallop = ms->min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001530
1531 assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
1532 if (MERGE_GETMEM(ms, nb) < 0)
1533 return -1;
1534 dest = pb + nb - 1;
1535 memcpy(ms->a, pb, nb * sizeof(PyObject*));
1536 basea = pa;
1537 baseb = ms->a;
1538 pb = ms->a + nb - 1;
1539 pa += na - 1;
1540
1541 *dest-- = *pa--;
1542 --na;
1543 if (na == 0)
1544 goto Succeed;
1545 if (nb == 1)
1546 goto CopyA;
1547
1548 compare = ms->compare;
1549 for (;;) {
1550 int acount = 0; /* # of times A won in a row */
1551 int bcount = 0; /* # of times B won in a row */
1552
1553 /* Do the straightforward thing until (if ever) one run
1554 * appears to win consistently.
1555 */
1556 for (;;) {
Tim Peterse05f65a2002-08-10 05:21:15 +00001557 assert(na > 0 && nb > 1);
Tim Peters66860f62002-08-04 17:47:26 +00001558 k = ISLT(*pb, *pa, compare);
Tim Petersa64dc242002-08-01 02:13:36 +00001559 if (k) {
1560 if (k < 0)
1561 goto Fail;
1562 *dest-- = *pa--;
1563 ++acount;
1564 bcount = 0;
1565 --na;
1566 if (na == 0)
1567 goto Succeed;
Tim Peterse05f65a2002-08-10 05:21:15 +00001568 if (acount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001569 break;
1570 }
1571 else {
1572 *dest-- = *pb--;
1573 ++bcount;
1574 acount = 0;
1575 --nb;
1576 if (nb == 1)
1577 goto CopyA;
Tim Peterse05f65a2002-08-10 05:21:15 +00001578 if (bcount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001579 break;
1580 }
1581 }
1582
1583 /* One run is winning so consistently that galloping may
1584 * be a huge win. So try that, and continue galloping until
1585 * (if ever) neither run appears to be winning consistently
1586 * anymore.
1587 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001588 ++min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001589 do {
Tim Peterse05f65a2002-08-10 05:21:15 +00001590 assert(na > 0 && nb > 1);
1591 min_gallop -= min_gallop > 1;
1592 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001593 k = gallop_right(*pb, basea, na, na-1, compare);
1594 if (k < 0)
1595 goto Fail;
1596 k = na - k;
1597 acount = k;
1598 if (k) {
1599 dest -= k;
1600 pa -= k;
1601 memmove(dest+1, pa+1, k * sizeof(PyObject *));
1602 na -= k;
1603 if (na == 0)
1604 goto Succeed;
1605 }
1606 *dest-- = *pb--;
1607 --nb;
1608 if (nb == 1)
1609 goto CopyA;
1610
1611 k = gallop_left(*pa, baseb, nb, nb-1, compare);
1612 if (k < 0)
1613 goto Fail;
1614 k = nb - k;
1615 bcount = k;
1616 if (k) {
1617 dest -= k;
1618 pb -= k;
1619 memcpy(dest+1, pb+1, k * sizeof(PyObject *));
1620 nb -= k;
1621 if (nb == 1)
1622 goto CopyA;
1623 /* nb==0 is impossible now if the comparison
1624 * function is consistent, but we can't assume
1625 * that it is.
1626 */
1627 if (nb == 0)
1628 goto Succeed;
1629 }
1630 *dest-- = *pa--;
1631 --na;
1632 if (na == 0)
1633 goto Succeed;
1634 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
Tim Peterse05f65a2002-08-10 05:21:15 +00001635 ++min_gallop; /* penalize it for leaving galloping mode */
1636 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001637 }
1638Succeed:
1639 result = 0;
1640Fail:
1641 if (nb)
1642 memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*));
1643 return result;
1644CopyA:
1645 assert(nb == 1 && na > 0);
1646 /* The first element of pb belongs at the front of the merge. */
1647 dest -= na;
1648 pa -= na;
1649 memmove(dest+1, pa+1, na * sizeof(PyObject *));
1650 *dest = *pb;
1651 return 0;
1652}
1653
1654/* Merge the two runs at stack indices i and i+1.
1655 * Returns 0 on success, -1 on error.
1656 */
1657static int
1658merge_at(MergeState *ms, int i)
1659{
1660 PyObject **pa, **pb;
1661 int na, nb;
1662 int k;
1663 PyObject *compare;
1664
1665 assert(ms != NULL);
1666 assert(ms->n >= 2);
1667 assert(i >= 0);
1668 assert(i == ms->n - 2 || i == ms->n - 3);
1669
Tim Peterse05f65a2002-08-10 05:21:15 +00001670 pa = ms->pending[i].base;
1671 na = ms->pending[i].len;
1672 pb = ms->pending[i+1].base;
1673 nb = ms->pending[i+1].len;
Tim Petersa64dc242002-08-01 02:13:36 +00001674 assert(na > 0 && nb > 0);
1675 assert(pa + na == pb);
1676
1677 /* Record the length of the combined runs; if i is the 3rd-last
1678 * run now, also slide over the last run (which isn't involved
1679 * in this merge). The current run i+1 goes away in any case.
1680 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001681 ms->pending[i].len = na + nb;
1682 if (i == ms->n - 3)
1683 ms->pending[i+1] = ms->pending[i+2];
Tim Petersa64dc242002-08-01 02:13:36 +00001684 --ms->n;
1685
1686 /* Where does b start in a? Elements in a before that can be
1687 * ignored (already in place).
1688 */
1689 compare = ms->compare;
1690 k = gallop_right(*pb, pa, na, 0, compare);
1691 if (k < 0)
1692 return -1;
1693 pa += k;
1694 na -= k;
1695 if (na == 0)
1696 return 0;
1697
1698 /* Where does a end in b? Elements in b after that can be
1699 * ignored (already in place).
1700 */
1701 nb = gallop_left(pa[na-1], pb, nb, nb-1, compare);
1702 if (nb <= 0)
1703 return nb;
1704
1705 /* Merge what remains of the runs, using a temp array with
1706 * min(na, nb) elements.
1707 */
1708 if (na <= nb)
1709 return merge_lo(ms, pa, na, pb, nb);
1710 else
1711 return merge_hi(ms, pa, na, pb, nb);
1712}
1713
1714/* Examine the stack of runs waiting to be merged, merging adjacent runs
1715 * until the stack invariants are re-established:
1716 *
1717 * 1. len[-3] > len[-2] + len[-1]
1718 * 2. len[-2] > len[-1]
1719 *
1720 * See listsort.txt for more info.
1721 *
1722 * Returns 0 on success, -1 on error.
1723 */
1724static int
1725merge_collapse(MergeState *ms)
1726{
Tim Peterse05f65a2002-08-10 05:21:15 +00001727 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001728
1729 assert(ms);
1730 while (ms->n > 1) {
1731 int n = ms->n - 2;
Tim Peterse05f65a2002-08-10 05:21:15 +00001732 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
1733 if (p[n-1].len < p[n+1].len)
Tim Petersa64dc242002-08-01 02:13:36 +00001734 --n;
1735 if (merge_at(ms, n) < 0)
1736 return -1;
1737 }
Tim Peterse05f65a2002-08-10 05:21:15 +00001738 else if (p[n].len <= p[n+1].len) {
Tim Petersa64dc242002-08-01 02:13:36 +00001739 if (merge_at(ms, n) < 0)
1740 return -1;
1741 }
1742 else
1743 break;
1744 }
1745 return 0;
1746}
1747
1748/* Regardless of invariants, merge all runs on the stack until only one
1749 * remains. This is used at the end of the mergesort.
1750 *
1751 * Returns 0 on success, -1 on error.
1752 */
1753static int
1754merge_force_collapse(MergeState *ms)
1755{
Tim Peterse05f65a2002-08-10 05:21:15 +00001756 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001757
1758 assert(ms);
1759 while (ms->n > 1) {
1760 int n = ms->n - 2;
Tim Peterse05f65a2002-08-10 05:21:15 +00001761 if (n > 0 && p[n-1].len < p[n+1].len)
Tim Petersa64dc242002-08-01 02:13:36 +00001762 --n;
1763 if (merge_at(ms, n) < 0)
1764 return -1;
1765 }
1766 return 0;
1767}
1768
1769/* Compute a good value for the minimum run length; natural runs shorter
1770 * than this are boosted artificially via binary insertion.
1771 *
1772 * If n < 64, return n (it's too small to bother with fancy stuff).
1773 * Else if n is an exact power of 2, return 32.
1774 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1775 * strictly less than, an exact power of 2.
1776 *
1777 * See listsort.txt for more info.
1778 */
1779static int
1780merge_compute_minrun(int n)
1781{
1782 int r = 0; /* becomes 1 if any 1 bits are shifted off */
1783
1784 assert(n >= 0);
1785 while (n >= 64) {
1786 r |= n & 1;
1787 n >>= 1;
1788 }
1789 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001790}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001791
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001792/* Special wrapper to support stable sorting using the decorate-sort-undecorate
Andrew M. Kuchling55be9ea2004-09-10 12:59:54 +00001793 pattern. Holds a key which is used for comparisons and the original record
Tim Petersb38e2b62004-07-29 02:29:26 +00001794 which is returned during the undecorate phase. By exposing only the key
1795 during comparisons, the underlying sort stability characteristics are left
1796 unchanged. Also, if a custom comparison function is used, it will only see
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001797 the key instead of a full record. */
1798
1799typedef struct {
1800 PyObject_HEAD
1801 PyObject *key;
1802 PyObject *value;
1803} sortwrapperobject;
1804
1805static PyTypeObject sortwrapper_type;
1806
1807static PyObject *
1808sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, int op)
1809{
1810 if (!PyObject_TypeCheck(b, &sortwrapper_type)) {
Tim Petersb38e2b62004-07-29 02:29:26 +00001811 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001812 "expected a sortwrapperobject");
1813 return NULL;
1814 }
1815 return PyObject_RichCompare(a->key, b->key, op);
1816}
1817
1818static void
1819sortwrapper_dealloc(sortwrapperobject *so)
1820{
1821 Py_XDECREF(so->key);
1822 Py_XDECREF(so->value);
1823 PyObject_Del(so);
1824}
1825
1826PyDoc_STRVAR(sortwrapper_doc, "Object wrapper with a custom sort key.");
1827
1828static PyTypeObject sortwrapper_type = {
1829 PyObject_HEAD_INIT(&PyType_Type)
1830 0, /* ob_size */
1831 "sortwrapper", /* tp_name */
1832 sizeof(sortwrapperobject), /* tp_basicsize */
1833 0, /* tp_itemsize */
1834 /* methods */
1835 (destructor)sortwrapper_dealloc, /* tp_dealloc */
1836 0, /* tp_print */
1837 0, /* tp_getattr */
1838 0, /* tp_setattr */
1839 0, /* tp_compare */
1840 0, /* tp_repr */
1841 0, /* tp_as_number */
1842 0, /* tp_as_sequence */
1843 0, /* tp_as_mapping */
1844 0, /* tp_hash */
1845 0, /* tp_call */
1846 0, /* tp_str */
1847 PyObject_GenericGetAttr, /* tp_getattro */
1848 0, /* tp_setattro */
1849 0, /* tp_as_buffer */
Tim Petersb38e2b62004-07-29 02:29:26 +00001850 Py_TPFLAGS_DEFAULT |
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001851 Py_TPFLAGS_HAVE_RICHCOMPARE, /* tp_flags */
1852 sortwrapper_doc, /* tp_doc */
1853 0, /* tp_traverse */
1854 0, /* tp_clear */
1855 (richcmpfunc)sortwrapper_richcompare, /* tp_richcompare */
1856};
1857
1858/* Returns a new reference to a sortwrapper.
1859 Consumes the references to the two underlying objects. */
1860
1861static PyObject *
1862build_sortwrapper(PyObject *key, PyObject *value)
1863{
1864 sortwrapperobject *so;
Tim Petersb38e2b62004-07-29 02:29:26 +00001865
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001866 so = PyObject_New(sortwrapperobject, &sortwrapper_type);
1867 if (so == NULL)
1868 return NULL;
1869 so->key = key;
1870 so->value = value;
1871 return (PyObject *)so;
1872}
1873
1874/* Returns a new reference to the value underlying the wrapper. */
1875static PyObject *
1876sortwrapper_getvalue(PyObject *so)
1877{
1878 PyObject *value;
1879
1880 if (!PyObject_TypeCheck(so, &sortwrapper_type)) {
Tim Petersb38e2b62004-07-29 02:29:26 +00001881 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001882 "expected a sortwrapperobject");
1883 return NULL;
1884 }
1885 value = ((sortwrapperobject *)so)->value;
1886 Py_INCREF(value);
1887 return value;
1888}
1889
1890/* Wrapper for user specified cmp functions in combination with a
1891 specified key function. Makes sure the cmp function is presented
1892 with the actual key instead of the sortwrapper */
1893
1894typedef struct {
1895 PyObject_HEAD
1896 PyObject *func;
1897} cmpwrapperobject;
1898
1899static void
1900cmpwrapper_dealloc(cmpwrapperobject *co)
1901{
1902 Py_XDECREF(co->func);
1903 PyObject_Del(co);
1904}
1905
1906static PyObject *
1907cmpwrapper_call(cmpwrapperobject *co, PyObject *args, PyObject *kwds)
1908{
1909 PyObject *x, *y, *xx, *yy;
1910
1911 if (!PyArg_UnpackTuple(args, "", 2, 2, &x, &y))
1912 return NULL;
1913 if (!PyObject_TypeCheck(x, &sortwrapper_type) ||
Raymond Hettingerae4a2992003-10-16 17:16:30 +00001914 !PyObject_TypeCheck(y, &sortwrapper_type)) {
Tim Petersb38e2b62004-07-29 02:29:26 +00001915 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001916 "expected a sortwrapperobject");
1917 return NULL;
1918 }
1919 xx = ((sortwrapperobject *)x)->key;
1920 yy = ((sortwrapperobject *)y)->key;
1921 return PyObject_CallFunctionObjArgs(co->func, xx, yy, NULL);
1922}
1923
1924PyDoc_STRVAR(cmpwrapper_doc, "cmp() wrapper for sort with custom keys.");
1925
1926static PyTypeObject cmpwrapper_type = {
1927 PyObject_HEAD_INIT(&PyType_Type)
1928 0, /* ob_size */
1929 "cmpwrapper", /* tp_name */
1930 sizeof(cmpwrapperobject), /* tp_basicsize */
1931 0, /* tp_itemsize */
1932 /* methods */
1933 (destructor)cmpwrapper_dealloc, /* tp_dealloc */
1934 0, /* tp_print */
1935 0, /* tp_getattr */
1936 0, /* tp_setattr */
1937 0, /* tp_compare */
1938 0, /* tp_repr */
1939 0, /* tp_as_number */
1940 0, /* tp_as_sequence */
1941 0, /* tp_as_mapping */
1942 0, /* tp_hash */
1943 (ternaryfunc)cmpwrapper_call, /* tp_call */
1944 0, /* tp_str */
1945 PyObject_GenericGetAttr, /* tp_getattro */
1946 0, /* tp_setattro */
1947 0, /* tp_as_buffer */
1948 Py_TPFLAGS_DEFAULT, /* tp_flags */
1949 cmpwrapper_doc, /* tp_doc */
1950};
1951
1952static PyObject *
1953build_cmpwrapper(PyObject *cmpfunc)
1954{
1955 cmpwrapperobject *co;
Tim Petersb38e2b62004-07-29 02:29:26 +00001956
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001957 co = PyObject_New(cmpwrapperobject, &cmpwrapper_type);
1958 if (co == NULL)
1959 return NULL;
1960 Py_INCREF(cmpfunc);
1961 co->func = cmpfunc;
1962 return (PyObject *)co;
1963}
1964
Tim Petersa64dc242002-08-01 02:13:36 +00001965/* An adaptive, stable, natural mergesort. See listsort.txt.
1966 * Returns Py_None on success, NULL on error. Even in case of error, the
1967 * list will be some permutation of its input state (nothing is lost or
1968 * duplicated).
1969 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001970static PyObject *
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001971listsort(PyListObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001972{
Tim Petersa64dc242002-08-01 02:13:36 +00001973 MergeState ms;
1974 PyObject **lo, **hi;
1975 int nremaining;
1976 int minrun;
Raymond Hettinger4bb95402004-02-13 11:36:39 +00001977 int saved_ob_size, saved_allocated;
Tim Petersb9099c32002-11-12 22:08:10 +00001978 PyObject **saved_ob_item;
Armin Rigo93677f02004-07-29 12:40:23 +00001979 PyObject **final_ob_item;
Tim Petersa64dc242002-08-01 02:13:36 +00001980 PyObject *compare = NULL;
1981 PyObject *result = NULL; /* guilty until proved innocent */
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001982 int reverse = 0;
1983 PyObject *keyfunc = NULL;
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001984 int i;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001985 PyObject *key, *value, *kvpair;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001986 static const char *kwlist[] = {"cmp", "key", "reverse", 0};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001987
Tim Petersa64dc242002-08-01 02:13:36 +00001988 assert(self != NULL);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001989 assert (PyList_Check(self));
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001990 if (args != NULL) {
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001991 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi:sort",
1992 kwlist, &compare, &keyfunc, &reverse))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001993 return NULL;
1994 }
Skip Montanaro4abd5f02003-01-02 20:51:08 +00001995 if (compare == Py_None)
1996 compare = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001997 if (keyfunc == Py_None)
1998 keyfunc = NULL;
1999 if (compare != NULL && keyfunc != NULL) {
2000 compare = build_cmpwrapper(compare);
2001 if (compare == NULL)
Hye-Shik Chang19cb1932003-12-10 07:31:08 +00002002 return NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002003 } else
2004 Py_XINCREF(compare);
2005
Tim Petersb9099c32002-11-12 22:08:10 +00002006 /* The list is temporarily made empty, so that mutations performed
2007 * by comparison functions can't affect the slice of memory we're
2008 * sorting (allowing mutations during sorting is a core-dump
2009 * factory, since ob_item may change).
2010 */
2011 saved_ob_size = self->ob_size;
2012 saved_ob_item = self->ob_item;
Raymond Hettinger4bb95402004-02-13 11:36:39 +00002013 saved_allocated = self->allocated;
Armin Rigo93677f02004-07-29 12:40:23 +00002014 self->ob_size = 0;
Tim Peters51b4ade2004-07-29 04:07:15 +00002015 self->ob_item = NULL;
Armin Rigo93677f02004-07-29 12:40:23 +00002016 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002017
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002018 if (keyfunc != NULL) {
2019 for (i=0 ; i < saved_ob_size ; i++) {
2020 value = saved_ob_item[i];
Tim Petersb38e2b62004-07-29 02:29:26 +00002021 key = PyObject_CallFunctionObjArgs(keyfunc, value,
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002022 NULL);
2023 if (key == NULL) {
2024 for (i=i-1 ; i>=0 ; i--) {
2025 kvpair = saved_ob_item[i];
2026 value = sortwrapper_getvalue(kvpair);
2027 saved_ob_item[i] = value;
2028 Py_DECREF(kvpair);
2029 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002030 goto dsu_fail;
2031 }
2032 kvpair = build_sortwrapper(key, value);
2033 if (kvpair == NULL)
2034 goto dsu_fail;
2035 saved_ob_item[i] = kvpair;
2036 }
2037 }
2038
2039 /* Reverse sort stability achieved by initially reversing the list,
2040 applying a stable forward sort, then reversing the final result. */
2041 if (reverse && saved_ob_size > 1)
2042 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
2043
2044 merge_init(&ms, compare);
2045
Tim Petersb9099c32002-11-12 22:08:10 +00002046 nremaining = saved_ob_size;
Tim Petersa64dc242002-08-01 02:13:36 +00002047 if (nremaining < 2)
2048 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002049
Tim Petersa64dc242002-08-01 02:13:36 +00002050 /* March over the array once, left to right, finding natural runs,
2051 * and extending short natural runs to minrun elements.
2052 */
Tim Petersb9099c32002-11-12 22:08:10 +00002053 lo = saved_ob_item;
Tim Petersa64dc242002-08-01 02:13:36 +00002054 hi = lo + nremaining;
2055 minrun = merge_compute_minrun(nremaining);
2056 do {
2057 int descending;
2058 int n;
Tim Peters330f9e92002-07-19 07:05:44 +00002059
Tim Petersa64dc242002-08-01 02:13:36 +00002060 /* Identify next run. */
2061 n = count_run(lo, hi, compare, &descending);
2062 if (n < 0)
2063 goto fail;
2064 if (descending)
2065 reverse_slice(lo, lo + n);
2066 /* If short, extend to min(minrun, nremaining). */
2067 if (n < minrun) {
2068 const int force = nremaining <= minrun ?
2069 nremaining : minrun;
2070 if (binarysort(lo, lo + force, lo + n, compare) < 0)
2071 goto fail;
2072 n = force;
2073 }
2074 /* Push run onto pending-runs stack, and maybe merge. */
2075 assert(ms.n < MAX_MERGE_PENDING);
Tim Peterse05f65a2002-08-10 05:21:15 +00002076 ms.pending[ms.n].base = lo;
2077 ms.pending[ms.n].len = n;
Tim Petersa64dc242002-08-01 02:13:36 +00002078 ++ms.n;
2079 if (merge_collapse(&ms) < 0)
2080 goto fail;
2081 /* Advance to find next run. */
2082 lo += n;
2083 nremaining -= n;
2084 } while (nremaining);
2085 assert(lo == hi);
Tim Peters330f9e92002-07-19 07:05:44 +00002086
Tim Petersa64dc242002-08-01 02:13:36 +00002087 if (merge_force_collapse(&ms) < 0)
2088 goto fail;
2089 assert(ms.n == 1);
Tim Petersb9099c32002-11-12 22:08:10 +00002090 assert(ms.pending[0].base == saved_ob_item);
2091 assert(ms.pending[0].len == saved_ob_size);
Tim Petersa64dc242002-08-01 02:13:36 +00002092
2093succeed:
2094 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002095fail:
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002096 if (keyfunc != NULL) {
2097 for (i=0 ; i < saved_ob_size ; i++) {
2098 kvpair = saved_ob_item[i];
2099 value = sortwrapper_getvalue(kvpair);
2100 saved_ob_item[i] = value;
2101 Py_DECREF(kvpair);
2102 }
2103 }
2104
Armin Rigo93677f02004-07-29 12:40:23 +00002105 if (self->allocated != -1 && result != NULL) {
Tim Peters51b4ade2004-07-29 04:07:15 +00002106 /* The user mucked with the list during the sort,
2107 * and we don't already have another error to report.
2108 */
2109 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2110 result = NULL;
Tim Petersb9099c32002-11-12 22:08:10 +00002111 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002112
2113 if (reverse && saved_ob_size > 1)
2114 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
2115
2116 merge_freemem(&ms);
2117
2118dsu_fail:
Armin Rigo93677f02004-07-29 12:40:23 +00002119 final_ob_item = self->ob_item;
2120 i = self->ob_size;
Tim Petersb9099c32002-11-12 22:08:10 +00002121 self->ob_size = saved_ob_size;
2122 self->ob_item = saved_ob_item;
Raymond Hettinger4bb95402004-02-13 11:36:39 +00002123 self->allocated = saved_allocated;
Armin Rigo93677f02004-07-29 12:40:23 +00002124 if (final_ob_item != NULL) {
2125 /* we cannot use list_clear() for this because it does not
2126 guarantee that the list is really empty when it returns */
2127 while (--i >= 0) {
2128 Py_XDECREF(final_ob_item[i]);
2129 }
2130 PyMem_FREE(final_ob_item);
2131 }
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002132 Py_XDECREF(compare);
Tim Petersa64dc242002-08-01 02:13:36 +00002133 Py_XINCREF(result);
2134 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002135}
Tim Peters330f9e92002-07-19 07:05:44 +00002136#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002137#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002138
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002139int
Fred Drakea2f55112000-07-09 15:16:51 +00002140PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002141{
2142 if (v == NULL || !PyList_Check(v)) {
2143 PyErr_BadInternalCall();
2144 return -1;
2145 }
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002146 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002147 if (v == NULL)
2148 return -1;
2149 Py_DECREF(v);
2150 return 0;
2151}
2152
Guido van Rossumb86c5492001-02-12 22:06:02 +00002153static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002154listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002155{
Tim Peters326b4482002-07-19 04:04:16 +00002156 if (self->ob_size > 1)
2157 reverse_slice(self->ob_item, self->ob_item + self->ob_size);
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +00002158 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002159}
2160
Guido van Rossum84c76f51990-10-30 13:32:20 +00002161int
Fred Drakea2f55112000-07-09 15:16:51 +00002162PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002163{
Tim Peters6063e262002-08-08 01:06:39 +00002164 PyListObject *self = (PyListObject *)v;
2165
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002166 if (v == NULL || !PyList_Check(v)) {
2167 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002168 return -1;
2169 }
Tim Peters6063e262002-08-08 01:06:39 +00002170 if (self->ob_size > 1)
2171 reverse_slice(self->ob_item, self->ob_item + self->ob_size);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002172 return 0;
2173}
2174
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002175PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002176PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002177{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002178 PyObject *w;
2179 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002180 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002181 if (v == NULL || !PyList_Check(v)) {
2182 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002183 return NULL;
2184 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002185 n = ((PyListObject *)v)->ob_size;
2186 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002187 if (w == NULL)
2188 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002189 p = ((PyTupleObject *)w)->ob_item;
Thomas Wouters334fb892000-07-25 12:56:38 +00002190 memcpy((void *)p,
2191 (void *)((PyListObject *)v)->ob_item,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002192 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002193 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002194 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002195 p++;
2196 }
2197 return w;
2198}
2199
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002200static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002201listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002202{
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002203 int i, start=0, stop=self->ob_size;
2204 PyObject *v;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002205
Walter Dörwalde8049bef2003-06-17 19:27:39 +00002206 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2207 _PyEval_SliceIndex, &start,
2208 _PyEval_SliceIndex, &stop))
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002209 return NULL;
Guido van Rossum2743d872003-06-17 14:25:14 +00002210 if (start < 0) {
2211 start += self->ob_size;
2212 if (start < 0)
2213 start = 0;
2214 }
2215 if (stop < 0) {
2216 stop += self->ob_size;
2217 if (stop < 0)
2218 stop = 0;
2219 }
Neal Norwitzf0769532004-08-13 03:18:29 +00002220 for (i = start; i < stop && i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002221 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2222 if (cmp > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002223 return PyInt_FromLong((long)i);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002224 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002225 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002226 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002227 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00002228 return NULL;
2229}
2230
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002231static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002232listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002233{
2234 int count = 0;
2235 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002236
Guido van Rossume6f7d181991-10-20 20:20:40 +00002237 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002238 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2239 if (cmp > 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002240 count++;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002241 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002242 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00002243 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002244 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002245}
2246
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002247static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002248listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002249{
2250 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002251
Guido van Rossumed98d481991-03-06 13:07:53 +00002252 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002253 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2254 if (cmp > 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002255 if (list_ass_slice(self, i, i+1,
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +00002256 (PyObject *)NULL) == 0)
2257 Py_RETURN_NONE;
2258 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002259 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002260 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002261 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002262 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002263 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00002264 return NULL;
2265}
2266
Jeremy Hylton8caad492000-06-23 14:18:11 +00002267static int
2268list_traverse(PyListObject *o, visitproc visit, void *arg)
2269{
2270 int i, err;
2271 PyObject *x;
2272
2273 for (i = o->ob_size; --i >= 0; ) {
2274 x = o->ob_item[i];
2275 if (x != NULL) {
2276 err = visit(x, arg);
2277 if (err)
2278 return err;
2279 }
2280 }
2281 return 0;
2282}
2283
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002284static PyObject *
2285list_richcompare(PyObject *v, PyObject *w, int op)
2286{
2287 PyListObject *vl, *wl;
2288 int i;
2289
2290 if (!PyList_Check(v) || !PyList_Check(w)) {
2291 Py_INCREF(Py_NotImplemented);
2292 return Py_NotImplemented;
2293 }
2294
2295 vl = (PyListObject *)v;
2296 wl = (PyListObject *)w;
2297
2298 if (vl->ob_size != wl->ob_size && (op == Py_EQ || op == Py_NE)) {
2299 /* Shortcut: if the lengths differ, the lists differ */
2300 PyObject *res;
2301 if (op == Py_EQ)
2302 res = Py_False;
2303 else
2304 res = Py_True;
2305 Py_INCREF(res);
2306 return res;
2307 }
2308
2309 /* Search for the first index where items are different */
2310 for (i = 0; i < vl->ob_size && i < wl->ob_size; i++) {
2311 int k = PyObject_RichCompareBool(vl->ob_item[i],
2312 wl->ob_item[i], Py_EQ);
2313 if (k < 0)
2314 return NULL;
2315 if (!k)
2316 break;
2317 }
2318
2319 if (i >= vl->ob_size || i >= wl->ob_size) {
2320 /* No more items to compare -- compare sizes */
2321 int vs = vl->ob_size;
2322 int ws = wl->ob_size;
2323 int cmp;
2324 PyObject *res;
2325 switch (op) {
2326 case Py_LT: cmp = vs < ws; break;
Tim Peters6ee42342001-07-06 17:45:43 +00002327 case Py_LE: cmp = vs <= ws; break;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002328 case Py_EQ: cmp = vs == ws; break;
2329 case Py_NE: cmp = vs != ws; break;
2330 case Py_GT: cmp = vs > ws; break;
2331 case Py_GE: cmp = vs >= ws; break;
2332 default: return NULL; /* cannot happen */
2333 }
2334 if (cmp)
2335 res = Py_True;
2336 else
2337 res = Py_False;
2338 Py_INCREF(res);
2339 return res;
2340 }
2341
2342 /* We have an item that differs -- shortcuts for EQ/NE */
2343 if (op == Py_EQ) {
2344 Py_INCREF(Py_False);
2345 return Py_False;
2346 }
2347 if (op == Py_NE) {
2348 Py_INCREF(Py_True);
2349 return Py_True;
2350 }
2351
2352 /* Compare the final item again using the proper operator */
2353 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
2354}
2355
Tim Peters6d6c1a32001-08-02 04:15:00 +00002356static int
2357list_init(PyListObject *self, PyObject *args, PyObject *kw)
2358{
2359 PyObject *arg = NULL;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002360 static const char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002361
2362 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2363 return -1;
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002364
2365 /* Verify list invariants established by PyType_GenericAlloc() */
Armin Rigoa37bbf22004-07-30 11:20:18 +00002366 assert(0 <= self->ob_size);
2367 assert(self->ob_size <= self->allocated || self->allocated == -1);
2368 assert(self->ob_item != NULL ||
2369 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002370
Raymond Hettinger90a39bf2004-02-15 03:57:00 +00002371 /* Empty previous contents */
Armin Rigo93677f02004-07-29 12:40:23 +00002372 if (self->ob_item != NULL) {
2373 (void)list_clear(self);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +00002374 }
2375 if (arg != NULL) {
2376 PyObject *rv = listextend(self, arg);
2377 if (rv == NULL)
2378 return -1;
2379 Py_DECREF(rv);
2380 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002381 return 0;
2382}
2383
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002384static long
2385list_nohash(PyObject *self)
2386{
2387 PyErr_SetString(PyExc_TypeError, "list objects are unhashable");
2388 return -1;
2389}
2390
Raymond Hettinger1021c442003-11-07 15:38:09 +00002391static PyObject *list_iter(PyObject *seq);
2392static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2393
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002394PyDoc_STRVAR(getitem_doc,
2395"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002396PyDoc_STRVAR(reversed_doc,
2397"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002398PyDoc_STRVAR(append_doc,
2399"L.append(object) -- append object to end");
2400PyDoc_STRVAR(extend_doc,
Raymond Hettingerf8bcfb12002-12-29 05:49:09 +00002401"L.extend(iterable) -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002402PyDoc_STRVAR(insert_doc,
2403"L.insert(index, object) -- insert object before index");
2404PyDoc_STRVAR(pop_doc,
2405"L.pop([index]) -> item -- remove and return item at index (default last)");
2406PyDoc_STRVAR(remove_doc,
2407"L.remove(value) -- remove first occurrence of value");
2408PyDoc_STRVAR(index_doc,
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002409"L.index(value, [start, [stop]]) -> integer -- return first index of value");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002410PyDoc_STRVAR(count_doc,
2411"L.count(value) -> integer -- return number of occurrences of value");
2412PyDoc_STRVAR(reverse_doc,
2413"L.reverse() -- reverse *IN PLACE*");
2414PyDoc_STRVAR(sort_doc,
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002415"L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\n\
2416cmp(x, y) -> -1, 0, 1");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002417
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002418static PyObject *list_subscript(PyListObject*, PyObject*);
2419
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002420static PyMethodDef list_methods[] = {
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002421 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
Raymond Hettinger1021c442003-11-07 15:38:09 +00002422 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002423 {"append", (PyCFunction)listappend, METH_O, append_doc},
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002424 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002425 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002426 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002427 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002428 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002429 {"count", (PyCFunction)listcount, METH_O, count_doc},
2430 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002431 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
Tim Petersa64dc242002-08-01 02:13:36 +00002432 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002433};
2434
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002435static PySequenceMethods list_as_sequence = {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002436 (inquiry)list_length, /* sq_length */
2437 (binaryfunc)list_concat, /* sq_concat */
2438 (intargfunc)list_repeat, /* sq_repeat */
2439 (intargfunc)list_item, /* sq_item */
2440 (intintargfunc)list_slice, /* sq_slice */
2441 (intobjargproc)list_ass_item, /* sq_ass_item */
2442 (intintobjargproc)list_ass_slice, /* sq_ass_slice */
2443 (objobjproc)list_contains, /* sq_contains */
2444 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2445 (intargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002446};
2447
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002448PyDoc_STRVAR(list_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002449"list() -> new list\n"
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002450"list(sequence) -> new list initialized from sequence's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002451
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002452static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002453list_subscript(PyListObject* self, PyObject* item)
2454{
2455 if (PyInt_Check(item)) {
2456 long i = PyInt_AS_LONG(item);
2457 if (i < 0)
2458 i += PyList_GET_SIZE(self);
2459 return list_item(self, i);
2460 }
2461 else if (PyLong_Check(item)) {
2462 long i = PyLong_AsLong(item);
2463 if (i == -1 && PyErr_Occurred())
2464 return NULL;
2465 if (i < 0)
2466 i += PyList_GET_SIZE(self);
2467 return list_item(self, i);
2468 }
2469 else if (PySlice_Check(item)) {
2470 int start, stop, step, slicelength, cur, i;
2471 PyObject* result;
2472 PyObject* it;
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002473 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002474
2475 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
2476 &start, &stop, &step, &slicelength) < 0) {
2477 return NULL;
2478 }
2479
2480 if (slicelength <= 0) {
2481 return PyList_New(0);
2482 }
2483 else {
2484 result = PyList_New(slicelength);
2485 if (!result) return NULL;
2486
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002487 src = self->ob_item;
2488 dest = ((PyListObject *)result)->ob_item;
Tim Peters3b01a122002-07-19 02:35:45 +00002489 for (cur = start, i = 0; i < slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002490 cur += step, i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002491 it = src[cur];
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002492 Py_INCREF(it);
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002493 dest[i] = it;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002494 }
Tim Peters3b01a122002-07-19 02:35:45 +00002495
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002496 return result;
2497 }
2498 }
2499 else {
2500 PyErr_SetString(PyExc_TypeError,
2501 "list indices must be integers");
2502 return NULL;
2503 }
2504}
2505
Tim Peters3b01a122002-07-19 02:35:45 +00002506static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002507list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2508{
2509 if (PyInt_Check(item)) {
2510 long i = PyInt_AS_LONG(item);
2511 if (i < 0)
2512 i += PyList_GET_SIZE(self);
2513 return list_ass_item(self, i, value);
2514 }
2515 else if (PyLong_Check(item)) {
2516 long i = PyLong_AsLong(item);
2517 if (i == -1 && PyErr_Occurred())
2518 return -1;
2519 if (i < 0)
2520 i += PyList_GET_SIZE(self);
2521 return list_ass_item(self, i, value);
2522 }
2523 else if (PySlice_Check(item)) {
2524 int start, stop, step, slicelength;
2525
2526 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
2527 &start, &stop, &step, &slicelength) < 0) {
2528 return -1;
2529 }
2530
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002531 /* treat L[slice(a,b)] = v _exactly_ like L[a:b] = v */
2532 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
2533 return list_ass_slice(self, start, stop, value);
2534
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002535 if (value == NULL) {
2536 /* delete slice */
Raymond Hettinger4bb95402004-02-13 11:36:39 +00002537 PyObject **garbage;
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002538 int cur, i;
Tim Peters3b01a122002-07-19 02:35:45 +00002539
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002540 if (slicelength <= 0)
2541 return 0;
2542
2543 if (step < 0) {
2544 stop = start + 1;
2545 start = stop + step*(slicelength - 1) - 1;
2546 step = -step;
2547 }
2548
2549 garbage = (PyObject**)
2550 PyMem_MALLOC(slicelength*sizeof(PyObject*));
Tim Peters3b01a122002-07-19 02:35:45 +00002551
2552 /* drawing pictures might help
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002553 understand these for loops */
Guido van Rossum75a20b12002-06-11 12:22:28 +00002554 for (cur = start, i = 0;
2555 cur < stop;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002556 cur += step, i++) {
2557 int lim = step;
2558
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002559 garbage[i] = PyList_GET_ITEM(self, cur);
2560
Michael W. Hudson56796f62002-07-29 14:35:04 +00002561 if (cur + step >= self->ob_size) {
2562 lim = self->ob_size - cur - 1;
2563 }
2564
Tim Petersb38e2b62004-07-29 02:29:26 +00002565 memmove(self->ob_item + cur - i,
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002566 self->ob_item + cur + 1,
2567 lim * sizeof(PyObject *));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002568 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002569
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002570 for (cur = start + slicelength*step + 1;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002571 cur < self->ob_size; cur++) {
2572 PyList_SET_ITEM(self, cur - slicelength,
2573 PyList_GET_ITEM(self, cur));
2574 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002575
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002576 self->ob_size -= slicelength;
Raymond Hettingerd4ff7412004-03-15 09:01:31 +00002577 list_resize(self, self->ob_size);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002578
2579 for (i = 0; i < slicelength; i++) {
2580 Py_DECREF(garbage[i]);
2581 }
2582 PyMem_FREE(garbage);
2583
2584 return 0;
2585 }
2586 else {
2587 /* assign slice */
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002588 PyObject **garbage, *ins, *seq, **seqitems, **selfitems;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002589 int cur, i;
2590
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002591 /* protect against a[::-1] = a */
Tim Peters3b01a122002-07-19 02:35:45 +00002592 if (self == (PyListObject*)value) {
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002593 seq = list_slice((PyListObject*)value, 0,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002594 PyList_GET_SIZE(value));
Tim Peters3b01a122002-07-19 02:35:45 +00002595 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002596 else {
Tim Petersb38e2b62004-07-29 02:29:26 +00002597 seq = PySequence_Fast(value,
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002598 "must assign iterable to extended slice");
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002599 if (!seq)
2600 return -1;
2601 }
2602
2603 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2604 PyErr_Format(PyExc_ValueError,
2605 "attempt to assign sequence of size %d to extended slice of size %d",
2606 PySequence_Fast_GET_SIZE(seq),
2607 slicelength);
2608 Py_DECREF(seq);
2609 return -1;
2610 }
2611
2612 if (!slicelength) {
2613 Py_DECREF(seq);
2614 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002615 }
2616
2617 garbage = (PyObject**)
2618 PyMem_MALLOC(slicelength*sizeof(PyObject*));
Tim Peters3b01a122002-07-19 02:35:45 +00002619
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002620 selfitems = self->ob_item;
Raymond Hettinger42bec932004-03-12 16:38:17 +00002621 seqitems = PySequence_Fast_ITEMS(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002622 for (cur = start, i = 0; i < slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002623 cur += step, i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002624 garbage[i] = selfitems[cur];
2625 ins = seqitems[i];
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002626 Py_INCREF(ins);
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002627 selfitems[cur] = ins;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002628 }
2629
2630 for (i = 0; i < slicelength; i++) {
2631 Py_DECREF(garbage[i]);
2632 }
Tim Peters3b01a122002-07-19 02:35:45 +00002633
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002634 PyMem_FREE(garbage);
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002635 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002636
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002637 return 0;
2638 }
Tim Peters3b01a122002-07-19 02:35:45 +00002639 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002640 else {
Tim Peters3b01a122002-07-19 02:35:45 +00002641 PyErr_SetString(PyExc_TypeError,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002642 "list indices must be integers");
2643 return -1;
2644 }
2645}
2646
2647static PyMappingMethods list_as_mapping = {
2648 (inquiry)list_length,
2649 (binaryfunc)list_subscript,
2650 (objobjargproc)list_ass_subscript
2651};
2652
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002653PyTypeObject PyList_Type = {
2654 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002655 0,
2656 "list",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002657 sizeof(PyListObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002658 0,
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002659 (destructor)list_dealloc, /* tp_dealloc */
2660 (printfunc)list_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002661 0, /* tp_getattr */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002662 0, /* tp_setattr */
2663 0, /* tp_compare */
2664 (reprfunc)list_repr, /* tp_repr */
2665 0, /* tp_as_number */
2666 &list_as_sequence, /* tp_as_sequence */
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002667 &list_as_mapping, /* tp_as_mapping */
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002668 list_nohash, /* tp_hash */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002669 0, /* tp_call */
2670 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002671 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002672 0, /* tp_setattro */
2673 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002674 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00002675 Py_TPFLAGS_BASETYPE, /* tp_flags */
2676 list_doc, /* tp_doc */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002677 (traverseproc)list_traverse, /* tp_traverse */
2678 (inquiry)list_clear, /* tp_clear */
2679 list_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002680 0, /* tp_weaklistoffset */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002681 list_iter, /* tp_iter */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002682 0, /* tp_iternext */
2683 list_methods, /* tp_methods */
2684 0, /* tp_members */
2685 0, /* tp_getset */
2686 0, /* tp_base */
2687 0, /* tp_dict */
2688 0, /* tp_descr_get */
2689 0, /* tp_descr_set */
2690 0, /* tp_dictoffset */
2691 (initproc)list_init, /* tp_init */
2692 PyType_GenericAlloc, /* tp_alloc */
2693 PyType_GenericNew, /* tp_new */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002694 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002695};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002696
2697
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002698/*********************** List Iterator **************************/
2699
2700typedef struct {
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002701 PyObject_HEAD
2702 long it_index;
Guido van Rossum86103ae2002-07-16 20:07:32 +00002703 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002704} listiterobject;
2705
2706PyTypeObject PyListIter_Type;
2707
Guido van Rossum5086e492002-07-16 15:56:52 +00002708static PyObject *
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002709list_iter(PyObject *seq)
2710{
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002711 listiterobject *it;
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002712
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002713 if (!PyList_Check(seq)) {
2714 PyErr_BadInternalCall();
2715 return NULL;
2716 }
2717 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2718 if (it == NULL)
2719 return NULL;
2720 it->it_index = 0;
2721 Py_INCREF(seq);
2722 it->it_seq = (PyListObject *)seq;
2723 _PyObject_GC_TRACK(it);
2724 return (PyObject *)it;
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002725}
2726
2727static void
2728listiter_dealloc(listiterobject *it)
2729{
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002730 _PyObject_GC_UNTRACK(it);
Guido van Rossum86103ae2002-07-16 20:07:32 +00002731 Py_XDECREF(it->it_seq);
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002732 PyObject_GC_Del(it);
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002733}
2734
2735static int
2736listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2737{
Guido van Rossum86103ae2002-07-16 20:07:32 +00002738 if (it->it_seq == NULL)
2739 return 0;
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002740 return visit((PyObject *)it->it_seq, arg);
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002741}
2742
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002743static PyObject *
Tim Peters93b2cc42002-06-01 05:22:55 +00002744listiter_next(listiterobject *it)
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002745{
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002746 PyListObject *seq;
2747 PyObject *item;
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002748
Tim Peters93b2cc42002-06-01 05:22:55 +00002749 assert(it != NULL);
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002750 seq = it->it_seq;
Guido van Rossum86103ae2002-07-16 20:07:32 +00002751 if (seq == NULL)
2752 return NULL;
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002753 assert(PyList_Check(seq));
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002754
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002755 if (it->it_index < PyList_GET_SIZE(seq)) {
Tim Peters93b2cc42002-06-01 05:22:55 +00002756 item = PyList_GET_ITEM(seq, it->it_index);
2757 ++it->it_index;
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002758 Py_INCREF(item);
2759 return item;
2760 }
Guido van Rossum86103ae2002-07-16 20:07:32 +00002761
2762 Py_DECREF(seq);
2763 it->it_seq = NULL;
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002764 return NULL;
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002765}
2766
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002767static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +00002768listiter_len(listiterobject *it)
2769{
Raymond Hettinger40a03822004-04-12 13:05:09 +00002770 int len;
2771 if (it->it_seq) {
2772 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2773 if (len >= 0)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002774 return PyInt_FromLong((long)len);
Raymond Hettinger40a03822004-04-12 13:05:09 +00002775 }
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002776 return PyInt_FromLong(0);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002777}
2778
Armin Rigof5b3e362006-02-11 21:32:43 +00002779PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002780
2781static PyMethodDef listiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002782 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002783 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002784};
2785
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002786PyTypeObject PyListIter_Type = {
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002787 PyObject_HEAD_INIT(&PyType_Type)
2788 0, /* ob_size */
2789 "listiterator", /* tp_name */
2790 sizeof(listiterobject), /* tp_basicsize */
2791 0, /* tp_itemsize */
2792 /* methods */
2793 (destructor)listiter_dealloc, /* tp_dealloc */
2794 0, /* tp_print */
2795 0, /* tp_getattr */
2796 0, /* tp_setattr */
2797 0, /* tp_compare */
2798 0, /* tp_repr */
2799 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002800 0, /* tp_as_sequence */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002801 0, /* tp_as_mapping */
2802 0, /* tp_hash */
2803 0, /* tp_call */
2804 0, /* tp_str */
2805 PyObject_GenericGetAttr, /* tp_getattro */
2806 0, /* tp_setattro */
2807 0, /* tp_as_buffer */
2808 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2809 0, /* tp_doc */
2810 (traverseproc)listiter_traverse, /* tp_traverse */
2811 0, /* tp_clear */
2812 0, /* tp_richcompare */
2813 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002814 PyObject_SelfIter, /* tp_iter */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002815 (iternextfunc)listiter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002816 listiter_methods, /* tp_methods */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002817 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002818};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002819
2820/*********************** List Reverse Iterator **************************/
2821
2822typedef struct {
2823 PyObject_HEAD
2824 long it_index;
Raymond Hettinger001f2282003-11-08 11:58:44 +00002825 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002826} listreviterobject;
2827
2828PyTypeObject PyListRevIter_Type;
2829
2830static PyObject *
2831list_reversed(PyListObject *seq, PyObject *unused)
2832{
2833 listreviterobject *it;
2834
2835 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2836 if (it == NULL)
2837 return NULL;
2838 assert(PyList_Check(seq));
2839 it->it_index = PyList_GET_SIZE(seq) - 1;
2840 Py_INCREF(seq);
2841 it->it_seq = seq;
2842 PyObject_GC_Track(it);
2843 return (PyObject *)it;
2844}
2845
2846static void
2847listreviter_dealloc(listreviterobject *it)
2848{
2849 PyObject_GC_UnTrack(it);
2850 Py_XDECREF(it->it_seq);
2851 PyObject_GC_Del(it);
2852}
2853
2854static int
2855listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2856{
2857 if (it->it_seq == NULL)
2858 return 0;
2859 return visit((PyObject *)it->it_seq, arg);
2860}
2861
2862static PyObject *
2863listreviter_next(listreviterobject *it)
2864{
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002865 PyObject *item;
2866 long index = it->it_index;
2867 PyListObject *seq = it->it_seq;
Raymond Hettinger1021c442003-11-07 15:38:09 +00002868
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002869 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2870 item = PyList_GET_ITEM(seq, index);
Raymond Hettinger1021c442003-11-07 15:38:09 +00002871 it->it_index--;
2872 Py_INCREF(item);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002873 return item;
Raymond Hettinger1021c442003-11-07 15:38:09 +00002874 }
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002875 it->it_index = -1;
2876 if (seq != NULL) {
2877 it->it_seq = NULL;
2878 Py_DECREF(seq);
2879 }
2880 return NULL;
Raymond Hettinger1021c442003-11-07 15:38:09 +00002881}
2882
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002883static int
2884listreviter_len(listreviterobject *it)
2885{
Raymond Hettinger40a03822004-04-12 13:05:09 +00002886 int len = it->it_index + 1;
2887 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2888 return 0;
2889 return len;
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002890}
2891
2892static PySequenceMethods listreviter_as_sequence = {
2893 (inquiry)listreviter_len, /* sq_length */
2894 0, /* sq_concat */
2895};
2896
Raymond Hettinger1021c442003-11-07 15:38:09 +00002897PyTypeObject PyListRevIter_Type = {
2898 PyObject_HEAD_INIT(&PyType_Type)
2899 0, /* ob_size */
2900 "listreverseiterator", /* tp_name */
2901 sizeof(listreviterobject), /* tp_basicsize */
2902 0, /* tp_itemsize */
2903 /* methods */
2904 (destructor)listreviter_dealloc, /* tp_dealloc */
2905 0, /* tp_print */
2906 0, /* tp_getattr */
2907 0, /* tp_setattr */
2908 0, /* tp_compare */
2909 0, /* tp_repr */
2910 0, /* tp_as_number */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002911 &listreviter_as_sequence, /* tp_as_sequence */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002912 0, /* tp_as_mapping */
2913 0, /* tp_hash */
2914 0, /* tp_call */
2915 0, /* tp_str */
2916 PyObject_GenericGetAttr, /* tp_getattro */
2917 0, /* tp_setattro */
2918 0, /* tp_as_buffer */
2919 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2920 0, /* tp_doc */
2921 (traverseproc)listreviter_traverse, /* tp_traverse */
2922 0, /* tp_clear */
2923 0, /* tp_richcompare */
2924 0, /* tp_weaklistoffset */
2925 PyObject_SelfIter, /* tp_iter */
2926 (iternextfunc)listreviter_next, /* tp_iternext */
2927 0,
2928};