blob: 049f2a8da8fd1fe37a98bbaa19dd6be7f7ac9446 [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);
2124 PyObject *v, *format_tuple, *err_string;
2125 static PyObject *err_format = NULL;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2128 _PyEval_SliceIndex, &start,
2129 _PyEval_SliceIndex, &stop))
2130 return NULL;
2131 if (start < 0) {
2132 start += Py_SIZE(self);
2133 if (start < 0)
2134 start = 0;
2135 }
2136 if (stop < 0) {
2137 stop += Py_SIZE(self);
2138 if (stop < 0)
2139 stop = 0;
2140 }
2141 for (i = start; i < stop && i < Py_SIZE(self); i++) {
2142 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2143 if (cmp > 0)
2144 return PyLong_FromSsize_t(i);
2145 else if (cmp < 0)
2146 return NULL;
2147 }
2148 if (err_format == NULL) {
2149 err_format = PyUnicode_FromString("%r is not in list");
2150 if (err_format == NULL)
2151 return NULL;
2152 }
2153 format_tuple = PyTuple_Pack(1, v);
2154 if (format_tuple == NULL)
2155 return NULL;
2156 err_string = PyUnicode_Format(err_format, format_tuple);
2157 Py_DECREF(format_tuple);
2158 if (err_string == NULL)
2159 return NULL;
2160 PyErr_SetObject(PyExc_ValueError, err_string);
2161 Py_DECREF(err_string);
2162 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002163}
2164
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002165static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002166listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 Py_ssize_t count = 0;
2169 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 for (i = 0; i < Py_SIZE(self); i++) {
2172 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2173 if (cmp > 0)
2174 count++;
2175 else if (cmp < 0)
2176 return NULL;
2177 }
2178 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002179}
2180
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002181static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002182listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 for (i = 0; i < Py_SIZE(self); i++) {
2187 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2188 if (cmp > 0) {
2189 if (list_ass_slice(self, i, i+1,
2190 (PyObject *)NULL) == 0)
2191 Py_RETURN_NONE;
2192 return NULL;
2193 }
2194 else if (cmp < 0)
2195 return NULL;
2196 }
2197 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2198 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002199}
2200
Jeremy Hylton8caad492000-06-23 14:18:11 +00002201static int
2202list_traverse(PyListObject *o, visitproc visit, void *arg)
2203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 for (i = Py_SIZE(o); --i >= 0; )
2207 Py_VISIT(o->ob_item[i]);
2208 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002209}
2210
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002211static PyObject *
2212list_richcompare(PyObject *v, PyObject *w, int op)
2213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 PyListObject *vl, *wl;
2215 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002216
Brian Curtindfc80e32011-08-10 20:28:54 -05002217 if (!PyList_Check(v) || !PyList_Check(w))
2218 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 vl = (PyListObject *)v;
2221 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2224 /* Shortcut: if the lengths differ, the lists differ */
2225 PyObject *res;
2226 if (op == Py_EQ)
2227 res = Py_False;
2228 else
2229 res = Py_True;
2230 Py_INCREF(res);
2231 return res;
2232 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 /* Search for the first index where items are different */
2235 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2236 int k = PyObject_RichCompareBool(vl->ob_item[i],
2237 wl->ob_item[i], Py_EQ);
2238 if (k < 0)
2239 return NULL;
2240 if (!k)
2241 break;
2242 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2245 /* No more items to compare -- compare sizes */
2246 Py_ssize_t vs = Py_SIZE(vl);
2247 Py_ssize_t ws = Py_SIZE(wl);
2248 int cmp;
2249 PyObject *res;
2250 switch (op) {
2251 case Py_LT: cmp = vs < ws; break;
2252 case Py_LE: cmp = vs <= ws; break;
2253 case Py_EQ: cmp = vs == ws; break;
2254 case Py_NE: cmp = vs != ws; break;
2255 case Py_GT: cmp = vs > ws; break;
2256 case Py_GE: cmp = vs >= ws; break;
2257 default: return NULL; /* cannot happen */
2258 }
2259 if (cmp)
2260 res = Py_True;
2261 else
2262 res = Py_False;
2263 Py_INCREF(res);
2264 return res;
2265 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 /* We have an item that differs -- shortcuts for EQ/NE */
2268 if (op == Py_EQ) {
2269 Py_INCREF(Py_False);
2270 return Py_False;
2271 }
2272 if (op == Py_NE) {
2273 Py_INCREF(Py_True);
2274 return Py_True;
2275 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 /* Compare the final item again using the proper operator */
2278 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002279}
2280
Tim Peters6d6c1a32001-08-02 04:15:00 +00002281static int
2282list_init(PyListObject *self, PyObject *args, PyObject *kw)
2283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 PyObject *arg = NULL;
2285 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2288 return -1;
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 /* Verify list invariants established by PyType_GenericAlloc() */
2291 assert(0 <= Py_SIZE(self));
2292 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2293 assert(self->ob_item != NULL ||
2294 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 /* Empty previous contents */
2297 if (self->ob_item != NULL) {
2298 (void)list_clear(self);
2299 }
2300 if (arg != NULL) {
2301 PyObject *rv = listextend(self, arg);
2302 if (rv == NULL)
2303 return -1;
2304 Py_DECREF(rv);
2305 }
2306 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002307}
2308
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002309static PyObject *
2310list_sizeof(PyListObject *self)
2311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 res = sizeof(PyListObject) + self->allocated * sizeof(void*);
2315 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002316}
2317
Raymond Hettinger1021c442003-11-07 15:38:09 +00002318static PyObject *list_iter(PyObject *seq);
2319static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2320
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002321PyDoc_STRVAR(getitem_doc,
2322"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002323PyDoc_STRVAR(reversed_doc,
2324"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002325PyDoc_STRVAR(sizeof_doc,
2326"L.__sizeof__() -- size of L in memory, in bytes");
Eli Benderskycbbaa962011-02-25 05:47:53 +00002327PyDoc_STRVAR(clear_doc,
2328"L.clear() -> None -- remove all items from L");
2329PyDoc_STRVAR(copy_doc,
2330"L.copy() -> list -- a shallow copy of L");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002331PyDoc_STRVAR(append_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002332"L.append(object) -> None -- append object to end");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002333PyDoc_STRVAR(extend_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002334"L.extend(iterable) -> None -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002335PyDoc_STRVAR(insert_doc,
2336"L.insert(index, object) -- insert object before index");
2337PyDoc_STRVAR(pop_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002338"L.pop([index]) -> item -- remove and return item at index (default last).\n"
2339"Raises IndexError if list is empty or index is out of range.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002340PyDoc_STRVAR(remove_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002341"L.remove(value) -> None -- remove first occurrence of value.\n"
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002342"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002343PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002344"L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2345"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002346PyDoc_STRVAR(count_doc,
2347"L.count(value) -> integer -- return number of occurrences of value");
2348PyDoc_STRVAR(reverse_doc,
2349"L.reverse() -- reverse *IN PLACE*");
2350PyDoc_STRVAR(sort_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002351"L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002352
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002353static PyObject *list_subscript(PyListObject*, PyObject*);
2354
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002355static PyMethodDef list_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
2357 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
2358 {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002359 {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc},
2360 {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 {"append", (PyCFunction)listappend, METH_O, append_doc},
2362 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002363 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
2365 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
2366 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
2367 {"count", (PyCFunction)listcount, METH_O, count_doc},
2368 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
2369 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
2370 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002371};
2372
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002373static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 (lenfunc)list_length, /* sq_length */
2375 (binaryfunc)list_concat, /* sq_concat */
2376 (ssizeargfunc)list_repeat, /* sq_repeat */
2377 (ssizeargfunc)list_item, /* sq_item */
2378 0, /* sq_slice */
2379 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2380 0, /* sq_ass_slice */
2381 (objobjproc)list_contains, /* sq_contains */
2382 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2383 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002384};
2385
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002386PyDoc_STRVAR(list_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002387"list() -> new empty list\n"
2388"list(iterable) -> new list initialized from iterable's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002389
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002390static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002391list_subscript(PyListObject* self, PyObject* item)
2392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 if (PyIndex_Check(item)) {
2394 Py_ssize_t i;
2395 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2396 if (i == -1 && PyErr_Occurred())
2397 return NULL;
2398 if (i < 0)
2399 i += PyList_GET_SIZE(self);
2400 return list_item(self, i);
2401 }
2402 else if (PySlice_Check(item)) {
2403 Py_ssize_t start, stop, step, slicelength, cur, i;
2404 PyObject* result;
2405 PyObject* it;
2406 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002407
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002408 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 &start, &stop, &step, &slicelength) < 0) {
2410 return NULL;
2411 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 if (slicelength <= 0) {
2414 return PyList_New(0);
2415 }
2416 else if (step == 1) {
2417 return list_slice(self, start, stop);
2418 }
2419 else {
2420 result = PyList_New(slicelength);
2421 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 src = self->ob_item;
2424 dest = ((PyListObject *)result)->ob_item;
2425 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002426 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 it = src[cur];
2428 Py_INCREF(it);
2429 dest[i] = it;
2430 }
Tim Peters3b01a122002-07-19 02:35:45 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 return result;
2433 }
2434 }
2435 else {
2436 PyErr_Format(PyExc_TypeError,
2437 "list indices must be integers, not %.200s",
2438 item->ob_type->tp_name);
2439 return NULL;
2440 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002441}
2442
Tim Peters3b01a122002-07-19 02:35:45 +00002443static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002444list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 if (PyIndex_Check(item)) {
2447 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2448 if (i == -1 && PyErr_Occurred())
2449 return -1;
2450 if (i < 0)
2451 i += PyList_GET_SIZE(self);
2452 return list_ass_item(self, i, value);
2453 }
2454 else if (PySlice_Check(item)) {
2455 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002456
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002457 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 &start, &stop, &step, &slicelength) < 0) {
2459 return -1;
2460 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 if (step == 1)
2463 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 /* Make sure s[5:2] = [..] inserts at the right place:
2466 before 5, not before 2. */
2467 if ((step < 0 && start < stop) ||
2468 (step > 0 && start > stop))
2469 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 if (value == NULL) {
2472 /* delete slice */
2473 PyObject **garbage;
2474 size_t cur;
2475 Py_ssize_t i;
Tim Peters3b01a122002-07-19 02:35:45 +00002476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 if (slicelength <= 0)
2478 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 if (step < 0) {
2481 stop = start + 1;
2482 start = stop + step*(slicelength - 1) - 1;
2483 step = -step;
2484 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 assert((size_t)slicelength <=
2487 PY_SIZE_MAX / sizeof(PyObject*));
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00002488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 garbage = (PyObject**)
2490 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2491 if (!garbage) {
2492 PyErr_NoMemory();
2493 return -1;
2494 }
Tim Peters3b01a122002-07-19 02:35:45 +00002495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 /* drawing pictures might help understand these for
2497 loops. Basically, we memmove the parts of the
2498 list that are *not* part of the slice: step-1
2499 items for each item that is part of the slice,
2500 and then tail end of the list that was not
2501 covered by the slice */
2502 for (cur = start, i = 0;
2503 cur < (size_t)stop;
2504 cur += step, i++) {
2505 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 if (cur + step >= (size_t)Py_SIZE(self)) {
2510 lim = Py_SIZE(self) - cur - 1;
2511 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 memmove(self->ob_item + cur - i,
2514 self->ob_item + cur + 1,
2515 lim * sizeof(PyObject *));
2516 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002517 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 if (cur < (size_t)Py_SIZE(self)) {
2519 memmove(self->ob_item + cur - slicelength,
2520 self->ob_item + cur,
2521 (Py_SIZE(self) - cur) *
2522 sizeof(PyObject *));
2523 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 Py_SIZE(self) -= slicelength;
2526 list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 for (i = 0; i < slicelength; i++) {
2529 Py_DECREF(garbage[i]);
2530 }
2531 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 return 0;
2534 }
2535 else {
2536 /* assign slice */
2537 PyObject *ins, *seq;
2538 PyObject **garbage, **seqitems, **selfitems;
2539 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 /* protect against a[::-1] = a */
2542 if (self == (PyListObject*)value) {
2543 seq = list_slice((PyListObject*)value, 0,
2544 PyList_GET_SIZE(value));
2545 }
2546 else {
2547 seq = PySequence_Fast(value,
2548 "must assign iterable "
2549 "to extended slice");
2550 }
2551 if (!seq)
2552 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2555 PyErr_Format(PyExc_ValueError,
2556 "attempt to assign sequence of "
2557 "size %zd to extended slice of "
2558 "size %zd",
2559 PySequence_Fast_GET_SIZE(seq),
2560 slicelength);
2561 Py_DECREF(seq);
2562 return -1;
2563 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 if (!slicelength) {
2566 Py_DECREF(seq);
2567 return 0;
2568 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 garbage = (PyObject**)
2571 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2572 if (!garbage) {
2573 Py_DECREF(seq);
2574 PyErr_NoMemory();
2575 return -1;
2576 }
Tim Peters3b01a122002-07-19 02:35:45 +00002577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 selfitems = self->ob_item;
2579 seqitems = PySequence_Fast_ITEMS(seq);
2580 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002581 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 garbage[i] = selfitems[cur];
2583 ins = seqitems[i];
2584 Py_INCREF(ins);
2585 selfitems[cur] = ins;
2586 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 for (i = 0; i < slicelength; i++) {
2589 Py_DECREF(garbage[i]);
2590 }
Tim Peters3b01a122002-07-19 02:35:45 +00002591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 PyMem_FREE(garbage);
2593 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 return 0;
2596 }
2597 }
2598 else {
2599 PyErr_Format(PyExc_TypeError,
2600 "list indices must be integers, not %.200s",
2601 item->ob_type->tp_name);
2602 return -1;
2603 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002604}
2605
2606static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 (lenfunc)list_length,
2608 (binaryfunc)list_subscript,
2609 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002610};
2611
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002612PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2614 "list",
2615 sizeof(PyListObject),
2616 0,
2617 (destructor)list_dealloc, /* tp_dealloc */
2618 0, /* tp_print */
2619 0, /* tp_getattr */
2620 0, /* tp_setattr */
2621 0, /* tp_reserved */
2622 (reprfunc)list_repr, /* tp_repr */
2623 0, /* tp_as_number */
2624 &list_as_sequence, /* tp_as_sequence */
2625 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002626 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 0, /* tp_call */
2628 0, /* tp_str */
2629 PyObject_GenericGetAttr, /* tp_getattro */
2630 0, /* tp_setattro */
2631 0, /* tp_as_buffer */
2632 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2633 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2634 list_doc, /* tp_doc */
2635 (traverseproc)list_traverse, /* tp_traverse */
2636 (inquiry)list_clear, /* tp_clear */
2637 list_richcompare, /* tp_richcompare */
2638 0, /* tp_weaklistoffset */
2639 list_iter, /* tp_iter */
2640 0, /* tp_iternext */
2641 list_methods, /* tp_methods */
2642 0, /* tp_members */
2643 0, /* tp_getset */
2644 0, /* tp_base */
2645 0, /* tp_dict */
2646 0, /* tp_descr_get */
2647 0, /* tp_descr_set */
2648 0, /* tp_dictoffset */
2649 (initproc)list_init, /* tp_init */
2650 PyType_GenericAlloc, /* tp_alloc */
2651 PyType_GenericNew, /* tp_new */
2652 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002653};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002654
2655
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002656/*********************** List Iterator **************************/
2657
2658typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 PyObject_HEAD
2660 long it_index;
2661 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002662} listiterobject;
2663
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002664static PyObject *list_iter(PyObject *);
2665static void listiter_dealloc(listiterobject *);
2666static int listiter_traverse(listiterobject *, visitproc, void *);
2667static PyObject *listiter_next(listiterobject *);
2668static PyObject *listiter_len(listiterobject *);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002669
Armin Rigof5b3e362006-02-11 21:32:43 +00002670PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002671
2672static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
2674 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002675};
2676
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002677PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2679 "list_iterator", /* tp_name */
2680 sizeof(listiterobject), /* tp_basicsize */
2681 0, /* tp_itemsize */
2682 /* methods */
2683 (destructor)listiter_dealloc, /* tp_dealloc */
2684 0, /* tp_print */
2685 0, /* tp_getattr */
2686 0, /* tp_setattr */
2687 0, /* tp_reserved */
2688 0, /* tp_repr */
2689 0, /* tp_as_number */
2690 0, /* tp_as_sequence */
2691 0, /* tp_as_mapping */
2692 0, /* tp_hash */
2693 0, /* tp_call */
2694 0, /* tp_str */
2695 PyObject_GenericGetAttr, /* tp_getattro */
2696 0, /* tp_setattro */
2697 0, /* tp_as_buffer */
2698 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2699 0, /* tp_doc */
2700 (traverseproc)listiter_traverse, /* tp_traverse */
2701 0, /* tp_clear */
2702 0, /* tp_richcompare */
2703 0, /* tp_weaklistoffset */
2704 PyObject_SelfIter, /* tp_iter */
2705 (iternextfunc)listiter_next, /* tp_iternext */
2706 listiter_methods, /* tp_methods */
2707 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002708};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002709
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002710
2711static PyObject *
2712list_iter(PyObject *seq)
2713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 if (!PyList_Check(seq)) {
2717 PyErr_BadInternalCall();
2718 return NULL;
2719 }
2720 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2721 if (it == NULL)
2722 return NULL;
2723 it->it_index = 0;
2724 Py_INCREF(seq);
2725 it->it_seq = (PyListObject *)seq;
2726 _PyObject_GC_TRACK(it);
2727 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002728}
2729
2730static void
2731listiter_dealloc(listiterobject *it)
2732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 _PyObject_GC_UNTRACK(it);
2734 Py_XDECREF(it->it_seq);
2735 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002736}
2737
2738static int
2739listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 Py_VISIT(it->it_seq);
2742 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002743}
2744
2745static PyObject *
2746listiter_next(listiterobject *it)
2747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 PyListObject *seq;
2749 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 assert(it != NULL);
2752 seq = it->it_seq;
2753 if (seq == NULL)
2754 return NULL;
2755 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 if (it->it_index < PyList_GET_SIZE(seq)) {
2758 item = PyList_GET_ITEM(seq, it->it_index);
2759 ++it->it_index;
2760 Py_INCREF(item);
2761 return item;
2762 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 Py_DECREF(seq);
2765 it->it_seq = NULL;
2766 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002767}
2768
2769static PyObject *
2770listiter_len(listiterobject *it)
2771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 Py_ssize_t len;
2773 if (it->it_seq) {
2774 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2775 if (len >= 0)
2776 return PyLong_FromSsize_t(len);
2777 }
2778 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002779}
Raymond Hettinger1021c442003-11-07 15:38:09 +00002780/*********************** List Reverse Iterator **************************/
2781
2782typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 PyObject_HEAD
2784 Py_ssize_t it_index;
2785 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002786} listreviterobject;
2787
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002788static PyObject *list_reversed(PyListObject *, PyObject *);
2789static void listreviter_dealloc(listreviterobject *);
2790static int listreviter_traverse(listreviterobject *, visitproc, void *);
2791static PyObject *listreviter_next(listreviterobject *);
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002792static PyObject *listreviter_len(listreviterobject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002793
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002794static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
2796 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002797};
2798
Raymond Hettinger1021c442003-11-07 15:38:09 +00002799PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2801 "list_reverseiterator", /* tp_name */
2802 sizeof(listreviterobject), /* tp_basicsize */
2803 0, /* tp_itemsize */
2804 /* methods */
2805 (destructor)listreviter_dealloc, /* tp_dealloc */
2806 0, /* tp_print */
2807 0, /* tp_getattr */
2808 0, /* tp_setattr */
2809 0, /* tp_reserved */
2810 0, /* tp_repr */
2811 0, /* tp_as_number */
2812 0, /* tp_as_sequence */
2813 0, /* tp_as_mapping */
2814 0, /* tp_hash */
2815 0, /* tp_call */
2816 0, /* tp_str */
2817 PyObject_GenericGetAttr, /* tp_getattro */
2818 0, /* tp_setattro */
2819 0, /* tp_as_buffer */
2820 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2821 0, /* tp_doc */
2822 (traverseproc)listreviter_traverse, /* tp_traverse */
2823 0, /* tp_clear */
2824 0, /* tp_richcompare */
2825 0, /* tp_weaklistoffset */
2826 PyObject_SelfIter, /* tp_iter */
2827 (iternextfunc)listreviter_next, /* tp_iternext */
2828 listreviter_methods, /* tp_methods */
2829 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00002830};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002831
2832static PyObject *
2833list_reversed(PyListObject *seq, PyObject *unused)
2834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2838 if (it == NULL)
2839 return NULL;
2840 assert(PyList_Check(seq));
2841 it->it_index = PyList_GET_SIZE(seq) - 1;
2842 Py_INCREF(seq);
2843 it->it_seq = seq;
2844 PyObject_GC_Track(it);
2845 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002846}
2847
2848static void
2849listreviter_dealloc(listreviterobject *it)
2850{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 PyObject_GC_UnTrack(it);
2852 Py_XDECREF(it->it_seq);
2853 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002854}
2855
2856static int
2857listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 Py_VISIT(it->it_seq);
2860 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002861}
2862
2863static PyObject *
2864listreviter_next(listreviterobject *it)
2865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 PyObject *item;
2867 Py_ssize_t index = it->it_index;
2868 PyListObject *seq = it->it_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2871 item = PyList_GET_ITEM(seq, index);
2872 it->it_index--;
2873 Py_INCREF(item);
2874 return item;
2875 }
2876 it->it_index = -1;
2877 if (seq != NULL) {
2878 it->it_seq = NULL;
2879 Py_DECREF(seq);
2880 }
2881 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002882}
2883
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002884static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002885listreviter_len(listreviterobject *it)
2886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 Py_ssize_t len = it->it_index + 1;
2888 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2889 len = 0;
2890 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002891}