blob: e59c9b1b63513b93e78c27be52cf6f623bd1c649 [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"
Antoine Pitrou0197ff92012-03-22 14:38:16 +01004#include "accu.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00005
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00006#ifdef STDC_HEADERS
7#include <stddef.h>
8#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00009#include <sys/types.h> /* For size_t */
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000010#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011
Tim Peters8d9eb102004-07-31 02:24:20 +000012/* Ensure ob_item has room for at least newsize elements, and set
13 * ob_size to newsize. If newsize > ob_size on entry, the content
14 * of the new slots at exit is undefined heap trash; it's the caller's
Ezio Melotti13925002011-03-16 11:05:33 +020015 * responsibility to overwrite them with sane values.
Tim Peters8d9eb102004-07-31 02:24:20 +000016 * The number of allocated elements may grow, shrink, or stay the same.
17 * Failure is impossible if newsize <= self.allocated on entry, although
18 * that partly relies on an assumption that the system realloc() never
19 * fails when passed a number of bytes <= the number of bytes last
20 * allocated (the C standard doesn't guarantee this, but it's hard to
21 * imagine a realloc implementation where it wouldn't be true).
22 * Note that self->ob_item may change, and even if newsize is less
23 * than ob_size on entry.
24 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000025static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000026list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 PyObject **items;
29 size_t new_allocated;
30 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 /* Bypass realloc() when a previous overallocation is large enough
33 to accommodate the newsize. If the newsize falls lower than half
34 the allocated size, then proceed with the realloc() to shrink the list.
35 */
36 if (allocated >= newsize && newsize >= (allocated >> 1)) {
37 assert(self->ob_item != NULL || newsize == 0);
38 Py_SIZE(self) = newsize;
39 return 0;
40 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 /* This over-allocates proportional to the list size, making room
43 * for additional growth. The over-allocation is mild, but is
44 * enough to give linear-time amortized behavior over a long
45 * sequence of appends() in the presence of a poorly-performing
46 * system realloc().
47 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
48 */
49 new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 /* check for integer overflow */
52 if (new_allocated > PY_SIZE_MAX - newsize) {
53 PyErr_NoMemory();
54 return -1;
55 } else {
56 new_allocated += newsize;
57 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 if (newsize == 0)
60 new_allocated = 0;
61 items = self->ob_item;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +010062 if (new_allocated <= (PY_SIZE_MAX / sizeof(PyObject *)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 PyMem_RESIZE(items, PyObject *, new_allocated);
64 else
65 items = NULL;
66 if (items == NULL) {
67 PyErr_NoMemory();
68 return -1;
69 }
70 self->ob_item = items;
71 Py_SIZE(self) = newsize;
72 self->allocated = new_allocated;
73 return 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000074}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000075
Christian Heimes77c02eb2008-02-09 02:18:51 +000076/* Debug statistic to compare allocations with reuse through the free list */
77#undef SHOW_ALLOC_COUNT
78#ifdef SHOW_ALLOC_COUNT
79static size_t count_alloc = 0;
80static size_t count_reuse = 0;
81
82static void
83show_alloc(void)
84{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
86 count_alloc);
87 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
88 "d\n", count_reuse);
89 fprintf(stderr, "%.2f%% reuse rate\n\n",
90 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +000091}
92#endif
93
Raymond Hettinger0468e412004-05-05 05:37:53 +000094/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +000095#ifndef PyList_MAXFREELIST
96#define PyList_MAXFREELIST 80
97#endif
98static PyListObject *free_list[PyList_MAXFREELIST];
99static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000100
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100101int
102PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100105 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 while (numfree) {
107 op = free_list[--numfree];
108 assert(PyList_CheckExact(op));
109 PyObject_GC_Del(op);
110 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100111 return ret;
112}
113
114void
115PyList_Fini(void)
116{
117 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000118}
119
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000121PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 PyListObject *op;
124 size_t nbytes;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000125#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 static int initialized = 0;
127 if (!initialized) {
128 Py_AtExit(show_alloc);
129 initialized = 1;
130 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000131#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 if (size < 0) {
134 PyErr_BadInternalCall();
135 return NULL;
136 }
137 /* Check for overflow without an actual overflow,
138 * which can cause compiler to optimise out */
139 if ((size_t)size > PY_SIZE_MAX / sizeof(PyObject *))
140 return PyErr_NoMemory();
141 nbytes = size * sizeof(PyObject *);
142 if (numfree) {
143 numfree--;
144 op = free_list[numfree];
145 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000146#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000148#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 } else {
150 op = PyObject_GC_New(PyListObject, &PyList_Type);
151 if (op == NULL)
152 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000153#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000155#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 }
157 if (size <= 0)
158 op->ob_item = NULL;
159 else {
160 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
161 if (op->ob_item == NULL) {
162 Py_DECREF(op);
163 return PyErr_NoMemory();
164 }
165 memset(op->ob_item, 0, nbytes);
166 }
167 Py_SIZE(op) = size;
168 op->allocated = size;
169 _PyObject_GC_TRACK(op);
170 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171}
172
Martin v. Löwis18e16552006-02-15 17:27:45 +0000173Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000174PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 if (!PyList_Check(op)) {
177 PyErr_BadInternalCall();
178 return -1;
179 }
180 else
181 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182}
183
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000184static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000185
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000186PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000187PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 if (!PyList_Check(op)) {
190 PyErr_BadInternalCall();
191 return NULL;
192 }
193 if (i < 0 || i >= Py_SIZE(op)) {
194 if (indexerr == NULL) {
195 indexerr = PyUnicode_FromString(
196 "list index out of range");
197 if (indexerr == NULL)
198 return NULL;
199 }
200 PyErr_SetObject(PyExc_IndexError, indexerr);
201 return NULL;
202 }
203 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204}
205
206int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000207PyList_SetItem(register PyObject *op, register Py_ssize_t i,
Fred Drakea2f55112000-07-09 15:16:51 +0000208 register PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 register PyObject *olditem;
211 register PyObject **p;
212 if (!PyList_Check(op)) {
213 Py_XDECREF(newitem);
214 PyErr_BadInternalCall();
215 return -1;
216 }
217 if (i < 0 || i >= Py_SIZE(op)) {
218 Py_XDECREF(newitem);
219 PyErr_SetString(PyExc_IndexError,
220 "list assignment index out of range");
221 return -1;
222 }
223 p = ((PyListObject *)op) -> ob_item + i;
224 olditem = *p;
225 *p = newitem;
226 Py_XDECREF(olditem);
227 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228}
229
230static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000231ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 Py_ssize_t i, n = Py_SIZE(self);
234 PyObject **items;
235 if (v == NULL) {
236 PyErr_BadInternalCall();
237 return -1;
238 }
239 if (n == PY_SSIZE_T_MAX) {
240 PyErr_SetString(PyExc_OverflowError,
241 "cannot add more objects to list");
242 return -1;
243 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if (list_resize(self, n+1) == -1)
246 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (where < 0) {
249 where += n;
250 if (where < 0)
251 where = 0;
252 }
253 if (where > n)
254 where = n;
255 items = self->ob_item;
256 for (i = n; --i >= where; )
257 items[i+1] = items[i];
258 Py_INCREF(v);
259 items[where] = v;
260 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261}
262
263int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000264PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 if (!PyList_Check(op)) {
267 PyErr_BadInternalCall();
268 return -1;
269 }
270 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271}
272
Raymond Hettinger40a03822004-04-12 13:05:09 +0000273static int
274app1(PyListObject *self, PyObject *v)
275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 assert (v != NULL);
279 if (n == PY_SSIZE_T_MAX) {
280 PyErr_SetString(PyExc_OverflowError,
281 "cannot add more objects to list");
282 return -1;
283 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (list_resize(self, n+1) == -1)
286 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_INCREF(v);
289 PyList_SET_ITEM(self, n, v);
290 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000291}
292
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000293int
Fred Drakea2f55112000-07-09 15:16:51 +0000294PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 if (PyList_Check(op) && (newitem != NULL))
297 return app1((PyListObject *)op, newitem);
298 PyErr_BadInternalCall();
299 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300}
301
302/* Methods */
303
304static void
Fred Drakea2f55112000-07-09 15:16:51 +0000305list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 Py_ssize_t i;
308 PyObject_GC_UnTrack(op);
309 Py_TRASHCAN_SAFE_BEGIN(op)
310 if (op->ob_item != NULL) {
311 /* Do it backwards, for Christian Tismer.
312 There's a simple test case where somehow this reduces
313 thrashing when a *very* large list is created and
314 immediately deleted. */
315 i = Py_SIZE(op);
316 while (--i >= 0) {
317 Py_XDECREF(op->ob_item[i]);
318 }
319 PyMem_FREE(op->ob_item);
320 }
321 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
322 free_list[numfree++] = op;
323 else
324 Py_TYPE(op)->tp_free((PyObject *)op);
325 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326}
327
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000328static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000329list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 Py_ssize_t i;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200332 PyObject *s = NULL;
333 _PyAccu acc;
334 static PyObject *sep = NULL;
335
336 if (Py_SIZE(v) == 0) {
337 return PyUnicode_FromString("[]");
338 }
339
340 if (sep == NULL) {
341 sep = PyUnicode_FromString(", ");
342 if (sep == NULL)
343 return NULL;
344 }
Guido van Rossumfb376de1998-04-10 22:47:27 +0000345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 i = Py_ReprEnter((PyObject*)v);
347 if (i != 0) {
348 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
349 }
Tim Petersa7259592001-06-16 05:11:17 +0000350
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200351 if (_PyAccu_Init(&acc))
352 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000353
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200354 s = PyUnicode_FromString("[");
355 if (s == NULL || _PyAccu_Accumulate(&acc, s))
356 goto error;
357 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 /* Do repr() on each element. Note that this may mutate the list,
360 so must refetch the list size on each iteration. */
361 for (i = 0; i < Py_SIZE(v); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 if (Py_EnterRecursiveCall(" while getting the repr of a list"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200363 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 s = PyObject_Repr(v->ob_item[i]);
365 Py_LeaveRecursiveCall();
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200366 if (i > 0 && _PyAccu_Accumulate(&acc, sep))
367 goto error;
368 if (s == NULL || _PyAccu_Accumulate(&acc, s))
369 goto error;
370 Py_CLEAR(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 s = PyUnicode_FromString("]");
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200373 if (s == NULL || _PyAccu_Accumulate(&acc, s))
374 goto error;
375 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 Py_ReprLeave((PyObject *)v);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200378 return _PyAccu_Finish(&acc);
379
380error:
381 _PyAccu_Destroy(&acc);
382 Py_XDECREF(s);
383 Py_ReprLeave((PyObject *)v);
384 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385}
386
Martin v. Löwis18e16552006-02-15 17:27:45 +0000387static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000388list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391}
392
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000393static int
Fred Drakea2f55112000-07-09 15:16:51 +0000394list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 Py_ssize_t i;
397 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
400 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
401 Py_EQ);
402 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000403}
404
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000406list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 if (i < 0 || i >= Py_SIZE(a)) {
409 if (indexerr == NULL) {
410 indexerr = PyUnicode_FromString(
411 "list index out of range");
412 if (indexerr == NULL)
413 return NULL;
414 }
415 PyErr_SetObject(PyExc_IndexError, indexerr);
416 return NULL;
417 }
418 Py_INCREF(a->ob_item[i]);
419 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000420}
421
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000422static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000423list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 PyListObject *np;
426 PyObject **src, **dest;
427 Py_ssize_t i, len;
428 if (ilow < 0)
429 ilow = 0;
430 else if (ilow > Py_SIZE(a))
431 ilow = Py_SIZE(a);
432 if (ihigh < ilow)
433 ihigh = ilow;
434 else if (ihigh > Py_SIZE(a))
435 ihigh = Py_SIZE(a);
436 len = ihigh - ilow;
437 np = (PyListObject *) PyList_New(len);
438 if (np == NULL)
439 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 src = a->ob_item + ilow;
442 dest = np->ob_item;
443 for (i = 0; i < len; i++) {
444 PyObject *v = src[i];
445 Py_INCREF(v);
446 dest[i] = v;
447 }
448 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449}
450
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000452PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 if (!PyList_Check(a)) {
455 PyErr_BadInternalCall();
456 return NULL;
457 }
458 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000459}
460
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000462list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 Py_ssize_t size;
465 Py_ssize_t i;
466 PyObject **src, **dest;
467 PyListObject *np;
468 if (!PyList_Check(bb)) {
469 PyErr_Format(PyExc_TypeError,
470 "can only concatenate list (not \"%.200s\") to list",
471 bb->ob_type->tp_name);
472 return NULL;
473 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000474#define b ((PyListObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 size = Py_SIZE(a) + Py_SIZE(b);
476 if (size < 0)
477 return PyErr_NoMemory();
478 np = (PyListObject *) PyList_New(size);
479 if (np == NULL) {
480 return NULL;
481 }
482 src = a->ob_item;
483 dest = np->ob_item;
484 for (i = 0; i < Py_SIZE(a); i++) {
485 PyObject *v = src[i];
486 Py_INCREF(v);
487 dest[i] = v;
488 }
489 src = b->ob_item;
490 dest = np->ob_item + Py_SIZE(a);
491 for (i = 0; i < Py_SIZE(b); i++) {
492 PyObject *v = src[i];
493 Py_INCREF(v);
494 dest[i] = v;
495 }
496 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000497#undef b
498}
499
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000500static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000501list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 Py_ssize_t i, j;
504 Py_ssize_t size;
505 PyListObject *np;
506 PyObject **p, **items;
507 PyObject *elem;
508 if (n < 0)
509 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100510 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100512 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 if (size == 0)
514 return PyList_New(0);
515 np = (PyListObject *) PyList_New(size);
516 if (np == NULL)
517 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 items = np->ob_item;
520 if (Py_SIZE(a) == 1) {
521 elem = a->ob_item[0];
522 for (i = 0; i < n; i++) {
523 items[i] = elem;
524 Py_INCREF(elem);
525 }
526 return (PyObject *) np;
527 }
528 p = np->ob_item;
529 items = a->ob_item;
530 for (i = 0; i < n; i++) {
531 for (j = 0; j < Py_SIZE(a); j++) {
532 *p = items[j];
533 Py_INCREF(*p);
534 p++;
535 }
536 }
537 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000538}
539
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540static int
Armin Rigo93677f02004-07-29 12:40:23 +0000541list_clear(PyListObject *a)
542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 Py_ssize_t i;
544 PyObject **item = a->ob_item;
545 if (item != NULL) {
546 /* Because XDECREF can recursively invoke operations on
547 this list, we make it empty first. */
548 i = Py_SIZE(a);
549 Py_SIZE(a) = 0;
550 a->ob_item = NULL;
551 a->allocated = 0;
552 while (--i >= 0) {
553 Py_XDECREF(item[i]);
554 }
555 PyMem_FREE(item);
556 }
557 /* Never fails; the return value can be ignored.
558 Note that there is no guarantee that the list is actually empty
559 at this point, because XDECREF may have populated it again! */
560 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000561}
562
Tim Peters8fc4a912004-07-31 21:53:19 +0000563/* a[ilow:ihigh] = v if v != NULL.
564 * del a[ilow:ihigh] if v == NULL.
565 *
566 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
567 * guaranteed the call cannot fail.
568 */
Armin Rigo93677f02004-07-29 12:40:23 +0000569static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000570list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 /* Because [X]DECREF can recursively invoke list operations on
573 this list, we must postpone all [X]DECREF activity until
574 after the list is back in its canonical shape. Therefore
575 we must allocate an additional array, 'recycle', into which
576 we temporarily copy the items that are deleted from the
577 list. :-( */
578 PyObject *recycle_on_stack[8];
579 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
580 PyObject **item;
581 PyObject **vitem = NULL;
582 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
583 Py_ssize_t n; /* # of elements in replacement list */
584 Py_ssize_t norig; /* # of elements in list getting replaced */
585 Py_ssize_t d; /* Change in size */
586 Py_ssize_t k;
587 size_t s;
588 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 if (v == NULL)
591 n = 0;
592 else {
593 if (a == b) {
594 /* Special case "a[i:j] = a" -- copy b first */
595 v = list_slice(b, 0, Py_SIZE(b));
596 if (v == NULL)
597 return result;
598 result = list_ass_slice(a, ilow, ihigh, v);
599 Py_DECREF(v);
600 return result;
601 }
602 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
603 if(v_as_SF == NULL)
604 goto Error;
605 n = PySequence_Fast_GET_SIZE(v_as_SF);
606 vitem = PySequence_Fast_ITEMS(v_as_SF);
607 }
608 if (ilow < 0)
609 ilow = 0;
610 else if (ilow > Py_SIZE(a))
611 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 if (ihigh < ilow)
614 ihigh = ilow;
615 else if (ihigh > Py_SIZE(a))
616 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 norig = ihigh - ilow;
619 assert(norig >= 0);
620 d = n - norig;
621 if (Py_SIZE(a) + d == 0) {
622 Py_XDECREF(v_as_SF);
623 return list_clear(a);
624 }
625 item = a->ob_item;
626 /* recycle the items that we are about to remove */
627 s = norig * sizeof(PyObject *);
628 if (s > sizeof(recycle_on_stack)) {
629 recycle = (PyObject **)PyMem_MALLOC(s);
630 if (recycle == NULL) {
631 PyErr_NoMemory();
632 goto Error;
633 }
634 }
635 memcpy(recycle, &item[ilow], s);
Tim Peters8d9eb102004-07-31 02:24:20 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 if (d < 0) { /* Delete -d items */
638 memmove(&item[ihigh+d], &item[ihigh],
639 (Py_SIZE(a) - ihigh)*sizeof(PyObject *));
640 list_resize(a, Py_SIZE(a) + d);
641 item = a->ob_item;
642 }
643 else if (d > 0) { /* Insert d items */
644 k = Py_SIZE(a);
645 if (list_resize(a, k+d) < 0)
646 goto Error;
647 item = a->ob_item;
648 memmove(&item[ihigh+d], &item[ihigh],
649 (k - ihigh)*sizeof(PyObject *));
650 }
651 for (k = 0; k < n; k++, ilow++) {
652 PyObject *w = vitem[k];
653 Py_XINCREF(w);
654 item[ilow] = w;
655 }
656 for (k = norig - 1; k >= 0; --k)
657 Py_XDECREF(recycle[k]);
658 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000659 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 if (recycle != recycle_on_stack)
661 PyMem_FREE(recycle);
662 Py_XDECREF(v_as_SF);
663 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000664#undef b
665}
666
Guido van Rossum234f9421993-06-17 12:35:49 +0000667int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000668PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 if (!PyList_Check(a)) {
671 PyErr_BadInternalCall();
672 return -1;
673 }
674 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000675}
676
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000677static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000678list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 PyObject **items;
681 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000682
683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 size = PyList_GET_SIZE(self);
685 if (size == 0 || n == 1) {
686 Py_INCREF(self);
687 return (PyObject *)self;
688 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 if (n < 1) {
691 (void)list_clear(self);
692 Py_INCREF(self);
693 return (PyObject *)self;
694 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 if (size > PY_SSIZE_T_MAX / n) {
697 return PyErr_NoMemory();
698 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 if (list_resize(self, size*n) == -1)
701 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 p = size;
704 items = self->ob_item;
705 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
706 for (j = 0; j < size; j++) {
707 PyObject *o = items[j];
708 Py_INCREF(o);
709 items[p++] = o;
710 }
711 }
712 Py_INCREF(self);
713 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000714}
715
Guido van Rossum4a450d01991-04-03 19:05:18 +0000716static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000717list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 PyObject *old_value;
720 if (i < 0 || i >= Py_SIZE(a)) {
721 PyErr_SetString(PyExc_IndexError,
722 "list assignment index out of range");
723 return -1;
724 }
725 if (v == NULL)
726 return list_ass_slice(a, i, i+1, v);
727 Py_INCREF(v);
728 old_value = a->ob_item[i];
729 a->ob_item[i] = v;
730 Py_DECREF(old_value);
731 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000732}
733
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000734static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000735listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 Py_ssize_t i;
738 PyObject *v;
739 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
740 return NULL;
741 if (ins1(self, i, v) == 0)
742 Py_RETURN_NONE;
743 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000744}
745
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000746static PyObject *
Eli Benderskycbbaa962011-02-25 05:47:53 +0000747listclear(PyListObject *self)
748{
749 list_clear(self);
750 Py_RETURN_NONE;
751}
752
753static PyObject *
754listcopy(PyListObject *self)
755{
756 return list_slice(self, 0, Py_SIZE(self));
757}
758
759static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000760listappend(PyListObject *self, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 if (app1(self, v) == 0)
763 Py_RETURN_NONE;
764 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000765}
766
Barry Warsawdedf6d61998-10-09 16:37:25 +0000767static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000768listextend(PyListObject *self, PyObject *b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 PyObject *it; /* iter(v) */
771 Py_ssize_t m; /* size of self */
772 Py_ssize_t n; /* guess for size of b */
773 Py_ssize_t mn; /* m + n */
774 Py_ssize_t i;
775 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 /* Special cases:
778 1) lists and tuples which can use PySequence_Fast ops
779 2) extending self to self requires making a copy first
780 */
781 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
782 PyObject **src, **dest;
783 b = PySequence_Fast(b, "argument must be iterable");
784 if (!b)
785 return NULL;
786 n = PySequence_Fast_GET_SIZE(b);
787 if (n == 0) {
788 /* short circuit when b is empty */
789 Py_DECREF(b);
790 Py_RETURN_NONE;
791 }
792 m = Py_SIZE(self);
793 if (list_resize(self, m + n) == -1) {
794 Py_DECREF(b);
795 return NULL;
796 }
797 /* note that we may still have self == b here for the
798 * situation a.extend(a), but the following code works
799 * in that case too. Just make sure to resize self
800 * before calling PySequence_Fast_ITEMS.
801 */
802 /* populate the end of self with b's items */
803 src = PySequence_Fast_ITEMS(b);
804 dest = self->ob_item + m;
805 for (i = 0; i < n; i++) {
806 PyObject *o = src[i];
807 Py_INCREF(o);
808 dest[i] = o;
809 }
810 Py_DECREF(b);
811 Py_RETURN_NONE;
812 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 it = PyObject_GetIter(b);
815 if (it == NULL)
816 return NULL;
817 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 /* Guess a result list size. */
820 n = _PyObject_LengthHint(b, 8);
821 if (n == -1) {
822 Py_DECREF(it);
823 return NULL;
824 }
825 m = Py_SIZE(self);
826 mn = m + n;
827 if (mn >= m) {
828 /* Make room. */
829 if (list_resize(self, mn) == -1)
830 goto error;
831 /* Make the list sane again. */
832 Py_SIZE(self) = m;
833 }
834 /* Else m + n overflowed; on the chance that n lied, and there really
835 * is enough room, ignore it. If n was telling the truth, we'll
836 * eventually run out of memory during the loop.
837 */
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 /* Run iterator to exhaustion. */
840 for (;;) {
841 PyObject *item = iternext(it);
842 if (item == NULL) {
843 if (PyErr_Occurred()) {
844 if (PyErr_ExceptionMatches(PyExc_StopIteration))
845 PyErr_Clear();
846 else
847 goto error;
848 }
849 break;
850 }
851 if (Py_SIZE(self) < self->allocated) {
852 /* steals ref */
853 PyList_SET_ITEM(self, Py_SIZE(self), item);
854 ++Py_SIZE(self);
855 }
856 else {
857 int status = app1(self, item);
858 Py_DECREF(item); /* append creates a new ref */
859 if (status < 0)
860 goto error;
861 }
862 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 /* Cut back result list if initial guess was too large. */
865 if (Py_SIZE(self) < self->allocated)
866 list_resize(self, Py_SIZE(self)); /* shrinking can't fail */
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 Py_DECREF(it);
869 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000870
871 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 Py_DECREF(it);
873 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000874}
875
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000876PyObject *
877_PyList_Extend(PyListObject *self, PyObject *b)
878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 return listextend(self, b);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000880}
881
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000882static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000883list_inplace_concat(PyListObject *self, PyObject *other)
884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 result = listextend(self, other);
888 if (result == NULL)
889 return result;
890 Py_DECREF(result);
891 Py_INCREF(self);
892 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000893}
894
895static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000896listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 Py_ssize_t i = -1;
899 PyObject *v;
900 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 if (!PyArg_ParseTuple(args, "|n:pop", &i))
903 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 if (Py_SIZE(self) == 0) {
906 /* Special-case most common failure cause */
907 PyErr_SetString(PyExc_IndexError, "pop from empty list");
908 return NULL;
909 }
910 if (i < 0)
911 i += Py_SIZE(self);
912 if (i < 0 || i >= Py_SIZE(self)) {
913 PyErr_SetString(PyExc_IndexError, "pop index out of range");
914 return NULL;
915 }
916 v = self->ob_item[i];
917 if (i == Py_SIZE(self) - 1) {
918 status = list_resize(self, Py_SIZE(self) - 1);
919 assert(status >= 0);
920 return v; /* and v now owns the reference the list had */
921 }
922 Py_INCREF(v);
923 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
924 assert(status >= 0);
925 /* Use status, so that in a release build compilers don't
926 * complain about the unused name.
927 */
928 (void) status;
Brett Cannon651dd522004-08-08 21:21:18 +0000929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000931}
932
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000933/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
934static void
935reverse_slice(PyObject **lo, PyObject **hi)
936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 --hi;
940 while (lo < hi) {
941 PyObject *t = *lo;
942 *lo = *hi;
943 *hi = t;
944 ++lo;
945 --hi;
946 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000947}
948
Tim Petersa64dc242002-08-01 02:13:36 +0000949/* Lots of code for an adaptive, stable, natural mergesort. There are many
950 * pieces to this algorithm; read listsort.txt for overviews and details.
951 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000952
Daniel Stutzbach98338222010-12-02 21:55:33 +0000953/* A sortslice contains a pointer to an array of keys and a pointer to
954 * an array of corresponding values. In other words, keys[i]
955 * corresponds with values[i]. If values == NULL, then the keys are
956 * also the values.
957 *
958 * Several convenience routines are provided here, so that keys and
959 * values are always moved in sync.
960 */
961
962typedef struct {
963 PyObject **keys;
964 PyObject **values;
965} sortslice;
966
967Py_LOCAL_INLINE(void)
968sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
969{
970 s1->keys[i] = s2->keys[j];
971 if (s1->values != NULL)
972 s1->values[i] = s2->values[j];
973}
974
975Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000976sortslice_copy_incr(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
983Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000984sortslice_copy_decr(sortslice *dst, sortslice *src)
985{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000986 *dst->keys-- = *src->keys--;
987 if (dst->values != NULL)
988 *dst->values-- = *src->values--;
989}
990
991
992Py_LOCAL_INLINE(void)
993sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000994 Py_ssize_t n)
995{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000996 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
997 if (s1->values != NULL)
998 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
999}
1000
1001Py_LOCAL_INLINE(void)
1002sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001003 Py_ssize_t n)
1004{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001005 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1006 if (s1->values != NULL)
1007 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1008}
1009
1010Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001011sortslice_advance(sortslice *slice, Py_ssize_t n)
1012{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001013 slice->keys += n;
1014 if (slice->values != NULL)
1015 slice->values += n;
1016}
1017
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001018/* Comparison function: PyObject_RichCompareBool with Py_LT.
Tim Petersa64dc242002-08-01 02:13:36 +00001019 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1020 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001021
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001022#define ISLT(X, Y) (PyObject_RichCompareBool(X, Y, Py_LT))
Tim Peters66860f62002-08-04 17:47:26 +00001023
1024/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001025 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1026 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1027*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001028#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001030
1031/* binarysort is the best method for sorting small arrays: it does
1032 few compares, but can do data movement quadratic in the number of
1033 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001034 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001035 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001036 On entry, must have lo <= start <= hi, and that [lo, start) is already
1037 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001038 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001039 Even in case of error, the output slice will be some permutation of
1040 the input (nothing is lost or duplicated).
1041*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001042static int
Daniel Stutzbach98338222010-12-02 21:55:33 +00001043binarysort(sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 register Py_ssize_t k;
1046 register PyObject **l, **p, **r;
1047 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001048
Daniel Stutzbach98338222010-12-02 21:55:33 +00001049 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001051 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 ++start;
1053 for (; start < hi; ++start) {
1054 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001055 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 r = start;
1057 pivot = *r;
1058 /* Invariants:
1059 * pivot >= all in [lo, l).
1060 * pivot < all in [r, start).
1061 * The second is vacuously true at the start.
1062 */
1063 assert(l < r);
1064 do {
1065 p = l + ((r - l) >> 1);
1066 IFLT(pivot, *p)
1067 r = p;
1068 else
1069 l = p+1;
1070 } while (l < r);
1071 assert(l == r);
1072 /* The invariants still hold, so pivot >= all in [lo, l) and
1073 pivot < all in [l, start), so pivot belongs at l. Note
1074 that if there are elements equal to pivot, l points to the
1075 first slot after them -- that's why this sort is stable.
1076 Slide over to make room.
1077 Caution: using memmove is much slower under MSVC 5;
1078 we're not usually moving many slots. */
1079 for (p = start; p > l; --p)
1080 *p = *(p-1);
1081 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001082 if (lo.values != NULL) {
1083 Py_ssize_t offset = lo.values - lo.keys;
1084 p = start + offset;
1085 pivot = *p;
1086 l += offset;
1087 for (p = start + offset; p > l; --p)
1088 *p = *(p-1);
1089 *l = pivot;
1090 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 }
1092 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001093
1094 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001096}
1097
Tim Petersa64dc242002-08-01 02:13:36 +00001098/*
1099Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1100is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001101
Tim Petersa64dc242002-08-01 02:13:36 +00001102 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001103
Tim Petersa64dc242002-08-01 02:13:36 +00001104or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001105
Tim Petersa64dc242002-08-01 02:13:36 +00001106 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001107
Tim Petersa64dc242002-08-01 02:13:36 +00001108Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1109For its intended use in a stable mergesort, the strictness of the defn of
1110"descending" is needed so that the caller can safely reverse a descending
1111sequence without violating stability (strict > ensures there are no equal
1112elements to get out of order).
1113
1114Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001115*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001116static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001117count_run(PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 Py_ssize_t k;
1120 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 assert(lo < hi);
1123 *descending = 0;
1124 ++lo;
1125 if (lo == hi)
1126 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 n = 2;
1129 IFLT(*lo, *(lo-1)) {
1130 *descending = 1;
1131 for (lo = lo+1; lo < hi; ++lo, ++n) {
1132 IFLT(*lo, *(lo-1))
1133 ;
1134 else
1135 break;
1136 }
1137 }
1138 else {
1139 for (lo = lo+1; lo < hi; ++lo, ++n) {
1140 IFLT(*lo, *(lo-1))
1141 break;
1142 }
1143 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001146fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001148}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001149
Tim Petersa64dc242002-08-01 02:13:36 +00001150/*
1151Locate the proper position of key in a sorted vector; if the vector contains
1152an element equal to key, return the position immediately to the left of
1153the leftmost equal element. [gallop_right() does the same except returns
1154the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001155
Tim Petersa64dc242002-08-01 02:13:36 +00001156"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001157
Tim Petersa64dc242002-08-01 02:13:36 +00001158"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1159hint is to the final result, the faster this runs.
1160
1161The return value is the int k in 0..n such that
1162
1163 a[k-1] < key <= a[k]
1164
1165pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1166key belongs at index k; or, IOW, the first k elements of a should precede
1167key, and the last n-k should follow key.
1168
1169Returns -1 on error. See listsort.txt for info on the method.
1170*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001171static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001172gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 Py_ssize_t ofs;
1175 Py_ssize_t lastofs;
1176 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 a += hint;
1181 lastofs = 0;
1182 ofs = 1;
1183 IFLT(*a, key) {
1184 /* a[hint] < key -- gallop right, until
1185 * a[hint + lastofs] < key <= a[hint + ofs]
1186 */
1187 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1188 while (ofs < maxofs) {
1189 IFLT(a[ofs], key) {
1190 lastofs = ofs;
1191 ofs = (ofs << 1) + 1;
1192 if (ofs <= 0) /* int overflow */
1193 ofs = maxofs;
1194 }
1195 else /* key <= a[hint + ofs] */
1196 break;
1197 }
1198 if (ofs > maxofs)
1199 ofs = maxofs;
1200 /* Translate back to offsets relative to &a[0]. */
1201 lastofs += hint;
1202 ofs += hint;
1203 }
1204 else {
1205 /* key <= a[hint] -- gallop left, until
1206 * a[hint - ofs] < key <= a[hint - lastofs]
1207 */
1208 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1209 while (ofs < maxofs) {
1210 IFLT(*(a-ofs), key)
1211 break;
1212 /* key <= a[hint - ofs] */
1213 lastofs = ofs;
1214 ofs = (ofs << 1) + 1;
1215 if (ofs <= 0) /* int overflow */
1216 ofs = maxofs;
1217 }
1218 if (ofs > maxofs)
1219 ofs = maxofs;
1220 /* Translate back to positive offsets relative to &a[0]. */
1221 k = lastofs;
1222 lastofs = hint - ofs;
1223 ofs = hint - k;
1224 }
1225 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1228 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1229 * right of lastofs but no farther right than ofs. Do a binary
1230 * search, with invariant a[lastofs-1] < key <= a[ofs].
1231 */
1232 ++lastofs;
1233 while (lastofs < ofs) {
1234 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 IFLT(a[m], key)
1237 lastofs = m+1; /* a[m] < key */
1238 else
1239 ofs = m; /* key <= a[m] */
1240 }
1241 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1242 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001243
1244fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001246}
1247
1248/*
1249Exactly like gallop_left(), except that if key already exists in a[0:n],
1250finds the position immediately to the right of the rightmost equal value.
1251
1252The return value is the int k in 0..n such that
1253
1254 a[k-1] <= key < a[k]
1255
1256or -1 if error.
1257
1258The code duplication is massive, but this is enough different given that
1259we're sticking to "<" comparisons that it's much harder to follow if
1260written as one routine with yet another "left or right?" flag.
1261*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001262static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001263gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 Py_ssize_t ofs;
1266 Py_ssize_t lastofs;
1267 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 a += hint;
1272 lastofs = 0;
1273 ofs = 1;
1274 IFLT(key, *a) {
1275 /* key < a[hint] -- gallop left, until
1276 * a[hint - ofs] <= key < a[hint - lastofs]
1277 */
1278 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1279 while (ofs < maxofs) {
1280 IFLT(key, *(a-ofs)) {
1281 lastofs = ofs;
1282 ofs = (ofs << 1) + 1;
1283 if (ofs <= 0) /* int overflow */
1284 ofs = maxofs;
1285 }
1286 else /* a[hint - ofs] <= key */
1287 break;
1288 }
1289 if (ofs > maxofs)
1290 ofs = maxofs;
1291 /* Translate back to positive offsets relative to &a[0]. */
1292 k = lastofs;
1293 lastofs = hint - ofs;
1294 ofs = hint - k;
1295 }
1296 else {
1297 /* a[hint] <= key -- gallop right, until
1298 * a[hint + lastofs] <= key < a[hint + ofs]
1299 */
1300 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1301 while (ofs < maxofs) {
1302 IFLT(key, a[ofs])
1303 break;
1304 /* a[hint + ofs] <= key */
1305 lastofs = ofs;
1306 ofs = (ofs << 1) + 1;
1307 if (ofs <= 0) /* int overflow */
1308 ofs = maxofs;
1309 }
1310 if (ofs > maxofs)
1311 ofs = maxofs;
1312 /* Translate back to offsets relative to &a[0]. */
1313 lastofs += hint;
1314 ofs += hint;
1315 }
1316 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1319 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1320 * right of lastofs but no farther right than ofs. Do a binary
1321 * search, with invariant a[lastofs-1] <= key < a[ofs].
1322 */
1323 ++lastofs;
1324 while (lastofs < ofs) {
1325 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 IFLT(key, a[m])
1328 ofs = m; /* key < a[m] */
1329 else
1330 lastofs = m+1; /* a[m] <= key */
1331 }
1332 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1333 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001334
1335fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001337}
1338
1339/* The maximum number of entries in a MergeState's pending-runs stack.
1340 * This is enough to sort arrays of size up to about
1341 * 32 * phi ** MAX_MERGE_PENDING
1342 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1343 * with 2**64 elements.
1344 */
1345#define MAX_MERGE_PENDING 85
1346
Tim Peterse05f65a2002-08-10 05:21:15 +00001347/* When we get into galloping mode, we stay there until both runs win less
1348 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
Tim Petersa64dc242002-08-01 02:13:36 +00001349 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001350#define MIN_GALLOP 7
Tim Petersa64dc242002-08-01 02:13:36 +00001351
1352/* Avoid malloc for small temp arrays. */
1353#define MERGESTATE_TEMP_SIZE 256
1354
1355/* One MergeState exists on the stack per invocation of mergesort. It's just
1356 * a convenient way to pass state around among the helper functions.
1357 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001358struct s_slice {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001359 sortslice base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 Py_ssize_t len;
Tim Peterse05f65a2002-08-10 05:21:15 +00001361};
1362
Tim Petersa64dc242002-08-01 02:13:36 +00001363typedef struct s_MergeState {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 /* This controls when we get *into* galloping mode. It's initialized
1365 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1366 * random data, and lower for highly structured data.
1367 */
1368 Py_ssize_t min_gallop;
Tim Peterse05f65a2002-08-10 05:21:15 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 /* 'a' is temp storage to help with merges. It contains room for
1371 * alloced entries.
1372 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001373 sortslice a; /* may point to temparray below */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 Py_ssize_t alloced;
Tim Petersa64dc242002-08-01 02:13:36 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 /* A stack of n pending runs yet to be merged. Run #i starts at
1377 * address base[i] and extends for len[i] elements. It's always
1378 * true (so long as the indices are in bounds) that
1379 *
1380 * pending[i].base + pending[i].len == pending[i+1].base
1381 *
1382 * so we could cut the storage for this, but it's a minor amount,
1383 * and keeping all the info explicit simplifies the code.
1384 */
1385 int n;
1386 struct s_slice pending[MAX_MERGE_PENDING];
Tim Petersa64dc242002-08-01 02:13:36 +00001387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 /* 'a' points to this when possible, rather than muck with malloc. */
1389 PyObject *temparray[MERGESTATE_TEMP_SIZE];
Tim Petersa64dc242002-08-01 02:13:36 +00001390} MergeState;
1391
1392/* Conceptually a MergeState's constructor. */
1393static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001394merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001397 if (has_keyfunc) {
1398 /* The temporary space for merging will need at most half the list
1399 * size rounded up. Use the minimum possible space so we can use the
1400 * rest of temparray for other things. In particular, if there is
1401 * enough extra space, listsort() will use it to store the keys.
1402 */
1403 ms->alloced = (list_size + 1) / 2;
1404
1405 /* ms->alloced describes how many keys will be stored at
1406 ms->temparray, but we also need to store the values. Hence,
1407 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1408 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1409 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1410 ms->a.values = &ms->temparray[ms->alloced];
1411 }
1412 else {
1413 ms->alloced = MERGESTATE_TEMP_SIZE;
1414 ms->a.values = NULL;
1415 }
1416 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 ms->n = 0;
1418 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001419}
1420
1421/* Free all the temp memory owned by the MergeState. This must be called
1422 * when you're done with a MergeState, and may be called before then if
1423 * you want to free the temp memory early.
1424 */
1425static void
1426merge_freemem(MergeState *ms)
1427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001429 if (ms->a.keys != ms->temparray)
1430 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001431}
1432
1433/* Ensure enough temp memory for 'need' array slots is available.
1434 * Returns 0 on success and -1 if the memory can't be gotten.
1435 */
1436static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001437merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001438{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001439 int multiplier;
1440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 assert(ms != NULL);
1442 if (need <= ms->alloced)
1443 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001444
1445 multiplier = ms->a.values != NULL ? 2 : 1;
1446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 /* Don't realloc! That can cost cycles to copy the old data, but
1448 * we don't care what's in the block.
1449 */
1450 merge_freemem(ms);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001451 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 PyErr_NoMemory();
1453 return -1;
1454 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001455 ms->a.keys = (PyObject**)PyMem_Malloc(multiplier * need
1456 * sizeof(PyObject *));
1457 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001459 if (ms->a.values != NULL)
1460 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 return 0;
1462 }
1463 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001465}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1467 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001468
Daniel Stutzbach98338222010-12-02 21:55:33 +00001469/* Merge the na elements starting at ssa with the nb elements starting at
1470 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1471 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1472 * should have na <= nb. See listsort.txt for more info. Return 0 if
1473 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001474 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001475static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001476merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1477 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001480 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 int result = -1; /* guilty until proved innocent */
1482 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001483
Daniel Stutzbach98338222010-12-02 21:55:33 +00001484 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1485 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 if (MERGE_GETMEM(ms, na) < 0)
1487 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001488 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1489 dest = ssa;
1490 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001491
Daniel Stutzbach98338222010-12-02 21:55:33 +00001492 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 --nb;
1494 if (nb == 0)
1495 goto Succeed;
1496 if (na == 1)
1497 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 min_gallop = ms->min_gallop;
1500 for (;;) {
1501 Py_ssize_t acount = 0; /* # of times A won in a row */
1502 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 /* Do the straightforward thing until (if ever) one run
1505 * appears to win consistently.
1506 */
1507 for (;;) {
1508 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001509 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 if (k) {
1511 if (k < 0)
1512 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001513 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 ++bcount;
1515 acount = 0;
1516 --nb;
1517 if (nb == 0)
1518 goto Succeed;
1519 if (bcount >= min_gallop)
1520 break;
1521 }
1522 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001523 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 ++acount;
1525 bcount = 0;
1526 --na;
1527 if (na == 1)
1528 goto CopyB;
1529 if (acount >= min_gallop)
1530 break;
1531 }
1532 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 /* One run is winning so consistently that galloping may
1535 * be a huge win. So try that, and continue galloping until
1536 * (if ever) neither run appears to be winning consistently
1537 * anymore.
1538 */
1539 ++min_gallop;
1540 do {
1541 assert(na > 1 && nb > 0);
1542 min_gallop -= min_gallop > 1;
1543 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001544 k = gallop_right(ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 acount = k;
1546 if (k) {
1547 if (k < 0)
1548 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001549 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1550 sortslice_advance(&dest, k);
1551 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 na -= k;
1553 if (na == 1)
1554 goto CopyB;
1555 /* na==0 is impossible now if the comparison
1556 * function is consistent, but we can't assume
1557 * that it is.
1558 */
1559 if (na == 0)
1560 goto Succeed;
1561 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001562 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 --nb;
1564 if (nb == 0)
1565 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001566
Daniel Stutzbach98338222010-12-02 21:55:33 +00001567 k = gallop_left(ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 bcount = k;
1569 if (k) {
1570 if (k < 0)
1571 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001572 sortslice_memmove(&dest, 0, &ssb, 0, k);
1573 sortslice_advance(&dest, k);
1574 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 nb -= k;
1576 if (nb == 0)
1577 goto Succeed;
1578 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001579 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 --na;
1581 if (na == 1)
1582 goto CopyB;
1583 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1584 ++min_gallop; /* penalize it for leaving galloping mode */
1585 ms->min_gallop = min_gallop;
1586 }
Tim Petersa64dc242002-08-01 02:13:36 +00001587Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001589Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001591 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001593CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001595 /* The last element of ssa belongs at the end of the merge. */
1596 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1597 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001599}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001600
Daniel Stutzbach98338222010-12-02 21:55:33 +00001601/* Merge the na elements starting at pa with the nb elements starting at
1602 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1603 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1604 * should have na >= nb. See listsort.txt for more info. Return 0 if
1605 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001606 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001607static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001608merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1609 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001612 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001615
Daniel Stutzbach98338222010-12-02 21:55:33 +00001616 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1617 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 if (MERGE_GETMEM(ms, nb) < 0)
1619 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001620 dest = ssb;
1621 sortslice_advance(&dest, nb-1);
1622 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1623 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001625 ssb.keys = ms->a.keys + nb - 1;
1626 if (ssb.values != NULL)
1627 ssb.values = ms->a.values + nb - 1;
1628 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001629
Daniel Stutzbach98338222010-12-02 21:55:33 +00001630 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 --na;
1632 if (na == 0)
1633 goto Succeed;
1634 if (nb == 1)
1635 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 min_gallop = ms->min_gallop;
1638 for (;;) {
1639 Py_ssize_t acount = 0; /* # of times A won in a row */
1640 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 /* Do the straightforward thing until (if ever) one run
1643 * appears to win consistently.
1644 */
1645 for (;;) {
1646 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001647 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (k) {
1649 if (k < 0)
1650 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001651 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 ++acount;
1653 bcount = 0;
1654 --na;
1655 if (na == 0)
1656 goto Succeed;
1657 if (acount >= min_gallop)
1658 break;
1659 }
1660 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001661 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 ++bcount;
1663 acount = 0;
1664 --nb;
1665 if (nb == 1)
1666 goto CopyA;
1667 if (bcount >= min_gallop)
1668 break;
1669 }
1670 }
Tim Petersa64dc242002-08-01 02:13:36 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 /* One run is winning so consistently that galloping may
1673 * be a huge win. So try that, and continue galloping until
1674 * (if ever) neither run appears to be winning consistently
1675 * anymore.
1676 */
1677 ++min_gallop;
1678 do {
1679 assert(na > 0 && nb > 1);
1680 min_gallop -= min_gallop > 1;
1681 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001682 k = gallop_right(ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 if (k < 0)
1684 goto Fail;
1685 k = na - k;
1686 acount = k;
1687 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001688 sortslice_advance(&dest, -k);
1689 sortslice_advance(&ssa, -k);
1690 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 na -= k;
1692 if (na == 0)
1693 goto Succeed;
1694 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001695 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 --nb;
1697 if (nb == 1)
1698 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001699
Daniel Stutzbach98338222010-12-02 21:55:33 +00001700 k = gallop_left(ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 if (k < 0)
1702 goto Fail;
1703 k = nb - k;
1704 bcount = k;
1705 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001706 sortslice_advance(&dest, -k);
1707 sortslice_advance(&ssb, -k);
1708 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 nb -= k;
1710 if (nb == 1)
1711 goto CopyA;
1712 /* nb==0 is impossible now if the comparison
1713 * function is consistent, but we can't assume
1714 * that it is.
1715 */
1716 if (nb == 0)
1717 goto Succeed;
1718 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001719 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 --na;
1721 if (na == 0)
1722 goto Succeed;
1723 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1724 ++min_gallop; /* penalize it for leaving galloping mode */
1725 ms->min_gallop = min_gallop;
1726 }
Tim Petersa64dc242002-08-01 02:13:36 +00001727Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001729Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001731 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001733CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001735 /* The first element of ssb belongs at the front of the merge. */
1736 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1737 sortslice_advance(&dest, -na);
1738 sortslice_advance(&ssa, -na);
1739 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001741}
1742
1743/* Merge the two runs at stack indices i and i+1.
1744 * Returns 0 on success, -1 on error.
1745 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001746static Py_ssize_t
1747merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001748{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001749 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 Py_ssize_t na, nb;
1751 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 assert(ms != NULL);
1754 assert(ms->n >= 2);
1755 assert(i >= 0);
1756 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001757
Daniel Stutzbach98338222010-12-02 21:55:33 +00001758 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001760 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 nb = ms->pending[i+1].len;
1762 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001763 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 /* Record the length of the combined runs; if i is the 3rd-last
1766 * run now, also slide over the last run (which isn't involved
1767 * in this merge). The current run i+1 goes away in any case.
1768 */
1769 ms->pending[i].len = na + nb;
1770 if (i == ms->n - 3)
1771 ms->pending[i+1] = ms->pending[i+2];
1772 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 /* Where does b start in a? Elements in a before that can be
1775 * ignored (already in place).
1776 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001777 k = gallop_right(*ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 if (k < 0)
1779 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001780 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 na -= k;
1782 if (na == 0)
1783 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 /* Where does a end in b? Elements in b after that can be
1786 * ignored (already in place).
1787 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001788 nb = gallop_left(ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 if (nb <= 0)
1790 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 /* Merge what remains of the runs, using a temp array with
1793 * min(na, nb) elements.
1794 */
1795 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001796 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001798 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001799}
1800
1801/* Examine the stack of runs waiting to be merged, merging adjacent runs
1802 * until the stack invariants are re-established:
1803 *
1804 * 1. len[-3] > len[-2] + len[-1]
1805 * 2. len[-2] > len[-1]
1806 *
1807 * See listsort.txt for more info.
1808 *
1809 * Returns 0 on success, -1 on error.
1810 */
1811static int
1812merge_collapse(MergeState *ms)
1813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 assert(ms);
1817 while (ms->n > 1) {
1818 Py_ssize_t n = ms->n - 2;
1819 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
1820 if (p[n-1].len < p[n+1].len)
1821 --n;
1822 if (merge_at(ms, n) < 0)
1823 return -1;
1824 }
1825 else if (p[n].len <= p[n+1].len) {
1826 if (merge_at(ms, n) < 0)
1827 return -1;
1828 }
1829 else
1830 break;
1831 }
1832 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001833}
1834
1835/* Regardless of invariants, merge all runs on the stack until only one
1836 * remains. This is used at the end of the mergesort.
1837 *
1838 * Returns 0 on success, -1 on error.
1839 */
1840static int
1841merge_force_collapse(MergeState *ms)
1842{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 assert(ms);
1846 while (ms->n > 1) {
1847 Py_ssize_t n = ms->n - 2;
1848 if (n > 0 && p[n-1].len < p[n+1].len)
1849 --n;
1850 if (merge_at(ms, n) < 0)
1851 return -1;
1852 }
1853 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001854}
1855
1856/* Compute a good value for the minimum run length; natural runs shorter
1857 * than this are boosted artificially via binary insertion.
1858 *
1859 * If n < 64, return n (it's too small to bother with fancy stuff).
1860 * Else if n is an exact power of 2, return 32.
1861 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1862 * strictly less than, an exact power of 2.
1863 *
1864 * See listsort.txt for more info.
1865 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001866static Py_ssize_t
1867merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 assert(n >= 0);
1872 while (n >= 64) {
1873 r |= n & 1;
1874 n >>= 1;
1875 }
1876 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001877}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001878
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001879static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001880reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001881{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001882 reverse_slice(s->keys, &s->keys[n]);
1883 if (s->values != NULL)
1884 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001885}
1886
Tim Petersa64dc242002-08-01 02:13:36 +00001887/* An adaptive, stable, natural mergesort. See listsort.txt.
1888 * Returns Py_None on success, NULL on error. Even in case of error, the
1889 * list will be some permutation of its input state (nothing is lost or
1890 * duplicated).
1891 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001892static PyObject *
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001893listsort(PyListObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 Py_ssize_t nremaining;
1897 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001898 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 Py_ssize_t saved_ob_size, saved_allocated;
1900 PyObject **saved_ob_item;
1901 PyObject **final_ob_item;
1902 PyObject *result = NULL; /* guilty until proved innocent */
1903 int reverse = 0;
1904 PyObject *keyfunc = NULL;
1905 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 static char *kwlist[] = {"key", "reverse", 0};
Daniel Stutzbach98338222010-12-02 21:55:33 +00001907 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 assert(self != NULL);
1910 assert (PyList_Check(self));
1911 if (args != NULL) {
1912 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort",
1913 kwlist, &keyfunc, &reverse))
1914 return NULL;
1915 if (Py_SIZE(args) > 0) {
1916 PyErr_SetString(PyExc_TypeError,
1917 "must use keyword argument for key function");
1918 return NULL;
1919 }
1920 }
1921 if (keyfunc == Py_None)
1922 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 /* The list is temporarily made empty, so that mutations performed
1925 * by comparison functions can't affect the slice of memory we're
1926 * sorting (allowing mutations during sorting is a core-dump
1927 * factory, since ob_item may change).
1928 */
1929 saved_ob_size = Py_SIZE(self);
1930 saved_ob_item = self->ob_item;
1931 saved_allocated = self->allocated;
1932 Py_SIZE(self) = 0;
1933 self->ob_item = NULL;
1934 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00001935
Daniel Stutzbach98338222010-12-02 21:55:33 +00001936 if (keyfunc == NULL) {
1937 keys = NULL;
1938 lo.keys = saved_ob_item;
1939 lo.values = NULL;
1940 }
1941 else {
1942 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
1943 /* Leverage stack space we allocated but won't otherwise use */
1944 keys = &ms.temparray[saved_ob_size+1];
1945 else {
1946 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
1947 if (keys == NULL)
1948 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001950
1951 for (i = 0; i < saved_ob_size ; i++) {
1952 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
1953 NULL);
1954 if (keys[i] == NULL) {
1955 for (i=i-1 ; i>=0 ; i--)
1956 Py_DECREF(keys[i]);
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00001957 if (keys != &ms.temparray[saved_ob_size+1])
1958 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001959 goto keyfunc_fail;
1960 }
1961 }
1962
1963 lo.keys = keys;
1964 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001966
Daniel Stutzbach98338222010-12-02 21:55:33 +00001967 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 nremaining = saved_ob_size;
1970 if (nremaining < 2)
1971 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00001972
Benjamin Peterson05380642010-08-23 19:35:39 +00001973 /* Reverse sort stability achieved by initially reversing the list,
1974 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001975 if (reverse) {
1976 if (keys != NULL)
1977 reverse_slice(&keys[0], &keys[saved_ob_size]);
1978 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
1979 }
Benjamin Peterson05380642010-08-23 19:35:39 +00001980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 /* March over the array once, left to right, finding natural runs,
1982 * and extending short natural runs to minrun elements.
1983 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 minrun = merge_compute_minrun(nremaining);
1985 do {
1986 int descending;
1987 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00001988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 /* Identify next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001990 n = count_run(lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 if (n < 0)
1992 goto fail;
1993 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001994 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 /* If short, extend to min(minrun, nremaining). */
1996 if (n < minrun) {
1997 const Py_ssize_t force = nremaining <= minrun ?
1998 nremaining : minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001999 if (binarysort(lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 goto fail;
2001 n = force;
2002 }
2003 /* Push run onto pending-runs stack, and maybe merge. */
2004 assert(ms.n < MAX_MERGE_PENDING);
2005 ms.pending[ms.n].base = lo;
2006 ms.pending[ms.n].len = n;
2007 ++ms.n;
2008 if (merge_collapse(&ms) < 0)
2009 goto fail;
2010 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002011 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 nremaining -= n;
2013 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 if (merge_force_collapse(&ms) < 0)
2016 goto fail;
2017 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002018 assert(keys == NULL
2019 ? ms.pending[0].base.keys == saved_ob_item
2020 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002022 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002023
2024succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002026fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002027 if (keys != NULL) {
2028 for (i = 0; i < saved_ob_size; i++)
2029 Py_DECREF(keys[i]);
2030 if (keys != &ms.temparray[saved_ob_size+1])
2031 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 if (self->allocated != -1 && result != NULL) {
2035 /* The user mucked with the list during the sort,
2036 * and we don't already have another error to report.
2037 */
2038 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2039 result = NULL;
2040 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 if (reverse && saved_ob_size > 1)
2043 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002046
Daniel Stutzbach98338222010-12-02 21:55:33 +00002047keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 final_ob_item = self->ob_item;
2049 i = Py_SIZE(self);
2050 Py_SIZE(self) = saved_ob_size;
2051 self->ob_item = saved_ob_item;
2052 self->allocated = saved_allocated;
2053 if (final_ob_item != NULL) {
2054 /* we cannot use list_clear() for this because it does not
2055 guarantee that the list is really empty when it returns */
2056 while (--i >= 0) {
2057 Py_XDECREF(final_ob_item[i]);
2058 }
2059 PyMem_FREE(final_ob_item);
2060 }
2061 Py_XINCREF(result);
2062 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002063}
Tim Peters330f9e92002-07-19 07:05:44 +00002064#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002065#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002066
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002067int
Fred Drakea2f55112000-07-09 15:16:51 +00002068PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 if (v == NULL || !PyList_Check(v)) {
2071 PyErr_BadInternalCall();
2072 return -1;
2073 }
2074 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
2075 if (v == NULL)
2076 return -1;
2077 Py_DECREF(v);
2078 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002079}
2080
Guido van Rossumb86c5492001-02-12 22:06:02 +00002081static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002082listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 if (Py_SIZE(self) > 1)
2085 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2086 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002087}
2088
Guido van Rossum84c76f51990-10-30 13:32:20 +00002089int
Fred Drakea2f55112000-07-09 15:16:51 +00002090PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 if (v == NULL || !PyList_Check(v)) {
2095 PyErr_BadInternalCall();
2096 return -1;
2097 }
2098 if (Py_SIZE(self) > 1)
2099 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2100 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002101}
2102
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002103PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002104PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 PyObject *w;
2107 PyObject **p, **q;
2108 Py_ssize_t n;
2109 if (v == NULL || !PyList_Check(v)) {
2110 PyErr_BadInternalCall();
2111 return NULL;
2112 }
2113 n = Py_SIZE(v);
2114 w = PyTuple_New(n);
2115 if (w == NULL)
2116 return NULL;
2117 p = ((PyTupleObject *)w)->ob_item;
2118 q = ((PyListObject *)v)->ob_item;
2119 while (--n >= 0) {
2120 Py_INCREF(*q);
2121 *p = *q;
2122 p++;
2123 q++;
2124 }
2125 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002126}
2127
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002128static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002129listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002132 PyObject *v;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002133
Petri Lehtinenebfaabd2011-11-06 21:02:39 +02002134 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2135 _PyEval_SliceIndex, &start,
2136 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 return NULL;
2138 if (start < 0) {
2139 start += Py_SIZE(self);
2140 if (start < 0)
2141 start = 0;
2142 }
2143 if (stop < 0) {
2144 stop += Py_SIZE(self);
2145 if (stop < 0)
2146 stop = 0;
2147 }
2148 for (i = start; i < stop && i < Py_SIZE(self); i++) {
2149 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2150 if (cmp > 0)
2151 return PyLong_FromSsize_t(i);
2152 else if (cmp < 0)
2153 return NULL;
2154 }
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002155 PyErr_Format(PyExc_ValueError, "%R is not in list", v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002157}
2158
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002159static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002160listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 Py_ssize_t count = 0;
2163 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 for (i = 0; i < Py_SIZE(self); i++) {
2166 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2167 if (cmp > 0)
2168 count++;
2169 else if (cmp < 0)
2170 return NULL;
2171 }
2172 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002173}
2174
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002175static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002176listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 for (i = 0; i < Py_SIZE(self); i++) {
2181 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2182 if (cmp > 0) {
2183 if (list_ass_slice(self, i, i+1,
2184 (PyObject *)NULL) == 0)
2185 Py_RETURN_NONE;
2186 return NULL;
2187 }
2188 else if (cmp < 0)
2189 return NULL;
2190 }
2191 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2192 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002193}
2194
Jeremy Hylton8caad492000-06-23 14:18:11 +00002195static int
2196list_traverse(PyListObject *o, visitproc visit, void *arg)
2197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 for (i = Py_SIZE(o); --i >= 0; )
2201 Py_VISIT(o->ob_item[i]);
2202 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002203}
2204
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002205static PyObject *
2206list_richcompare(PyObject *v, PyObject *w, int op)
2207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 PyListObject *vl, *wl;
2209 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002210
Brian Curtindfc80e32011-08-10 20:28:54 -05002211 if (!PyList_Check(v) || !PyList_Check(w))
2212 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 vl = (PyListObject *)v;
2215 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2218 /* Shortcut: if the lengths differ, the lists differ */
2219 PyObject *res;
2220 if (op == Py_EQ)
2221 res = Py_False;
2222 else
2223 res = Py_True;
2224 Py_INCREF(res);
2225 return res;
2226 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 /* Search for the first index where items are different */
2229 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2230 int k = PyObject_RichCompareBool(vl->ob_item[i],
2231 wl->ob_item[i], Py_EQ);
2232 if (k < 0)
2233 return NULL;
2234 if (!k)
2235 break;
2236 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2239 /* No more items to compare -- compare sizes */
2240 Py_ssize_t vs = Py_SIZE(vl);
2241 Py_ssize_t ws = Py_SIZE(wl);
2242 int cmp;
2243 PyObject *res;
2244 switch (op) {
2245 case Py_LT: cmp = vs < ws; break;
2246 case Py_LE: cmp = vs <= ws; break;
2247 case Py_EQ: cmp = vs == ws; break;
2248 case Py_NE: cmp = vs != ws; break;
2249 case Py_GT: cmp = vs > ws; break;
2250 case Py_GE: cmp = vs >= ws; break;
2251 default: return NULL; /* cannot happen */
2252 }
2253 if (cmp)
2254 res = Py_True;
2255 else
2256 res = Py_False;
2257 Py_INCREF(res);
2258 return res;
2259 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 /* We have an item that differs -- shortcuts for EQ/NE */
2262 if (op == Py_EQ) {
2263 Py_INCREF(Py_False);
2264 return Py_False;
2265 }
2266 if (op == Py_NE) {
2267 Py_INCREF(Py_True);
2268 return Py_True;
2269 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 /* Compare the final item again using the proper operator */
2272 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002273}
2274
Tim Peters6d6c1a32001-08-02 04:15:00 +00002275static int
2276list_init(PyListObject *self, PyObject *args, PyObject *kw)
2277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 PyObject *arg = NULL;
2279 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2282 return -1;
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 /* Verify list invariants established by PyType_GenericAlloc() */
2285 assert(0 <= Py_SIZE(self));
2286 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2287 assert(self->ob_item != NULL ||
2288 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 /* Empty previous contents */
2291 if (self->ob_item != NULL) {
2292 (void)list_clear(self);
2293 }
2294 if (arg != NULL) {
2295 PyObject *rv = listextend(self, arg);
2296 if (rv == NULL)
2297 return -1;
2298 Py_DECREF(rv);
2299 }
2300 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002301}
2302
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002303static PyObject *
2304list_sizeof(PyListObject *self)
2305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 res = sizeof(PyListObject) + self->allocated * sizeof(void*);
2309 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002310}
2311
Raymond Hettinger1021c442003-11-07 15:38:09 +00002312static PyObject *list_iter(PyObject *seq);
2313static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2314
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002315PyDoc_STRVAR(getitem_doc,
2316"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002317PyDoc_STRVAR(reversed_doc,
2318"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002319PyDoc_STRVAR(sizeof_doc,
2320"L.__sizeof__() -- size of L in memory, in bytes");
Eli Benderskycbbaa962011-02-25 05:47:53 +00002321PyDoc_STRVAR(clear_doc,
2322"L.clear() -> None -- remove all items from L");
2323PyDoc_STRVAR(copy_doc,
2324"L.copy() -> list -- a shallow copy of L");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002325PyDoc_STRVAR(append_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002326"L.append(object) -> None -- append object to end");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002327PyDoc_STRVAR(extend_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002328"L.extend(iterable) -> None -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002329PyDoc_STRVAR(insert_doc,
2330"L.insert(index, object) -- insert object before index");
2331PyDoc_STRVAR(pop_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002332"L.pop([index]) -> item -- remove and return item at index (default last).\n"
2333"Raises IndexError if list is empty or index is out of range.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002334PyDoc_STRVAR(remove_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002335"L.remove(value) -> None -- remove first occurrence of value.\n"
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002336"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002337PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002338"L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2339"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002340PyDoc_STRVAR(count_doc,
2341"L.count(value) -> integer -- return number of occurrences of value");
2342PyDoc_STRVAR(reverse_doc,
2343"L.reverse() -- reverse *IN PLACE*");
2344PyDoc_STRVAR(sort_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002345"L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002346
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002347static PyObject *list_subscript(PyListObject*, PyObject*);
2348
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002349static PyMethodDef list_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
2351 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
2352 {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002353 {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc},
2354 {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 {"append", (PyCFunction)listappend, METH_O, append_doc},
2356 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002357 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
2359 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
2360 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
2361 {"count", (PyCFunction)listcount, METH_O, count_doc},
2362 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
2363 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
2364 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002365};
2366
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002367static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 (lenfunc)list_length, /* sq_length */
2369 (binaryfunc)list_concat, /* sq_concat */
2370 (ssizeargfunc)list_repeat, /* sq_repeat */
2371 (ssizeargfunc)list_item, /* sq_item */
2372 0, /* sq_slice */
2373 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2374 0, /* sq_ass_slice */
2375 (objobjproc)list_contains, /* sq_contains */
2376 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2377 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002378};
2379
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002380PyDoc_STRVAR(list_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002381"list() -> new empty list\n"
2382"list(iterable) -> new list initialized from iterable's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002383
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002384static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002385list_subscript(PyListObject* self, PyObject* item)
2386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 if (PyIndex_Check(item)) {
2388 Py_ssize_t i;
2389 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2390 if (i == -1 && PyErr_Occurred())
2391 return NULL;
2392 if (i < 0)
2393 i += PyList_GET_SIZE(self);
2394 return list_item(self, i);
2395 }
2396 else if (PySlice_Check(item)) {
2397 Py_ssize_t start, stop, step, slicelength, cur, i;
2398 PyObject* result;
2399 PyObject* it;
2400 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002401
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002402 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 &start, &stop, &step, &slicelength) < 0) {
2404 return NULL;
2405 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 if (slicelength <= 0) {
2408 return PyList_New(0);
2409 }
2410 else if (step == 1) {
2411 return list_slice(self, start, stop);
2412 }
2413 else {
2414 result = PyList_New(slicelength);
2415 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 src = self->ob_item;
2418 dest = ((PyListObject *)result)->ob_item;
2419 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002420 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 it = src[cur];
2422 Py_INCREF(it);
2423 dest[i] = it;
2424 }
Tim Peters3b01a122002-07-19 02:35:45 +00002425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 return result;
2427 }
2428 }
2429 else {
2430 PyErr_Format(PyExc_TypeError,
2431 "list indices must be integers, not %.200s",
2432 item->ob_type->tp_name);
2433 return NULL;
2434 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002435}
2436
Tim Peters3b01a122002-07-19 02:35:45 +00002437static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002438list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 if (PyIndex_Check(item)) {
2441 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2442 if (i == -1 && PyErr_Occurred())
2443 return -1;
2444 if (i < 0)
2445 i += PyList_GET_SIZE(self);
2446 return list_ass_item(self, i, value);
2447 }
2448 else if (PySlice_Check(item)) {
2449 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002450
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002451 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 &start, &stop, &step, &slicelength) < 0) {
2453 return -1;
2454 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 if (step == 1)
2457 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 /* Make sure s[5:2] = [..] inserts at the right place:
2460 before 5, not before 2. */
2461 if ((step < 0 && start < stop) ||
2462 (step > 0 && start > stop))
2463 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 if (value == NULL) {
2466 /* delete slice */
2467 PyObject **garbage;
2468 size_t cur;
2469 Py_ssize_t i;
Tim Peters3b01a122002-07-19 02:35:45 +00002470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 if (slicelength <= 0)
2472 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 if (step < 0) {
2475 stop = start + 1;
2476 start = stop + step*(slicelength - 1) - 1;
2477 step = -step;
2478 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 assert((size_t)slicelength <=
2481 PY_SIZE_MAX / sizeof(PyObject*));
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00002482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 garbage = (PyObject**)
2484 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2485 if (!garbage) {
2486 PyErr_NoMemory();
2487 return -1;
2488 }
Tim Peters3b01a122002-07-19 02:35:45 +00002489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 /* drawing pictures might help understand these for
2491 loops. Basically, we memmove the parts of the
2492 list that are *not* part of the slice: step-1
2493 items for each item that is part of the slice,
2494 and then tail end of the list that was not
2495 covered by the slice */
2496 for (cur = start, i = 0;
2497 cur < (size_t)stop;
2498 cur += step, i++) {
2499 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 if (cur + step >= (size_t)Py_SIZE(self)) {
2504 lim = Py_SIZE(self) - cur - 1;
2505 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 memmove(self->ob_item + cur - i,
2508 self->ob_item + cur + 1,
2509 lim * sizeof(PyObject *));
2510 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002511 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 if (cur < (size_t)Py_SIZE(self)) {
2513 memmove(self->ob_item + cur - slicelength,
2514 self->ob_item + cur,
2515 (Py_SIZE(self) - cur) *
2516 sizeof(PyObject *));
2517 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 Py_SIZE(self) -= slicelength;
2520 list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 for (i = 0; i < slicelength; i++) {
2523 Py_DECREF(garbage[i]);
2524 }
2525 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 return 0;
2528 }
2529 else {
2530 /* assign slice */
2531 PyObject *ins, *seq;
2532 PyObject **garbage, **seqitems, **selfitems;
2533 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 /* protect against a[::-1] = a */
2536 if (self == (PyListObject*)value) {
2537 seq = list_slice((PyListObject*)value, 0,
2538 PyList_GET_SIZE(value));
2539 }
2540 else {
2541 seq = PySequence_Fast(value,
2542 "must assign iterable "
2543 "to extended slice");
2544 }
2545 if (!seq)
2546 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2549 PyErr_Format(PyExc_ValueError,
2550 "attempt to assign sequence of "
2551 "size %zd to extended slice of "
2552 "size %zd",
2553 PySequence_Fast_GET_SIZE(seq),
2554 slicelength);
2555 Py_DECREF(seq);
2556 return -1;
2557 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 if (!slicelength) {
2560 Py_DECREF(seq);
2561 return 0;
2562 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 garbage = (PyObject**)
2565 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2566 if (!garbage) {
2567 Py_DECREF(seq);
2568 PyErr_NoMemory();
2569 return -1;
2570 }
Tim Peters3b01a122002-07-19 02:35:45 +00002571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 selfitems = self->ob_item;
2573 seqitems = PySequence_Fast_ITEMS(seq);
2574 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002575 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 garbage[i] = selfitems[cur];
2577 ins = seqitems[i];
2578 Py_INCREF(ins);
2579 selfitems[cur] = ins;
2580 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 for (i = 0; i < slicelength; i++) {
2583 Py_DECREF(garbage[i]);
2584 }
Tim Peters3b01a122002-07-19 02:35:45 +00002585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 PyMem_FREE(garbage);
2587 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 return 0;
2590 }
2591 }
2592 else {
2593 PyErr_Format(PyExc_TypeError,
2594 "list indices must be integers, not %.200s",
2595 item->ob_type->tp_name);
2596 return -1;
2597 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002598}
2599
2600static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002601 (lenfunc)list_length,
2602 (binaryfunc)list_subscript,
2603 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002604};
2605
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002606PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2608 "list",
2609 sizeof(PyListObject),
2610 0,
2611 (destructor)list_dealloc, /* tp_dealloc */
2612 0, /* tp_print */
2613 0, /* tp_getattr */
2614 0, /* tp_setattr */
2615 0, /* tp_reserved */
2616 (reprfunc)list_repr, /* tp_repr */
2617 0, /* tp_as_number */
2618 &list_as_sequence, /* tp_as_sequence */
2619 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002620 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 0, /* tp_call */
2622 0, /* tp_str */
2623 PyObject_GenericGetAttr, /* tp_getattro */
2624 0, /* tp_setattro */
2625 0, /* tp_as_buffer */
2626 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2627 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2628 list_doc, /* tp_doc */
2629 (traverseproc)list_traverse, /* tp_traverse */
2630 (inquiry)list_clear, /* tp_clear */
2631 list_richcompare, /* tp_richcompare */
2632 0, /* tp_weaklistoffset */
2633 list_iter, /* tp_iter */
2634 0, /* tp_iternext */
2635 list_methods, /* tp_methods */
2636 0, /* tp_members */
2637 0, /* tp_getset */
2638 0, /* tp_base */
2639 0, /* tp_dict */
2640 0, /* tp_descr_get */
2641 0, /* tp_descr_set */
2642 0, /* tp_dictoffset */
2643 (initproc)list_init, /* tp_init */
2644 PyType_GenericAlloc, /* tp_alloc */
2645 PyType_GenericNew, /* tp_new */
2646 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002647};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002648
2649
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002650/*********************** List Iterator **************************/
2651
2652typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 PyObject_HEAD
2654 long it_index;
2655 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002656} listiterobject;
2657
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002658static PyObject *list_iter(PyObject *);
2659static void listiter_dealloc(listiterobject *);
2660static int listiter_traverse(listiterobject *, visitproc, void *);
2661static PyObject *listiter_next(listiterobject *);
2662static PyObject *listiter_len(listiterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002663static PyObject *listiter_reduce_general(void *_it, int forward);
2664static PyObject *listiter_reduce(listiterobject *);
2665static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002666
Armin Rigof5b3e362006-02-11 21:32:43 +00002667PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002668PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2669PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002670
2671static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002673 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
2674 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002676};
2677
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002678PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2680 "list_iterator", /* tp_name */
2681 sizeof(listiterobject), /* tp_basicsize */
2682 0, /* tp_itemsize */
2683 /* methods */
2684 (destructor)listiter_dealloc, /* tp_dealloc */
2685 0, /* tp_print */
2686 0, /* tp_getattr */
2687 0, /* tp_setattr */
2688 0, /* tp_reserved */
2689 0, /* tp_repr */
2690 0, /* tp_as_number */
2691 0, /* tp_as_sequence */
2692 0, /* tp_as_mapping */
2693 0, /* tp_hash */
2694 0, /* tp_call */
2695 0, /* tp_str */
2696 PyObject_GenericGetAttr, /* tp_getattro */
2697 0, /* tp_setattro */
2698 0, /* tp_as_buffer */
2699 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2700 0, /* tp_doc */
2701 (traverseproc)listiter_traverse, /* tp_traverse */
2702 0, /* tp_clear */
2703 0, /* tp_richcompare */
2704 0, /* tp_weaklistoffset */
2705 PyObject_SelfIter, /* tp_iter */
2706 (iternextfunc)listiter_next, /* tp_iternext */
2707 listiter_methods, /* tp_methods */
2708 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002709};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002710
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002711
2712static PyObject *
2713list_iter(PyObject *seq)
2714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 if (!PyList_Check(seq)) {
2718 PyErr_BadInternalCall();
2719 return NULL;
2720 }
2721 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2722 if (it == NULL)
2723 return NULL;
2724 it->it_index = 0;
2725 Py_INCREF(seq);
2726 it->it_seq = (PyListObject *)seq;
2727 _PyObject_GC_TRACK(it);
2728 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002729}
2730
2731static void
2732listiter_dealloc(listiterobject *it)
2733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 _PyObject_GC_UNTRACK(it);
2735 Py_XDECREF(it->it_seq);
2736 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002737}
2738
2739static int
2740listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 Py_VISIT(it->it_seq);
2743 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002744}
2745
2746static PyObject *
2747listiter_next(listiterobject *it)
2748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 PyListObject *seq;
2750 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 assert(it != NULL);
2753 seq = it->it_seq;
2754 if (seq == NULL)
2755 return NULL;
2756 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 if (it->it_index < PyList_GET_SIZE(seq)) {
2759 item = PyList_GET_ITEM(seq, it->it_index);
2760 ++it->it_index;
2761 Py_INCREF(item);
2762 return item;
2763 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 Py_DECREF(seq);
2766 it->it_seq = NULL;
2767 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002768}
2769
2770static PyObject *
2771listiter_len(listiterobject *it)
2772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 Py_ssize_t len;
2774 if (it->it_seq) {
2775 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2776 if (len >= 0)
2777 return PyLong_FromSsize_t(len);
2778 }
2779 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002780}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002781
2782static PyObject *
2783listiter_reduce(listiterobject *it)
2784{
2785 return listiter_reduce_general(it, 1);
2786}
2787
2788static PyObject *
2789listiter_setstate(listiterobject *it, PyObject *state)
2790{
2791 long index = PyLong_AsLong(state);
2792 if (index == -1 && PyErr_Occurred())
2793 return NULL;
2794 if (it->it_seq != NULL) {
2795 if (index < 0)
2796 index = 0;
2797 it->it_index = index;
2798 }
2799 Py_RETURN_NONE;
2800}
2801
Raymond Hettinger1021c442003-11-07 15:38:09 +00002802/*********************** List Reverse Iterator **************************/
2803
2804typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 PyObject_HEAD
2806 Py_ssize_t it_index;
2807 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002808} listreviterobject;
2809
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002810static PyObject *list_reversed(PyListObject *, PyObject *);
2811static void listreviter_dealloc(listreviterobject *);
2812static int listreviter_traverse(listreviterobject *, visitproc, void *);
2813static PyObject *listreviter_next(listreviterobject *);
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002814static PyObject *listreviter_len(listreviterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002815static PyObject *listreviter_reduce(listreviterobject *);
2816static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002817
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002818static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002820 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
2821 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002823};
2824
Raymond Hettinger1021c442003-11-07 15:38:09 +00002825PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2827 "list_reverseiterator", /* tp_name */
2828 sizeof(listreviterobject), /* tp_basicsize */
2829 0, /* tp_itemsize */
2830 /* methods */
2831 (destructor)listreviter_dealloc, /* tp_dealloc */
2832 0, /* tp_print */
2833 0, /* tp_getattr */
2834 0, /* tp_setattr */
2835 0, /* tp_reserved */
2836 0, /* tp_repr */
2837 0, /* tp_as_number */
2838 0, /* tp_as_sequence */
2839 0, /* tp_as_mapping */
2840 0, /* tp_hash */
2841 0, /* tp_call */
2842 0, /* tp_str */
2843 PyObject_GenericGetAttr, /* tp_getattro */
2844 0, /* tp_setattro */
2845 0, /* tp_as_buffer */
2846 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2847 0, /* tp_doc */
2848 (traverseproc)listreviter_traverse, /* tp_traverse */
2849 0, /* tp_clear */
2850 0, /* tp_richcompare */
2851 0, /* tp_weaklistoffset */
2852 PyObject_SelfIter, /* tp_iter */
2853 (iternextfunc)listreviter_next, /* tp_iternext */
2854 listreviter_methods, /* tp_methods */
2855 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00002856};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002857
2858static PyObject *
2859list_reversed(PyListObject *seq, PyObject *unused)
2860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2864 if (it == NULL)
2865 return NULL;
2866 assert(PyList_Check(seq));
2867 it->it_index = PyList_GET_SIZE(seq) - 1;
2868 Py_INCREF(seq);
2869 it->it_seq = seq;
2870 PyObject_GC_Track(it);
2871 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002872}
2873
2874static void
2875listreviter_dealloc(listreviterobject *it)
2876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 PyObject_GC_UnTrack(it);
2878 Py_XDECREF(it->it_seq);
2879 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002880}
2881
2882static int
2883listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 Py_VISIT(it->it_seq);
2886 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002887}
2888
2889static PyObject *
2890listreviter_next(listreviterobject *it)
2891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 PyObject *item;
2893 Py_ssize_t index = it->it_index;
2894 PyListObject *seq = it->it_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2897 item = PyList_GET_ITEM(seq, index);
2898 it->it_index--;
2899 Py_INCREF(item);
2900 return item;
2901 }
2902 it->it_index = -1;
2903 if (seq != NULL) {
2904 it->it_seq = NULL;
2905 Py_DECREF(seq);
2906 }
2907 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002908}
2909
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002910static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002911listreviter_len(listreviterobject *it)
2912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 Py_ssize_t len = it->it_index + 1;
2914 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2915 len = 0;
2916 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002917}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002918
2919static PyObject *
2920listreviter_reduce(listreviterobject *it)
2921{
2922 return listiter_reduce_general(it, 0);
2923}
2924
2925static PyObject *
2926listreviter_setstate(listreviterobject *it, PyObject *state)
2927{
2928 Py_ssize_t index = PyLong_AsSsize_t(state);
2929 if (index == -1 && PyErr_Occurred())
2930 return NULL;
2931 if (it->it_seq != NULL) {
2932 if (index < -1)
2933 index = -1;
2934 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
2935 index = PyList_GET_SIZE(it->it_seq) - 1;
2936 it->it_index = index;
2937 }
2938 Py_RETURN_NONE;
2939}
2940
2941/* common pickling support */
2942
2943static PyObject *
2944listiter_reduce_general(void *_it, int forward)
2945{
2946 PyObject *list;
2947
2948 /* the objects are not the same, index is of different types! */
2949 if (forward) {
2950 listiterobject *it = (listiterobject *)_it;
2951 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +02002952 return Py_BuildValue("N(O)l", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002953 it->it_seq, it->it_index);
2954 } else {
2955 listreviterobject *it = (listreviterobject *)_it;
2956 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +02002957 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("reversed"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002958 it->it_seq, it->it_index);
2959 }
2960 /* empty iterator, create an empty list */
2961 list = PyList_New(0);
2962 if (list == NULL)
2963 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02002964 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002965}