blob: 0ec70e587af815b330fe4c3fc4ca0a9b7902df0c [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. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200874 if (Py_SIZE(self) < self->allocated) {
875 if (list_resize(self, Py_SIZE(self)) < 0)
876 goto error;
877 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 Py_DECREF(it);
880 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000881
882 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 Py_DECREF(it);
884 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000885}
886
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000887PyObject *
888_PyList_Extend(PyListObject *self, PyObject *b)
889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 return listextend(self, b);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000891}
892
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000893static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000894list_inplace_concat(PyListObject *self, PyObject *other)
895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 result = listextend(self, other);
899 if (result == NULL)
900 return result;
901 Py_DECREF(result);
902 Py_INCREF(self);
903 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000904}
905
906static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000907listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 Py_ssize_t i = -1;
910 PyObject *v;
911 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 if (!PyArg_ParseTuple(args, "|n:pop", &i))
914 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 if (Py_SIZE(self) == 0) {
917 /* Special-case most common failure cause */
918 PyErr_SetString(PyExc_IndexError, "pop from empty list");
919 return NULL;
920 }
921 if (i < 0)
922 i += Py_SIZE(self);
923 if (i < 0 || i >= Py_SIZE(self)) {
924 PyErr_SetString(PyExc_IndexError, "pop index out of range");
925 return NULL;
926 }
927 v = self->ob_item[i];
928 if (i == Py_SIZE(self) - 1) {
929 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +0200930 if (status >= 0)
931 return v; /* and v now owns the reference the list had */
932 else
933 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 }
935 Py_INCREF(v);
936 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
937 assert(status >= 0);
938 /* Use status, so that in a release build compilers don't
939 * complain about the unused name.
940 */
941 (void) status;
Brett Cannon651dd522004-08-08 21:21:18 +0000942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000944}
945
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000946/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
947static void
948reverse_slice(PyObject **lo, PyObject **hi)
949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 --hi;
953 while (lo < hi) {
954 PyObject *t = *lo;
955 *lo = *hi;
956 *hi = t;
957 ++lo;
958 --hi;
959 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000960}
961
Tim Petersa64dc242002-08-01 02:13:36 +0000962/* Lots of code for an adaptive, stable, natural mergesort. There are many
963 * pieces to this algorithm; read listsort.txt for overviews and details.
964 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000965
Daniel Stutzbach98338222010-12-02 21:55:33 +0000966/* A sortslice contains a pointer to an array of keys and a pointer to
967 * an array of corresponding values. In other words, keys[i]
968 * corresponds with values[i]. If values == NULL, then the keys are
969 * also the values.
970 *
971 * Several convenience routines are provided here, so that keys and
972 * values are always moved in sync.
973 */
974
975typedef struct {
976 PyObject **keys;
977 PyObject **values;
978} sortslice;
979
980Py_LOCAL_INLINE(void)
981sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
982{
983 s1->keys[i] = s2->keys[j];
984 if (s1->values != NULL)
985 s1->values[i] = s2->values[j];
986}
987
988Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000989sortslice_copy_incr(sortslice *dst, sortslice *src)
990{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000991 *dst->keys++ = *src->keys++;
992 if (dst->values != NULL)
993 *dst->values++ = *src->values++;
994}
995
996Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000997sortslice_copy_decr(sortslice *dst, sortslice *src)
998{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000999 *dst->keys-- = *src->keys--;
1000 if (dst->values != NULL)
1001 *dst->values-- = *src->values--;
1002}
1003
1004
1005Py_LOCAL_INLINE(void)
1006sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001007 Py_ssize_t n)
1008{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001009 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1010 if (s1->values != NULL)
1011 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1012}
1013
1014Py_LOCAL_INLINE(void)
1015sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001016 Py_ssize_t n)
1017{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001018 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1019 if (s1->values != NULL)
1020 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1021}
1022
1023Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001024sortslice_advance(sortslice *slice, Py_ssize_t n)
1025{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001026 slice->keys += n;
1027 if (slice->values != NULL)
1028 slice->values += n;
1029}
1030
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001031/* Comparison function: PyObject_RichCompareBool with Py_LT.
Tim Petersa64dc242002-08-01 02:13:36 +00001032 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1033 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001034
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001035#define ISLT(X, Y) (PyObject_RichCompareBool(X, Y, Py_LT))
Tim Peters66860f62002-08-04 17:47:26 +00001036
1037/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001038 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1039 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1040*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001041#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001043
1044/* binarysort is the best method for sorting small arrays: it does
1045 few compares, but can do data movement quadratic in the number of
1046 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001047 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001048 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001049 On entry, must have lo <= start <= hi, and that [lo, start) is already
1050 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001051 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001052 Even in case of error, the output slice will be some permutation of
1053 the input (nothing is lost or duplicated).
1054*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001055static int
Daniel Stutzbach98338222010-12-02 21:55:33 +00001056binarysort(sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 register Py_ssize_t k;
1059 register PyObject **l, **p, **r;
1060 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001061
Daniel Stutzbach98338222010-12-02 21:55:33 +00001062 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001064 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 ++start;
1066 for (; start < hi; ++start) {
1067 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001068 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 r = start;
1070 pivot = *r;
1071 /* Invariants:
1072 * pivot >= all in [lo, l).
1073 * pivot < all in [r, start).
1074 * The second is vacuously true at the start.
1075 */
1076 assert(l < r);
1077 do {
1078 p = l + ((r - l) >> 1);
1079 IFLT(pivot, *p)
1080 r = p;
1081 else
1082 l = p+1;
1083 } while (l < r);
1084 assert(l == r);
1085 /* The invariants still hold, so pivot >= all in [lo, l) and
1086 pivot < all in [l, start), so pivot belongs at l. Note
1087 that if there are elements equal to pivot, l points to the
1088 first slot after them -- that's why this sort is stable.
1089 Slide over to make room.
1090 Caution: using memmove is much slower under MSVC 5;
1091 we're not usually moving many slots. */
1092 for (p = start; p > l; --p)
1093 *p = *(p-1);
1094 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001095 if (lo.values != NULL) {
1096 Py_ssize_t offset = lo.values - lo.keys;
1097 p = start + offset;
1098 pivot = *p;
1099 l += offset;
1100 for (p = start + offset; p > l; --p)
1101 *p = *(p-1);
1102 *l = pivot;
1103 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 }
1105 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001106
1107 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001109}
1110
Tim Petersa64dc242002-08-01 02:13:36 +00001111/*
1112Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1113is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001114
Tim Petersa64dc242002-08-01 02:13:36 +00001115 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001116
Tim Petersa64dc242002-08-01 02:13:36 +00001117or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001118
Tim Petersa64dc242002-08-01 02:13:36 +00001119 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001120
Tim Petersa64dc242002-08-01 02:13:36 +00001121Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1122For its intended use in a stable mergesort, the strictness of the defn of
1123"descending" is needed so that the caller can safely reverse a descending
1124sequence without violating stability (strict > ensures there are no equal
1125elements to get out of order).
1126
1127Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001128*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001129static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001130count_run(PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 Py_ssize_t k;
1133 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 assert(lo < hi);
1136 *descending = 0;
1137 ++lo;
1138 if (lo == hi)
1139 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 n = 2;
1142 IFLT(*lo, *(lo-1)) {
1143 *descending = 1;
1144 for (lo = lo+1; lo < hi; ++lo, ++n) {
1145 IFLT(*lo, *(lo-1))
1146 ;
1147 else
1148 break;
1149 }
1150 }
1151 else {
1152 for (lo = lo+1; lo < hi; ++lo, ++n) {
1153 IFLT(*lo, *(lo-1))
1154 break;
1155 }
1156 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001159fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001161}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001162
Tim Petersa64dc242002-08-01 02:13:36 +00001163/*
1164Locate the proper position of key in a sorted vector; if the vector contains
1165an element equal to key, return the position immediately to the left of
1166the leftmost equal element. [gallop_right() does the same except returns
1167the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001168
Tim Petersa64dc242002-08-01 02:13:36 +00001169"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001170
Tim Petersa64dc242002-08-01 02:13:36 +00001171"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1172hint is to the final result, the faster this runs.
1173
1174The return value is the int k in 0..n such that
1175
1176 a[k-1] < key <= a[k]
1177
1178pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1179key belongs at index k; or, IOW, the first k elements of a should precede
1180key, and the last n-k should follow key.
1181
1182Returns -1 on error. See listsort.txt for info on the method.
1183*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001184static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001185gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 Py_ssize_t ofs;
1188 Py_ssize_t lastofs;
1189 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 a += hint;
1194 lastofs = 0;
1195 ofs = 1;
1196 IFLT(*a, key) {
1197 /* a[hint] < key -- gallop right, until
1198 * a[hint + lastofs] < key <= a[hint + ofs]
1199 */
1200 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1201 while (ofs < maxofs) {
1202 IFLT(a[ofs], key) {
1203 lastofs = ofs;
1204 ofs = (ofs << 1) + 1;
1205 if (ofs <= 0) /* int overflow */
1206 ofs = maxofs;
1207 }
1208 else /* key <= a[hint + ofs] */
1209 break;
1210 }
1211 if (ofs > maxofs)
1212 ofs = maxofs;
1213 /* Translate back to offsets relative to &a[0]. */
1214 lastofs += hint;
1215 ofs += hint;
1216 }
1217 else {
1218 /* key <= a[hint] -- gallop left, until
1219 * a[hint - ofs] < key <= a[hint - lastofs]
1220 */
1221 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1222 while (ofs < maxofs) {
1223 IFLT(*(a-ofs), key)
1224 break;
1225 /* key <= a[hint - ofs] */
1226 lastofs = ofs;
1227 ofs = (ofs << 1) + 1;
1228 if (ofs <= 0) /* int overflow */
1229 ofs = maxofs;
1230 }
1231 if (ofs > maxofs)
1232 ofs = maxofs;
1233 /* Translate back to positive offsets relative to &a[0]. */
1234 k = lastofs;
1235 lastofs = hint - ofs;
1236 ofs = hint - k;
1237 }
1238 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1241 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1242 * right of lastofs but no farther right than ofs. Do a binary
1243 * search, with invariant a[lastofs-1] < key <= a[ofs].
1244 */
1245 ++lastofs;
1246 while (lastofs < ofs) {
1247 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 IFLT(a[m], key)
1250 lastofs = m+1; /* a[m] < key */
1251 else
1252 ofs = m; /* key <= a[m] */
1253 }
1254 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1255 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001256
1257fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001259}
1260
1261/*
1262Exactly like gallop_left(), except that if key already exists in a[0:n],
1263finds the position immediately to the right of the rightmost equal value.
1264
1265The return value is the int k in 0..n such that
1266
1267 a[k-1] <= key < a[k]
1268
1269or -1 if error.
1270
1271The code duplication is massive, but this is enough different given that
1272we're sticking to "<" comparisons that it's much harder to follow if
1273written as one routine with yet another "left or right?" flag.
1274*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001275static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001276gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 Py_ssize_t ofs;
1279 Py_ssize_t lastofs;
1280 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 a += hint;
1285 lastofs = 0;
1286 ofs = 1;
1287 IFLT(key, *a) {
1288 /* key < a[hint] -- gallop left, until
1289 * a[hint - ofs] <= key < a[hint - lastofs]
1290 */
1291 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1292 while (ofs < maxofs) {
1293 IFLT(key, *(a-ofs)) {
1294 lastofs = ofs;
1295 ofs = (ofs << 1) + 1;
1296 if (ofs <= 0) /* int overflow */
1297 ofs = maxofs;
1298 }
1299 else /* a[hint - ofs] <= key */
1300 break;
1301 }
1302 if (ofs > maxofs)
1303 ofs = maxofs;
1304 /* Translate back to positive offsets relative to &a[0]. */
1305 k = lastofs;
1306 lastofs = hint - ofs;
1307 ofs = hint - k;
1308 }
1309 else {
1310 /* a[hint] <= key -- gallop right, until
1311 * a[hint + lastofs] <= key < a[hint + ofs]
1312 */
1313 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1314 while (ofs < maxofs) {
1315 IFLT(key, a[ofs])
1316 break;
1317 /* a[hint + ofs] <= key */
1318 lastofs = ofs;
1319 ofs = (ofs << 1) + 1;
1320 if (ofs <= 0) /* int overflow */
1321 ofs = maxofs;
1322 }
1323 if (ofs > maxofs)
1324 ofs = maxofs;
1325 /* Translate back to offsets relative to &a[0]. */
1326 lastofs += hint;
1327 ofs += hint;
1328 }
1329 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1332 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1333 * right of lastofs but no farther right than ofs. Do a binary
1334 * search, with invariant a[lastofs-1] <= key < a[ofs].
1335 */
1336 ++lastofs;
1337 while (lastofs < ofs) {
1338 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 IFLT(key, a[m])
1341 ofs = m; /* key < a[m] */
1342 else
1343 lastofs = m+1; /* a[m] <= key */
1344 }
1345 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1346 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001347
1348fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001350}
1351
1352/* The maximum number of entries in a MergeState's pending-runs stack.
1353 * This is enough to sort arrays of size up to about
1354 * 32 * phi ** MAX_MERGE_PENDING
1355 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1356 * with 2**64 elements.
1357 */
1358#define MAX_MERGE_PENDING 85
1359
Tim Peterse05f65a2002-08-10 05:21:15 +00001360/* When we get into galloping mode, we stay there until both runs win less
1361 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
Tim Petersa64dc242002-08-01 02:13:36 +00001362 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001363#define MIN_GALLOP 7
Tim Petersa64dc242002-08-01 02:13:36 +00001364
1365/* Avoid malloc for small temp arrays. */
1366#define MERGESTATE_TEMP_SIZE 256
1367
1368/* One MergeState exists on the stack per invocation of mergesort. It's just
1369 * a convenient way to pass state around among the helper functions.
1370 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001371struct s_slice {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001372 sortslice base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 Py_ssize_t len;
Tim Peterse05f65a2002-08-10 05:21:15 +00001374};
1375
Tim Petersa64dc242002-08-01 02:13:36 +00001376typedef struct s_MergeState {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 /* This controls when we get *into* galloping mode. It's initialized
1378 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1379 * random data, and lower for highly structured data.
1380 */
1381 Py_ssize_t min_gallop;
Tim Peterse05f65a2002-08-10 05:21:15 +00001382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 /* 'a' is temp storage to help with merges. It contains room for
1384 * alloced entries.
1385 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001386 sortslice a; /* may point to temparray below */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 Py_ssize_t alloced;
Tim Petersa64dc242002-08-01 02:13:36 +00001388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 /* A stack of n pending runs yet to be merged. Run #i starts at
1390 * address base[i] and extends for len[i] elements. It's always
1391 * true (so long as the indices are in bounds) that
1392 *
1393 * pending[i].base + pending[i].len == pending[i+1].base
1394 *
1395 * so we could cut the storage for this, but it's a minor amount,
1396 * and keeping all the info explicit simplifies the code.
1397 */
1398 int n;
1399 struct s_slice pending[MAX_MERGE_PENDING];
Tim Petersa64dc242002-08-01 02:13:36 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 /* 'a' points to this when possible, rather than muck with malloc. */
1402 PyObject *temparray[MERGESTATE_TEMP_SIZE];
Tim Petersa64dc242002-08-01 02:13:36 +00001403} MergeState;
1404
1405/* Conceptually a MergeState's constructor. */
1406static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001407merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001410 if (has_keyfunc) {
1411 /* The temporary space for merging will need at most half the list
1412 * size rounded up. Use the minimum possible space so we can use the
1413 * rest of temparray for other things. In particular, if there is
1414 * enough extra space, listsort() will use it to store the keys.
1415 */
1416 ms->alloced = (list_size + 1) / 2;
1417
1418 /* ms->alloced describes how many keys will be stored at
1419 ms->temparray, but we also need to store the values. Hence,
1420 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1421 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1422 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1423 ms->a.values = &ms->temparray[ms->alloced];
1424 }
1425 else {
1426 ms->alloced = MERGESTATE_TEMP_SIZE;
1427 ms->a.values = NULL;
1428 }
1429 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 ms->n = 0;
1431 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001432}
1433
1434/* Free all the temp memory owned by the MergeState. This must be called
1435 * when you're done with a MergeState, and may be called before then if
1436 * you want to free the temp memory early.
1437 */
1438static void
1439merge_freemem(MergeState *ms)
1440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001442 if (ms->a.keys != ms->temparray)
1443 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001444}
1445
1446/* Ensure enough temp memory for 'need' array slots is available.
1447 * Returns 0 on success and -1 if the memory can't be gotten.
1448 */
1449static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001450merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001451{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001452 int multiplier;
1453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 assert(ms != NULL);
1455 if (need <= ms->alloced)
1456 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001457
1458 multiplier = ms->a.values != NULL ? 2 : 1;
1459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 /* Don't realloc! That can cost cycles to copy the old data, but
1461 * we don't care what's in the block.
1462 */
1463 merge_freemem(ms);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001464 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 PyErr_NoMemory();
1466 return -1;
1467 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001468 ms->a.keys = (PyObject**)PyMem_Malloc(multiplier * need
1469 * sizeof(PyObject *));
1470 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001472 if (ms->a.values != NULL)
1473 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 return 0;
1475 }
1476 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001478}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1480 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001481
Daniel Stutzbach98338222010-12-02 21:55:33 +00001482/* Merge the na elements starting at ssa with the nb elements starting at
1483 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1484 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1485 * should have na <= nb. See listsort.txt for more info. Return 0 if
1486 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001487 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001488static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001489merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1490 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001493 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 int result = -1; /* guilty until proved innocent */
1495 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001496
Daniel Stutzbach98338222010-12-02 21:55:33 +00001497 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1498 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 if (MERGE_GETMEM(ms, na) < 0)
1500 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001501 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1502 dest = ssa;
1503 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001504
Daniel Stutzbach98338222010-12-02 21:55:33 +00001505 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 --nb;
1507 if (nb == 0)
1508 goto Succeed;
1509 if (na == 1)
1510 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 min_gallop = ms->min_gallop;
1513 for (;;) {
1514 Py_ssize_t acount = 0; /* # of times A won in a row */
1515 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 /* Do the straightforward thing until (if ever) one run
1518 * appears to win consistently.
1519 */
1520 for (;;) {
1521 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001522 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 if (k) {
1524 if (k < 0)
1525 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001526 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 ++bcount;
1528 acount = 0;
1529 --nb;
1530 if (nb == 0)
1531 goto Succeed;
1532 if (bcount >= min_gallop)
1533 break;
1534 }
1535 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001536 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 ++acount;
1538 bcount = 0;
1539 --na;
1540 if (na == 1)
1541 goto CopyB;
1542 if (acount >= min_gallop)
1543 break;
1544 }
1545 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 /* One run is winning so consistently that galloping may
1548 * be a huge win. So try that, and continue galloping until
1549 * (if ever) neither run appears to be winning consistently
1550 * anymore.
1551 */
1552 ++min_gallop;
1553 do {
1554 assert(na > 1 && nb > 0);
1555 min_gallop -= min_gallop > 1;
1556 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001557 k = gallop_right(ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 acount = k;
1559 if (k) {
1560 if (k < 0)
1561 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001562 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1563 sortslice_advance(&dest, k);
1564 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 na -= k;
1566 if (na == 1)
1567 goto CopyB;
1568 /* na==0 is impossible now if the comparison
1569 * function is consistent, but we can't assume
1570 * that it is.
1571 */
1572 if (na == 0)
1573 goto Succeed;
1574 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001575 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 --nb;
1577 if (nb == 0)
1578 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001579
Daniel Stutzbach98338222010-12-02 21:55:33 +00001580 k = gallop_left(ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 bcount = k;
1582 if (k) {
1583 if (k < 0)
1584 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001585 sortslice_memmove(&dest, 0, &ssb, 0, k);
1586 sortslice_advance(&dest, k);
1587 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 nb -= k;
1589 if (nb == 0)
1590 goto Succeed;
1591 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001592 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 --na;
1594 if (na == 1)
1595 goto CopyB;
1596 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1597 ++min_gallop; /* penalize it for leaving galloping mode */
1598 ms->min_gallop = min_gallop;
1599 }
Tim Petersa64dc242002-08-01 02:13:36 +00001600Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001602Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001604 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001606CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001608 /* The last element of ssa belongs at the end of the merge. */
1609 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1610 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001612}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001613
Daniel Stutzbach98338222010-12-02 21:55:33 +00001614/* Merge the na elements starting at pa with the nb elements starting at
1615 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1616 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1617 * should have na >= nb. See listsort.txt for more info. Return 0 if
1618 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001619 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001620static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001621merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1622 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001625 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001628
Daniel Stutzbach98338222010-12-02 21:55:33 +00001629 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1630 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 if (MERGE_GETMEM(ms, nb) < 0)
1632 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001633 dest = ssb;
1634 sortslice_advance(&dest, nb-1);
1635 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1636 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001638 ssb.keys = ms->a.keys + nb - 1;
1639 if (ssb.values != NULL)
1640 ssb.values = ms->a.values + nb - 1;
1641 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001642
Daniel Stutzbach98338222010-12-02 21:55:33 +00001643 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 --na;
1645 if (na == 0)
1646 goto Succeed;
1647 if (nb == 1)
1648 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 min_gallop = ms->min_gallop;
1651 for (;;) {
1652 Py_ssize_t acount = 0; /* # of times A won in a row */
1653 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 /* Do the straightforward thing until (if ever) one run
1656 * appears to win consistently.
1657 */
1658 for (;;) {
1659 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001660 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 if (k) {
1662 if (k < 0)
1663 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001664 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 ++acount;
1666 bcount = 0;
1667 --na;
1668 if (na == 0)
1669 goto Succeed;
1670 if (acount >= min_gallop)
1671 break;
1672 }
1673 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001674 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 ++bcount;
1676 acount = 0;
1677 --nb;
1678 if (nb == 1)
1679 goto CopyA;
1680 if (bcount >= min_gallop)
1681 break;
1682 }
1683 }
Tim Petersa64dc242002-08-01 02:13:36 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 /* One run is winning so consistently that galloping may
1686 * be a huge win. So try that, and continue galloping until
1687 * (if ever) neither run appears to be winning consistently
1688 * anymore.
1689 */
1690 ++min_gallop;
1691 do {
1692 assert(na > 0 && nb > 1);
1693 min_gallop -= min_gallop > 1;
1694 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001695 k = gallop_right(ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 if (k < 0)
1697 goto Fail;
1698 k = na - k;
1699 acount = k;
1700 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001701 sortslice_advance(&dest, -k);
1702 sortslice_advance(&ssa, -k);
1703 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 na -= k;
1705 if (na == 0)
1706 goto Succeed;
1707 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001708 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 --nb;
1710 if (nb == 1)
1711 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001712
Daniel Stutzbach98338222010-12-02 21:55:33 +00001713 k = gallop_left(ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 if (k < 0)
1715 goto Fail;
1716 k = nb - k;
1717 bcount = k;
1718 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001719 sortslice_advance(&dest, -k);
1720 sortslice_advance(&ssb, -k);
1721 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 nb -= k;
1723 if (nb == 1)
1724 goto CopyA;
1725 /* nb==0 is impossible now if the comparison
1726 * function is consistent, but we can't assume
1727 * that it is.
1728 */
1729 if (nb == 0)
1730 goto Succeed;
1731 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001732 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 --na;
1734 if (na == 0)
1735 goto Succeed;
1736 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1737 ++min_gallop; /* penalize it for leaving galloping mode */
1738 ms->min_gallop = min_gallop;
1739 }
Tim Petersa64dc242002-08-01 02:13:36 +00001740Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001742Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001744 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001746CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001748 /* The first element of ssb belongs at the front of the merge. */
1749 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1750 sortslice_advance(&dest, -na);
1751 sortslice_advance(&ssa, -na);
1752 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001754}
1755
1756/* Merge the two runs at stack indices i and i+1.
1757 * Returns 0 on success, -1 on error.
1758 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001759static Py_ssize_t
1760merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001761{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001762 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 Py_ssize_t na, nb;
1764 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 assert(ms != NULL);
1767 assert(ms->n >= 2);
1768 assert(i >= 0);
1769 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001770
Daniel Stutzbach98338222010-12-02 21:55:33 +00001771 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001773 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 nb = ms->pending[i+1].len;
1775 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001776 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 /* Record the length of the combined runs; if i is the 3rd-last
1779 * run now, also slide over the last run (which isn't involved
1780 * in this merge). The current run i+1 goes away in any case.
1781 */
1782 ms->pending[i].len = na + nb;
1783 if (i == ms->n - 3)
1784 ms->pending[i+1] = ms->pending[i+2];
1785 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 /* Where does b start in a? Elements in a before that can be
1788 * ignored (already in place).
1789 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001790 k = gallop_right(*ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 if (k < 0)
1792 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001793 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 na -= k;
1795 if (na == 0)
1796 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 /* Where does a end in b? Elements in b after that can be
1799 * ignored (already in place).
1800 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001801 nb = gallop_left(ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 if (nb <= 0)
1803 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 /* Merge what remains of the runs, using a temp array with
1806 * min(na, nb) elements.
1807 */
1808 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001809 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001811 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001812}
1813
1814/* Examine the stack of runs waiting to be merged, merging adjacent runs
1815 * until the stack invariants are re-established:
1816 *
1817 * 1. len[-3] > len[-2] + len[-1]
1818 * 2. len[-2] > len[-1]
1819 *
1820 * See listsort.txt for more info.
1821 *
1822 * Returns 0 on success, -1 on error.
1823 */
1824static int
1825merge_collapse(MergeState *ms)
1826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 assert(ms);
1830 while (ms->n > 1) {
1831 Py_ssize_t n = ms->n - 2;
1832 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
1833 if (p[n-1].len < p[n+1].len)
1834 --n;
1835 if (merge_at(ms, n) < 0)
1836 return -1;
1837 }
1838 else if (p[n].len <= p[n+1].len) {
1839 if (merge_at(ms, n) < 0)
1840 return -1;
1841 }
1842 else
1843 break;
1844 }
1845 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001846}
1847
1848/* Regardless of invariants, merge all runs on the stack until only one
1849 * remains. This is used at the end of the mergesort.
1850 *
1851 * Returns 0 on success, -1 on error.
1852 */
1853static int
1854merge_force_collapse(MergeState *ms)
1855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 assert(ms);
1859 while (ms->n > 1) {
1860 Py_ssize_t n = ms->n - 2;
1861 if (n > 0 && p[n-1].len < p[n+1].len)
1862 --n;
1863 if (merge_at(ms, n) < 0)
1864 return -1;
1865 }
1866 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001867}
1868
1869/* Compute a good value for the minimum run length; natural runs shorter
1870 * than this are boosted artificially via binary insertion.
1871 *
1872 * If n < 64, return n (it's too small to bother with fancy stuff).
1873 * Else if n is an exact power of 2, return 32.
1874 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1875 * strictly less than, an exact power of 2.
1876 *
1877 * See listsort.txt for more info.
1878 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001879static Py_ssize_t
1880merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 assert(n >= 0);
1885 while (n >= 64) {
1886 r |= n & 1;
1887 n >>= 1;
1888 }
1889 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001890}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001891
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001892static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001893reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001894{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001895 reverse_slice(s->keys, &s->keys[n]);
1896 if (s->values != NULL)
1897 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001898}
1899
Tim Petersa64dc242002-08-01 02:13:36 +00001900/* An adaptive, stable, natural mergesort. See listsort.txt.
1901 * Returns Py_None on success, NULL on error. Even in case of error, the
1902 * list will be some permutation of its input state (nothing is lost or
1903 * duplicated).
1904 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001905static PyObject *
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001906listsort(PyListObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 Py_ssize_t nremaining;
1910 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001911 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 Py_ssize_t saved_ob_size, saved_allocated;
1913 PyObject **saved_ob_item;
1914 PyObject **final_ob_item;
1915 PyObject *result = NULL; /* guilty until proved innocent */
1916 int reverse = 0;
1917 PyObject *keyfunc = NULL;
1918 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 static char *kwlist[] = {"key", "reverse", 0};
Daniel Stutzbach98338222010-12-02 21:55:33 +00001920 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 assert(self != NULL);
1923 assert (PyList_Check(self));
1924 if (args != NULL) {
1925 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort",
1926 kwlist, &keyfunc, &reverse))
1927 return NULL;
1928 if (Py_SIZE(args) > 0) {
1929 PyErr_SetString(PyExc_TypeError,
1930 "must use keyword argument for key function");
1931 return NULL;
1932 }
1933 }
1934 if (keyfunc == Py_None)
1935 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 /* The list is temporarily made empty, so that mutations performed
1938 * by comparison functions can't affect the slice of memory we're
1939 * sorting (allowing mutations during sorting is a core-dump
1940 * factory, since ob_item may change).
1941 */
1942 saved_ob_size = Py_SIZE(self);
1943 saved_ob_item = self->ob_item;
1944 saved_allocated = self->allocated;
1945 Py_SIZE(self) = 0;
1946 self->ob_item = NULL;
1947 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00001948
Daniel Stutzbach98338222010-12-02 21:55:33 +00001949 if (keyfunc == NULL) {
1950 keys = NULL;
1951 lo.keys = saved_ob_item;
1952 lo.values = NULL;
1953 }
1954 else {
1955 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
1956 /* Leverage stack space we allocated but won't otherwise use */
1957 keys = &ms.temparray[saved_ob_size+1];
1958 else {
1959 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
1960 if (keys == NULL)
1961 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001963
1964 for (i = 0; i < saved_ob_size ; i++) {
1965 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
1966 NULL);
1967 if (keys[i] == NULL) {
1968 for (i=i-1 ; i>=0 ; i--)
1969 Py_DECREF(keys[i]);
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00001970 if (keys != &ms.temparray[saved_ob_size+1])
1971 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001972 goto keyfunc_fail;
1973 }
1974 }
1975
1976 lo.keys = keys;
1977 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001979
Daniel Stutzbach98338222010-12-02 21:55:33 +00001980 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 nremaining = saved_ob_size;
1983 if (nremaining < 2)
1984 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00001985
Benjamin Peterson05380642010-08-23 19:35:39 +00001986 /* Reverse sort stability achieved by initially reversing the list,
1987 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001988 if (reverse) {
1989 if (keys != NULL)
1990 reverse_slice(&keys[0], &keys[saved_ob_size]);
1991 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
1992 }
Benjamin Peterson05380642010-08-23 19:35:39 +00001993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 /* March over the array once, left to right, finding natural runs,
1995 * and extending short natural runs to minrun elements.
1996 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 minrun = merge_compute_minrun(nremaining);
1998 do {
1999 int descending;
2000 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 /* Identify next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002003 n = count_run(lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 if (n < 0)
2005 goto fail;
2006 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002007 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 /* If short, extend to min(minrun, nremaining). */
2009 if (n < minrun) {
2010 const Py_ssize_t force = nremaining <= minrun ?
2011 nremaining : minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002012 if (binarysort(lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 goto fail;
2014 n = force;
2015 }
2016 /* Push run onto pending-runs stack, and maybe merge. */
2017 assert(ms.n < MAX_MERGE_PENDING);
2018 ms.pending[ms.n].base = lo;
2019 ms.pending[ms.n].len = n;
2020 ++ms.n;
2021 if (merge_collapse(&ms) < 0)
2022 goto fail;
2023 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002024 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 nremaining -= n;
2026 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 if (merge_force_collapse(&ms) < 0)
2029 goto fail;
2030 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002031 assert(keys == NULL
2032 ? ms.pending[0].base.keys == saved_ob_item
2033 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002035 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002036
2037succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002039fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002040 if (keys != NULL) {
2041 for (i = 0; i < saved_ob_size; i++)
2042 Py_DECREF(keys[i]);
2043 if (keys != &ms.temparray[saved_ob_size+1])
2044 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 if (self->allocated != -1 && result != NULL) {
2048 /* The user mucked with the list during the sort,
2049 * and we don't already have another error to report.
2050 */
2051 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2052 result = NULL;
2053 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 if (reverse && saved_ob_size > 1)
2056 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002059
Daniel Stutzbach98338222010-12-02 21:55:33 +00002060keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 final_ob_item = self->ob_item;
2062 i = Py_SIZE(self);
2063 Py_SIZE(self) = saved_ob_size;
2064 self->ob_item = saved_ob_item;
2065 self->allocated = saved_allocated;
2066 if (final_ob_item != NULL) {
2067 /* we cannot use list_clear() for this because it does not
2068 guarantee that the list is really empty when it returns */
2069 while (--i >= 0) {
2070 Py_XDECREF(final_ob_item[i]);
2071 }
2072 PyMem_FREE(final_ob_item);
2073 }
2074 Py_XINCREF(result);
2075 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002076}
Tim Peters330f9e92002-07-19 07:05:44 +00002077#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002078#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002079
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002080int
Fred Drakea2f55112000-07-09 15:16:51 +00002081PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 if (v == NULL || !PyList_Check(v)) {
2084 PyErr_BadInternalCall();
2085 return -1;
2086 }
2087 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
2088 if (v == NULL)
2089 return -1;
2090 Py_DECREF(v);
2091 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002092}
2093
Guido van Rossumb86c5492001-02-12 22:06:02 +00002094static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002095listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 if (Py_SIZE(self) > 1)
2098 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2099 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002100}
2101
Guido van Rossum84c76f51990-10-30 13:32:20 +00002102int
Fred Drakea2f55112000-07-09 15:16:51 +00002103PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 if (v == NULL || !PyList_Check(v)) {
2108 PyErr_BadInternalCall();
2109 return -1;
2110 }
2111 if (Py_SIZE(self) > 1)
2112 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2113 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002114}
2115
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002116PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002117PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 PyObject *w;
2120 PyObject **p, **q;
2121 Py_ssize_t n;
2122 if (v == NULL || !PyList_Check(v)) {
2123 PyErr_BadInternalCall();
2124 return NULL;
2125 }
2126 n = Py_SIZE(v);
2127 w = PyTuple_New(n);
2128 if (w == NULL)
2129 return NULL;
2130 p = ((PyTupleObject *)w)->ob_item;
2131 q = ((PyListObject *)v)->ob_item;
2132 while (--n >= 0) {
2133 Py_INCREF(*q);
2134 *p = *q;
2135 p++;
2136 q++;
2137 }
2138 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002139}
2140
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002141static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002142listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002145 PyObject *v;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002146
Petri Lehtinenebfaabd2011-11-06 21:02:39 +02002147 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2148 _PyEval_SliceIndex, &start,
2149 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 return NULL;
2151 if (start < 0) {
2152 start += Py_SIZE(self);
2153 if (start < 0)
2154 start = 0;
2155 }
2156 if (stop < 0) {
2157 stop += Py_SIZE(self);
2158 if (stop < 0)
2159 stop = 0;
2160 }
2161 for (i = start; i < stop && i < Py_SIZE(self); i++) {
2162 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2163 if (cmp > 0)
2164 return PyLong_FromSsize_t(i);
2165 else if (cmp < 0)
2166 return NULL;
2167 }
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002168 PyErr_Format(PyExc_ValueError, "%R is not in list", v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002170}
2171
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002172static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002173listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 Py_ssize_t count = 0;
2176 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 for (i = 0; i < Py_SIZE(self); i++) {
2179 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2180 if (cmp > 0)
2181 count++;
2182 else if (cmp < 0)
2183 return NULL;
2184 }
2185 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002186}
2187
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002188static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002189listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 for (i = 0; i < Py_SIZE(self); i++) {
2194 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2195 if (cmp > 0) {
2196 if (list_ass_slice(self, i, i+1,
2197 (PyObject *)NULL) == 0)
2198 Py_RETURN_NONE;
2199 return NULL;
2200 }
2201 else if (cmp < 0)
2202 return NULL;
2203 }
2204 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2205 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002206}
2207
Jeremy Hylton8caad492000-06-23 14:18:11 +00002208static int
2209list_traverse(PyListObject *o, visitproc visit, void *arg)
2210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 for (i = Py_SIZE(o); --i >= 0; )
2214 Py_VISIT(o->ob_item[i]);
2215 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002216}
2217
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002218static PyObject *
2219list_richcompare(PyObject *v, PyObject *w, int op)
2220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 PyListObject *vl, *wl;
2222 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002223
Brian Curtindfc80e32011-08-10 20:28:54 -05002224 if (!PyList_Check(v) || !PyList_Check(w))
2225 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 vl = (PyListObject *)v;
2228 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2231 /* Shortcut: if the lengths differ, the lists differ */
2232 PyObject *res;
2233 if (op == Py_EQ)
2234 res = Py_False;
2235 else
2236 res = Py_True;
2237 Py_INCREF(res);
2238 return res;
2239 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 /* Search for the first index where items are different */
2242 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2243 int k = PyObject_RichCompareBool(vl->ob_item[i],
2244 wl->ob_item[i], Py_EQ);
2245 if (k < 0)
2246 return NULL;
2247 if (!k)
2248 break;
2249 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2252 /* No more items to compare -- compare sizes */
2253 Py_ssize_t vs = Py_SIZE(vl);
2254 Py_ssize_t ws = Py_SIZE(wl);
2255 int cmp;
2256 PyObject *res;
2257 switch (op) {
2258 case Py_LT: cmp = vs < ws; break;
2259 case Py_LE: cmp = vs <= ws; break;
2260 case Py_EQ: cmp = vs == ws; break;
2261 case Py_NE: cmp = vs != ws; break;
2262 case Py_GT: cmp = vs > ws; break;
2263 case Py_GE: cmp = vs >= ws; break;
2264 default: return NULL; /* cannot happen */
2265 }
2266 if (cmp)
2267 res = Py_True;
2268 else
2269 res = Py_False;
2270 Py_INCREF(res);
2271 return res;
2272 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 /* We have an item that differs -- shortcuts for EQ/NE */
2275 if (op == Py_EQ) {
2276 Py_INCREF(Py_False);
2277 return Py_False;
2278 }
2279 if (op == Py_NE) {
2280 Py_INCREF(Py_True);
2281 return Py_True;
2282 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 /* Compare the final item again using the proper operator */
2285 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002286}
2287
Tim Peters6d6c1a32001-08-02 04:15:00 +00002288static int
2289list_init(PyListObject *self, PyObject *args, PyObject *kw)
2290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 PyObject *arg = NULL;
2292 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2295 return -1;
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 /* Verify list invariants established by PyType_GenericAlloc() */
2298 assert(0 <= Py_SIZE(self));
2299 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2300 assert(self->ob_item != NULL ||
2301 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 /* Empty previous contents */
2304 if (self->ob_item != NULL) {
2305 (void)list_clear(self);
2306 }
2307 if (arg != NULL) {
2308 PyObject *rv = listextend(self, arg);
2309 if (rv == NULL)
2310 return -1;
2311 Py_DECREF(rv);
2312 }
2313 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002314}
2315
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002316static PyObject *
2317list_sizeof(PyListObject *self)
2318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 res = sizeof(PyListObject) + self->allocated * sizeof(void*);
2322 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002323}
2324
Raymond Hettinger1021c442003-11-07 15:38:09 +00002325static PyObject *list_iter(PyObject *seq);
2326static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2327
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002328PyDoc_STRVAR(getitem_doc,
2329"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002330PyDoc_STRVAR(reversed_doc,
2331"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002332PyDoc_STRVAR(sizeof_doc,
2333"L.__sizeof__() -- size of L in memory, in bytes");
Eli Benderskycbbaa962011-02-25 05:47:53 +00002334PyDoc_STRVAR(clear_doc,
2335"L.clear() -> None -- remove all items from L");
2336PyDoc_STRVAR(copy_doc,
2337"L.copy() -> list -- a shallow copy of L");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002338PyDoc_STRVAR(append_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002339"L.append(object) -> None -- append object to end");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002340PyDoc_STRVAR(extend_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002341"L.extend(iterable) -> None -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002342PyDoc_STRVAR(insert_doc,
2343"L.insert(index, object) -- insert object before index");
2344PyDoc_STRVAR(pop_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002345"L.pop([index]) -> item -- remove and return item at index (default last).\n"
2346"Raises IndexError if list is empty or index is out of range.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002347PyDoc_STRVAR(remove_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002348"L.remove(value) -> None -- remove first occurrence of value.\n"
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002349"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002350PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002351"L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2352"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002353PyDoc_STRVAR(count_doc,
2354"L.count(value) -> integer -- return number of occurrences of value");
2355PyDoc_STRVAR(reverse_doc,
2356"L.reverse() -- reverse *IN PLACE*");
2357PyDoc_STRVAR(sort_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002358"L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002359
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002360static PyObject *list_subscript(PyListObject*, PyObject*);
2361
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002362static PyMethodDef list_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
2364 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
2365 {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002366 {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc},
2367 {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 {"append", (PyCFunction)listappend, METH_O, append_doc},
2369 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002370 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
2372 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
2373 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
2374 {"count", (PyCFunction)listcount, METH_O, count_doc},
2375 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
2376 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
2377 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002378};
2379
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002380static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 (lenfunc)list_length, /* sq_length */
2382 (binaryfunc)list_concat, /* sq_concat */
2383 (ssizeargfunc)list_repeat, /* sq_repeat */
2384 (ssizeargfunc)list_item, /* sq_item */
2385 0, /* sq_slice */
2386 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2387 0, /* sq_ass_slice */
2388 (objobjproc)list_contains, /* sq_contains */
2389 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2390 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002391};
2392
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002393PyDoc_STRVAR(list_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002394"list() -> new empty list\n"
2395"list(iterable) -> new list initialized from iterable's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002396
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002397static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002398list_subscript(PyListObject* self, PyObject* item)
2399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 if (PyIndex_Check(item)) {
2401 Py_ssize_t i;
2402 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2403 if (i == -1 && PyErr_Occurred())
2404 return NULL;
2405 if (i < 0)
2406 i += PyList_GET_SIZE(self);
2407 return list_item(self, i);
2408 }
2409 else if (PySlice_Check(item)) {
2410 Py_ssize_t start, stop, step, slicelength, cur, i;
2411 PyObject* result;
2412 PyObject* it;
2413 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002414
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002415 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 &start, &stop, &step, &slicelength) < 0) {
2417 return NULL;
2418 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 if (slicelength <= 0) {
2421 return PyList_New(0);
2422 }
2423 else if (step == 1) {
2424 return list_slice(self, start, stop);
2425 }
2426 else {
2427 result = PyList_New(slicelength);
2428 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 src = self->ob_item;
2431 dest = ((PyListObject *)result)->ob_item;
2432 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002433 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 it = src[cur];
2435 Py_INCREF(it);
2436 dest[i] = it;
2437 }
Tim Peters3b01a122002-07-19 02:35:45 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 return result;
2440 }
2441 }
2442 else {
2443 PyErr_Format(PyExc_TypeError,
2444 "list indices must be integers, not %.200s",
2445 item->ob_type->tp_name);
2446 return NULL;
2447 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002448}
2449
Tim Peters3b01a122002-07-19 02:35:45 +00002450static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002451list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 if (PyIndex_Check(item)) {
2454 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2455 if (i == -1 && PyErr_Occurred())
2456 return -1;
2457 if (i < 0)
2458 i += PyList_GET_SIZE(self);
2459 return list_ass_item(self, i, value);
2460 }
2461 else if (PySlice_Check(item)) {
2462 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002463
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002464 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 &start, &stop, &step, &slicelength) < 0) {
2466 return -1;
2467 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 if (step == 1)
2470 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 /* Make sure s[5:2] = [..] inserts at the right place:
2473 before 5, not before 2. */
2474 if ((step < 0 && start < stop) ||
2475 (step > 0 && start > stop))
2476 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 if (value == NULL) {
2479 /* delete slice */
2480 PyObject **garbage;
2481 size_t cur;
2482 Py_ssize_t i;
Tim Peters3b01a122002-07-19 02:35:45 +00002483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 if (slicelength <= 0)
2485 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 if (step < 0) {
2488 stop = start + 1;
2489 start = stop + step*(slicelength - 1) - 1;
2490 step = -step;
2491 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 assert((size_t)slicelength <=
2494 PY_SIZE_MAX / sizeof(PyObject*));
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00002495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 garbage = (PyObject**)
2497 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2498 if (!garbage) {
2499 PyErr_NoMemory();
2500 return -1;
2501 }
Tim Peters3b01a122002-07-19 02:35:45 +00002502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 /* drawing pictures might help understand these for
2504 loops. Basically, we memmove the parts of the
2505 list that are *not* part of the slice: step-1
2506 items for each item that is part of the slice,
2507 and then tail end of the list that was not
2508 covered by the slice */
2509 for (cur = start, i = 0;
2510 cur < (size_t)stop;
2511 cur += step, i++) {
2512 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 if (cur + step >= (size_t)Py_SIZE(self)) {
2517 lim = Py_SIZE(self) - cur - 1;
2518 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 memmove(self->ob_item + cur - i,
2521 self->ob_item + cur + 1,
2522 lim * sizeof(PyObject *));
2523 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002524 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 if (cur < (size_t)Py_SIZE(self)) {
2526 memmove(self->ob_item + cur - slicelength,
2527 self->ob_item + cur,
2528 (Py_SIZE(self) - cur) *
2529 sizeof(PyObject *));
2530 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 Py_SIZE(self) -= slicelength;
2533 list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 for (i = 0; i < slicelength; i++) {
2536 Py_DECREF(garbage[i]);
2537 }
2538 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 return 0;
2541 }
2542 else {
2543 /* assign slice */
2544 PyObject *ins, *seq;
2545 PyObject **garbage, **seqitems, **selfitems;
2546 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 /* protect against a[::-1] = a */
2549 if (self == (PyListObject*)value) {
2550 seq = list_slice((PyListObject*)value, 0,
2551 PyList_GET_SIZE(value));
2552 }
2553 else {
2554 seq = PySequence_Fast(value,
2555 "must assign iterable "
2556 "to extended slice");
2557 }
2558 if (!seq)
2559 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2562 PyErr_Format(PyExc_ValueError,
2563 "attempt to assign sequence of "
2564 "size %zd to extended slice of "
2565 "size %zd",
2566 PySequence_Fast_GET_SIZE(seq),
2567 slicelength);
2568 Py_DECREF(seq);
2569 return -1;
2570 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 if (!slicelength) {
2573 Py_DECREF(seq);
2574 return 0;
2575 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 garbage = (PyObject**)
2578 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2579 if (!garbage) {
2580 Py_DECREF(seq);
2581 PyErr_NoMemory();
2582 return -1;
2583 }
Tim Peters3b01a122002-07-19 02:35:45 +00002584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 selfitems = self->ob_item;
2586 seqitems = PySequence_Fast_ITEMS(seq);
2587 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002588 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 garbage[i] = selfitems[cur];
2590 ins = seqitems[i];
2591 Py_INCREF(ins);
2592 selfitems[cur] = ins;
2593 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 for (i = 0; i < slicelength; i++) {
2596 Py_DECREF(garbage[i]);
2597 }
Tim Peters3b01a122002-07-19 02:35:45 +00002598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 PyMem_FREE(garbage);
2600 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 return 0;
2603 }
2604 }
2605 else {
2606 PyErr_Format(PyExc_TypeError,
2607 "list indices must be integers, not %.200s",
2608 item->ob_type->tp_name);
2609 return -1;
2610 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002611}
2612
2613static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 (lenfunc)list_length,
2615 (binaryfunc)list_subscript,
2616 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002617};
2618
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002619PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002620 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2621 "list",
2622 sizeof(PyListObject),
2623 0,
2624 (destructor)list_dealloc, /* tp_dealloc */
2625 0, /* tp_print */
2626 0, /* tp_getattr */
2627 0, /* tp_setattr */
2628 0, /* tp_reserved */
2629 (reprfunc)list_repr, /* tp_repr */
2630 0, /* tp_as_number */
2631 &list_as_sequence, /* tp_as_sequence */
2632 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002633 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 0, /* tp_call */
2635 0, /* tp_str */
2636 PyObject_GenericGetAttr, /* tp_getattro */
2637 0, /* tp_setattro */
2638 0, /* tp_as_buffer */
2639 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2640 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2641 list_doc, /* tp_doc */
2642 (traverseproc)list_traverse, /* tp_traverse */
2643 (inquiry)list_clear, /* tp_clear */
2644 list_richcompare, /* tp_richcompare */
2645 0, /* tp_weaklistoffset */
2646 list_iter, /* tp_iter */
2647 0, /* tp_iternext */
2648 list_methods, /* tp_methods */
2649 0, /* tp_members */
2650 0, /* tp_getset */
2651 0, /* tp_base */
2652 0, /* tp_dict */
2653 0, /* tp_descr_get */
2654 0, /* tp_descr_set */
2655 0, /* tp_dictoffset */
2656 (initproc)list_init, /* tp_init */
2657 PyType_GenericAlloc, /* tp_alloc */
2658 PyType_GenericNew, /* tp_new */
2659 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002660};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002661
2662
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002663/*********************** List Iterator **************************/
2664
2665typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02002667 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002669} listiterobject;
2670
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002671static PyObject *list_iter(PyObject *);
2672static void listiter_dealloc(listiterobject *);
2673static int listiter_traverse(listiterobject *, visitproc, void *);
2674static PyObject *listiter_next(listiterobject *);
2675static PyObject *listiter_len(listiterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002676static PyObject *listiter_reduce_general(void *_it, int forward);
2677static PyObject *listiter_reduce(listiterobject *);
2678static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002679
Armin Rigof5b3e362006-02-11 21:32:43 +00002680PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002681PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2682PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002683
2684static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002686 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
2687 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002689};
2690
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002691PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2693 "list_iterator", /* tp_name */
2694 sizeof(listiterobject), /* tp_basicsize */
2695 0, /* tp_itemsize */
2696 /* methods */
2697 (destructor)listiter_dealloc, /* tp_dealloc */
2698 0, /* tp_print */
2699 0, /* tp_getattr */
2700 0, /* tp_setattr */
2701 0, /* tp_reserved */
2702 0, /* tp_repr */
2703 0, /* tp_as_number */
2704 0, /* tp_as_sequence */
2705 0, /* tp_as_mapping */
2706 0, /* tp_hash */
2707 0, /* tp_call */
2708 0, /* tp_str */
2709 PyObject_GenericGetAttr, /* tp_getattro */
2710 0, /* tp_setattro */
2711 0, /* tp_as_buffer */
2712 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2713 0, /* tp_doc */
2714 (traverseproc)listiter_traverse, /* tp_traverse */
2715 0, /* tp_clear */
2716 0, /* tp_richcompare */
2717 0, /* tp_weaklistoffset */
2718 PyObject_SelfIter, /* tp_iter */
2719 (iternextfunc)listiter_next, /* tp_iternext */
2720 listiter_methods, /* tp_methods */
2721 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002722};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002723
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002724
2725static PyObject *
2726list_iter(PyObject *seq)
2727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 if (!PyList_Check(seq)) {
2731 PyErr_BadInternalCall();
2732 return NULL;
2733 }
2734 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2735 if (it == NULL)
2736 return NULL;
2737 it->it_index = 0;
2738 Py_INCREF(seq);
2739 it->it_seq = (PyListObject *)seq;
2740 _PyObject_GC_TRACK(it);
2741 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002742}
2743
2744static void
2745listiter_dealloc(listiterobject *it)
2746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 _PyObject_GC_UNTRACK(it);
2748 Py_XDECREF(it->it_seq);
2749 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002750}
2751
2752static int
2753listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 Py_VISIT(it->it_seq);
2756 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002757}
2758
2759static PyObject *
2760listiter_next(listiterobject *it)
2761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 PyListObject *seq;
2763 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 assert(it != NULL);
2766 seq = it->it_seq;
2767 if (seq == NULL)
2768 return NULL;
2769 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 if (it->it_index < PyList_GET_SIZE(seq)) {
2772 item = PyList_GET_ITEM(seq, it->it_index);
2773 ++it->it_index;
2774 Py_INCREF(item);
2775 return item;
2776 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 Py_DECREF(seq);
2779 it->it_seq = NULL;
2780 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002781}
2782
2783static PyObject *
2784listiter_len(listiterobject *it)
2785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 Py_ssize_t len;
2787 if (it->it_seq) {
2788 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2789 if (len >= 0)
2790 return PyLong_FromSsize_t(len);
2791 }
2792 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002793}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002794
2795static PyObject *
2796listiter_reduce(listiterobject *it)
2797{
2798 return listiter_reduce_general(it, 1);
2799}
2800
2801static PyObject *
2802listiter_setstate(listiterobject *it, PyObject *state)
2803{
Victor Stinner7660b882013-06-24 23:59:24 +02002804 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002805 if (index == -1 && PyErr_Occurred())
2806 return NULL;
2807 if (it->it_seq != NULL) {
2808 if (index < 0)
2809 index = 0;
2810 it->it_index = index;
2811 }
2812 Py_RETURN_NONE;
2813}
2814
Raymond Hettinger1021c442003-11-07 15:38:09 +00002815/*********************** List Reverse Iterator **************************/
2816
2817typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 PyObject_HEAD
2819 Py_ssize_t it_index;
2820 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002821} listreviterobject;
2822
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002823static PyObject *list_reversed(PyListObject *, PyObject *);
2824static void listreviter_dealloc(listreviterobject *);
2825static int listreviter_traverse(listreviterobject *, visitproc, void *);
2826static PyObject *listreviter_next(listreviterobject *);
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002827static PyObject *listreviter_len(listreviterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002828static PyObject *listreviter_reduce(listreviterobject *);
2829static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002830
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002831static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002833 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
2834 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002836};
2837
Raymond Hettinger1021c442003-11-07 15:38:09 +00002838PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2840 "list_reverseiterator", /* tp_name */
2841 sizeof(listreviterobject), /* tp_basicsize */
2842 0, /* tp_itemsize */
2843 /* methods */
2844 (destructor)listreviter_dealloc, /* tp_dealloc */
2845 0, /* tp_print */
2846 0, /* tp_getattr */
2847 0, /* tp_setattr */
2848 0, /* tp_reserved */
2849 0, /* tp_repr */
2850 0, /* tp_as_number */
2851 0, /* tp_as_sequence */
2852 0, /* tp_as_mapping */
2853 0, /* tp_hash */
2854 0, /* tp_call */
2855 0, /* tp_str */
2856 PyObject_GenericGetAttr, /* tp_getattro */
2857 0, /* tp_setattro */
2858 0, /* tp_as_buffer */
2859 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2860 0, /* tp_doc */
2861 (traverseproc)listreviter_traverse, /* tp_traverse */
2862 0, /* tp_clear */
2863 0, /* tp_richcompare */
2864 0, /* tp_weaklistoffset */
2865 PyObject_SelfIter, /* tp_iter */
2866 (iternextfunc)listreviter_next, /* tp_iternext */
2867 listreviter_methods, /* tp_methods */
2868 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00002869};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002870
2871static PyObject *
2872list_reversed(PyListObject *seq, PyObject *unused)
2873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2877 if (it == NULL)
2878 return NULL;
2879 assert(PyList_Check(seq));
2880 it->it_index = PyList_GET_SIZE(seq) - 1;
2881 Py_INCREF(seq);
2882 it->it_seq = seq;
2883 PyObject_GC_Track(it);
2884 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002885}
2886
2887static void
2888listreviter_dealloc(listreviterobject *it)
2889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 PyObject_GC_UnTrack(it);
2891 Py_XDECREF(it->it_seq);
2892 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002893}
2894
2895static int
2896listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 Py_VISIT(it->it_seq);
2899 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002900}
2901
2902static PyObject *
2903listreviter_next(listreviterobject *it)
2904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 PyObject *item;
2906 Py_ssize_t index = it->it_index;
2907 PyListObject *seq = it->it_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2910 item = PyList_GET_ITEM(seq, index);
2911 it->it_index--;
2912 Py_INCREF(item);
2913 return item;
2914 }
2915 it->it_index = -1;
2916 if (seq != NULL) {
2917 it->it_seq = NULL;
2918 Py_DECREF(seq);
2919 }
2920 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002921}
2922
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002923static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002924listreviter_len(listreviterobject *it)
2925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 Py_ssize_t len = it->it_index + 1;
2927 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2928 len = 0;
2929 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002930}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002931
2932static PyObject *
2933listreviter_reduce(listreviterobject *it)
2934{
2935 return listiter_reduce_general(it, 0);
2936}
2937
2938static PyObject *
2939listreviter_setstate(listreviterobject *it, PyObject *state)
2940{
2941 Py_ssize_t index = PyLong_AsSsize_t(state);
2942 if (index == -1 && PyErr_Occurred())
2943 return NULL;
2944 if (it->it_seq != NULL) {
2945 if (index < -1)
2946 index = -1;
2947 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
2948 index = PyList_GET_SIZE(it->it_seq) - 1;
2949 it->it_index = index;
2950 }
2951 Py_RETURN_NONE;
2952}
2953
2954/* common pickling support */
2955
2956static PyObject *
2957listiter_reduce_general(void *_it, int forward)
2958{
2959 PyObject *list;
2960
2961 /* the objects are not the same, index is of different types! */
2962 if (forward) {
2963 listiterobject *it = (listiterobject *)_it;
2964 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02002965 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002966 it->it_seq, it->it_index);
2967 } else {
2968 listreviterobject *it = (listreviterobject *)_it;
2969 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +02002970 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("reversed"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002971 it->it_seq, it->it_index);
2972 }
2973 /* empty iterator, create an empty list */
2974 list = PyList_New(0);
2975 if (list == NULL)
2976 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02002977 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002978}