blob: eafa98c308d7395402629b3b4a784103b98fa04a [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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00008#include <sys/types.h> /* For size_t */
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00009#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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 PyObject **items;
28 size_t new_allocated;
29 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 /* Bypass realloc() when a previous overallocation is large enough
32 to accommodate the newsize. If the newsize falls lower than half
33 the allocated size, then proceed with the realloc() to shrink the list.
34 */
35 if (allocated >= newsize && newsize >= (allocated >> 1)) {
36 assert(self->ob_item != NULL || newsize == 0);
37 Py_SIZE(self) = newsize;
38 return 0;
39 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +000040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 /* This over-allocates proportional to the list size, making room
42 * for additional growth. The over-allocation is mild, but is
43 * enough to give linear-time amortized behavior over a long
44 * sequence of appends() in the presence of a poorly-performing
45 * system realloc().
46 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
47 */
48 new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 /* check for integer overflow */
51 if (new_allocated > PY_SIZE_MAX - newsize) {
52 PyErr_NoMemory();
53 return -1;
54 } else {
55 new_allocated += newsize;
56 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 if (newsize == 0)
59 new_allocated = 0;
60 items = self->ob_item;
61 if (new_allocated <= ((~(size_t)0) / sizeof(PyObject *)))
62 PyMem_RESIZE(items, PyObject *, new_allocated);
63 else
64 items = NULL;
65 if (items == NULL) {
66 PyErr_NoMemory();
67 return -1;
68 }
69 self->ob_item = items;
70 Py_SIZE(self) = newsize;
71 self->allocated = new_allocated;
72 return 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000073}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000074
Christian Heimes77c02eb2008-02-09 02:18:51 +000075/* Debug statistic to compare allocations with reuse through the free list */
76#undef SHOW_ALLOC_COUNT
77#ifdef SHOW_ALLOC_COUNT
78static size_t count_alloc = 0;
79static size_t count_reuse = 0;
80
81static void
82show_alloc(void)
83{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
85 count_alloc);
86 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
87 "d\n", count_reuse);
88 fprintf(stderr, "%.2f%% reuse rate\n\n",
89 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +000090}
91#endif
92
Raymond Hettinger0468e412004-05-05 05:37:53 +000093/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +000094#ifndef PyList_MAXFREELIST
95#define PyList_MAXFREELIST 80
96#endif
97static PyListObject *free_list[PyList_MAXFREELIST];
98static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +000099
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000100void
101PyList_Fini(void)
102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 PyListObject *op;
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 while (numfree) {
106 op = free_list[--numfree];
107 assert(PyList_CheckExact(op));
108 PyObject_GC_Del(op);
109 }
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000110}
111
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000113PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 PyListObject *op;
116 size_t nbytes;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000117#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 static int initialized = 0;
119 if (!initialized) {
120 Py_AtExit(show_alloc);
121 initialized = 1;
122 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000123#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 if (size < 0) {
126 PyErr_BadInternalCall();
127 return NULL;
128 }
129 /* Check for overflow without an actual overflow,
130 * which can cause compiler to optimise out */
131 if ((size_t)size > PY_SIZE_MAX / sizeof(PyObject *))
132 return PyErr_NoMemory();
133 nbytes = size * sizeof(PyObject *);
134 if (numfree) {
135 numfree--;
136 op = free_list[numfree];
137 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000138#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000140#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 } else {
142 op = PyObject_GC_New(PyListObject, &PyList_Type);
143 if (op == NULL)
144 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000145#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000147#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 }
149 if (size <= 0)
150 op->ob_item = NULL;
151 else {
152 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
153 if (op->ob_item == NULL) {
154 Py_DECREF(op);
155 return PyErr_NoMemory();
156 }
157 memset(op->ob_item, 0, nbytes);
158 }
159 Py_SIZE(op) = size;
160 op->allocated = size;
161 _PyObject_GC_TRACK(op);
162 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163}
164
Martin v. Löwis18e16552006-02-15 17:27:45 +0000165Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000166PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 if (!PyList_Check(op)) {
169 PyErr_BadInternalCall();
170 return -1;
171 }
172 else
173 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174}
175
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000176static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000177
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000179PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 if (!PyList_Check(op)) {
182 PyErr_BadInternalCall();
183 return NULL;
184 }
185 if (i < 0 || i >= Py_SIZE(op)) {
186 if (indexerr == NULL) {
187 indexerr = PyUnicode_FromString(
188 "list index out of range");
189 if (indexerr == NULL)
190 return NULL;
191 }
192 PyErr_SetObject(PyExc_IndexError, indexerr);
193 return NULL;
194 }
195 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000196}
197
198int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000199PyList_SetItem(register PyObject *op, register Py_ssize_t i,
Fred Drakea2f55112000-07-09 15:16:51 +0000200 register PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 register PyObject *olditem;
203 register PyObject **p;
204 if (!PyList_Check(op)) {
205 Py_XDECREF(newitem);
206 PyErr_BadInternalCall();
207 return -1;
208 }
209 if (i < 0 || i >= Py_SIZE(op)) {
210 Py_XDECREF(newitem);
211 PyErr_SetString(PyExc_IndexError,
212 "list assignment index out of range");
213 return -1;
214 }
215 p = ((PyListObject *)op) -> ob_item + i;
216 olditem = *p;
217 *p = newitem;
218 Py_XDECREF(olditem);
219 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220}
221
222static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000223ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 Py_ssize_t i, n = Py_SIZE(self);
226 PyObject **items;
227 if (v == NULL) {
228 PyErr_BadInternalCall();
229 return -1;
230 }
231 if (n == PY_SSIZE_T_MAX) {
232 PyErr_SetString(PyExc_OverflowError,
233 "cannot add more objects to list");
234 return -1;
235 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 if (list_resize(self, n+1) == -1)
238 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 if (where < 0) {
241 where += n;
242 if (where < 0)
243 where = 0;
244 }
245 if (where > n)
246 where = n;
247 items = self->ob_item;
248 for (i = n; --i >= where; )
249 items[i+1] = items[i];
250 Py_INCREF(v);
251 items[where] = v;
252 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253}
254
255int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000256PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 if (!PyList_Check(op)) {
259 PyErr_BadInternalCall();
260 return -1;
261 }
262 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263}
264
Raymond Hettinger40a03822004-04-12 13:05:09 +0000265static int
266app1(PyListObject *self, PyObject *v)
267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 assert (v != NULL);
271 if (n == PY_SSIZE_T_MAX) {
272 PyErr_SetString(PyExc_OverflowError,
273 "cannot add more objects to list");
274 return -1;
275 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 if (list_resize(self, n+1) == -1)
278 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 Py_INCREF(v);
281 PyList_SET_ITEM(self, n, v);
282 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000283}
284
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285int
Fred Drakea2f55112000-07-09 15:16:51 +0000286PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 if (PyList_Check(op) && (newitem != NULL))
289 return app1((PyListObject *)op, newitem);
290 PyErr_BadInternalCall();
291 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292}
293
294/* Methods */
295
296static void
Fred Drakea2f55112000-07-09 15:16:51 +0000297list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 Py_ssize_t i;
300 PyObject_GC_UnTrack(op);
301 Py_TRASHCAN_SAFE_BEGIN(op)
302 if (op->ob_item != NULL) {
303 /* Do it backwards, for Christian Tismer.
304 There's a simple test case where somehow this reduces
305 thrashing when a *very* large list is created and
306 immediately deleted. */
307 i = Py_SIZE(op);
308 while (--i >= 0) {
309 Py_XDECREF(op->ob_item[i]);
310 }
311 PyMem_FREE(op->ob_item);
312 }
313 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
314 free_list[numfree++] = op;
315 else
316 Py_TYPE(op)->tp_free((PyObject *)op);
317 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318}
319
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000321list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 Py_ssize_t i;
324 PyObject *s, *temp;
325 PyObject *pieces = NULL, *result = NULL;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 i = Py_ReprEnter((PyObject*)v);
328 if (i != 0) {
329 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
330 }
Tim Petersa7259592001-06-16 05:11:17 +0000331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 if (Py_SIZE(v) == 0) {
333 result = PyUnicode_FromString("[]");
334 goto Done;
335 }
Tim Petersa7259592001-06-16 05:11:17 +0000336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 pieces = PyList_New(0);
338 if (pieces == NULL)
339 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +0000340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 /* Do repr() on each element. Note that this may mutate the list,
342 so must refetch the list size on each iteration. */
343 for (i = 0; i < Py_SIZE(v); ++i) {
344 int status;
345 if (Py_EnterRecursiveCall(" while getting the repr of a list"))
346 goto Done;
347 s = PyObject_Repr(v->ob_item[i]);
348 Py_LeaveRecursiveCall();
349 if (s == NULL)
350 goto Done;
351 status = PyList_Append(pieces, s);
352 Py_DECREF(s); /* append created a new ref */
353 if (status < 0)
354 goto Done;
355 }
Tim Petersa7259592001-06-16 05:11:17 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 /* Add "[]" decorations to the first and last items. */
358 assert(PyList_GET_SIZE(pieces) > 0);
359 s = PyUnicode_FromString("[");
360 if (s == NULL)
361 goto Done;
362 temp = PyList_GET_ITEM(pieces, 0);
363 PyUnicode_AppendAndDel(&s, temp);
364 PyList_SET_ITEM(pieces, 0, s);
365 if (s == NULL)
366 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 s = PyUnicode_FromString("]");
369 if (s == NULL)
370 goto Done;
371 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
372 PyUnicode_AppendAndDel(&temp, s);
373 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
374 if (temp == NULL)
375 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 /* Paste them all together with ", " between. */
378 s = PyUnicode_FromString(", ");
379 if (s == NULL)
380 goto Done;
381 result = PyUnicode_Join(s, pieces);
382 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000383
384Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 Py_XDECREF(pieces);
386 Py_ReprLeave((PyObject *)v);
387 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388}
389
Martin v. Löwis18e16552006-02-15 17:27:45 +0000390static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000391list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394}
395
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000396static int
Fred Drakea2f55112000-07-09 15:16:51 +0000397list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 Py_ssize_t i;
400 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
403 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
404 Py_EQ);
405 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000406}
407
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000408static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000409list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (i < 0 || i >= Py_SIZE(a)) {
412 if (indexerr == NULL) {
413 indexerr = PyUnicode_FromString(
414 "list index out of range");
415 if (indexerr == NULL)
416 return NULL;
417 }
418 PyErr_SetObject(PyExc_IndexError, indexerr);
419 return NULL;
420 }
421 Py_INCREF(a->ob_item[i]);
422 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423}
424
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000426list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 PyListObject *np;
429 PyObject **src, **dest;
430 Py_ssize_t i, len;
431 if (ilow < 0)
432 ilow = 0;
433 else if (ilow > Py_SIZE(a))
434 ilow = Py_SIZE(a);
435 if (ihigh < ilow)
436 ihigh = ilow;
437 else if (ihigh > Py_SIZE(a))
438 ihigh = Py_SIZE(a);
439 len = ihigh - ilow;
440 np = (PyListObject *) PyList_New(len);
441 if (np == NULL)
442 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 src = a->ob_item + ilow;
445 dest = np->ob_item;
446 for (i = 0; i < len; i++) {
447 PyObject *v = src[i];
448 Py_INCREF(v);
449 dest[i] = v;
450 }
451 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000452}
453
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000455PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (!PyList_Check(a)) {
458 PyErr_BadInternalCall();
459 return NULL;
460 }
461 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000462}
463
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000465list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 Py_ssize_t size;
468 Py_ssize_t i;
469 PyObject **src, **dest;
470 PyListObject *np;
471 if (!PyList_Check(bb)) {
472 PyErr_Format(PyExc_TypeError,
473 "can only concatenate list (not \"%.200s\") to list",
474 bb->ob_type->tp_name);
475 return NULL;
476 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477#define b ((PyListObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 size = Py_SIZE(a) + Py_SIZE(b);
479 if (size < 0)
480 return PyErr_NoMemory();
481 np = (PyListObject *) PyList_New(size);
482 if (np == NULL) {
483 return NULL;
484 }
485 src = a->ob_item;
486 dest = np->ob_item;
487 for (i = 0; i < Py_SIZE(a); i++) {
488 PyObject *v = src[i];
489 Py_INCREF(v);
490 dest[i] = v;
491 }
492 src = b->ob_item;
493 dest = np->ob_item + Py_SIZE(a);
494 for (i = 0; i < Py_SIZE(b); i++) {
495 PyObject *v = src[i];
496 Py_INCREF(v);
497 dest[i] = v;
498 }
499 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000500#undef b
501}
502
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000503static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000504list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 Py_ssize_t i, j;
507 Py_ssize_t size;
508 PyListObject *np;
509 PyObject **p, **items;
510 PyObject *elem;
511 if (n < 0)
512 n = 0;
513 size = Py_SIZE(a) * n;
514 if (n && size/n != Py_SIZE(a))
515 return PyErr_NoMemory();
516 if (size == 0)
517 return PyList_New(0);
518 np = (PyListObject *) PyList_New(size);
519 if (np == NULL)
520 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 items = np->ob_item;
523 if (Py_SIZE(a) == 1) {
524 elem = a->ob_item[0];
525 for (i = 0; i < n; i++) {
526 items[i] = elem;
527 Py_INCREF(elem);
528 }
529 return (PyObject *) np;
530 }
531 p = np->ob_item;
532 items = a->ob_item;
533 for (i = 0; i < n; i++) {
534 for (j = 0; j < Py_SIZE(a); j++) {
535 *p = items[j];
536 Py_INCREF(*p);
537 p++;
538 }
539 }
540 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000541}
542
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543static int
Armin Rigo93677f02004-07-29 12:40:23 +0000544list_clear(PyListObject *a)
545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 Py_ssize_t i;
547 PyObject **item = a->ob_item;
548 if (item != NULL) {
549 /* Because XDECREF can recursively invoke operations on
550 this list, we make it empty first. */
551 i = Py_SIZE(a);
552 Py_SIZE(a) = 0;
553 a->ob_item = NULL;
554 a->allocated = 0;
555 while (--i >= 0) {
556 Py_XDECREF(item[i]);
557 }
558 PyMem_FREE(item);
559 }
560 /* Never fails; the return value can be ignored.
561 Note that there is no guarantee that the list is actually empty
562 at this point, because XDECREF may have populated it again! */
563 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000564}
565
Tim Peters8fc4a912004-07-31 21:53:19 +0000566/* a[ilow:ihigh] = v if v != NULL.
567 * del a[ilow:ihigh] if v == NULL.
568 *
569 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
570 * guaranteed the call cannot fail.
571 */
Armin Rigo93677f02004-07-29 12:40:23 +0000572static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000573list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 /* Because [X]DECREF can recursively invoke list operations on
576 this list, we must postpone all [X]DECREF activity until
577 after the list is back in its canonical shape. Therefore
578 we must allocate an additional array, 'recycle', into which
579 we temporarily copy the items that are deleted from the
580 list. :-( */
581 PyObject *recycle_on_stack[8];
582 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
583 PyObject **item;
584 PyObject **vitem = NULL;
585 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
586 Py_ssize_t n; /* # of elements in replacement list */
587 Py_ssize_t norig; /* # of elements in list getting replaced */
588 Py_ssize_t d; /* Change in size */
589 Py_ssize_t k;
590 size_t s;
591 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 if (v == NULL)
594 n = 0;
595 else {
596 if (a == b) {
597 /* Special case "a[i:j] = a" -- copy b first */
598 v = list_slice(b, 0, Py_SIZE(b));
599 if (v == NULL)
600 return result;
601 result = list_ass_slice(a, ilow, ihigh, v);
602 Py_DECREF(v);
603 return result;
604 }
605 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
606 if(v_as_SF == NULL)
607 goto Error;
608 n = PySequence_Fast_GET_SIZE(v_as_SF);
609 vitem = PySequence_Fast_ITEMS(v_as_SF);
610 }
611 if (ilow < 0)
612 ilow = 0;
613 else if (ilow > Py_SIZE(a))
614 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 if (ihigh < ilow)
617 ihigh = ilow;
618 else if (ihigh > Py_SIZE(a))
619 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 norig = ihigh - ilow;
622 assert(norig >= 0);
623 d = n - norig;
624 if (Py_SIZE(a) + d == 0) {
625 Py_XDECREF(v_as_SF);
626 return list_clear(a);
627 }
628 item = a->ob_item;
629 /* recycle the items that we are about to remove */
630 s = norig * sizeof(PyObject *);
631 if (s > sizeof(recycle_on_stack)) {
632 recycle = (PyObject **)PyMem_MALLOC(s);
633 if (recycle == NULL) {
634 PyErr_NoMemory();
635 goto Error;
636 }
637 }
638 memcpy(recycle, &item[ilow], s);
Tim Peters8d9eb102004-07-31 02:24:20 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 if (d < 0) { /* Delete -d items */
641 memmove(&item[ihigh+d], &item[ihigh],
642 (Py_SIZE(a) - ihigh)*sizeof(PyObject *));
643 list_resize(a, Py_SIZE(a) + d);
644 item = a->ob_item;
645 }
646 else if (d > 0) { /* Insert d items */
647 k = Py_SIZE(a);
648 if (list_resize(a, k+d) < 0)
649 goto Error;
650 item = a->ob_item;
651 memmove(&item[ihigh+d], &item[ihigh],
652 (k - ihigh)*sizeof(PyObject *));
653 }
654 for (k = 0; k < n; k++, ilow++) {
655 PyObject *w = vitem[k];
656 Py_XINCREF(w);
657 item[ilow] = w;
658 }
659 for (k = norig - 1; k >= 0; --k)
660 Py_XDECREF(recycle[k]);
661 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000662 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 if (recycle != recycle_on_stack)
664 PyMem_FREE(recycle);
665 Py_XDECREF(v_as_SF);
666 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000667#undef b
668}
669
Guido van Rossum234f9421993-06-17 12:35:49 +0000670int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000671PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 if (!PyList_Check(a)) {
674 PyErr_BadInternalCall();
675 return -1;
676 }
677 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000678}
679
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000680static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000681list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 PyObject **items;
684 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000685
686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 size = PyList_GET_SIZE(self);
688 if (size == 0 || n == 1) {
689 Py_INCREF(self);
690 return (PyObject *)self;
691 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 if (n < 1) {
694 (void)list_clear(self);
695 Py_INCREF(self);
696 return (PyObject *)self;
697 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 if (size > PY_SSIZE_T_MAX / n) {
700 return PyErr_NoMemory();
701 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 if (list_resize(self, size*n) == -1)
704 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 p = size;
707 items = self->ob_item;
708 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
709 for (j = 0; j < size; j++) {
710 PyObject *o = items[j];
711 Py_INCREF(o);
712 items[p++] = o;
713 }
714 }
715 Py_INCREF(self);
716 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000717}
718
Guido van Rossum4a450d01991-04-03 19:05:18 +0000719static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000720list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 PyObject *old_value;
723 if (i < 0 || i >= Py_SIZE(a)) {
724 PyErr_SetString(PyExc_IndexError,
725 "list assignment index out of range");
726 return -1;
727 }
728 if (v == NULL)
729 return list_ass_slice(a, i, i+1, v);
730 Py_INCREF(v);
731 old_value = a->ob_item[i];
732 a->ob_item[i] = v;
733 Py_DECREF(old_value);
734 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000735}
736
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000737static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000738listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 Py_ssize_t i;
741 PyObject *v;
742 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
743 return NULL;
744 if (ins1(self, i, v) == 0)
745 Py_RETURN_NONE;
746 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000747}
748
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000749static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000750listappend(PyListObject *self, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 if (app1(self, v) == 0)
753 Py_RETURN_NONE;
754 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755}
756
Barry Warsawdedf6d61998-10-09 16:37:25 +0000757static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000758listextend(PyListObject *self, PyObject *b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 PyObject *it; /* iter(v) */
761 Py_ssize_t m; /* size of self */
762 Py_ssize_t n; /* guess for size of b */
763 Py_ssize_t mn; /* m + n */
764 Py_ssize_t i;
765 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 /* Special cases:
768 1) lists and tuples which can use PySequence_Fast ops
769 2) extending self to self requires making a copy first
770 */
771 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
772 PyObject **src, **dest;
773 b = PySequence_Fast(b, "argument must be iterable");
774 if (!b)
775 return NULL;
776 n = PySequence_Fast_GET_SIZE(b);
777 if (n == 0) {
778 /* short circuit when b is empty */
779 Py_DECREF(b);
780 Py_RETURN_NONE;
781 }
782 m = Py_SIZE(self);
783 if (list_resize(self, m + n) == -1) {
784 Py_DECREF(b);
785 return NULL;
786 }
787 /* note that we may still have self == b here for the
788 * situation a.extend(a), but the following code works
789 * in that case too. Just make sure to resize self
790 * before calling PySequence_Fast_ITEMS.
791 */
792 /* populate the end of self with b's items */
793 src = PySequence_Fast_ITEMS(b);
794 dest = self->ob_item + m;
795 for (i = 0; i < n; i++) {
796 PyObject *o = src[i];
797 Py_INCREF(o);
798 dest[i] = o;
799 }
800 Py_DECREF(b);
801 Py_RETURN_NONE;
802 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 it = PyObject_GetIter(b);
805 if (it == NULL)
806 return NULL;
807 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 /* Guess a result list size. */
810 n = _PyObject_LengthHint(b, 8);
811 if (n == -1) {
812 Py_DECREF(it);
813 return NULL;
814 }
815 m = Py_SIZE(self);
816 mn = m + n;
817 if (mn >= m) {
818 /* Make room. */
819 if (list_resize(self, mn) == -1)
820 goto error;
821 /* Make the list sane again. */
822 Py_SIZE(self) = m;
823 }
824 /* Else m + n overflowed; on the chance that n lied, and there really
825 * is enough room, ignore it. If n was telling the truth, we'll
826 * eventually run out of memory during the loop.
827 */
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 /* Run iterator to exhaustion. */
830 for (;;) {
831 PyObject *item = iternext(it);
832 if (item == NULL) {
833 if (PyErr_Occurred()) {
834 if (PyErr_ExceptionMatches(PyExc_StopIteration))
835 PyErr_Clear();
836 else
837 goto error;
838 }
839 break;
840 }
841 if (Py_SIZE(self) < self->allocated) {
842 /* steals ref */
843 PyList_SET_ITEM(self, Py_SIZE(self), item);
844 ++Py_SIZE(self);
845 }
846 else {
847 int status = app1(self, item);
848 Py_DECREF(item); /* append creates a new ref */
849 if (status < 0)
850 goto error;
851 }
852 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 /* Cut back result list if initial guess was too large. */
855 if (Py_SIZE(self) < self->allocated)
856 list_resize(self, Py_SIZE(self)); /* shrinking can't fail */
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 Py_DECREF(it);
859 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000860
861 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 Py_DECREF(it);
863 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000864}
865
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000866PyObject *
867_PyList_Extend(PyListObject *self, PyObject *b)
868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 return listextend(self, b);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000870}
871
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000872static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000873list_inplace_concat(PyListObject *self, PyObject *other)
874{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 result = listextend(self, other);
878 if (result == NULL)
879 return result;
880 Py_DECREF(result);
881 Py_INCREF(self);
882 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000883}
884
885static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000886listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 Py_ssize_t i = -1;
889 PyObject *v;
890 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 if (!PyArg_ParseTuple(args, "|n:pop", &i))
893 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 if (Py_SIZE(self) == 0) {
896 /* Special-case most common failure cause */
897 PyErr_SetString(PyExc_IndexError, "pop from empty list");
898 return NULL;
899 }
900 if (i < 0)
901 i += Py_SIZE(self);
902 if (i < 0 || i >= Py_SIZE(self)) {
903 PyErr_SetString(PyExc_IndexError, "pop index out of range");
904 return NULL;
905 }
906 v = self->ob_item[i];
907 if (i == Py_SIZE(self) - 1) {
908 status = list_resize(self, Py_SIZE(self) - 1);
909 assert(status >= 0);
910 return v; /* and v now owns the reference the list had */
911 }
912 Py_INCREF(v);
913 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
914 assert(status >= 0);
915 /* Use status, so that in a release build compilers don't
916 * complain about the unused name.
917 */
918 (void) status;
Brett Cannon651dd522004-08-08 21:21:18 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000921}
922
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000923/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
924static void
925reverse_slice(PyObject **lo, PyObject **hi)
926{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 --hi;
930 while (lo < hi) {
931 PyObject *t = *lo;
932 *lo = *hi;
933 *hi = t;
934 ++lo;
935 --hi;
936 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000937}
938
Tim Petersa64dc242002-08-01 02:13:36 +0000939/* Lots of code for an adaptive, stable, natural mergesort. There are many
940 * pieces to this algorithm; read listsort.txt for overviews and details.
941 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000942
Daniel Stutzbach98338222010-12-02 21:55:33 +0000943/* A sortslice contains a pointer to an array of keys and a pointer to
944 * an array of corresponding values. In other words, keys[i]
945 * corresponds with values[i]. If values == NULL, then the keys are
946 * also the values.
947 *
948 * Several convenience routines are provided here, so that keys and
949 * values are always moved in sync.
950 */
951
952typedef struct {
953 PyObject **keys;
954 PyObject **values;
955} sortslice;
956
957Py_LOCAL_INLINE(void)
958sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
959{
960 s1->keys[i] = s2->keys[j];
961 if (s1->values != NULL)
962 s1->values[i] = s2->values[j];
963}
964
965Py_LOCAL_INLINE(void)
966sortslice_copy_incr(sortslice *dst, sortslice *src) {
967 *dst->keys++ = *src->keys++;
968 if (dst->values != NULL)
969 *dst->values++ = *src->values++;
970}
971
972Py_LOCAL_INLINE(void)
973sortslice_copy_decr(sortslice *dst, sortslice *src) {
974 *dst->keys-- = *src->keys--;
975 if (dst->values != NULL)
976 *dst->values-- = *src->values--;
977}
978
979
980Py_LOCAL_INLINE(void)
981sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
982 Py_ssize_t n) {
983 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
984 if (s1->values != NULL)
985 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
986}
987
988Py_LOCAL_INLINE(void)
989sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
990 Py_ssize_t n) {
991 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
992 if (s1->values != NULL)
993 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
994}
995
996Py_LOCAL_INLINE(void)
997sortslice_advance(sortslice *slice, Py_ssize_t n) {
998 slice->keys += n;
999 if (slice->values != NULL)
1000 slice->values += n;
1001}
1002
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001003/* Comparison function: PyObject_RichCompareBool with Py_LT.
Tim Petersa64dc242002-08-01 02:13:36 +00001004 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1005 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001006
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001007#define ISLT(X, Y) (PyObject_RichCompareBool(X, Y, Py_LT))
Tim Peters66860f62002-08-04 17:47:26 +00001008
1009/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001010 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1011 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1012*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001013#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001015
1016/* binarysort is the best method for sorting small arrays: it does
1017 few compares, but can do data movement quadratic in the number of
1018 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001019 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001020 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001021 On entry, must have lo <= start <= hi, and that [lo, start) is already
1022 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001023 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001024 Even in case of error, the output slice will be some permutation of
1025 the input (nothing is lost or duplicated).
1026*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001027static int
Daniel Stutzbach98338222010-12-02 21:55:33 +00001028binarysort(sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 register Py_ssize_t k;
1031 register PyObject **l, **p, **r;
1032 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001033
Daniel Stutzbach98338222010-12-02 21:55:33 +00001034 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001036 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 ++start;
1038 for (; start < hi; ++start) {
1039 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001040 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 r = start;
1042 pivot = *r;
1043 /* Invariants:
1044 * pivot >= all in [lo, l).
1045 * pivot < all in [r, start).
1046 * The second is vacuously true at the start.
1047 */
1048 assert(l < r);
1049 do {
1050 p = l + ((r - l) >> 1);
1051 IFLT(pivot, *p)
1052 r = p;
1053 else
1054 l = p+1;
1055 } while (l < r);
1056 assert(l == r);
1057 /* The invariants still hold, so pivot >= all in [lo, l) and
1058 pivot < all in [l, start), so pivot belongs at l. Note
1059 that if there are elements equal to pivot, l points to the
1060 first slot after them -- that's why this sort is stable.
1061 Slide over to make room.
1062 Caution: using memmove is much slower under MSVC 5;
1063 we're not usually moving many slots. */
1064 for (p = start; p > l; --p)
1065 *p = *(p-1);
1066 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001067 if (lo.values != NULL) {
1068 Py_ssize_t offset = lo.values - lo.keys;
1069 p = start + offset;
1070 pivot = *p;
1071 l += offset;
1072 for (p = start + offset; p > l; --p)
1073 *p = *(p-1);
1074 *l = pivot;
1075 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 }
1077 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001078
1079 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001081}
1082
Tim Petersa64dc242002-08-01 02:13:36 +00001083/*
1084Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1085is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001086
Tim Petersa64dc242002-08-01 02:13:36 +00001087 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001088
Tim Petersa64dc242002-08-01 02:13:36 +00001089or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001090
Tim Petersa64dc242002-08-01 02:13:36 +00001091 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001092
Tim Petersa64dc242002-08-01 02:13:36 +00001093Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1094For its intended use in a stable mergesort, the strictness of the defn of
1095"descending" is needed so that the caller can safely reverse a descending
1096sequence without violating stability (strict > ensures there are no equal
1097elements to get out of order).
1098
1099Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001100*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001101static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001102count_run(PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 Py_ssize_t k;
1105 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 assert(lo < hi);
1108 *descending = 0;
1109 ++lo;
1110 if (lo == hi)
1111 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 n = 2;
1114 IFLT(*lo, *(lo-1)) {
1115 *descending = 1;
1116 for (lo = lo+1; lo < hi; ++lo, ++n) {
1117 IFLT(*lo, *(lo-1))
1118 ;
1119 else
1120 break;
1121 }
1122 }
1123 else {
1124 for (lo = lo+1; lo < hi; ++lo, ++n) {
1125 IFLT(*lo, *(lo-1))
1126 break;
1127 }
1128 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001131fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001133}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001134
Tim Petersa64dc242002-08-01 02:13:36 +00001135/*
1136Locate the proper position of key in a sorted vector; if the vector contains
1137an element equal to key, return the position immediately to the left of
1138the leftmost equal element. [gallop_right() does the same except returns
1139the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001140
Tim Petersa64dc242002-08-01 02:13:36 +00001141"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001142
Tim Petersa64dc242002-08-01 02:13:36 +00001143"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1144hint is to the final result, the faster this runs.
1145
1146The return value is the int k in 0..n such that
1147
1148 a[k-1] < key <= a[k]
1149
1150pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1151key belongs at index k; or, IOW, the first k elements of a should precede
1152key, and the last n-k should follow key.
1153
1154Returns -1 on error. See listsort.txt for info on the method.
1155*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001156static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001157gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 Py_ssize_t ofs;
1160 Py_ssize_t lastofs;
1161 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 a += hint;
1166 lastofs = 0;
1167 ofs = 1;
1168 IFLT(*a, key) {
1169 /* a[hint] < key -- gallop right, until
1170 * a[hint + lastofs] < key <= a[hint + ofs]
1171 */
1172 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1173 while (ofs < maxofs) {
1174 IFLT(a[ofs], key) {
1175 lastofs = ofs;
1176 ofs = (ofs << 1) + 1;
1177 if (ofs <= 0) /* int overflow */
1178 ofs = maxofs;
1179 }
1180 else /* key <= a[hint + ofs] */
1181 break;
1182 }
1183 if (ofs > maxofs)
1184 ofs = maxofs;
1185 /* Translate back to offsets relative to &a[0]. */
1186 lastofs += hint;
1187 ofs += hint;
1188 }
1189 else {
1190 /* key <= a[hint] -- gallop left, until
1191 * a[hint - ofs] < key <= a[hint - lastofs]
1192 */
1193 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1194 while (ofs < maxofs) {
1195 IFLT(*(a-ofs), key)
1196 break;
1197 /* key <= a[hint - ofs] */
1198 lastofs = ofs;
1199 ofs = (ofs << 1) + 1;
1200 if (ofs <= 0) /* int overflow */
1201 ofs = maxofs;
1202 }
1203 if (ofs > maxofs)
1204 ofs = maxofs;
1205 /* Translate back to positive offsets relative to &a[0]. */
1206 k = lastofs;
1207 lastofs = hint - ofs;
1208 ofs = hint - k;
1209 }
1210 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1213 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1214 * right of lastofs but no farther right than ofs. Do a binary
1215 * search, with invariant a[lastofs-1] < key <= a[ofs].
1216 */
1217 ++lastofs;
1218 while (lastofs < ofs) {
1219 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 IFLT(a[m], key)
1222 lastofs = m+1; /* a[m] < key */
1223 else
1224 ofs = m; /* key <= a[m] */
1225 }
1226 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1227 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001228
1229fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001231}
1232
1233/*
1234Exactly like gallop_left(), except that if key already exists in a[0:n],
1235finds the position immediately to the right of the rightmost equal value.
1236
1237The return value is the int k in 0..n such that
1238
1239 a[k-1] <= key < a[k]
1240
1241or -1 if error.
1242
1243The code duplication is massive, but this is enough different given that
1244we're sticking to "<" comparisons that it's much harder to follow if
1245written as one routine with yet another "left or right?" flag.
1246*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001247static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001248gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 Py_ssize_t ofs;
1251 Py_ssize_t lastofs;
1252 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 a += hint;
1257 lastofs = 0;
1258 ofs = 1;
1259 IFLT(key, *a) {
1260 /* key < a[hint] -- gallop left, until
1261 * a[hint - ofs] <= key < a[hint - lastofs]
1262 */
1263 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1264 while (ofs < maxofs) {
1265 IFLT(key, *(a-ofs)) {
1266 lastofs = ofs;
1267 ofs = (ofs << 1) + 1;
1268 if (ofs <= 0) /* int overflow */
1269 ofs = maxofs;
1270 }
1271 else /* a[hint - ofs] <= key */
1272 break;
1273 }
1274 if (ofs > maxofs)
1275 ofs = maxofs;
1276 /* Translate back to positive offsets relative to &a[0]. */
1277 k = lastofs;
1278 lastofs = hint - ofs;
1279 ofs = hint - k;
1280 }
1281 else {
1282 /* a[hint] <= key -- gallop right, until
1283 * a[hint + lastofs] <= key < a[hint + ofs]
1284 */
1285 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1286 while (ofs < maxofs) {
1287 IFLT(key, a[ofs])
1288 break;
1289 /* a[hint + ofs] <= key */
1290 lastofs = ofs;
1291 ofs = (ofs << 1) + 1;
1292 if (ofs <= 0) /* int overflow */
1293 ofs = maxofs;
1294 }
1295 if (ofs > maxofs)
1296 ofs = maxofs;
1297 /* Translate back to offsets relative to &a[0]. */
1298 lastofs += hint;
1299 ofs += hint;
1300 }
1301 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1304 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1305 * right of lastofs but no farther right than ofs. Do a binary
1306 * search, with invariant a[lastofs-1] <= key < a[ofs].
1307 */
1308 ++lastofs;
1309 while (lastofs < ofs) {
1310 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 IFLT(key, a[m])
1313 ofs = m; /* key < a[m] */
1314 else
1315 lastofs = m+1; /* a[m] <= key */
1316 }
1317 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1318 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001319
1320fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001322}
1323
1324/* The maximum number of entries in a MergeState's pending-runs stack.
1325 * This is enough to sort arrays of size up to about
1326 * 32 * phi ** MAX_MERGE_PENDING
1327 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1328 * with 2**64 elements.
1329 */
1330#define MAX_MERGE_PENDING 85
1331
Tim Peterse05f65a2002-08-10 05:21:15 +00001332/* When we get into galloping mode, we stay there until both runs win less
1333 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
Tim Petersa64dc242002-08-01 02:13:36 +00001334 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001335#define MIN_GALLOP 7
Tim Petersa64dc242002-08-01 02:13:36 +00001336
1337/* Avoid malloc for small temp arrays. */
1338#define MERGESTATE_TEMP_SIZE 256
1339
1340/* One MergeState exists on the stack per invocation of mergesort. It's just
1341 * a convenient way to pass state around among the helper functions.
1342 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001343struct s_slice {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001344 sortslice base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 Py_ssize_t len;
Tim Peterse05f65a2002-08-10 05:21:15 +00001346};
1347
Tim Petersa64dc242002-08-01 02:13:36 +00001348typedef struct s_MergeState {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 /* This controls when we get *into* galloping mode. It's initialized
1350 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1351 * random data, and lower for highly structured data.
1352 */
1353 Py_ssize_t min_gallop;
Tim Peterse05f65a2002-08-10 05:21:15 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 /* 'a' is temp storage to help with merges. It contains room for
1356 * alloced entries.
1357 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001358 sortslice a; /* may point to temparray below */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 Py_ssize_t alloced;
Tim Petersa64dc242002-08-01 02:13:36 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 /* A stack of n pending runs yet to be merged. Run #i starts at
1362 * address base[i] and extends for len[i] elements. It's always
1363 * true (so long as the indices are in bounds) that
1364 *
1365 * pending[i].base + pending[i].len == pending[i+1].base
1366 *
1367 * so we could cut the storage for this, but it's a minor amount,
1368 * and keeping all the info explicit simplifies the code.
1369 */
1370 int n;
1371 struct s_slice pending[MAX_MERGE_PENDING];
Tim Petersa64dc242002-08-01 02:13:36 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 /* 'a' points to this when possible, rather than muck with malloc. */
1374 PyObject *temparray[MERGESTATE_TEMP_SIZE];
Tim Petersa64dc242002-08-01 02:13:36 +00001375} MergeState;
1376
1377/* Conceptually a MergeState's constructor. */
1378static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001379merge_init(MergeState *ms, int list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001382 if (has_keyfunc) {
1383 /* The temporary space for merging will need at most half the list
1384 * size rounded up. Use the minimum possible space so we can use the
1385 * rest of temparray for other things. In particular, if there is
1386 * enough extra space, listsort() will use it to store the keys.
1387 */
1388 ms->alloced = (list_size + 1) / 2;
1389
1390 /* ms->alloced describes how many keys will be stored at
1391 ms->temparray, but we also need to store the values. Hence,
1392 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1393 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1394 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1395 ms->a.values = &ms->temparray[ms->alloced];
1396 }
1397 else {
1398 ms->alloced = MERGESTATE_TEMP_SIZE;
1399 ms->a.values = NULL;
1400 }
1401 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 ms->n = 0;
1403 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001404}
1405
1406/* Free all the temp memory owned by the MergeState. This must be called
1407 * when you're done with a MergeState, and may be called before then if
1408 * you want to free the temp memory early.
1409 */
1410static void
1411merge_freemem(MergeState *ms)
1412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001414 if (ms->a.keys != ms->temparray)
1415 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001416}
1417
1418/* Ensure enough temp memory for 'need' array slots is available.
1419 * Returns 0 on success and -1 if the memory can't be gotten.
1420 */
1421static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001422merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001423{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001424 int multiplier;
1425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 assert(ms != NULL);
1427 if (need <= ms->alloced)
1428 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001429
1430 multiplier = ms->a.values != NULL ? 2 : 1;
1431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 /* Don't realloc! That can cost cycles to copy the old data, but
1433 * we don't care what's in the block.
1434 */
1435 merge_freemem(ms);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001436 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 PyErr_NoMemory();
1438 return -1;
1439 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001440 ms->a.keys = (PyObject**)PyMem_Malloc(multiplier * need
1441 * sizeof(PyObject *));
1442 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001444 if (ms->a.values != NULL)
1445 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 return 0;
1447 }
1448 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001450}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1452 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001453
Daniel Stutzbach98338222010-12-02 21:55:33 +00001454/* Merge the na elements starting at ssa with the nb elements starting at
1455 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1456 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1457 * should have na <= nb. See listsort.txt for more info. Return 0 if
1458 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001459 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001460static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001461merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1462 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001465 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 int result = -1; /* guilty until proved innocent */
1467 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001468
Daniel Stutzbach98338222010-12-02 21:55:33 +00001469 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1470 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 if (MERGE_GETMEM(ms, na) < 0)
1472 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001473 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1474 dest = ssa;
1475 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001476
Daniel Stutzbach98338222010-12-02 21:55:33 +00001477 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 --nb;
1479 if (nb == 0)
1480 goto Succeed;
1481 if (na == 1)
1482 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 min_gallop = ms->min_gallop;
1485 for (;;) {
1486 Py_ssize_t acount = 0; /* # of times A won in a row */
1487 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 /* Do the straightforward thing until (if ever) one run
1490 * appears to win consistently.
1491 */
1492 for (;;) {
1493 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001494 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 if (k) {
1496 if (k < 0)
1497 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001498 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 ++bcount;
1500 acount = 0;
1501 --nb;
1502 if (nb == 0)
1503 goto Succeed;
1504 if (bcount >= min_gallop)
1505 break;
1506 }
1507 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001508 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 ++acount;
1510 bcount = 0;
1511 --na;
1512 if (na == 1)
1513 goto CopyB;
1514 if (acount >= min_gallop)
1515 break;
1516 }
1517 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 /* One run is winning so consistently that galloping may
1520 * be a huge win. So try that, and continue galloping until
1521 * (if ever) neither run appears to be winning consistently
1522 * anymore.
1523 */
1524 ++min_gallop;
1525 do {
1526 assert(na > 1 && nb > 0);
1527 min_gallop -= min_gallop > 1;
1528 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001529 k = gallop_right(ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 acount = k;
1531 if (k) {
1532 if (k < 0)
1533 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001534 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1535 sortslice_advance(&dest, k);
1536 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 na -= k;
1538 if (na == 1)
1539 goto CopyB;
1540 /* na==0 is impossible now if the comparison
1541 * function is consistent, but we can't assume
1542 * that it is.
1543 */
1544 if (na == 0)
1545 goto Succeed;
1546 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001547 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 --nb;
1549 if (nb == 0)
1550 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001551
Daniel Stutzbach98338222010-12-02 21:55:33 +00001552 k = gallop_left(ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 bcount = k;
1554 if (k) {
1555 if (k < 0)
1556 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001557 sortslice_memmove(&dest, 0, &ssb, 0, k);
1558 sortslice_advance(&dest, k);
1559 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 nb -= k;
1561 if (nb == 0)
1562 goto Succeed;
1563 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001564 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 --na;
1566 if (na == 1)
1567 goto CopyB;
1568 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1569 ++min_gallop; /* penalize it for leaving galloping mode */
1570 ms->min_gallop = min_gallop;
1571 }
Tim Petersa64dc242002-08-01 02:13:36 +00001572Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001574Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001576 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001578CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001580 /* The last element of ssa belongs at the end of the merge. */
1581 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1582 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001584}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001585
Daniel Stutzbach98338222010-12-02 21:55:33 +00001586/* Merge the na elements starting at pa with the nb elements starting at
1587 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1588 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1589 * should have na >= nb. See listsort.txt for more info. Return 0 if
1590 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001591 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001592static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001593merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1594 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001597 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001600
Daniel Stutzbach98338222010-12-02 21:55:33 +00001601 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1602 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 if (MERGE_GETMEM(ms, nb) < 0)
1604 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001605 dest = ssb;
1606 sortslice_advance(&dest, nb-1);
1607 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1608 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001610 ssb.keys = ms->a.keys + nb - 1;
1611 if (ssb.values != NULL)
1612 ssb.values = ms->a.values + nb - 1;
1613 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001614
Daniel Stutzbach98338222010-12-02 21:55:33 +00001615 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 --na;
1617 if (na == 0)
1618 goto Succeed;
1619 if (nb == 1)
1620 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 min_gallop = ms->min_gallop;
1623 for (;;) {
1624 Py_ssize_t acount = 0; /* # of times A won in a row */
1625 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 /* Do the straightforward thing until (if ever) one run
1628 * appears to win consistently.
1629 */
1630 for (;;) {
1631 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001632 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 if (k) {
1634 if (k < 0)
1635 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001636 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 ++acount;
1638 bcount = 0;
1639 --na;
1640 if (na == 0)
1641 goto Succeed;
1642 if (acount >= min_gallop)
1643 break;
1644 }
1645 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001646 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 ++bcount;
1648 acount = 0;
1649 --nb;
1650 if (nb == 1)
1651 goto CopyA;
1652 if (bcount >= min_gallop)
1653 break;
1654 }
1655 }
Tim Petersa64dc242002-08-01 02:13:36 +00001656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 /* One run is winning so consistently that galloping may
1658 * be a huge win. So try that, and continue galloping until
1659 * (if ever) neither run appears to be winning consistently
1660 * anymore.
1661 */
1662 ++min_gallop;
1663 do {
1664 assert(na > 0 && nb > 1);
1665 min_gallop -= min_gallop > 1;
1666 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001667 k = gallop_right(ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 if (k < 0)
1669 goto Fail;
1670 k = na - k;
1671 acount = k;
1672 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001673 sortslice_advance(&dest, -k);
1674 sortslice_advance(&ssa, -k);
1675 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 na -= k;
1677 if (na == 0)
1678 goto Succeed;
1679 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001680 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 --nb;
1682 if (nb == 1)
1683 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001684
Daniel Stutzbach98338222010-12-02 21:55:33 +00001685 k = gallop_left(ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 if (k < 0)
1687 goto Fail;
1688 k = nb - k;
1689 bcount = k;
1690 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001691 sortslice_advance(&dest, -k);
1692 sortslice_advance(&ssb, -k);
1693 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 nb -= k;
1695 if (nb == 1)
1696 goto CopyA;
1697 /* nb==0 is impossible now if the comparison
1698 * function is consistent, but we can't assume
1699 * that it is.
1700 */
1701 if (nb == 0)
1702 goto Succeed;
1703 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001704 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 --na;
1706 if (na == 0)
1707 goto Succeed;
1708 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1709 ++min_gallop; /* penalize it for leaving galloping mode */
1710 ms->min_gallop = min_gallop;
1711 }
Tim Petersa64dc242002-08-01 02:13:36 +00001712Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001714Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001716 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001718CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001720 /* The first element of ssb belongs at the front of the merge. */
1721 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1722 sortslice_advance(&dest, -na);
1723 sortslice_advance(&ssa, -na);
1724 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001726}
1727
1728/* Merge the two runs at stack indices i and i+1.
1729 * Returns 0 on success, -1 on error.
1730 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001731static Py_ssize_t
1732merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001733{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001734 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 Py_ssize_t na, nb;
1736 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 assert(ms != NULL);
1739 assert(ms->n >= 2);
1740 assert(i >= 0);
1741 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001742
Daniel Stutzbach98338222010-12-02 21:55:33 +00001743 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001745 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 nb = ms->pending[i+1].len;
1747 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001748 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 /* Record the length of the combined runs; if i is the 3rd-last
1751 * run now, also slide over the last run (which isn't involved
1752 * in this merge). The current run i+1 goes away in any case.
1753 */
1754 ms->pending[i].len = na + nb;
1755 if (i == ms->n - 3)
1756 ms->pending[i+1] = ms->pending[i+2];
1757 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 /* Where does b start in a? Elements in a before that can be
1760 * ignored (already in place).
1761 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001762 k = gallop_right(*ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 if (k < 0)
1764 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001765 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 na -= k;
1767 if (na == 0)
1768 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 /* Where does a end in b? Elements in b after that can be
1771 * ignored (already in place).
1772 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001773 nb = gallop_left(ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 if (nb <= 0)
1775 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 /* Merge what remains of the runs, using a temp array with
1778 * min(na, nb) elements.
1779 */
1780 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001781 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001783 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001784}
1785
1786/* Examine the stack of runs waiting to be merged, merging adjacent runs
1787 * until the stack invariants are re-established:
1788 *
1789 * 1. len[-3] > len[-2] + len[-1]
1790 * 2. len[-2] > len[-1]
1791 *
1792 * See listsort.txt for more info.
1793 *
1794 * Returns 0 on success, -1 on error.
1795 */
1796static int
1797merge_collapse(MergeState *ms)
1798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 assert(ms);
1802 while (ms->n > 1) {
1803 Py_ssize_t n = ms->n - 2;
1804 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
1805 if (p[n-1].len < p[n+1].len)
1806 --n;
1807 if (merge_at(ms, n) < 0)
1808 return -1;
1809 }
1810 else if (p[n].len <= p[n+1].len) {
1811 if (merge_at(ms, n) < 0)
1812 return -1;
1813 }
1814 else
1815 break;
1816 }
1817 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001818}
1819
1820/* Regardless of invariants, merge all runs on the stack until only one
1821 * remains. This is used at the end of the mergesort.
1822 *
1823 * Returns 0 on success, -1 on error.
1824 */
1825static int
1826merge_force_collapse(MergeState *ms)
1827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 assert(ms);
1831 while (ms->n > 1) {
1832 Py_ssize_t n = ms->n - 2;
1833 if (n > 0 && p[n-1].len < p[n+1].len)
1834 --n;
1835 if (merge_at(ms, n) < 0)
1836 return -1;
1837 }
1838 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001839}
1840
1841/* Compute a good value for the minimum run length; natural runs shorter
1842 * than this are boosted artificially via binary insertion.
1843 *
1844 * If n < 64, return n (it's too small to bother with fancy stuff).
1845 * Else if n is an exact power of 2, return 32.
1846 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1847 * strictly less than, an exact power of 2.
1848 *
1849 * See listsort.txt for more info.
1850 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001851static Py_ssize_t
1852merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 assert(n >= 0);
1857 while (n >= 64) {
1858 r |= n & 1;
1859 n >>= 1;
1860 }
1861 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001862}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001863
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001864static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001865reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001866{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001867 reverse_slice(s->keys, &s->keys[n]);
1868 if (s->values != NULL)
1869 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001870}
1871
Tim Petersa64dc242002-08-01 02:13:36 +00001872/* An adaptive, stable, natural mergesort. See listsort.txt.
1873 * Returns Py_None on success, NULL on error. Even in case of error, the
1874 * list will be some permutation of its input state (nothing is lost or
1875 * duplicated).
1876 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001877static PyObject *
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001878listsort(PyListObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 Py_ssize_t nremaining;
1882 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001883 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 Py_ssize_t saved_ob_size, saved_allocated;
1885 PyObject **saved_ob_item;
1886 PyObject **final_ob_item;
1887 PyObject *result = NULL; /* guilty until proved innocent */
1888 int reverse = 0;
1889 PyObject *keyfunc = NULL;
1890 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 static char *kwlist[] = {"key", "reverse", 0};
Daniel Stutzbach98338222010-12-02 21:55:33 +00001892 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 assert(self != NULL);
1895 assert (PyList_Check(self));
1896 if (args != NULL) {
1897 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort",
1898 kwlist, &keyfunc, &reverse))
1899 return NULL;
1900 if (Py_SIZE(args) > 0) {
1901 PyErr_SetString(PyExc_TypeError,
1902 "must use keyword argument for key function");
1903 return NULL;
1904 }
1905 }
1906 if (keyfunc == Py_None)
1907 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 /* The list is temporarily made empty, so that mutations performed
1910 * by comparison functions can't affect the slice of memory we're
1911 * sorting (allowing mutations during sorting is a core-dump
1912 * factory, since ob_item may change).
1913 */
1914 saved_ob_size = Py_SIZE(self);
1915 saved_ob_item = self->ob_item;
1916 saved_allocated = self->allocated;
1917 Py_SIZE(self) = 0;
1918 self->ob_item = NULL;
1919 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00001920
Daniel Stutzbach98338222010-12-02 21:55:33 +00001921 if (keyfunc == NULL) {
1922 keys = NULL;
1923 lo.keys = saved_ob_item;
1924 lo.values = NULL;
1925 }
1926 else {
1927 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
1928 /* Leverage stack space we allocated but won't otherwise use */
1929 keys = &ms.temparray[saved_ob_size+1];
1930 else {
1931 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
1932 if (keys == NULL)
1933 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001935
1936 for (i = 0; i < saved_ob_size ; i++) {
1937 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
1938 NULL);
1939 if (keys[i] == NULL) {
1940 for (i=i-1 ; i>=0 ; i--)
1941 Py_DECREF(keys[i]);
1942 goto keyfunc_fail;
1943 }
1944 }
1945
1946 lo.keys = keys;
1947 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001949
Daniel Stutzbach98338222010-12-02 21:55:33 +00001950 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 nremaining = saved_ob_size;
1953 if (nremaining < 2)
1954 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00001955
Benjamin Peterson05380642010-08-23 19:35:39 +00001956 /* Reverse sort stability achieved by initially reversing the list,
1957 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001958 if (reverse) {
1959 if (keys != NULL)
1960 reverse_slice(&keys[0], &keys[saved_ob_size]);
1961 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
1962 }
Benjamin Peterson05380642010-08-23 19:35:39 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 /* March over the array once, left to right, finding natural runs,
1965 * and extending short natural runs to minrun elements.
1966 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 minrun = merge_compute_minrun(nremaining);
1968 do {
1969 int descending;
1970 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00001971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 /* Identify next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001973 n = count_run(lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 if (n < 0)
1975 goto fail;
1976 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001977 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 /* If short, extend to min(minrun, nremaining). */
1979 if (n < minrun) {
1980 const Py_ssize_t force = nremaining <= minrun ?
1981 nremaining : minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001982 if (binarysort(lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 goto fail;
1984 n = force;
1985 }
1986 /* Push run onto pending-runs stack, and maybe merge. */
1987 assert(ms.n < MAX_MERGE_PENDING);
1988 ms.pending[ms.n].base = lo;
1989 ms.pending[ms.n].len = n;
1990 ++ms.n;
1991 if (merge_collapse(&ms) < 0)
1992 goto fail;
1993 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001994 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 nremaining -= n;
1996 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00001997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 if (merge_force_collapse(&ms) < 0)
1999 goto fail;
2000 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002001 assert(keys == NULL
2002 ? ms.pending[0].base.keys == saved_ob_item
2003 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002005 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002006
2007succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002009fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002010 if (keys != NULL) {
2011 for (i = 0; i < saved_ob_size; i++)
2012 Py_DECREF(keys[i]);
2013 if (keys != &ms.temparray[saved_ob_size+1])
2014 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 if (self->allocated != -1 && result != NULL) {
2018 /* The user mucked with the list during the sort,
2019 * and we don't already have another error to report.
2020 */
2021 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2022 result = NULL;
2023 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 if (reverse && saved_ob_size > 1)
2026 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002029
Daniel Stutzbach98338222010-12-02 21:55:33 +00002030keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 final_ob_item = self->ob_item;
2032 i = Py_SIZE(self);
2033 Py_SIZE(self) = saved_ob_size;
2034 self->ob_item = saved_ob_item;
2035 self->allocated = saved_allocated;
2036 if (final_ob_item != NULL) {
2037 /* we cannot use list_clear() for this because it does not
2038 guarantee that the list is really empty when it returns */
2039 while (--i >= 0) {
2040 Py_XDECREF(final_ob_item[i]);
2041 }
2042 PyMem_FREE(final_ob_item);
2043 }
2044 Py_XINCREF(result);
2045 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002046}
Tim Peters330f9e92002-07-19 07:05:44 +00002047#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002048#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002049
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002050int
Fred Drakea2f55112000-07-09 15:16:51 +00002051PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 if (v == NULL || !PyList_Check(v)) {
2054 PyErr_BadInternalCall();
2055 return -1;
2056 }
2057 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
2058 if (v == NULL)
2059 return -1;
2060 Py_DECREF(v);
2061 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002062}
2063
Guido van Rossumb86c5492001-02-12 22:06:02 +00002064static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002065listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 if (Py_SIZE(self) > 1)
2068 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2069 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002070}
2071
Guido van Rossum84c76f51990-10-30 13:32:20 +00002072int
Fred Drakea2f55112000-07-09 15:16:51 +00002073PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 if (v == NULL || !PyList_Check(v)) {
2078 PyErr_BadInternalCall();
2079 return -1;
2080 }
2081 if (Py_SIZE(self) > 1)
2082 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2083 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002084}
2085
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002086PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002087PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 PyObject *w;
2090 PyObject **p, **q;
2091 Py_ssize_t n;
2092 if (v == NULL || !PyList_Check(v)) {
2093 PyErr_BadInternalCall();
2094 return NULL;
2095 }
2096 n = Py_SIZE(v);
2097 w = PyTuple_New(n);
2098 if (w == NULL)
2099 return NULL;
2100 p = ((PyTupleObject *)w)->ob_item;
2101 q = ((PyListObject *)v)->ob_item;
2102 while (--n >= 0) {
2103 Py_INCREF(*q);
2104 *p = *q;
2105 p++;
2106 q++;
2107 }
2108 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002109}
2110
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002111static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002112listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 Py_ssize_t i, start=0, stop=Py_SIZE(self);
2115 PyObject *v, *format_tuple, *err_string;
2116 static PyObject *err_format = NULL;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2119 _PyEval_SliceIndex, &start,
2120 _PyEval_SliceIndex, &stop))
2121 return NULL;
2122 if (start < 0) {
2123 start += Py_SIZE(self);
2124 if (start < 0)
2125 start = 0;
2126 }
2127 if (stop < 0) {
2128 stop += Py_SIZE(self);
2129 if (stop < 0)
2130 stop = 0;
2131 }
2132 for (i = start; i < stop && i < Py_SIZE(self); i++) {
2133 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2134 if (cmp > 0)
2135 return PyLong_FromSsize_t(i);
2136 else if (cmp < 0)
2137 return NULL;
2138 }
2139 if (err_format == NULL) {
2140 err_format = PyUnicode_FromString("%r is not in list");
2141 if (err_format == NULL)
2142 return NULL;
2143 }
2144 format_tuple = PyTuple_Pack(1, v);
2145 if (format_tuple == NULL)
2146 return NULL;
2147 err_string = PyUnicode_Format(err_format, format_tuple);
2148 Py_DECREF(format_tuple);
2149 if (err_string == NULL)
2150 return NULL;
2151 PyErr_SetObject(PyExc_ValueError, err_string);
2152 Py_DECREF(err_string);
2153 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002154}
2155
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002156static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002157listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 Py_ssize_t count = 0;
2160 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 for (i = 0; i < Py_SIZE(self); i++) {
2163 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2164 if (cmp > 0)
2165 count++;
2166 else if (cmp < 0)
2167 return NULL;
2168 }
2169 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002170}
2171
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002172static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002173listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 for (i = 0; i < Py_SIZE(self); i++) {
2178 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2179 if (cmp > 0) {
2180 if (list_ass_slice(self, i, i+1,
2181 (PyObject *)NULL) == 0)
2182 Py_RETURN_NONE;
2183 return NULL;
2184 }
2185 else if (cmp < 0)
2186 return NULL;
2187 }
2188 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2189 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002190}
2191
Jeremy Hylton8caad492000-06-23 14:18:11 +00002192static int
2193list_traverse(PyListObject *o, visitproc visit, void *arg)
2194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 for (i = Py_SIZE(o); --i >= 0; )
2198 Py_VISIT(o->ob_item[i]);
2199 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002200}
2201
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002202static PyObject *
2203list_richcompare(PyObject *v, PyObject *w, int op)
2204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 PyListObject *vl, *wl;
2206 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 if (!PyList_Check(v) || !PyList_Check(w)) {
2209 Py_INCREF(Py_NotImplemented);
2210 return Py_NotImplemented;
2211 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 vl = (PyListObject *)v;
2214 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2217 /* Shortcut: if the lengths differ, the lists differ */
2218 PyObject *res;
2219 if (op == Py_EQ)
2220 res = Py_False;
2221 else
2222 res = Py_True;
2223 Py_INCREF(res);
2224 return res;
2225 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 /* Search for the first index where items are different */
2228 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2229 int k = PyObject_RichCompareBool(vl->ob_item[i],
2230 wl->ob_item[i], Py_EQ);
2231 if (k < 0)
2232 return NULL;
2233 if (!k)
2234 break;
2235 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2238 /* No more items to compare -- compare sizes */
2239 Py_ssize_t vs = Py_SIZE(vl);
2240 Py_ssize_t ws = Py_SIZE(wl);
2241 int cmp;
2242 PyObject *res;
2243 switch (op) {
2244 case Py_LT: cmp = vs < ws; break;
2245 case Py_LE: cmp = vs <= ws; break;
2246 case Py_EQ: cmp = vs == ws; break;
2247 case Py_NE: cmp = vs != ws; break;
2248 case Py_GT: cmp = vs > ws; break;
2249 case Py_GE: cmp = vs >= ws; break;
2250 default: return NULL; /* cannot happen */
2251 }
2252 if (cmp)
2253 res = Py_True;
2254 else
2255 res = Py_False;
2256 Py_INCREF(res);
2257 return res;
2258 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 /* We have an item that differs -- shortcuts for EQ/NE */
2261 if (op == Py_EQ) {
2262 Py_INCREF(Py_False);
2263 return Py_False;
2264 }
2265 if (op == Py_NE) {
2266 Py_INCREF(Py_True);
2267 return Py_True;
2268 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 /* Compare the final item again using the proper operator */
2271 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002272}
2273
Tim Peters6d6c1a32001-08-02 04:15:00 +00002274static int
2275list_init(PyListObject *self, PyObject *args, PyObject *kw)
2276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 PyObject *arg = NULL;
2278 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2281 return -1;
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 /* Verify list invariants established by PyType_GenericAlloc() */
2284 assert(0 <= Py_SIZE(self));
2285 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2286 assert(self->ob_item != NULL ||
2287 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 /* Empty previous contents */
2290 if (self->ob_item != NULL) {
2291 (void)list_clear(self);
2292 }
2293 if (arg != NULL) {
2294 PyObject *rv = listextend(self, arg);
2295 if (rv == NULL)
2296 return -1;
2297 Py_DECREF(rv);
2298 }
2299 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002300}
2301
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002302static PyObject *
2303list_sizeof(PyListObject *self)
2304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 res = sizeof(PyListObject) + self->allocated * sizeof(void*);
2308 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002309}
2310
Raymond Hettinger1021c442003-11-07 15:38:09 +00002311static PyObject *list_iter(PyObject *seq);
2312static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2313
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002314PyDoc_STRVAR(getitem_doc,
2315"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002316PyDoc_STRVAR(reversed_doc,
2317"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002318PyDoc_STRVAR(sizeof_doc,
2319"L.__sizeof__() -- size of L in memory, in bytes");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002320PyDoc_STRVAR(append_doc,
2321"L.append(object) -- append object to end");
2322PyDoc_STRVAR(extend_doc,
Raymond Hettingerf8bcfb12002-12-29 05:49:09 +00002323"L.extend(iterable) -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002324PyDoc_STRVAR(insert_doc,
2325"L.insert(index, object) -- insert object before index");
2326PyDoc_STRVAR(pop_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002327"L.pop([index]) -> item -- remove and return item at index (default last).\n"
2328"Raises IndexError if list is empty or index is out of range.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002329PyDoc_STRVAR(remove_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002330"L.remove(value) -- remove first occurrence of value.\n"
2331"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002332PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002333"L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2334"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002335PyDoc_STRVAR(count_doc,
2336"L.count(value) -> integer -- return number of occurrences of value");
2337PyDoc_STRVAR(reverse_doc,
2338"L.reverse() -- reverse *IN PLACE*");
2339PyDoc_STRVAR(sort_doc,
Benjamin Petersoncb9a5512008-09-30 02:08:36 +00002340"L.sort(key=None, reverse=False) -- stable sort *IN PLACE*");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002341
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002342static PyObject *list_subscript(PyListObject*, PyObject*);
2343
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002344static PyMethodDef list_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
2346 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
2347 {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
2348 {"append", (PyCFunction)listappend, METH_O, append_doc},
2349 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
2350 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
2351 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
2352 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
2353 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
2354 {"count", (PyCFunction)listcount, METH_O, count_doc},
2355 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
2356 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
2357 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002358};
2359
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002360static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 (lenfunc)list_length, /* sq_length */
2362 (binaryfunc)list_concat, /* sq_concat */
2363 (ssizeargfunc)list_repeat, /* sq_repeat */
2364 (ssizeargfunc)list_item, /* sq_item */
2365 0, /* sq_slice */
2366 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2367 0, /* sq_ass_slice */
2368 (objobjproc)list_contains, /* sq_contains */
2369 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2370 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002371};
2372
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002373PyDoc_STRVAR(list_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002374"list() -> new empty list\n"
2375"list(iterable) -> new list initialized from iterable's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002376
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002377static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002378list_subscript(PyListObject* self, PyObject* item)
2379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 if (PyIndex_Check(item)) {
2381 Py_ssize_t i;
2382 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2383 if (i == -1 && PyErr_Occurred())
2384 return NULL;
2385 if (i < 0)
2386 i += PyList_GET_SIZE(self);
2387 return list_item(self, i);
2388 }
2389 else if (PySlice_Check(item)) {
2390 Py_ssize_t start, stop, step, slicelength, cur, i;
2391 PyObject* result;
2392 PyObject* it;
2393 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
2396 &start, &stop, &step, &slicelength) < 0) {
2397 return NULL;
2398 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 if (slicelength <= 0) {
2401 return PyList_New(0);
2402 }
2403 else if (step == 1) {
2404 return list_slice(self, start, stop);
2405 }
2406 else {
2407 result = PyList_New(slicelength);
2408 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 src = self->ob_item;
2411 dest = ((PyListObject *)result)->ob_item;
2412 for (cur = start, i = 0; i < slicelength;
2413 cur += step, i++) {
2414 it = src[cur];
2415 Py_INCREF(it);
2416 dest[i] = it;
2417 }
Tim Peters3b01a122002-07-19 02:35:45 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 return result;
2420 }
2421 }
2422 else {
2423 PyErr_Format(PyExc_TypeError,
2424 "list indices must be integers, not %.200s",
2425 item->ob_type->tp_name);
2426 return NULL;
2427 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002428}
2429
Tim Peters3b01a122002-07-19 02:35:45 +00002430static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002431list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 if (PyIndex_Check(item)) {
2434 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2435 if (i == -1 && PyErr_Occurred())
2436 return -1;
2437 if (i < 0)
2438 i += PyList_GET_SIZE(self);
2439 return list_ass_item(self, i, value);
2440 }
2441 else if (PySlice_Check(item)) {
2442 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
2445 &start, &stop, &step, &slicelength) < 0) {
2446 return -1;
2447 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 if (step == 1)
2450 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 /* Make sure s[5:2] = [..] inserts at the right place:
2453 before 5, not before 2. */
2454 if ((step < 0 && start < stop) ||
2455 (step > 0 && start > stop))
2456 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 if (value == NULL) {
2459 /* delete slice */
2460 PyObject **garbage;
2461 size_t cur;
2462 Py_ssize_t i;
Tim Peters3b01a122002-07-19 02:35:45 +00002463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 if (slicelength <= 0)
2465 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 if (step < 0) {
2468 stop = start + 1;
2469 start = stop + step*(slicelength - 1) - 1;
2470 step = -step;
2471 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 assert((size_t)slicelength <=
2474 PY_SIZE_MAX / sizeof(PyObject*));
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00002475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 garbage = (PyObject**)
2477 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2478 if (!garbage) {
2479 PyErr_NoMemory();
2480 return -1;
2481 }
Tim Peters3b01a122002-07-19 02:35:45 +00002482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 /* drawing pictures might help understand these for
2484 loops. Basically, we memmove the parts of the
2485 list that are *not* part of the slice: step-1
2486 items for each item that is part of the slice,
2487 and then tail end of the list that was not
2488 covered by the slice */
2489 for (cur = start, i = 0;
2490 cur < (size_t)stop;
2491 cur += step, i++) {
2492 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 if (cur + step >= (size_t)Py_SIZE(self)) {
2497 lim = Py_SIZE(self) - cur - 1;
2498 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 memmove(self->ob_item + cur - i,
2501 self->ob_item + cur + 1,
2502 lim * sizeof(PyObject *));
2503 }
2504 cur = start + slicelength*step;
2505 if (cur < (size_t)Py_SIZE(self)) {
2506 memmove(self->ob_item + cur - slicelength,
2507 self->ob_item + cur,
2508 (Py_SIZE(self) - cur) *
2509 sizeof(PyObject *));
2510 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 Py_SIZE(self) -= slicelength;
2513 list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 for (i = 0; i < slicelength; i++) {
2516 Py_DECREF(garbage[i]);
2517 }
2518 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 return 0;
2521 }
2522 else {
2523 /* assign slice */
2524 PyObject *ins, *seq;
2525 PyObject **garbage, **seqitems, **selfitems;
2526 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 /* protect against a[::-1] = a */
2529 if (self == (PyListObject*)value) {
2530 seq = list_slice((PyListObject*)value, 0,
2531 PyList_GET_SIZE(value));
2532 }
2533 else {
2534 seq = PySequence_Fast(value,
2535 "must assign iterable "
2536 "to extended slice");
2537 }
2538 if (!seq)
2539 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2542 PyErr_Format(PyExc_ValueError,
2543 "attempt to assign sequence of "
2544 "size %zd to extended slice of "
2545 "size %zd",
2546 PySequence_Fast_GET_SIZE(seq),
2547 slicelength);
2548 Py_DECREF(seq);
2549 return -1;
2550 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 if (!slicelength) {
2553 Py_DECREF(seq);
2554 return 0;
2555 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 garbage = (PyObject**)
2558 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2559 if (!garbage) {
2560 Py_DECREF(seq);
2561 PyErr_NoMemory();
2562 return -1;
2563 }
Tim Peters3b01a122002-07-19 02:35:45 +00002564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 selfitems = self->ob_item;
2566 seqitems = PySequence_Fast_ITEMS(seq);
2567 for (cur = start, i = 0; i < slicelength;
2568 cur += step, i++) {
2569 garbage[i] = selfitems[cur];
2570 ins = seqitems[i];
2571 Py_INCREF(ins);
2572 selfitems[cur] = ins;
2573 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 for (i = 0; i < slicelength; i++) {
2576 Py_DECREF(garbage[i]);
2577 }
Tim Peters3b01a122002-07-19 02:35:45 +00002578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 PyMem_FREE(garbage);
2580 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 return 0;
2583 }
2584 }
2585 else {
2586 PyErr_Format(PyExc_TypeError,
2587 "list indices must be integers, not %.200s",
2588 item->ob_type->tp_name);
2589 return -1;
2590 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002591}
2592
2593static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 (lenfunc)list_length,
2595 (binaryfunc)list_subscript,
2596 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002597};
2598
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002599PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2601 "list",
2602 sizeof(PyListObject),
2603 0,
2604 (destructor)list_dealloc, /* tp_dealloc */
2605 0, /* tp_print */
2606 0, /* tp_getattr */
2607 0, /* tp_setattr */
2608 0, /* tp_reserved */
2609 (reprfunc)list_repr, /* tp_repr */
2610 0, /* tp_as_number */
2611 &list_as_sequence, /* tp_as_sequence */
2612 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002613 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 0, /* tp_call */
2615 0, /* tp_str */
2616 PyObject_GenericGetAttr, /* tp_getattro */
2617 0, /* tp_setattro */
2618 0, /* tp_as_buffer */
2619 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2620 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2621 list_doc, /* tp_doc */
2622 (traverseproc)list_traverse, /* tp_traverse */
2623 (inquiry)list_clear, /* tp_clear */
2624 list_richcompare, /* tp_richcompare */
2625 0, /* tp_weaklistoffset */
2626 list_iter, /* tp_iter */
2627 0, /* tp_iternext */
2628 list_methods, /* tp_methods */
2629 0, /* tp_members */
2630 0, /* tp_getset */
2631 0, /* tp_base */
2632 0, /* tp_dict */
2633 0, /* tp_descr_get */
2634 0, /* tp_descr_set */
2635 0, /* tp_dictoffset */
2636 (initproc)list_init, /* tp_init */
2637 PyType_GenericAlloc, /* tp_alloc */
2638 PyType_GenericNew, /* tp_new */
2639 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002640};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002641
2642
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002643/*********************** List Iterator **************************/
2644
2645typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 PyObject_HEAD
2647 long it_index;
2648 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002649} listiterobject;
2650
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002651static PyObject *list_iter(PyObject *);
2652static void listiter_dealloc(listiterobject *);
2653static int listiter_traverse(listiterobject *, visitproc, void *);
2654static PyObject *listiter_next(listiterobject *);
2655static PyObject *listiter_len(listiterobject *);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002656
Armin Rigof5b3e362006-02-11 21:32:43 +00002657PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002658
2659static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
2661 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002662};
2663
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002664PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2666 "list_iterator", /* tp_name */
2667 sizeof(listiterobject), /* tp_basicsize */
2668 0, /* tp_itemsize */
2669 /* methods */
2670 (destructor)listiter_dealloc, /* tp_dealloc */
2671 0, /* tp_print */
2672 0, /* tp_getattr */
2673 0, /* tp_setattr */
2674 0, /* tp_reserved */
2675 0, /* tp_repr */
2676 0, /* tp_as_number */
2677 0, /* tp_as_sequence */
2678 0, /* tp_as_mapping */
2679 0, /* tp_hash */
2680 0, /* tp_call */
2681 0, /* tp_str */
2682 PyObject_GenericGetAttr, /* tp_getattro */
2683 0, /* tp_setattro */
2684 0, /* tp_as_buffer */
2685 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2686 0, /* tp_doc */
2687 (traverseproc)listiter_traverse, /* tp_traverse */
2688 0, /* tp_clear */
2689 0, /* tp_richcompare */
2690 0, /* tp_weaklistoffset */
2691 PyObject_SelfIter, /* tp_iter */
2692 (iternextfunc)listiter_next, /* tp_iternext */
2693 listiter_methods, /* tp_methods */
2694 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002695};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002696
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002697
2698static PyObject *
2699list_iter(PyObject *seq)
2700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 if (!PyList_Check(seq)) {
2704 PyErr_BadInternalCall();
2705 return NULL;
2706 }
2707 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2708 if (it == NULL)
2709 return NULL;
2710 it->it_index = 0;
2711 Py_INCREF(seq);
2712 it->it_seq = (PyListObject *)seq;
2713 _PyObject_GC_TRACK(it);
2714 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002715}
2716
2717static void
2718listiter_dealloc(listiterobject *it)
2719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 _PyObject_GC_UNTRACK(it);
2721 Py_XDECREF(it->it_seq);
2722 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002723}
2724
2725static int
2726listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 Py_VISIT(it->it_seq);
2729 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002730}
2731
2732static PyObject *
2733listiter_next(listiterobject *it)
2734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 PyListObject *seq;
2736 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 assert(it != NULL);
2739 seq = it->it_seq;
2740 if (seq == NULL)
2741 return NULL;
2742 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 if (it->it_index < PyList_GET_SIZE(seq)) {
2745 item = PyList_GET_ITEM(seq, it->it_index);
2746 ++it->it_index;
2747 Py_INCREF(item);
2748 return item;
2749 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 Py_DECREF(seq);
2752 it->it_seq = NULL;
2753 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002754}
2755
2756static PyObject *
2757listiter_len(listiterobject *it)
2758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 Py_ssize_t len;
2760 if (it->it_seq) {
2761 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2762 if (len >= 0)
2763 return PyLong_FromSsize_t(len);
2764 }
2765 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002766}
Raymond Hettinger1021c442003-11-07 15:38:09 +00002767/*********************** List Reverse Iterator **************************/
2768
2769typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 PyObject_HEAD
2771 Py_ssize_t it_index;
2772 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002773} listreviterobject;
2774
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002775static PyObject *list_reversed(PyListObject *, PyObject *);
2776static void listreviter_dealloc(listreviterobject *);
2777static int listreviter_traverse(listreviterobject *, visitproc, void *);
2778static PyObject *listreviter_next(listreviterobject *);
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002779static PyObject *listreviter_len(listreviterobject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002780
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002781static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
2783 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002784};
2785
Raymond Hettinger1021c442003-11-07 15:38:09 +00002786PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2788 "list_reverseiterator", /* tp_name */
2789 sizeof(listreviterobject), /* tp_basicsize */
2790 0, /* tp_itemsize */
2791 /* methods */
2792 (destructor)listreviter_dealloc, /* tp_dealloc */
2793 0, /* tp_print */
2794 0, /* tp_getattr */
2795 0, /* tp_setattr */
2796 0, /* tp_reserved */
2797 0, /* tp_repr */
2798 0, /* tp_as_number */
2799 0, /* tp_as_sequence */
2800 0, /* tp_as_mapping */
2801 0, /* tp_hash */
2802 0, /* tp_call */
2803 0, /* tp_str */
2804 PyObject_GenericGetAttr, /* tp_getattro */
2805 0, /* tp_setattro */
2806 0, /* tp_as_buffer */
2807 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2808 0, /* tp_doc */
2809 (traverseproc)listreviter_traverse, /* tp_traverse */
2810 0, /* tp_clear */
2811 0, /* tp_richcompare */
2812 0, /* tp_weaklistoffset */
2813 PyObject_SelfIter, /* tp_iter */
2814 (iternextfunc)listreviter_next, /* tp_iternext */
2815 listreviter_methods, /* tp_methods */
2816 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00002817};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002818
2819static PyObject *
2820list_reversed(PyListObject *seq, PyObject *unused)
2821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2825 if (it == NULL)
2826 return NULL;
2827 assert(PyList_Check(seq));
2828 it->it_index = PyList_GET_SIZE(seq) - 1;
2829 Py_INCREF(seq);
2830 it->it_seq = seq;
2831 PyObject_GC_Track(it);
2832 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002833}
2834
2835static void
2836listreviter_dealloc(listreviterobject *it)
2837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 PyObject_GC_UnTrack(it);
2839 Py_XDECREF(it->it_seq);
2840 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002841}
2842
2843static int
2844listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2845{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 Py_VISIT(it->it_seq);
2847 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002848}
2849
2850static PyObject *
2851listreviter_next(listreviterobject *it)
2852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 PyObject *item;
2854 Py_ssize_t index = it->it_index;
2855 PyListObject *seq = it->it_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2858 item = PyList_GET_ITEM(seq, index);
2859 it->it_index--;
2860 Py_INCREF(item);
2861 return item;
2862 }
2863 it->it_index = -1;
2864 if (seq != NULL) {
2865 it->it_seq = NULL;
2866 Py_DECREF(seq);
2867 }
2868 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002869}
2870
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002871static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002872listreviter_len(listreviterobject *it)
2873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 Py_ssize_t len = it->it_index + 1;
2875 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2876 len = 0;
2877 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002878}