blob: 69a632d5289d485385a36dd2b7c5adf5ee366754 [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
Ezio Melotti13925002011-03-16 11:05:33 +020014 * responsibility to overwrite them with sane values.
Tim Peters8d9eb102004-07-31 02:24:20 +000015 * 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;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +010061 if (new_allocated <= (PY_SIZE_MAX / sizeof(PyObject *)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 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;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200324 PyObject *s = NULL;
325 _PyAccu acc;
326 static PyObject *sep = NULL;
327
328 if (Py_SIZE(v) == 0) {
329 return PyUnicode_FromString("[]");
330 }
331
332 if (sep == NULL) {
333 sep = PyUnicode_FromString(", ");
334 if (sep == NULL)
335 return NULL;
336 }
Guido van Rossumfb376de1998-04-10 22:47:27 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 i = Py_ReprEnter((PyObject*)v);
339 if (i != 0) {
340 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
341 }
Tim Petersa7259592001-06-16 05:11:17 +0000342
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200343 if (_PyAccu_Init(&acc))
344 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000345
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200346 s = PyUnicode_FromString("[");
347 if (s == NULL || _PyAccu_Accumulate(&acc, s))
348 goto error;
349 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 /* Do repr() on each element. Note that this may mutate the list,
352 so must refetch the list size on each iteration. */
353 for (i = 0; i < Py_SIZE(v); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 if (Py_EnterRecursiveCall(" while getting the repr of a list"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200355 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 s = PyObject_Repr(v->ob_item[i]);
357 Py_LeaveRecursiveCall();
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200358 if (i > 0 && _PyAccu_Accumulate(&acc, sep))
359 goto error;
360 if (s == NULL || _PyAccu_Accumulate(&acc, s))
361 goto error;
362 Py_CLEAR(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 s = PyUnicode_FromString("]");
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200365 if (s == NULL || _PyAccu_Accumulate(&acc, s))
366 goto error;
367 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 Py_ReprLeave((PyObject *)v);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200370 return _PyAccu_Finish(&acc);
371
372error:
373 _PyAccu_Destroy(&acc);
374 Py_XDECREF(s);
375 Py_ReprLeave((PyObject *)v);
376 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377}
378
Martin v. Löwis18e16552006-02-15 17:27:45 +0000379static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000380list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383}
384
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000385static int
Fred Drakea2f55112000-07-09 15:16:51 +0000386list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 Py_ssize_t i;
389 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
392 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
393 Py_EQ);
394 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000395}
396
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000398list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (i < 0 || i >= Py_SIZE(a)) {
401 if (indexerr == NULL) {
402 indexerr = PyUnicode_FromString(
403 "list index out of range");
404 if (indexerr == NULL)
405 return NULL;
406 }
407 PyErr_SetObject(PyExc_IndexError, indexerr);
408 return NULL;
409 }
410 Py_INCREF(a->ob_item[i]);
411 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412}
413
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000414static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000415list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 PyListObject *np;
418 PyObject **src, **dest;
419 Py_ssize_t i, len;
420 if (ilow < 0)
421 ilow = 0;
422 else if (ilow > Py_SIZE(a))
423 ilow = Py_SIZE(a);
424 if (ihigh < ilow)
425 ihigh = ilow;
426 else if (ihigh > Py_SIZE(a))
427 ihigh = Py_SIZE(a);
428 len = ihigh - ilow;
429 np = (PyListObject *) PyList_New(len);
430 if (np == NULL)
431 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 src = a->ob_item + ilow;
434 dest = np->ob_item;
435 for (i = 0; i < len; i++) {
436 PyObject *v = src[i];
437 Py_INCREF(v);
438 dest[i] = v;
439 }
440 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441}
442
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000444PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (!PyList_Check(a)) {
447 PyErr_BadInternalCall();
448 return NULL;
449 }
450 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000451}
452
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000453static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000454list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 Py_ssize_t size;
457 Py_ssize_t i;
458 PyObject **src, **dest;
459 PyListObject *np;
460 if (!PyList_Check(bb)) {
461 PyErr_Format(PyExc_TypeError,
462 "can only concatenate list (not \"%.200s\") to list",
463 bb->ob_type->tp_name);
464 return NULL;
465 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466#define b ((PyListObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 size = Py_SIZE(a) + Py_SIZE(b);
468 if (size < 0)
469 return PyErr_NoMemory();
470 np = (PyListObject *) PyList_New(size);
471 if (np == NULL) {
472 return NULL;
473 }
474 src = a->ob_item;
475 dest = np->ob_item;
476 for (i = 0; i < Py_SIZE(a); i++) {
477 PyObject *v = src[i];
478 Py_INCREF(v);
479 dest[i] = v;
480 }
481 src = b->ob_item;
482 dest = np->ob_item + Py_SIZE(a);
483 for (i = 0; i < Py_SIZE(b); i++) {
484 PyObject *v = src[i];
485 Py_INCREF(v);
486 dest[i] = v;
487 }
488 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489#undef b
490}
491
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000492static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000493list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 Py_ssize_t i, j;
496 Py_ssize_t size;
497 PyListObject *np;
498 PyObject **p, **items;
499 PyObject *elem;
500 if (n < 0)
501 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100502 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100504 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 if (size == 0)
506 return PyList_New(0);
507 np = (PyListObject *) PyList_New(size);
508 if (np == NULL)
509 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 items = np->ob_item;
512 if (Py_SIZE(a) == 1) {
513 elem = a->ob_item[0];
514 for (i = 0; i < n; i++) {
515 items[i] = elem;
516 Py_INCREF(elem);
517 }
518 return (PyObject *) np;
519 }
520 p = np->ob_item;
521 items = a->ob_item;
522 for (i = 0; i < n; i++) {
523 for (j = 0; j < Py_SIZE(a); j++) {
524 *p = items[j];
525 Py_INCREF(*p);
526 p++;
527 }
528 }
529 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000530}
531
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532static int
Armin Rigo93677f02004-07-29 12:40:23 +0000533list_clear(PyListObject *a)
534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 Py_ssize_t i;
536 PyObject **item = a->ob_item;
537 if (item != NULL) {
538 /* Because XDECREF can recursively invoke operations on
539 this list, we make it empty first. */
540 i = Py_SIZE(a);
541 Py_SIZE(a) = 0;
542 a->ob_item = NULL;
543 a->allocated = 0;
544 while (--i >= 0) {
545 Py_XDECREF(item[i]);
546 }
547 PyMem_FREE(item);
548 }
549 /* Never fails; the return value can be ignored.
550 Note that there is no guarantee that the list is actually empty
551 at this point, because XDECREF may have populated it again! */
552 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000553}
554
Tim Peters8fc4a912004-07-31 21:53:19 +0000555/* a[ilow:ihigh] = v if v != NULL.
556 * del a[ilow:ihigh] if v == NULL.
557 *
558 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
559 * guaranteed the call cannot fail.
560 */
Armin Rigo93677f02004-07-29 12:40:23 +0000561static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000562list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 /* Because [X]DECREF can recursively invoke list operations on
565 this list, we must postpone all [X]DECREF activity until
566 after the list is back in its canonical shape. Therefore
567 we must allocate an additional array, 'recycle', into which
568 we temporarily copy the items that are deleted from the
569 list. :-( */
570 PyObject *recycle_on_stack[8];
571 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
572 PyObject **item;
573 PyObject **vitem = NULL;
574 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
575 Py_ssize_t n; /* # of elements in replacement list */
576 Py_ssize_t norig; /* # of elements in list getting replaced */
577 Py_ssize_t d; /* Change in size */
578 Py_ssize_t k;
579 size_t s;
580 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000581#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 if (v == NULL)
583 n = 0;
584 else {
585 if (a == b) {
586 /* Special case "a[i:j] = a" -- copy b first */
587 v = list_slice(b, 0, Py_SIZE(b));
588 if (v == NULL)
589 return result;
590 result = list_ass_slice(a, ilow, ihigh, v);
591 Py_DECREF(v);
592 return result;
593 }
594 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
595 if(v_as_SF == NULL)
596 goto Error;
597 n = PySequence_Fast_GET_SIZE(v_as_SF);
598 vitem = PySequence_Fast_ITEMS(v_as_SF);
599 }
600 if (ilow < 0)
601 ilow = 0;
602 else if (ilow > Py_SIZE(a))
603 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 if (ihigh < ilow)
606 ihigh = ilow;
607 else if (ihigh > Py_SIZE(a))
608 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 norig = ihigh - ilow;
611 assert(norig >= 0);
612 d = n - norig;
613 if (Py_SIZE(a) + d == 0) {
614 Py_XDECREF(v_as_SF);
615 return list_clear(a);
616 }
617 item = a->ob_item;
618 /* recycle the items that we are about to remove */
619 s = norig * sizeof(PyObject *);
620 if (s > sizeof(recycle_on_stack)) {
621 recycle = (PyObject **)PyMem_MALLOC(s);
622 if (recycle == NULL) {
623 PyErr_NoMemory();
624 goto Error;
625 }
626 }
627 memcpy(recycle, &item[ilow], s);
Tim Peters8d9eb102004-07-31 02:24:20 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 if (d < 0) { /* Delete -d items */
630 memmove(&item[ihigh+d], &item[ihigh],
631 (Py_SIZE(a) - ihigh)*sizeof(PyObject *));
632 list_resize(a, Py_SIZE(a) + d);
633 item = a->ob_item;
634 }
635 else if (d > 0) { /* Insert d items */
636 k = Py_SIZE(a);
637 if (list_resize(a, k+d) < 0)
638 goto Error;
639 item = a->ob_item;
640 memmove(&item[ihigh+d], &item[ihigh],
641 (k - ihigh)*sizeof(PyObject *));
642 }
643 for (k = 0; k < n; k++, ilow++) {
644 PyObject *w = vitem[k];
645 Py_XINCREF(w);
646 item[ilow] = w;
647 }
648 for (k = norig - 1; k >= 0; --k)
649 Py_XDECREF(recycle[k]);
650 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000651 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 if (recycle != recycle_on_stack)
653 PyMem_FREE(recycle);
654 Py_XDECREF(v_as_SF);
655 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000656#undef b
657}
658
Guido van Rossum234f9421993-06-17 12:35:49 +0000659int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000660PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 if (!PyList_Check(a)) {
663 PyErr_BadInternalCall();
664 return -1;
665 }
666 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000667}
668
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000669static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000670list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 PyObject **items;
673 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000674
675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 size = PyList_GET_SIZE(self);
677 if (size == 0 || n == 1) {
678 Py_INCREF(self);
679 return (PyObject *)self;
680 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 if (n < 1) {
683 (void)list_clear(self);
684 Py_INCREF(self);
685 return (PyObject *)self;
686 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 if (size > PY_SSIZE_T_MAX / n) {
689 return PyErr_NoMemory();
690 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 if (list_resize(self, size*n) == -1)
693 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 p = size;
696 items = self->ob_item;
697 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
698 for (j = 0; j < size; j++) {
699 PyObject *o = items[j];
700 Py_INCREF(o);
701 items[p++] = o;
702 }
703 }
704 Py_INCREF(self);
705 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000706}
707
Guido van Rossum4a450d01991-04-03 19:05:18 +0000708static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000709list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 PyObject *old_value;
712 if (i < 0 || i >= Py_SIZE(a)) {
713 PyErr_SetString(PyExc_IndexError,
714 "list assignment index out of range");
715 return -1;
716 }
717 if (v == NULL)
718 return list_ass_slice(a, i, i+1, v);
719 Py_INCREF(v);
720 old_value = a->ob_item[i];
721 a->ob_item[i] = v;
722 Py_DECREF(old_value);
723 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000724}
725
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000726static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000727listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 Py_ssize_t i;
730 PyObject *v;
731 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
732 return NULL;
733 if (ins1(self, i, v) == 0)
734 Py_RETURN_NONE;
735 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000736}
737
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000738static PyObject *
Eli Benderskycbbaa962011-02-25 05:47:53 +0000739listclear(PyListObject *self)
740{
741 list_clear(self);
742 Py_RETURN_NONE;
743}
744
745static PyObject *
746listcopy(PyListObject *self)
747{
748 return list_slice(self, 0, Py_SIZE(self));
749}
750
751static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000752listappend(PyListObject *self, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 if (app1(self, v) == 0)
755 Py_RETURN_NONE;
756 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000757}
758
Barry Warsawdedf6d61998-10-09 16:37:25 +0000759static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000760listextend(PyListObject *self, PyObject *b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 PyObject *it; /* iter(v) */
763 Py_ssize_t m; /* size of self */
764 Py_ssize_t n; /* guess for size of b */
765 Py_ssize_t mn; /* m + n */
766 Py_ssize_t i;
767 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 /* Special cases:
770 1) lists and tuples which can use PySequence_Fast ops
771 2) extending self to self requires making a copy first
772 */
773 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
774 PyObject **src, **dest;
775 b = PySequence_Fast(b, "argument must be iterable");
776 if (!b)
777 return NULL;
778 n = PySequence_Fast_GET_SIZE(b);
779 if (n == 0) {
780 /* short circuit when b is empty */
781 Py_DECREF(b);
782 Py_RETURN_NONE;
783 }
784 m = Py_SIZE(self);
785 if (list_resize(self, m + n) == -1) {
786 Py_DECREF(b);
787 return NULL;
788 }
789 /* note that we may still have self == b here for the
790 * situation a.extend(a), but the following code works
791 * in that case too. Just make sure to resize self
792 * before calling PySequence_Fast_ITEMS.
793 */
794 /* populate the end of self with b's items */
795 src = PySequence_Fast_ITEMS(b);
796 dest = self->ob_item + m;
797 for (i = 0; i < n; i++) {
798 PyObject *o = src[i];
799 Py_INCREF(o);
800 dest[i] = o;
801 }
802 Py_DECREF(b);
803 Py_RETURN_NONE;
804 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 it = PyObject_GetIter(b);
807 if (it == NULL)
808 return NULL;
809 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 /* Guess a result list size. */
812 n = _PyObject_LengthHint(b, 8);
813 if (n == -1) {
814 Py_DECREF(it);
815 return NULL;
816 }
817 m = Py_SIZE(self);
818 mn = m + n;
819 if (mn >= m) {
820 /* Make room. */
821 if (list_resize(self, mn) == -1)
822 goto error;
823 /* Make the list sane again. */
824 Py_SIZE(self) = m;
825 }
826 /* Else m + n overflowed; on the chance that n lied, and there really
827 * is enough room, ignore it. If n was telling the truth, we'll
828 * eventually run out of memory during the loop.
829 */
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 /* Run iterator to exhaustion. */
832 for (;;) {
833 PyObject *item = iternext(it);
834 if (item == NULL) {
835 if (PyErr_Occurred()) {
836 if (PyErr_ExceptionMatches(PyExc_StopIteration))
837 PyErr_Clear();
838 else
839 goto error;
840 }
841 break;
842 }
843 if (Py_SIZE(self) < self->allocated) {
844 /* steals ref */
845 PyList_SET_ITEM(self, Py_SIZE(self), item);
846 ++Py_SIZE(self);
847 }
848 else {
849 int status = app1(self, item);
850 Py_DECREF(item); /* append creates a new ref */
851 if (status < 0)
852 goto error;
853 }
854 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 /* Cut back result list if initial guess was too large. */
857 if (Py_SIZE(self) < self->allocated)
858 list_resize(self, Py_SIZE(self)); /* shrinking can't fail */
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 Py_DECREF(it);
861 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000862
863 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 Py_DECREF(it);
865 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000866}
867
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000868PyObject *
869_PyList_Extend(PyListObject *self, PyObject *b)
870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 return listextend(self, b);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000872}
873
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000874static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000875list_inplace_concat(PyListObject *self, PyObject *other)
876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 result = listextend(self, other);
880 if (result == NULL)
881 return result;
882 Py_DECREF(result);
883 Py_INCREF(self);
884 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000885}
886
887static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000888listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 Py_ssize_t i = -1;
891 PyObject *v;
892 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 if (!PyArg_ParseTuple(args, "|n:pop", &i))
895 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 if (Py_SIZE(self) == 0) {
898 /* Special-case most common failure cause */
899 PyErr_SetString(PyExc_IndexError, "pop from empty list");
900 return NULL;
901 }
902 if (i < 0)
903 i += Py_SIZE(self);
904 if (i < 0 || i >= Py_SIZE(self)) {
905 PyErr_SetString(PyExc_IndexError, "pop index out of range");
906 return NULL;
907 }
908 v = self->ob_item[i];
909 if (i == Py_SIZE(self) - 1) {
910 status = list_resize(self, Py_SIZE(self) - 1);
911 assert(status >= 0);
912 return v; /* and v now owns the reference the list had */
913 }
914 Py_INCREF(v);
915 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
916 assert(status >= 0);
917 /* Use status, so that in a release build compilers don't
918 * complain about the unused name.
919 */
920 (void) status;
Brett Cannon651dd522004-08-08 21:21:18 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000923}
924
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000925/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
926static void
927reverse_slice(PyObject **lo, PyObject **hi)
928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 --hi;
932 while (lo < hi) {
933 PyObject *t = *lo;
934 *lo = *hi;
935 *hi = t;
936 ++lo;
937 --hi;
938 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000939}
940
Tim Petersa64dc242002-08-01 02:13:36 +0000941/* Lots of code for an adaptive, stable, natural mergesort. There are many
942 * pieces to this algorithm; read listsort.txt for overviews and details.
943 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000944
Daniel Stutzbach98338222010-12-02 21:55:33 +0000945/* A sortslice contains a pointer to an array of keys and a pointer to
946 * an array of corresponding values. In other words, keys[i]
947 * corresponds with values[i]. If values == NULL, then the keys are
948 * also the values.
949 *
950 * Several convenience routines are provided here, so that keys and
951 * values are always moved in sync.
952 */
953
954typedef struct {
955 PyObject **keys;
956 PyObject **values;
957} sortslice;
958
959Py_LOCAL_INLINE(void)
960sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
961{
962 s1->keys[i] = s2->keys[j];
963 if (s1->values != NULL)
964 s1->values[i] = s2->values[j];
965}
966
967Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000968sortslice_copy_incr(sortslice *dst, sortslice *src)
969{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000970 *dst->keys++ = *src->keys++;
971 if (dst->values != NULL)
972 *dst->values++ = *src->values++;
973}
974
975Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000976sortslice_copy_decr(sortslice *dst, sortslice *src)
977{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000978 *dst->keys-- = *src->keys--;
979 if (dst->values != NULL)
980 *dst->values-- = *src->values--;
981}
982
983
984Py_LOCAL_INLINE(void)
985sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000986 Py_ssize_t n)
987{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000988 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
989 if (s1->values != NULL)
990 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
991}
992
993Py_LOCAL_INLINE(void)
994sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000995 Py_ssize_t n)
996{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000997 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
998 if (s1->values != NULL)
999 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1000}
1001
1002Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001003sortslice_advance(sortslice *slice, Py_ssize_t n)
1004{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001005 slice->keys += n;
1006 if (slice->values != NULL)
1007 slice->values += n;
1008}
1009
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001010/* Comparison function: PyObject_RichCompareBool with Py_LT.
Tim Petersa64dc242002-08-01 02:13:36 +00001011 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1012 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001013
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001014#define ISLT(X, Y) (PyObject_RichCompareBool(X, Y, Py_LT))
Tim Peters66860f62002-08-04 17:47:26 +00001015
1016/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001017 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1018 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1019*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001020#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001022
1023/* binarysort is the best method for sorting small arrays: it does
1024 few compares, but can do data movement quadratic in the number of
1025 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001026 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001027 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001028 On entry, must have lo <= start <= hi, and that [lo, start) is already
1029 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001030 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001031 Even in case of error, the output slice will be some permutation of
1032 the input (nothing is lost or duplicated).
1033*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001034static int
Daniel Stutzbach98338222010-12-02 21:55:33 +00001035binarysort(sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 register Py_ssize_t k;
1038 register PyObject **l, **p, **r;
1039 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001040
Daniel Stutzbach98338222010-12-02 21:55:33 +00001041 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001043 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 ++start;
1045 for (; start < hi; ++start) {
1046 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001047 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 r = start;
1049 pivot = *r;
1050 /* Invariants:
1051 * pivot >= all in [lo, l).
1052 * pivot < all in [r, start).
1053 * The second is vacuously true at the start.
1054 */
1055 assert(l < r);
1056 do {
1057 p = l + ((r - l) >> 1);
1058 IFLT(pivot, *p)
1059 r = p;
1060 else
1061 l = p+1;
1062 } while (l < r);
1063 assert(l == r);
1064 /* The invariants still hold, so pivot >= all in [lo, l) and
1065 pivot < all in [l, start), so pivot belongs at l. Note
1066 that if there are elements equal to pivot, l points to the
1067 first slot after them -- that's why this sort is stable.
1068 Slide over to make room.
1069 Caution: using memmove is much slower under MSVC 5;
1070 we're not usually moving many slots. */
1071 for (p = start; p > l; --p)
1072 *p = *(p-1);
1073 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001074 if (lo.values != NULL) {
1075 Py_ssize_t offset = lo.values - lo.keys;
1076 p = start + offset;
1077 pivot = *p;
1078 l += offset;
1079 for (p = start + offset; p > l; --p)
1080 *p = *(p-1);
1081 *l = pivot;
1082 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 }
1084 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001085
1086 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001088}
1089
Tim Petersa64dc242002-08-01 02:13:36 +00001090/*
1091Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1092is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001093
Tim Petersa64dc242002-08-01 02:13:36 +00001094 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001095
Tim Petersa64dc242002-08-01 02:13:36 +00001096or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001097
Tim Petersa64dc242002-08-01 02:13:36 +00001098 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001099
Tim Petersa64dc242002-08-01 02:13:36 +00001100Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1101For its intended use in a stable mergesort, the strictness of the defn of
1102"descending" is needed so that the caller can safely reverse a descending
1103sequence without violating stability (strict > ensures there are no equal
1104elements to get out of order).
1105
1106Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001107*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001108static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001109count_run(PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 Py_ssize_t k;
1112 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 assert(lo < hi);
1115 *descending = 0;
1116 ++lo;
1117 if (lo == hi)
1118 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 n = 2;
1121 IFLT(*lo, *(lo-1)) {
1122 *descending = 1;
1123 for (lo = lo+1; lo < hi; ++lo, ++n) {
1124 IFLT(*lo, *(lo-1))
1125 ;
1126 else
1127 break;
1128 }
1129 }
1130 else {
1131 for (lo = lo+1; lo < hi; ++lo, ++n) {
1132 IFLT(*lo, *(lo-1))
1133 break;
1134 }
1135 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001138fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001140}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001141
Tim Petersa64dc242002-08-01 02:13:36 +00001142/*
1143Locate the proper position of key in a sorted vector; if the vector contains
1144an element equal to key, return the position immediately to the left of
1145the leftmost equal element. [gallop_right() does the same except returns
1146the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001147
Tim Petersa64dc242002-08-01 02:13:36 +00001148"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001149
Tim Petersa64dc242002-08-01 02:13:36 +00001150"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1151hint is to the final result, the faster this runs.
1152
1153The return value is the int k in 0..n such that
1154
1155 a[k-1] < key <= a[k]
1156
1157pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1158key belongs at index k; or, IOW, the first k elements of a should precede
1159key, and the last n-k should follow key.
1160
1161Returns -1 on error. See listsort.txt for info on the method.
1162*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001163static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001164gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 Py_ssize_t ofs;
1167 Py_ssize_t lastofs;
1168 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 a += hint;
1173 lastofs = 0;
1174 ofs = 1;
1175 IFLT(*a, key) {
1176 /* a[hint] < key -- gallop right, until
1177 * a[hint + lastofs] < key <= a[hint + ofs]
1178 */
1179 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1180 while (ofs < maxofs) {
1181 IFLT(a[ofs], key) {
1182 lastofs = ofs;
1183 ofs = (ofs << 1) + 1;
1184 if (ofs <= 0) /* int overflow */
1185 ofs = maxofs;
1186 }
1187 else /* key <= a[hint + ofs] */
1188 break;
1189 }
1190 if (ofs > maxofs)
1191 ofs = maxofs;
1192 /* Translate back to offsets relative to &a[0]. */
1193 lastofs += hint;
1194 ofs += hint;
1195 }
1196 else {
1197 /* key <= a[hint] -- gallop left, until
1198 * a[hint - ofs] < key <= a[hint - lastofs]
1199 */
1200 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1201 while (ofs < maxofs) {
1202 IFLT(*(a-ofs), key)
1203 break;
1204 /* key <= a[hint - ofs] */
1205 lastofs = ofs;
1206 ofs = (ofs << 1) + 1;
1207 if (ofs <= 0) /* int overflow */
1208 ofs = maxofs;
1209 }
1210 if (ofs > maxofs)
1211 ofs = maxofs;
1212 /* Translate back to positive offsets relative to &a[0]. */
1213 k = lastofs;
1214 lastofs = hint - ofs;
1215 ofs = hint - k;
1216 }
1217 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1220 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1221 * right of lastofs but no farther right than ofs. Do a binary
1222 * search, with invariant a[lastofs-1] < key <= a[ofs].
1223 */
1224 ++lastofs;
1225 while (lastofs < ofs) {
1226 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 IFLT(a[m], key)
1229 lastofs = m+1; /* a[m] < key */
1230 else
1231 ofs = m; /* key <= a[m] */
1232 }
1233 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1234 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001235
1236fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001238}
1239
1240/*
1241Exactly like gallop_left(), except that if key already exists in a[0:n],
1242finds the position immediately to the right of the rightmost equal value.
1243
1244The return value is the int k in 0..n such that
1245
1246 a[k-1] <= key < a[k]
1247
1248or -1 if error.
1249
1250The code duplication is massive, but this is enough different given that
1251we're sticking to "<" comparisons that it's much harder to follow if
1252written as one routine with yet another "left or right?" flag.
1253*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001254static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001255gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 Py_ssize_t ofs;
1258 Py_ssize_t lastofs;
1259 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 a += hint;
1264 lastofs = 0;
1265 ofs = 1;
1266 IFLT(key, *a) {
1267 /* key < a[hint] -- gallop left, until
1268 * a[hint - ofs] <= key < a[hint - lastofs]
1269 */
1270 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1271 while (ofs < maxofs) {
1272 IFLT(key, *(a-ofs)) {
1273 lastofs = ofs;
1274 ofs = (ofs << 1) + 1;
1275 if (ofs <= 0) /* int overflow */
1276 ofs = maxofs;
1277 }
1278 else /* a[hint - ofs] <= key */
1279 break;
1280 }
1281 if (ofs > maxofs)
1282 ofs = maxofs;
1283 /* Translate back to positive offsets relative to &a[0]. */
1284 k = lastofs;
1285 lastofs = hint - ofs;
1286 ofs = hint - k;
1287 }
1288 else {
1289 /* a[hint] <= key -- gallop right, until
1290 * a[hint + lastofs] <= key < a[hint + ofs]
1291 */
1292 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1293 while (ofs < maxofs) {
1294 IFLT(key, a[ofs])
1295 break;
1296 /* a[hint + ofs] <= key */
1297 lastofs = ofs;
1298 ofs = (ofs << 1) + 1;
1299 if (ofs <= 0) /* int overflow */
1300 ofs = maxofs;
1301 }
1302 if (ofs > maxofs)
1303 ofs = maxofs;
1304 /* Translate back to offsets relative to &a[0]. */
1305 lastofs += hint;
1306 ofs += hint;
1307 }
1308 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1311 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1312 * right of lastofs but no farther right than ofs. Do a binary
1313 * search, with invariant a[lastofs-1] <= key < a[ofs].
1314 */
1315 ++lastofs;
1316 while (lastofs < ofs) {
1317 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 IFLT(key, a[m])
1320 ofs = m; /* key < a[m] */
1321 else
1322 lastofs = m+1; /* a[m] <= key */
1323 }
1324 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1325 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001326
1327fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001329}
1330
1331/* The maximum number of entries in a MergeState's pending-runs stack.
1332 * This is enough to sort arrays of size up to about
1333 * 32 * phi ** MAX_MERGE_PENDING
1334 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1335 * with 2**64 elements.
1336 */
1337#define MAX_MERGE_PENDING 85
1338
Tim Peterse05f65a2002-08-10 05:21:15 +00001339/* When we get into galloping mode, we stay there until both runs win less
1340 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
Tim Petersa64dc242002-08-01 02:13:36 +00001341 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001342#define MIN_GALLOP 7
Tim Petersa64dc242002-08-01 02:13:36 +00001343
1344/* Avoid malloc for small temp arrays. */
1345#define MERGESTATE_TEMP_SIZE 256
1346
1347/* One MergeState exists on the stack per invocation of mergesort. It's just
1348 * a convenient way to pass state around among the helper functions.
1349 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001350struct s_slice {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001351 sortslice base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 Py_ssize_t len;
Tim Peterse05f65a2002-08-10 05:21:15 +00001353};
1354
Tim Petersa64dc242002-08-01 02:13:36 +00001355typedef struct s_MergeState {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 /* This controls when we get *into* galloping mode. It's initialized
1357 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1358 * random data, and lower for highly structured data.
1359 */
1360 Py_ssize_t min_gallop;
Tim Peterse05f65a2002-08-10 05:21:15 +00001361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 /* 'a' is temp storage to help with merges. It contains room for
1363 * alloced entries.
1364 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001365 sortslice a; /* may point to temparray below */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 Py_ssize_t alloced;
Tim Petersa64dc242002-08-01 02:13:36 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 /* A stack of n pending runs yet to be merged. Run #i starts at
1369 * address base[i] and extends for len[i] elements. It's always
1370 * true (so long as the indices are in bounds) that
1371 *
1372 * pending[i].base + pending[i].len == pending[i+1].base
1373 *
1374 * so we could cut the storage for this, but it's a minor amount,
1375 * and keeping all the info explicit simplifies the code.
1376 */
1377 int n;
1378 struct s_slice pending[MAX_MERGE_PENDING];
Tim Petersa64dc242002-08-01 02:13:36 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 /* 'a' points to this when possible, rather than muck with malloc. */
1381 PyObject *temparray[MERGESTATE_TEMP_SIZE];
Tim Petersa64dc242002-08-01 02:13:36 +00001382} MergeState;
1383
1384/* Conceptually a MergeState's constructor. */
1385static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001386merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001389 if (has_keyfunc) {
1390 /* The temporary space for merging will need at most half the list
1391 * size rounded up. Use the minimum possible space so we can use the
1392 * rest of temparray for other things. In particular, if there is
1393 * enough extra space, listsort() will use it to store the keys.
1394 */
1395 ms->alloced = (list_size + 1) / 2;
1396
1397 /* ms->alloced describes how many keys will be stored at
1398 ms->temparray, but we also need to store the values. Hence,
1399 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1400 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1401 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1402 ms->a.values = &ms->temparray[ms->alloced];
1403 }
1404 else {
1405 ms->alloced = MERGESTATE_TEMP_SIZE;
1406 ms->a.values = NULL;
1407 }
1408 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 ms->n = 0;
1410 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001411}
1412
1413/* Free all the temp memory owned by the MergeState. This must be called
1414 * when you're done with a MergeState, and may be called before then if
1415 * you want to free the temp memory early.
1416 */
1417static void
1418merge_freemem(MergeState *ms)
1419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001421 if (ms->a.keys != ms->temparray)
1422 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001423}
1424
1425/* Ensure enough temp memory for 'need' array slots is available.
1426 * Returns 0 on success and -1 if the memory can't be gotten.
1427 */
1428static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001429merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001430{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001431 int multiplier;
1432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 assert(ms != NULL);
1434 if (need <= ms->alloced)
1435 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001436
1437 multiplier = ms->a.values != NULL ? 2 : 1;
1438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 /* Don't realloc! That can cost cycles to copy the old data, but
1440 * we don't care what's in the block.
1441 */
1442 merge_freemem(ms);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001443 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 PyErr_NoMemory();
1445 return -1;
1446 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001447 ms->a.keys = (PyObject**)PyMem_Malloc(multiplier * need
1448 * sizeof(PyObject *));
1449 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001451 if (ms->a.values != NULL)
1452 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 return 0;
1454 }
1455 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001457}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1459 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001460
Daniel Stutzbach98338222010-12-02 21:55:33 +00001461/* Merge the na elements starting at ssa with the nb elements starting at
1462 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1463 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1464 * should have na <= nb. See listsort.txt for more info. Return 0 if
1465 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001466 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001467static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001468merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1469 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001472 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 int result = -1; /* guilty until proved innocent */
1474 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001475
Daniel Stutzbach98338222010-12-02 21:55:33 +00001476 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1477 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 if (MERGE_GETMEM(ms, na) < 0)
1479 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001480 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1481 dest = ssa;
1482 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001483
Daniel Stutzbach98338222010-12-02 21:55:33 +00001484 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 --nb;
1486 if (nb == 0)
1487 goto Succeed;
1488 if (na == 1)
1489 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 min_gallop = ms->min_gallop;
1492 for (;;) {
1493 Py_ssize_t acount = 0; /* # of times A won in a row */
1494 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 /* Do the straightforward thing until (if ever) one run
1497 * appears to win consistently.
1498 */
1499 for (;;) {
1500 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001501 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 if (k) {
1503 if (k < 0)
1504 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001505 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 ++bcount;
1507 acount = 0;
1508 --nb;
1509 if (nb == 0)
1510 goto Succeed;
1511 if (bcount >= min_gallop)
1512 break;
1513 }
1514 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001515 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 ++acount;
1517 bcount = 0;
1518 --na;
1519 if (na == 1)
1520 goto CopyB;
1521 if (acount >= min_gallop)
1522 break;
1523 }
1524 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 /* One run is winning so consistently that galloping may
1527 * be a huge win. So try that, and continue galloping until
1528 * (if ever) neither run appears to be winning consistently
1529 * anymore.
1530 */
1531 ++min_gallop;
1532 do {
1533 assert(na > 1 && nb > 0);
1534 min_gallop -= min_gallop > 1;
1535 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001536 k = gallop_right(ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 acount = k;
1538 if (k) {
1539 if (k < 0)
1540 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001541 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1542 sortslice_advance(&dest, k);
1543 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 na -= k;
1545 if (na == 1)
1546 goto CopyB;
1547 /* na==0 is impossible now if the comparison
1548 * function is consistent, but we can't assume
1549 * that it is.
1550 */
1551 if (na == 0)
1552 goto Succeed;
1553 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001554 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 --nb;
1556 if (nb == 0)
1557 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001558
Daniel Stutzbach98338222010-12-02 21:55:33 +00001559 k = gallop_left(ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 bcount = k;
1561 if (k) {
1562 if (k < 0)
1563 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001564 sortslice_memmove(&dest, 0, &ssb, 0, k);
1565 sortslice_advance(&dest, k);
1566 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 nb -= k;
1568 if (nb == 0)
1569 goto Succeed;
1570 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001571 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 --na;
1573 if (na == 1)
1574 goto CopyB;
1575 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1576 ++min_gallop; /* penalize it for leaving galloping mode */
1577 ms->min_gallop = min_gallop;
1578 }
Tim Petersa64dc242002-08-01 02:13:36 +00001579Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001581Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001583 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001585CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001587 /* The last element of ssa belongs at the end of the merge. */
1588 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1589 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001591}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001592
Daniel Stutzbach98338222010-12-02 21:55:33 +00001593/* Merge the na elements starting at pa with the nb elements starting at
1594 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1595 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1596 * should have na >= nb. See listsort.txt for more info. Return 0 if
1597 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001598 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001599static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001600merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1601 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001604 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001607
Daniel Stutzbach98338222010-12-02 21:55:33 +00001608 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1609 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 if (MERGE_GETMEM(ms, nb) < 0)
1611 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001612 dest = ssb;
1613 sortslice_advance(&dest, nb-1);
1614 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1615 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001617 ssb.keys = ms->a.keys + nb - 1;
1618 if (ssb.values != NULL)
1619 ssb.values = ms->a.values + nb - 1;
1620 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001621
Daniel Stutzbach98338222010-12-02 21:55:33 +00001622 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 --na;
1624 if (na == 0)
1625 goto Succeed;
1626 if (nb == 1)
1627 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 min_gallop = ms->min_gallop;
1630 for (;;) {
1631 Py_ssize_t acount = 0; /* # of times A won in a row */
1632 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 /* Do the straightforward thing until (if ever) one run
1635 * appears to win consistently.
1636 */
1637 for (;;) {
1638 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001639 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 if (k) {
1641 if (k < 0)
1642 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001643 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 ++acount;
1645 bcount = 0;
1646 --na;
1647 if (na == 0)
1648 goto Succeed;
1649 if (acount >= min_gallop)
1650 break;
1651 }
1652 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001653 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 ++bcount;
1655 acount = 0;
1656 --nb;
1657 if (nb == 1)
1658 goto CopyA;
1659 if (bcount >= min_gallop)
1660 break;
1661 }
1662 }
Tim Petersa64dc242002-08-01 02:13:36 +00001663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 /* One run is winning so consistently that galloping may
1665 * be a huge win. So try that, and continue galloping until
1666 * (if ever) neither run appears to be winning consistently
1667 * anymore.
1668 */
1669 ++min_gallop;
1670 do {
1671 assert(na > 0 && nb > 1);
1672 min_gallop -= min_gallop > 1;
1673 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001674 k = gallop_right(ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 if (k < 0)
1676 goto Fail;
1677 k = na - k;
1678 acount = k;
1679 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001680 sortslice_advance(&dest, -k);
1681 sortslice_advance(&ssa, -k);
1682 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 na -= k;
1684 if (na == 0)
1685 goto Succeed;
1686 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001687 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 --nb;
1689 if (nb == 1)
1690 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001691
Daniel Stutzbach98338222010-12-02 21:55:33 +00001692 k = gallop_left(ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 if (k < 0)
1694 goto Fail;
1695 k = nb - k;
1696 bcount = k;
1697 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001698 sortslice_advance(&dest, -k);
1699 sortslice_advance(&ssb, -k);
1700 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 nb -= k;
1702 if (nb == 1)
1703 goto CopyA;
1704 /* nb==0 is impossible now if the comparison
1705 * function is consistent, but we can't assume
1706 * that it is.
1707 */
1708 if (nb == 0)
1709 goto Succeed;
1710 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001711 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 --na;
1713 if (na == 0)
1714 goto Succeed;
1715 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1716 ++min_gallop; /* penalize it for leaving galloping mode */
1717 ms->min_gallop = min_gallop;
1718 }
Tim Petersa64dc242002-08-01 02:13:36 +00001719Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001721Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001723 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001725CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001727 /* The first element of ssb belongs at the front of the merge. */
1728 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1729 sortslice_advance(&dest, -na);
1730 sortslice_advance(&ssa, -na);
1731 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001733}
1734
1735/* Merge the two runs at stack indices i and i+1.
1736 * Returns 0 on success, -1 on error.
1737 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001738static Py_ssize_t
1739merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001740{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001741 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 Py_ssize_t na, nb;
1743 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 assert(ms != NULL);
1746 assert(ms->n >= 2);
1747 assert(i >= 0);
1748 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001749
Daniel Stutzbach98338222010-12-02 21:55:33 +00001750 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001752 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 nb = ms->pending[i+1].len;
1754 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001755 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 /* Record the length of the combined runs; if i is the 3rd-last
1758 * run now, also slide over the last run (which isn't involved
1759 * in this merge). The current run i+1 goes away in any case.
1760 */
1761 ms->pending[i].len = na + nb;
1762 if (i == ms->n - 3)
1763 ms->pending[i+1] = ms->pending[i+2];
1764 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 /* Where does b start in a? Elements in a before that can be
1767 * ignored (already in place).
1768 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001769 k = gallop_right(*ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 if (k < 0)
1771 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001772 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 na -= k;
1774 if (na == 0)
1775 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 /* Where does a end in b? Elements in b after that can be
1778 * ignored (already in place).
1779 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001780 nb = gallop_left(ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 if (nb <= 0)
1782 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 /* Merge what remains of the runs, using a temp array with
1785 * min(na, nb) elements.
1786 */
1787 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001788 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001790 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001791}
1792
1793/* Examine the stack of runs waiting to be merged, merging adjacent runs
1794 * until the stack invariants are re-established:
1795 *
1796 * 1. len[-3] > len[-2] + len[-1]
1797 * 2. len[-2] > len[-1]
1798 *
1799 * See listsort.txt for more info.
1800 *
1801 * Returns 0 on success, -1 on error.
1802 */
1803static int
1804merge_collapse(MergeState *ms)
1805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 assert(ms);
1809 while (ms->n > 1) {
1810 Py_ssize_t n = ms->n - 2;
1811 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
1812 if (p[n-1].len < p[n+1].len)
1813 --n;
1814 if (merge_at(ms, n) < 0)
1815 return -1;
1816 }
1817 else if (p[n].len <= p[n+1].len) {
1818 if (merge_at(ms, n) < 0)
1819 return -1;
1820 }
1821 else
1822 break;
1823 }
1824 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001825}
1826
1827/* Regardless of invariants, merge all runs on the stack until only one
1828 * remains. This is used at the end of the mergesort.
1829 *
1830 * Returns 0 on success, -1 on error.
1831 */
1832static int
1833merge_force_collapse(MergeState *ms)
1834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 assert(ms);
1838 while (ms->n > 1) {
1839 Py_ssize_t n = ms->n - 2;
1840 if (n > 0 && p[n-1].len < p[n+1].len)
1841 --n;
1842 if (merge_at(ms, n) < 0)
1843 return -1;
1844 }
1845 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001846}
1847
1848/* Compute a good value for the minimum run length; natural runs shorter
1849 * than this are boosted artificially via binary insertion.
1850 *
1851 * If n < 64, return n (it's too small to bother with fancy stuff).
1852 * Else if n is an exact power of 2, return 32.
1853 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1854 * strictly less than, an exact power of 2.
1855 *
1856 * See listsort.txt for more info.
1857 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001858static Py_ssize_t
1859merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 assert(n >= 0);
1864 while (n >= 64) {
1865 r |= n & 1;
1866 n >>= 1;
1867 }
1868 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001869}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001870
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001871static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001872reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001873{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001874 reverse_slice(s->keys, &s->keys[n]);
1875 if (s->values != NULL)
1876 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001877}
1878
Tim Petersa64dc242002-08-01 02:13:36 +00001879/* An adaptive, stable, natural mergesort. See listsort.txt.
1880 * Returns Py_None on success, NULL on error. Even in case of error, the
1881 * list will be some permutation of its input state (nothing is lost or
1882 * duplicated).
1883 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001884static PyObject *
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001885listsort(PyListObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 Py_ssize_t nremaining;
1889 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001890 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 Py_ssize_t saved_ob_size, saved_allocated;
1892 PyObject **saved_ob_item;
1893 PyObject **final_ob_item;
1894 PyObject *result = NULL; /* guilty until proved innocent */
1895 int reverse = 0;
1896 PyObject *keyfunc = NULL;
1897 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 static char *kwlist[] = {"key", "reverse", 0};
Daniel Stutzbach98338222010-12-02 21:55:33 +00001899 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 assert(self != NULL);
1902 assert (PyList_Check(self));
1903 if (args != NULL) {
1904 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort",
1905 kwlist, &keyfunc, &reverse))
1906 return NULL;
1907 if (Py_SIZE(args) > 0) {
1908 PyErr_SetString(PyExc_TypeError,
1909 "must use keyword argument for key function");
1910 return NULL;
1911 }
1912 }
1913 if (keyfunc == Py_None)
1914 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 /* The list is temporarily made empty, so that mutations performed
1917 * by comparison functions can't affect the slice of memory we're
1918 * sorting (allowing mutations during sorting is a core-dump
1919 * factory, since ob_item may change).
1920 */
1921 saved_ob_size = Py_SIZE(self);
1922 saved_ob_item = self->ob_item;
1923 saved_allocated = self->allocated;
1924 Py_SIZE(self) = 0;
1925 self->ob_item = NULL;
1926 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00001927
Daniel Stutzbach98338222010-12-02 21:55:33 +00001928 if (keyfunc == NULL) {
1929 keys = NULL;
1930 lo.keys = saved_ob_item;
1931 lo.values = NULL;
1932 }
1933 else {
1934 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
1935 /* Leverage stack space we allocated but won't otherwise use */
1936 keys = &ms.temparray[saved_ob_size+1];
1937 else {
1938 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
1939 if (keys == NULL)
1940 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001942
1943 for (i = 0; i < saved_ob_size ; i++) {
1944 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
1945 NULL);
1946 if (keys[i] == NULL) {
1947 for (i=i-1 ; i>=0 ; i--)
1948 Py_DECREF(keys[i]);
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00001949 if (keys != &ms.temparray[saved_ob_size+1])
1950 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001951 goto keyfunc_fail;
1952 }
1953 }
1954
1955 lo.keys = keys;
1956 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001958
Daniel Stutzbach98338222010-12-02 21:55:33 +00001959 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 nremaining = saved_ob_size;
1962 if (nremaining < 2)
1963 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00001964
Benjamin Peterson05380642010-08-23 19:35:39 +00001965 /* Reverse sort stability achieved by initially reversing the list,
1966 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001967 if (reverse) {
1968 if (keys != NULL)
1969 reverse_slice(&keys[0], &keys[saved_ob_size]);
1970 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
1971 }
Benjamin Peterson05380642010-08-23 19:35:39 +00001972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 /* March over the array once, left to right, finding natural runs,
1974 * and extending short natural runs to minrun elements.
1975 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 minrun = merge_compute_minrun(nremaining);
1977 do {
1978 int descending;
1979 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00001980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 /* Identify next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001982 n = count_run(lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 if (n < 0)
1984 goto fail;
1985 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001986 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 /* If short, extend to min(minrun, nremaining). */
1988 if (n < minrun) {
1989 const Py_ssize_t force = nremaining <= minrun ?
1990 nremaining : minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001991 if (binarysort(lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 goto fail;
1993 n = force;
1994 }
1995 /* Push run onto pending-runs stack, and maybe merge. */
1996 assert(ms.n < MAX_MERGE_PENDING);
1997 ms.pending[ms.n].base = lo;
1998 ms.pending[ms.n].len = n;
1999 ++ms.n;
2000 if (merge_collapse(&ms) < 0)
2001 goto fail;
2002 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002003 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 nremaining -= n;
2005 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 if (merge_force_collapse(&ms) < 0)
2008 goto fail;
2009 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002010 assert(keys == NULL
2011 ? ms.pending[0].base.keys == saved_ob_item
2012 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002014 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002015
2016succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002018fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002019 if (keys != NULL) {
2020 for (i = 0; i < saved_ob_size; i++)
2021 Py_DECREF(keys[i]);
2022 if (keys != &ms.temparray[saved_ob_size+1])
2023 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 if (self->allocated != -1 && result != NULL) {
2027 /* The user mucked with the list during the sort,
2028 * and we don't already have another error to report.
2029 */
2030 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2031 result = NULL;
2032 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 if (reverse && saved_ob_size > 1)
2035 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002038
Daniel Stutzbach98338222010-12-02 21:55:33 +00002039keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 final_ob_item = self->ob_item;
2041 i = Py_SIZE(self);
2042 Py_SIZE(self) = saved_ob_size;
2043 self->ob_item = saved_ob_item;
2044 self->allocated = saved_allocated;
2045 if (final_ob_item != NULL) {
2046 /* we cannot use list_clear() for this because it does not
2047 guarantee that the list is really empty when it returns */
2048 while (--i >= 0) {
2049 Py_XDECREF(final_ob_item[i]);
2050 }
2051 PyMem_FREE(final_ob_item);
2052 }
2053 Py_XINCREF(result);
2054 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002055}
Tim Peters330f9e92002-07-19 07:05:44 +00002056#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002057#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002058
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002059int
Fred Drakea2f55112000-07-09 15:16:51 +00002060PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 if (v == NULL || !PyList_Check(v)) {
2063 PyErr_BadInternalCall();
2064 return -1;
2065 }
2066 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
2067 if (v == NULL)
2068 return -1;
2069 Py_DECREF(v);
2070 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002071}
2072
Guido van Rossumb86c5492001-02-12 22:06:02 +00002073static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002074listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 if (Py_SIZE(self) > 1)
2077 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2078 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002079}
2080
Guido van Rossum84c76f51990-10-30 13:32:20 +00002081int
Fred Drakea2f55112000-07-09 15:16:51 +00002082PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 if (v == NULL || !PyList_Check(v)) {
2087 PyErr_BadInternalCall();
2088 return -1;
2089 }
2090 if (Py_SIZE(self) > 1)
2091 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2092 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002093}
2094
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002095PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002096PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 PyObject *w;
2099 PyObject **p, **q;
2100 Py_ssize_t n;
2101 if (v == NULL || !PyList_Check(v)) {
2102 PyErr_BadInternalCall();
2103 return NULL;
2104 }
2105 n = Py_SIZE(v);
2106 w = PyTuple_New(n);
2107 if (w == NULL)
2108 return NULL;
2109 p = ((PyTupleObject *)w)->ob_item;
2110 q = ((PyListObject *)v)->ob_item;
2111 while (--n >= 0) {
2112 Py_INCREF(*q);
2113 *p = *q;
2114 p++;
2115 q++;
2116 }
2117 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002118}
2119
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002120static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002121listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002124 PyObject *v;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002125
Petri Lehtinenebfaabd2011-11-06 21:02:39 +02002126 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2127 _PyEval_SliceIndex, &start,
2128 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 return NULL;
2130 if (start < 0) {
2131 start += Py_SIZE(self);
2132 if (start < 0)
2133 start = 0;
2134 }
2135 if (stop < 0) {
2136 stop += Py_SIZE(self);
2137 if (stop < 0)
2138 stop = 0;
2139 }
2140 for (i = start; i < stop && i < Py_SIZE(self); i++) {
2141 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2142 if (cmp > 0)
2143 return PyLong_FromSsize_t(i);
2144 else if (cmp < 0)
2145 return NULL;
2146 }
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002147 PyErr_Format(PyExc_ValueError, "%R is not in list", v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002149}
2150
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002151static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002152listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 Py_ssize_t count = 0;
2155 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 for (i = 0; i < Py_SIZE(self); i++) {
2158 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2159 if (cmp > 0)
2160 count++;
2161 else if (cmp < 0)
2162 return NULL;
2163 }
2164 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002165}
2166
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002167static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002168listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 for (i = 0; i < Py_SIZE(self); i++) {
2173 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2174 if (cmp > 0) {
2175 if (list_ass_slice(self, i, i+1,
2176 (PyObject *)NULL) == 0)
2177 Py_RETURN_NONE;
2178 return NULL;
2179 }
2180 else if (cmp < 0)
2181 return NULL;
2182 }
2183 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2184 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002185}
2186
Jeremy Hylton8caad492000-06-23 14:18:11 +00002187static int
2188list_traverse(PyListObject *o, visitproc visit, void *arg)
2189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 for (i = Py_SIZE(o); --i >= 0; )
2193 Py_VISIT(o->ob_item[i]);
2194 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002195}
2196
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002197static PyObject *
2198list_richcompare(PyObject *v, PyObject *w, int op)
2199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 PyListObject *vl, *wl;
2201 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002202
Brian Curtindfc80e32011-08-10 20:28:54 -05002203 if (!PyList_Check(v) || !PyList_Check(w))
2204 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 vl = (PyListObject *)v;
2207 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2210 /* Shortcut: if the lengths differ, the lists differ */
2211 PyObject *res;
2212 if (op == Py_EQ)
2213 res = Py_False;
2214 else
2215 res = Py_True;
2216 Py_INCREF(res);
2217 return res;
2218 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 /* Search for the first index where items are different */
2221 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2222 int k = PyObject_RichCompareBool(vl->ob_item[i],
2223 wl->ob_item[i], Py_EQ);
2224 if (k < 0)
2225 return NULL;
2226 if (!k)
2227 break;
2228 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2231 /* No more items to compare -- compare sizes */
2232 Py_ssize_t vs = Py_SIZE(vl);
2233 Py_ssize_t ws = Py_SIZE(wl);
2234 int cmp;
2235 PyObject *res;
2236 switch (op) {
2237 case Py_LT: cmp = vs < ws; break;
2238 case Py_LE: cmp = vs <= ws; break;
2239 case Py_EQ: cmp = vs == ws; break;
2240 case Py_NE: cmp = vs != ws; break;
2241 case Py_GT: cmp = vs > ws; break;
2242 case Py_GE: cmp = vs >= ws; break;
2243 default: return NULL; /* cannot happen */
2244 }
2245 if (cmp)
2246 res = Py_True;
2247 else
2248 res = Py_False;
2249 Py_INCREF(res);
2250 return res;
2251 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 /* We have an item that differs -- shortcuts for EQ/NE */
2254 if (op == Py_EQ) {
2255 Py_INCREF(Py_False);
2256 return Py_False;
2257 }
2258 if (op == Py_NE) {
2259 Py_INCREF(Py_True);
2260 return Py_True;
2261 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 /* Compare the final item again using the proper operator */
2264 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002265}
2266
Tim Peters6d6c1a32001-08-02 04:15:00 +00002267static int
2268list_init(PyListObject *self, PyObject *args, PyObject *kw)
2269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 PyObject *arg = NULL;
2271 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2274 return -1;
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 /* Verify list invariants established by PyType_GenericAlloc() */
2277 assert(0 <= Py_SIZE(self));
2278 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2279 assert(self->ob_item != NULL ||
2280 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 /* Empty previous contents */
2283 if (self->ob_item != NULL) {
2284 (void)list_clear(self);
2285 }
2286 if (arg != NULL) {
2287 PyObject *rv = listextend(self, arg);
2288 if (rv == NULL)
2289 return -1;
2290 Py_DECREF(rv);
2291 }
2292 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002293}
2294
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002295static PyObject *
2296list_sizeof(PyListObject *self)
2297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 res = sizeof(PyListObject) + self->allocated * sizeof(void*);
2301 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002302}
2303
Raymond Hettinger1021c442003-11-07 15:38:09 +00002304static PyObject *list_iter(PyObject *seq);
2305static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2306
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002307PyDoc_STRVAR(getitem_doc,
2308"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002309PyDoc_STRVAR(reversed_doc,
2310"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002311PyDoc_STRVAR(sizeof_doc,
2312"L.__sizeof__() -- size of L in memory, in bytes");
Eli Benderskycbbaa962011-02-25 05:47:53 +00002313PyDoc_STRVAR(clear_doc,
2314"L.clear() -> None -- remove all items from L");
2315PyDoc_STRVAR(copy_doc,
2316"L.copy() -> list -- a shallow copy of L");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002317PyDoc_STRVAR(append_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002318"L.append(object) -> None -- append object to end");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002319PyDoc_STRVAR(extend_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002320"L.extend(iterable) -> None -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002321PyDoc_STRVAR(insert_doc,
2322"L.insert(index, object) -- insert object before index");
2323PyDoc_STRVAR(pop_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002324"L.pop([index]) -> item -- remove and return item at index (default last).\n"
2325"Raises IndexError if list is empty or index is out of range.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002326PyDoc_STRVAR(remove_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002327"L.remove(value) -> None -- remove first occurrence of value.\n"
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002328"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002329PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002330"L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2331"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002332PyDoc_STRVAR(count_doc,
2333"L.count(value) -> integer -- return number of occurrences of value");
2334PyDoc_STRVAR(reverse_doc,
2335"L.reverse() -- reverse *IN PLACE*");
2336PyDoc_STRVAR(sort_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002337"L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002338
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002339static PyObject *list_subscript(PyListObject*, PyObject*);
2340
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002341static PyMethodDef list_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
2343 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
2344 {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002345 {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc},
2346 {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 {"append", (PyCFunction)listappend, METH_O, append_doc},
2348 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002349 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
2351 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
2352 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
2353 {"count", (PyCFunction)listcount, METH_O, count_doc},
2354 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
2355 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
2356 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002357};
2358
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002359static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 (lenfunc)list_length, /* sq_length */
2361 (binaryfunc)list_concat, /* sq_concat */
2362 (ssizeargfunc)list_repeat, /* sq_repeat */
2363 (ssizeargfunc)list_item, /* sq_item */
2364 0, /* sq_slice */
2365 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2366 0, /* sq_ass_slice */
2367 (objobjproc)list_contains, /* sq_contains */
2368 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2369 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002370};
2371
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002372PyDoc_STRVAR(list_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002373"list() -> new empty list\n"
2374"list(iterable) -> new list initialized from iterable's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002375
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002376static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002377list_subscript(PyListObject* self, PyObject* item)
2378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 if (PyIndex_Check(item)) {
2380 Py_ssize_t i;
2381 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2382 if (i == -1 && PyErr_Occurred())
2383 return NULL;
2384 if (i < 0)
2385 i += PyList_GET_SIZE(self);
2386 return list_item(self, i);
2387 }
2388 else if (PySlice_Check(item)) {
2389 Py_ssize_t start, stop, step, slicelength, cur, i;
2390 PyObject* result;
2391 PyObject* it;
2392 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002393
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002394 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 &start, &stop, &step, &slicelength) < 0) {
2396 return NULL;
2397 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 if (slicelength <= 0) {
2400 return PyList_New(0);
2401 }
2402 else if (step == 1) {
2403 return list_slice(self, start, stop);
2404 }
2405 else {
2406 result = PyList_New(slicelength);
2407 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 src = self->ob_item;
2410 dest = ((PyListObject *)result)->ob_item;
2411 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002412 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 it = src[cur];
2414 Py_INCREF(it);
2415 dest[i] = it;
2416 }
Tim Peters3b01a122002-07-19 02:35:45 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 return result;
2419 }
2420 }
2421 else {
2422 PyErr_Format(PyExc_TypeError,
2423 "list indices must be integers, not %.200s",
2424 item->ob_type->tp_name);
2425 return NULL;
2426 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002427}
2428
Tim Peters3b01a122002-07-19 02:35:45 +00002429static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002430list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 if (PyIndex_Check(item)) {
2433 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2434 if (i == -1 && PyErr_Occurred())
2435 return -1;
2436 if (i < 0)
2437 i += PyList_GET_SIZE(self);
2438 return list_ass_item(self, i, value);
2439 }
2440 else if (PySlice_Check(item)) {
2441 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002442
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002443 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 &start, &stop, &step, &slicelength) < 0) {
2445 return -1;
2446 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 if (step == 1)
2449 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 /* Make sure s[5:2] = [..] inserts at the right place:
2452 before 5, not before 2. */
2453 if ((step < 0 && start < stop) ||
2454 (step > 0 && start > stop))
2455 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 if (value == NULL) {
2458 /* delete slice */
2459 PyObject **garbage;
2460 size_t cur;
2461 Py_ssize_t i;
Tim Peters3b01a122002-07-19 02:35:45 +00002462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 if (slicelength <= 0)
2464 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 if (step < 0) {
2467 stop = start + 1;
2468 start = stop + step*(slicelength - 1) - 1;
2469 step = -step;
2470 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 assert((size_t)slicelength <=
2473 PY_SIZE_MAX / sizeof(PyObject*));
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00002474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 garbage = (PyObject**)
2476 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2477 if (!garbage) {
2478 PyErr_NoMemory();
2479 return -1;
2480 }
Tim Peters3b01a122002-07-19 02:35:45 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 /* drawing pictures might help understand these for
2483 loops. Basically, we memmove the parts of the
2484 list that are *not* part of the slice: step-1
2485 items for each item that is part of the slice,
2486 and then tail end of the list that was not
2487 covered by the slice */
2488 for (cur = start, i = 0;
2489 cur < (size_t)stop;
2490 cur += step, i++) {
2491 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 if (cur + step >= (size_t)Py_SIZE(self)) {
2496 lim = Py_SIZE(self) - cur - 1;
2497 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 memmove(self->ob_item + cur - i,
2500 self->ob_item + cur + 1,
2501 lim * sizeof(PyObject *));
2502 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002503 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 if (cur < (size_t)Py_SIZE(self)) {
2505 memmove(self->ob_item + cur - slicelength,
2506 self->ob_item + cur,
2507 (Py_SIZE(self) - cur) *
2508 sizeof(PyObject *));
2509 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 Py_SIZE(self) -= slicelength;
2512 list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 for (i = 0; i < slicelength; i++) {
2515 Py_DECREF(garbage[i]);
2516 }
2517 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 return 0;
2520 }
2521 else {
2522 /* assign slice */
2523 PyObject *ins, *seq;
2524 PyObject **garbage, **seqitems, **selfitems;
2525 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 /* protect against a[::-1] = a */
2528 if (self == (PyListObject*)value) {
2529 seq = list_slice((PyListObject*)value, 0,
2530 PyList_GET_SIZE(value));
2531 }
2532 else {
2533 seq = PySequence_Fast(value,
2534 "must assign iterable "
2535 "to extended slice");
2536 }
2537 if (!seq)
2538 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2541 PyErr_Format(PyExc_ValueError,
2542 "attempt to assign sequence of "
2543 "size %zd to extended slice of "
2544 "size %zd",
2545 PySequence_Fast_GET_SIZE(seq),
2546 slicelength);
2547 Py_DECREF(seq);
2548 return -1;
2549 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 if (!slicelength) {
2552 Py_DECREF(seq);
2553 return 0;
2554 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 garbage = (PyObject**)
2557 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2558 if (!garbage) {
2559 Py_DECREF(seq);
2560 PyErr_NoMemory();
2561 return -1;
2562 }
Tim Peters3b01a122002-07-19 02:35:45 +00002563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 selfitems = self->ob_item;
2565 seqitems = PySequence_Fast_ITEMS(seq);
2566 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002567 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 garbage[i] = selfitems[cur];
2569 ins = seqitems[i];
2570 Py_INCREF(ins);
2571 selfitems[cur] = ins;
2572 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 for (i = 0; i < slicelength; i++) {
2575 Py_DECREF(garbage[i]);
2576 }
Tim Peters3b01a122002-07-19 02:35:45 +00002577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 PyMem_FREE(garbage);
2579 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002581 return 0;
2582 }
2583 }
2584 else {
2585 PyErr_Format(PyExc_TypeError,
2586 "list indices must be integers, not %.200s",
2587 item->ob_type->tp_name);
2588 return -1;
2589 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002590}
2591
2592static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 (lenfunc)list_length,
2594 (binaryfunc)list_subscript,
2595 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002596};
2597
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002598PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2600 "list",
2601 sizeof(PyListObject),
2602 0,
2603 (destructor)list_dealloc, /* tp_dealloc */
2604 0, /* tp_print */
2605 0, /* tp_getattr */
2606 0, /* tp_setattr */
2607 0, /* tp_reserved */
2608 (reprfunc)list_repr, /* tp_repr */
2609 0, /* tp_as_number */
2610 &list_as_sequence, /* tp_as_sequence */
2611 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002612 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 0, /* tp_call */
2614 0, /* tp_str */
2615 PyObject_GenericGetAttr, /* tp_getattro */
2616 0, /* tp_setattro */
2617 0, /* tp_as_buffer */
2618 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2619 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2620 list_doc, /* tp_doc */
2621 (traverseproc)list_traverse, /* tp_traverse */
2622 (inquiry)list_clear, /* tp_clear */
2623 list_richcompare, /* tp_richcompare */
2624 0, /* tp_weaklistoffset */
2625 list_iter, /* tp_iter */
2626 0, /* tp_iternext */
2627 list_methods, /* tp_methods */
2628 0, /* tp_members */
2629 0, /* tp_getset */
2630 0, /* tp_base */
2631 0, /* tp_dict */
2632 0, /* tp_descr_get */
2633 0, /* tp_descr_set */
2634 0, /* tp_dictoffset */
2635 (initproc)list_init, /* tp_init */
2636 PyType_GenericAlloc, /* tp_alloc */
2637 PyType_GenericNew, /* tp_new */
2638 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002639};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002640
2641
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002642/*********************** List Iterator **************************/
2643
2644typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 PyObject_HEAD
2646 long it_index;
2647 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002648} listiterobject;
2649
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002650static PyObject *list_iter(PyObject *);
2651static void listiter_dealloc(listiterobject *);
2652static int listiter_traverse(listiterobject *, visitproc, void *);
2653static PyObject *listiter_next(listiterobject *);
2654static PyObject *listiter_len(listiterobject *);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002655
Armin Rigof5b3e362006-02-11 21:32:43 +00002656PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002657
2658static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
2660 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002661};
2662
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002663PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2665 "list_iterator", /* tp_name */
2666 sizeof(listiterobject), /* tp_basicsize */
2667 0, /* tp_itemsize */
2668 /* methods */
2669 (destructor)listiter_dealloc, /* tp_dealloc */
2670 0, /* tp_print */
2671 0, /* tp_getattr */
2672 0, /* tp_setattr */
2673 0, /* tp_reserved */
2674 0, /* tp_repr */
2675 0, /* tp_as_number */
2676 0, /* tp_as_sequence */
2677 0, /* tp_as_mapping */
2678 0, /* tp_hash */
2679 0, /* tp_call */
2680 0, /* tp_str */
2681 PyObject_GenericGetAttr, /* tp_getattro */
2682 0, /* tp_setattro */
2683 0, /* tp_as_buffer */
2684 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2685 0, /* tp_doc */
2686 (traverseproc)listiter_traverse, /* tp_traverse */
2687 0, /* tp_clear */
2688 0, /* tp_richcompare */
2689 0, /* tp_weaklistoffset */
2690 PyObject_SelfIter, /* tp_iter */
2691 (iternextfunc)listiter_next, /* tp_iternext */
2692 listiter_methods, /* tp_methods */
2693 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002694};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002695
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002696
2697static PyObject *
2698list_iter(PyObject *seq)
2699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 if (!PyList_Check(seq)) {
2703 PyErr_BadInternalCall();
2704 return NULL;
2705 }
2706 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2707 if (it == NULL)
2708 return NULL;
2709 it->it_index = 0;
2710 Py_INCREF(seq);
2711 it->it_seq = (PyListObject *)seq;
2712 _PyObject_GC_TRACK(it);
2713 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002714}
2715
2716static void
2717listiter_dealloc(listiterobject *it)
2718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 _PyObject_GC_UNTRACK(it);
2720 Py_XDECREF(it->it_seq);
2721 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002722}
2723
2724static int
2725listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 Py_VISIT(it->it_seq);
2728 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002729}
2730
2731static PyObject *
2732listiter_next(listiterobject *it)
2733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 PyListObject *seq;
2735 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 assert(it != NULL);
2738 seq = it->it_seq;
2739 if (seq == NULL)
2740 return NULL;
2741 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 if (it->it_index < PyList_GET_SIZE(seq)) {
2744 item = PyList_GET_ITEM(seq, it->it_index);
2745 ++it->it_index;
2746 Py_INCREF(item);
2747 return item;
2748 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 Py_DECREF(seq);
2751 it->it_seq = NULL;
2752 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002753}
2754
2755static PyObject *
2756listiter_len(listiterobject *it)
2757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 Py_ssize_t len;
2759 if (it->it_seq) {
2760 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2761 if (len >= 0)
2762 return PyLong_FromSsize_t(len);
2763 }
2764 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002765}
Raymond Hettinger1021c442003-11-07 15:38:09 +00002766/*********************** List Reverse Iterator **************************/
2767
2768typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 PyObject_HEAD
2770 Py_ssize_t it_index;
2771 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002772} listreviterobject;
2773
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002774static PyObject *list_reversed(PyListObject *, PyObject *);
2775static void listreviter_dealloc(listreviterobject *);
2776static int listreviter_traverse(listreviterobject *, visitproc, void *);
2777static PyObject *listreviter_next(listreviterobject *);
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002778static PyObject *listreviter_len(listreviterobject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002779
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002780static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
2782 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002783};
2784
Raymond Hettinger1021c442003-11-07 15:38:09 +00002785PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2787 "list_reverseiterator", /* tp_name */
2788 sizeof(listreviterobject), /* tp_basicsize */
2789 0, /* tp_itemsize */
2790 /* methods */
2791 (destructor)listreviter_dealloc, /* tp_dealloc */
2792 0, /* tp_print */
2793 0, /* tp_getattr */
2794 0, /* tp_setattr */
2795 0, /* tp_reserved */
2796 0, /* tp_repr */
2797 0, /* tp_as_number */
2798 0, /* tp_as_sequence */
2799 0, /* tp_as_mapping */
2800 0, /* tp_hash */
2801 0, /* tp_call */
2802 0, /* tp_str */
2803 PyObject_GenericGetAttr, /* tp_getattro */
2804 0, /* tp_setattro */
2805 0, /* tp_as_buffer */
2806 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2807 0, /* tp_doc */
2808 (traverseproc)listreviter_traverse, /* tp_traverse */
2809 0, /* tp_clear */
2810 0, /* tp_richcompare */
2811 0, /* tp_weaklistoffset */
2812 PyObject_SelfIter, /* tp_iter */
2813 (iternextfunc)listreviter_next, /* tp_iternext */
2814 listreviter_methods, /* tp_methods */
2815 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00002816};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002817
2818static PyObject *
2819list_reversed(PyListObject *seq, PyObject *unused)
2820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2824 if (it == NULL)
2825 return NULL;
2826 assert(PyList_Check(seq));
2827 it->it_index = PyList_GET_SIZE(seq) - 1;
2828 Py_INCREF(seq);
2829 it->it_seq = seq;
2830 PyObject_GC_Track(it);
2831 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002832}
2833
2834static void
2835listreviter_dealloc(listreviterobject *it)
2836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 PyObject_GC_UnTrack(it);
2838 Py_XDECREF(it->it_seq);
2839 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002840}
2841
2842static int
2843listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 Py_VISIT(it->it_seq);
2846 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002847}
2848
2849static PyObject *
2850listreviter_next(listreviterobject *it)
2851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 PyObject *item;
2853 Py_ssize_t index = it->it_index;
2854 PyListObject *seq = it->it_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2857 item = PyList_GET_ITEM(seq, index);
2858 it->it_index--;
2859 Py_INCREF(item);
2860 return item;
2861 }
2862 it->it_index = -1;
2863 if (seq != NULL) {
2864 it->it_seq = NULL;
2865 Py_DECREF(seq);
2866 }
2867 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002868}
2869
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002870static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002871listreviter_len(listreviterobject *it)
2872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 Py_ssize_t len = it->it_index + 1;
2874 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2875 len = 0;
2876 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002877}