blob: 4385e4b01fd1c84c080a27b8f5cabc6822e82043 [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
Nicholas Bastin1ce9e4c2004-06-17 18:27:18 +00005#ifdef __SUNPRO_C
6#pragma error_messages (off,E_END_OF_LOOP_CODE_NOT_REACHED)
7#endif
8
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00009#ifdef STDC_HEADERS
10#include <stddef.h>
11#else
12#include <sys/types.h> /* For size_t */
13#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014
Guido van Rossuma46d51d1995-01-26 22:59:43 +000015static int
Raymond Hettingerd4ff7412004-03-15 09:01:31 +000016list_resize(PyListObject *self, int newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000017{
Raymond Hettinger4bb95402004-02-13 11:36:39 +000018 PyObject **items;
19 size_t _new_size;
Tim Peters65b8b842001-05-26 05:28:40 +000020
Raymond Hettinger4bb95402004-02-13 11:36:39 +000021 /* Bypass realloc() when a previous overallocation is large enough
22 to accommodate the newsize. If the newsize is 16 smaller than the
23 current size, then proceed with the realloc() to shrink the list.
24 */
25
26 if (self->allocated >= newsize &&
27 self->ob_size < newsize + 16 &&
28 self->ob_item != NULL) {
29 self->ob_size = newsize;
30 return 0;
31 }
32
33 /* This over-allocates proportional to the list size, making room
Tim Peters65b8b842001-05-26 05:28:40 +000034 * for additional growth. The over-allocation is mild, but is
35 * enough to give linear-time amortized behavior over a long
36 * sequence of appends() in the presence of a poorly-performing
Raymond Hettingerab517d22004-02-14 18:34:46 +000037 * system realloc().
38 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
Tim Peters65b8b842001-05-26 05:28:40 +000039 */
Raymond Hettingerd4ff7412004-03-15 09:01:31 +000040 _new_size = (newsize >> 3) + (self->ob_size < 8 ? 3 : 6) + newsize;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000041 items = self->ob_item;
42 if (_new_size <= ((~(size_t)0) / sizeof(PyObject *)))
43 PyMem_RESIZE(items, PyObject *, _new_size);
44 else
45 items = NULL;
46 if (items == NULL) {
47 PyErr_NoMemory();
48 return -1;
49 }
50 self->ob_item = items;
51 self->ob_size = newsize;
52 self->allocated = _new_size;
53 return 0;
54}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000055
Raymond Hettinger0468e412004-05-05 05:37:53 +000056/* Empty list reuse scheme to save calls to malloc and free */
57#define MAXFREELISTS 80
58static PyListObject *free_lists[MAXFREELISTS];
59static int num_free_lists = 0;
60
Guido van Rossumc0b618a1997-05-02 03:12:38 +000061PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000062PyList_New(int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064 PyListObject *op;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000065 size_t nbytes;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 if (size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000067 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068 return NULL;
69 }
Tim Peters7049d812004-01-18 20:31:02 +000070 nbytes = size * sizeof(PyObject *);
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000071 /* Check for overflow */
Raymond Hettingerfdfe6182004-05-05 06:28:16 +000072 if (nbytes / sizeof(PyObject *) != (size_t)size)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000073 return PyErr_NoMemory();
Raymond Hettinger0468e412004-05-05 05:37:53 +000074 if (num_free_lists) {
75 num_free_lists--;
76 op = free_lists[num_free_lists];
77 _Py_NewReference((PyObject *)op);
78 } else {
79 op = PyObject_GC_New(PyListObject, &PyList_Type);
80 if (op == NULL)
81 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082 }
Raymond Hettingerfdfe6182004-05-05 06:28:16 +000083 if (size <= 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084 op->ob_item = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +000086 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
Raymond Hettingerfdfe6182004-05-05 06:28:16 +000087 if (op->ob_item == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088 return PyErr_NoMemory();
Tim Peters7049d812004-01-18 20:31:02 +000089 memset(op->ob_item, 0, sizeof(*op->ob_item) * size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 }
Neil Schemenauere83c00e2001-08-29 23:54:21 +000091 op->ob_size = size;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000092 op->allocated = size;
Neil Schemenauere83c00e2001-08-29 23:54:21 +000093 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000094 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095}
96
97int
Fred Drakea2f55112000-07-09 15:16:51 +000098PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100 if (!PyList_Check(op)) {
101 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102 return -1;
103 }
104 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000105 return ((PyListObject *)op) -> ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106}
107
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000108static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000109
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000111PyList_GetItem(PyObject *op, int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113 if (!PyList_Check(op)) {
114 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115 return NULL;
116 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000117 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000118 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000119 indexerr = PyString_FromString(
120 "list index out of range");
121 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122 return NULL;
123 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000124 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125}
126
127int
Fred Drakea2f55112000-07-09 15:16:51 +0000128PyList_SetItem(register PyObject *op, register int i,
129 register PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000131 register PyObject *olditem;
132 register PyObject **p;
133 if (!PyList_Check(op)) {
134 Py_XDECREF(newitem);
135 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000136 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000138 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
139 Py_XDECREF(newitem);
140 PyErr_SetString(PyExc_IndexError,
141 "list assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000142 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000144 p = ((PyListObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000145 olditem = *p;
146 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000147 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148 return 0;
149}
150
151static int
Fred Drakea2f55112000-07-09 15:16:51 +0000152ins1(PyListObject *self, int where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153{
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000154 int i, n = self->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000155 PyObject **items;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000156 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000158 return -1;
159 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000160 if (n == INT_MAX) {
Trent Micka5846642000-08-13 22:47:45 +0000161 PyErr_SetString(PyExc_OverflowError,
162 "cannot add more objects to list");
163 return -1;
164 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000165
Raymond Hettingerd4ff7412004-03-15 09:01:31 +0000166 if (list_resize(self, n+1) == -1)
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000167 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000168
Guido van Rossum3a3cca52003-04-14 20:58:14 +0000169 if (where < 0) {
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000170 where += n;
Guido van Rossum3a3cca52003-04-14 20:58:14 +0000171 if (where < 0)
172 where = 0;
173 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000174 if (where > n)
175 where = n;
176 items = self->ob_item;
177 for (i = n; --i >= where; )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000178 items[i+1] = items[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180 items[where] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181 return 0;
182}
183
184int
Fred Drakea2f55112000-07-09 15:16:51 +0000185PyList_Insert(PyObject *op, int where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000187 if (!PyList_Check(op)) {
188 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000189 return -1;
190 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192}
193
Raymond Hettinger40a03822004-04-12 13:05:09 +0000194static int
195app1(PyListObject *self, PyObject *v)
196{
197 int n = PyList_GET_SIZE(self);
198
199 assert (v != NULL);
200 if (n == INT_MAX) {
201 PyErr_SetString(PyExc_OverflowError,
202 "cannot add more objects to list");
203 return -1;
204 }
205
206 if (list_resize(self, n+1) == -1)
207 return -1;
208
209 Py_INCREF(v);
210 PyList_SET_ITEM(self, n, v);
211 return 0;
212}
213
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214int
Fred Drakea2f55112000-07-09 15:16:51 +0000215PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216{
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000217 if (PyList_Check(op) && (newitem != NULL))
218 return app1((PyListObject *)op, newitem);
219 PyErr_BadInternalCall();
220 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221}
222
223/* Methods */
224
225static void
Fred Drakea2f55112000-07-09 15:16:51 +0000226list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227{
228 int i;
Guido van Rossumff413af2002-03-28 20:34:59 +0000229 PyObject_GC_UnTrack(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000230 Py_TRASHCAN_SAFE_BEGIN(op)
Jack Jansen7874d1f1995-01-19 12:09:27 +0000231 if (op->ob_item != NULL) {
Guido van Rossumfa717011999-06-09 15:19:34 +0000232 /* Do it backwards, for Christian Tismer.
233 There's a simple test case where somehow this reduces
234 thrashing when a *very* large list is created and
235 immediately deleted. */
236 i = op->ob_size;
237 while (--i >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238 Py_XDECREF(op->ob_item[i]);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000239 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000240 PyMem_FREE(op->ob_item);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000241 }
Raymond Hettinger0468e412004-05-05 05:37:53 +0000242 if (num_free_lists < MAXFREELISTS && PyList_CheckExact(op))
243 free_lists[num_free_lists++] = op;
244 else
245 op->ob_type->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000246 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247}
248
Guido van Rossum90933611991-06-07 16:10:43 +0000249static int
Fred Drakea2f55112000-07-09 15:16:51 +0000250list_print(PyListObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251{
252 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000253
254 i = Py_ReprEnter((PyObject*)op);
255 if (i != 0) {
256 if (i < 0)
257 return i;
258 fprintf(fp, "[...]");
259 return 0;
260 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261 fprintf(fp, "[");
Guido van Rossum90933611991-06-07 16:10:43 +0000262 for (i = 0; i < op->ob_size; i++) {
263 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264 fprintf(fp, ", ");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000265 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
266 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000267 return -1;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000268 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000269 }
270 fprintf(fp, "]");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000271 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000272 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273}
274
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000276list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278 int i;
Tim Petersa7259592001-06-16 05:11:17 +0000279 PyObject *s, *temp;
280 PyObject *pieces = NULL, *result = NULL;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000281
282 i = Py_ReprEnter((PyObject*)v);
283 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000284 return i > 0 ? PyString_FromString("[...]") : NULL;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000285 }
Tim Petersa7259592001-06-16 05:11:17 +0000286
287 if (v->ob_size == 0) {
288 result = PyString_FromString("[]");
289 goto Done;
290 }
291
292 pieces = PyList_New(0);
293 if (pieces == NULL)
294 goto Done;
295
296 /* Do repr() on each element. Note that this may mutate the list,
297 so must refetch the list size on each iteration. */
298 for (i = 0; i < v->ob_size; ++i) {
299 int status;
300 s = PyObject_Repr(v->ob_item[i]);
301 if (s == NULL)
302 goto Done;
303 status = PyList_Append(pieces, s);
304 Py_DECREF(s); /* append created a new ref */
305 if (status < 0)
306 goto Done;
307 }
308
309 /* Add "[]" decorations to the first and last items. */
310 assert(PyList_GET_SIZE(pieces) > 0);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311 s = PyString_FromString("[");
Tim Petersa7259592001-06-16 05:11:17 +0000312 if (s == NULL)
313 goto Done;
314 temp = PyList_GET_ITEM(pieces, 0);
315 PyString_ConcatAndDel(&s, temp);
316 PyList_SET_ITEM(pieces, 0, s);
317 if (s == NULL)
318 goto Done;
319
320 s = PyString_FromString("]");
321 if (s == NULL)
322 goto Done;
323 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
324 PyString_ConcatAndDel(&temp, s);
325 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
326 if (temp == NULL)
327 goto Done;
328
329 /* Paste them all together with ", " between. */
330 s = PyString_FromString(", ");
331 if (s == NULL)
332 goto Done;
333 result = _PyString_Join(s, pieces);
Tim Peters3b01a122002-07-19 02:35:45 +0000334 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000335
336Done:
337 Py_XDECREF(pieces);
Guido van Rossumfb376de1998-04-10 22:47:27 +0000338 Py_ReprLeave((PyObject *)v);
Tim Petersa7259592001-06-16 05:11:17 +0000339 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340}
341
342static int
Fred Drakea2f55112000-07-09 15:16:51 +0000343list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344{
345 return a->ob_size;
346}
347
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000348static int
Fred Drakea2f55112000-07-09 15:16:51 +0000349list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000350{
Raymond Hettingeraae59992002-09-05 14:23:49 +0000351 int i, cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000352
Raymond Hettingeraae59992002-09-05 14:23:49 +0000353 for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i)
354 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
Guido van Rossum65e1cea2001-01-17 22:11:59 +0000355 Py_EQ);
Neal Norwitzbb9c5f52002-09-05 21:32:55 +0000356 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000357}
358
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000359static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000360list_item(PyListObject *a, int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361{
362 if (i < 0 || i >= a->ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000363 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364 indexerr = PyString_FromString(
365 "list index out of range");
366 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367 return NULL;
368 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370 return a->ob_item[i];
371}
372
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000373static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000374list_slice(PyListObject *a, int ilow, int ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000376 PyListObject *np;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000377 PyObject **src, **dest;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000378 int i, len;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000379 if (ilow < 0)
380 ilow = 0;
381 else if (ilow > a->ob_size)
382 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383 if (ihigh < ilow)
384 ihigh = ilow;
385 else if (ihigh > a->ob_size)
386 ihigh = a->ob_size;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000387 len = ihigh - ilow;
388 np = (PyListObject *) PyList_New(len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389 if (np == NULL)
390 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000391
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000392 src = a->ob_item + ilow;
393 dest = np->ob_item;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000394 for (i = 0; i < len; i++) {
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000395 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396 Py_INCREF(v);
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000397 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000399 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400}
401
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000403PyList_GetSlice(PyObject *a, int ilow, int ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000404{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405 if (!PyList_Check(a)) {
406 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000407 return NULL;
408 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000409 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000410}
411
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000413list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414{
415 int size;
416 int i;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000417 PyObject **src, **dest;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000418 PyListObject *np;
419 if (!PyList_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000420 PyErr_Format(PyExc_TypeError,
Fred Drake914a2ed2000-06-01 14:31:03 +0000421 "can only concatenate list (not \"%.200s\") to list",
422 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423 return NULL;
424 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425#define b ((PyListObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426 size = a->ob_size + b->ob_size;
Guido van Rossuma5c0e6d2002-10-11 21:05:56 +0000427 if (size < 0)
428 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000429 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000431 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000432 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000433 src = a->ob_item;
434 dest = np->ob_item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435 for (i = 0; i < a->ob_size; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000436 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000437 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000438 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000440 src = b->ob_item;
441 dest = np->ob_item + a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442 for (i = 0; i < b->ob_size; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000443 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000444 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000445 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000447 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448#undef b
449}
450
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000452list_repeat(PyListObject *a, int n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000453{
454 int i, j;
455 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456 PyListObject *np;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000457 PyObject **p, **items;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000458 PyObject *elem;
Guido van Rossumed98d481991-03-06 13:07:53 +0000459 if (n < 0)
460 n = 0;
461 size = a->ob_size * n;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000462 if (size == 0)
463 return PyList_New(0);
Guido van Rossumbfa5a142002-10-11 23:39:35 +0000464 if (n && size/n != a->ob_size)
Guido van Rossuma5c0e6d2002-10-11 21:05:56 +0000465 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000467 if (np == NULL)
468 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000469
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000470 items = np->ob_item;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000471 if (a->ob_size == 1) {
472 elem = a->ob_item[0];
473 for (i = 0; i < n; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000474 items[i] = elem;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000475 Py_INCREF(elem);
476 }
477 return (PyObject *) np;
478 }
Guido van Rossumed98d481991-03-06 13:07:53 +0000479 p = np->ob_item;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000480 items = a->ob_item;
Guido van Rossumed98d481991-03-06 13:07:53 +0000481 for (i = 0; i < n; i++) {
482 for (j = 0; j < a->ob_size; j++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000483 *p = items[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000484 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000485 p++;
486 }
487 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000488 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000489}
490
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000491static int
Fred Drakea2f55112000-07-09 15:16:51 +0000492list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000494 /* Because [X]DECREF can recursively invoke list operations on
495 this list, we must postpone all [X]DECREF activity until
496 after the list is back in its canonical shape. Therefore
497 we must allocate an additional array, 'recycle', into which
498 we temporarily copy the items that are deleted from the
499 list. :-( */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000500 PyObject **recycle, **p;
501 PyObject **item;
Raymond Hettingerf889e102004-03-09 08:04:33 +0000502 PyObject **vitem = NULL;
Michael W. Hudson5da854f2002-11-05 17:38:05 +0000503 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000504 int n; /* Size of replacement list */
505 int d; /* Change in size */
506 int k; /* Loop index */
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000507 int s;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000508#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000509 if (v == NULL)
510 n = 0;
Michael W. Hudson5da854f2002-11-05 17:38:05 +0000511 else {
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000512 if (a == b) {
513 /* Special case "a[i:j] = a" -- copy b first */
514 int ret;
Michael W. Hudsonda0a0672003-08-15 12:06:41 +0000515 v = list_slice(b, 0, b->ob_size);
Martin v. Löwiscd12bfc2003-05-03 10:53:08 +0000516 if (v == NULL)
517 return -1;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000518 ret = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000519 Py_DECREF(v);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000520 return ret;
521 }
Raymond Hettingerf889e102004-03-09 08:04:33 +0000522 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
Michael W. Hudsonb4f49382003-08-14 17:04:28 +0000523 if(v_as_SF == NULL)
524 return -1;
525 n = PySequence_Fast_GET_SIZE(v_as_SF);
Raymond Hettinger42bec932004-03-12 16:38:17 +0000526 vitem = PySequence_Fast_ITEMS(v_as_SF);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000527 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000528 if (ilow < 0)
529 ilow = 0;
530 else if (ilow > a->ob_size)
531 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532 if (ihigh < ilow)
533 ihigh = ilow;
534 else if (ihigh > a->ob_size)
535 ihigh = a->ob_size;
536 item = a->ob_item;
537 d = n - (ihigh-ilow);
Martin v. Löwiscd12bfc2003-05-03 10:53:08 +0000538 if (ihigh > ilow) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000539 p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
Martin v. Löwiscd12bfc2003-05-03 10:53:08 +0000540 if (recycle == NULL) {
541 PyErr_NoMemory();
542 return -1;
543 }
544 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000545 else
546 p = recycle = NULL;
547 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
Raymond Hettinger66d31f82004-03-10 11:44:04 +0000548 memcpy(p, &item[ilow], (ihigh - ilow)*sizeof(PyObject *));
Raymond Hettingerf889e102004-03-09 08:04:33 +0000549 p += ihigh - ilow;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550 if (d < 0) {
Raymond Hettingerf889e102004-03-09 08:04:33 +0000551 memmove(&item[ihigh+d], &item[ihigh],
552 (a->ob_size - ihigh)*sizeof(PyObject *));
Raymond Hettingerd4ff7412004-03-15 09:01:31 +0000553 list_resize(a, a->ob_size + d);
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000554 item = a->ob_item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555 }
556 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000557 else { /* Insert d items; recycle ihigh-ilow items */
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000558 s = a->ob_size;
Raymond Hettingerd4ff7412004-03-15 09:01:31 +0000559 if (list_resize(a, s+d) == -1) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000560 if (recycle != NULL)
561 PyMem_DEL(recycle);
Raymond Hettinger2731ae42004-02-14 03:07:21 +0000562 return -1;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000563 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000564 item = a->ob_item;
Raymond Hettingerf889e102004-03-09 08:04:33 +0000565 memmove(&item[ihigh+d], &item[ihigh],
566 (s - ihigh)*sizeof(PyObject *));
Raymond Hettinger66d31f82004-03-10 11:44:04 +0000567 memcpy(p, &item[ilow], (ihigh - ilow)*sizeof(PyObject *));
Raymond Hettingerf889e102004-03-09 08:04:33 +0000568 p += ihigh - ilow;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569 }
570 for (k = 0; k < n; k++, ilow++) {
Raymond Hettingerf889e102004-03-09 08:04:33 +0000571 PyObject *w = vitem[k];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573 item[ilow] = w;
574 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000575 if (recycle) {
576 while (--p >= recycle)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000577 Py_XDECREF(*p);
578 PyMem_DEL(recycle);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000579 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000580 if (a->ob_size == 0 && a->ob_item != NULL) {
581 PyMem_FREE(a->ob_item);
582 a->ob_item = NULL;
583 }
Michael W. Hudson5da854f2002-11-05 17:38:05 +0000584 Py_XDECREF(v_as_SF);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000585 return 0;
586#undef b
587}
588
Guido van Rossum234f9421993-06-17 12:35:49 +0000589int
Fred Drakea2f55112000-07-09 15:16:51 +0000590PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000591{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592 if (!PyList_Check(a)) {
593 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000594 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000595 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000596 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000597}
598
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000599static PyObject *
600list_inplace_repeat(PyListObject *self, int n)
601{
602 PyObject **items;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000603 int size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000604
605
606 size = PyList_GET_SIZE(self);
607 if (size == 0) {
608 Py_INCREF(self);
609 return (PyObject *)self;
610 }
611
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000612 if (n < 1) {
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000613 items = self->ob_item;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000614 self->ob_item = NULL;
615 self->ob_size = 0;
616 for (i = 0; i < size; i++)
617 Py_XDECREF(items[i]);
618 PyMem_DEL(items);
619 Py_INCREF(self);
620 return (PyObject *)self;
621 }
622
Raymond Hettingerd4ff7412004-03-15 09:01:31 +0000623 if (list_resize(self, size*n) == -1)
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000624 return NULL;
625
626 p = size;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000627 items = self->ob_item;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000628 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
629 for (j = 0; j < size; j++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000630 PyObject *o = items[j];
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000631 Py_INCREF(o);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000632 items[p++] = o;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000633 }
634 }
635 Py_INCREF(self);
636 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000637}
638
Guido van Rossum4a450d01991-04-03 19:05:18 +0000639static int
Fred Drakea2f55112000-07-09 15:16:51 +0000640list_ass_item(PyListObject *a, int i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000641{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000642 PyObject *old_value;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000643 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000644 PyErr_SetString(PyExc_IndexError,
645 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000646 return -1;
647 }
648 if (v == NULL)
649 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000651 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000652 a->ob_item[i] = v;
Tim Peters3b01a122002-07-19 02:35:45 +0000653 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000654 return 0;
655}
656
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000658listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000659{
660 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000662 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000663 return NULL;
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +0000664 if (ins1(self, i, v) == 0)
665 Py_RETURN_NONE;
Raymond Hettinger501f02c2004-04-12 14:01:16 +0000666 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000667}
668
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000670listappend(PyListObject *self, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000671{
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +0000672 if (app1(self, v) == 0)
673 Py_RETURN_NONE;
Raymond Hettinger501f02c2004-04-12 14:01:16 +0000674 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000675}
676
Barry Warsawdedf6d61998-10-09 16:37:25 +0000677static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000678listextend(PyListObject *self, PyObject *b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000679{
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000680 PyObject *it; /* iter(v) */
681 int m; /* size of self */
682 int n; /* guess for size of b */
683 int mn; /* m + n */
684 int i;
Raymond Hettinger57c45422004-03-11 09:48:18 +0000685 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000686
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000687 /* Special cases:
688 1) lists and tuples which can use PySequence_Fast ops
689 2) extending self to self requires making a copy first
690 */
691 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
Armin Rigo70d172d2004-03-20 22:19:23 +0000692 PyObject **src, **dest;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000693 b = PySequence_Fast(b, "argument must be iterable");
694 if (!b)
695 return NULL;
Armin Rigo70d172d2004-03-20 22:19:23 +0000696 n = PySequence_Fast_GET_SIZE(b);
697 if (n == 0) {
698 /* short circuit when b is empty */
699 Py_DECREF(b);
700 Py_RETURN_NONE;
701 }
702 m = self->ob_size;
703 if (list_resize(self, m + n) == -1) {
704 Py_DECREF(b);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000705 return NULL;
Armin Rigo70d172d2004-03-20 22:19:23 +0000706 }
707 /* note that we may still have self == b here for the
708 * situation a.extend(a), but the following code works
709 * in that case too. Just make sure to resize self
710 * before calling PySequence_Fast_ITEMS.
711 */
712 /* populate the end of self with b's items */
713 src = PySequence_Fast_ITEMS(b);
714 dest = self->ob_item + m;
715 for (i = 0; i < n; i++) {
716 PyObject *o = src[i];
717 Py_INCREF(o);
718 dest[i] = o;
719 }
720 Py_DECREF(b);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000721 Py_RETURN_NONE;
722 }
723
724 it = PyObject_GetIter(b);
725 if (it == NULL)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000726 return NULL;
Raymond Hettinger57c45422004-03-11 09:48:18 +0000727 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000728
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000729 /* Guess a result list size. */
730 n = PyObject_Size(b);
731 if (n < 0) {
732 PyErr_Clear();
733 n = 8; /* arbitrary */
734 }
735 m = self->ob_size;
736 mn = m + n;
Raymond Hettingerd4ff7412004-03-15 09:01:31 +0000737 if (list_resize(self, mn) == -1)
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000738 goto error;
739 memset(&(self->ob_item[m]), 0, sizeof(*self->ob_item) * n);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000740
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000741 /* Run iterator to exhaustion. */
742 for (i = m; ; i++) {
Raymond Hettinger57c45422004-03-11 09:48:18 +0000743 PyObject *item = iternext(it);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000744 if (item == NULL) {
Raymond Hettinger57c45422004-03-11 09:48:18 +0000745 if (PyErr_Occurred()) {
746 if (PyErr_ExceptionMatches(PyExc_StopIteration))
747 PyErr_Clear();
748 else
749 goto error;
750 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000751 break;
752 }
753 if (i < mn)
754 PyList_SET_ITEM(self, i, item); /* steals ref */
755 else {
Raymond Hettinger40a03822004-04-12 13:05:09 +0000756 int status = app1(self, item);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000757 Py_DECREF(item); /* append creates a new ref */
758 if (status < 0)
759 goto error;
760 }
761 }
762
763 /* Cut back result list if initial guess was too large. */
764 if (i < mn && self != NULL) {
765 if (list_ass_slice(self, i, mn, (PyObject *)NULL) != 0)
766 goto error;
767 }
768 Py_DECREF(it);
769 Py_RETURN_NONE;
770
771 error:
772 Py_DECREF(it);
773 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000774}
775
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000776PyObject *
777_PyList_Extend(PyListObject *self, PyObject *b)
778{
779 return listextend(self, b);
780}
781
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000782static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000783list_inplace_concat(PyListObject *self, PyObject *other)
784{
785 PyObject *result;
786
787 result = listextend(self, other);
788 if (result == NULL)
789 return result;
790 Py_DECREF(result);
791 Py_INCREF(self);
792 return (PyObject *)self;
793}
794
795static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000796listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000797{
798 int i = -1;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000799 PyObject *v, *arg = NULL;
800
801 if (!PyArg_UnpackTuple(args, "pop", 0, 1, &arg))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000802 return NULL;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000803 if (arg != NULL) {
804 if (PyInt_Check(arg))
805 i = (int)(PyInt_AS_LONG((PyIntObject*) arg));
Raymond Hettingerfa6c6f82004-02-19 06:12:06 +0000806 else if (!PyArg_ParseTuple(args, "|i:pop", &i))
807 return NULL;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000808 }
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000809 if (self->ob_size == 0) {
810 /* Special-case most common failure cause */
811 PyErr_SetString(PyExc_IndexError, "pop from empty list");
812 return NULL;
813 }
814 if (i < 0)
815 i += self->ob_size;
816 if (i < 0 || i >= self->ob_size) {
817 PyErr_SetString(PyExc_IndexError, "pop index out of range");
818 return NULL;
819 }
820 v = self->ob_item[i];
Raymond Hettingercb3e5802004-02-13 18:36:31 +0000821 if (i == self->ob_size - 1) {
Raymond Hettingerd4ff7412004-03-15 09:01:31 +0000822 if (list_resize(self, self->ob_size - 1) == -1)
Raymond Hettingercb3e5802004-02-13 18:36:31 +0000823 return NULL;
824 return v;
825 }
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000826 Py_INCREF(v);
827 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
828 Py_DECREF(v);
829 return NULL;
830 }
831 return v;
832}
833
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000834/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
835static void
836reverse_slice(PyObject **lo, PyObject **hi)
837{
838 assert(lo && hi);
839
840 --hi;
841 while (lo < hi) {
842 PyObject *t = *lo;
843 *lo = *hi;
844 *hi = t;
845 ++lo;
846 --hi;
847 }
848}
849
Tim Petersa64dc242002-08-01 02:13:36 +0000850/* Lots of code for an adaptive, stable, natural mergesort. There are many
851 * pieces to this algorithm; read listsort.txt for overviews and details.
852 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000853
Guido van Rossum3f236de1996-12-10 23:55:39 +0000854/* Comparison function. Takes care of calling a user-supplied
Tim Peters66860f62002-08-04 17:47:26 +0000855 * comparison function (any callable Python object), which must not be
856 * NULL (use the ISLT macro if you don't know, or call PyObject_RichCompareBool
857 * with Py_LT if you know it's NULL).
Tim Petersa64dc242002-08-01 02:13:36 +0000858 * Returns -1 on error, 1 if x < y, 0 if x >= y.
859 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000860static int
Tim Petersa8c974c2002-07-19 03:30:57 +0000861islt(PyObject *x, PyObject *y, PyObject *compare)
Guido van Rossum3f236de1996-12-10 23:55:39 +0000862{
Tim Petersf2a04732002-07-11 21:46:16 +0000863 PyObject *res;
864 PyObject *args;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000865 int i;
866
Tim Peters66860f62002-08-04 17:47:26 +0000867 assert(compare != NULL);
Tim Petersa8c974c2002-07-19 03:30:57 +0000868 /* Call the user's comparison function and translate the 3-way
869 * result into true or false (or error).
870 */
Tim Petersf2a04732002-07-11 21:46:16 +0000871 args = PyTuple_New(2);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000872 if (args == NULL)
Tim Petersa8c974c2002-07-19 03:30:57 +0000873 return -1;
Tim Petersf2a04732002-07-11 21:46:16 +0000874 Py_INCREF(x);
875 Py_INCREF(y);
876 PyTuple_SET_ITEM(args, 0, x);
877 PyTuple_SET_ITEM(args, 1, y);
Tim Peters58cf3612002-07-15 05:16:13 +0000878 res = PyObject_Call(compare, args, NULL);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000879 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000880 if (res == NULL)
Tim Petersa8c974c2002-07-19 03:30:57 +0000881 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000882 if (!PyInt_Check(res)) {
883 Py_DECREF(res);
884 PyErr_SetString(PyExc_TypeError,
Guido van Rossum9bcd1d71999-04-19 17:44:39 +0000885 "comparison function must return int");
Tim Petersa8c974c2002-07-19 03:30:57 +0000886 return -1;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000887 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888 i = PyInt_AsLong(res);
889 Py_DECREF(res);
Tim Petersa8c974c2002-07-19 03:30:57 +0000890 return i < 0;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000891}
892
Tim Peters66860f62002-08-04 17:47:26 +0000893/* If COMPARE is NULL, calls PyObject_RichCompareBool with Py_LT, else calls
894 * islt. This avoids a layer of function call in the usual case, and
895 * sorting does many comparisons.
896 * Returns -1 on error, 1 if x < y, 0 if x >= y.
897 */
898#define ISLT(X, Y, COMPARE) ((COMPARE) == NULL ? \
899 PyObject_RichCompareBool(X, Y, Py_LT) : \
900 islt(X, Y, COMPARE))
901
902/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +0000903 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
904 started. It makes more sense in context <wink>. X and Y are PyObject*s.
905*/
Tim Peters66860f62002-08-04 17:47:26 +0000906#define IFLT(X, Y) if ((k = ISLT(X, Y, compare)) < 0) goto fail; \
Tim Petersa8c974c2002-07-19 03:30:57 +0000907 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000908
909/* binarysort is the best method for sorting small arrays: it does
910 few compares, but can do data movement quadratic in the number of
911 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000912 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +0000913 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000914 On entry, must have lo <= start <= hi, and that [lo, start) is already
915 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +0000916 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000917 Even in case of error, the output slice will be some permutation of
918 the input (nothing is lost or duplicated).
919*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000920static int
Fred Drakea2f55112000-07-09 15:16:51 +0000921binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
922 /* compare -- comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000923{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000924 register int k;
925 register PyObject **l, **p, **r;
926 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000927
Tim Petersa8c974c2002-07-19 03:30:57 +0000928 assert(lo <= start && start <= hi);
929 /* assert [lo, start) is sorted */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000930 if (lo == start)
931 ++start;
932 for (; start < hi; ++start) {
933 /* set l to where *start belongs */
934 l = lo;
935 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000936 pivot = *r;
Tim Peters0fe977c2002-07-19 06:12:32 +0000937 /* Invariants:
938 * pivot >= all in [lo, l).
939 * pivot < all in [r, start).
940 * The second is vacuously true at the start.
941 */
942 assert(l < r);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000943 do {
944 p = l + ((r - l) >> 1);
Tim Petersa8c974c2002-07-19 03:30:57 +0000945 IFLT(pivot, *p)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000946 r = p;
947 else
Tim Peters0fe977c2002-07-19 06:12:32 +0000948 l = p+1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000949 } while (l < r);
Tim Peters0fe977c2002-07-19 06:12:32 +0000950 assert(l == r);
951 /* The invariants still hold, so pivot >= all in [lo, l) and
952 pivot < all in [l, start), so pivot belongs at l. Note
953 that if there are elements equal to pivot, l points to the
954 first slot after them -- that's why this sort is stable.
955 Slide over to make room.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000956 Caution: using memmove is much slower under MSVC 5;
957 we're not usually moving many slots. */
958 for (p = start; p > l; --p)
959 *p = *(p-1);
960 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000961 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000962 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000963
964 fail:
965 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000966}
967
Tim Petersa64dc242002-08-01 02:13:36 +0000968/*
969Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
970is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000971
Tim Petersa64dc242002-08-01 02:13:36 +0000972 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000973
Tim Petersa64dc242002-08-01 02:13:36 +0000974or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000975
Tim Petersa64dc242002-08-01 02:13:36 +0000976 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +0000977
Tim Petersa64dc242002-08-01 02:13:36 +0000978Boolean *descending is set to 0 in the former case, or to 1 in the latter.
979For its intended use in a stable mergesort, the strictness of the defn of
980"descending" is needed so that the caller can safely reverse a descending
981sequence without violating stability (strict > ensures there are no equal
982elements to get out of order).
983
984Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000985*/
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000986static int
Tim Petersa64dc242002-08-01 02:13:36 +0000987count_run(PyObject **lo, PyObject **hi, PyObject *compare, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000988{
Tim Petersa64dc242002-08-01 02:13:36 +0000989 int k;
990 int n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000991
Tim Petersa64dc242002-08-01 02:13:36 +0000992 assert(lo < hi);
993 *descending = 0;
994 ++lo;
995 if (lo == hi)
996 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000997
Tim Petersa64dc242002-08-01 02:13:36 +0000998 n = 2;
999 IFLT(*lo, *(lo-1)) {
1000 *descending = 1;
1001 for (lo = lo+1; lo < hi; ++lo, ++n) {
1002 IFLT(*lo, *(lo-1))
1003 ;
1004 else
1005 break;
1006 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001007 }
Tim Petersa64dc242002-08-01 02:13:36 +00001008 else {
1009 for (lo = lo+1; lo < hi; ++lo, ++n) {
1010 IFLT(*lo, *(lo-1))
1011 break;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001012 }
1013 }
1014
Tim Petersa64dc242002-08-01 02:13:36 +00001015 return n;
1016fail:
1017 return -1;
1018}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001019
Tim Petersa64dc242002-08-01 02:13:36 +00001020/*
1021Locate the proper position of key in a sorted vector; if the vector contains
1022an element equal to key, return the position immediately to the left of
1023the leftmost equal element. [gallop_right() does the same except returns
1024the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001025
Tim Petersa64dc242002-08-01 02:13:36 +00001026"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001027
Tim Petersa64dc242002-08-01 02:13:36 +00001028"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1029hint is to the final result, the faster this runs.
1030
1031The return value is the int k in 0..n such that
1032
1033 a[k-1] < key <= a[k]
1034
1035pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1036key belongs at index k; or, IOW, the first k elements of a should precede
1037key, and the last n-k should follow key.
1038
1039Returns -1 on error. See listsort.txt for info on the method.
1040*/
1041static int
1042gallop_left(PyObject *key, PyObject **a, int n, int hint, PyObject *compare)
1043{
1044 int ofs;
1045 int lastofs;
1046 int k;
1047
1048 assert(key && a && n > 0 && hint >= 0 && hint < n);
1049
1050 a += hint;
1051 lastofs = 0;
1052 ofs = 1;
1053 IFLT(*a, key) {
1054 /* a[hint] < key -- gallop right, until
1055 * a[hint + lastofs] < key <= a[hint + ofs]
1056 */
1057 const int maxofs = n - hint; /* &a[n-1] is highest */
1058 while (ofs < maxofs) {
1059 IFLT(a[ofs], key) {
1060 lastofs = ofs;
1061 ofs = (ofs << 1) + 1;
1062 if (ofs <= 0) /* int overflow */
1063 ofs = maxofs;
1064 }
1065 else /* key <= a[hint + ofs] */
1066 break;
1067 }
1068 if (ofs > maxofs)
1069 ofs = maxofs;
1070 /* Translate back to offsets relative to &a[0]. */
1071 lastofs += hint;
1072 ofs += hint;
1073 }
1074 else {
1075 /* key <= a[hint] -- gallop left, until
1076 * a[hint - ofs] < key <= a[hint - lastofs]
1077 */
1078 const int maxofs = hint + 1; /* &a[0] is lowest */
1079 while (ofs < maxofs) {
1080 IFLT(*(a-ofs), key)
1081 break;
1082 /* key <= a[hint - ofs] */
1083 lastofs = ofs;
1084 ofs = (ofs << 1) + 1;
1085 if (ofs <= 0) /* int overflow */
1086 ofs = maxofs;
1087 }
1088 if (ofs > maxofs)
1089 ofs = maxofs;
1090 /* Translate back to positive offsets relative to &a[0]. */
1091 k = lastofs;
1092 lastofs = hint - ofs;
1093 ofs = hint - k;
1094 }
1095 a -= hint;
1096
1097 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1098 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1099 * right of lastofs but no farther right than ofs. Do a binary
1100 * search, with invariant a[lastofs-1] < key <= a[ofs].
1101 */
1102 ++lastofs;
1103 while (lastofs < ofs) {
1104 int m = lastofs + ((ofs - lastofs) >> 1);
1105
1106 IFLT(a[m], key)
1107 lastofs = m+1; /* a[m] < key */
1108 else
1109 ofs = m; /* key <= a[m] */
1110 }
1111 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1112 return ofs;
1113
1114fail:
1115 return -1;
1116}
1117
1118/*
1119Exactly like gallop_left(), except that if key already exists in a[0:n],
1120finds the position immediately to the right of the rightmost equal value.
1121
1122The return value is the int k in 0..n such that
1123
1124 a[k-1] <= key < a[k]
1125
1126or -1 if error.
1127
1128The code duplication is massive, but this is enough different given that
1129we're sticking to "<" comparisons that it's much harder to follow if
1130written as one routine with yet another "left or right?" flag.
1131*/
1132static int
1133gallop_right(PyObject *key, PyObject **a, int n, int hint, PyObject *compare)
1134{
1135 int ofs;
1136 int lastofs;
1137 int k;
1138
1139 assert(key && a && n > 0 && hint >= 0 && hint < n);
1140
1141 a += hint;
1142 lastofs = 0;
1143 ofs = 1;
1144 IFLT(key, *a) {
1145 /* key < a[hint] -- gallop left, until
1146 * a[hint - ofs] <= key < a[hint - lastofs]
1147 */
1148 const int maxofs = hint + 1; /* &a[0] is lowest */
1149 while (ofs < maxofs) {
1150 IFLT(key, *(a-ofs)) {
1151 lastofs = ofs;
1152 ofs = (ofs << 1) + 1;
1153 if (ofs <= 0) /* int overflow */
1154 ofs = maxofs;
1155 }
1156 else /* a[hint - ofs] <= key */
1157 break;
1158 }
1159 if (ofs > maxofs)
1160 ofs = maxofs;
1161 /* Translate back to positive offsets relative to &a[0]. */
1162 k = lastofs;
1163 lastofs = hint - ofs;
1164 ofs = hint - k;
1165 }
1166 else {
1167 /* a[hint] <= key -- gallop right, until
1168 * a[hint + lastofs] <= key < a[hint + ofs]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001169 */
Tim Petersa64dc242002-08-01 02:13:36 +00001170 const int maxofs = n - hint; /* &a[n-1] is highest */
1171 while (ofs < maxofs) {
1172 IFLT(key, a[ofs])
1173 break;
1174 /* a[hint + ofs] <= key */
1175 lastofs = ofs;
1176 ofs = (ofs << 1) + 1;
1177 if (ofs <= 0) /* int overflow */
1178 ofs = maxofs;
1179 }
1180 if (ofs > maxofs)
1181 ofs = maxofs;
1182 /* Translate back to offsets relative to &a[0]. */
1183 lastofs += hint;
1184 ofs += hint;
1185 }
1186 a -= hint;
1187
1188 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1189 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1190 * right of lastofs but no farther right than ofs. Do a binary
1191 * search, with invariant a[lastofs-1] <= key < a[ofs].
1192 */
1193 ++lastofs;
1194 while (lastofs < ofs) {
1195 int m = lastofs + ((ofs - lastofs) >> 1);
1196
1197 IFLT(key, a[m])
1198 ofs = m; /* key < a[m] */
1199 else
1200 lastofs = m+1; /* a[m] <= key */
1201 }
1202 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1203 return ofs;
1204
1205fail:
1206 return -1;
1207}
1208
1209/* The maximum number of entries in a MergeState's pending-runs stack.
1210 * This is enough to sort arrays of size up to about
1211 * 32 * phi ** MAX_MERGE_PENDING
1212 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1213 * with 2**64 elements.
1214 */
1215#define MAX_MERGE_PENDING 85
1216
Tim Peterse05f65a2002-08-10 05:21:15 +00001217/* When we get into galloping mode, we stay there until both runs win less
1218 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
Tim Petersa64dc242002-08-01 02:13:36 +00001219 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001220#define MIN_GALLOP 7
Tim Petersa64dc242002-08-01 02:13:36 +00001221
1222/* Avoid malloc for small temp arrays. */
1223#define MERGESTATE_TEMP_SIZE 256
1224
1225/* One MergeState exists on the stack per invocation of mergesort. It's just
1226 * a convenient way to pass state around among the helper functions.
1227 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001228struct s_slice {
1229 PyObject **base;
1230 int len;
1231};
1232
Tim Petersa64dc242002-08-01 02:13:36 +00001233typedef struct s_MergeState {
1234 /* The user-supplied comparison function. or NULL if none given. */
1235 PyObject *compare;
1236
Tim Peterse05f65a2002-08-10 05:21:15 +00001237 /* This controls when we get *into* galloping mode. It's initialized
1238 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1239 * random data, and lower for highly structured data.
1240 */
1241 int min_gallop;
1242
Tim Petersa64dc242002-08-01 02:13:36 +00001243 /* 'a' is temp storage to help with merges. It contains room for
1244 * alloced entries.
1245 */
1246 PyObject **a; /* may point to temparray below */
1247 int alloced;
1248
1249 /* A stack of n pending runs yet to be merged. Run #i starts at
1250 * address base[i] and extends for len[i] elements. It's always
1251 * true (so long as the indices are in bounds) that
1252 *
Tim Peterse05f65a2002-08-10 05:21:15 +00001253 * pending[i].base + pending[i].len == pending[i+1].base
Tim Petersa64dc242002-08-01 02:13:36 +00001254 *
1255 * so we could cut the storage for this, but it's a minor amount,
1256 * and keeping all the info explicit simplifies the code.
1257 */
1258 int n;
Tim Peterse05f65a2002-08-10 05:21:15 +00001259 struct s_slice pending[MAX_MERGE_PENDING];
Tim Petersa64dc242002-08-01 02:13:36 +00001260
1261 /* 'a' points to this when possible, rather than muck with malloc. */
1262 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1263} MergeState;
1264
1265/* Conceptually a MergeState's constructor. */
1266static void
1267merge_init(MergeState *ms, PyObject *compare)
1268{
1269 assert(ms != NULL);
1270 ms->compare = compare;
1271 ms->a = ms->temparray;
1272 ms->alloced = MERGESTATE_TEMP_SIZE;
1273 ms->n = 0;
Tim Peterse05f65a2002-08-10 05:21:15 +00001274 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001275}
1276
1277/* Free all the temp memory owned by the MergeState. This must be called
1278 * when you're done with a MergeState, and may be called before then if
1279 * you want to free the temp memory early.
1280 */
1281static void
1282merge_freemem(MergeState *ms)
1283{
1284 assert(ms != NULL);
1285 if (ms->a != ms->temparray)
1286 PyMem_Free(ms->a);
1287 ms->a = ms->temparray;
1288 ms->alloced = MERGESTATE_TEMP_SIZE;
1289}
1290
1291/* Ensure enough temp memory for 'need' array slots is available.
1292 * Returns 0 on success and -1 if the memory can't be gotten.
1293 */
1294static int
1295merge_getmem(MergeState *ms, int need)
1296{
1297 assert(ms != NULL);
1298 if (need <= ms->alloced)
1299 return 0;
1300 /* Don't realloc! That can cost cycles to copy the old data, but
1301 * we don't care what's in the block.
1302 */
1303 merge_freemem(ms);
1304 ms->a = (PyObject **)PyMem_Malloc(need * sizeof(PyObject*));
1305 if (ms->a) {
1306 ms->alloced = need;
1307 return 0;
1308 }
1309 PyErr_NoMemory();
1310 merge_freemem(ms); /* reset to sane state */
1311 return -1;
1312}
1313#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1314 merge_getmem(MS, NEED))
1315
1316/* Merge the na elements starting at pa with the nb elements starting at pb
1317 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
1318 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
1319 * merge, and should have na <= nb. See listsort.txt for more info.
1320 * Return 0 if successful, -1 if error.
1321 */
1322static int
1323merge_lo(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb)
1324{
1325 int k;
1326 PyObject *compare;
1327 PyObject **dest;
1328 int result = -1; /* guilty until proved innocent */
Tim Peterse05f65a2002-08-10 05:21:15 +00001329 int min_gallop = ms->min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001330
1331 assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
1332 if (MERGE_GETMEM(ms, na) < 0)
1333 return -1;
1334 memcpy(ms->a, pa, na * sizeof(PyObject*));
1335 dest = pa;
1336 pa = ms->a;
1337
1338 *dest++ = *pb++;
1339 --nb;
1340 if (nb == 0)
1341 goto Succeed;
1342 if (na == 1)
1343 goto CopyB;
1344
1345 compare = ms->compare;
1346 for (;;) {
1347 int acount = 0; /* # of times A won in a row */
1348 int bcount = 0; /* # of times B won in a row */
1349
1350 /* Do the straightforward thing until (if ever) one run
1351 * appears to win consistently.
1352 */
1353 for (;;) {
Tim Peterse05f65a2002-08-10 05:21:15 +00001354 assert(na > 1 && nb > 0);
Tim Peters66860f62002-08-04 17:47:26 +00001355 k = ISLT(*pb, *pa, compare);
Tim Petersa64dc242002-08-01 02:13:36 +00001356 if (k) {
1357 if (k < 0)
1358 goto Fail;
1359 *dest++ = *pb++;
1360 ++bcount;
1361 acount = 0;
1362 --nb;
1363 if (nb == 0)
1364 goto Succeed;
Tim Peterse05f65a2002-08-10 05:21:15 +00001365 if (bcount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001366 break;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001367 }
1368 else {
Tim Petersa64dc242002-08-01 02:13:36 +00001369 *dest++ = *pa++;
1370 ++acount;
1371 bcount = 0;
1372 --na;
1373 if (na == 1)
1374 goto CopyB;
Tim Peterse05f65a2002-08-10 05:21:15 +00001375 if (acount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001376 break;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001377 }
Tim Petersa64dc242002-08-01 02:13:36 +00001378 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001379
Tim Petersa64dc242002-08-01 02:13:36 +00001380 /* One run is winning so consistently that galloping may
1381 * be a huge win. So try that, and continue galloping until
1382 * (if ever) neither run appears to be winning consistently
1383 * anymore.
1384 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001385 ++min_gallop;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001386 do {
Tim Peterse05f65a2002-08-10 05:21:15 +00001387 assert(na > 1 && nb > 0);
1388 min_gallop -= min_gallop > 1;
1389 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001390 k = gallop_right(*pb, pa, na, 0, compare);
1391 acount = k;
1392 if (k) {
1393 if (k < 0)
1394 goto Fail;
1395 memcpy(dest, pa, k * sizeof(PyObject *));
1396 dest += k;
1397 pa += k;
1398 na -= k;
1399 if (na == 1)
1400 goto CopyB;
1401 /* na==0 is impossible now if the comparison
1402 * function is consistent, but we can't assume
1403 * that it is.
1404 */
1405 if (na == 0)
1406 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001407 }
Tim Petersa64dc242002-08-01 02:13:36 +00001408 *dest++ = *pb++;
1409 --nb;
1410 if (nb == 0)
1411 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001412
Tim Petersa64dc242002-08-01 02:13:36 +00001413 k = gallop_left(*pa, pb, nb, 0, compare);
1414 bcount = k;
1415 if (k) {
1416 if (k < 0)
1417 goto Fail;
1418 memmove(dest, pb, k * sizeof(PyObject *));
1419 dest += k;
1420 pb += k;
1421 nb -= k;
1422 if (nb == 0)
1423 goto Succeed;
1424 }
1425 *dest++ = *pa++;
1426 --na;
1427 if (na == 1)
1428 goto CopyB;
1429 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
Tim Peterse05f65a2002-08-10 05:21:15 +00001430 ++min_gallop; /* penalize it for leaving galloping mode */
1431 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001432 }
1433Succeed:
1434 result = 0;
1435Fail:
1436 if (na)
1437 memcpy(dest, pa, na * sizeof(PyObject*));
1438 return result;
1439CopyB:
1440 assert(na == 1 && nb > 0);
1441 /* The last element of pa belongs at the end of the merge. */
1442 memmove(dest, pb, nb * sizeof(PyObject *));
1443 dest[nb] = *pa;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001444 return 0;
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/* Merge the na elements starting at pa with the nb elements starting at pb
1448 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
1449 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
1450 * merge, and should have na >= nb. See listsort.txt for more info.
1451 * Return 0 if successful, -1 if error.
1452 */
1453static int
1454merge_hi(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb)
1455{
1456 int k;
1457 PyObject *compare;
1458 PyObject **dest;
1459 int result = -1; /* guilty until proved innocent */
1460 PyObject **basea;
1461 PyObject **baseb;
Tim Peterse05f65a2002-08-10 05:21:15 +00001462 int min_gallop = ms->min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001463
1464 assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
1465 if (MERGE_GETMEM(ms, nb) < 0)
1466 return -1;
1467 dest = pb + nb - 1;
1468 memcpy(ms->a, pb, nb * sizeof(PyObject*));
1469 basea = pa;
1470 baseb = ms->a;
1471 pb = ms->a + nb - 1;
1472 pa += na - 1;
1473
1474 *dest-- = *pa--;
1475 --na;
1476 if (na == 0)
1477 goto Succeed;
1478 if (nb == 1)
1479 goto CopyA;
1480
1481 compare = ms->compare;
1482 for (;;) {
1483 int acount = 0; /* # of times A won in a row */
1484 int bcount = 0; /* # of times B won in a row */
1485
1486 /* Do the straightforward thing until (if ever) one run
1487 * appears to win consistently.
1488 */
1489 for (;;) {
Tim Peterse05f65a2002-08-10 05:21:15 +00001490 assert(na > 0 && nb > 1);
Tim Peters66860f62002-08-04 17:47:26 +00001491 k = ISLT(*pb, *pa, compare);
Tim Petersa64dc242002-08-01 02:13:36 +00001492 if (k) {
1493 if (k < 0)
1494 goto Fail;
1495 *dest-- = *pa--;
1496 ++acount;
1497 bcount = 0;
1498 --na;
1499 if (na == 0)
1500 goto Succeed;
Tim Peterse05f65a2002-08-10 05:21:15 +00001501 if (acount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001502 break;
1503 }
1504 else {
1505 *dest-- = *pb--;
1506 ++bcount;
1507 acount = 0;
1508 --nb;
1509 if (nb == 1)
1510 goto CopyA;
Tim Peterse05f65a2002-08-10 05:21:15 +00001511 if (bcount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001512 break;
1513 }
1514 }
1515
1516 /* One run is winning so consistently that galloping may
1517 * be a huge win. So try that, and continue galloping until
1518 * (if ever) neither run appears to be winning consistently
1519 * anymore.
1520 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001521 ++min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001522 do {
Tim Peterse05f65a2002-08-10 05:21:15 +00001523 assert(na > 0 && nb > 1);
1524 min_gallop -= min_gallop > 1;
1525 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001526 k = gallop_right(*pb, basea, na, na-1, compare);
1527 if (k < 0)
1528 goto Fail;
1529 k = na - k;
1530 acount = k;
1531 if (k) {
1532 dest -= k;
1533 pa -= k;
1534 memmove(dest+1, pa+1, k * sizeof(PyObject *));
1535 na -= k;
1536 if (na == 0)
1537 goto Succeed;
1538 }
1539 *dest-- = *pb--;
1540 --nb;
1541 if (nb == 1)
1542 goto CopyA;
1543
1544 k = gallop_left(*pa, baseb, nb, nb-1, compare);
1545 if (k < 0)
1546 goto Fail;
1547 k = nb - k;
1548 bcount = k;
1549 if (k) {
1550 dest -= k;
1551 pb -= k;
1552 memcpy(dest+1, pb+1, k * sizeof(PyObject *));
1553 nb -= k;
1554 if (nb == 1)
1555 goto CopyA;
1556 /* nb==0 is impossible now if the comparison
1557 * function is consistent, but we can't assume
1558 * that it is.
1559 */
1560 if (nb == 0)
1561 goto Succeed;
1562 }
1563 *dest-- = *pa--;
1564 --na;
1565 if (na == 0)
1566 goto Succeed;
1567 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
Tim Peterse05f65a2002-08-10 05:21:15 +00001568 ++min_gallop; /* penalize it for leaving galloping mode */
1569 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001570 }
1571Succeed:
1572 result = 0;
1573Fail:
1574 if (nb)
1575 memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*));
1576 return result;
1577CopyA:
1578 assert(nb == 1 && na > 0);
1579 /* The first element of pb belongs at the front of the merge. */
1580 dest -= na;
1581 pa -= na;
1582 memmove(dest+1, pa+1, na * sizeof(PyObject *));
1583 *dest = *pb;
1584 return 0;
1585}
1586
1587/* Merge the two runs at stack indices i and i+1.
1588 * Returns 0 on success, -1 on error.
1589 */
1590static int
1591merge_at(MergeState *ms, int i)
1592{
1593 PyObject **pa, **pb;
1594 int na, nb;
1595 int k;
1596 PyObject *compare;
1597
1598 assert(ms != NULL);
1599 assert(ms->n >= 2);
1600 assert(i >= 0);
1601 assert(i == ms->n - 2 || i == ms->n - 3);
1602
Tim Peterse05f65a2002-08-10 05:21:15 +00001603 pa = ms->pending[i].base;
1604 na = ms->pending[i].len;
1605 pb = ms->pending[i+1].base;
1606 nb = ms->pending[i+1].len;
Tim Petersa64dc242002-08-01 02:13:36 +00001607 assert(na > 0 && nb > 0);
1608 assert(pa + na == pb);
1609
1610 /* Record the length of the combined runs; if i is the 3rd-last
1611 * run now, also slide over the last run (which isn't involved
1612 * in this merge). The current run i+1 goes away in any case.
1613 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001614 ms->pending[i].len = na + nb;
1615 if (i == ms->n - 3)
1616 ms->pending[i+1] = ms->pending[i+2];
Tim Petersa64dc242002-08-01 02:13:36 +00001617 --ms->n;
1618
1619 /* Where does b start in a? Elements in a before that can be
1620 * ignored (already in place).
1621 */
1622 compare = ms->compare;
1623 k = gallop_right(*pb, pa, na, 0, compare);
1624 if (k < 0)
1625 return -1;
1626 pa += k;
1627 na -= k;
1628 if (na == 0)
1629 return 0;
1630
1631 /* Where does a end in b? Elements in b after that can be
1632 * ignored (already in place).
1633 */
1634 nb = gallop_left(pa[na-1], pb, nb, nb-1, compare);
1635 if (nb <= 0)
1636 return nb;
1637
1638 /* Merge what remains of the runs, using a temp array with
1639 * min(na, nb) elements.
1640 */
1641 if (na <= nb)
1642 return merge_lo(ms, pa, na, pb, nb);
1643 else
1644 return merge_hi(ms, pa, na, pb, nb);
1645}
1646
1647/* Examine the stack of runs waiting to be merged, merging adjacent runs
1648 * until the stack invariants are re-established:
1649 *
1650 * 1. len[-3] > len[-2] + len[-1]
1651 * 2. len[-2] > len[-1]
1652 *
1653 * See listsort.txt for more info.
1654 *
1655 * Returns 0 on success, -1 on error.
1656 */
1657static int
1658merge_collapse(MergeState *ms)
1659{
Tim Peterse05f65a2002-08-10 05:21:15 +00001660 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001661
1662 assert(ms);
1663 while (ms->n > 1) {
1664 int n = ms->n - 2;
Tim Peterse05f65a2002-08-10 05:21:15 +00001665 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
1666 if (p[n-1].len < p[n+1].len)
Tim Petersa64dc242002-08-01 02:13:36 +00001667 --n;
1668 if (merge_at(ms, n) < 0)
1669 return -1;
1670 }
Tim Peterse05f65a2002-08-10 05:21:15 +00001671 else if (p[n].len <= p[n+1].len) {
Tim Petersa64dc242002-08-01 02:13:36 +00001672 if (merge_at(ms, n) < 0)
1673 return -1;
1674 }
1675 else
1676 break;
1677 }
1678 return 0;
1679}
1680
1681/* Regardless of invariants, merge all runs on the stack until only one
1682 * remains. This is used at the end of the mergesort.
1683 *
1684 * Returns 0 on success, -1 on error.
1685 */
1686static int
1687merge_force_collapse(MergeState *ms)
1688{
Tim Peterse05f65a2002-08-10 05:21:15 +00001689 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001690
1691 assert(ms);
1692 while (ms->n > 1) {
1693 int n = ms->n - 2;
Tim Peterse05f65a2002-08-10 05:21:15 +00001694 if (n > 0 && p[n-1].len < p[n+1].len)
Tim Petersa64dc242002-08-01 02:13:36 +00001695 --n;
1696 if (merge_at(ms, n) < 0)
1697 return -1;
1698 }
1699 return 0;
1700}
1701
1702/* Compute a good value for the minimum run length; natural runs shorter
1703 * than this are boosted artificially via binary insertion.
1704 *
1705 * If n < 64, return n (it's too small to bother with fancy stuff).
1706 * Else if n is an exact power of 2, return 32.
1707 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1708 * strictly less than, an exact power of 2.
1709 *
1710 * See listsort.txt for more info.
1711 */
1712static int
1713merge_compute_minrun(int n)
1714{
1715 int r = 0; /* becomes 1 if any 1 bits are shifted off */
1716
1717 assert(n >= 0);
1718 while (n >= 64) {
1719 r |= n & 1;
1720 n >>= 1;
1721 }
1722 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001723}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001724
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001725/* Special wrapper to support stable sorting using the decorate-sort-undecorate
1726 pattern. Holds a key which is used for comparisions and the original record
1727 which is returned during the undecorate phase. By exposing only the key
1728 during comparisons, the underlying sort stability characteristics are left
1729 unchanged. Also, if a custom comparison function is used, it will only see
1730 the key instead of a full record. */
1731
1732typedef struct {
1733 PyObject_HEAD
1734 PyObject *key;
1735 PyObject *value;
1736} sortwrapperobject;
1737
1738static PyTypeObject sortwrapper_type;
1739
1740static PyObject *
1741sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, int op)
1742{
1743 if (!PyObject_TypeCheck(b, &sortwrapper_type)) {
1744 PyErr_SetString(PyExc_TypeError,
1745 "expected a sortwrapperobject");
1746 return NULL;
1747 }
1748 return PyObject_RichCompare(a->key, b->key, op);
1749}
1750
1751static void
1752sortwrapper_dealloc(sortwrapperobject *so)
1753{
1754 Py_XDECREF(so->key);
1755 Py_XDECREF(so->value);
1756 PyObject_Del(so);
1757}
1758
1759PyDoc_STRVAR(sortwrapper_doc, "Object wrapper with a custom sort key.");
1760
1761static PyTypeObject sortwrapper_type = {
1762 PyObject_HEAD_INIT(&PyType_Type)
1763 0, /* ob_size */
1764 "sortwrapper", /* tp_name */
1765 sizeof(sortwrapperobject), /* tp_basicsize */
1766 0, /* tp_itemsize */
1767 /* methods */
1768 (destructor)sortwrapper_dealloc, /* tp_dealloc */
1769 0, /* tp_print */
1770 0, /* tp_getattr */
1771 0, /* tp_setattr */
1772 0, /* tp_compare */
1773 0, /* tp_repr */
1774 0, /* tp_as_number */
1775 0, /* tp_as_sequence */
1776 0, /* tp_as_mapping */
1777 0, /* tp_hash */
1778 0, /* tp_call */
1779 0, /* tp_str */
1780 PyObject_GenericGetAttr, /* tp_getattro */
1781 0, /* tp_setattro */
1782 0, /* tp_as_buffer */
1783 Py_TPFLAGS_DEFAULT |
1784 Py_TPFLAGS_HAVE_RICHCOMPARE, /* tp_flags */
1785 sortwrapper_doc, /* tp_doc */
1786 0, /* tp_traverse */
1787 0, /* tp_clear */
1788 (richcmpfunc)sortwrapper_richcompare, /* tp_richcompare */
1789};
1790
1791/* Returns a new reference to a sortwrapper.
1792 Consumes the references to the two underlying objects. */
1793
1794static PyObject *
1795build_sortwrapper(PyObject *key, PyObject *value)
1796{
1797 sortwrapperobject *so;
1798
1799 so = PyObject_New(sortwrapperobject, &sortwrapper_type);
1800 if (so == NULL)
1801 return NULL;
1802 so->key = key;
1803 so->value = value;
1804 return (PyObject *)so;
1805}
1806
1807/* Returns a new reference to the value underlying the wrapper. */
1808static PyObject *
1809sortwrapper_getvalue(PyObject *so)
1810{
1811 PyObject *value;
1812
1813 if (!PyObject_TypeCheck(so, &sortwrapper_type)) {
1814 PyErr_SetString(PyExc_TypeError,
1815 "expected a sortwrapperobject");
1816 return NULL;
1817 }
1818 value = ((sortwrapperobject *)so)->value;
1819 Py_INCREF(value);
1820 return value;
1821}
1822
1823/* Wrapper for user specified cmp functions in combination with a
1824 specified key function. Makes sure the cmp function is presented
1825 with the actual key instead of the sortwrapper */
1826
1827typedef struct {
1828 PyObject_HEAD
1829 PyObject *func;
1830} cmpwrapperobject;
1831
1832static void
1833cmpwrapper_dealloc(cmpwrapperobject *co)
1834{
1835 Py_XDECREF(co->func);
1836 PyObject_Del(co);
1837}
1838
1839static PyObject *
1840cmpwrapper_call(cmpwrapperobject *co, PyObject *args, PyObject *kwds)
1841{
1842 PyObject *x, *y, *xx, *yy;
1843
1844 if (!PyArg_UnpackTuple(args, "", 2, 2, &x, &y))
1845 return NULL;
1846 if (!PyObject_TypeCheck(x, &sortwrapper_type) ||
Raymond Hettingerae4a2992003-10-16 17:16:30 +00001847 !PyObject_TypeCheck(y, &sortwrapper_type)) {
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001848 PyErr_SetString(PyExc_TypeError,
1849 "expected a sortwrapperobject");
1850 return NULL;
1851 }
1852 xx = ((sortwrapperobject *)x)->key;
1853 yy = ((sortwrapperobject *)y)->key;
1854 return PyObject_CallFunctionObjArgs(co->func, xx, yy, NULL);
1855}
1856
1857PyDoc_STRVAR(cmpwrapper_doc, "cmp() wrapper for sort with custom keys.");
1858
1859static PyTypeObject cmpwrapper_type = {
1860 PyObject_HEAD_INIT(&PyType_Type)
1861 0, /* ob_size */
1862 "cmpwrapper", /* tp_name */
1863 sizeof(cmpwrapperobject), /* tp_basicsize */
1864 0, /* tp_itemsize */
1865 /* methods */
1866 (destructor)cmpwrapper_dealloc, /* tp_dealloc */
1867 0, /* tp_print */
1868 0, /* tp_getattr */
1869 0, /* tp_setattr */
1870 0, /* tp_compare */
1871 0, /* tp_repr */
1872 0, /* tp_as_number */
1873 0, /* tp_as_sequence */
1874 0, /* tp_as_mapping */
1875 0, /* tp_hash */
1876 (ternaryfunc)cmpwrapper_call, /* tp_call */
1877 0, /* tp_str */
1878 PyObject_GenericGetAttr, /* tp_getattro */
1879 0, /* tp_setattro */
1880 0, /* tp_as_buffer */
1881 Py_TPFLAGS_DEFAULT, /* tp_flags */
1882 cmpwrapper_doc, /* tp_doc */
1883};
1884
1885static PyObject *
1886build_cmpwrapper(PyObject *cmpfunc)
1887{
1888 cmpwrapperobject *co;
1889
1890 co = PyObject_New(cmpwrapperobject, &cmpwrapper_type);
1891 if (co == NULL)
1892 return NULL;
1893 Py_INCREF(cmpfunc);
1894 co->func = cmpfunc;
1895 return (PyObject *)co;
1896}
1897
Tim Petersa64dc242002-08-01 02:13:36 +00001898/* An adaptive, stable, natural mergesort. See listsort.txt.
1899 * Returns Py_None on success, NULL on error. Even in case of error, the
1900 * list will be some permutation of its input state (nothing is lost or
1901 * duplicated).
1902 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001903static PyObject *
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001904listsort(PyListObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001905{
Tim Petersa64dc242002-08-01 02:13:36 +00001906 MergeState ms;
1907 PyObject **lo, **hi;
1908 int nremaining;
1909 int minrun;
Raymond Hettinger4bb95402004-02-13 11:36:39 +00001910 int saved_ob_size, saved_allocated;
Tim Petersb9099c32002-11-12 22:08:10 +00001911 PyObject **saved_ob_item;
1912 PyObject **empty_ob_item;
Tim Petersa64dc242002-08-01 02:13:36 +00001913 PyObject *compare = NULL;
1914 PyObject *result = NULL; /* guilty until proved innocent */
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001915 int reverse = 0;
1916 PyObject *keyfunc = NULL;
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001917 int i;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001918 PyObject *key, *value, *kvpair;
1919 static char *kwlist[] = {"cmp", "key", "reverse", 0};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001920
Tim Petersa64dc242002-08-01 02:13:36 +00001921 assert(self != NULL);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001922 assert (PyList_Check(self));
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001923 if (args != NULL) {
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001924 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi:sort",
1925 kwlist, &compare, &keyfunc, &reverse))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001926 return NULL;
1927 }
Skip Montanaro4abd5f02003-01-02 20:51:08 +00001928 if (compare == Py_None)
1929 compare = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001930 if (keyfunc == Py_None)
1931 keyfunc = NULL;
1932 if (compare != NULL && keyfunc != NULL) {
1933 compare = build_cmpwrapper(compare);
1934 if (compare == NULL)
Hye-Shik Chang19cb1932003-12-10 07:31:08 +00001935 return NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001936 } else
1937 Py_XINCREF(compare);
1938
Tim Petersb9099c32002-11-12 22:08:10 +00001939 /* The list is temporarily made empty, so that mutations performed
1940 * by comparison functions can't affect the slice of memory we're
1941 * sorting (allowing mutations during sorting is a core-dump
1942 * factory, since ob_item may change).
1943 */
1944 saved_ob_size = self->ob_size;
1945 saved_ob_item = self->ob_item;
Raymond Hettinger4bb95402004-02-13 11:36:39 +00001946 saved_allocated = self->allocated;
Tim Petersb9099c32002-11-12 22:08:10 +00001947 self->ob_size = 0;
Tim Peters7049d812004-01-18 20:31:02 +00001948 self->ob_item = empty_ob_item = PyMem_NEW(PyObject *, 0);
Raymond Hettinger4bb95402004-02-13 11:36:39 +00001949 self->allocated = 0;
Tim Peters330f9e92002-07-19 07:05:44 +00001950
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001951 if (keyfunc != NULL) {
1952 for (i=0 ; i < saved_ob_size ; i++) {
1953 value = saved_ob_item[i];
1954 key = PyObject_CallFunctionObjArgs(keyfunc, value,
1955 NULL);
1956 if (key == NULL) {
1957 for (i=i-1 ; i>=0 ; i--) {
1958 kvpair = saved_ob_item[i];
1959 value = sortwrapper_getvalue(kvpair);
1960 saved_ob_item[i] = value;
1961 Py_DECREF(kvpair);
1962 }
1963 if (self->ob_item != empty_ob_item
1964 || self->ob_size) {
1965 /* If the list changed *as well* we
1966 have two errors. We let the first
1967 one "win", but we shouldn't let
1968 what's in the list currently
1969 leak. */
1970 (void)list_ass_slice(
1971 self, 0, self->ob_size,
1972 (PyObject *)NULL);
1973 }
1974
1975 goto dsu_fail;
1976 }
1977 kvpair = build_sortwrapper(key, value);
1978 if (kvpair == NULL)
1979 goto dsu_fail;
1980 saved_ob_item[i] = kvpair;
1981 }
1982 }
1983
1984 /* Reverse sort stability achieved by initially reversing the list,
1985 applying a stable forward sort, then reversing the final result. */
1986 if (reverse && saved_ob_size > 1)
1987 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
1988
1989 merge_init(&ms, compare);
1990
Tim Petersb9099c32002-11-12 22:08:10 +00001991 nremaining = saved_ob_size;
Tim Petersa64dc242002-08-01 02:13:36 +00001992 if (nremaining < 2)
1993 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00001994
Tim Petersa64dc242002-08-01 02:13:36 +00001995 /* March over the array once, left to right, finding natural runs,
1996 * and extending short natural runs to minrun elements.
1997 */
Tim Petersb9099c32002-11-12 22:08:10 +00001998 lo = saved_ob_item;
Tim Petersa64dc242002-08-01 02:13:36 +00001999 hi = lo + nremaining;
2000 minrun = merge_compute_minrun(nremaining);
2001 do {
2002 int descending;
2003 int n;
Tim Peters330f9e92002-07-19 07:05:44 +00002004
Tim Petersa64dc242002-08-01 02:13:36 +00002005 /* Identify next run. */
2006 n = count_run(lo, hi, compare, &descending);
2007 if (n < 0)
2008 goto fail;
2009 if (descending)
2010 reverse_slice(lo, lo + n);
2011 /* If short, extend to min(minrun, nremaining). */
2012 if (n < minrun) {
2013 const int force = nremaining <= minrun ?
2014 nremaining : minrun;
2015 if (binarysort(lo, lo + force, lo + n, compare) < 0)
2016 goto fail;
2017 n = force;
2018 }
2019 /* Push run onto pending-runs stack, and maybe merge. */
2020 assert(ms.n < MAX_MERGE_PENDING);
Tim Peterse05f65a2002-08-10 05:21:15 +00002021 ms.pending[ms.n].base = lo;
2022 ms.pending[ms.n].len = n;
Tim Petersa64dc242002-08-01 02:13:36 +00002023 ++ms.n;
2024 if (merge_collapse(&ms) < 0)
2025 goto fail;
2026 /* Advance to find next run. */
2027 lo += n;
2028 nremaining -= n;
2029 } while (nremaining);
2030 assert(lo == hi);
Tim Peters330f9e92002-07-19 07:05:44 +00002031
Tim Petersa64dc242002-08-01 02:13:36 +00002032 if (merge_force_collapse(&ms) < 0)
2033 goto fail;
2034 assert(ms.n == 1);
Tim Petersb9099c32002-11-12 22:08:10 +00002035 assert(ms.pending[0].base == saved_ob_item);
2036 assert(ms.pending[0].len == saved_ob_size);
Tim Petersa64dc242002-08-01 02:13:36 +00002037
2038succeed:
2039 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002040fail:
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002041 if (keyfunc != NULL) {
2042 for (i=0 ; i < saved_ob_size ; i++) {
2043 kvpair = saved_ob_item[i];
2044 value = sortwrapper_getvalue(kvpair);
2045 saved_ob_item[i] = value;
2046 Py_DECREF(kvpair);
2047 }
2048 }
2049
Tim Petersb9099c32002-11-12 22:08:10 +00002050 if (self->ob_item != empty_ob_item || self->ob_size) {
2051 /* The user mucked with the list during the sort. */
2052 (void)list_ass_slice(self, 0, self->ob_size, (PyObject *)NULL);
2053 if (result != NULL) {
2054 PyErr_SetString(PyExc_ValueError,
2055 "list modified during sort");
2056 result = NULL;
2057 }
2058 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002059
2060 if (reverse && saved_ob_size > 1)
2061 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
2062
2063 merge_freemem(&ms);
2064
2065dsu_fail:
Tim Petersb9099c32002-11-12 22:08:10 +00002066 if (self->ob_item == empty_ob_item)
2067 PyMem_FREE(empty_ob_item);
2068 self->ob_size = saved_ob_size;
2069 self->ob_item = saved_ob_item;
Raymond Hettinger4bb95402004-02-13 11:36:39 +00002070 self->allocated = saved_allocated;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002071 Py_XDECREF(compare);
Tim Petersa64dc242002-08-01 02:13:36 +00002072 Py_XINCREF(result);
2073 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002074}
Tim Peters330f9e92002-07-19 07:05:44 +00002075#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002076#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002077
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002078int
Fred Drakea2f55112000-07-09 15:16:51 +00002079PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002080{
2081 if (v == NULL || !PyList_Check(v)) {
2082 PyErr_BadInternalCall();
2083 return -1;
2084 }
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002085 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002086 if (v == NULL)
2087 return -1;
2088 Py_DECREF(v);
2089 return 0;
2090}
2091
Guido van Rossumb86c5492001-02-12 22:06:02 +00002092static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002093listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002094{
Tim Peters326b4482002-07-19 04:04:16 +00002095 if (self->ob_size > 1)
2096 reverse_slice(self->ob_item, self->ob_item + self->ob_size);
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +00002097 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002098}
2099
Guido van Rossum84c76f51990-10-30 13:32:20 +00002100int
Fred Drakea2f55112000-07-09 15:16:51 +00002101PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002102{
Tim Peters6063e262002-08-08 01:06:39 +00002103 PyListObject *self = (PyListObject *)v;
2104
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002105 if (v == NULL || !PyList_Check(v)) {
2106 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002107 return -1;
2108 }
Tim Peters6063e262002-08-08 01:06:39 +00002109 if (self->ob_size > 1)
2110 reverse_slice(self->ob_item, self->ob_item + self->ob_size);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002111 return 0;
2112}
2113
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002114PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002115PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002116{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002117 PyObject *w;
2118 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002119 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002120 if (v == NULL || !PyList_Check(v)) {
2121 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002122 return NULL;
2123 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002124 n = ((PyListObject *)v)->ob_size;
2125 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002126 if (w == NULL)
2127 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002128 p = ((PyTupleObject *)w)->ob_item;
Thomas Wouters334fb892000-07-25 12:56:38 +00002129 memcpy((void *)p,
2130 (void *)((PyListObject *)v)->ob_item,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002131 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002132 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002133 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002134 p++;
2135 }
2136 return w;
2137}
2138
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002139static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002140listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002141{
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002142 int i, start=0, stop=self->ob_size;
2143 PyObject *v;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002144
Walter Dörwalde8049bef2003-06-17 19:27:39 +00002145 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2146 _PyEval_SliceIndex, &start,
2147 _PyEval_SliceIndex, &stop))
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002148 return NULL;
Guido van Rossum2743d872003-06-17 14:25:14 +00002149 if (start < 0) {
2150 start += self->ob_size;
2151 if (start < 0)
2152 start = 0;
2153 }
2154 if (stop < 0) {
2155 stop += self->ob_size;
2156 if (stop < 0)
2157 stop = 0;
2158 }
2159 else if (stop > self->ob_size)
2160 stop = self->ob_size;
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002161 for (i = start; i < stop; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002162 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2163 if (cmp > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002164 return PyInt_FromLong((long)i);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002165 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002166 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002167 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002168 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00002169 return NULL;
2170}
2171
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002172static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002173listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002174{
2175 int count = 0;
2176 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002177
Guido van Rossume6f7d181991-10-20 20:20:40 +00002178 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002179 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2180 if (cmp > 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002181 count++;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002182 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002183 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00002184 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002185 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002186}
2187
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002188static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002189listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002190{
2191 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002192
Guido van Rossumed98d481991-03-06 13:07:53 +00002193 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002194 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2195 if (cmp > 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002196 if (list_ass_slice(self, i, i+1,
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +00002197 (PyObject *)NULL) == 0)
2198 Py_RETURN_NONE;
2199 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002200 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002201 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002202 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002203 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002204 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00002205 return NULL;
2206}
2207
Jeremy Hylton8caad492000-06-23 14:18:11 +00002208static int
2209list_traverse(PyListObject *o, visitproc visit, void *arg)
2210{
2211 int i, err;
2212 PyObject *x;
2213
2214 for (i = o->ob_size; --i >= 0; ) {
2215 x = o->ob_item[i];
2216 if (x != NULL) {
2217 err = visit(x, arg);
2218 if (err)
2219 return err;
2220 }
2221 }
2222 return 0;
2223}
2224
2225static int
2226list_clear(PyListObject *lp)
2227{
2228 (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0);
2229 return 0;
2230}
2231
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002232static PyObject *
2233list_richcompare(PyObject *v, PyObject *w, int op)
2234{
2235 PyListObject *vl, *wl;
2236 int i;
2237
2238 if (!PyList_Check(v) || !PyList_Check(w)) {
2239 Py_INCREF(Py_NotImplemented);
2240 return Py_NotImplemented;
2241 }
2242
2243 vl = (PyListObject *)v;
2244 wl = (PyListObject *)w;
2245
2246 if (vl->ob_size != wl->ob_size && (op == Py_EQ || op == Py_NE)) {
2247 /* Shortcut: if the lengths differ, the lists differ */
2248 PyObject *res;
2249 if (op == Py_EQ)
2250 res = Py_False;
2251 else
2252 res = Py_True;
2253 Py_INCREF(res);
2254 return res;
2255 }
2256
2257 /* Search for the first index where items are different */
2258 for (i = 0; i < vl->ob_size && i < wl->ob_size; i++) {
2259 int k = PyObject_RichCompareBool(vl->ob_item[i],
2260 wl->ob_item[i], Py_EQ);
2261 if (k < 0)
2262 return NULL;
2263 if (!k)
2264 break;
2265 }
2266
2267 if (i >= vl->ob_size || i >= wl->ob_size) {
2268 /* No more items to compare -- compare sizes */
2269 int vs = vl->ob_size;
2270 int ws = wl->ob_size;
2271 int cmp;
2272 PyObject *res;
2273 switch (op) {
2274 case Py_LT: cmp = vs < ws; break;
Tim Peters6ee42342001-07-06 17:45:43 +00002275 case Py_LE: cmp = vs <= ws; break;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002276 case Py_EQ: cmp = vs == ws; break;
2277 case Py_NE: cmp = vs != ws; break;
2278 case Py_GT: cmp = vs > ws; break;
2279 case Py_GE: cmp = vs >= ws; break;
2280 default: return NULL; /* cannot happen */
2281 }
2282 if (cmp)
2283 res = Py_True;
2284 else
2285 res = Py_False;
2286 Py_INCREF(res);
2287 return res;
2288 }
2289
2290 /* We have an item that differs -- shortcuts for EQ/NE */
2291 if (op == Py_EQ) {
2292 Py_INCREF(Py_False);
2293 return Py_False;
2294 }
2295 if (op == Py_NE) {
2296 Py_INCREF(Py_True);
2297 return Py_True;
2298 }
2299
2300 /* Compare the final item again using the proper operator */
2301 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
2302}
2303
Tim Peters6d6c1a32001-08-02 04:15:00 +00002304static int
2305list_init(PyListObject *self, PyObject *args, PyObject *kw)
2306{
2307 PyObject *arg = NULL;
2308 static char *kwlist[] = {"sequence", 0};
2309
2310 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2311 return -1;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +00002312 /* Empty previous contents */
Raymond Hettingerfdfe6182004-05-05 06:28:16 +00002313 self->allocated = self->ob_size;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +00002314 if (self->ob_size != 0) {
2315 if (list_ass_slice(self, 0, self->ob_size, (PyObject *)NULL) != 0)
2316 return -1;
2317 }
2318 if (arg != NULL) {
2319 PyObject *rv = listextend(self, arg);
2320 if (rv == NULL)
2321 return -1;
2322 Py_DECREF(rv);
2323 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002324 return 0;
2325}
2326
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002327static long
2328list_nohash(PyObject *self)
2329{
2330 PyErr_SetString(PyExc_TypeError, "list objects are unhashable");
2331 return -1;
2332}
2333
Raymond Hettinger1021c442003-11-07 15:38:09 +00002334static PyObject *list_iter(PyObject *seq);
2335static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2336
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002337PyDoc_STRVAR(getitem_doc,
2338"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002339PyDoc_STRVAR(reversed_doc,
2340"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002341PyDoc_STRVAR(append_doc,
2342"L.append(object) -- append object to end");
2343PyDoc_STRVAR(extend_doc,
Raymond Hettingerf8bcfb12002-12-29 05:49:09 +00002344"L.extend(iterable) -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002345PyDoc_STRVAR(insert_doc,
2346"L.insert(index, object) -- insert object before index");
2347PyDoc_STRVAR(pop_doc,
2348"L.pop([index]) -> item -- remove and return item at index (default last)");
2349PyDoc_STRVAR(remove_doc,
2350"L.remove(value) -- remove first occurrence of value");
2351PyDoc_STRVAR(index_doc,
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002352"L.index(value, [start, [stop]]) -> integer -- return first index of value");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002353PyDoc_STRVAR(count_doc,
2354"L.count(value) -> integer -- return number of occurrences of value");
2355PyDoc_STRVAR(reverse_doc,
2356"L.reverse() -- reverse *IN PLACE*");
2357PyDoc_STRVAR(sort_doc,
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002358"L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\n\
2359cmp(x, y) -> -1, 0, 1");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002360
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002361static PyObject *list_subscript(PyListObject*, PyObject*);
2362
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002363static PyMethodDef list_methods[] = {
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002364 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
Raymond Hettinger1021c442003-11-07 15:38:09 +00002365 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002366 {"append", (PyCFunction)listappend, METH_O, append_doc},
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002367 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002368 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002369 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002370 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002371 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002372 {"count", (PyCFunction)listcount, METH_O, count_doc},
2373 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002374 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
Tim Petersa64dc242002-08-01 02:13:36 +00002375 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002376};
2377
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002378static PySequenceMethods list_as_sequence = {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002379 (inquiry)list_length, /* sq_length */
2380 (binaryfunc)list_concat, /* sq_concat */
2381 (intargfunc)list_repeat, /* sq_repeat */
2382 (intargfunc)list_item, /* sq_item */
2383 (intintargfunc)list_slice, /* sq_slice */
2384 (intobjargproc)list_ass_item, /* sq_ass_item */
2385 (intintobjargproc)list_ass_slice, /* sq_ass_slice */
2386 (objobjproc)list_contains, /* sq_contains */
2387 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2388 (intargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002389};
2390
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002391PyDoc_STRVAR(list_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002392"list() -> new list\n"
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002393"list(sequence) -> new list initialized from sequence's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002394
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002395static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002396list_subscript(PyListObject* self, PyObject* item)
2397{
2398 if (PyInt_Check(item)) {
2399 long i = PyInt_AS_LONG(item);
2400 if (i < 0)
2401 i += PyList_GET_SIZE(self);
2402 return list_item(self, i);
2403 }
2404 else if (PyLong_Check(item)) {
2405 long i = PyLong_AsLong(item);
2406 if (i == -1 && PyErr_Occurred())
2407 return NULL;
2408 if (i < 0)
2409 i += PyList_GET_SIZE(self);
2410 return list_item(self, i);
2411 }
2412 else if (PySlice_Check(item)) {
2413 int start, stop, step, slicelength, cur, i;
2414 PyObject* result;
2415 PyObject* it;
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002416 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002417
2418 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
2419 &start, &stop, &step, &slicelength) < 0) {
2420 return NULL;
2421 }
2422
2423 if (slicelength <= 0) {
2424 return PyList_New(0);
2425 }
2426 else {
2427 result = PyList_New(slicelength);
2428 if (!result) return NULL;
2429
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002430 src = self->ob_item;
2431 dest = ((PyListObject *)result)->ob_item;
Tim Peters3b01a122002-07-19 02:35:45 +00002432 for (cur = start, i = 0; i < slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002433 cur += step, i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002434 it = src[cur];
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002435 Py_INCREF(it);
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002436 dest[i] = it;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002437 }
Tim Peters3b01a122002-07-19 02:35:45 +00002438
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002439 return result;
2440 }
2441 }
2442 else {
2443 PyErr_SetString(PyExc_TypeError,
2444 "list indices must be integers");
2445 return NULL;
2446 }
2447}
2448
Tim Peters3b01a122002-07-19 02:35:45 +00002449static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002450list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2451{
2452 if (PyInt_Check(item)) {
2453 long i = PyInt_AS_LONG(item);
2454 if (i < 0)
2455 i += PyList_GET_SIZE(self);
2456 return list_ass_item(self, i, value);
2457 }
2458 else if (PyLong_Check(item)) {
2459 long i = PyLong_AsLong(item);
2460 if (i == -1 && PyErr_Occurred())
2461 return -1;
2462 if (i < 0)
2463 i += PyList_GET_SIZE(self);
2464 return list_ass_item(self, i, value);
2465 }
2466 else if (PySlice_Check(item)) {
2467 int start, stop, step, slicelength;
2468
2469 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
2470 &start, &stop, &step, &slicelength) < 0) {
2471 return -1;
2472 }
2473
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002474 /* treat L[slice(a,b)] = v _exactly_ like L[a:b] = v */
2475 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
2476 return list_ass_slice(self, start, stop, value);
2477
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002478 if (value == NULL) {
2479 /* delete slice */
Raymond Hettinger4bb95402004-02-13 11:36:39 +00002480 PyObject **garbage;
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002481 int cur, i;
Tim Peters3b01a122002-07-19 02:35:45 +00002482
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002483 if (slicelength <= 0)
2484 return 0;
2485
2486 if (step < 0) {
2487 stop = start + 1;
2488 start = stop + step*(slicelength - 1) - 1;
2489 step = -step;
2490 }
2491
2492 garbage = (PyObject**)
2493 PyMem_MALLOC(slicelength*sizeof(PyObject*));
Tim Peters3b01a122002-07-19 02:35:45 +00002494
2495 /* drawing pictures might help
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002496 understand these for loops */
Guido van Rossum75a20b12002-06-11 12:22:28 +00002497 for (cur = start, i = 0;
2498 cur < stop;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002499 cur += step, i++) {
2500 int lim = step;
2501
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002502 garbage[i] = PyList_GET_ITEM(self, cur);
2503
Michael W. Hudson56796f62002-07-29 14:35:04 +00002504 if (cur + step >= self->ob_size) {
2505 lim = self->ob_size - cur - 1;
2506 }
2507
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002508 memmove(self->ob_item + cur - i,
2509 self->ob_item + cur + 1,
2510 lim * sizeof(PyObject *));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002511 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002512
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002513 for (cur = start + slicelength*step + 1;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002514 cur < self->ob_size; cur++) {
2515 PyList_SET_ITEM(self, cur - slicelength,
2516 PyList_GET_ITEM(self, cur));
2517 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002518
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002519 self->ob_size -= slicelength;
Raymond Hettingerd4ff7412004-03-15 09:01:31 +00002520 list_resize(self, self->ob_size);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002521
2522 for (i = 0; i < slicelength; i++) {
2523 Py_DECREF(garbage[i]);
2524 }
2525 PyMem_FREE(garbage);
2526
2527 return 0;
2528 }
2529 else {
2530 /* assign slice */
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002531 PyObject **garbage, *ins, *seq, **seqitems, **selfitems;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002532 int cur, i;
2533
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002534 /* protect against a[::-1] = a */
Tim Peters3b01a122002-07-19 02:35:45 +00002535 if (self == (PyListObject*)value) {
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002536 seq = list_slice((PyListObject*)value, 0,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002537 PyList_GET_SIZE(value));
Tim Peters3b01a122002-07-19 02:35:45 +00002538 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002539 else {
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002540 seq = PySequence_Fast(value,
2541 "must assign iterable to extended slice");
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002542 if (!seq)
2543 return -1;
2544 }
2545
2546 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2547 PyErr_Format(PyExc_ValueError,
2548 "attempt to assign sequence of size %d to extended slice of size %d",
2549 PySequence_Fast_GET_SIZE(seq),
2550 slicelength);
2551 Py_DECREF(seq);
2552 return -1;
2553 }
2554
2555 if (!slicelength) {
2556 Py_DECREF(seq);
2557 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002558 }
2559
2560 garbage = (PyObject**)
2561 PyMem_MALLOC(slicelength*sizeof(PyObject*));
Tim Peters3b01a122002-07-19 02:35:45 +00002562
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002563 selfitems = self->ob_item;
Raymond Hettinger42bec932004-03-12 16:38:17 +00002564 seqitems = PySequence_Fast_ITEMS(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002565 for (cur = start, i = 0; i < slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002566 cur += step, i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002567 garbage[i] = selfitems[cur];
2568 ins = seqitems[i];
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002569 Py_INCREF(ins);
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002570 selfitems[cur] = ins;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002571 }
2572
2573 for (i = 0; i < slicelength; i++) {
2574 Py_DECREF(garbage[i]);
2575 }
Tim Peters3b01a122002-07-19 02:35:45 +00002576
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002577 PyMem_FREE(garbage);
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002578 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002579
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002580 return 0;
2581 }
Tim Peters3b01a122002-07-19 02:35:45 +00002582 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002583 else {
Tim Peters3b01a122002-07-19 02:35:45 +00002584 PyErr_SetString(PyExc_TypeError,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002585 "list indices must be integers");
2586 return -1;
2587 }
2588}
2589
2590static PyMappingMethods list_as_mapping = {
2591 (inquiry)list_length,
2592 (binaryfunc)list_subscript,
2593 (objobjargproc)list_ass_subscript
2594};
2595
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002596PyTypeObject PyList_Type = {
2597 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002598 0,
2599 "list",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002600 sizeof(PyListObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002601 0,
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002602 (destructor)list_dealloc, /* tp_dealloc */
2603 (printfunc)list_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002604 0, /* tp_getattr */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002605 0, /* tp_setattr */
2606 0, /* tp_compare */
2607 (reprfunc)list_repr, /* tp_repr */
2608 0, /* tp_as_number */
2609 &list_as_sequence, /* tp_as_sequence */
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002610 &list_as_mapping, /* tp_as_mapping */
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002611 list_nohash, /* tp_hash */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002612 0, /* tp_call */
2613 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002614 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002615 0, /* tp_setattro */
2616 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002617 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00002618 Py_TPFLAGS_BASETYPE, /* tp_flags */
2619 list_doc, /* tp_doc */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002620 (traverseproc)list_traverse, /* tp_traverse */
2621 (inquiry)list_clear, /* tp_clear */
2622 list_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002623 0, /* tp_weaklistoffset */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002624 list_iter, /* tp_iter */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002625 0, /* tp_iternext */
2626 list_methods, /* tp_methods */
2627 0, /* tp_members */
2628 0, /* tp_getset */
2629 0, /* tp_base */
2630 0, /* tp_dict */
2631 0, /* tp_descr_get */
2632 0, /* tp_descr_set */
2633 0, /* tp_dictoffset */
2634 (initproc)list_init, /* tp_init */
2635 PyType_GenericAlloc, /* tp_alloc */
2636 PyType_GenericNew, /* tp_new */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002637 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002638};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002639
2640
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002641/*********************** List Iterator **************************/
2642
2643typedef struct {
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002644 PyObject_HEAD
2645 long it_index;
Guido van Rossum86103ae2002-07-16 20:07:32 +00002646 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002647} listiterobject;
2648
2649PyTypeObject PyListIter_Type;
2650
Guido van Rossum5086e492002-07-16 15:56:52 +00002651static PyObject *
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002652list_iter(PyObject *seq)
2653{
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002654 listiterobject *it;
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002655
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002656 if (!PyList_Check(seq)) {
2657 PyErr_BadInternalCall();
2658 return NULL;
2659 }
2660 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2661 if (it == NULL)
2662 return NULL;
2663 it->it_index = 0;
2664 Py_INCREF(seq);
2665 it->it_seq = (PyListObject *)seq;
2666 _PyObject_GC_TRACK(it);
2667 return (PyObject *)it;
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002668}
2669
2670static void
2671listiter_dealloc(listiterobject *it)
2672{
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002673 _PyObject_GC_UNTRACK(it);
Guido van Rossum86103ae2002-07-16 20:07:32 +00002674 Py_XDECREF(it->it_seq);
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002675 PyObject_GC_Del(it);
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002676}
2677
2678static int
2679listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2680{
Guido van Rossum86103ae2002-07-16 20:07:32 +00002681 if (it->it_seq == NULL)
2682 return 0;
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002683 return visit((PyObject *)it->it_seq, arg);
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002684}
2685
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002686static PyObject *
Tim Peters93b2cc42002-06-01 05:22:55 +00002687listiter_next(listiterobject *it)
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002688{
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002689 PyListObject *seq;
2690 PyObject *item;
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002691
Tim Peters93b2cc42002-06-01 05:22:55 +00002692 assert(it != NULL);
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002693 seq = it->it_seq;
Guido van Rossum86103ae2002-07-16 20:07:32 +00002694 if (seq == NULL)
2695 return NULL;
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002696 assert(PyList_Check(seq));
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002697
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002698 if (it->it_index < PyList_GET_SIZE(seq)) {
Tim Peters93b2cc42002-06-01 05:22:55 +00002699 item = PyList_GET_ITEM(seq, it->it_index);
2700 ++it->it_index;
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002701 Py_INCREF(item);
2702 return item;
2703 }
Guido van Rossum86103ae2002-07-16 20:07:32 +00002704
2705 Py_DECREF(seq);
2706 it->it_seq = NULL;
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002707 return NULL;
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002708}
2709
Raymond Hettinger435bf582004-03-18 22:43:10 +00002710static int
2711listiter_len(listiterobject *it)
2712{
Raymond Hettinger40a03822004-04-12 13:05:09 +00002713 int len;
2714 if (it->it_seq) {
2715 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2716 if (len >= 0)
2717 return len;
2718 }
Raymond Hettinger435bf582004-03-18 22:43:10 +00002719 return 0;
2720}
2721
2722static PySequenceMethods listiter_as_sequence = {
2723 (inquiry)listiter_len, /* sq_length */
2724 0, /* sq_concat */
2725};
2726
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002727PyTypeObject PyListIter_Type = {
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002728 PyObject_HEAD_INIT(&PyType_Type)
2729 0, /* ob_size */
2730 "listiterator", /* tp_name */
2731 sizeof(listiterobject), /* tp_basicsize */
2732 0, /* tp_itemsize */
2733 /* methods */
2734 (destructor)listiter_dealloc, /* tp_dealloc */
2735 0, /* tp_print */
2736 0, /* tp_getattr */
2737 0, /* tp_setattr */
2738 0, /* tp_compare */
2739 0, /* tp_repr */
2740 0, /* tp_as_number */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002741 &listiter_as_sequence, /* tp_as_sequence */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002742 0, /* tp_as_mapping */
2743 0, /* tp_hash */
2744 0, /* tp_call */
2745 0, /* tp_str */
2746 PyObject_GenericGetAttr, /* tp_getattro */
2747 0, /* tp_setattro */
2748 0, /* tp_as_buffer */
2749 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2750 0, /* tp_doc */
2751 (traverseproc)listiter_traverse, /* tp_traverse */
2752 0, /* tp_clear */
2753 0, /* tp_richcompare */
2754 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002755 PyObject_SelfIter, /* tp_iter */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002756 (iternextfunc)listiter_next, /* tp_iternext */
Guido van Rossum86103ae2002-07-16 20:07:32 +00002757 0, /* tp_methods */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002758 0, /* tp_members */
2759 0, /* tp_getset */
2760 0, /* tp_base */
2761 0, /* tp_dict */
2762 0, /* tp_descr_get */
2763 0, /* tp_descr_set */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002764};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002765
2766/*********************** List Reverse Iterator **************************/
2767
2768typedef struct {
2769 PyObject_HEAD
2770 long it_index;
Raymond Hettinger001f2282003-11-08 11:58:44 +00002771 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002772} listreviterobject;
2773
2774PyTypeObject PyListRevIter_Type;
2775
2776static PyObject *
2777list_reversed(PyListObject *seq, PyObject *unused)
2778{
2779 listreviterobject *it;
2780
2781 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2782 if (it == NULL)
2783 return NULL;
2784 assert(PyList_Check(seq));
2785 it->it_index = PyList_GET_SIZE(seq) - 1;
2786 Py_INCREF(seq);
2787 it->it_seq = seq;
2788 PyObject_GC_Track(it);
2789 return (PyObject *)it;
2790}
2791
2792static void
2793listreviter_dealloc(listreviterobject *it)
2794{
2795 PyObject_GC_UnTrack(it);
2796 Py_XDECREF(it->it_seq);
2797 PyObject_GC_Del(it);
2798}
2799
2800static int
2801listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2802{
2803 if (it->it_seq == NULL)
2804 return 0;
2805 return visit((PyObject *)it->it_seq, arg);
2806}
2807
2808static PyObject *
2809listreviter_next(listreviterobject *it)
2810{
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002811 PyObject *item;
2812 long index = it->it_index;
2813 PyListObject *seq = it->it_seq;
Raymond Hettinger1021c442003-11-07 15:38:09 +00002814
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002815 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2816 item = PyList_GET_ITEM(seq, index);
Raymond Hettinger1021c442003-11-07 15:38:09 +00002817 it->it_index--;
2818 Py_INCREF(item);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002819 return item;
Raymond Hettinger1021c442003-11-07 15:38:09 +00002820 }
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002821 it->it_index = -1;
2822 if (seq != NULL) {
2823 it->it_seq = NULL;
2824 Py_DECREF(seq);
2825 }
2826 return NULL;
Raymond Hettinger1021c442003-11-07 15:38:09 +00002827}
2828
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002829static int
2830listreviter_len(listreviterobject *it)
2831{
Raymond Hettinger40a03822004-04-12 13:05:09 +00002832 int len = it->it_index + 1;
2833 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2834 return 0;
2835 return len;
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002836}
2837
2838static PySequenceMethods listreviter_as_sequence = {
2839 (inquiry)listreviter_len, /* sq_length */
2840 0, /* sq_concat */
2841};
2842
Raymond Hettinger1021c442003-11-07 15:38:09 +00002843PyTypeObject PyListRevIter_Type = {
2844 PyObject_HEAD_INIT(&PyType_Type)
2845 0, /* ob_size */
2846 "listreverseiterator", /* tp_name */
2847 sizeof(listreviterobject), /* tp_basicsize */
2848 0, /* tp_itemsize */
2849 /* methods */
2850 (destructor)listreviter_dealloc, /* tp_dealloc */
2851 0, /* tp_print */
2852 0, /* tp_getattr */
2853 0, /* tp_setattr */
2854 0, /* tp_compare */
2855 0, /* tp_repr */
2856 0, /* tp_as_number */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002857 &listreviter_as_sequence, /* tp_as_sequence */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002858 0, /* tp_as_mapping */
2859 0, /* tp_hash */
2860 0, /* tp_call */
2861 0, /* tp_str */
2862 PyObject_GenericGetAttr, /* tp_getattro */
2863 0, /* tp_setattro */
2864 0, /* tp_as_buffer */
2865 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2866 0, /* tp_doc */
2867 (traverseproc)listreviter_traverse, /* tp_traverse */
2868 0, /* tp_clear */
2869 0, /* tp_richcompare */
2870 0, /* tp_weaklistoffset */
2871 PyObject_SelfIter, /* tp_iter */
2872 (iternextfunc)listreviter_next, /* tp_iternext */
2873 0,
2874};