blob: 8b5771bf4e3af2222aacc6ed79b861f69a1528ed [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* List object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
4
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00005#ifdef STDC_HEADERS
6#include <stddef.h>
7#else
8#include <sys/types.h> /* For size_t */
9#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010
Tim Peters8d9eb102004-07-31 02:24:20 +000011/* Ensure ob_item has room for at least newsize elements, and set
12 * ob_size to newsize. If newsize > ob_size on entry, the content
13 * of the new slots at exit is undefined heap trash; it's the caller's
14 * responsiblity to overwrite them with sane values.
15 * The number of allocated elements may grow, shrink, or stay the same.
16 * Failure is impossible if newsize <= self.allocated on entry, although
17 * that partly relies on an assumption that the system realloc() never
18 * fails when passed a number of bytes <= the number of bytes last
19 * allocated (the C standard doesn't guarantee this, but it's hard to
20 * imagine a realloc implementation where it wouldn't be true).
21 * Note that self->ob_item may change, and even if newsize is less
22 * than ob_size on entry.
23 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000024static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000025list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000026{
Raymond Hettinger4bb95402004-02-13 11:36:39 +000027 PyObject **items;
Raymond Hettingera84f3ab2004-09-12 19:53:07 +000028 size_t new_allocated;
Martin v. Löwis18e16552006-02-15 17:27:45 +000029 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000030
Raymond Hettinger4bb95402004-02-13 11:36:39 +000031 /* Bypass realloc() when a previous overallocation is large enough
Raymond Hettingera84f3ab2004-09-12 19:53:07 +000032 to accommodate the newsize. If the newsize falls lower than half
33 the allocated size, then proceed with the realloc() to shrink the list.
Raymond Hettinger4bb95402004-02-13 11:36:39 +000034 */
Raymond Hettingera84f3ab2004-09-12 19:53:07 +000035 if (allocated >= newsize && newsize >= (allocated >> 1)) {
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +000036 assert(self->ob_item != NULL || newsize == 0);
Raymond Hettinger4bb95402004-02-13 11:36:39 +000037 self->ob_size = newsize;
38 return 0;
39 }
40
41 /* This over-allocates proportional to the list size, making room
Tim Peters65b8b842001-05-26 05:28:40 +000042 * for additional growth. The over-allocation is mild, but is
43 * enough to give linear-time amortized behavior over a long
44 * sequence of appends() in the presence of a poorly-performing
Raymond Hettingerab517d22004-02-14 18:34:46 +000045 * system realloc().
46 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
Tim Peters65b8b842001-05-26 05:28:40 +000047 */
Raymond Hettingera84f3ab2004-09-12 19:53:07 +000048 new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize;
49 if (newsize == 0)
50 new_allocated = 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000051 items = self->ob_item;
Raymond Hettingera84f3ab2004-09-12 19:53:07 +000052 if (new_allocated <= ((~(size_t)0) / sizeof(PyObject *)))
53 PyMem_RESIZE(items, PyObject *, new_allocated);
Raymond Hettinger4bb95402004-02-13 11:36:39 +000054 else
55 items = NULL;
56 if (items == NULL) {
57 PyErr_NoMemory();
58 return -1;
59 }
60 self->ob_item = items;
61 self->ob_size = newsize;
Raymond Hettingera84f3ab2004-09-12 19:53:07 +000062 self->allocated = new_allocated;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000063 return 0;
64}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000065
Raymond Hettinger0468e412004-05-05 05:37:53 +000066/* Empty list reuse scheme to save calls to malloc and free */
67#define MAXFREELISTS 80
68static PyListObject *free_lists[MAXFREELISTS];
69static int num_free_lists = 0;
70
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +000071void
72PyList_Fini(void)
73{
74 PyListObject *op;
75
76 while (num_free_lists) {
77 num_free_lists--;
78 op = free_lists[num_free_lists];
79 assert(PyList_CheckExact(op));
80 PyObject_GC_Del(op);
81 }
82}
83
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +000085PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 PyListObject *op;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000088 size_t nbytes;
Tim Peters3986d4e2004-07-29 02:28:42 +000089
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 if (size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092 return NULL;
93 }
Tim Peters7049d812004-01-18 20:31:02 +000094 nbytes = size * sizeof(PyObject *);
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000095 /* Check for overflow */
Raymond Hettingerfdfe6182004-05-05 06:28:16 +000096 if (nbytes / sizeof(PyObject *) != (size_t)size)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097 return PyErr_NoMemory();
Raymond Hettinger0468e412004-05-05 05:37:53 +000098 if (num_free_lists) {
99 num_free_lists--;
100 op = free_lists[num_free_lists];
101 _Py_NewReference((PyObject *)op);
102 } else {
103 op = PyObject_GC_New(PyListObject, &PyList_Type);
104 if (op == NULL)
105 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106 }
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000107 if (size <= 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108 op->ob_item = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000110 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000111 if (op->ob_item == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112 return PyErr_NoMemory();
Tim Peters3986d4e2004-07-29 02:28:42 +0000113 memset(op->ob_item, 0, nbytes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114 }
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000115 op->ob_size = size;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000116 op->allocated = size;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000117 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000118 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119}
120
Martin v. Löwis18e16552006-02-15 17:27:45 +0000121Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000122PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000124 if (!PyList_Check(op)) {
125 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 return -1;
127 }
128 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129 return ((PyListObject *)op) -> ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130}
131
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000132static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000133
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000135PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000137 if (!PyList_Check(op)) {
138 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139 return NULL;
140 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000141 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000142 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000143 indexerr = PyString_FromString(
144 "list index out of range");
145 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146 return NULL;
147 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149}
150
151int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000152PyList_SetItem(register PyObject *op, register Py_ssize_t i,
Fred Drakea2f55112000-07-09 15:16:51 +0000153 register PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000155 register PyObject *olditem;
156 register PyObject **p;
157 if (!PyList_Check(op)) {
158 Py_XDECREF(newitem);
159 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000160 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
163 Py_XDECREF(newitem);
164 PyErr_SetString(PyExc_IndexError,
165 "list assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000166 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000168 p = ((PyListObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000169 olditem = *p;
170 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000171 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172 return 0;
173}
174
175static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000176ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000178 Py_ssize_t i, n = self->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179 PyObject **items;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000180 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000181 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000182 return -1;
183 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000184 if (n == INT_MAX) {
Trent Micka5846642000-08-13 22:47:45 +0000185 PyErr_SetString(PyExc_OverflowError,
186 "cannot add more objects to list");
187 return -1;
188 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000189
Raymond Hettingerd4ff7412004-03-15 09:01:31 +0000190 if (list_resize(self, n+1) == -1)
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000191 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000192
Guido van Rossum3a3cca52003-04-14 20:58:14 +0000193 if (where < 0) {
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000194 where += n;
Guido van Rossum3a3cca52003-04-14 20:58:14 +0000195 if (where < 0)
196 where = 0;
197 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000198 if (where > n)
199 where = n;
200 items = self->ob_item;
201 for (i = n; --i >= where; )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202 items[i+1] = items[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204 items[where] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205 return 0;
206}
207
208int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000209PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211 if (!PyList_Check(op)) {
212 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000213 return -1;
214 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216}
217
Raymond Hettinger40a03822004-04-12 13:05:09 +0000218static int
219app1(PyListObject *self, PyObject *v)
220{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000221 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000222
223 assert (v != NULL);
224 if (n == INT_MAX) {
225 PyErr_SetString(PyExc_OverflowError,
226 "cannot add more objects to list");
227 return -1;
228 }
229
230 if (list_resize(self, n+1) == -1)
231 return -1;
232
233 Py_INCREF(v);
234 PyList_SET_ITEM(self, n, v);
235 return 0;
236}
237
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238int
Fred Drakea2f55112000-07-09 15:16:51 +0000239PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240{
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000241 if (PyList_Check(op) && (newitem != NULL))
242 return app1((PyListObject *)op, newitem);
243 PyErr_BadInternalCall();
244 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245}
246
247/* Methods */
248
249static void
Fred Drakea2f55112000-07-09 15:16:51 +0000250list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000252 Py_ssize_t i;
Guido van Rossumff413af2002-03-28 20:34:59 +0000253 PyObject_GC_UnTrack(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000254 Py_TRASHCAN_SAFE_BEGIN(op)
Jack Jansen7874d1f1995-01-19 12:09:27 +0000255 if (op->ob_item != NULL) {
Guido van Rossumfa717011999-06-09 15:19:34 +0000256 /* Do it backwards, for Christian Tismer.
257 There's a simple test case where somehow this reduces
258 thrashing when a *very* large list is created and
259 immediately deleted. */
260 i = op->ob_size;
261 while (--i >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000262 Py_XDECREF(op->ob_item[i]);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000263 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000264 PyMem_FREE(op->ob_item);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000265 }
Raymond Hettinger0468e412004-05-05 05:37:53 +0000266 if (num_free_lists < MAXFREELISTS && PyList_CheckExact(op))
267 free_lists[num_free_lists++] = op;
Tim Petersb38e2b62004-07-29 02:29:26 +0000268 else
269 op->ob_type->tp_free((PyObject *)op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000270 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271}
272
Guido van Rossum90933611991-06-07 16:10:43 +0000273static int
Fred Drakea2f55112000-07-09 15:16:51 +0000274list_print(PyListObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000276 int rc;
277 Py_ssize_t i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000278
Martin v. Löwis18e16552006-02-15 17:27:45 +0000279 rc = Py_ReprEnter((PyObject*)op);
280 if (rc != 0) {
281 if (rc < 0)
282 return rc;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000283 fprintf(fp, "[...]");
284 return 0;
285 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 fprintf(fp, "[");
Guido van Rossum90933611991-06-07 16:10:43 +0000287 for (i = 0; i < op->ob_size; i++) {
288 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289 fprintf(fp, ", ");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000290 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
291 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000292 return -1;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000293 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294 }
295 fprintf(fp, "]");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000296 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000297 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298}
299
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000300static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000301list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000303 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000304 PyObject *s, *temp;
305 PyObject *pieces = NULL, *result = NULL;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000306
307 i = Py_ReprEnter((PyObject*)v);
308 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000309 return i > 0 ? PyString_FromString("[...]") : NULL;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000310 }
Tim Petersa7259592001-06-16 05:11:17 +0000311
312 if (v->ob_size == 0) {
313 result = PyString_FromString("[]");
314 goto Done;
315 }
316
317 pieces = PyList_New(0);
318 if (pieces == NULL)
319 goto Done;
320
321 /* Do repr() on each element. Note that this may mutate the list,
322 so must refetch the list size on each iteration. */
323 for (i = 0; i < v->ob_size; ++i) {
324 int status;
325 s = PyObject_Repr(v->ob_item[i]);
326 if (s == NULL)
327 goto Done;
328 status = PyList_Append(pieces, s);
329 Py_DECREF(s); /* append created a new ref */
330 if (status < 0)
331 goto Done;
332 }
333
334 /* Add "[]" decorations to the first and last items. */
335 assert(PyList_GET_SIZE(pieces) > 0);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000336 s = PyString_FromString("[");
Tim Petersa7259592001-06-16 05:11:17 +0000337 if (s == NULL)
338 goto Done;
339 temp = PyList_GET_ITEM(pieces, 0);
340 PyString_ConcatAndDel(&s, temp);
341 PyList_SET_ITEM(pieces, 0, s);
342 if (s == NULL)
343 goto Done;
344
345 s = PyString_FromString("]");
346 if (s == NULL)
347 goto Done;
348 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
349 PyString_ConcatAndDel(&temp, s);
350 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
351 if (temp == NULL)
352 goto Done;
353
354 /* Paste them all together with ", " between. */
355 s = PyString_FromString(", ");
356 if (s == NULL)
357 goto Done;
358 result = _PyString_Join(s, pieces);
Tim Peters3b01a122002-07-19 02:35:45 +0000359 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000360
361Done:
362 Py_XDECREF(pieces);
Guido van Rossumfb376de1998-04-10 22:47:27 +0000363 Py_ReprLeave((PyObject *)v);
Tim Petersa7259592001-06-16 05:11:17 +0000364 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365}
366
Martin v. Löwis18e16552006-02-15 17:27:45 +0000367static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000368list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369{
370 return a->ob_size;
371}
372
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000373static int
Fred Drakea2f55112000-07-09 15:16:51 +0000374list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000375{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000376 Py_ssize_t i;
377 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000378
Raymond Hettingeraae59992002-09-05 14:23:49 +0000379 for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i)
380 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
Guido van Rossum65e1cea2001-01-17 22:11:59 +0000381 Py_EQ);
Neal Norwitzbb9c5f52002-09-05 21:32:55 +0000382 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000383}
384
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000385static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000386list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387{
388 if (i < 0 || i >= a->ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000389 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000390 indexerr = PyString_FromString(
391 "list index out of range");
392 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393 return NULL;
394 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000395 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396 return a->ob_item[i];
397}
398
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000399static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000400list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402 PyListObject *np;
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000403 PyObject **src, **dest;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000404 Py_ssize_t i, len;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405 if (ilow < 0)
406 ilow = 0;
407 else if (ilow > a->ob_size)
408 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409 if (ihigh < ilow)
410 ihigh = ilow;
411 else if (ihigh > a->ob_size)
412 ihigh = a->ob_size;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000413 len = ihigh - ilow;
414 np = (PyListObject *) PyList_New(len);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415 if (np == NULL)
416 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000417
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000418 src = a->ob_item + ilow;
419 dest = np->ob_item;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000420 for (i = 0; i < len; i++) {
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000421 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000422 Py_INCREF(v);
Raymond Hettingerb7d05db2004-03-08 07:25:05 +0000423 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426}
427
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000428PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000429PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000430{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431 if (!PyList_Check(a)) {
432 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000433 return NULL;
434 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000435 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000436}
437
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000439list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000441 Py_ssize_t size;
442 Py_ssize_t i;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000443 PyObject **src, **dest;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000444 PyListObject *np;
445 if (!PyList_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000446 PyErr_Format(PyExc_TypeError,
Fred Drake914a2ed2000-06-01 14:31:03 +0000447 "can only concatenate list (not \"%.200s\") to list",
448 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449 return NULL;
450 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451#define b ((PyListObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000452 size = a->ob_size + b->ob_size;
Guido van Rossuma5c0e6d2002-10-11 21:05:56 +0000453 if (size < 0)
454 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000457 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000459 src = a->ob_item;
460 dest = np->ob_item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461 for (i = 0; i < a->ob_size; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000462 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000463 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000464 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000466 src = b->ob_item;
467 dest = np->ob_item + a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468 for (i = 0; i < b->ob_size; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000469 PyObject *v = src[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000470 Py_INCREF(v);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000471 dest[i] = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474#undef b
475}
476
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000478list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000479{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000480 Py_ssize_t i, j;
481 Py_ssize_t size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000482 PyListObject *np;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000483 PyObject **p, **items;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000484 PyObject *elem;
Guido van Rossumed98d481991-03-06 13:07:53 +0000485 if (n < 0)
486 n = 0;
487 size = a->ob_size * n;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000488 if (size == 0)
489 return PyList_New(0);
Guido van Rossumbfa5a142002-10-11 23:39:35 +0000490 if (n && size/n != a->ob_size)
Guido van Rossuma5c0e6d2002-10-11 21:05:56 +0000491 return PyErr_NoMemory();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000492 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000493 if (np == NULL)
494 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000495
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000496 items = np->ob_item;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000497 if (a->ob_size == 1) {
498 elem = a->ob_item[0];
499 for (i = 0; i < n; i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000500 items[i] = elem;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000501 Py_INCREF(elem);
502 }
503 return (PyObject *) np;
504 }
Guido van Rossumed98d481991-03-06 13:07:53 +0000505 p = np->ob_item;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000506 items = a->ob_item;
Guido van Rossumed98d481991-03-06 13:07:53 +0000507 for (i = 0; i < n; i++) {
508 for (j = 0; j < a->ob_size; j++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000509 *p = items[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000511 p++;
512 }
513 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000514 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000515}
516
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517static int
Armin Rigo93677f02004-07-29 12:40:23 +0000518list_clear(PyListObject *a)
519{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000520 Py_ssize_t i;
Armin Rigo93677f02004-07-29 12:40:23 +0000521 PyObject **item = a->ob_item;
522 if (item != NULL) {
523 /* Because XDECREF can recursively invoke operations on
524 this list, we make it empty first. */
525 i = a->ob_size;
526 a->ob_size = 0;
527 a->ob_item = NULL;
528 a->allocated = 0;
529 while (--i >= 0) {
530 Py_XDECREF(item[i]);
531 }
532 PyMem_FREE(item);
533 }
534 /* Never fails; the return value can be ignored.
535 Note that there is no guarantee that the list is actually empty
536 at this point, because XDECREF may have populated it again! */
537 return 0;
538}
539
Tim Peters8fc4a912004-07-31 21:53:19 +0000540/* a[ilow:ihigh] = v if v != NULL.
541 * del a[ilow:ihigh] if v == NULL.
542 *
543 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
544 * guaranteed the call cannot fail.
545 */
Armin Rigo93677f02004-07-29 12:40:23 +0000546static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000547list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000549 /* Because [X]DECREF can recursively invoke list operations on
550 this list, we must postpone all [X]DECREF activity until
551 after the list is back in its canonical shape. Therefore
552 we must allocate an additional array, 'recycle', into which
553 we temporarily copy the items that are deleted from the
554 list. :-( */
Tim Peters73572222004-07-31 02:54:42 +0000555 PyObject *recycle_on_stack[8];
556 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000557 PyObject **item;
Raymond Hettingerf889e102004-03-09 08:04:33 +0000558 PyObject **vitem = NULL;
Michael W. Hudson5da854f2002-11-05 17:38:05 +0000559 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000560 Py_ssize_t n; /* # of elements in replacement list */
561 Py_ssize_t norig; /* # of elements in list getting replaced */
562 Py_ssize_t d; /* Change in size */
563 Py_ssize_t k;
Tim Peters8d9eb102004-07-31 02:24:20 +0000564 size_t s;
565 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000566#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000567 if (v == NULL)
568 n = 0;
Michael W. Hudson5da854f2002-11-05 17:38:05 +0000569 else {
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000570 if (a == b) {
571 /* Special case "a[i:j] = a" -- copy b first */
Michael W. Hudsonda0a0672003-08-15 12:06:41 +0000572 v = list_slice(b, 0, b->ob_size);
Martin v. Löwiscd12bfc2003-05-03 10:53:08 +0000573 if (v == NULL)
Tim Peters8d9eb102004-07-31 02:24:20 +0000574 return result;
575 result = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000576 Py_DECREF(v);
Tim Peters8d9eb102004-07-31 02:24:20 +0000577 return result;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000578 }
Raymond Hettingerf889e102004-03-09 08:04:33 +0000579 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
Michael W. Hudsonb4f49382003-08-14 17:04:28 +0000580 if(v_as_SF == NULL)
Tim Peters8d9eb102004-07-31 02:24:20 +0000581 goto Error;
Michael W. Hudsonb4f49382003-08-14 17:04:28 +0000582 n = PySequence_Fast_GET_SIZE(v_as_SF);
Raymond Hettinger42bec932004-03-12 16:38:17 +0000583 vitem = PySequence_Fast_ITEMS(v_as_SF);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000584 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000585 if (ilow < 0)
586 ilow = 0;
587 else if (ilow > a->ob_size)
588 ilow = a->ob_size;
Tim Peters8d9eb102004-07-31 02:24:20 +0000589
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590 if (ihigh < ilow)
591 ihigh = ilow;
592 else if (ihigh > a->ob_size)
593 ihigh = a->ob_size;
Armin Rigo93677f02004-07-29 12:40:23 +0000594
Tim Peters8d9eb102004-07-31 02:24:20 +0000595 norig = ihigh - ilow;
596 assert(norig >= 0);
597 d = n - norig;
Armin Rigo93677f02004-07-29 12:40:23 +0000598 if (a->ob_size + d == 0) {
599 Py_XDECREF(v_as_SF);
600 return list_clear(a);
601 }
602 item = a->ob_item;
Tim Peters8d9eb102004-07-31 02:24:20 +0000603 /* recycle the items that we are about to remove */
604 s = norig * sizeof(PyObject *);
Tim Peters73572222004-07-31 02:54:42 +0000605 if (s > sizeof(recycle_on_stack)) {
Armin Rigo1dd04a02004-07-30 11:38:22 +0000606 recycle = (PyObject **)PyMem_MALLOC(s);
Martin v. Löwiscd12bfc2003-05-03 10:53:08 +0000607 if (recycle == NULL) {
608 PyErr_NoMemory();
Tim Peters8d9eb102004-07-31 02:24:20 +0000609 goto Error;
Martin v. Löwiscd12bfc2003-05-03 10:53:08 +0000610 }
611 }
Armin Rigo1dd04a02004-07-30 11:38:22 +0000612 memcpy(recycle, &item[ilow], s);
Tim Peters8d9eb102004-07-31 02:24:20 +0000613
Armin Rigo1dd04a02004-07-30 11:38:22 +0000614 if (d < 0) { /* Delete -d items */
615 memmove(&item[ihigh+d], &item[ihigh],
616 (a->ob_size - ihigh)*sizeof(PyObject *));
617 list_resize(a, a->ob_size + d);
618 item = a->ob_item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619 }
Armin Rigo1dd04a02004-07-30 11:38:22 +0000620 else if (d > 0) { /* Insert d items */
Tim Peters73572222004-07-31 02:54:42 +0000621 k = a->ob_size;
622 if (list_resize(a, k+d) < 0)
Tim Peters8d9eb102004-07-31 02:24:20 +0000623 goto Error;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000624 item = a->ob_item;
Raymond Hettingerf889e102004-03-09 08:04:33 +0000625 memmove(&item[ihigh+d], &item[ihigh],
Tim Peters73572222004-07-31 02:54:42 +0000626 (k - ihigh)*sizeof(PyObject *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000627 }
628 for (k = 0; k < n; k++, ilow++) {
Raymond Hettingerf889e102004-03-09 08:04:33 +0000629 PyObject *w = vitem[k];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631 item[ilow] = w;
632 }
Tim Peters73572222004-07-31 02:54:42 +0000633 for (k = norig - 1; k >= 0; --k)
634 Py_XDECREF(recycle[k]);
Tim Peters8d9eb102004-07-31 02:24:20 +0000635 result = 0;
636 Error:
Tim Peters73572222004-07-31 02:54:42 +0000637 if (recycle != recycle_on_stack)
Armin Rigo1dd04a02004-07-30 11:38:22 +0000638 PyMem_FREE(recycle);
Michael W. Hudson5da854f2002-11-05 17:38:05 +0000639 Py_XDECREF(v_as_SF);
Tim Peters8d9eb102004-07-31 02:24:20 +0000640 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641#undef b
642}
643
Guido van Rossum234f9421993-06-17 12:35:49 +0000644int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000645PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000646{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647 if (!PyList_Check(a)) {
648 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000649 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000650 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000651 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000652}
653
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000654static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000655list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000656{
657 PyObject **items;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000658 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000659
660
661 size = PyList_GET_SIZE(self);
662 if (size == 0) {
663 Py_INCREF(self);
664 return (PyObject *)self;
665 }
666
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000667 if (n < 1) {
Armin Rigo93677f02004-07-29 12:40:23 +0000668 (void)list_clear(self);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000669 Py_INCREF(self);
670 return (PyObject *)self;
671 }
672
Tim Petersb38e2b62004-07-29 02:29:26 +0000673 if (list_resize(self, size*n) == -1)
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000674 return NULL;
675
676 p = size;
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000677 items = self->ob_item;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000678 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
679 for (j = 0; j < size; j++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000680 PyObject *o = items[j];
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000681 Py_INCREF(o);
Raymond Hettingera6366fe2004-03-09 13:05:22 +0000682 items[p++] = o;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000683 }
684 }
685 Py_INCREF(self);
686 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000687}
688
Guido van Rossum4a450d01991-04-03 19:05:18 +0000689static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000690list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000691{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000692 PyObject *old_value;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000693 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 PyErr_SetString(PyExc_IndexError,
695 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000696 return -1;
697 }
698 if (v == NULL)
699 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000700 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000701 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000702 a->ob_item[i] = v;
Tim Peters3b01a122002-07-19 02:35:45 +0000703 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000704 return 0;
705}
706
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000708listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000709{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000710 Py_ssize_t i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000711 PyObject *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000712 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000713 return NULL;
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +0000714 if (ins1(self, i, v) == 0)
715 Py_RETURN_NONE;
Raymond Hettinger501f02c2004-04-12 14:01:16 +0000716 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000717}
718
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000719static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000720listappend(PyListObject *self, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000721{
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +0000722 if (app1(self, v) == 0)
723 Py_RETURN_NONE;
Raymond Hettinger501f02c2004-04-12 14:01:16 +0000724 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000725}
726
Barry Warsawdedf6d61998-10-09 16:37:25 +0000727static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000728listextend(PyListObject *self, PyObject *b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000729{
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000730 PyObject *it; /* iter(v) */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000731 Py_ssize_t m; /* size of self */
732 Py_ssize_t n; /* guess for size of b */
733 Py_ssize_t mn; /* m + n */
734 Py_ssize_t i;
Raymond Hettinger57c45422004-03-11 09:48:18 +0000735 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000736
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000737 /* Special cases:
Tim Petersb38e2b62004-07-29 02:29:26 +0000738 1) lists and tuples which can use PySequence_Fast ops
739 2) extending self to self requires making a copy first
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000740 */
741 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
Armin Rigo70d172d2004-03-20 22:19:23 +0000742 PyObject **src, **dest;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000743 b = PySequence_Fast(b, "argument must be iterable");
744 if (!b)
745 return NULL;
Armin Rigo70d172d2004-03-20 22:19:23 +0000746 n = PySequence_Fast_GET_SIZE(b);
747 if (n == 0) {
748 /* short circuit when b is empty */
749 Py_DECREF(b);
750 Py_RETURN_NONE;
751 }
752 m = self->ob_size;
753 if (list_resize(self, m + n) == -1) {
754 Py_DECREF(b);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000755 return NULL;
Armin Rigo70d172d2004-03-20 22:19:23 +0000756 }
757 /* note that we may still have self == b here for the
758 * situation a.extend(a), but the following code works
759 * in that case too. Just make sure to resize self
760 * before calling PySequence_Fast_ITEMS.
761 */
762 /* populate the end of self with b's items */
763 src = PySequence_Fast_ITEMS(b);
764 dest = self->ob_item + m;
765 for (i = 0; i < n; i++) {
766 PyObject *o = src[i];
767 Py_INCREF(o);
768 dest[i] = o;
769 }
770 Py_DECREF(b);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000771 Py_RETURN_NONE;
772 }
773
774 it = PyObject_GetIter(b);
775 if (it == NULL)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000776 return NULL;
Raymond Hettinger57c45422004-03-11 09:48:18 +0000777 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000778
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000779 /* Guess a result list size. */
Armin Rigof5b3e362006-02-11 21:32:43 +0000780 n = _PyObject_LengthHint(b);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000781 if (n < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000782 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
783 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
784 Py_DECREF(it);
785 return NULL;
786 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000787 PyErr_Clear();
788 n = 8; /* arbitrary */
789 }
790 m = self->ob_size;
791 mn = m + n;
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000792 if (mn >= m) {
793 /* Make room. */
794 if (list_resize(self, mn) == -1)
795 goto error;
796 /* Make the list sane again. */
797 self->ob_size = m;
798 }
799 /* Else m + n overflowed; on the chance that n lied, and there really
800 * is enough room, ignore it. If n was telling the truth, we'll
801 * eventually run out of memory during the loop.
802 */
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000803
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000804 /* Run iterator to exhaustion. */
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000805 for (;;) {
Raymond Hettinger57c45422004-03-11 09:48:18 +0000806 PyObject *item = iternext(it);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000807 if (item == NULL) {
Raymond Hettinger57c45422004-03-11 09:48:18 +0000808 if (PyErr_Occurred()) {
809 if (PyErr_ExceptionMatches(PyExc_StopIteration))
810 PyErr_Clear();
811 else
812 goto error;
813 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000814 break;
815 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000816 if (self->ob_size < self->allocated) {
817 /* steals ref */
818 PyList_SET_ITEM(self, self->ob_size, item);
819 ++self->ob_size;
820 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000821 else {
Raymond Hettinger40a03822004-04-12 13:05:09 +0000822 int status = app1(self, item);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000823 Py_DECREF(item); /* append creates a new ref */
824 if (status < 0)
825 goto error;
826 }
827 }
828
829 /* Cut back result list if initial guess was too large. */
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000830 if (self->ob_size < self->allocated)
831 list_resize(self, self->ob_size); /* shrinking can't fail */
832
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000833 Py_DECREF(it);
834 Py_RETURN_NONE;
835
836 error:
837 Py_DECREF(it);
838 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000839}
840
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000841PyObject *
842_PyList_Extend(PyListObject *self, PyObject *b)
843{
844 return listextend(self, b);
845}
846
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000847static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000848list_inplace_concat(PyListObject *self, PyObject *other)
849{
850 PyObject *result;
851
852 result = listextend(self, other);
853 if (result == NULL)
854 return result;
855 Py_DECREF(result);
856 Py_INCREF(self);
857 return (PyObject *)self;
858}
859
860static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000861listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000862{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000863 Py_ssize_t i = -1;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000864 PyObject *v, *arg = NULL;
Tim Peters8fc4a912004-07-31 21:53:19 +0000865 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000866
867 if (!PyArg_UnpackTuple(args, "pop", 0, 1, &arg))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000868 return NULL;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000869 if (arg != NULL) {
870 if (PyInt_Check(arg))
Martin v. Löwis18e16552006-02-15 17:27:45 +0000871 i = PyInt_AS_LONG((PyIntObject*) arg);
872 else if (!PyArg_ParseTuple(args, "|n:pop", &i))
Raymond Hettingerfa6c6f82004-02-19 06:12:06 +0000873 return NULL;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000874 }
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000875 if (self->ob_size == 0) {
876 /* Special-case most common failure cause */
877 PyErr_SetString(PyExc_IndexError, "pop from empty list");
878 return NULL;
879 }
880 if (i < 0)
881 i += self->ob_size;
882 if (i < 0 || i >= self->ob_size) {
883 PyErr_SetString(PyExc_IndexError, "pop index out of range");
884 return NULL;
885 }
886 v = self->ob_item[i];
Raymond Hettingercb3e5802004-02-13 18:36:31 +0000887 if (i == self->ob_size - 1) {
Tim Peters8fc4a912004-07-31 21:53:19 +0000888 status = list_resize(self, self->ob_size - 1);
889 assert(status >= 0);
890 return v; /* and v now owns the reference the list had */
Raymond Hettingercb3e5802004-02-13 18:36:31 +0000891 }
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000892 Py_INCREF(v);
Tim Peters8fc4a912004-07-31 21:53:19 +0000893 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
894 assert(status >= 0);
895 /* Use status, so that in a release build compilers don't
896 * complain about the unused name.
897 */
Brett Cannon651dd522004-08-08 21:21:18 +0000898 (void) status;
899
900 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000901}
902
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000903/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
904static void
905reverse_slice(PyObject **lo, PyObject **hi)
906{
907 assert(lo && hi);
908
909 --hi;
910 while (lo < hi) {
911 PyObject *t = *lo;
912 *lo = *hi;
913 *hi = t;
914 ++lo;
915 --hi;
916 }
917}
918
Tim Petersa64dc242002-08-01 02:13:36 +0000919/* Lots of code for an adaptive, stable, natural mergesort. There are many
920 * pieces to this algorithm; read listsort.txt for overviews and details.
921 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000922
Guido van Rossum3f236de1996-12-10 23:55:39 +0000923/* Comparison function. Takes care of calling a user-supplied
Tim Peters66860f62002-08-04 17:47:26 +0000924 * comparison function (any callable Python object), which must not be
925 * NULL (use the ISLT macro if you don't know, or call PyObject_RichCompareBool
926 * with Py_LT if you know it's NULL).
Tim Petersa64dc242002-08-01 02:13:36 +0000927 * Returns -1 on error, 1 if x < y, 0 if x >= y.
928 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000929static int
Tim Petersa8c974c2002-07-19 03:30:57 +0000930islt(PyObject *x, PyObject *y, PyObject *compare)
Guido van Rossum3f236de1996-12-10 23:55:39 +0000931{
Tim Petersf2a04732002-07-11 21:46:16 +0000932 PyObject *res;
933 PyObject *args;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000934 Py_ssize_t i;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000935
Tim Peters66860f62002-08-04 17:47:26 +0000936 assert(compare != NULL);
Tim Petersa8c974c2002-07-19 03:30:57 +0000937 /* Call the user's comparison function and translate the 3-way
938 * result into true or false (or error).
939 */
Tim Petersf2a04732002-07-11 21:46:16 +0000940 args = PyTuple_New(2);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000941 if (args == NULL)
Tim Petersa8c974c2002-07-19 03:30:57 +0000942 return -1;
Tim Petersf2a04732002-07-11 21:46:16 +0000943 Py_INCREF(x);
944 Py_INCREF(y);
945 PyTuple_SET_ITEM(args, 0, x);
946 PyTuple_SET_ITEM(args, 1, y);
Tim Peters58cf3612002-07-15 05:16:13 +0000947 res = PyObject_Call(compare, args, NULL);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000948 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000949 if (res == NULL)
Tim Petersa8c974c2002-07-19 03:30:57 +0000950 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000951 if (!PyInt_Check(res)) {
952 Py_DECREF(res);
953 PyErr_SetString(PyExc_TypeError,
Guido van Rossum9bcd1d71999-04-19 17:44:39 +0000954 "comparison function must return int");
Tim Petersa8c974c2002-07-19 03:30:57 +0000955 return -1;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000956 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000957 i = PyInt_AsLong(res);
958 Py_DECREF(res);
Tim Petersa8c974c2002-07-19 03:30:57 +0000959 return i < 0;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000960}
961
Tim Peters66860f62002-08-04 17:47:26 +0000962/* If COMPARE is NULL, calls PyObject_RichCompareBool with Py_LT, else calls
963 * islt. This avoids a layer of function call in the usual case, and
964 * sorting does many comparisons.
965 * Returns -1 on error, 1 if x < y, 0 if x >= y.
966 */
967#define ISLT(X, Y, COMPARE) ((COMPARE) == NULL ? \
968 PyObject_RichCompareBool(X, Y, Py_LT) : \
969 islt(X, Y, COMPARE))
970
971/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +0000972 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
973 started. It makes more sense in context <wink>. X and Y are PyObject*s.
974*/
Tim Peters66860f62002-08-04 17:47:26 +0000975#define IFLT(X, Y) if ((k = ISLT(X, Y, compare)) < 0) goto fail; \
Tim Petersa8c974c2002-07-19 03:30:57 +0000976 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000977
978/* binarysort is the best method for sorting small arrays: it does
979 few compares, but can do data movement quadratic in the number of
980 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000981 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +0000982 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000983 On entry, must have lo <= start <= hi, and that [lo, start) is already
984 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +0000985 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000986 Even in case of error, the output slice will be some permutation of
987 the input (nothing is lost or duplicated).
988*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000989static int
Fred Drakea2f55112000-07-09 15:16:51 +0000990binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
991 /* compare -- comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000992{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000993 register Py_ssize_t k;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000994 register PyObject **l, **p, **r;
995 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000996
Tim Petersa8c974c2002-07-19 03:30:57 +0000997 assert(lo <= start && start <= hi);
998 /* assert [lo, start) is sorted */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000999 if (lo == start)
1000 ++start;
1001 for (; start < hi; ++start) {
1002 /* set l to where *start belongs */
1003 l = lo;
1004 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001005 pivot = *r;
Tim Peters0fe977c2002-07-19 06:12:32 +00001006 /* Invariants:
1007 * pivot >= all in [lo, l).
1008 * pivot < all in [r, start).
1009 * The second is vacuously true at the start.
1010 */
1011 assert(l < r);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001012 do {
1013 p = l + ((r - l) >> 1);
Tim Petersa8c974c2002-07-19 03:30:57 +00001014 IFLT(pivot, *p)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001015 r = p;
1016 else
Tim Peters0fe977c2002-07-19 06:12:32 +00001017 l = p+1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001018 } while (l < r);
Tim Peters0fe977c2002-07-19 06:12:32 +00001019 assert(l == r);
1020 /* The invariants still hold, so pivot >= all in [lo, l) and
1021 pivot < all in [l, start), so pivot belongs at l. Note
1022 that if there are elements equal to pivot, l points to the
1023 first slot after them -- that's why this sort is stable.
1024 Slide over to make room.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001025 Caution: using memmove is much slower under MSVC 5;
1026 we're not usually moving many slots. */
1027 for (p = start; p > l; --p)
1028 *p = *(p-1);
1029 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001030 }
Guido van Rossum3f236de1996-12-10 23:55:39 +00001031 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001032
1033 fail:
1034 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001035}
1036
Tim Petersa64dc242002-08-01 02:13:36 +00001037/*
1038Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1039is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001040
Tim Petersa64dc242002-08-01 02:13:36 +00001041 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001042
Tim Petersa64dc242002-08-01 02:13:36 +00001043or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001044
Tim Petersa64dc242002-08-01 02:13:36 +00001045 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001046
Tim Petersa64dc242002-08-01 02:13:36 +00001047Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1048For its intended use in a stable mergesort, the strictness of the defn of
1049"descending" is needed so that the caller can safely reverse a descending
1050sequence without violating stability (strict > ensures there are no equal
1051elements to get out of order).
1052
1053Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001054*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001055static Py_ssize_t
Tim Petersa64dc242002-08-01 02:13:36 +00001056count_run(PyObject **lo, PyObject **hi, PyObject *compare, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001057{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001058 Py_ssize_t k;
1059 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001060
Tim Petersa64dc242002-08-01 02:13:36 +00001061 assert(lo < hi);
1062 *descending = 0;
1063 ++lo;
1064 if (lo == hi)
1065 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001066
Tim Petersa64dc242002-08-01 02:13:36 +00001067 n = 2;
1068 IFLT(*lo, *(lo-1)) {
1069 *descending = 1;
1070 for (lo = lo+1; lo < hi; ++lo, ++n) {
1071 IFLT(*lo, *(lo-1))
1072 ;
1073 else
1074 break;
1075 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001076 }
Tim Petersa64dc242002-08-01 02:13:36 +00001077 else {
1078 for (lo = lo+1; lo < hi; ++lo, ++n) {
1079 IFLT(*lo, *(lo-1))
1080 break;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001081 }
1082 }
1083
Tim Petersa64dc242002-08-01 02:13:36 +00001084 return n;
1085fail:
1086 return -1;
1087}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001088
Tim Petersa64dc242002-08-01 02:13:36 +00001089/*
1090Locate the proper position of key in a sorted vector; if the vector contains
1091an element equal to key, return the position immediately to the left of
1092the leftmost equal element. [gallop_right() does the same except returns
1093the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001094
Tim Petersa64dc242002-08-01 02:13:36 +00001095"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001096
Tim Petersa64dc242002-08-01 02:13:36 +00001097"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1098hint is to the final result, the faster this runs.
1099
1100The return value is the int k in 0..n such that
1101
1102 a[k-1] < key <= a[k]
1103
1104pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1105key belongs at index k; or, IOW, the first k elements of a should precede
1106key, and the last n-k should follow key.
1107
1108Returns -1 on error. See listsort.txt for info on the method.
1109*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001110static Py_ssize_t
1111gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare)
Tim Petersa64dc242002-08-01 02:13:36 +00001112{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001113 Py_ssize_t ofs;
1114 Py_ssize_t lastofs;
1115 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001116
1117 assert(key && a && n > 0 && hint >= 0 && hint < n);
1118
1119 a += hint;
1120 lastofs = 0;
1121 ofs = 1;
1122 IFLT(*a, key) {
1123 /* a[hint] < key -- gallop right, until
1124 * a[hint + lastofs] < key <= a[hint + ofs]
1125 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001126 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
Tim Petersa64dc242002-08-01 02:13:36 +00001127 while (ofs < maxofs) {
1128 IFLT(a[ofs], key) {
1129 lastofs = ofs;
1130 ofs = (ofs << 1) + 1;
1131 if (ofs <= 0) /* int overflow */
1132 ofs = maxofs;
1133 }
1134 else /* key <= a[hint + ofs] */
1135 break;
1136 }
1137 if (ofs > maxofs)
1138 ofs = maxofs;
1139 /* Translate back to offsets relative to &a[0]. */
1140 lastofs += hint;
1141 ofs += hint;
1142 }
1143 else {
1144 /* key <= a[hint] -- gallop left, until
1145 * a[hint - ofs] < key <= a[hint - lastofs]
1146 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001147 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
Tim Petersa64dc242002-08-01 02:13:36 +00001148 while (ofs < maxofs) {
1149 IFLT(*(a-ofs), key)
1150 break;
1151 /* key <= a[hint - ofs] */
1152 lastofs = ofs;
1153 ofs = (ofs << 1) + 1;
1154 if (ofs <= 0) /* int overflow */
1155 ofs = maxofs;
1156 }
1157 if (ofs > maxofs)
1158 ofs = maxofs;
1159 /* Translate back to positive offsets relative to &a[0]. */
1160 k = lastofs;
1161 lastofs = hint - ofs;
1162 ofs = hint - k;
1163 }
1164 a -= hint;
1165
1166 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1167 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1168 * right of lastofs but no farther right than ofs. Do a binary
1169 * search, with invariant a[lastofs-1] < key <= a[ofs].
1170 */
1171 ++lastofs;
1172 while (lastofs < ofs) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001173 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001174
1175 IFLT(a[m], key)
1176 lastofs = m+1; /* a[m] < key */
1177 else
1178 ofs = m; /* key <= a[m] */
1179 }
1180 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1181 return ofs;
1182
1183fail:
1184 return -1;
1185}
1186
1187/*
1188Exactly like gallop_left(), except that if key already exists in a[0:n],
1189finds the position immediately to the right of the rightmost equal value.
1190
1191The return value is the int k in 0..n such that
1192
1193 a[k-1] <= key < a[k]
1194
1195or -1 if error.
1196
1197The code duplication is massive, but this is enough different given that
1198we're sticking to "<" comparisons that it's much harder to follow if
1199written as one routine with yet another "left or right?" flag.
1200*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001201static Py_ssize_t
1202gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare)
Tim Petersa64dc242002-08-01 02:13:36 +00001203{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001204 Py_ssize_t ofs;
1205 Py_ssize_t lastofs;
1206 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001207
1208 assert(key && a && n > 0 && hint >= 0 && hint < n);
1209
1210 a += hint;
1211 lastofs = 0;
1212 ofs = 1;
1213 IFLT(key, *a) {
1214 /* key < a[hint] -- gallop left, until
1215 * a[hint - ofs] <= key < a[hint - lastofs]
1216 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001217 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
Tim Petersa64dc242002-08-01 02:13:36 +00001218 while (ofs < maxofs) {
1219 IFLT(key, *(a-ofs)) {
1220 lastofs = ofs;
1221 ofs = (ofs << 1) + 1;
1222 if (ofs <= 0) /* int overflow */
1223 ofs = maxofs;
1224 }
1225 else /* a[hint - ofs] <= key */
1226 break;
1227 }
1228 if (ofs > maxofs)
1229 ofs = maxofs;
1230 /* Translate back to positive offsets relative to &a[0]. */
1231 k = lastofs;
1232 lastofs = hint - ofs;
1233 ofs = hint - k;
1234 }
1235 else {
1236 /* a[hint] <= key -- gallop right, until
1237 * a[hint + lastofs] <= key < a[hint + ofs]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001238 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001239 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
Tim Petersa64dc242002-08-01 02:13:36 +00001240 while (ofs < maxofs) {
1241 IFLT(key, a[ofs])
1242 break;
1243 /* a[hint + ofs] <= key */
1244 lastofs = ofs;
1245 ofs = (ofs << 1) + 1;
1246 if (ofs <= 0) /* int overflow */
1247 ofs = maxofs;
1248 }
1249 if (ofs > maxofs)
1250 ofs = maxofs;
1251 /* Translate back to offsets relative to &a[0]. */
1252 lastofs += hint;
1253 ofs += hint;
1254 }
1255 a -= hint;
1256
1257 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1258 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1259 * right of lastofs but no farther right than ofs. Do a binary
1260 * search, with invariant a[lastofs-1] <= key < a[ofs].
1261 */
1262 ++lastofs;
1263 while (lastofs < ofs) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001264 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001265
1266 IFLT(key, a[m])
1267 ofs = m; /* key < a[m] */
1268 else
1269 lastofs = m+1; /* a[m] <= key */
1270 }
1271 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1272 return ofs;
1273
1274fail:
1275 return -1;
1276}
1277
1278/* The maximum number of entries in a MergeState's pending-runs stack.
1279 * This is enough to sort arrays of size up to about
1280 * 32 * phi ** MAX_MERGE_PENDING
1281 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1282 * with 2**64 elements.
1283 */
1284#define MAX_MERGE_PENDING 85
1285
Tim Peterse05f65a2002-08-10 05:21:15 +00001286/* When we get into galloping mode, we stay there until both runs win less
1287 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
Tim Petersa64dc242002-08-01 02:13:36 +00001288 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001289#define MIN_GALLOP 7
Tim Petersa64dc242002-08-01 02:13:36 +00001290
1291/* Avoid malloc for small temp arrays. */
1292#define MERGESTATE_TEMP_SIZE 256
1293
1294/* One MergeState exists on the stack per invocation of mergesort. It's just
1295 * a convenient way to pass state around among the helper functions.
1296 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001297struct s_slice {
1298 PyObject **base;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001299 Py_ssize_t len;
Tim Peterse05f65a2002-08-10 05:21:15 +00001300};
1301
Tim Petersa64dc242002-08-01 02:13:36 +00001302typedef struct s_MergeState {
1303 /* The user-supplied comparison function. or NULL if none given. */
1304 PyObject *compare;
1305
Tim Peterse05f65a2002-08-10 05:21:15 +00001306 /* This controls when we get *into* galloping mode. It's initialized
1307 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1308 * random data, and lower for highly structured data.
1309 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001310 Py_ssize_t min_gallop;
Tim Peterse05f65a2002-08-10 05:21:15 +00001311
Tim Petersa64dc242002-08-01 02:13:36 +00001312 /* 'a' is temp storage to help with merges. It contains room for
1313 * alloced entries.
1314 */
1315 PyObject **a; /* may point to temparray below */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001316 Py_ssize_t alloced;
Tim Petersa64dc242002-08-01 02:13:36 +00001317
1318 /* A stack of n pending runs yet to be merged. Run #i starts at
1319 * address base[i] and extends for len[i] elements. It's always
1320 * true (so long as the indices are in bounds) that
1321 *
Tim Peterse05f65a2002-08-10 05:21:15 +00001322 * pending[i].base + pending[i].len == pending[i+1].base
Tim Petersa64dc242002-08-01 02:13:36 +00001323 *
1324 * so we could cut the storage for this, but it's a minor amount,
1325 * and keeping all the info explicit simplifies the code.
1326 */
1327 int n;
Tim Peterse05f65a2002-08-10 05:21:15 +00001328 struct s_slice pending[MAX_MERGE_PENDING];
Tim Petersa64dc242002-08-01 02:13:36 +00001329
1330 /* 'a' points to this when possible, rather than muck with malloc. */
1331 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1332} MergeState;
1333
1334/* Conceptually a MergeState's constructor. */
1335static void
1336merge_init(MergeState *ms, PyObject *compare)
1337{
1338 assert(ms != NULL);
1339 ms->compare = compare;
1340 ms->a = ms->temparray;
1341 ms->alloced = MERGESTATE_TEMP_SIZE;
1342 ms->n = 0;
Tim Peterse05f65a2002-08-10 05:21:15 +00001343 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001344}
1345
1346/* Free all the temp memory owned by the MergeState. This must be called
1347 * when you're done with a MergeState, and may be called before then if
1348 * you want to free the temp memory early.
1349 */
1350static void
1351merge_freemem(MergeState *ms)
1352{
1353 assert(ms != NULL);
1354 if (ms->a != ms->temparray)
1355 PyMem_Free(ms->a);
1356 ms->a = ms->temparray;
1357 ms->alloced = MERGESTATE_TEMP_SIZE;
1358}
1359
1360/* Ensure enough temp memory for 'need' array slots is available.
1361 * Returns 0 on success and -1 if the memory can't be gotten.
1362 */
1363static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001364merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001365{
1366 assert(ms != NULL);
1367 if (need <= ms->alloced)
1368 return 0;
1369 /* Don't realloc! That can cost cycles to copy the old data, but
1370 * we don't care what's in the block.
1371 */
1372 merge_freemem(ms);
1373 ms->a = (PyObject **)PyMem_Malloc(need * sizeof(PyObject*));
1374 if (ms->a) {
1375 ms->alloced = need;
1376 return 0;
1377 }
1378 PyErr_NoMemory();
1379 merge_freemem(ms); /* reset to sane state */
1380 return -1;
1381}
1382#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1383 merge_getmem(MS, NEED))
1384
1385/* Merge the na elements starting at pa with the nb elements starting at pb
1386 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
1387 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
1388 * merge, and should have na <= nb. See listsort.txt for more info.
1389 * Return 0 if successful, -1 if error.
1390 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001391static Py_ssize_t
1392merge_lo(MergeState *ms, PyObject **pa, Py_ssize_t na,
1393 PyObject **pb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001394{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001395 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001396 PyObject *compare;
1397 PyObject **dest;
1398 int result = -1; /* guilty until proved innocent */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001399 Py_ssize_t min_gallop = ms->min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001400
1401 assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
1402 if (MERGE_GETMEM(ms, na) < 0)
1403 return -1;
1404 memcpy(ms->a, pa, na * sizeof(PyObject*));
1405 dest = pa;
1406 pa = ms->a;
1407
1408 *dest++ = *pb++;
1409 --nb;
1410 if (nb == 0)
1411 goto Succeed;
1412 if (na == 1)
1413 goto CopyB;
1414
1415 compare = ms->compare;
1416 for (;;) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001417 Py_ssize_t acount = 0; /* # of times A won in a row */
1418 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001419
1420 /* Do the straightforward thing until (if ever) one run
1421 * appears to win consistently.
1422 */
1423 for (;;) {
Tim Peterse05f65a2002-08-10 05:21:15 +00001424 assert(na > 1 && nb > 0);
Tim Peters66860f62002-08-04 17:47:26 +00001425 k = ISLT(*pb, *pa, compare);
Tim Petersa64dc242002-08-01 02:13:36 +00001426 if (k) {
1427 if (k < 0)
1428 goto Fail;
1429 *dest++ = *pb++;
1430 ++bcount;
1431 acount = 0;
1432 --nb;
1433 if (nb == 0)
1434 goto Succeed;
Tim Peterse05f65a2002-08-10 05:21:15 +00001435 if (bcount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001436 break;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001437 }
1438 else {
Tim Petersa64dc242002-08-01 02:13:36 +00001439 *dest++ = *pa++;
1440 ++acount;
1441 bcount = 0;
1442 --na;
1443 if (na == 1)
1444 goto CopyB;
Tim Peterse05f65a2002-08-10 05:21:15 +00001445 if (acount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001446 break;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001447 }
Tim Petersa64dc242002-08-01 02:13:36 +00001448 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001449
Tim Petersa64dc242002-08-01 02:13:36 +00001450 /* One run is winning so consistently that galloping may
1451 * be a huge win. So try that, and continue galloping until
1452 * (if ever) neither run appears to be winning consistently
1453 * anymore.
1454 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001455 ++min_gallop;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001456 do {
Tim Peterse05f65a2002-08-10 05:21:15 +00001457 assert(na > 1 && nb > 0);
1458 min_gallop -= min_gallop > 1;
1459 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001460 k = gallop_right(*pb, pa, na, 0, compare);
1461 acount = k;
1462 if (k) {
1463 if (k < 0)
1464 goto Fail;
1465 memcpy(dest, pa, k * sizeof(PyObject *));
1466 dest += k;
1467 pa += k;
1468 na -= k;
1469 if (na == 1)
1470 goto CopyB;
1471 /* na==0 is impossible now if the comparison
1472 * function is consistent, but we can't assume
1473 * that it is.
1474 */
1475 if (na == 0)
1476 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001477 }
Tim Petersa64dc242002-08-01 02:13:36 +00001478 *dest++ = *pb++;
1479 --nb;
1480 if (nb == 0)
1481 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001482
Tim Petersa64dc242002-08-01 02:13:36 +00001483 k = gallop_left(*pa, pb, nb, 0, compare);
1484 bcount = k;
1485 if (k) {
1486 if (k < 0)
1487 goto Fail;
1488 memmove(dest, pb, k * sizeof(PyObject *));
1489 dest += k;
1490 pb += k;
1491 nb -= k;
1492 if (nb == 0)
1493 goto Succeed;
1494 }
1495 *dest++ = *pa++;
1496 --na;
1497 if (na == 1)
1498 goto CopyB;
1499 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
Tim Peterse05f65a2002-08-10 05:21:15 +00001500 ++min_gallop; /* penalize it for leaving galloping mode */
1501 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001502 }
1503Succeed:
1504 result = 0;
1505Fail:
1506 if (na)
1507 memcpy(dest, pa, na * sizeof(PyObject*));
1508 return result;
1509CopyB:
1510 assert(na == 1 && nb > 0);
1511 /* The last element of pa belongs at the end of the merge. */
1512 memmove(dest, pb, nb * sizeof(PyObject *));
1513 dest[nb] = *pa;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001514 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001515}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001516
Tim Petersa64dc242002-08-01 02:13:36 +00001517/* Merge the na elements starting at pa with the nb elements starting at pb
1518 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
1519 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
1520 * merge, and should have na >= nb. See listsort.txt for more info.
1521 * Return 0 if successful, -1 if error.
1522 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001523static Py_ssize_t
1524merge_hi(MergeState *ms, PyObject **pa, Py_ssize_t na, PyObject **pb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001525{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001526 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001527 PyObject *compare;
1528 PyObject **dest;
1529 int result = -1; /* guilty until proved innocent */
1530 PyObject **basea;
1531 PyObject **baseb;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001532 Py_ssize_t min_gallop = ms->min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001533
1534 assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
1535 if (MERGE_GETMEM(ms, nb) < 0)
1536 return -1;
1537 dest = pb + nb - 1;
1538 memcpy(ms->a, pb, nb * sizeof(PyObject*));
1539 basea = pa;
1540 baseb = ms->a;
1541 pb = ms->a + nb - 1;
1542 pa += na - 1;
1543
1544 *dest-- = *pa--;
1545 --na;
1546 if (na == 0)
1547 goto Succeed;
1548 if (nb == 1)
1549 goto CopyA;
1550
1551 compare = ms->compare;
1552 for (;;) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001553 Py_ssize_t acount = 0; /* # of times A won in a row */
1554 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001555
1556 /* Do the straightforward thing until (if ever) one run
1557 * appears to win consistently.
1558 */
1559 for (;;) {
Tim Peterse05f65a2002-08-10 05:21:15 +00001560 assert(na > 0 && nb > 1);
Tim Peters66860f62002-08-04 17:47:26 +00001561 k = ISLT(*pb, *pa, compare);
Tim Petersa64dc242002-08-01 02:13:36 +00001562 if (k) {
1563 if (k < 0)
1564 goto Fail;
1565 *dest-- = *pa--;
1566 ++acount;
1567 bcount = 0;
1568 --na;
1569 if (na == 0)
1570 goto Succeed;
Tim Peterse05f65a2002-08-10 05:21:15 +00001571 if (acount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001572 break;
1573 }
1574 else {
1575 *dest-- = *pb--;
1576 ++bcount;
1577 acount = 0;
1578 --nb;
1579 if (nb == 1)
1580 goto CopyA;
Tim Peterse05f65a2002-08-10 05:21:15 +00001581 if (bcount >= min_gallop)
Tim Petersa64dc242002-08-01 02:13:36 +00001582 break;
1583 }
1584 }
1585
1586 /* One run is winning so consistently that galloping may
1587 * be a huge win. So try that, and continue galloping until
1588 * (if ever) neither run appears to be winning consistently
1589 * anymore.
1590 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001591 ++min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001592 do {
Tim Peterse05f65a2002-08-10 05:21:15 +00001593 assert(na > 0 && nb > 1);
1594 min_gallop -= min_gallop > 1;
1595 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001596 k = gallop_right(*pb, basea, na, na-1, compare);
1597 if (k < 0)
1598 goto Fail;
1599 k = na - k;
1600 acount = k;
1601 if (k) {
1602 dest -= k;
1603 pa -= k;
1604 memmove(dest+1, pa+1, k * sizeof(PyObject *));
1605 na -= k;
1606 if (na == 0)
1607 goto Succeed;
1608 }
1609 *dest-- = *pb--;
1610 --nb;
1611 if (nb == 1)
1612 goto CopyA;
1613
1614 k = gallop_left(*pa, baseb, nb, nb-1, compare);
1615 if (k < 0)
1616 goto Fail;
1617 k = nb - k;
1618 bcount = k;
1619 if (k) {
1620 dest -= k;
1621 pb -= k;
1622 memcpy(dest+1, pb+1, k * sizeof(PyObject *));
1623 nb -= k;
1624 if (nb == 1)
1625 goto CopyA;
1626 /* nb==0 is impossible now if the comparison
1627 * function is consistent, but we can't assume
1628 * that it is.
1629 */
1630 if (nb == 0)
1631 goto Succeed;
1632 }
1633 *dest-- = *pa--;
1634 --na;
1635 if (na == 0)
1636 goto Succeed;
1637 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
Tim Peterse05f65a2002-08-10 05:21:15 +00001638 ++min_gallop; /* penalize it for leaving galloping mode */
1639 ms->min_gallop = min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001640 }
1641Succeed:
1642 result = 0;
1643Fail:
1644 if (nb)
1645 memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*));
1646 return result;
1647CopyA:
1648 assert(nb == 1 && na > 0);
1649 /* The first element of pb belongs at the front of the merge. */
1650 dest -= na;
1651 pa -= na;
1652 memmove(dest+1, pa+1, na * sizeof(PyObject *));
1653 *dest = *pb;
1654 return 0;
1655}
1656
1657/* Merge the two runs at stack indices i and i+1.
1658 * Returns 0 on success, -1 on error.
1659 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001660static Py_ssize_t
1661merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001662{
1663 PyObject **pa, **pb;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001664 Py_ssize_t na, nb;
1665 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001666 PyObject *compare;
1667
1668 assert(ms != NULL);
1669 assert(ms->n >= 2);
1670 assert(i >= 0);
1671 assert(i == ms->n - 2 || i == ms->n - 3);
1672
Tim Peterse05f65a2002-08-10 05:21:15 +00001673 pa = ms->pending[i].base;
1674 na = ms->pending[i].len;
1675 pb = ms->pending[i+1].base;
1676 nb = ms->pending[i+1].len;
Tim Petersa64dc242002-08-01 02:13:36 +00001677 assert(na > 0 && nb > 0);
1678 assert(pa + na == pb);
1679
1680 /* Record the length of the combined runs; if i is the 3rd-last
1681 * run now, also slide over the last run (which isn't involved
1682 * in this merge). The current run i+1 goes away in any case.
1683 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001684 ms->pending[i].len = na + nb;
1685 if (i == ms->n - 3)
1686 ms->pending[i+1] = ms->pending[i+2];
Tim Petersa64dc242002-08-01 02:13:36 +00001687 --ms->n;
1688
1689 /* Where does b start in a? Elements in a before that can be
1690 * ignored (already in place).
1691 */
1692 compare = ms->compare;
1693 k = gallop_right(*pb, pa, na, 0, compare);
1694 if (k < 0)
1695 return -1;
1696 pa += k;
1697 na -= k;
1698 if (na == 0)
1699 return 0;
1700
1701 /* Where does a end in b? Elements in b after that can be
1702 * ignored (already in place).
1703 */
1704 nb = gallop_left(pa[na-1], pb, nb, nb-1, compare);
1705 if (nb <= 0)
1706 return nb;
1707
1708 /* Merge what remains of the runs, using a temp array with
1709 * min(na, nb) elements.
1710 */
1711 if (na <= nb)
1712 return merge_lo(ms, pa, na, pb, nb);
1713 else
1714 return merge_hi(ms, pa, na, pb, nb);
1715}
1716
1717/* Examine the stack of runs waiting to be merged, merging adjacent runs
1718 * until the stack invariants are re-established:
1719 *
1720 * 1. len[-3] > len[-2] + len[-1]
1721 * 2. len[-2] > len[-1]
1722 *
1723 * See listsort.txt for more info.
1724 *
1725 * Returns 0 on success, -1 on error.
1726 */
1727static int
1728merge_collapse(MergeState *ms)
1729{
Tim Peterse05f65a2002-08-10 05:21:15 +00001730 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001731
1732 assert(ms);
1733 while (ms->n > 1) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001734 Py_ssize_t n = ms->n - 2;
Tim Peterse05f65a2002-08-10 05:21:15 +00001735 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
1736 if (p[n-1].len < p[n+1].len)
Tim Petersa64dc242002-08-01 02:13:36 +00001737 --n;
1738 if (merge_at(ms, n) < 0)
1739 return -1;
1740 }
Tim Peterse05f65a2002-08-10 05:21:15 +00001741 else if (p[n].len <= p[n+1].len) {
Tim Petersa64dc242002-08-01 02:13:36 +00001742 if (merge_at(ms, n) < 0)
1743 return -1;
1744 }
1745 else
1746 break;
1747 }
1748 return 0;
1749}
1750
1751/* Regardless of invariants, merge all runs on the stack until only one
1752 * remains. This is used at the end of the mergesort.
1753 *
1754 * Returns 0 on success, -1 on error.
1755 */
1756static int
1757merge_force_collapse(MergeState *ms)
1758{
Tim Peterse05f65a2002-08-10 05:21:15 +00001759 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001760
1761 assert(ms);
1762 while (ms->n > 1) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001763 Py_ssize_t n = ms->n - 2;
Tim Peterse05f65a2002-08-10 05:21:15 +00001764 if (n > 0 && p[n-1].len < p[n+1].len)
Tim Petersa64dc242002-08-01 02:13:36 +00001765 --n;
1766 if (merge_at(ms, n) < 0)
1767 return -1;
1768 }
1769 return 0;
1770}
1771
1772/* Compute a good value for the minimum run length; natural runs shorter
1773 * than this are boosted artificially via binary insertion.
1774 *
1775 * If n < 64, return n (it's too small to bother with fancy stuff).
1776 * Else if n is an exact power of 2, return 32.
1777 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1778 * strictly less than, an exact power of 2.
1779 *
1780 * See listsort.txt for more info.
1781 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001782static Py_ssize_t
1783merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001784{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001785 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001786
1787 assert(n >= 0);
1788 while (n >= 64) {
1789 r |= n & 1;
1790 n >>= 1;
1791 }
1792 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001793}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001794
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001795/* Special wrapper to support stable sorting using the decorate-sort-undecorate
Andrew M. Kuchling55be9ea2004-09-10 12:59:54 +00001796 pattern. Holds a key which is used for comparisons and the original record
Tim Petersb38e2b62004-07-29 02:29:26 +00001797 which is returned during the undecorate phase. By exposing only the key
1798 during comparisons, the underlying sort stability characteristics are left
1799 unchanged. Also, if a custom comparison function is used, it will only see
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001800 the key instead of a full record. */
1801
1802typedef struct {
1803 PyObject_HEAD
1804 PyObject *key;
1805 PyObject *value;
1806} sortwrapperobject;
1807
1808static PyTypeObject sortwrapper_type;
1809
1810static PyObject *
1811sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, int op)
1812{
1813 if (!PyObject_TypeCheck(b, &sortwrapper_type)) {
Tim Petersb38e2b62004-07-29 02:29:26 +00001814 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001815 "expected a sortwrapperobject");
1816 return NULL;
1817 }
1818 return PyObject_RichCompare(a->key, b->key, op);
1819}
1820
1821static void
1822sortwrapper_dealloc(sortwrapperobject *so)
1823{
1824 Py_XDECREF(so->key);
1825 Py_XDECREF(so->value);
1826 PyObject_Del(so);
1827}
1828
1829PyDoc_STRVAR(sortwrapper_doc, "Object wrapper with a custom sort key.");
1830
1831static PyTypeObject sortwrapper_type = {
1832 PyObject_HEAD_INIT(&PyType_Type)
1833 0, /* ob_size */
1834 "sortwrapper", /* tp_name */
1835 sizeof(sortwrapperobject), /* tp_basicsize */
1836 0, /* tp_itemsize */
1837 /* methods */
1838 (destructor)sortwrapper_dealloc, /* tp_dealloc */
1839 0, /* tp_print */
1840 0, /* tp_getattr */
1841 0, /* tp_setattr */
1842 0, /* tp_compare */
1843 0, /* tp_repr */
1844 0, /* tp_as_number */
1845 0, /* tp_as_sequence */
1846 0, /* tp_as_mapping */
1847 0, /* tp_hash */
1848 0, /* tp_call */
1849 0, /* tp_str */
1850 PyObject_GenericGetAttr, /* tp_getattro */
1851 0, /* tp_setattro */
1852 0, /* tp_as_buffer */
Tim Petersb38e2b62004-07-29 02:29:26 +00001853 Py_TPFLAGS_DEFAULT |
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001854 Py_TPFLAGS_HAVE_RICHCOMPARE, /* tp_flags */
1855 sortwrapper_doc, /* tp_doc */
1856 0, /* tp_traverse */
1857 0, /* tp_clear */
1858 (richcmpfunc)sortwrapper_richcompare, /* tp_richcompare */
1859};
1860
1861/* Returns a new reference to a sortwrapper.
1862 Consumes the references to the two underlying objects. */
1863
1864static PyObject *
1865build_sortwrapper(PyObject *key, PyObject *value)
1866{
1867 sortwrapperobject *so;
Tim Petersb38e2b62004-07-29 02:29:26 +00001868
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001869 so = PyObject_New(sortwrapperobject, &sortwrapper_type);
1870 if (so == NULL)
1871 return NULL;
1872 so->key = key;
1873 so->value = value;
1874 return (PyObject *)so;
1875}
1876
1877/* Returns a new reference to the value underlying the wrapper. */
1878static PyObject *
1879sortwrapper_getvalue(PyObject *so)
1880{
1881 PyObject *value;
1882
1883 if (!PyObject_TypeCheck(so, &sortwrapper_type)) {
Tim Petersb38e2b62004-07-29 02:29:26 +00001884 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001885 "expected a sortwrapperobject");
1886 return NULL;
1887 }
1888 value = ((sortwrapperobject *)so)->value;
1889 Py_INCREF(value);
1890 return value;
1891}
1892
1893/* Wrapper for user specified cmp functions in combination with a
1894 specified key function. Makes sure the cmp function is presented
1895 with the actual key instead of the sortwrapper */
1896
1897typedef struct {
1898 PyObject_HEAD
1899 PyObject *func;
1900} cmpwrapperobject;
1901
1902static void
1903cmpwrapper_dealloc(cmpwrapperobject *co)
1904{
1905 Py_XDECREF(co->func);
1906 PyObject_Del(co);
1907}
1908
1909static PyObject *
1910cmpwrapper_call(cmpwrapperobject *co, PyObject *args, PyObject *kwds)
1911{
1912 PyObject *x, *y, *xx, *yy;
1913
1914 if (!PyArg_UnpackTuple(args, "", 2, 2, &x, &y))
1915 return NULL;
1916 if (!PyObject_TypeCheck(x, &sortwrapper_type) ||
Raymond Hettingerae4a2992003-10-16 17:16:30 +00001917 !PyObject_TypeCheck(y, &sortwrapper_type)) {
Tim Petersb38e2b62004-07-29 02:29:26 +00001918 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001919 "expected a sortwrapperobject");
1920 return NULL;
1921 }
1922 xx = ((sortwrapperobject *)x)->key;
1923 yy = ((sortwrapperobject *)y)->key;
1924 return PyObject_CallFunctionObjArgs(co->func, xx, yy, NULL);
1925}
1926
1927PyDoc_STRVAR(cmpwrapper_doc, "cmp() wrapper for sort with custom keys.");
1928
1929static PyTypeObject cmpwrapper_type = {
1930 PyObject_HEAD_INIT(&PyType_Type)
1931 0, /* ob_size */
1932 "cmpwrapper", /* tp_name */
1933 sizeof(cmpwrapperobject), /* tp_basicsize */
1934 0, /* tp_itemsize */
1935 /* methods */
1936 (destructor)cmpwrapper_dealloc, /* tp_dealloc */
1937 0, /* tp_print */
1938 0, /* tp_getattr */
1939 0, /* tp_setattr */
1940 0, /* tp_compare */
1941 0, /* tp_repr */
1942 0, /* tp_as_number */
1943 0, /* tp_as_sequence */
1944 0, /* tp_as_mapping */
1945 0, /* tp_hash */
1946 (ternaryfunc)cmpwrapper_call, /* tp_call */
1947 0, /* tp_str */
1948 PyObject_GenericGetAttr, /* tp_getattro */
1949 0, /* tp_setattro */
1950 0, /* tp_as_buffer */
1951 Py_TPFLAGS_DEFAULT, /* tp_flags */
1952 cmpwrapper_doc, /* tp_doc */
1953};
1954
1955static PyObject *
1956build_cmpwrapper(PyObject *cmpfunc)
1957{
1958 cmpwrapperobject *co;
Tim Petersb38e2b62004-07-29 02:29:26 +00001959
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001960 co = PyObject_New(cmpwrapperobject, &cmpwrapper_type);
1961 if (co == NULL)
1962 return NULL;
1963 Py_INCREF(cmpfunc);
1964 co->func = cmpfunc;
1965 return (PyObject *)co;
1966}
1967
Tim Petersa64dc242002-08-01 02:13:36 +00001968/* An adaptive, stable, natural mergesort. See listsort.txt.
1969 * Returns Py_None on success, NULL on error. Even in case of error, the
1970 * list will be some permutation of its input state (nothing is lost or
1971 * duplicated).
1972 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001973static PyObject *
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001974listsort(PyListObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001975{
Tim Petersa64dc242002-08-01 02:13:36 +00001976 MergeState ms;
1977 PyObject **lo, **hi;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001978 Py_ssize_t nremaining;
1979 Py_ssize_t minrun;
1980 Py_ssize_t saved_ob_size, saved_allocated;
Tim Petersb9099c32002-11-12 22:08:10 +00001981 PyObject **saved_ob_item;
Armin Rigo93677f02004-07-29 12:40:23 +00001982 PyObject **final_ob_item;
Tim Petersa64dc242002-08-01 02:13:36 +00001983 PyObject *compare = NULL;
1984 PyObject *result = NULL; /* guilty until proved innocent */
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001985 int reverse = 0;
1986 PyObject *keyfunc = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001987 Py_ssize_t i;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001988 PyObject *key, *value, *kvpair;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001989 static const char *kwlist[] = {"cmp", "key", "reverse", 0};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001990
Tim Petersa64dc242002-08-01 02:13:36 +00001991 assert(self != NULL);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001992 assert (PyList_Check(self));
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001993 if (args != NULL) {
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001994 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi:sort",
1995 kwlist, &compare, &keyfunc, &reverse))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001996 return NULL;
1997 }
Skip Montanaro4abd5f02003-01-02 20:51:08 +00001998 if (compare == Py_None)
1999 compare = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002000 if (keyfunc == Py_None)
2001 keyfunc = NULL;
2002 if (compare != NULL && keyfunc != NULL) {
2003 compare = build_cmpwrapper(compare);
2004 if (compare == NULL)
Hye-Shik Chang19cb1932003-12-10 07:31:08 +00002005 return NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002006 } else
2007 Py_XINCREF(compare);
2008
Tim Petersb9099c32002-11-12 22:08:10 +00002009 /* The list is temporarily made empty, so that mutations performed
2010 * by comparison functions can't affect the slice of memory we're
2011 * sorting (allowing mutations during sorting is a core-dump
2012 * factory, since ob_item may change).
2013 */
2014 saved_ob_size = self->ob_size;
2015 saved_ob_item = self->ob_item;
Raymond Hettinger4bb95402004-02-13 11:36:39 +00002016 saved_allocated = self->allocated;
Armin Rigo93677f02004-07-29 12:40:23 +00002017 self->ob_size = 0;
Tim Peters51b4ade2004-07-29 04:07:15 +00002018 self->ob_item = NULL;
Armin Rigo93677f02004-07-29 12:40:23 +00002019 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002020
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002021 if (keyfunc != NULL) {
2022 for (i=0 ; i < saved_ob_size ; i++) {
2023 value = saved_ob_item[i];
Tim Petersb38e2b62004-07-29 02:29:26 +00002024 key = PyObject_CallFunctionObjArgs(keyfunc, value,
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002025 NULL);
2026 if (key == NULL) {
2027 for (i=i-1 ; i>=0 ; i--) {
2028 kvpair = saved_ob_item[i];
2029 value = sortwrapper_getvalue(kvpair);
2030 saved_ob_item[i] = value;
2031 Py_DECREF(kvpair);
2032 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002033 goto dsu_fail;
2034 }
2035 kvpair = build_sortwrapper(key, value);
2036 if (kvpair == NULL)
2037 goto dsu_fail;
2038 saved_ob_item[i] = kvpair;
2039 }
2040 }
2041
2042 /* Reverse sort stability achieved by initially reversing the list,
2043 applying a stable forward sort, then reversing the final result. */
2044 if (reverse && saved_ob_size > 1)
2045 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
2046
2047 merge_init(&ms, compare);
2048
Tim Petersb9099c32002-11-12 22:08:10 +00002049 nremaining = saved_ob_size;
Tim Petersa64dc242002-08-01 02:13:36 +00002050 if (nremaining < 2)
2051 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002052
Tim Petersa64dc242002-08-01 02:13:36 +00002053 /* March over the array once, left to right, finding natural runs,
2054 * and extending short natural runs to minrun elements.
2055 */
Tim Petersb9099c32002-11-12 22:08:10 +00002056 lo = saved_ob_item;
Tim Petersa64dc242002-08-01 02:13:36 +00002057 hi = lo + nremaining;
2058 minrun = merge_compute_minrun(nremaining);
2059 do {
2060 int descending;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002061 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002062
Tim Petersa64dc242002-08-01 02:13:36 +00002063 /* Identify next run. */
2064 n = count_run(lo, hi, compare, &descending);
2065 if (n < 0)
2066 goto fail;
2067 if (descending)
2068 reverse_slice(lo, lo + n);
2069 /* If short, extend to min(minrun, nremaining). */
2070 if (n < minrun) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002071 const Py_ssize_t force = nremaining <= minrun ?
Tim Petersa64dc242002-08-01 02:13:36 +00002072 nremaining : minrun;
2073 if (binarysort(lo, lo + force, lo + n, compare) < 0)
2074 goto fail;
2075 n = force;
2076 }
2077 /* Push run onto pending-runs stack, and maybe merge. */
2078 assert(ms.n < MAX_MERGE_PENDING);
Tim Peterse05f65a2002-08-10 05:21:15 +00002079 ms.pending[ms.n].base = lo;
2080 ms.pending[ms.n].len = n;
Tim Petersa64dc242002-08-01 02:13:36 +00002081 ++ms.n;
2082 if (merge_collapse(&ms) < 0)
2083 goto fail;
2084 /* Advance to find next run. */
2085 lo += n;
2086 nremaining -= n;
2087 } while (nremaining);
2088 assert(lo == hi);
Tim Peters330f9e92002-07-19 07:05:44 +00002089
Tim Petersa64dc242002-08-01 02:13:36 +00002090 if (merge_force_collapse(&ms) < 0)
2091 goto fail;
2092 assert(ms.n == 1);
Tim Petersb9099c32002-11-12 22:08:10 +00002093 assert(ms.pending[0].base == saved_ob_item);
2094 assert(ms.pending[0].len == saved_ob_size);
Tim Petersa64dc242002-08-01 02:13:36 +00002095
2096succeed:
2097 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002098fail:
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002099 if (keyfunc != NULL) {
2100 for (i=0 ; i < saved_ob_size ; i++) {
2101 kvpair = saved_ob_item[i];
2102 value = sortwrapper_getvalue(kvpair);
2103 saved_ob_item[i] = value;
2104 Py_DECREF(kvpair);
2105 }
2106 }
2107
Armin Rigo93677f02004-07-29 12:40:23 +00002108 if (self->allocated != -1 && result != NULL) {
Tim Peters51b4ade2004-07-29 04:07:15 +00002109 /* The user mucked with the list during the sort,
2110 * and we don't already have another error to report.
2111 */
2112 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2113 result = NULL;
Tim Petersb9099c32002-11-12 22:08:10 +00002114 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002115
2116 if (reverse && saved_ob_size > 1)
2117 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
2118
2119 merge_freemem(&ms);
2120
2121dsu_fail:
Armin Rigo93677f02004-07-29 12:40:23 +00002122 final_ob_item = self->ob_item;
2123 i = self->ob_size;
Tim Petersb9099c32002-11-12 22:08:10 +00002124 self->ob_size = saved_ob_size;
2125 self->ob_item = saved_ob_item;
Raymond Hettinger4bb95402004-02-13 11:36:39 +00002126 self->allocated = saved_allocated;
Armin Rigo93677f02004-07-29 12:40:23 +00002127 if (final_ob_item != NULL) {
2128 /* we cannot use list_clear() for this because it does not
2129 guarantee that the list is really empty when it returns */
2130 while (--i >= 0) {
2131 Py_XDECREF(final_ob_item[i]);
2132 }
2133 PyMem_FREE(final_ob_item);
2134 }
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002135 Py_XDECREF(compare);
Tim Petersa64dc242002-08-01 02:13:36 +00002136 Py_XINCREF(result);
2137 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002138}
Tim Peters330f9e92002-07-19 07:05:44 +00002139#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002140#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002141
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002142int
Fred Drakea2f55112000-07-09 15:16:51 +00002143PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002144{
2145 if (v == NULL || !PyList_Check(v)) {
2146 PyErr_BadInternalCall();
2147 return -1;
2148 }
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002149 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002150 if (v == NULL)
2151 return -1;
2152 Py_DECREF(v);
2153 return 0;
2154}
2155
Guido van Rossumb86c5492001-02-12 22:06:02 +00002156static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002157listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002158{
Tim Peters326b4482002-07-19 04:04:16 +00002159 if (self->ob_size > 1)
2160 reverse_slice(self->ob_item, self->ob_item + self->ob_size);
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +00002161 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002162}
2163
Guido van Rossum84c76f51990-10-30 13:32:20 +00002164int
Fred Drakea2f55112000-07-09 15:16:51 +00002165PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002166{
Tim Peters6063e262002-08-08 01:06:39 +00002167 PyListObject *self = (PyListObject *)v;
2168
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002169 if (v == NULL || !PyList_Check(v)) {
2170 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002171 return -1;
2172 }
Tim Peters6063e262002-08-08 01:06:39 +00002173 if (self->ob_size > 1)
2174 reverse_slice(self->ob_item, self->ob_item + self->ob_size);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002175 return 0;
2176}
2177
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002178PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002179PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002180{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002181 PyObject *w;
2182 PyObject **p;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002183 Py_ssize_t n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002184 if (v == NULL || !PyList_Check(v)) {
2185 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002186 return NULL;
2187 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002188 n = ((PyListObject *)v)->ob_size;
2189 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002190 if (w == NULL)
2191 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002192 p = ((PyTupleObject *)w)->ob_item;
Thomas Wouters334fb892000-07-25 12:56:38 +00002193 memcpy((void *)p,
2194 (void *)((PyListObject *)v)->ob_item,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002195 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002196 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002197 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002198 p++;
2199 }
2200 return w;
2201}
2202
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002203static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002204listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002205{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002206 Py_ssize_t i, start=0, stop=self->ob_size;
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002207 PyObject *v;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002208
Walter Dörwalde8049bef2003-06-17 19:27:39 +00002209 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2210 _PyEval_SliceIndex, &start,
2211 _PyEval_SliceIndex, &stop))
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002212 return NULL;
Guido van Rossum2743d872003-06-17 14:25:14 +00002213 if (start < 0) {
2214 start += self->ob_size;
2215 if (start < 0)
2216 start = 0;
2217 }
2218 if (stop < 0) {
2219 stop += self->ob_size;
2220 if (stop < 0)
2221 stop = 0;
2222 }
Neal Norwitzf0769532004-08-13 03:18:29 +00002223 for (i = start; i < stop && i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002224 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2225 if (cmp > 0)
Martin v. Löwis18e16552006-02-15 17:27:45 +00002226 return PyInt_FromSsize_t(i);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002227 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002228 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002229 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002230 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00002231 return NULL;
2232}
2233
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002234static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002235listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002236{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002237 Py_ssize_t count = 0;
2238 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002239
Guido van Rossume6f7d181991-10-20 20:20:40 +00002240 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002241 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2242 if (cmp > 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002243 count++;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002244 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002245 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00002246 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002247 return PyInt_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002248}
2249
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002250static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002251listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002252{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002253 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002254
Guido van Rossumed98d481991-03-06 13:07:53 +00002255 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002256 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2257 if (cmp > 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002258 if (list_ass_slice(self, i, i+1,
Raymond Hettinger45d0b5c2004-04-12 17:21:03 +00002259 (PyObject *)NULL) == 0)
2260 Py_RETURN_NONE;
2261 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002262 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002263 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002264 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002265 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002266 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00002267 return NULL;
2268}
2269
Jeremy Hylton8caad492000-06-23 14:18:11 +00002270static int
2271list_traverse(PyListObject *o, visitproc visit, void *arg)
2272{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002273 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002274 PyObject *x;
2275
2276 for (i = o->ob_size; --i >= 0; ) {
2277 x = o->ob_item[i];
2278 if (x != NULL) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002279 int err = visit(x, arg);
Jeremy Hylton8caad492000-06-23 14:18:11 +00002280 if (err)
2281 return err;
2282 }
2283 }
2284 return 0;
2285}
2286
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002287static PyObject *
2288list_richcompare(PyObject *v, PyObject *w, int op)
2289{
2290 PyListObject *vl, *wl;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002291 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002292
2293 if (!PyList_Check(v) || !PyList_Check(w)) {
2294 Py_INCREF(Py_NotImplemented);
2295 return Py_NotImplemented;
2296 }
2297
2298 vl = (PyListObject *)v;
2299 wl = (PyListObject *)w;
2300
2301 if (vl->ob_size != wl->ob_size && (op == Py_EQ || op == Py_NE)) {
2302 /* Shortcut: if the lengths differ, the lists differ */
2303 PyObject *res;
2304 if (op == Py_EQ)
2305 res = Py_False;
2306 else
2307 res = Py_True;
2308 Py_INCREF(res);
2309 return res;
2310 }
2311
2312 /* Search for the first index where items are different */
2313 for (i = 0; i < vl->ob_size && i < wl->ob_size; i++) {
2314 int k = PyObject_RichCompareBool(vl->ob_item[i],
2315 wl->ob_item[i], Py_EQ);
2316 if (k < 0)
2317 return NULL;
2318 if (!k)
2319 break;
2320 }
2321
2322 if (i >= vl->ob_size || i >= wl->ob_size) {
2323 /* No more items to compare -- compare sizes */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002324 Py_ssize_t vs = vl->ob_size;
2325 Py_ssize_t ws = wl->ob_size;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002326 int cmp;
2327 PyObject *res;
2328 switch (op) {
2329 case Py_LT: cmp = vs < ws; break;
Tim Peters6ee42342001-07-06 17:45:43 +00002330 case Py_LE: cmp = vs <= ws; break;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002331 case Py_EQ: cmp = vs == ws; break;
2332 case Py_NE: cmp = vs != ws; break;
2333 case Py_GT: cmp = vs > ws; break;
2334 case Py_GE: cmp = vs >= ws; break;
2335 default: return NULL; /* cannot happen */
2336 }
2337 if (cmp)
2338 res = Py_True;
2339 else
2340 res = Py_False;
2341 Py_INCREF(res);
2342 return res;
2343 }
2344
2345 /* We have an item that differs -- shortcuts for EQ/NE */
2346 if (op == Py_EQ) {
2347 Py_INCREF(Py_False);
2348 return Py_False;
2349 }
2350 if (op == Py_NE) {
2351 Py_INCREF(Py_True);
2352 return Py_True;
2353 }
2354
2355 /* Compare the final item again using the proper operator */
2356 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
2357}
2358
Tim Peters6d6c1a32001-08-02 04:15:00 +00002359static int
2360list_init(PyListObject *self, PyObject *args, PyObject *kw)
2361{
2362 PyObject *arg = NULL;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002363 static const char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002364
2365 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2366 return -1;
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002367
2368 /* Verify list invariants established by PyType_GenericAlloc() */
Armin Rigoa37bbf22004-07-30 11:20:18 +00002369 assert(0 <= self->ob_size);
2370 assert(self->ob_size <= self->allocated || self->allocated == -1);
2371 assert(self->ob_item != NULL ||
2372 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002373
Raymond Hettinger90a39bf2004-02-15 03:57:00 +00002374 /* Empty previous contents */
Armin Rigo93677f02004-07-29 12:40:23 +00002375 if (self->ob_item != NULL) {
2376 (void)list_clear(self);
Raymond Hettinger90a39bf2004-02-15 03:57:00 +00002377 }
2378 if (arg != NULL) {
2379 PyObject *rv = listextend(self, arg);
2380 if (rv == NULL)
2381 return -1;
2382 Py_DECREF(rv);
2383 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002384 return 0;
2385}
2386
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002387static long
2388list_nohash(PyObject *self)
2389{
2390 PyErr_SetString(PyExc_TypeError, "list objects are unhashable");
2391 return -1;
2392}
2393
Raymond Hettinger1021c442003-11-07 15:38:09 +00002394static PyObject *list_iter(PyObject *seq);
2395static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2396
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002397PyDoc_STRVAR(getitem_doc,
2398"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002399PyDoc_STRVAR(reversed_doc,
2400"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002401PyDoc_STRVAR(append_doc,
2402"L.append(object) -- append object to end");
2403PyDoc_STRVAR(extend_doc,
Raymond Hettingerf8bcfb12002-12-29 05:49:09 +00002404"L.extend(iterable) -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002405PyDoc_STRVAR(insert_doc,
2406"L.insert(index, object) -- insert object before index");
2407PyDoc_STRVAR(pop_doc,
2408"L.pop([index]) -> item -- remove and return item at index (default last)");
2409PyDoc_STRVAR(remove_doc,
2410"L.remove(value) -- remove first occurrence of value");
2411PyDoc_STRVAR(index_doc,
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002412"L.index(value, [start, [stop]]) -> integer -- return first index of value");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002413PyDoc_STRVAR(count_doc,
2414"L.count(value) -> integer -- return number of occurrences of value");
2415PyDoc_STRVAR(reverse_doc,
2416"L.reverse() -- reverse *IN PLACE*");
2417PyDoc_STRVAR(sort_doc,
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002418"L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\n\
2419cmp(x, y) -> -1, 0, 1");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002420
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002421static PyObject *list_subscript(PyListObject*, PyObject*);
2422
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002423static PyMethodDef list_methods[] = {
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002424 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
Raymond Hettinger1021c442003-11-07 15:38:09 +00002425 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002426 {"append", (PyCFunction)listappend, METH_O, append_doc},
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002427 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002428 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002429 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002430 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002431 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002432 {"count", (PyCFunction)listcount, METH_O, count_doc},
2433 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002434 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
Tim Petersa64dc242002-08-01 02:13:36 +00002435 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002436};
2437
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002438static PySequenceMethods list_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002439 (lenfunc)list_length, /* sq_length */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002440 (binaryfunc)list_concat, /* sq_concat */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002441 (ssizeargfunc)list_repeat, /* sq_repeat */
2442 (ssizeargfunc)list_item, /* sq_item */
2443 (ssizessizeargfunc)list_slice, /* sq_slice */
2444 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2445 (ssizessizeobjargproc)list_ass_slice, /* sq_ass_slice */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002446 (objobjproc)list_contains, /* sq_contains */
2447 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002448 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002449};
2450
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002451PyDoc_STRVAR(list_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002452"list() -> new list\n"
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002453"list(sequence) -> new list initialized from sequence's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002454
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002455static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002456list_subscript(PyListObject* self, PyObject* item)
2457{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002458 if (PyInt_Check(item) || PyLong_Check(item)) {
2459 Py_ssize_t i = PyInt_AsSsize_t(item);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002460 if (i == -1 && PyErr_Occurred())
2461 return NULL;
2462 if (i < 0)
2463 i += PyList_GET_SIZE(self);
2464 return list_item(self, i);
2465 }
2466 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002467 Py_ssize_t start, stop, step, slicelength, cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002468 PyObject* result;
2469 PyObject* it;
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002470 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002471
2472 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
2473 &start, &stop, &step, &slicelength) < 0) {
2474 return NULL;
2475 }
2476
2477 if (slicelength <= 0) {
2478 return PyList_New(0);
2479 }
2480 else {
2481 result = PyList_New(slicelength);
2482 if (!result) return NULL;
2483
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002484 src = self->ob_item;
2485 dest = ((PyListObject *)result)->ob_item;
Tim Peters3b01a122002-07-19 02:35:45 +00002486 for (cur = start, i = 0; i < slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002487 cur += step, i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002488 it = src[cur];
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002489 Py_INCREF(it);
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002490 dest[i] = it;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002491 }
Tim Peters3b01a122002-07-19 02:35:45 +00002492
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002493 return result;
2494 }
2495 }
2496 else {
2497 PyErr_SetString(PyExc_TypeError,
2498 "list indices must be integers");
2499 return NULL;
2500 }
2501}
2502
Tim Peters3b01a122002-07-19 02:35:45 +00002503static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002504list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2505{
2506 if (PyInt_Check(item)) {
Martin v. Löwiseb079f12006-02-16 14:32:27 +00002507 Py_ssize_t i = PyInt_AS_LONG(item);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002508 if (i < 0)
2509 i += PyList_GET_SIZE(self);
2510 return list_ass_item(self, i, value);
2511 }
2512 else if (PyLong_Check(item)) {
Martin v. Löwiseb079f12006-02-16 14:32:27 +00002513 Py_ssize_t i = PyInt_AsSsize_t(item);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002514 if (i == -1 && PyErr_Occurred())
2515 return -1;
2516 if (i < 0)
2517 i += PyList_GET_SIZE(self);
2518 return list_ass_item(self, i, value);
2519 }
2520 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002521 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002522
2523 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
2524 &start, &stop, &step, &slicelength) < 0) {
2525 return -1;
2526 }
2527
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002528 /* treat L[slice(a,b)] = v _exactly_ like L[a:b] = v */
2529 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
2530 return list_ass_slice(self, start, stop, value);
2531
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002532 if (value == NULL) {
2533 /* delete slice */
Raymond Hettinger4bb95402004-02-13 11:36:39 +00002534 PyObject **garbage;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002535 Py_ssize_t cur, i;
Tim Peters3b01a122002-07-19 02:35:45 +00002536
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002537 if (slicelength <= 0)
2538 return 0;
2539
2540 if (step < 0) {
2541 stop = start + 1;
2542 start = stop + step*(slicelength - 1) - 1;
2543 step = -step;
2544 }
2545
2546 garbage = (PyObject**)
2547 PyMem_MALLOC(slicelength*sizeof(PyObject*));
Tim Peters3b01a122002-07-19 02:35:45 +00002548
2549 /* drawing pictures might help
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002550 understand these for loops */
Guido van Rossum75a20b12002-06-11 12:22:28 +00002551 for (cur = start, i = 0;
2552 cur < stop;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002553 cur += step, i++) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002554 Py_ssize_t lim = step;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002555
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002556 garbage[i] = PyList_GET_ITEM(self, cur);
2557
Michael W. Hudson56796f62002-07-29 14:35:04 +00002558 if (cur + step >= self->ob_size) {
2559 lim = self->ob_size - cur - 1;
2560 }
2561
Tim Petersb38e2b62004-07-29 02:29:26 +00002562 memmove(self->ob_item + cur - i,
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002563 self->ob_item + cur + 1,
2564 lim * sizeof(PyObject *));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002565 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002566
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002567 for (cur = start + slicelength*step + 1;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002568 cur < self->ob_size; cur++) {
2569 PyList_SET_ITEM(self, cur - slicelength,
2570 PyList_GET_ITEM(self, cur));
2571 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002572
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002573 self->ob_size -= slicelength;
Raymond Hettingerd4ff7412004-03-15 09:01:31 +00002574 list_resize(self, self->ob_size);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002575
2576 for (i = 0; i < slicelength; i++) {
2577 Py_DECREF(garbage[i]);
2578 }
2579 PyMem_FREE(garbage);
2580
2581 return 0;
2582 }
2583 else {
2584 /* assign slice */
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002585 PyObject **garbage, *ins, *seq, **seqitems, **selfitems;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002586 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002587
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002588 /* protect against a[::-1] = a */
Tim Peters3b01a122002-07-19 02:35:45 +00002589 if (self == (PyListObject*)value) {
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002590 seq = list_slice((PyListObject*)value, 0,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002591 PyList_GET_SIZE(value));
Tim Peters3b01a122002-07-19 02:35:45 +00002592 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002593 else {
Tim Petersb38e2b62004-07-29 02:29:26 +00002594 seq = PySequence_Fast(value,
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002595 "must assign iterable to extended slice");
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002596 if (!seq)
2597 return -1;
2598 }
2599
2600 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2601 PyErr_Format(PyExc_ValueError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00002602 "attempt to assign sequence of size %zd to extended slice of size %zd",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00002603 PySequence_Fast_GET_SIZE(seq),
2604 slicelength);
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002605 Py_DECREF(seq);
2606 return -1;
2607 }
2608
2609 if (!slicelength) {
2610 Py_DECREF(seq);
2611 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002612 }
2613
2614 garbage = (PyObject**)
2615 PyMem_MALLOC(slicelength*sizeof(PyObject*));
Tim Peters3b01a122002-07-19 02:35:45 +00002616
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002617 selfitems = self->ob_item;
Raymond Hettinger42bec932004-03-12 16:38:17 +00002618 seqitems = PySequence_Fast_ITEMS(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002619 for (cur = start, i = 0; i < slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002620 cur += step, i++) {
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002621 garbage[i] = selfitems[cur];
2622 ins = seqitems[i];
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002623 Py_INCREF(ins);
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002624 selfitems[cur] = ins;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002625 }
2626
2627 for (i = 0; i < slicelength; i++) {
2628 Py_DECREF(garbage[i]);
2629 }
Tim Peters3b01a122002-07-19 02:35:45 +00002630
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002631 PyMem_FREE(garbage);
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002632 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002633
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002634 return 0;
2635 }
Tim Peters3b01a122002-07-19 02:35:45 +00002636 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002637 else {
Tim Peters3b01a122002-07-19 02:35:45 +00002638 PyErr_SetString(PyExc_TypeError,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002639 "list indices must be integers");
2640 return -1;
2641 }
2642}
2643
2644static PyMappingMethods list_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002645 (lenfunc)list_length,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002646 (binaryfunc)list_subscript,
2647 (objobjargproc)list_ass_subscript
2648};
2649
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002650PyTypeObject PyList_Type = {
2651 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002652 0,
2653 "list",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002654 sizeof(PyListObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002655 0,
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002656 (destructor)list_dealloc, /* tp_dealloc */
2657 (printfunc)list_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002658 0, /* tp_getattr */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002659 0, /* tp_setattr */
2660 0, /* tp_compare */
2661 (reprfunc)list_repr, /* tp_repr */
2662 0, /* tp_as_number */
2663 &list_as_sequence, /* tp_as_sequence */
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002664 &list_as_mapping, /* tp_as_mapping */
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002665 list_nohash, /* tp_hash */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002666 0, /* tp_call */
2667 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002668 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002669 0, /* tp_setattro */
2670 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002671 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00002672 Py_TPFLAGS_BASETYPE, /* tp_flags */
2673 list_doc, /* tp_doc */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002674 (traverseproc)list_traverse, /* tp_traverse */
2675 (inquiry)list_clear, /* tp_clear */
2676 list_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002677 0, /* tp_weaklistoffset */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002678 list_iter, /* tp_iter */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002679 0, /* tp_iternext */
2680 list_methods, /* tp_methods */
2681 0, /* tp_members */
2682 0, /* tp_getset */
2683 0, /* tp_base */
2684 0, /* tp_dict */
2685 0, /* tp_descr_get */
2686 0, /* tp_descr_set */
2687 0, /* tp_dictoffset */
2688 (initproc)list_init, /* tp_init */
2689 PyType_GenericAlloc, /* tp_alloc */
2690 PyType_GenericNew, /* tp_new */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002691 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002692};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002693
2694
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002695/*********************** List Iterator **************************/
2696
2697typedef struct {
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002698 PyObject_HEAD
2699 long it_index;
Guido van Rossum86103ae2002-07-16 20:07:32 +00002700 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002701} listiterobject;
2702
2703PyTypeObject PyListIter_Type;
2704
Guido van Rossum5086e492002-07-16 15:56:52 +00002705static PyObject *
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002706list_iter(PyObject *seq)
2707{
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002708 listiterobject *it;
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002709
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002710 if (!PyList_Check(seq)) {
2711 PyErr_BadInternalCall();
2712 return NULL;
2713 }
2714 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2715 if (it == NULL)
2716 return NULL;
2717 it->it_index = 0;
2718 Py_INCREF(seq);
2719 it->it_seq = (PyListObject *)seq;
2720 _PyObject_GC_TRACK(it);
2721 return (PyObject *)it;
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002722}
2723
2724static void
2725listiter_dealloc(listiterobject *it)
2726{
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002727 _PyObject_GC_UNTRACK(it);
Guido van Rossum86103ae2002-07-16 20:07:32 +00002728 Py_XDECREF(it->it_seq);
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002729 PyObject_GC_Del(it);
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002730}
2731
2732static int
2733listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2734{
Guido van Rossum86103ae2002-07-16 20:07:32 +00002735 if (it->it_seq == NULL)
2736 return 0;
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002737 return visit((PyObject *)it->it_seq, arg);
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002738}
2739
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002740static PyObject *
Tim Peters93b2cc42002-06-01 05:22:55 +00002741listiter_next(listiterobject *it)
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002742{
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002743 PyListObject *seq;
2744 PyObject *item;
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002745
Tim Peters93b2cc42002-06-01 05:22:55 +00002746 assert(it != NULL);
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002747 seq = it->it_seq;
Guido van Rossum86103ae2002-07-16 20:07:32 +00002748 if (seq == NULL)
2749 return NULL;
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002750 assert(PyList_Check(seq));
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002751
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002752 if (it->it_index < PyList_GET_SIZE(seq)) {
Tim Peters93b2cc42002-06-01 05:22:55 +00002753 item = PyList_GET_ITEM(seq, it->it_index);
2754 ++it->it_index;
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002755 Py_INCREF(item);
2756 return item;
2757 }
Guido van Rossum86103ae2002-07-16 20:07:32 +00002758
2759 Py_DECREF(seq);
2760 it->it_seq = NULL;
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002761 return NULL;
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002762}
2763
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002764static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +00002765listiter_len(listiterobject *it)
2766{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002767 Py_ssize_t len;
Raymond Hettinger40a03822004-04-12 13:05:09 +00002768 if (it->it_seq) {
2769 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2770 if (len >= 0)
Martin v. Löwis18e16552006-02-15 17:27:45 +00002771 return PyInt_FromSsize_t(len);
Raymond Hettinger40a03822004-04-12 13:05:09 +00002772 }
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002773 return PyInt_FromLong(0);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002774}
2775
Armin Rigof5b3e362006-02-11 21:32:43 +00002776PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002777
2778static PyMethodDef listiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002779 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002780 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002781};
2782
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002783PyTypeObject PyListIter_Type = {
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002784 PyObject_HEAD_INIT(&PyType_Type)
2785 0, /* ob_size */
2786 "listiterator", /* tp_name */
2787 sizeof(listiterobject), /* tp_basicsize */
2788 0, /* tp_itemsize */
2789 /* methods */
2790 (destructor)listiter_dealloc, /* tp_dealloc */
2791 0, /* tp_print */
2792 0, /* tp_getattr */
2793 0, /* tp_setattr */
2794 0, /* tp_compare */
2795 0, /* tp_repr */
2796 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002797 0, /* tp_as_sequence */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002798 0, /* tp_as_mapping */
2799 0, /* tp_hash */
2800 0, /* tp_call */
2801 0, /* tp_str */
2802 PyObject_GenericGetAttr, /* tp_getattro */
2803 0, /* tp_setattro */
2804 0, /* tp_as_buffer */
2805 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2806 0, /* tp_doc */
2807 (traverseproc)listiter_traverse, /* tp_traverse */
2808 0, /* tp_clear */
2809 0, /* tp_richcompare */
2810 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002811 PyObject_SelfIter, /* tp_iter */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002812 (iternextfunc)listiter_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002813 listiter_methods, /* tp_methods */
Guido van Rossum6b6272c2002-07-16 20:10:23 +00002814 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002815};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002816
2817/*********************** List Reverse Iterator **************************/
2818
2819typedef struct {
2820 PyObject_HEAD
Martin v. Löwiseb079f12006-02-16 14:32:27 +00002821 Py_ssize_t it_index;
Raymond Hettinger001f2282003-11-08 11:58:44 +00002822 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002823} listreviterobject;
2824
2825PyTypeObject PyListRevIter_Type;
2826
2827static PyObject *
2828list_reversed(PyListObject *seq, PyObject *unused)
2829{
2830 listreviterobject *it;
2831
2832 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2833 if (it == NULL)
2834 return NULL;
2835 assert(PyList_Check(seq));
2836 it->it_index = PyList_GET_SIZE(seq) - 1;
2837 Py_INCREF(seq);
2838 it->it_seq = seq;
2839 PyObject_GC_Track(it);
2840 return (PyObject *)it;
2841}
2842
2843static void
2844listreviter_dealloc(listreviterobject *it)
2845{
2846 PyObject_GC_UnTrack(it);
2847 Py_XDECREF(it->it_seq);
2848 PyObject_GC_Del(it);
2849}
2850
2851static int
2852listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2853{
2854 if (it->it_seq == NULL)
2855 return 0;
2856 return visit((PyObject *)it->it_seq, arg);
2857}
2858
2859static PyObject *
2860listreviter_next(listreviterobject *it)
2861{
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002862 PyObject *item;
Martin v. Löwiseb079f12006-02-16 14:32:27 +00002863 Py_ssize_t index = it->it_index;
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002864 PyListObject *seq = it->it_seq;
Raymond Hettinger1021c442003-11-07 15:38:09 +00002865
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002866 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2867 item = PyList_GET_ITEM(seq, index);
Raymond Hettinger1021c442003-11-07 15:38:09 +00002868 it->it_index--;
2869 Py_INCREF(item);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002870 return item;
Raymond Hettinger1021c442003-11-07 15:38:09 +00002871 }
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002872 it->it_index = -1;
2873 if (seq != NULL) {
2874 it->it_seq = NULL;
2875 Py_DECREF(seq);
2876 }
2877 return NULL;
Raymond Hettinger1021c442003-11-07 15:38:09 +00002878}
2879
Martin v. Löwis18e16552006-02-15 17:27:45 +00002880static Py_ssize_t
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002881listreviter_len(listreviterobject *it)
2882{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002883 Py_ssize_t len = it->it_index + 1;
Raymond Hettinger40a03822004-04-12 13:05:09 +00002884 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2885 return 0;
2886 return len;
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002887}
2888
2889static PySequenceMethods listreviter_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002890 (lenfunc)listreviter_len, /* sq_length */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002891 0, /* sq_concat */
2892};
2893
Raymond Hettinger1021c442003-11-07 15:38:09 +00002894PyTypeObject PyListRevIter_Type = {
2895 PyObject_HEAD_INIT(&PyType_Type)
2896 0, /* ob_size */
2897 "listreverseiterator", /* tp_name */
2898 sizeof(listreviterobject), /* tp_basicsize */
2899 0, /* tp_itemsize */
2900 /* methods */
2901 (destructor)listreviter_dealloc, /* tp_dealloc */
2902 0, /* tp_print */
2903 0, /* tp_getattr */
2904 0, /* tp_setattr */
2905 0, /* tp_compare */
2906 0, /* tp_repr */
2907 0, /* tp_as_number */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002908 &listreviter_as_sequence, /* tp_as_sequence */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002909 0, /* tp_as_mapping */
2910 0, /* tp_hash */
2911 0, /* tp_call */
2912 0, /* tp_str */
2913 PyObject_GenericGetAttr, /* tp_getattro */
2914 0, /* tp_setattro */
2915 0, /* tp_as_buffer */
2916 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2917 0, /* tp_doc */
2918 (traverseproc)listreviter_traverse, /* tp_traverse */
2919 0, /* tp_clear */
2920 0, /* tp_richcompare */
2921 0, /* tp_weaklistoffset */
2922 PyObject_SelfIter, /* tp_iter */
2923 (iternextfunc)listreviter_next, /* tp_iternext */
2924 0,
2925};