blob: b18ef5763afee5bf9c0a7e3d2f0f2e6b958e95cc [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
David Malcolm49526f42012-06-22 14:55:41 -0400120/* Print summary info about the state of the optimized allocator */
121void
122_PyList_DebugMallocStats(FILE *out)
123{
124 _PyDebugAllocatorStats(out,
125 "free PyListObject",
126 numfree, sizeof(PyListObject));
127}
128
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000130PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 PyListObject *op;
133 size_t nbytes;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000134#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 static int initialized = 0;
136 if (!initialized) {
137 Py_AtExit(show_alloc);
138 initialized = 1;
139 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000140#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 if (size < 0) {
143 PyErr_BadInternalCall();
144 return NULL;
145 }
146 /* Check for overflow without an actual overflow,
147 * which can cause compiler to optimise out */
148 if ((size_t)size > PY_SIZE_MAX / sizeof(PyObject *))
149 return PyErr_NoMemory();
150 nbytes = size * sizeof(PyObject *);
151 if (numfree) {
152 numfree--;
153 op = free_list[numfree];
154 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000155#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000157#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 } else {
159 op = PyObject_GC_New(PyListObject, &PyList_Type);
160 if (op == NULL)
161 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000162#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000164#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 }
166 if (size <= 0)
167 op->ob_item = NULL;
168 else {
169 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
170 if (op->ob_item == NULL) {
171 Py_DECREF(op);
172 return PyErr_NoMemory();
173 }
174 memset(op->ob_item, 0, nbytes);
175 }
176 Py_SIZE(op) = size;
177 op->allocated = size;
178 _PyObject_GC_TRACK(op);
179 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180}
181
Martin v. Löwis18e16552006-02-15 17:27:45 +0000182Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000183PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 if (!PyList_Check(op)) {
186 PyErr_BadInternalCall();
187 return -1;
188 }
189 else
190 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191}
192
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000193static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000194
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000195PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000196PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 if (!PyList_Check(op)) {
199 PyErr_BadInternalCall();
200 return NULL;
201 }
202 if (i < 0 || i >= Py_SIZE(op)) {
203 if (indexerr == NULL) {
204 indexerr = PyUnicode_FromString(
205 "list index out of range");
206 if (indexerr == NULL)
207 return NULL;
208 }
209 PyErr_SetObject(PyExc_IndexError, indexerr);
210 return NULL;
211 }
212 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213}
214
215int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000216PyList_SetItem(register PyObject *op, register Py_ssize_t i,
Fred Drakea2f55112000-07-09 15:16:51 +0000217 register PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 register PyObject *olditem;
220 register PyObject **p;
221 if (!PyList_Check(op)) {
222 Py_XDECREF(newitem);
223 PyErr_BadInternalCall();
224 return -1;
225 }
226 if (i < 0 || i >= Py_SIZE(op)) {
227 Py_XDECREF(newitem);
228 PyErr_SetString(PyExc_IndexError,
229 "list assignment index out of range");
230 return -1;
231 }
232 p = ((PyListObject *)op) -> ob_item + i;
233 olditem = *p;
234 *p = newitem;
235 Py_XDECREF(olditem);
236 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237}
238
239static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000240ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 Py_ssize_t i, n = Py_SIZE(self);
243 PyObject **items;
244 if (v == NULL) {
245 PyErr_BadInternalCall();
246 return -1;
247 }
248 if (n == PY_SSIZE_T_MAX) {
249 PyErr_SetString(PyExc_OverflowError,
250 "cannot add more objects to list");
251 return -1;
252 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 if (list_resize(self, n+1) == -1)
255 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 if (where < 0) {
258 where += n;
259 if (where < 0)
260 where = 0;
261 }
262 if (where > n)
263 where = n;
264 items = self->ob_item;
265 for (i = n; --i >= where; )
266 items[i+1] = items[i];
267 Py_INCREF(v);
268 items[where] = v;
269 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270}
271
272int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000273PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 if (!PyList_Check(op)) {
276 PyErr_BadInternalCall();
277 return -1;
278 }
279 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280}
281
Raymond Hettinger40a03822004-04-12 13:05:09 +0000282static int
283app1(PyListObject *self, PyObject *v)
284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 assert (v != NULL);
288 if (n == PY_SSIZE_T_MAX) {
289 PyErr_SetString(PyExc_OverflowError,
290 "cannot add more objects to list");
291 return -1;
292 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 if (list_resize(self, n+1) == -1)
295 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 Py_INCREF(v);
298 PyList_SET_ITEM(self, n, v);
299 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000300}
301
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302int
Fred Drakea2f55112000-07-09 15:16:51 +0000303PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (PyList_Check(op) && (newitem != NULL))
306 return app1((PyListObject *)op, newitem);
307 PyErr_BadInternalCall();
308 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309}
310
311/* Methods */
312
313static void
Fred Drakea2f55112000-07-09 15:16:51 +0000314list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 Py_ssize_t i;
317 PyObject_GC_UnTrack(op);
318 Py_TRASHCAN_SAFE_BEGIN(op)
319 if (op->ob_item != NULL) {
320 /* Do it backwards, for Christian Tismer.
321 There's a simple test case where somehow this reduces
322 thrashing when a *very* large list is created and
323 immediately deleted. */
324 i = Py_SIZE(op);
325 while (--i >= 0) {
326 Py_XDECREF(op->ob_item[i]);
327 }
328 PyMem_FREE(op->ob_item);
329 }
330 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
331 free_list[numfree++] = op;
332 else
333 Py_TYPE(op)->tp_free((PyObject *)op);
334 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335}
336
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000337static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000338list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 Py_ssize_t i;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200341 PyObject *s = NULL;
342 _PyAccu acc;
343 static PyObject *sep = NULL;
344
345 if (Py_SIZE(v) == 0) {
346 return PyUnicode_FromString("[]");
347 }
348
349 if (sep == NULL) {
350 sep = PyUnicode_FromString(", ");
351 if (sep == NULL)
352 return NULL;
353 }
Guido van Rossumfb376de1998-04-10 22:47:27 +0000354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 i = Py_ReprEnter((PyObject*)v);
356 if (i != 0) {
357 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
358 }
Tim Petersa7259592001-06-16 05:11:17 +0000359
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200360 if (_PyAccu_Init(&acc))
361 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000362
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200363 s = PyUnicode_FromString("[");
364 if (s == NULL || _PyAccu_Accumulate(&acc, s))
365 goto error;
366 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 /* Do repr() on each element. Note that this may mutate the list,
369 so must refetch the list size on each iteration. */
370 for (i = 0; i < Py_SIZE(v); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 if (Py_EnterRecursiveCall(" while getting the repr of a list"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200372 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 s = PyObject_Repr(v->ob_item[i]);
374 Py_LeaveRecursiveCall();
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200375 if (i > 0 && _PyAccu_Accumulate(&acc, sep))
376 goto error;
377 if (s == NULL || _PyAccu_Accumulate(&acc, s))
378 goto error;
379 Py_CLEAR(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 s = PyUnicode_FromString("]");
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200382 if (s == NULL || _PyAccu_Accumulate(&acc, s))
383 goto error;
384 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 Py_ReprLeave((PyObject *)v);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200387 return _PyAccu_Finish(&acc);
388
389error:
390 _PyAccu_Destroy(&acc);
391 Py_XDECREF(s);
392 Py_ReprLeave((PyObject *)v);
393 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394}
395
Martin v. Löwis18e16552006-02-15 17:27:45 +0000396static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000397list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400}
401
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000402static int
Fred Drakea2f55112000-07-09 15:16:51 +0000403list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 Py_ssize_t i;
406 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
409 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
410 Py_EQ);
411 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000412}
413
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000414static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000415list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 if (i < 0 || i >= Py_SIZE(a)) {
418 if (indexerr == NULL) {
419 indexerr = PyUnicode_FromString(
420 "list index out of range");
421 if (indexerr == NULL)
422 return NULL;
423 }
424 PyErr_SetObject(PyExc_IndexError, indexerr);
425 return NULL;
426 }
427 Py_INCREF(a->ob_item[i]);
428 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000429}
430
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000432list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 PyListObject *np;
435 PyObject **src, **dest;
436 Py_ssize_t i, len;
437 if (ilow < 0)
438 ilow = 0;
439 else if (ilow > Py_SIZE(a))
440 ilow = Py_SIZE(a);
441 if (ihigh < ilow)
442 ihigh = ilow;
443 else if (ihigh > Py_SIZE(a))
444 ihigh = Py_SIZE(a);
445 len = ihigh - ilow;
446 np = (PyListObject *) PyList_New(len);
447 if (np == NULL)
448 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 src = a->ob_item + ilow;
451 dest = np->ob_item;
452 for (i = 0; i < len; i++) {
453 PyObject *v = src[i];
454 Py_INCREF(v);
455 dest[i] = v;
456 }
457 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458}
459
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000460PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000461PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (!PyList_Check(a)) {
464 PyErr_BadInternalCall();
465 return NULL;
466 }
467 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000468}
469
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000470static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000471list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 Py_ssize_t size;
474 Py_ssize_t i;
475 PyObject **src, **dest;
476 PyListObject *np;
477 if (!PyList_Check(bb)) {
478 PyErr_Format(PyExc_TypeError,
479 "can only concatenate list (not \"%.200s\") to list",
480 bb->ob_type->tp_name);
481 return NULL;
482 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483#define b ((PyListObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 size = Py_SIZE(a) + Py_SIZE(b);
485 if (size < 0)
486 return PyErr_NoMemory();
487 np = (PyListObject *) PyList_New(size);
488 if (np == NULL) {
489 return NULL;
490 }
491 src = a->ob_item;
492 dest = np->ob_item;
493 for (i = 0; i < Py_SIZE(a); i++) {
494 PyObject *v = src[i];
495 Py_INCREF(v);
496 dest[i] = v;
497 }
498 src = b->ob_item;
499 dest = np->ob_item + Py_SIZE(a);
500 for (i = 0; i < Py_SIZE(b); i++) {
501 PyObject *v = src[i];
502 Py_INCREF(v);
503 dest[i] = v;
504 }
505 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000506#undef b
507}
508
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000509static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000510list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 Py_ssize_t i, j;
513 Py_ssize_t size;
514 PyListObject *np;
515 PyObject **p, **items;
516 PyObject *elem;
517 if (n < 0)
518 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100519 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100521 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 if (size == 0)
523 return PyList_New(0);
524 np = (PyListObject *) PyList_New(size);
525 if (np == NULL)
526 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 items = np->ob_item;
529 if (Py_SIZE(a) == 1) {
530 elem = a->ob_item[0];
531 for (i = 0; i < n; i++) {
532 items[i] = elem;
533 Py_INCREF(elem);
534 }
535 return (PyObject *) np;
536 }
537 p = np->ob_item;
538 items = a->ob_item;
539 for (i = 0; i < n; i++) {
540 for (j = 0; j < Py_SIZE(a); j++) {
541 *p = items[j];
542 Py_INCREF(*p);
543 p++;
544 }
545 }
546 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000547}
548
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000549static int
Armin Rigo93677f02004-07-29 12:40:23 +0000550list_clear(PyListObject *a)
551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 Py_ssize_t i;
553 PyObject **item = a->ob_item;
554 if (item != NULL) {
555 /* Because XDECREF can recursively invoke operations on
556 this list, we make it empty first. */
557 i = Py_SIZE(a);
558 Py_SIZE(a) = 0;
559 a->ob_item = NULL;
560 a->allocated = 0;
561 while (--i >= 0) {
562 Py_XDECREF(item[i]);
563 }
564 PyMem_FREE(item);
565 }
566 /* Never fails; the return value can be ignored.
567 Note that there is no guarantee that the list is actually empty
568 at this point, because XDECREF may have populated it again! */
569 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000570}
571
Tim Peters8fc4a912004-07-31 21:53:19 +0000572/* a[ilow:ihigh] = v if v != NULL.
573 * del a[ilow:ihigh] if v == NULL.
574 *
575 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
576 * guaranteed the call cannot fail.
577 */
Armin Rigo93677f02004-07-29 12:40:23 +0000578static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000579list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 /* Because [X]DECREF can recursively invoke list operations on
582 this list, we must postpone all [X]DECREF activity until
583 after the list is back in its canonical shape. Therefore
584 we must allocate an additional array, 'recycle', into which
585 we temporarily copy the items that are deleted from the
586 list. :-( */
587 PyObject *recycle_on_stack[8];
588 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
589 PyObject **item;
590 PyObject **vitem = NULL;
591 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
592 Py_ssize_t n; /* # of elements in replacement list */
593 Py_ssize_t norig; /* # of elements in list getting replaced */
594 Py_ssize_t d; /* Change in size */
595 Py_ssize_t k;
596 size_t s;
597 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000598#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 if (v == NULL)
600 n = 0;
601 else {
602 if (a == b) {
603 /* Special case "a[i:j] = a" -- copy b first */
604 v = list_slice(b, 0, Py_SIZE(b));
605 if (v == NULL)
606 return result;
607 result = list_ass_slice(a, ilow, ihigh, v);
608 Py_DECREF(v);
609 return result;
610 }
611 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
612 if(v_as_SF == NULL)
613 goto Error;
614 n = PySequence_Fast_GET_SIZE(v_as_SF);
615 vitem = PySequence_Fast_ITEMS(v_as_SF);
616 }
617 if (ilow < 0)
618 ilow = 0;
619 else if (ilow > Py_SIZE(a))
620 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 if (ihigh < ilow)
623 ihigh = ilow;
624 else if (ihigh > Py_SIZE(a))
625 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 norig = ihigh - ilow;
628 assert(norig >= 0);
629 d = n - norig;
630 if (Py_SIZE(a) + d == 0) {
631 Py_XDECREF(v_as_SF);
632 return list_clear(a);
633 }
634 item = a->ob_item;
635 /* recycle the items that we are about to remove */
636 s = norig * sizeof(PyObject *);
637 if (s > sizeof(recycle_on_stack)) {
638 recycle = (PyObject **)PyMem_MALLOC(s);
639 if (recycle == NULL) {
640 PyErr_NoMemory();
641 goto Error;
642 }
643 }
644 memcpy(recycle, &item[ilow], s);
Tim Peters8d9eb102004-07-31 02:24:20 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 if (d < 0) { /* Delete -d items */
647 memmove(&item[ihigh+d], &item[ihigh],
648 (Py_SIZE(a) - ihigh)*sizeof(PyObject *));
649 list_resize(a, Py_SIZE(a) + d);
650 item = a->ob_item;
651 }
652 else if (d > 0) { /* Insert d items */
653 k = Py_SIZE(a);
654 if (list_resize(a, k+d) < 0)
655 goto Error;
656 item = a->ob_item;
657 memmove(&item[ihigh+d], &item[ihigh],
658 (k - ihigh)*sizeof(PyObject *));
659 }
660 for (k = 0; k < n; k++, ilow++) {
661 PyObject *w = vitem[k];
662 Py_XINCREF(w);
663 item[ilow] = w;
664 }
665 for (k = norig - 1; k >= 0; --k)
666 Py_XDECREF(recycle[k]);
667 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000668 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 if (recycle != recycle_on_stack)
670 PyMem_FREE(recycle);
671 Py_XDECREF(v_as_SF);
672 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673#undef b
674}
675
Guido van Rossum234f9421993-06-17 12:35:49 +0000676int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000677PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 if (!PyList_Check(a)) {
680 PyErr_BadInternalCall();
681 return -1;
682 }
683 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000684}
685
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000686static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000687list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 PyObject **items;
690 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000691
692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 size = PyList_GET_SIZE(self);
694 if (size == 0 || n == 1) {
695 Py_INCREF(self);
696 return (PyObject *)self;
697 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 if (n < 1) {
700 (void)list_clear(self);
701 Py_INCREF(self);
702 return (PyObject *)self;
703 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 if (size > PY_SSIZE_T_MAX / n) {
706 return PyErr_NoMemory();
707 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 if (list_resize(self, size*n) == -1)
710 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 p = size;
713 items = self->ob_item;
714 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
715 for (j = 0; j < size; j++) {
716 PyObject *o = items[j];
717 Py_INCREF(o);
718 items[p++] = o;
719 }
720 }
721 Py_INCREF(self);
722 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000723}
724
Guido van Rossum4a450d01991-04-03 19:05:18 +0000725static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000726list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 PyObject *old_value;
729 if (i < 0 || i >= Py_SIZE(a)) {
730 PyErr_SetString(PyExc_IndexError,
731 "list assignment index out of range");
732 return -1;
733 }
734 if (v == NULL)
735 return list_ass_slice(a, i, i+1, v);
736 Py_INCREF(v);
737 old_value = a->ob_item[i];
738 a->ob_item[i] = v;
739 Py_DECREF(old_value);
740 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000741}
742
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000743static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000744listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 Py_ssize_t i;
747 PyObject *v;
748 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
749 return NULL;
750 if (ins1(self, i, v) == 0)
751 Py_RETURN_NONE;
752 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000753}
754
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000755static PyObject *
Eli Benderskycbbaa962011-02-25 05:47:53 +0000756listclear(PyListObject *self)
757{
758 list_clear(self);
759 Py_RETURN_NONE;
760}
761
762static PyObject *
763listcopy(PyListObject *self)
764{
765 return list_slice(self, 0, Py_SIZE(self));
766}
767
768static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000769listappend(PyListObject *self, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 if (app1(self, v) == 0)
772 Py_RETURN_NONE;
773 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000774}
775
Barry Warsawdedf6d61998-10-09 16:37:25 +0000776static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000777listextend(PyListObject *self, PyObject *b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 PyObject *it; /* iter(v) */
780 Py_ssize_t m; /* size of self */
781 Py_ssize_t n; /* guess for size of b */
782 Py_ssize_t mn; /* m + n */
783 Py_ssize_t i;
784 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 /* Special cases:
787 1) lists and tuples which can use PySequence_Fast ops
788 2) extending self to self requires making a copy first
789 */
790 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
791 PyObject **src, **dest;
792 b = PySequence_Fast(b, "argument must be iterable");
793 if (!b)
794 return NULL;
795 n = PySequence_Fast_GET_SIZE(b);
796 if (n == 0) {
797 /* short circuit when b is empty */
798 Py_DECREF(b);
799 Py_RETURN_NONE;
800 }
801 m = Py_SIZE(self);
802 if (list_resize(self, m + n) == -1) {
803 Py_DECREF(b);
804 return NULL;
805 }
806 /* note that we may still have self == b here for the
807 * situation a.extend(a), but the following code works
808 * in that case too. Just make sure to resize self
809 * before calling PySequence_Fast_ITEMS.
810 */
811 /* populate the end of self with b's items */
812 src = PySequence_Fast_ITEMS(b);
813 dest = self->ob_item + m;
814 for (i = 0; i < n; i++) {
815 PyObject *o = src[i];
816 Py_INCREF(o);
817 dest[i] = o;
818 }
819 Py_DECREF(b);
820 Py_RETURN_NONE;
821 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 it = PyObject_GetIter(b);
824 if (it == NULL)
825 return NULL;
826 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 /* Guess a result list size. */
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200829 n = PyObject_LengthHint(b, 8);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 if (n == -1) {
831 Py_DECREF(it);
832 return NULL;
833 }
834 m = Py_SIZE(self);
835 mn = m + n;
836 if (mn >= m) {
837 /* Make room. */
838 if (list_resize(self, mn) == -1)
839 goto error;
840 /* Make the list sane again. */
841 Py_SIZE(self) = m;
842 }
843 /* Else m + n overflowed; on the chance that n lied, and there really
844 * is enough room, ignore it. If n was telling the truth, we'll
845 * eventually run out of memory during the loop.
846 */
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 /* Run iterator to exhaustion. */
849 for (;;) {
850 PyObject *item = iternext(it);
851 if (item == NULL) {
852 if (PyErr_Occurred()) {
853 if (PyErr_ExceptionMatches(PyExc_StopIteration))
854 PyErr_Clear();
855 else
856 goto error;
857 }
858 break;
859 }
860 if (Py_SIZE(self) < self->allocated) {
861 /* steals ref */
862 PyList_SET_ITEM(self, Py_SIZE(self), item);
863 ++Py_SIZE(self);
864 }
865 else {
866 int status = app1(self, item);
867 Py_DECREF(item); /* append creates a new ref */
868 if (status < 0)
869 goto error;
870 }
871 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 /* Cut back result list if initial guess was too large. */
874 if (Py_SIZE(self) < self->allocated)
875 list_resize(self, Py_SIZE(self)); /* shrinking can't fail */
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 Py_DECREF(it);
878 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000879
880 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 Py_DECREF(it);
882 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000883}
884
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000885PyObject *
886_PyList_Extend(PyListObject *self, PyObject *b)
887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 return listextend(self, b);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000889}
890
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000891static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000892list_inplace_concat(PyListObject *self, PyObject *other)
893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 result = listextend(self, other);
897 if (result == NULL)
898 return result;
899 Py_DECREF(result);
900 Py_INCREF(self);
901 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000902}
903
904static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000905listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 Py_ssize_t i = -1;
908 PyObject *v;
909 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 if (!PyArg_ParseTuple(args, "|n:pop", &i))
912 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 if (Py_SIZE(self) == 0) {
915 /* Special-case most common failure cause */
916 PyErr_SetString(PyExc_IndexError, "pop from empty list");
917 return NULL;
918 }
919 if (i < 0)
920 i += Py_SIZE(self);
921 if (i < 0 || i >= Py_SIZE(self)) {
922 PyErr_SetString(PyExc_IndexError, "pop index out of range");
923 return NULL;
924 }
925 v = self->ob_item[i];
926 if (i == Py_SIZE(self) - 1) {
927 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +0200928 if (status >= 0)
929 return v; /* and v now owns the reference the list had */
930 else
931 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 }
933 Py_INCREF(v);
934 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
935 assert(status >= 0);
936 /* Use status, so that in a release build compilers don't
937 * complain about the unused name.
938 */
939 (void) status;
Brett Cannon651dd522004-08-08 21:21:18 +0000940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000942}
943
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000944/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
945static void
946reverse_slice(PyObject **lo, PyObject **hi)
947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 --hi;
951 while (lo < hi) {
952 PyObject *t = *lo;
953 *lo = *hi;
954 *hi = t;
955 ++lo;
956 --hi;
957 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000958}
959
Tim Petersa64dc242002-08-01 02:13:36 +0000960/* Lots of code for an adaptive, stable, natural mergesort. There are many
961 * pieces to this algorithm; read listsort.txt for overviews and details.
962 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000963
Daniel Stutzbach98338222010-12-02 21:55:33 +0000964/* A sortslice contains a pointer to an array of keys and a pointer to
965 * an array of corresponding values. In other words, keys[i]
966 * corresponds with values[i]. If values == NULL, then the keys are
967 * also the values.
968 *
969 * Several convenience routines are provided here, so that keys and
970 * values are always moved in sync.
971 */
972
973typedef struct {
974 PyObject **keys;
975 PyObject **values;
976} sortslice;
977
978Py_LOCAL_INLINE(void)
979sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
980{
981 s1->keys[i] = s2->keys[j];
982 if (s1->values != NULL)
983 s1->values[i] = s2->values[j];
984}
985
986Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000987sortslice_copy_incr(sortslice *dst, sortslice *src)
988{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000989 *dst->keys++ = *src->keys++;
990 if (dst->values != NULL)
991 *dst->values++ = *src->values++;
992}
993
994Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000995sortslice_copy_decr(sortslice *dst, sortslice *src)
996{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000997 *dst->keys-- = *src->keys--;
998 if (dst->values != NULL)
999 *dst->values-- = *src->values--;
1000}
1001
1002
1003Py_LOCAL_INLINE(void)
1004sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001005 Py_ssize_t n)
1006{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001007 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1008 if (s1->values != NULL)
1009 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1010}
1011
1012Py_LOCAL_INLINE(void)
1013sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001014 Py_ssize_t n)
1015{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001016 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1017 if (s1->values != NULL)
1018 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1019}
1020
1021Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001022sortslice_advance(sortslice *slice, Py_ssize_t n)
1023{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001024 slice->keys += n;
1025 if (slice->values != NULL)
1026 slice->values += n;
1027}
1028
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001029/* Comparison function: PyObject_RichCompareBool with Py_LT.
Tim Petersa64dc242002-08-01 02:13:36 +00001030 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1031 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001032
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001033#define ISLT(X, Y) (PyObject_RichCompareBool(X, Y, Py_LT))
Tim Peters66860f62002-08-04 17:47:26 +00001034
1035/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001036 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1037 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1038*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001039#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001041
1042/* binarysort is the best method for sorting small arrays: it does
1043 few compares, but can do data movement quadratic in the number of
1044 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001045 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001046 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001047 On entry, must have lo <= start <= hi, and that [lo, start) is already
1048 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001049 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001050 Even in case of error, the output slice will be some permutation of
1051 the input (nothing is lost or duplicated).
1052*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001053static int
Daniel Stutzbach98338222010-12-02 21:55:33 +00001054binarysort(sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 register Py_ssize_t k;
1057 register PyObject **l, **p, **r;
1058 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001059
Daniel Stutzbach98338222010-12-02 21:55:33 +00001060 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001062 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 ++start;
1064 for (; start < hi; ++start) {
1065 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001066 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 r = start;
1068 pivot = *r;
1069 /* Invariants:
1070 * pivot >= all in [lo, l).
1071 * pivot < all in [r, start).
1072 * The second is vacuously true at the start.
1073 */
1074 assert(l < r);
1075 do {
1076 p = l + ((r - l) >> 1);
1077 IFLT(pivot, *p)
1078 r = p;
1079 else
1080 l = p+1;
1081 } while (l < r);
1082 assert(l == r);
1083 /* The invariants still hold, so pivot >= all in [lo, l) and
1084 pivot < all in [l, start), so pivot belongs at l. Note
1085 that if there are elements equal to pivot, l points to the
1086 first slot after them -- that's why this sort is stable.
1087 Slide over to make room.
1088 Caution: using memmove is much slower under MSVC 5;
1089 we're not usually moving many slots. */
1090 for (p = start; p > l; --p)
1091 *p = *(p-1);
1092 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001093 if (lo.values != NULL) {
1094 Py_ssize_t offset = lo.values - lo.keys;
1095 p = start + offset;
1096 pivot = *p;
1097 l += offset;
1098 for (p = start + offset; p > l; --p)
1099 *p = *(p-1);
1100 *l = pivot;
1101 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 }
1103 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001104
1105 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001107}
1108
Tim Petersa64dc242002-08-01 02:13:36 +00001109/*
1110Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1111is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001112
Tim Petersa64dc242002-08-01 02:13:36 +00001113 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001114
Tim Petersa64dc242002-08-01 02:13:36 +00001115or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001116
Tim Petersa64dc242002-08-01 02:13:36 +00001117 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001118
Tim Petersa64dc242002-08-01 02:13:36 +00001119Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1120For its intended use in a stable mergesort, the strictness of the defn of
1121"descending" is needed so that the caller can safely reverse a descending
1122sequence without violating stability (strict > ensures there are no equal
1123elements to get out of order).
1124
1125Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001126*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001127static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001128count_run(PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 Py_ssize_t k;
1131 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 assert(lo < hi);
1134 *descending = 0;
1135 ++lo;
1136 if (lo == hi)
1137 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 n = 2;
1140 IFLT(*lo, *(lo-1)) {
1141 *descending = 1;
1142 for (lo = lo+1; lo < hi; ++lo, ++n) {
1143 IFLT(*lo, *(lo-1))
1144 ;
1145 else
1146 break;
1147 }
1148 }
1149 else {
1150 for (lo = lo+1; lo < hi; ++lo, ++n) {
1151 IFLT(*lo, *(lo-1))
1152 break;
1153 }
1154 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001157fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001159}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001160
Tim Petersa64dc242002-08-01 02:13:36 +00001161/*
1162Locate the proper position of key in a sorted vector; if the vector contains
1163an element equal to key, return the position immediately to the left of
1164the leftmost equal element. [gallop_right() does the same except returns
1165the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001166
Tim Petersa64dc242002-08-01 02:13:36 +00001167"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001168
Tim Petersa64dc242002-08-01 02:13:36 +00001169"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1170hint is to the final result, the faster this runs.
1171
1172The return value is the int k in 0..n such that
1173
1174 a[k-1] < key <= a[k]
1175
1176pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1177key belongs at index k; or, IOW, the first k elements of a should precede
1178key, and the last n-k should follow key.
1179
1180Returns -1 on error. See listsort.txt for info on the method.
1181*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001182static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001183gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 Py_ssize_t ofs;
1186 Py_ssize_t lastofs;
1187 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 a += hint;
1192 lastofs = 0;
1193 ofs = 1;
1194 IFLT(*a, key) {
1195 /* a[hint] < key -- gallop right, until
1196 * a[hint + lastofs] < key <= a[hint + ofs]
1197 */
1198 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1199 while (ofs < maxofs) {
1200 IFLT(a[ofs], key) {
1201 lastofs = ofs;
1202 ofs = (ofs << 1) + 1;
1203 if (ofs <= 0) /* int overflow */
1204 ofs = maxofs;
1205 }
1206 else /* key <= a[hint + ofs] */
1207 break;
1208 }
1209 if (ofs > maxofs)
1210 ofs = maxofs;
1211 /* Translate back to offsets relative to &a[0]. */
1212 lastofs += hint;
1213 ofs += hint;
1214 }
1215 else {
1216 /* key <= a[hint] -- gallop left, until
1217 * a[hint - ofs] < key <= a[hint - lastofs]
1218 */
1219 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1220 while (ofs < maxofs) {
1221 IFLT(*(a-ofs), key)
1222 break;
1223 /* key <= a[hint - ofs] */
1224 lastofs = ofs;
1225 ofs = (ofs << 1) + 1;
1226 if (ofs <= 0) /* int overflow */
1227 ofs = maxofs;
1228 }
1229 if (ofs > maxofs)
1230 ofs = maxofs;
1231 /* Translate back to positive offsets relative to &a[0]. */
1232 k = lastofs;
1233 lastofs = hint - ofs;
1234 ofs = hint - k;
1235 }
1236 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1239 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1240 * right of lastofs but no farther right than ofs. Do a binary
1241 * search, with invariant a[lastofs-1] < key <= a[ofs].
1242 */
1243 ++lastofs;
1244 while (lastofs < ofs) {
1245 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 IFLT(a[m], key)
1248 lastofs = m+1; /* a[m] < key */
1249 else
1250 ofs = m; /* key <= a[m] */
1251 }
1252 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1253 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001254
1255fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001257}
1258
1259/*
1260Exactly like gallop_left(), except that if key already exists in a[0:n],
1261finds the position immediately to the right of the rightmost equal value.
1262
1263The return value is the int k in 0..n such that
1264
1265 a[k-1] <= key < a[k]
1266
1267or -1 if error.
1268
1269The code duplication is massive, but this is enough different given that
1270we're sticking to "<" comparisons that it's much harder to follow if
1271written as one routine with yet another "left or right?" flag.
1272*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001273static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001274gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 Py_ssize_t ofs;
1277 Py_ssize_t lastofs;
1278 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 a += hint;
1283 lastofs = 0;
1284 ofs = 1;
1285 IFLT(key, *a) {
1286 /* key < a[hint] -- gallop left, until
1287 * a[hint - ofs] <= key < a[hint - lastofs]
1288 */
1289 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1290 while (ofs < maxofs) {
1291 IFLT(key, *(a-ofs)) {
1292 lastofs = ofs;
1293 ofs = (ofs << 1) + 1;
1294 if (ofs <= 0) /* int overflow */
1295 ofs = maxofs;
1296 }
1297 else /* a[hint - ofs] <= key */
1298 break;
1299 }
1300 if (ofs > maxofs)
1301 ofs = maxofs;
1302 /* Translate back to positive offsets relative to &a[0]. */
1303 k = lastofs;
1304 lastofs = hint - ofs;
1305 ofs = hint - k;
1306 }
1307 else {
1308 /* a[hint] <= key -- gallop right, until
1309 * a[hint + lastofs] <= key < a[hint + ofs]
1310 */
1311 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1312 while (ofs < maxofs) {
1313 IFLT(key, a[ofs])
1314 break;
1315 /* a[hint + ofs] <= key */
1316 lastofs = ofs;
1317 ofs = (ofs << 1) + 1;
1318 if (ofs <= 0) /* int overflow */
1319 ofs = maxofs;
1320 }
1321 if (ofs > maxofs)
1322 ofs = maxofs;
1323 /* Translate back to offsets relative to &a[0]. */
1324 lastofs += hint;
1325 ofs += hint;
1326 }
1327 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1330 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1331 * right of lastofs but no farther right than ofs. Do a binary
1332 * search, with invariant a[lastofs-1] <= key < a[ofs].
1333 */
1334 ++lastofs;
1335 while (lastofs < ofs) {
1336 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 IFLT(key, a[m])
1339 ofs = m; /* key < a[m] */
1340 else
1341 lastofs = m+1; /* a[m] <= key */
1342 }
1343 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1344 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001345
1346fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001348}
1349
1350/* The maximum number of entries in a MergeState's pending-runs stack.
1351 * This is enough to sort arrays of size up to about
1352 * 32 * phi ** MAX_MERGE_PENDING
1353 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1354 * with 2**64 elements.
1355 */
1356#define MAX_MERGE_PENDING 85
1357
Tim Peterse05f65a2002-08-10 05:21:15 +00001358/* When we get into galloping mode, we stay there until both runs win less
1359 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
Tim Petersa64dc242002-08-01 02:13:36 +00001360 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001361#define MIN_GALLOP 7
Tim Petersa64dc242002-08-01 02:13:36 +00001362
1363/* Avoid malloc for small temp arrays. */
1364#define MERGESTATE_TEMP_SIZE 256
1365
1366/* One MergeState exists on the stack per invocation of mergesort. It's just
1367 * a convenient way to pass state around among the helper functions.
1368 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001369struct s_slice {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001370 sortslice base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 Py_ssize_t len;
Tim Peterse05f65a2002-08-10 05:21:15 +00001372};
1373
Tim Petersa64dc242002-08-01 02:13:36 +00001374typedef struct s_MergeState {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 /* This controls when we get *into* galloping mode. It's initialized
1376 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1377 * random data, and lower for highly structured data.
1378 */
1379 Py_ssize_t min_gallop;
Tim Peterse05f65a2002-08-10 05:21:15 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 /* 'a' is temp storage to help with merges. It contains room for
1382 * alloced entries.
1383 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001384 sortslice a; /* may point to temparray below */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 Py_ssize_t alloced;
Tim Petersa64dc242002-08-01 02:13:36 +00001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 /* A stack of n pending runs yet to be merged. Run #i starts at
1388 * address base[i] and extends for len[i] elements. It's always
1389 * true (so long as the indices are in bounds) that
1390 *
1391 * pending[i].base + pending[i].len == pending[i+1].base
1392 *
1393 * so we could cut the storage for this, but it's a minor amount,
1394 * and keeping all the info explicit simplifies the code.
1395 */
1396 int n;
1397 struct s_slice pending[MAX_MERGE_PENDING];
Tim Petersa64dc242002-08-01 02:13:36 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 /* 'a' points to this when possible, rather than muck with malloc. */
1400 PyObject *temparray[MERGESTATE_TEMP_SIZE];
Tim Petersa64dc242002-08-01 02:13:36 +00001401} MergeState;
1402
1403/* Conceptually a MergeState's constructor. */
1404static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001405merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001408 if (has_keyfunc) {
1409 /* The temporary space for merging will need at most half the list
1410 * size rounded up. Use the minimum possible space so we can use the
1411 * rest of temparray for other things. In particular, if there is
1412 * enough extra space, listsort() will use it to store the keys.
1413 */
1414 ms->alloced = (list_size + 1) / 2;
1415
1416 /* ms->alloced describes how many keys will be stored at
1417 ms->temparray, but we also need to store the values. Hence,
1418 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1419 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1420 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1421 ms->a.values = &ms->temparray[ms->alloced];
1422 }
1423 else {
1424 ms->alloced = MERGESTATE_TEMP_SIZE;
1425 ms->a.values = NULL;
1426 }
1427 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 ms->n = 0;
1429 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001430}
1431
1432/* Free all the temp memory owned by the MergeState. This must be called
1433 * when you're done with a MergeState, and may be called before then if
1434 * you want to free the temp memory early.
1435 */
1436static void
1437merge_freemem(MergeState *ms)
1438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001440 if (ms->a.keys != ms->temparray)
1441 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001442}
1443
1444/* Ensure enough temp memory for 'need' array slots is available.
1445 * Returns 0 on success and -1 if the memory can't be gotten.
1446 */
1447static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001448merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001449{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001450 int multiplier;
1451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 assert(ms != NULL);
1453 if (need <= ms->alloced)
1454 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001455
1456 multiplier = ms->a.values != NULL ? 2 : 1;
1457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 /* Don't realloc! That can cost cycles to copy the old data, but
1459 * we don't care what's in the block.
1460 */
1461 merge_freemem(ms);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001462 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 PyErr_NoMemory();
1464 return -1;
1465 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001466 ms->a.keys = (PyObject**)PyMem_Malloc(multiplier * need
1467 * sizeof(PyObject *));
1468 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001470 if (ms->a.values != NULL)
1471 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 return 0;
1473 }
1474 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001476}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1478 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001479
Daniel Stutzbach98338222010-12-02 21:55:33 +00001480/* Merge the na elements starting at ssa with the nb elements starting at
1481 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1482 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1483 * should have na <= nb. See listsort.txt for more info. Return 0 if
1484 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001485 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001486static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001487merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1488 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001491 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 int result = -1; /* guilty until proved innocent */
1493 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001494
Daniel Stutzbach98338222010-12-02 21:55:33 +00001495 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1496 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 if (MERGE_GETMEM(ms, na) < 0)
1498 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001499 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1500 dest = ssa;
1501 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001502
Daniel Stutzbach98338222010-12-02 21:55:33 +00001503 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 --nb;
1505 if (nb == 0)
1506 goto Succeed;
1507 if (na == 1)
1508 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 min_gallop = ms->min_gallop;
1511 for (;;) {
1512 Py_ssize_t acount = 0; /* # of times A won in a row */
1513 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 /* Do the straightforward thing until (if ever) one run
1516 * appears to win consistently.
1517 */
1518 for (;;) {
1519 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001520 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 if (k) {
1522 if (k < 0)
1523 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001524 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 ++bcount;
1526 acount = 0;
1527 --nb;
1528 if (nb == 0)
1529 goto Succeed;
1530 if (bcount >= min_gallop)
1531 break;
1532 }
1533 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001534 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 ++acount;
1536 bcount = 0;
1537 --na;
1538 if (na == 1)
1539 goto CopyB;
1540 if (acount >= min_gallop)
1541 break;
1542 }
1543 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 /* One run is winning so consistently that galloping may
1546 * be a huge win. So try that, and continue galloping until
1547 * (if ever) neither run appears to be winning consistently
1548 * anymore.
1549 */
1550 ++min_gallop;
1551 do {
1552 assert(na > 1 && nb > 0);
1553 min_gallop -= min_gallop > 1;
1554 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001555 k = gallop_right(ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 acount = k;
1557 if (k) {
1558 if (k < 0)
1559 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001560 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1561 sortslice_advance(&dest, k);
1562 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 na -= k;
1564 if (na == 1)
1565 goto CopyB;
1566 /* na==0 is impossible now if the comparison
1567 * function is consistent, but we can't assume
1568 * that it is.
1569 */
1570 if (na == 0)
1571 goto Succeed;
1572 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001573 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 --nb;
1575 if (nb == 0)
1576 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001577
Daniel Stutzbach98338222010-12-02 21:55:33 +00001578 k = gallop_left(ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 bcount = k;
1580 if (k) {
1581 if (k < 0)
1582 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001583 sortslice_memmove(&dest, 0, &ssb, 0, k);
1584 sortslice_advance(&dest, k);
1585 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 nb -= k;
1587 if (nb == 0)
1588 goto Succeed;
1589 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001590 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 --na;
1592 if (na == 1)
1593 goto CopyB;
1594 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1595 ++min_gallop; /* penalize it for leaving galloping mode */
1596 ms->min_gallop = min_gallop;
1597 }
Tim Petersa64dc242002-08-01 02:13:36 +00001598Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001600Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001602 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001604CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001606 /* The last element of ssa belongs at the end of the merge. */
1607 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1608 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001610}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001611
Daniel Stutzbach98338222010-12-02 21:55:33 +00001612/* Merge the na elements starting at pa with the nb elements starting at
1613 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1614 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1615 * should have na >= nb. See listsort.txt for more info. Return 0 if
1616 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001617 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001618static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001619merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1620 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001623 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001626
Daniel Stutzbach98338222010-12-02 21:55:33 +00001627 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1628 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 if (MERGE_GETMEM(ms, nb) < 0)
1630 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001631 dest = ssb;
1632 sortslice_advance(&dest, nb-1);
1633 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1634 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001636 ssb.keys = ms->a.keys + nb - 1;
1637 if (ssb.values != NULL)
1638 ssb.values = ms->a.values + nb - 1;
1639 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001640
Daniel Stutzbach98338222010-12-02 21:55:33 +00001641 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 --na;
1643 if (na == 0)
1644 goto Succeed;
1645 if (nb == 1)
1646 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 min_gallop = ms->min_gallop;
1649 for (;;) {
1650 Py_ssize_t acount = 0; /* # of times A won in a row */
1651 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 /* Do the straightforward thing until (if ever) one run
1654 * appears to win consistently.
1655 */
1656 for (;;) {
1657 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001658 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 if (k) {
1660 if (k < 0)
1661 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001662 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 ++acount;
1664 bcount = 0;
1665 --na;
1666 if (na == 0)
1667 goto Succeed;
1668 if (acount >= min_gallop)
1669 break;
1670 }
1671 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001672 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 ++bcount;
1674 acount = 0;
1675 --nb;
1676 if (nb == 1)
1677 goto CopyA;
1678 if (bcount >= min_gallop)
1679 break;
1680 }
1681 }
Tim Petersa64dc242002-08-01 02:13:36 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 /* One run is winning so consistently that galloping may
1684 * be a huge win. So try that, and continue galloping until
1685 * (if ever) neither run appears to be winning consistently
1686 * anymore.
1687 */
1688 ++min_gallop;
1689 do {
1690 assert(na > 0 && nb > 1);
1691 min_gallop -= min_gallop > 1;
1692 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001693 k = gallop_right(ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 if (k < 0)
1695 goto Fail;
1696 k = na - k;
1697 acount = k;
1698 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001699 sortslice_advance(&dest, -k);
1700 sortslice_advance(&ssa, -k);
1701 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 na -= k;
1703 if (na == 0)
1704 goto Succeed;
1705 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001706 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 --nb;
1708 if (nb == 1)
1709 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001710
Daniel Stutzbach98338222010-12-02 21:55:33 +00001711 k = gallop_left(ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 if (k < 0)
1713 goto Fail;
1714 k = nb - k;
1715 bcount = k;
1716 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001717 sortslice_advance(&dest, -k);
1718 sortslice_advance(&ssb, -k);
1719 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 nb -= k;
1721 if (nb == 1)
1722 goto CopyA;
1723 /* nb==0 is impossible now if the comparison
1724 * function is consistent, but we can't assume
1725 * that it is.
1726 */
1727 if (nb == 0)
1728 goto Succeed;
1729 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001730 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 --na;
1732 if (na == 0)
1733 goto Succeed;
1734 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1735 ++min_gallop; /* penalize it for leaving galloping mode */
1736 ms->min_gallop = min_gallop;
1737 }
Tim Petersa64dc242002-08-01 02:13:36 +00001738Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001740Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001742 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001744CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001746 /* The first element of ssb belongs at the front of the merge. */
1747 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1748 sortslice_advance(&dest, -na);
1749 sortslice_advance(&ssa, -na);
1750 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001752}
1753
1754/* Merge the two runs at stack indices i and i+1.
1755 * Returns 0 on success, -1 on error.
1756 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001757static Py_ssize_t
1758merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001759{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001760 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 Py_ssize_t na, nb;
1762 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 assert(ms != NULL);
1765 assert(ms->n >= 2);
1766 assert(i >= 0);
1767 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001768
Daniel Stutzbach98338222010-12-02 21:55:33 +00001769 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001771 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 nb = ms->pending[i+1].len;
1773 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001774 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 /* Record the length of the combined runs; if i is the 3rd-last
1777 * run now, also slide over the last run (which isn't involved
1778 * in this merge). The current run i+1 goes away in any case.
1779 */
1780 ms->pending[i].len = na + nb;
1781 if (i == ms->n - 3)
1782 ms->pending[i+1] = ms->pending[i+2];
1783 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 /* Where does b start in a? Elements in a before that can be
1786 * ignored (already in place).
1787 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001788 k = gallop_right(*ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 if (k < 0)
1790 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001791 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 na -= k;
1793 if (na == 0)
1794 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 /* Where does a end in b? Elements in b after that can be
1797 * ignored (already in place).
1798 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001799 nb = gallop_left(ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 if (nb <= 0)
1801 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 /* Merge what remains of the runs, using a temp array with
1804 * min(na, nb) elements.
1805 */
1806 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001807 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001809 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001810}
1811
1812/* Examine the stack of runs waiting to be merged, merging adjacent runs
1813 * until the stack invariants are re-established:
1814 *
1815 * 1. len[-3] > len[-2] + len[-1]
1816 * 2. len[-2] > len[-1]
1817 *
1818 * See listsort.txt for more info.
1819 *
1820 * Returns 0 on success, -1 on error.
1821 */
1822static int
1823merge_collapse(MergeState *ms)
1824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 assert(ms);
1828 while (ms->n > 1) {
1829 Py_ssize_t n = ms->n - 2;
1830 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
1831 if (p[n-1].len < p[n+1].len)
1832 --n;
1833 if (merge_at(ms, n) < 0)
1834 return -1;
1835 }
1836 else if (p[n].len <= p[n+1].len) {
1837 if (merge_at(ms, n) < 0)
1838 return -1;
1839 }
1840 else
1841 break;
1842 }
1843 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001844}
1845
1846/* Regardless of invariants, merge all runs on the stack until only one
1847 * remains. This is used at the end of the mergesort.
1848 *
1849 * Returns 0 on success, -1 on error.
1850 */
1851static int
1852merge_force_collapse(MergeState *ms)
1853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 assert(ms);
1857 while (ms->n > 1) {
1858 Py_ssize_t n = ms->n - 2;
1859 if (n > 0 && p[n-1].len < p[n+1].len)
1860 --n;
1861 if (merge_at(ms, n) < 0)
1862 return -1;
1863 }
1864 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001865}
1866
1867/* Compute a good value for the minimum run length; natural runs shorter
1868 * than this are boosted artificially via binary insertion.
1869 *
1870 * If n < 64, return n (it's too small to bother with fancy stuff).
1871 * Else if n is an exact power of 2, return 32.
1872 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1873 * strictly less than, an exact power of 2.
1874 *
1875 * See listsort.txt for more info.
1876 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001877static Py_ssize_t
1878merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 assert(n >= 0);
1883 while (n >= 64) {
1884 r |= n & 1;
1885 n >>= 1;
1886 }
1887 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001888}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001889
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001890static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001891reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001892{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001893 reverse_slice(s->keys, &s->keys[n]);
1894 if (s->values != NULL)
1895 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001896}
1897
Tim Petersa64dc242002-08-01 02:13:36 +00001898/* An adaptive, stable, natural mergesort. See listsort.txt.
1899 * Returns Py_None on success, NULL on error. Even in case of error, the
1900 * list will be some permutation of its input state (nothing is lost or
1901 * duplicated).
1902 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001903static PyObject *
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001904listsort(PyListObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 Py_ssize_t nremaining;
1908 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001909 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 Py_ssize_t saved_ob_size, saved_allocated;
1911 PyObject **saved_ob_item;
1912 PyObject **final_ob_item;
1913 PyObject *result = NULL; /* guilty until proved innocent */
1914 int reverse = 0;
1915 PyObject *keyfunc = NULL;
1916 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 static char *kwlist[] = {"key", "reverse", 0};
Daniel Stutzbach98338222010-12-02 21:55:33 +00001918 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 assert(self != NULL);
1921 assert (PyList_Check(self));
1922 if (args != NULL) {
1923 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort",
1924 kwlist, &keyfunc, &reverse))
1925 return NULL;
1926 if (Py_SIZE(args) > 0) {
1927 PyErr_SetString(PyExc_TypeError,
1928 "must use keyword argument for key function");
1929 return NULL;
1930 }
1931 }
1932 if (keyfunc == Py_None)
1933 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 /* The list is temporarily made empty, so that mutations performed
1936 * by comparison functions can't affect the slice of memory we're
1937 * sorting (allowing mutations during sorting is a core-dump
1938 * factory, since ob_item may change).
1939 */
1940 saved_ob_size = Py_SIZE(self);
1941 saved_ob_item = self->ob_item;
1942 saved_allocated = self->allocated;
1943 Py_SIZE(self) = 0;
1944 self->ob_item = NULL;
1945 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00001946
Daniel Stutzbach98338222010-12-02 21:55:33 +00001947 if (keyfunc == NULL) {
1948 keys = NULL;
1949 lo.keys = saved_ob_item;
1950 lo.values = NULL;
1951 }
1952 else {
1953 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
1954 /* Leverage stack space we allocated but won't otherwise use */
1955 keys = &ms.temparray[saved_ob_size+1];
1956 else {
1957 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
1958 if (keys == NULL)
1959 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001961
1962 for (i = 0; i < saved_ob_size ; i++) {
1963 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
1964 NULL);
1965 if (keys[i] == NULL) {
1966 for (i=i-1 ; i>=0 ; i--)
1967 Py_DECREF(keys[i]);
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00001968 if (keys != &ms.temparray[saved_ob_size+1])
1969 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001970 goto keyfunc_fail;
1971 }
1972 }
1973
1974 lo.keys = keys;
1975 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001977
Daniel Stutzbach98338222010-12-02 21:55:33 +00001978 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 nremaining = saved_ob_size;
1981 if (nremaining < 2)
1982 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00001983
Benjamin Peterson05380642010-08-23 19:35:39 +00001984 /* Reverse sort stability achieved by initially reversing the list,
1985 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001986 if (reverse) {
1987 if (keys != NULL)
1988 reverse_slice(&keys[0], &keys[saved_ob_size]);
1989 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
1990 }
Benjamin Peterson05380642010-08-23 19:35:39 +00001991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 /* March over the array once, left to right, finding natural runs,
1993 * and extending short natural runs to minrun elements.
1994 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 minrun = merge_compute_minrun(nremaining);
1996 do {
1997 int descending;
1998 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00001999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 /* Identify next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002001 n = count_run(lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 if (n < 0)
2003 goto fail;
2004 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002005 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 /* If short, extend to min(minrun, nremaining). */
2007 if (n < minrun) {
2008 const Py_ssize_t force = nremaining <= minrun ?
2009 nremaining : minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002010 if (binarysort(lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 goto fail;
2012 n = force;
2013 }
2014 /* Push run onto pending-runs stack, and maybe merge. */
2015 assert(ms.n < MAX_MERGE_PENDING);
2016 ms.pending[ms.n].base = lo;
2017 ms.pending[ms.n].len = n;
2018 ++ms.n;
2019 if (merge_collapse(&ms) < 0)
2020 goto fail;
2021 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002022 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 nremaining -= n;
2024 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 if (merge_force_collapse(&ms) < 0)
2027 goto fail;
2028 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002029 assert(keys == NULL
2030 ? ms.pending[0].base.keys == saved_ob_item
2031 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002033 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002034
2035succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002037fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002038 if (keys != NULL) {
2039 for (i = 0; i < saved_ob_size; i++)
2040 Py_DECREF(keys[i]);
2041 if (keys != &ms.temparray[saved_ob_size+1])
2042 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 if (self->allocated != -1 && result != NULL) {
2046 /* The user mucked with the list during the sort,
2047 * and we don't already have another error to report.
2048 */
2049 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2050 result = NULL;
2051 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 if (reverse && saved_ob_size > 1)
2054 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002057
Daniel Stutzbach98338222010-12-02 21:55:33 +00002058keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 final_ob_item = self->ob_item;
2060 i = Py_SIZE(self);
2061 Py_SIZE(self) = saved_ob_size;
2062 self->ob_item = saved_ob_item;
2063 self->allocated = saved_allocated;
2064 if (final_ob_item != NULL) {
2065 /* we cannot use list_clear() for this because it does not
2066 guarantee that the list is really empty when it returns */
2067 while (--i >= 0) {
2068 Py_XDECREF(final_ob_item[i]);
2069 }
2070 PyMem_FREE(final_ob_item);
2071 }
2072 Py_XINCREF(result);
2073 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002074}
Tim Peters330f9e92002-07-19 07:05:44 +00002075#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002076#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002077
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002078int
Fred Drakea2f55112000-07-09 15:16:51 +00002079PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 if (v == NULL || !PyList_Check(v)) {
2082 PyErr_BadInternalCall();
2083 return -1;
2084 }
2085 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
2086 if (v == NULL)
2087 return -1;
2088 Py_DECREF(v);
2089 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002090}
2091
Guido van Rossumb86c5492001-02-12 22:06:02 +00002092static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002093listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 if (Py_SIZE(self) > 1)
2096 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2097 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002098}
2099
Guido van Rossum84c76f51990-10-30 13:32:20 +00002100int
Fred Drakea2f55112000-07-09 15:16:51 +00002101PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 if (v == NULL || !PyList_Check(v)) {
2106 PyErr_BadInternalCall();
2107 return -1;
2108 }
2109 if (Py_SIZE(self) > 1)
2110 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2111 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002112}
2113
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002114PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002115PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 PyObject *w;
2118 PyObject **p, **q;
2119 Py_ssize_t n;
2120 if (v == NULL || !PyList_Check(v)) {
2121 PyErr_BadInternalCall();
2122 return NULL;
2123 }
2124 n = Py_SIZE(v);
2125 w = PyTuple_New(n);
2126 if (w == NULL)
2127 return NULL;
2128 p = ((PyTupleObject *)w)->ob_item;
2129 q = ((PyListObject *)v)->ob_item;
2130 while (--n >= 0) {
2131 Py_INCREF(*q);
2132 *p = *q;
2133 p++;
2134 q++;
2135 }
2136 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002137}
2138
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002139static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002140listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002143 PyObject *v;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002144
Petri Lehtinenebfaabd2011-11-06 21:02:39 +02002145 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2146 _PyEval_SliceIndex, &start,
2147 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 return NULL;
2149 if (start < 0) {
2150 start += Py_SIZE(self);
2151 if (start < 0)
2152 start = 0;
2153 }
2154 if (stop < 0) {
2155 stop += Py_SIZE(self);
2156 if (stop < 0)
2157 stop = 0;
2158 }
2159 for (i = start; i < stop && i < Py_SIZE(self); i++) {
2160 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2161 if (cmp > 0)
2162 return PyLong_FromSsize_t(i);
2163 else if (cmp < 0)
2164 return NULL;
2165 }
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002166 PyErr_Format(PyExc_ValueError, "%R is not in list", v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002168}
2169
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002170static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002171listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 Py_ssize_t count = 0;
2174 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 for (i = 0; i < Py_SIZE(self); i++) {
2177 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2178 if (cmp > 0)
2179 count++;
2180 else if (cmp < 0)
2181 return NULL;
2182 }
2183 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002184}
2185
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002186static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002187listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 for (i = 0; i < Py_SIZE(self); i++) {
2192 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2193 if (cmp > 0) {
2194 if (list_ass_slice(self, i, i+1,
2195 (PyObject *)NULL) == 0)
2196 Py_RETURN_NONE;
2197 return NULL;
2198 }
2199 else if (cmp < 0)
2200 return NULL;
2201 }
2202 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2203 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002204}
2205
Jeremy Hylton8caad492000-06-23 14:18:11 +00002206static int
2207list_traverse(PyListObject *o, visitproc visit, void *arg)
2208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 for (i = Py_SIZE(o); --i >= 0; )
2212 Py_VISIT(o->ob_item[i]);
2213 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002214}
2215
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002216static PyObject *
2217list_richcompare(PyObject *v, PyObject *w, int op)
2218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 PyListObject *vl, *wl;
2220 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002221
Brian Curtindfc80e32011-08-10 20:28:54 -05002222 if (!PyList_Check(v) || !PyList_Check(w))
2223 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 vl = (PyListObject *)v;
2226 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2229 /* Shortcut: if the lengths differ, the lists differ */
2230 PyObject *res;
2231 if (op == Py_EQ)
2232 res = Py_False;
2233 else
2234 res = Py_True;
2235 Py_INCREF(res);
2236 return res;
2237 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 /* Search for the first index where items are different */
2240 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2241 int k = PyObject_RichCompareBool(vl->ob_item[i],
2242 wl->ob_item[i], Py_EQ);
2243 if (k < 0)
2244 return NULL;
2245 if (!k)
2246 break;
2247 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2250 /* No more items to compare -- compare sizes */
2251 Py_ssize_t vs = Py_SIZE(vl);
2252 Py_ssize_t ws = Py_SIZE(wl);
2253 int cmp;
2254 PyObject *res;
2255 switch (op) {
2256 case Py_LT: cmp = vs < ws; break;
2257 case Py_LE: cmp = vs <= ws; break;
2258 case Py_EQ: cmp = vs == ws; break;
2259 case Py_NE: cmp = vs != ws; break;
2260 case Py_GT: cmp = vs > ws; break;
2261 case Py_GE: cmp = vs >= ws; break;
2262 default: return NULL; /* cannot happen */
2263 }
2264 if (cmp)
2265 res = Py_True;
2266 else
2267 res = Py_False;
2268 Py_INCREF(res);
2269 return res;
2270 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 /* We have an item that differs -- shortcuts for EQ/NE */
2273 if (op == Py_EQ) {
2274 Py_INCREF(Py_False);
2275 return Py_False;
2276 }
2277 if (op == Py_NE) {
2278 Py_INCREF(Py_True);
2279 return Py_True;
2280 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 /* Compare the final item again using the proper operator */
2283 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002284}
2285
Tim Peters6d6c1a32001-08-02 04:15:00 +00002286static int
2287list_init(PyListObject *self, PyObject *args, PyObject *kw)
2288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 PyObject *arg = NULL;
2290 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2293 return -1;
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 /* Verify list invariants established by PyType_GenericAlloc() */
2296 assert(0 <= Py_SIZE(self));
2297 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2298 assert(self->ob_item != NULL ||
2299 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 /* Empty previous contents */
2302 if (self->ob_item != NULL) {
2303 (void)list_clear(self);
2304 }
2305 if (arg != NULL) {
2306 PyObject *rv = listextend(self, arg);
2307 if (rv == NULL)
2308 return -1;
2309 Py_DECREF(rv);
2310 }
2311 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002312}
2313
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002314static PyObject *
2315list_sizeof(PyListObject *self)
2316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 res = sizeof(PyListObject) + self->allocated * sizeof(void*);
2320 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002321}
2322
Raymond Hettinger1021c442003-11-07 15:38:09 +00002323static PyObject *list_iter(PyObject *seq);
2324static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2325
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002326PyDoc_STRVAR(getitem_doc,
2327"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002328PyDoc_STRVAR(reversed_doc,
2329"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002330PyDoc_STRVAR(sizeof_doc,
2331"L.__sizeof__() -- size of L in memory, in bytes");
Eli Benderskycbbaa962011-02-25 05:47:53 +00002332PyDoc_STRVAR(clear_doc,
2333"L.clear() -> None -- remove all items from L");
2334PyDoc_STRVAR(copy_doc,
2335"L.copy() -> list -- a shallow copy of L");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002336PyDoc_STRVAR(append_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002337"L.append(object) -> None -- append object to end");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002338PyDoc_STRVAR(extend_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002339"L.extend(iterable) -> None -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002340PyDoc_STRVAR(insert_doc,
2341"L.insert(index, object) -- insert object before index");
2342PyDoc_STRVAR(pop_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002343"L.pop([index]) -> item -- remove and return item at index (default last).\n"
2344"Raises IndexError if list is empty or index is out of range.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002345PyDoc_STRVAR(remove_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002346"L.remove(value) -> None -- remove first occurrence of value.\n"
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002347"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002348PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002349"L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2350"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002351PyDoc_STRVAR(count_doc,
2352"L.count(value) -> integer -- return number of occurrences of value");
2353PyDoc_STRVAR(reverse_doc,
2354"L.reverse() -- reverse *IN PLACE*");
2355PyDoc_STRVAR(sort_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002356"L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002357
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002358static PyObject *list_subscript(PyListObject*, PyObject*);
2359
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002360static PyMethodDef list_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
2362 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
2363 {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002364 {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc},
2365 {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 {"append", (PyCFunction)listappend, METH_O, append_doc},
2367 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002368 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
2370 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
2371 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
2372 {"count", (PyCFunction)listcount, METH_O, count_doc},
2373 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
2374 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
2375 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002376};
2377
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002378static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 (lenfunc)list_length, /* sq_length */
2380 (binaryfunc)list_concat, /* sq_concat */
2381 (ssizeargfunc)list_repeat, /* sq_repeat */
2382 (ssizeargfunc)list_item, /* sq_item */
2383 0, /* sq_slice */
2384 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2385 0, /* sq_ass_slice */
2386 (objobjproc)list_contains, /* sq_contains */
2387 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2388 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002389};
2390
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002391PyDoc_STRVAR(list_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002392"list() -> new empty list\n"
2393"list(iterable) -> new list initialized from iterable's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002394
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002395static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002396list_subscript(PyListObject* self, PyObject* item)
2397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 if (PyIndex_Check(item)) {
2399 Py_ssize_t i;
2400 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2401 if (i == -1 && PyErr_Occurred())
2402 return NULL;
2403 if (i < 0)
2404 i += PyList_GET_SIZE(self);
2405 return list_item(self, i);
2406 }
2407 else if (PySlice_Check(item)) {
2408 Py_ssize_t start, stop, step, slicelength, cur, i;
2409 PyObject* result;
2410 PyObject* it;
2411 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002412
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002413 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 &start, &stop, &step, &slicelength) < 0) {
2415 return NULL;
2416 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 if (slicelength <= 0) {
2419 return PyList_New(0);
2420 }
2421 else if (step == 1) {
2422 return list_slice(self, start, stop);
2423 }
2424 else {
2425 result = PyList_New(slicelength);
2426 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 src = self->ob_item;
2429 dest = ((PyListObject *)result)->ob_item;
2430 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002431 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 it = src[cur];
2433 Py_INCREF(it);
2434 dest[i] = it;
2435 }
Tim Peters3b01a122002-07-19 02:35:45 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 return result;
2438 }
2439 }
2440 else {
2441 PyErr_Format(PyExc_TypeError,
2442 "list indices must be integers, not %.200s",
2443 item->ob_type->tp_name);
2444 return NULL;
2445 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002446}
2447
Tim Peters3b01a122002-07-19 02:35:45 +00002448static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002449list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 if (PyIndex_Check(item)) {
2452 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2453 if (i == -1 && PyErr_Occurred())
2454 return -1;
2455 if (i < 0)
2456 i += PyList_GET_SIZE(self);
2457 return list_ass_item(self, i, value);
2458 }
2459 else if (PySlice_Check(item)) {
2460 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002461
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002462 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 &start, &stop, &step, &slicelength) < 0) {
2464 return -1;
2465 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 if (step == 1)
2468 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 /* Make sure s[5:2] = [..] inserts at the right place:
2471 before 5, not before 2. */
2472 if ((step < 0 && start < stop) ||
2473 (step > 0 && start > stop))
2474 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 if (value == NULL) {
2477 /* delete slice */
2478 PyObject **garbage;
2479 size_t cur;
2480 Py_ssize_t i;
Tim Peters3b01a122002-07-19 02:35:45 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 if (slicelength <= 0)
2483 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 if (step < 0) {
2486 stop = start + 1;
2487 start = stop + step*(slicelength - 1) - 1;
2488 step = -step;
2489 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 assert((size_t)slicelength <=
2492 PY_SIZE_MAX / sizeof(PyObject*));
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 garbage = (PyObject**)
2495 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2496 if (!garbage) {
2497 PyErr_NoMemory();
2498 return -1;
2499 }
Tim Peters3b01a122002-07-19 02:35:45 +00002500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 /* drawing pictures might help understand these for
2502 loops. Basically, we memmove the parts of the
2503 list that are *not* part of the slice: step-1
2504 items for each item that is part of the slice,
2505 and then tail end of the list that was not
2506 covered by the slice */
2507 for (cur = start, i = 0;
2508 cur < (size_t)stop;
2509 cur += step, i++) {
2510 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 if (cur + step >= (size_t)Py_SIZE(self)) {
2515 lim = Py_SIZE(self) - cur - 1;
2516 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 memmove(self->ob_item + cur - i,
2519 self->ob_item + cur + 1,
2520 lim * sizeof(PyObject *));
2521 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002522 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 if (cur < (size_t)Py_SIZE(self)) {
2524 memmove(self->ob_item + cur - slicelength,
2525 self->ob_item + cur,
2526 (Py_SIZE(self) - cur) *
2527 sizeof(PyObject *));
2528 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 Py_SIZE(self) -= slicelength;
2531 list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 for (i = 0; i < slicelength; i++) {
2534 Py_DECREF(garbage[i]);
2535 }
2536 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 return 0;
2539 }
2540 else {
2541 /* assign slice */
2542 PyObject *ins, *seq;
2543 PyObject **garbage, **seqitems, **selfitems;
2544 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 /* protect against a[::-1] = a */
2547 if (self == (PyListObject*)value) {
2548 seq = list_slice((PyListObject*)value, 0,
2549 PyList_GET_SIZE(value));
2550 }
2551 else {
2552 seq = PySequence_Fast(value,
2553 "must assign iterable "
2554 "to extended slice");
2555 }
2556 if (!seq)
2557 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2560 PyErr_Format(PyExc_ValueError,
2561 "attempt to assign sequence of "
2562 "size %zd to extended slice of "
2563 "size %zd",
2564 PySequence_Fast_GET_SIZE(seq),
2565 slicelength);
2566 Py_DECREF(seq);
2567 return -1;
2568 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 if (!slicelength) {
2571 Py_DECREF(seq);
2572 return 0;
2573 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 garbage = (PyObject**)
2576 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2577 if (!garbage) {
2578 Py_DECREF(seq);
2579 PyErr_NoMemory();
2580 return -1;
2581 }
Tim Peters3b01a122002-07-19 02:35:45 +00002582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 selfitems = self->ob_item;
2584 seqitems = PySequence_Fast_ITEMS(seq);
2585 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002586 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 garbage[i] = selfitems[cur];
2588 ins = seqitems[i];
2589 Py_INCREF(ins);
2590 selfitems[cur] = ins;
2591 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 for (i = 0; i < slicelength; i++) {
2594 Py_DECREF(garbage[i]);
2595 }
Tim Peters3b01a122002-07-19 02:35:45 +00002596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 PyMem_FREE(garbage);
2598 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 return 0;
2601 }
2602 }
2603 else {
2604 PyErr_Format(PyExc_TypeError,
2605 "list indices must be integers, not %.200s",
2606 item->ob_type->tp_name);
2607 return -1;
2608 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002609}
2610
2611static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 (lenfunc)list_length,
2613 (binaryfunc)list_subscript,
2614 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002615};
2616
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002617PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2619 "list",
2620 sizeof(PyListObject),
2621 0,
2622 (destructor)list_dealloc, /* tp_dealloc */
2623 0, /* tp_print */
2624 0, /* tp_getattr */
2625 0, /* tp_setattr */
2626 0, /* tp_reserved */
2627 (reprfunc)list_repr, /* tp_repr */
2628 0, /* tp_as_number */
2629 &list_as_sequence, /* tp_as_sequence */
2630 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002631 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 0, /* tp_call */
2633 0, /* tp_str */
2634 PyObject_GenericGetAttr, /* tp_getattro */
2635 0, /* tp_setattro */
2636 0, /* tp_as_buffer */
2637 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2638 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2639 list_doc, /* tp_doc */
2640 (traverseproc)list_traverse, /* tp_traverse */
2641 (inquiry)list_clear, /* tp_clear */
2642 list_richcompare, /* tp_richcompare */
2643 0, /* tp_weaklistoffset */
2644 list_iter, /* tp_iter */
2645 0, /* tp_iternext */
2646 list_methods, /* tp_methods */
2647 0, /* tp_members */
2648 0, /* tp_getset */
2649 0, /* tp_base */
2650 0, /* tp_dict */
2651 0, /* tp_descr_get */
2652 0, /* tp_descr_set */
2653 0, /* tp_dictoffset */
2654 (initproc)list_init, /* tp_init */
2655 PyType_GenericAlloc, /* tp_alloc */
2656 PyType_GenericNew, /* tp_new */
2657 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002658};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002659
2660
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002661/*********************** List Iterator **************************/
2662
2663typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02002665 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002667} listiterobject;
2668
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002669static PyObject *list_iter(PyObject *);
2670static void listiter_dealloc(listiterobject *);
2671static int listiter_traverse(listiterobject *, visitproc, void *);
2672static PyObject *listiter_next(listiterobject *);
2673static PyObject *listiter_len(listiterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002674static PyObject *listiter_reduce_general(void *_it, int forward);
2675static PyObject *listiter_reduce(listiterobject *);
2676static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002677
Armin Rigof5b3e362006-02-11 21:32:43 +00002678PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002679PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2680PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002681
2682static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002684 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
2685 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002687};
2688
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002689PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2691 "list_iterator", /* tp_name */
2692 sizeof(listiterobject), /* tp_basicsize */
2693 0, /* tp_itemsize */
2694 /* methods */
2695 (destructor)listiter_dealloc, /* tp_dealloc */
2696 0, /* tp_print */
2697 0, /* tp_getattr */
2698 0, /* tp_setattr */
2699 0, /* tp_reserved */
2700 0, /* tp_repr */
2701 0, /* tp_as_number */
2702 0, /* tp_as_sequence */
2703 0, /* tp_as_mapping */
2704 0, /* tp_hash */
2705 0, /* tp_call */
2706 0, /* tp_str */
2707 PyObject_GenericGetAttr, /* tp_getattro */
2708 0, /* tp_setattro */
2709 0, /* tp_as_buffer */
2710 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2711 0, /* tp_doc */
2712 (traverseproc)listiter_traverse, /* tp_traverse */
2713 0, /* tp_clear */
2714 0, /* tp_richcompare */
2715 0, /* tp_weaklistoffset */
2716 PyObject_SelfIter, /* tp_iter */
2717 (iternextfunc)listiter_next, /* tp_iternext */
2718 listiter_methods, /* tp_methods */
2719 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002720};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002721
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002722
2723static PyObject *
2724list_iter(PyObject *seq)
2725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 if (!PyList_Check(seq)) {
2729 PyErr_BadInternalCall();
2730 return NULL;
2731 }
2732 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2733 if (it == NULL)
2734 return NULL;
2735 it->it_index = 0;
2736 Py_INCREF(seq);
2737 it->it_seq = (PyListObject *)seq;
2738 _PyObject_GC_TRACK(it);
2739 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002740}
2741
2742static void
2743listiter_dealloc(listiterobject *it)
2744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 _PyObject_GC_UNTRACK(it);
2746 Py_XDECREF(it->it_seq);
2747 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002748}
2749
2750static int
2751listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 Py_VISIT(it->it_seq);
2754 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002755}
2756
2757static PyObject *
2758listiter_next(listiterobject *it)
2759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 PyListObject *seq;
2761 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 assert(it != NULL);
2764 seq = it->it_seq;
2765 if (seq == NULL)
2766 return NULL;
2767 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 if (it->it_index < PyList_GET_SIZE(seq)) {
2770 item = PyList_GET_ITEM(seq, it->it_index);
2771 ++it->it_index;
2772 Py_INCREF(item);
2773 return item;
2774 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 Py_DECREF(seq);
2777 it->it_seq = NULL;
2778 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002779}
2780
2781static PyObject *
2782listiter_len(listiterobject *it)
2783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 Py_ssize_t len;
2785 if (it->it_seq) {
2786 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2787 if (len >= 0)
2788 return PyLong_FromSsize_t(len);
2789 }
2790 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002791}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002792
2793static PyObject *
2794listiter_reduce(listiterobject *it)
2795{
2796 return listiter_reduce_general(it, 1);
2797}
2798
2799static PyObject *
2800listiter_setstate(listiterobject *it, PyObject *state)
2801{
Victor Stinner7660b882013-06-24 23:59:24 +02002802 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002803 if (index == -1 && PyErr_Occurred())
2804 return NULL;
2805 if (it->it_seq != NULL) {
2806 if (index < 0)
2807 index = 0;
2808 it->it_index = index;
2809 }
2810 Py_RETURN_NONE;
2811}
2812
Raymond Hettinger1021c442003-11-07 15:38:09 +00002813/*********************** List Reverse Iterator **************************/
2814
2815typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 PyObject_HEAD
2817 Py_ssize_t it_index;
2818 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002819} listreviterobject;
2820
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002821static PyObject *list_reversed(PyListObject *, PyObject *);
2822static void listreviter_dealloc(listreviterobject *);
2823static int listreviter_traverse(listreviterobject *, visitproc, void *);
2824static PyObject *listreviter_next(listreviterobject *);
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002825static PyObject *listreviter_len(listreviterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002826static PyObject *listreviter_reduce(listreviterobject *);
2827static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002828
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002829static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002831 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
2832 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002834};
2835
Raymond Hettinger1021c442003-11-07 15:38:09 +00002836PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2838 "list_reverseiterator", /* tp_name */
2839 sizeof(listreviterobject), /* tp_basicsize */
2840 0, /* tp_itemsize */
2841 /* methods */
2842 (destructor)listreviter_dealloc, /* tp_dealloc */
2843 0, /* tp_print */
2844 0, /* tp_getattr */
2845 0, /* tp_setattr */
2846 0, /* tp_reserved */
2847 0, /* tp_repr */
2848 0, /* tp_as_number */
2849 0, /* tp_as_sequence */
2850 0, /* tp_as_mapping */
2851 0, /* tp_hash */
2852 0, /* tp_call */
2853 0, /* tp_str */
2854 PyObject_GenericGetAttr, /* tp_getattro */
2855 0, /* tp_setattro */
2856 0, /* tp_as_buffer */
2857 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2858 0, /* tp_doc */
2859 (traverseproc)listreviter_traverse, /* tp_traverse */
2860 0, /* tp_clear */
2861 0, /* tp_richcompare */
2862 0, /* tp_weaklistoffset */
2863 PyObject_SelfIter, /* tp_iter */
2864 (iternextfunc)listreviter_next, /* tp_iternext */
2865 listreviter_methods, /* tp_methods */
2866 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00002867};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002868
2869static PyObject *
2870list_reversed(PyListObject *seq, PyObject *unused)
2871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2875 if (it == NULL)
2876 return NULL;
2877 assert(PyList_Check(seq));
2878 it->it_index = PyList_GET_SIZE(seq) - 1;
2879 Py_INCREF(seq);
2880 it->it_seq = seq;
2881 PyObject_GC_Track(it);
2882 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002883}
2884
2885static void
2886listreviter_dealloc(listreviterobject *it)
2887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 PyObject_GC_UnTrack(it);
2889 Py_XDECREF(it->it_seq);
2890 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002891}
2892
2893static int
2894listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 Py_VISIT(it->it_seq);
2897 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002898}
2899
2900static PyObject *
2901listreviter_next(listreviterobject *it)
2902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 PyObject *item;
2904 Py_ssize_t index = it->it_index;
2905 PyListObject *seq = it->it_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2908 item = PyList_GET_ITEM(seq, index);
2909 it->it_index--;
2910 Py_INCREF(item);
2911 return item;
2912 }
2913 it->it_index = -1;
2914 if (seq != NULL) {
2915 it->it_seq = NULL;
2916 Py_DECREF(seq);
2917 }
2918 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002919}
2920
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002921static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002922listreviter_len(listreviterobject *it)
2923{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 Py_ssize_t len = it->it_index + 1;
2925 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2926 len = 0;
2927 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002928}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002929
2930static PyObject *
2931listreviter_reduce(listreviterobject *it)
2932{
2933 return listiter_reduce_general(it, 0);
2934}
2935
2936static PyObject *
2937listreviter_setstate(listreviterobject *it, PyObject *state)
2938{
2939 Py_ssize_t index = PyLong_AsSsize_t(state);
2940 if (index == -1 && PyErr_Occurred())
2941 return NULL;
2942 if (it->it_seq != NULL) {
2943 if (index < -1)
2944 index = -1;
2945 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
2946 index = PyList_GET_SIZE(it->it_seq) - 1;
2947 it->it_index = index;
2948 }
2949 Py_RETURN_NONE;
2950}
2951
2952/* common pickling support */
2953
2954static PyObject *
2955listiter_reduce_general(void *_it, int forward)
2956{
2957 PyObject *list;
2958
2959 /* the objects are not the same, index is of different types! */
2960 if (forward) {
2961 listiterobject *it = (listiterobject *)_it;
2962 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02002963 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002964 it->it_seq, it->it_index);
2965 } else {
2966 listreviterobject *it = (listreviterobject *)_it;
2967 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +02002968 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("reversed"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002969 it->it_seq, it->it_index);
2970 }
2971 /* empty iterator, create an empty list */
2972 list = PyList_New(0);
2973 if (list == NULL)
2974 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02002975 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002976}