blob: 5e40667239e033fdb97ca19b5189b216d7eab50e [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
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200216PyList_SetItem(PyObject *op, Py_ssize_t i,
217 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200219 PyObject *olditem;
220 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 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;
Victor Stinner5c733472013-11-18 21:11:57 +0100341 PyObject *s;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200342 static PyObject *sep = NULL;
Victor Stinner5c733472013-11-18 21:11:57 +0100343 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200344
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
Victor Stinner5c733472013-11-18 21:11:57 +0100360 _PyUnicodeWriter_Init(&writer);
361 writer.overallocate = 1;
362 if (Py_SIZE(v) > 1) {
363 /* "[" + "1" + ", 2" * (len - 1) + "]" */
364 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
365 }
366 else {
367 /* "[1]" */
368 writer.min_length = 3;
369 }
Tim Petersa7259592001-06-16 05:11:17 +0000370
Victor Stinner5c733472013-11-18 21:11:57 +0100371 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200372 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 /* Do repr() on each element. Note that this may mutate the list,
375 so must refetch the list size on each iteration. */
376 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100377 if (i > 0) {
378 if (_PyUnicodeWriter_WriteStr(&writer, sep) < 0)
379 goto error;
380 }
381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 if (Py_EnterRecursiveCall(" while getting the repr of a list"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200383 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 s = PyObject_Repr(v->ob_item[i]);
385 Py_LeaveRecursiveCall();
Victor Stinner5c733472013-11-18 21:11:57 +0100386 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200387 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100388
389 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
390 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200391 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100392 }
393 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 }
Victor Stinner5c733472013-11-18 21:11:57 +0100395
396 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200397 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100400 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200401
402error:
Victor Stinner5c733472013-11-18 21:11:57 +0100403 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200404 Py_ReprLeave((PyObject *)v);
405 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406}
407
Martin v. Löwis18e16552006-02-15 17:27:45 +0000408static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000409list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412}
413
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000414static int
Fred Drakea2f55112000-07-09 15:16:51 +0000415list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 Py_ssize_t i;
418 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
421 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
422 Py_EQ);
423 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000424}
425
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000426static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000427list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (i < 0 || i >= Py_SIZE(a)) {
430 if (indexerr == NULL) {
431 indexerr = PyUnicode_FromString(
432 "list index out of range");
433 if (indexerr == NULL)
434 return NULL;
435 }
436 PyErr_SetObject(PyExc_IndexError, indexerr);
437 return NULL;
438 }
439 Py_INCREF(a->ob_item[i]);
440 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441}
442
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000444list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 PyListObject *np;
447 PyObject **src, **dest;
448 Py_ssize_t i, len;
449 if (ilow < 0)
450 ilow = 0;
451 else if (ilow > Py_SIZE(a))
452 ilow = Py_SIZE(a);
453 if (ihigh < ilow)
454 ihigh = ilow;
455 else if (ihigh > Py_SIZE(a))
456 ihigh = Py_SIZE(a);
457 len = ihigh - ilow;
458 np = (PyListObject *) PyList_New(len);
459 if (np == NULL)
460 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 src = a->ob_item + ilow;
463 dest = np->ob_item;
464 for (i = 0; i < len; i++) {
465 PyObject *v = src[i];
466 Py_INCREF(v);
467 dest[i] = v;
468 }
469 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000470}
471
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000472PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000473PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 if (!PyList_Check(a)) {
476 PyErr_BadInternalCall();
477 return NULL;
478 }
479 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000480}
481
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000482static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000483list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 Py_ssize_t size;
486 Py_ssize_t i;
487 PyObject **src, **dest;
488 PyListObject *np;
489 if (!PyList_Check(bb)) {
490 PyErr_Format(PyExc_TypeError,
491 "can only concatenate list (not \"%.200s\") to list",
492 bb->ob_type->tp_name);
493 return NULL;
494 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000495#define b ((PyListObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 size = Py_SIZE(a) + Py_SIZE(b);
497 if (size < 0)
498 return PyErr_NoMemory();
499 np = (PyListObject *) PyList_New(size);
500 if (np == NULL) {
501 return NULL;
502 }
503 src = a->ob_item;
504 dest = np->ob_item;
505 for (i = 0; i < Py_SIZE(a); i++) {
506 PyObject *v = src[i];
507 Py_INCREF(v);
508 dest[i] = v;
509 }
510 src = b->ob_item;
511 dest = np->ob_item + Py_SIZE(a);
512 for (i = 0; i < Py_SIZE(b); i++) {
513 PyObject *v = src[i];
514 Py_INCREF(v);
515 dest[i] = v;
516 }
517 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518#undef b
519}
520
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000521static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000522list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 Py_ssize_t i, j;
525 Py_ssize_t size;
526 PyListObject *np;
527 PyObject **p, **items;
528 PyObject *elem;
529 if (n < 0)
530 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100531 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100533 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 if (size == 0)
535 return PyList_New(0);
536 np = (PyListObject *) PyList_New(size);
537 if (np == NULL)
538 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 items = np->ob_item;
541 if (Py_SIZE(a) == 1) {
542 elem = a->ob_item[0];
543 for (i = 0; i < n; i++) {
544 items[i] = elem;
545 Py_INCREF(elem);
546 }
547 return (PyObject *) np;
548 }
549 p = np->ob_item;
550 items = a->ob_item;
551 for (i = 0; i < n; i++) {
552 for (j = 0; j < Py_SIZE(a); j++) {
553 *p = items[j];
554 Py_INCREF(*p);
555 p++;
556 }
557 }
558 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000559}
560
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000561static int
Armin Rigo93677f02004-07-29 12:40:23 +0000562list_clear(PyListObject *a)
563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 Py_ssize_t i;
565 PyObject **item = a->ob_item;
566 if (item != NULL) {
567 /* Because XDECREF can recursively invoke operations on
568 this list, we make it empty first. */
569 i = Py_SIZE(a);
570 Py_SIZE(a) = 0;
571 a->ob_item = NULL;
572 a->allocated = 0;
573 while (--i >= 0) {
574 Py_XDECREF(item[i]);
575 }
576 PyMem_FREE(item);
577 }
578 /* Never fails; the return value can be ignored.
579 Note that there is no guarantee that the list is actually empty
580 at this point, because XDECREF may have populated it again! */
581 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000582}
583
Tim Peters8fc4a912004-07-31 21:53:19 +0000584/* a[ilow:ihigh] = v if v != NULL.
585 * del a[ilow:ihigh] if v == NULL.
586 *
587 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
588 * guaranteed the call cannot fail.
589 */
Armin Rigo93677f02004-07-29 12:40:23 +0000590static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000591list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 /* Because [X]DECREF can recursively invoke list operations on
594 this list, we must postpone all [X]DECREF activity until
595 after the list is back in its canonical shape. Therefore
596 we must allocate an additional array, 'recycle', into which
597 we temporarily copy the items that are deleted from the
598 list. :-( */
599 PyObject *recycle_on_stack[8];
600 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
601 PyObject **item;
602 PyObject **vitem = NULL;
603 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
604 Py_ssize_t n; /* # of elements in replacement list */
605 Py_ssize_t norig; /* # of elements in list getting replaced */
606 Py_ssize_t d; /* Change in size */
607 Py_ssize_t k;
608 size_t s;
609 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 if (v == NULL)
612 n = 0;
613 else {
614 if (a == b) {
615 /* Special case "a[i:j] = a" -- copy b first */
616 v = list_slice(b, 0, Py_SIZE(b));
617 if (v == NULL)
618 return result;
619 result = list_ass_slice(a, ilow, ihigh, v);
620 Py_DECREF(v);
621 return result;
622 }
623 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
624 if(v_as_SF == NULL)
625 goto Error;
626 n = PySequence_Fast_GET_SIZE(v_as_SF);
627 vitem = PySequence_Fast_ITEMS(v_as_SF);
628 }
629 if (ilow < 0)
630 ilow = 0;
631 else if (ilow > Py_SIZE(a))
632 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 if (ihigh < ilow)
635 ihigh = ilow;
636 else if (ihigh > Py_SIZE(a))
637 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 norig = ihigh - ilow;
640 assert(norig >= 0);
641 d = n - norig;
642 if (Py_SIZE(a) + d == 0) {
643 Py_XDECREF(v_as_SF);
644 return list_clear(a);
645 }
646 item = a->ob_item;
647 /* recycle the items that we are about to remove */
648 s = norig * sizeof(PyObject *);
649 if (s > sizeof(recycle_on_stack)) {
650 recycle = (PyObject **)PyMem_MALLOC(s);
651 if (recycle == NULL) {
652 PyErr_NoMemory();
653 goto Error;
654 }
655 }
656 memcpy(recycle, &item[ilow], s);
Tim Peters8d9eb102004-07-31 02:24:20 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200659 Py_ssize_t tail;
660 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
661 memmove(&item[ihigh+d], &item[ihigh], tail);
662 if (list_resize(a, Py_SIZE(a) + d) < 0) {
663 memmove(&item[ihigh], &item[ihigh+d], tail);
664 memcpy(&item[ilow], recycle, s);
665 goto Error;
666 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 item = a->ob_item;
668 }
669 else if (d > 0) { /* Insert d items */
670 k = Py_SIZE(a);
671 if (list_resize(a, k+d) < 0)
672 goto Error;
673 item = a->ob_item;
674 memmove(&item[ihigh+d], &item[ihigh],
675 (k - ihigh)*sizeof(PyObject *));
676 }
677 for (k = 0; k < n; k++, ilow++) {
678 PyObject *w = vitem[k];
679 Py_XINCREF(w);
680 item[ilow] = w;
681 }
682 for (k = norig - 1; k >= 0; --k)
683 Py_XDECREF(recycle[k]);
684 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000685 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (recycle != recycle_on_stack)
687 PyMem_FREE(recycle);
688 Py_XDECREF(v_as_SF);
689 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000690#undef b
691}
692
Guido van Rossum234f9421993-06-17 12:35:49 +0000693int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000694PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 if (!PyList_Check(a)) {
697 PyErr_BadInternalCall();
698 return -1;
699 }
700 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000701}
702
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000703static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000704list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 PyObject **items;
707 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000708
709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 size = PyList_GET_SIZE(self);
711 if (size == 0 || n == 1) {
712 Py_INCREF(self);
713 return (PyObject *)self;
714 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 if (n < 1) {
717 (void)list_clear(self);
718 Py_INCREF(self);
719 return (PyObject *)self;
720 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 if (size > PY_SSIZE_T_MAX / n) {
723 return PyErr_NoMemory();
724 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 if (list_resize(self, size*n) == -1)
727 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 p = size;
730 items = self->ob_item;
731 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
732 for (j = 0; j < size; j++) {
733 PyObject *o = items[j];
734 Py_INCREF(o);
735 items[p++] = o;
736 }
737 }
738 Py_INCREF(self);
739 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000740}
741
Guido van Rossum4a450d01991-04-03 19:05:18 +0000742static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000743list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 PyObject *old_value;
746 if (i < 0 || i >= Py_SIZE(a)) {
747 PyErr_SetString(PyExc_IndexError,
748 "list assignment index out of range");
749 return -1;
750 }
751 if (v == NULL)
752 return list_ass_slice(a, i, i+1, v);
753 Py_INCREF(v);
754 old_value = a->ob_item[i];
755 a->ob_item[i] = v;
756 Py_DECREF(old_value);
757 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000758}
759
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000760static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000761listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 Py_ssize_t i;
764 PyObject *v;
765 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
766 return NULL;
767 if (ins1(self, i, v) == 0)
768 Py_RETURN_NONE;
769 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770}
771
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000772static PyObject *
Eli Benderskycbbaa962011-02-25 05:47:53 +0000773listclear(PyListObject *self)
774{
775 list_clear(self);
776 Py_RETURN_NONE;
777}
778
779static PyObject *
780listcopy(PyListObject *self)
781{
782 return list_slice(self, 0, Py_SIZE(self));
783}
784
785static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000786listappend(PyListObject *self, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 if (app1(self, v) == 0)
789 Py_RETURN_NONE;
790 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000791}
792
Barry Warsawdedf6d61998-10-09 16:37:25 +0000793static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000794listextend(PyListObject *self, PyObject *b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 PyObject *it; /* iter(v) */
797 Py_ssize_t m; /* size of self */
798 Py_ssize_t n; /* guess for size of b */
799 Py_ssize_t mn; /* m + n */
800 Py_ssize_t i;
801 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 /* Special cases:
804 1) lists and tuples which can use PySequence_Fast ops
805 2) extending self to self requires making a copy first
806 */
807 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
808 PyObject **src, **dest;
809 b = PySequence_Fast(b, "argument must be iterable");
810 if (!b)
811 return NULL;
812 n = PySequence_Fast_GET_SIZE(b);
813 if (n == 0) {
814 /* short circuit when b is empty */
815 Py_DECREF(b);
816 Py_RETURN_NONE;
817 }
818 m = Py_SIZE(self);
819 if (list_resize(self, m + n) == -1) {
820 Py_DECREF(b);
821 return NULL;
822 }
823 /* note that we may still have self == b here for the
824 * situation a.extend(a), but the following code works
825 * in that case too. Just make sure to resize self
826 * before calling PySequence_Fast_ITEMS.
827 */
828 /* populate the end of self with b's items */
829 src = PySequence_Fast_ITEMS(b);
830 dest = self->ob_item + m;
831 for (i = 0; i < n; i++) {
832 PyObject *o = src[i];
833 Py_INCREF(o);
834 dest[i] = o;
835 }
836 Py_DECREF(b);
837 Py_RETURN_NONE;
838 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 it = PyObject_GetIter(b);
841 if (it == NULL)
842 return NULL;
843 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 /* Guess a result list size. */
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200846 n = PyObject_LengthHint(b, 8);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 if (n == -1) {
848 Py_DECREF(it);
849 return NULL;
850 }
851 m = Py_SIZE(self);
852 mn = m + n;
853 if (mn >= m) {
854 /* Make room. */
855 if (list_resize(self, mn) == -1)
856 goto error;
857 /* Make the list sane again. */
858 Py_SIZE(self) = m;
859 }
860 /* Else m + n overflowed; on the chance that n lied, and there really
861 * is enough room, ignore it. If n was telling the truth, we'll
862 * eventually run out of memory during the loop.
863 */
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 /* Run iterator to exhaustion. */
866 for (;;) {
867 PyObject *item = iternext(it);
868 if (item == NULL) {
869 if (PyErr_Occurred()) {
870 if (PyErr_ExceptionMatches(PyExc_StopIteration))
871 PyErr_Clear();
872 else
873 goto error;
874 }
875 break;
876 }
877 if (Py_SIZE(self) < self->allocated) {
878 /* steals ref */
879 PyList_SET_ITEM(self, Py_SIZE(self), item);
880 ++Py_SIZE(self);
881 }
882 else {
883 int status = app1(self, item);
884 Py_DECREF(item); /* append creates a new ref */
885 if (status < 0)
886 goto error;
887 }
888 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200891 if (Py_SIZE(self) < self->allocated) {
892 if (list_resize(self, Py_SIZE(self)) < 0)
893 goto error;
894 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 Py_DECREF(it);
897 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000898
899 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 Py_DECREF(it);
901 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000902}
903
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000904PyObject *
905_PyList_Extend(PyListObject *self, PyObject *b)
906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 return listextend(self, b);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000908}
909
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000910static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000911list_inplace_concat(PyListObject *self, PyObject *other)
912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 result = listextend(self, other);
916 if (result == NULL)
917 return result;
918 Py_DECREF(result);
919 Py_INCREF(self);
920 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000921}
922
923static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000924listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 Py_ssize_t i = -1;
927 PyObject *v;
928 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 if (!PyArg_ParseTuple(args, "|n:pop", &i))
931 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 if (Py_SIZE(self) == 0) {
934 /* Special-case most common failure cause */
935 PyErr_SetString(PyExc_IndexError, "pop from empty list");
936 return NULL;
937 }
938 if (i < 0)
939 i += Py_SIZE(self);
940 if (i < 0 || i >= Py_SIZE(self)) {
941 PyErr_SetString(PyExc_IndexError, "pop index out of range");
942 return NULL;
943 }
944 v = self->ob_item[i];
945 if (i == Py_SIZE(self) - 1) {
946 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +0200947 if (status >= 0)
948 return v; /* and v now owns the reference the list had */
949 else
950 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 }
952 Py_INCREF(v);
953 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +0200954 if (status < 0) {
955 Py_DECREF(v);
956 return NULL;
957 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000959}
960
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000961/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
962static void
963reverse_slice(PyObject **lo, PyObject **hi)
964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 --hi;
968 while (lo < hi) {
969 PyObject *t = *lo;
970 *lo = *hi;
971 *hi = t;
972 ++lo;
973 --hi;
974 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000975}
976
Tim Petersa64dc242002-08-01 02:13:36 +0000977/* Lots of code for an adaptive, stable, natural mergesort. There are many
978 * pieces to this algorithm; read listsort.txt for overviews and details.
979 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000980
Daniel Stutzbach98338222010-12-02 21:55:33 +0000981/* A sortslice contains a pointer to an array of keys and a pointer to
982 * an array of corresponding values. In other words, keys[i]
983 * corresponds with values[i]. If values == NULL, then the keys are
984 * also the values.
985 *
986 * Several convenience routines are provided here, so that keys and
987 * values are always moved in sync.
988 */
989
990typedef struct {
991 PyObject **keys;
992 PyObject **values;
993} sortslice;
994
995Py_LOCAL_INLINE(void)
996sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
997{
998 s1->keys[i] = s2->keys[j];
999 if (s1->values != NULL)
1000 s1->values[i] = s2->values[j];
1001}
1002
1003Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001004sortslice_copy_incr(sortslice *dst, sortslice *src)
1005{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001006 *dst->keys++ = *src->keys++;
1007 if (dst->values != NULL)
1008 *dst->values++ = *src->values++;
1009}
1010
1011Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001012sortslice_copy_decr(sortslice *dst, sortslice *src)
1013{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001014 *dst->keys-- = *src->keys--;
1015 if (dst->values != NULL)
1016 *dst->values-- = *src->values--;
1017}
1018
1019
1020Py_LOCAL_INLINE(void)
1021sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001022 Py_ssize_t n)
1023{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001024 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1025 if (s1->values != NULL)
1026 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1027}
1028
1029Py_LOCAL_INLINE(void)
1030sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001031 Py_ssize_t n)
1032{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001033 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1034 if (s1->values != NULL)
1035 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1036}
1037
1038Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001039sortslice_advance(sortslice *slice, Py_ssize_t n)
1040{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001041 slice->keys += n;
1042 if (slice->values != NULL)
1043 slice->values += n;
1044}
1045
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001046/* Comparison function: PyObject_RichCompareBool with Py_LT.
Tim Petersa64dc242002-08-01 02:13:36 +00001047 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1048 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001049
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001050#define ISLT(X, Y) (PyObject_RichCompareBool(X, Y, Py_LT))
Tim Peters66860f62002-08-04 17:47:26 +00001051
1052/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001053 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1054 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1055*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001056#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001058
1059/* binarysort is the best method for sorting small arrays: it does
1060 few compares, but can do data movement quadratic in the number of
1061 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001062 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001063 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001064 On entry, must have lo <= start <= hi, and that [lo, start) is already
1065 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001066 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001067 Even in case of error, the output slice will be some permutation of
1068 the input (nothing is lost or duplicated).
1069*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001070static int
Daniel Stutzbach98338222010-12-02 21:55:33 +00001071binarysort(sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001072{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001073 Py_ssize_t k;
1074 PyObject **l, **p, **r;
1075 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001076
Daniel Stutzbach98338222010-12-02 21:55:33 +00001077 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001079 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 ++start;
1081 for (; start < hi; ++start) {
1082 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001083 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 r = start;
1085 pivot = *r;
1086 /* Invariants:
1087 * pivot >= all in [lo, l).
1088 * pivot < all in [r, start).
1089 * The second is vacuously true at the start.
1090 */
1091 assert(l < r);
1092 do {
1093 p = l + ((r - l) >> 1);
1094 IFLT(pivot, *p)
1095 r = p;
1096 else
1097 l = p+1;
1098 } while (l < r);
1099 assert(l == r);
1100 /* The invariants still hold, so pivot >= all in [lo, l) and
1101 pivot < all in [l, start), so pivot belongs at l. Note
1102 that if there are elements equal to pivot, l points to the
1103 first slot after them -- that's why this sort is stable.
1104 Slide over to make room.
1105 Caution: using memmove is much slower under MSVC 5;
1106 we're not usually moving many slots. */
1107 for (p = start; p > l; --p)
1108 *p = *(p-1);
1109 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001110 if (lo.values != NULL) {
1111 Py_ssize_t offset = lo.values - lo.keys;
1112 p = start + offset;
1113 pivot = *p;
1114 l += offset;
1115 for (p = start + offset; p > l; --p)
1116 *p = *(p-1);
1117 *l = pivot;
1118 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 }
1120 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001121
1122 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001124}
1125
Tim Petersa64dc242002-08-01 02:13:36 +00001126/*
1127Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1128is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001129
Tim Petersa64dc242002-08-01 02:13:36 +00001130 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001131
Tim Petersa64dc242002-08-01 02:13:36 +00001132or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001133
Tim Petersa64dc242002-08-01 02:13:36 +00001134 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001135
Tim Petersa64dc242002-08-01 02:13:36 +00001136Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1137For its intended use in a stable mergesort, the strictness of the defn of
1138"descending" is needed so that the caller can safely reverse a descending
1139sequence without violating stability (strict > ensures there are no equal
1140elements to get out of order).
1141
1142Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001143*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001144static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001145count_run(PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 Py_ssize_t k;
1148 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 assert(lo < hi);
1151 *descending = 0;
1152 ++lo;
1153 if (lo == hi)
1154 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 n = 2;
1157 IFLT(*lo, *(lo-1)) {
1158 *descending = 1;
1159 for (lo = lo+1; lo < hi; ++lo, ++n) {
1160 IFLT(*lo, *(lo-1))
1161 ;
1162 else
1163 break;
1164 }
1165 }
1166 else {
1167 for (lo = lo+1; lo < hi; ++lo, ++n) {
1168 IFLT(*lo, *(lo-1))
1169 break;
1170 }
1171 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001174fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001176}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001177
Tim Petersa64dc242002-08-01 02:13:36 +00001178/*
1179Locate the proper position of key in a sorted vector; if the vector contains
1180an element equal to key, return the position immediately to the left of
1181the leftmost equal element. [gallop_right() does the same except returns
1182the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001183
Tim Petersa64dc242002-08-01 02:13:36 +00001184"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001185
Tim Petersa64dc242002-08-01 02:13:36 +00001186"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1187hint is to the final result, the faster this runs.
1188
1189The return value is the int k in 0..n such that
1190
1191 a[k-1] < key <= a[k]
1192
1193pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1194key belongs at index k; or, IOW, the first k elements of a should precede
1195key, and the last n-k should follow key.
1196
1197Returns -1 on error. See listsort.txt for info on the method.
1198*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001199static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001200gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 Py_ssize_t ofs;
1203 Py_ssize_t lastofs;
1204 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 a += hint;
1209 lastofs = 0;
1210 ofs = 1;
1211 IFLT(*a, key) {
1212 /* a[hint] < key -- gallop right, until
1213 * a[hint + lastofs] < key <= a[hint + ofs]
1214 */
1215 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1216 while (ofs < maxofs) {
1217 IFLT(a[ofs], key) {
1218 lastofs = ofs;
1219 ofs = (ofs << 1) + 1;
1220 if (ofs <= 0) /* int overflow */
1221 ofs = maxofs;
1222 }
1223 else /* key <= a[hint + ofs] */
1224 break;
1225 }
1226 if (ofs > maxofs)
1227 ofs = maxofs;
1228 /* Translate back to offsets relative to &a[0]. */
1229 lastofs += hint;
1230 ofs += hint;
1231 }
1232 else {
1233 /* key <= a[hint] -- gallop left, until
1234 * a[hint - ofs] < key <= a[hint - lastofs]
1235 */
1236 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1237 while (ofs < maxofs) {
1238 IFLT(*(a-ofs), key)
1239 break;
1240 /* key <= a[hint - ofs] */
1241 lastofs = ofs;
1242 ofs = (ofs << 1) + 1;
1243 if (ofs <= 0) /* int overflow */
1244 ofs = maxofs;
1245 }
1246 if (ofs > maxofs)
1247 ofs = maxofs;
1248 /* Translate back to positive offsets relative to &a[0]. */
1249 k = lastofs;
1250 lastofs = hint - ofs;
1251 ofs = hint - k;
1252 }
1253 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1256 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1257 * right of lastofs but no farther right than ofs. Do a binary
1258 * search, with invariant a[lastofs-1] < key <= a[ofs].
1259 */
1260 ++lastofs;
1261 while (lastofs < ofs) {
1262 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 IFLT(a[m], key)
1265 lastofs = m+1; /* a[m] < key */
1266 else
1267 ofs = m; /* key <= a[m] */
1268 }
1269 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1270 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001271
1272fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001274}
1275
1276/*
1277Exactly like gallop_left(), except that if key already exists in a[0:n],
1278finds the position immediately to the right of the rightmost equal value.
1279
1280The return value is the int k in 0..n such that
1281
1282 a[k-1] <= key < a[k]
1283
1284or -1 if error.
1285
1286The code duplication is massive, but this is enough different given that
1287we're sticking to "<" comparisons that it's much harder to follow if
1288written as one routine with yet another "left or right?" flag.
1289*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001290static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001291gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 Py_ssize_t ofs;
1294 Py_ssize_t lastofs;
1295 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 a += hint;
1300 lastofs = 0;
1301 ofs = 1;
1302 IFLT(key, *a) {
1303 /* key < a[hint] -- gallop left, until
1304 * a[hint - ofs] <= key < a[hint - lastofs]
1305 */
1306 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1307 while (ofs < maxofs) {
1308 IFLT(key, *(a-ofs)) {
1309 lastofs = ofs;
1310 ofs = (ofs << 1) + 1;
1311 if (ofs <= 0) /* int overflow */
1312 ofs = maxofs;
1313 }
1314 else /* a[hint - ofs] <= key */
1315 break;
1316 }
1317 if (ofs > maxofs)
1318 ofs = maxofs;
1319 /* Translate back to positive offsets relative to &a[0]. */
1320 k = lastofs;
1321 lastofs = hint - ofs;
1322 ofs = hint - k;
1323 }
1324 else {
1325 /* a[hint] <= key -- gallop right, until
1326 * a[hint + lastofs] <= key < a[hint + ofs]
1327 */
1328 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1329 while (ofs < maxofs) {
1330 IFLT(key, a[ofs])
1331 break;
1332 /* a[hint + ofs] <= key */
1333 lastofs = ofs;
1334 ofs = (ofs << 1) + 1;
1335 if (ofs <= 0) /* int overflow */
1336 ofs = maxofs;
1337 }
1338 if (ofs > maxofs)
1339 ofs = maxofs;
1340 /* Translate back to offsets relative to &a[0]. */
1341 lastofs += hint;
1342 ofs += hint;
1343 }
1344 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1347 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1348 * right of lastofs but no farther right than ofs. Do a binary
1349 * search, with invariant a[lastofs-1] <= key < a[ofs].
1350 */
1351 ++lastofs;
1352 while (lastofs < ofs) {
1353 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 IFLT(key, a[m])
1356 ofs = m; /* key < a[m] */
1357 else
1358 lastofs = m+1; /* a[m] <= key */
1359 }
1360 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1361 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001362
1363fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001365}
1366
1367/* The maximum number of entries in a MergeState's pending-runs stack.
1368 * This is enough to sort arrays of size up to about
1369 * 32 * phi ** MAX_MERGE_PENDING
1370 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1371 * with 2**64 elements.
1372 */
1373#define MAX_MERGE_PENDING 85
1374
Tim Peterse05f65a2002-08-10 05:21:15 +00001375/* When we get into galloping mode, we stay there until both runs win less
1376 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
Tim Petersa64dc242002-08-01 02:13:36 +00001377 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001378#define MIN_GALLOP 7
Tim Petersa64dc242002-08-01 02:13:36 +00001379
1380/* Avoid malloc for small temp arrays. */
1381#define MERGESTATE_TEMP_SIZE 256
1382
1383/* One MergeState exists on the stack per invocation of mergesort. It's just
1384 * a convenient way to pass state around among the helper functions.
1385 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001386struct s_slice {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001387 sortslice base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 Py_ssize_t len;
Tim Peterse05f65a2002-08-10 05:21:15 +00001389};
1390
Tim Petersa64dc242002-08-01 02:13:36 +00001391typedef struct s_MergeState {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 /* This controls when we get *into* galloping mode. It's initialized
1393 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1394 * random data, and lower for highly structured data.
1395 */
1396 Py_ssize_t min_gallop;
Tim Peterse05f65a2002-08-10 05:21:15 +00001397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 /* 'a' is temp storage to help with merges. It contains room for
1399 * alloced entries.
1400 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001401 sortslice a; /* may point to temparray below */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 Py_ssize_t alloced;
Tim Petersa64dc242002-08-01 02:13:36 +00001403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 /* A stack of n pending runs yet to be merged. Run #i starts at
1405 * address base[i] and extends for len[i] elements. It's always
1406 * true (so long as the indices are in bounds) that
1407 *
1408 * pending[i].base + pending[i].len == pending[i+1].base
1409 *
1410 * so we could cut the storage for this, but it's a minor amount,
1411 * and keeping all the info explicit simplifies the code.
1412 */
1413 int n;
1414 struct s_slice pending[MAX_MERGE_PENDING];
Tim Petersa64dc242002-08-01 02:13:36 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 /* 'a' points to this when possible, rather than muck with malloc. */
1417 PyObject *temparray[MERGESTATE_TEMP_SIZE];
Tim Petersa64dc242002-08-01 02:13:36 +00001418} MergeState;
1419
1420/* Conceptually a MergeState's constructor. */
1421static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001422merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001425 if (has_keyfunc) {
1426 /* The temporary space for merging will need at most half the list
1427 * size rounded up. Use the minimum possible space so we can use the
1428 * rest of temparray for other things. In particular, if there is
1429 * enough extra space, listsort() will use it to store the keys.
1430 */
1431 ms->alloced = (list_size + 1) / 2;
1432
1433 /* ms->alloced describes how many keys will be stored at
1434 ms->temparray, but we also need to store the values. Hence,
1435 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1436 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1437 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1438 ms->a.values = &ms->temparray[ms->alloced];
1439 }
1440 else {
1441 ms->alloced = MERGESTATE_TEMP_SIZE;
1442 ms->a.values = NULL;
1443 }
1444 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 ms->n = 0;
1446 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001447}
1448
1449/* Free all the temp memory owned by the MergeState. This must be called
1450 * when you're done with a MergeState, and may be called before then if
1451 * you want to free the temp memory early.
1452 */
1453static void
1454merge_freemem(MergeState *ms)
1455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001457 if (ms->a.keys != ms->temparray)
1458 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001459}
1460
1461/* Ensure enough temp memory for 'need' array slots is available.
1462 * Returns 0 on success and -1 if the memory can't be gotten.
1463 */
1464static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001465merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001466{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001467 int multiplier;
1468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 assert(ms != NULL);
1470 if (need <= ms->alloced)
1471 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001472
1473 multiplier = ms->a.values != NULL ? 2 : 1;
1474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 /* Don't realloc! That can cost cycles to copy the old data, but
1476 * we don't care what's in the block.
1477 */
1478 merge_freemem(ms);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001479 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 PyErr_NoMemory();
1481 return -1;
1482 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001483 ms->a.keys = (PyObject**)PyMem_Malloc(multiplier * need
1484 * sizeof(PyObject *));
1485 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001487 if (ms->a.values != NULL)
1488 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 return 0;
1490 }
1491 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001493}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1495 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001496
Daniel Stutzbach98338222010-12-02 21:55:33 +00001497/* Merge the na elements starting at ssa with the nb elements starting at
1498 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1499 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1500 * should have na <= nb. See listsort.txt for more info. Return 0 if
1501 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001502 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001503static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001504merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1505 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001508 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 int result = -1; /* guilty until proved innocent */
1510 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001511
Daniel Stutzbach98338222010-12-02 21:55:33 +00001512 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1513 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 if (MERGE_GETMEM(ms, na) < 0)
1515 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001516 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1517 dest = ssa;
1518 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001519
Daniel Stutzbach98338222010-12-02 21:55:33 +00001520 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 --nb;
1522 if (nb == 0)
1523 goto Succeed;
1524 if (na == 1)
1525 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 min_gallop = ms->min_gallop;
1528 for (;;) {
1529 Py_ssize_t acount = 0; /* # of times A won in a row */
1530 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 /* Do the straightforward thing until (if ever) one run
1533 * appears to win consistently.
1534 */
1535 for (;;) {
1536 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001537 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 if (k) {
1539 if (k < 0)
1540 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001541 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 ++bcount;
1543 acount = 0;
1544 --nb;
1545 if (nb == 0)
1546 goto Succeed;
1547 if (bcount >= min_gallop)
1548 break;
1549 }
1550 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001551 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 ++acount;
1553 bcount = 0;
1554 --na;
1555 if (na == 1)
1556 goto CopyB;
1557 if (acount >= min_gallop)
1558 break;
1559 }
1560 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 /* One run is winning so consistently that galloping may
1563 * be a huge win. So try that, and continue galloping until
1564 * (if ever) neither run appears to be winning consistently
1565 * anymore.
1566 */
1567 ++min_gallop;
1568 do {
1569 assert(na > 1 && nb > 0);
1570 min_gallop -= min_gallop > 1;
1571 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001572 k = gallop_right(ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 acount = k;
1574 if (k) {
1575 if (k < 0)
1576 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001577 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1578 sortslice_advance(&dest, k);
1579 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 na -= k;
1581 if (na == 1)
1582 goto CopyB;
1583 /* na==0 is impossible now if the comparison
1584 * function is consistent, but we can't assume
1585 * that it is.
1586 */
1587 if (na == 0)
1588 goto Succeed;
1589 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001590 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 --nb;
1592 if (nb == 0)
1593 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001594
Daniel Stutzbach98338222010-12-02 21:55:33 +00001595 k = gallop_left(ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 bcount = k;
1597 if (k) {
1598 if (k < 0)
1599 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001600 sortslice_memmove(&dest, 0, &ssb, 0, k);
1601 sortslice_advance(&dest, k);
1602 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 nb -= k;
1604 if (nb == 0)
1605 goto Succeed;
1606 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001607 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 --na;
1609 if (na == 1)
1610 goto CopyB;
1611 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1612 ++min_gallop; /* penalize it for leaving galloping mode */
1613 ms->min_gallop = min_gallop;
1614 }
Tim Petersa64dc242002-08-01 02:13:36 +00001615Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001617Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001619 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001621CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001623 /* The last element of ssa belongs at the end of the merge. */
1624 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1625 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001627}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001628
Daniel Stutzbach98338222010-12-02 21:55:33 +00001629/* Merge the na elements starting at pa with the nb elements starting at
1630 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1631 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1632 * should have na >= nb. See listsort.txt for more info. Return 0 if
1633 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001634 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001635static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001636merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1637 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001640 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001643
Daniel Stutzbach98338222010-12-02 21:55:33 +00001644 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1645 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 if (MERGE_GETMEM(ms, nb) < 0)
1647 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001648 dest = ssb;
1649 sortslice_advance(&dest, nb-1);
1650 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1651 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001653 ssb.keys = ms->a.keys + nb - 1;
1654 if (ssb.values != NULL)
1655 ssb.values = ms->a.values + nb - 1;
1656 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001657
Daniel Stutzbach98338222010-12-02 21:55:33 +00001658 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 --na;
1660 if (na == 0)
1661 goto Succeed;
1662 if (nb == 1)
1663 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 min_gallop = ms->min_gallop;
1666 for (;;) {
1667 Py_ssize_t acount = 0; /* # of times A won in a row */
1668 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 /* Do the straightforward thing until (if ever) one run
1671 * appears to win consistently.
1672 */
1673 for (;;) {
1674 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001675 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 if (k) {
1677 if (k < 0)
1678 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001679 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 ++acount;
1681 bcount = 0;
1682 --na;
1683 if (na == 0)
1684 goto Succeed;
1685 if (acount >= min_gallop)
1686 break;
1687 }
1688 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001689 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 ++bcount;
1691 acount = 0;
1692 --nb;
1693 if (nb == 1)
1694 goto CopyA;
1695 if (bcount >= min_gallop)
1696 break;
1697 }
1698 }
Tim Petersa64dc242002-08-01 02:13:36 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 /* One run is winning so consistently that galloping may
1701 * be a huge win. So try that, and continue galloping until
1702 * (if ever) neither run appears to be winning consistently
1703 * anymore.
1704 */
1705 ++min_gallop;
1706 do {
1707 assert(na > 0 && nb > 1);
1708 min_gallop -= min_gallop > 1;
1709 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001710 k = gallop_right(ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 if (k < 0)
1712 goto Fail;
1713 k = na - k;
1714 acount = k;
1715 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001716 sortslice_advance(&dest, -k);
1717 sortslice_advance(&ssa, -k);
1718 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 na -= k;
1720 if (na == 0)
1721 goto Succeed;
1722 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001723 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 --nb;
1725 if (nb == 1)
1726 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001727
Daniel Stutzbach98338222010-12-02 21:55:33 +00001728 k = gallop_left(ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 if (k < 0)
1730 goto Fail;
1731 k = nb - k;
1732 bcount = k;
1733 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001734 sortslice_advance(&dest, -k);
1735 sortslice_advance(&ssb, -k);
1736 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 nb -= k;
1738 if (nb == 1)
1739 goto CopyA;
1740 /* nb==0 is impossible now if the comparison
1741 * function is consistent, but we can't assume
1742 * that it is.
1743 */
1744 if (nb == 0)
1745 goto Succeed;
1746 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001747 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 --na;
1749 if (na == 0)
1750 goto Succeed;
1751 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1752 ++min_gallop; /* penalize it for leaving galloping mode */
1753 ms->min_gallop = min_gallop;
1754 }
Tim Petersa64dc242002-08-01 02:13:36 +00001755Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001757Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001759 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001761CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001763 /* The first element of ssb belongs at the front of the merge. */
1764 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1765 sortslice_advance(&dest, -na);
1766 sortslice_advance(&ssa, -na);
1767 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001769}
1770
1771/* Merge the two runs at stack indices i and i+1.
1772 * Returns 0 on success, -1 on error.
1773 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001774static Py_ssize_t
1775merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001776{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001777 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 Py_ssize_t na, nb;
1779 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 assert(ms != NULL);
1782 assert(ms->n >= 2);
1783 assert(i >= 0);
1784 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001785
Daniel Stutzbach98338222010-12-02 21:55:33 +00001786 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001788 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 nb = ms->pending[i+1].len;
1790 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001791 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 /* Record the length of the combined runs; if i is the 3rd-last
1794 * run now, also slide over the last run (which isn't involved
1795 * in this merge). The current run i+1 goes away in any case.
1796 */
1797 ms->pending[i].len = na + nb;
1798 if (i == ms->n - 3)
1799 ms->pending[i+1] = ms->pending[i+2];
1800 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 /* Where does b start in a? Elements in a before that can be
1803 * ignored (already in place).
1804 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001805 k = gallop_right(*ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 if (k < 0)
1807 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001808 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 na -= k;
1810 if (na == 0)
1811 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 /* Where does a end in b? Elements in b after that can be
1814 * ignored (already in place).
1815 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001816 nb = gallop_left(ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 if (nb <= 0)
1818 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 /* Merge what remains of the runs, using a temp array with
1821 * min(na, nb) elements.
1822 */
1823 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001824 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001826 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001827}
1828
1829/* Examine the stack of runs waiting to be merged, merging adjacent runs
1830 * until the stack invariants are re-established:
1831 *
1832 * 1. len[-3] > len[-2] + len[-1]
1833 * 2. len[-2] > len[-1]
1834 *
1835 * See listsort.txt for more info.
1836 *
1837 * Returns 0 on success, -1 on error.
1838 */
1839static int
1840merge_collapse(MergeState *ms)
1841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 assert(ms);
1845 while (ms->n > 1) {
1846 Py_ssize_t n = ms->n - 2;
1847 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
1848 if (p[n-1].len < p[n+1].len)
1849 --n;
1850 if (merge_at(ms, n) < 0)
1851 return -1;
1852 }
1853 else if (p[n].len <= p[n+1].len) {
1854 if (merge_at(ms, n) < 0)
1855 return -1;
1856 }
1857 else
1858 break;
1859 }
1860 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001861}
1862
1863/* Regardless of invariants, merge all runs on the stack until only one
1864 * remains. This is used at the end of the mergesort.
1865 *
1866 * Returns 0 on success, -1 on error.
1867 */
1868static int
1869merge_force_collapse(MergeState *ms)
1870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 assert(ms);
1874 while (ms->n > 1) {
1875 Py_ssize_t n = ms->n - 2;
1876 if (n > 0 && p[n-1].len < p[n+1].len)
1877 --n;
1878 if (merge_at(ms, n) < 0)
1879 return -1;
1880 }
1881 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001882}
1883
1884/* Compute a good value for the minimum run length; natural runs shorter
1885 * than this are boosted artificially via binary insertion.
1886 *
1887 * If n < 64, return n (it's too small to bother with fancy stuff).
1888 * Else if n is an exact power of 2, return 32.
1889 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1890 * strictly less than, an exact power of 2.
1891 *
1892 * See listsort.txt for more info.
1893 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001894static Py_ssize_t
1895merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 assert(n >= 0);
1900 while (n >= 64) {
1901 r |= n & 1;
1902 n >>= 1;
1903 }
1904 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001905}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001906
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001907static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001908reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001909{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001910 reverse_slice(s->keys, &s->keys[n]);
1911 if (s->values != NULL)
1912 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001913}
1914
Tim Petersa64dc242002-08-01 02:13:36 +00001915/* An adaptive, stable, natural mergesort. See listsort.txt.
1916 * Returns Py_None on success, NULL on error. Even in case of error, the
1917 * list will be some permutation of its input state (nothing is lost or
1918 * duplicated).
1919 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001920static PyObject *
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001921listsort(PyListObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 Py_ssize_t nremaining;
1925 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001926 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 Py_ssize_t saved_ob_size, saved_allocated;
1928 PyObject **saved_ob_item;
1929 PyObject **final_ob_item;
1930 PyObject *result = NULL; /* guilty until proved innocent */
1931 int reverse = 0;
1932 PyObject *keyfunc = NULL;
1933 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 static char *kwlist[] = {"key", "reverse", 0};
Daniel Stutzbach98338222010-12-02 21:55:33 +00001935 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 assert(self != NULL);
1938 assert (PyList_Check(self));
1939 if (args != NULL) {
1940 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort",
1941 kwlist, &keyfunc, &reverse))
1942 return NULL;
1943 if (Py_SIZE(args) > 0) {
1944 PyErr_SetString(PyExc_TypeError,
1945 "must use keyword argument for key function");
1946 return NULL;
1947 }
1948 }
1949 if (keyfunc == Py_None)
1950 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 /* The list is temporarily made empty, so that mutations performed
1953 * by comparison functions can't affect the slice of memory we're
1954 * sorting (allowing mutations during sorting is a core-dump
1955 * factory, since ob_item may change).
1956 */
1957 saved_ob_size = Py_SIZE(self);
1958 saved_ob_item = self->ob_item;
1959 saved_allocated = self->allocated;
1960 Py_SIZE(self) = 0;
1961 self->ob_item = NULL;
1962 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00001963
Daniel Stutzbach98338222010-12-02 21:55:33 +00001964 if (keyfunc == NULL) {
1965 keys = NULL;
1966 lo.keys = saved_ob_item;
1967 lo.values = NULL;
1968 }
1969 else {
1970 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
1971 /* Leverage stack space we allocated but won't otherwise use */
1972 keys = &ms.temparray[saved_ob_size+1];
1973 else {
1974 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
1975 if (keys == NULL)
1976 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001978
1979 for (i = 0; i < saved_ob_size ; i++) {
1980 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
1981 NULL);
1982 if (keys[i] == NULL) {
1983 for (i=i-1 ; i>=0 ; i--)
1984 Py_DECREF(keys[i]);
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00001985 if (keys != &ms.temparray[saved_ob_size+1])
1986 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001987 goto keyfunc_fail;
1988 }
1989 }
1990
1991 lo.keys = keys;
1992 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001994
Daniel Stutzbach98338222010-12-02 21:55:33 +00001995 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 nremaining = saved_ob_size;
1998 if (nremaining < 2)
1999 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002000
Benjamin Peterson05380642010-08-23 19:35:39 +00002001 /* Reverse sort stability achieved by initially reversing the list,
2002 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002003 if (reverse) {
2004 if (keys != NULL)
2005 reverse_slice(&keys[0], &keys[saved_ob_size]);
2006 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2007 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 /* March over the array once, left to right, finding natural runs,
2010 * and extending short natural runs to minrun elements.
2011 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 minrun = merge_compute_minrun(nremaining);
2013 do {
2014 int descending;
2015 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 /* Identify next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002018 n = count_run(lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 if (n < 0)
2020 goto fail;
2021 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002022 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 /* If short, extend to min(minrun, nremaining). */
2024 if (n < minrun) {
2025 const Py_ssize_t force = nremaining <= minrun ?
2026 nremaining : minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002027 if (binarysort(lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 goto fail;
2029 n = force;
2030 }
2031 /* Push run onto pending-runs stack, and maybe merge. */
2032 assert(ms.n < MAX_MERGE_PENDING);
2033 ms.pending[ms.n].base = lo;
2034 ms.pending[ms.n].len = n;
2035 ++ms.n;
2036 if (merge_collapse(&ms) < 0)
2037 goto fail;
2038 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002039 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 nremaining -= n;
2041 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 if (merge_force_collapse(&ms) < 0)
2044 goto fail;
2045 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002046 assert(keys == NULL
2047 ? ms.pending[0].base.keys == saved_ob_item
2048 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002050 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002051
2052succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002054fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002055 if (keys != NULL) {
2056 for (i = 0; i < saved_ob_size; i++)
2057 Py_DECREF(keys[i]);
2058 if (keys != &ms.temparray[saved_ob_size+1])
2059 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 if (self->allocated != -1 && result != NULL) {
2063 /* The user mucked with the list during the sort,
2064 * and we don't already have another error to report.
2065 */
2066 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2067 result = NULL;
2068 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 if (reverse && saved_ob_size > 1)
2071 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002074
Daniel Stutzbach98338222010-12-02 21:55:33 +00002075keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 final_ob_item = self->ob_item;
2077 i = Py_SIZE(self);
2078 Py_SIZE(self) = saved_ob_size;
2079 self->ob_item = saved_ob_item;
2080 self->allocated = saved_allocated;
2081 if (final_ob_item != NULL) {
2082 /* we cannot use list_clear() for this because it does not
2083 guarantee that the list is really empty when it returns */
2084 while (--i >= 0) {
2085 Py_XDECREF(final_ob_item[i]);
2086 }
2087 PyMem_FREE(final_ob_item);
2088 }
2089 Py_XINCREF(result);
2090 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002091}
Tim Peters330f9e92002-07-19 07:05:44 +00002092#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002093#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002094
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002095int
Fred Drakea2f55112000-07-09 15:16:51 +00002096PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 if (v == NULL || !PyList_Check(v)) {
2099 PyErr_BadInternalCall();
2100 return -1;
2101 }
2102 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
2103 if (v == NULL)
2104 return -1;
2105 Py_DECREF(v);
2106 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002107}
2108
Guido van Rossumb86c5492001-02-12 22:06:02 +00002109static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002110listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 if (Py_SIZE(self) > 1)
2113 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2114 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002115}
2116
Guido van Rossum84c76f51990-10-30 13:32:20 +00002117int
Fred Drakea2f55112000-07-09 15:16:51 +00002118PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 if (v == NULL || !PyList_Check(v)) {
2123 PyErr_BadInternalCall();
2124 return -1;
2125 }
2126 if (Py_SIZE(self) > 1)
2127 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2128 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002129}
2130
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002131PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002132PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 PyObject *w;
2135 PyObject **p, **q;
2136 Py_ssize_t n;
2137 if (v == NULL || !PyList_Check(v)) {
2138 PyErr_BadInternalCall();
2139 return NULL;
2140 }
2141 n = Py_SIZE(v);
2142 w = PyTuple_New(n);
2143 if (w == NULL)
2144 return NULL;
2145 p = ((PyTupleObject *)w)->ob_item;
2146 q = ((PyListObject *)v)->ob_item;
2147 while (--n >= 0) {
2148 Py_INCREF(*q);
2149 *p = *q;
2150 p++;
2151 q++;
2152 }
2153 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002154}
2155
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002156static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002157listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002160 PyObject *v;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002161
Petri Lehtinenebfaabd2011-11-06 21:02:39 +02002162 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2163 _PyEval_SliceIndex, &start,
2164 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 return NULL;
2166 if (start < 0) {
2167 start += Py_SIZE(self);
2168 if (start < 0)
2169 start = 0;
2170 }
2171 if (stop < 0) {
2172 stop += Py_SIZE(self);
2173 if (stop < 0)
2174 stop = 0;
2175 }
2176 for (i = start; i < stop && i < Py_SIZE(self); i++) {
2177 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2178 if (cmp > 0)
2179 return PyLong_FromSsize_t(i);
2180 else if (cmp < 0)
2181 return NULL;
2182 }
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002183 PyErr_Format(PyExc_ValueError, "%R is not in list", v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002185}
2186
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002187static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002188listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 Py_ssize_t count = 0;
2191 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 count++;
2197 else if (cmp < 0)
2198 return NULL;
2199 }
2200 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002201}
2202
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002203static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002204listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 for (i = 0; i < Py_SIZE(self); i++) {
2209 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2210 if (cmp > 0) {
2211 if (list_ass_slice(self, i, i+1,
2212 (PyObject *)NULL) == 0)
2213 Py_RETURN_NONE;
2214 return NULL;
2215 }
2216 else if (cmp < 0)
2217 return NULL;
2218 }
2219 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2220 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002221}
2222
Jeremy Hylton8caad492000-06-23 14:18:11 +00002223static int
2224list_traverse(PyListObject *o, visitproc visit, void *arg)
2225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 for (i = Py_SIZE(o); --i >= 0; )
2229 Py_VISIT(o->ob_item[i]);
2230 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002231}
2232
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002233static PyObject *
2234list_richcompare(PyObject *v, PyObject *w, int op)
2235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 PyListObject *vl, *wl;
2237 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002238
Brian Curtindfc80e32011-08-10 20:28:54 -05002239 if (!PyList_Check(v) || !PyList_Check(w))
2240 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 vl = (PyListObject *)v;
2243 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2246 /* Shortcut: if the lengths differ, the lists differ */
2247 PyObject *res;
2248 if (op == Py_EQ)
2249 res = Py_False;
2250 else
2251 res = Py_True;
2252 Py_INCREF(res);
2253 return res;
2254 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 /* Search for the first index where items are different */
2257 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2258 int k = PyObject_RichCompareBool(vl->ob_item[i],
2259 wl->ob_item[i], Py_EQ);
2260 if (k < 0)
2261 return NULL;
2262 if (!k)
2263 break;
2264 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2267 /* No more items to compare -- compare sizes */
2268 Py_ssize_t vs = Py_SIZE(vl);
2269 Py_ssize_t ws = Py_SIZE(wl);
2270 int cmp;
2271 PyObject *res;
2272 switch (op) {
2273 case Py_LT: cmp = vs < ws; break;
2274 case Py_LE: cmp = vs <= ws; break;
2275 case Py_EQ: cmp = vs == ws; break;
2276 case Py_NE: cmp = vs != ws; break;
2277 case Py_GT: cmp = vs > ws; break;
2278 case Py_GE: cmp = vs >= ws; break;
2279 default: return NULL; /* cannot happen */
2280 }
2281 if (cmp)
2282 res = Py_True;
2283 else
2284 res = Py_False;
2285 Py_INCREF(res);
2286 return res;
2287 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 /* We have an item that differs -- shortcuts for EQ/NE */
2290 if (op == Py_EQ) {
2291 Py_INCREF(Py_False);
2292 return Py_False;
2293 }
2294 if (op == Py_NE) {
2295 Py_INCREF(Py_True);
2296 return Py_True;
2297 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 /* Compare the final item again using the proper operator */
2300 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002301}
2302
Tim Peters6d6c1a32001-08-02 04:15:00 +00002303static int
2304list_init(PyListObject *self, PyObject *args, PyObject *kw)
2305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 PyObject *arg = NULL;
2307 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2310 return -1;
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 /* Verify list invariants established by PyType_GenericAlloc() */
2313 assert(0 <= Py_SIZE(self));
2314 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2315 assert(self->ob_item != NULL ||
2316 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 /* Empty previous contents */
2319 if (self->ob_item != NULL) {
2320 (void)list_clear(self);
2321 }
2322 if (arg != NULL) {
2323 PyObject *rv = listextend(self, arg);
2324 if (rv == NULL)
2325 return -1;
2326 Py_DECREF(rv);
2327 }
2328 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002329}
2330
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002331static PyObject *
2332list_sizeof(PyListObject *self)
2333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 res = sizeof(PyListObject) + self->allocated * sizeof(void*);
2337 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002338}
2339
Raymond Hettinger1021c442003-11-07 15:38:09 +00002340static PyObject *list_iter(PyObject *seq);
2341static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2342
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002343PyDoc_STRVAR(getitem_doc,
2344"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002345PyDoc_STRVAR(reversed_doc,
2346"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002347PyDoc_STRVAR(sizeof_doc,
2348"L.__sizeof__() -- size of L in memory, in bytes");
Eli Benderskycbbaa962011-02-25 05:47:53 +00002349PyDoc_STRVAR(clear_doc,
2350"L.clear() -> None -- remove all items from L");
2351PyDoc_STRVAR(copy_doc,
2352"L.copy() -> list -- a shallow copy of L");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002353PyDoc_STRVAR(append_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002354"L.append(object) -> None -- append object to end");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002355PyDoc_STRVAR(extend_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002356"L.extend(iterable) -> None -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002357PyDoc_STRVAR(insert_doc,
2358"L.insert(index, object) -- insert object before index");
2359PyDoc_STRVAR(pop_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002360"L.pop([index]) -> item -- remove and return item at index (default last).\n"
2361"Raises IndexError if list is empty or index is out of range.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002362PyDoc_STRVAR(remove_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002363"L.remove(value) -> None -- remove first occurrence of value.\n"
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002364"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002365PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002366"L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2367"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002368PyDoc_STRVAR(count_doc,
2369"L.count(value) -> integer -- return number of occurrences of value");
2370PyDoc_STRVAR(reverse_doc,
2371"L.reverse() -- reverse *IN PLACE*");
2372PyDoc_STRVAR(sort_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002373"L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002374
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002375static PyObject *list_subscript(PyListObject*, PyObject*);
2376
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002377static PyMethodDef list_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
2379 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
2380 {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002381 {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc},
2382 {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 {"append", (PyCFunction)listappend, METH_O, append_doc},
2384 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002385 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
2387 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
2388 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
2389 {"count", (PyCFunction)listcount, METH_O, count_doc},
2390 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
2391 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
2392 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002393};
2394
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002395static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 (lenfunc)list_length, /* sq_length */
2397 (binaryfunc)list_concat, /* sq_concat */
2398 (ssizeargfunc)list_repeat, /* sq_repeat */
2399 (ssizeargfunc)list_item, /* sq_item */
2400 0, /* sq_slice */
2401 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2402 0, /* sq_ass_slice */
2403 (objobjproc)list_contains, /* sq_contains */
2404 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2405 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002406};
2407
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002408PyDoc_STRVAR(list_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002409"list() -> new empty list\n"
2410"list(iterable) -> new list initialized from iterable's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002411
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002412static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002413list_subscript(PyListObject* self, PyObject* item)
2414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 if (PyIndex_Check(item)) {
2416 Py_ssize_t i;
2417 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2418 if (i == -1 && PyErr_Occurred())
2419 return NULL;
2420 if (i < 0)
2421 i += PyList_GET_SIZE(self);
2422 return list_item(self, i);
2423 }
2424 else if (PySlice_Check(item)) {
2425 Py_ssize_t start, stop, step, slicelength, cur, i;
2426 PyObject* result;
2427 PyObject* it;
2428 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002429
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002430 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 &start, &stop, &step, &slicelength) < 0) {
2432 return NULL;
2433 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 if (slicelength <= 0) {
2436 return PyList_New(0);
2437 }
2438 else if (step == 1) {
2439 return list_slice(self, start, stop);
2440 }
2441 else {
2442 result = PyList_New(slicelength);
2443 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 src = self->ob_item;
2446 dest = ((PyListObject *)result)->ob_item;
2447 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002448 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 it = src[cur];
2450 Py_INCREF(it);
2451 dest[i] = it;
2452 }
Tim Peters3b01a122002-07-19 02:35:45 +00002453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 return result;
2455 }
2456 }
2457 else {
2458 PyErr_Format(PyExc_TypeError,
2459 "list indices must be integers, not %.200s",
2460 item->ob_type->tp_name);
2461 return NULL;
2462 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002463}
2464
Tim Peters3b01a122002-07-19 02:35:45 +00002465static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002466list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 if (PyIndex_Check(item)) {
2469 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2470 if (i == -1 && PyErr_Occurred())
2471 return -1;
2472 if (i < 0)
2473 i += PyList_GET_SIZE(self);
2474 return list_ass_item(self, i, value);
2475 }
2476 else if (PySlice_Check(item)) {
2477 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002478
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002479 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 &start, &stop, &step, &slicelength) < 0) {
2481 return -1;
2482 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 if (step == 1)
2485 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 /* Make sure s[5:2] = [..] inserts at the right place:
2488 before 5, not before 2. */
2489 if ((step < 0 && start < stop) ||
2490 (step > 0 && start > stop))
2491 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 if (value == NULL) {
2494 /* delete slice */
2495 PyObject **garbage;
2496 size_t cur;
2497 Py_ssize_t i;
Tim Peters3b01a122002-07-19 02:35:45 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 if (slicelength <= 0)
2500 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 if (step < 0) {
2503 stop = start + 1;
2504 start = stop + step*(slicelength - 1) - 1;
2505 step = -step;
2506 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 assert((size_t)slicelength <=
2509 PY_SIZE_MAX / sizeof(PyObject*));
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00002510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 garbage = (PyObject**)
2512 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2513 if (!garbage) {
2514 PyErr_NoMemory();
2515 return -1;
2516 }
Tim Peters3b01a122002-07-19 02:35:45 +00002517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 /* drawing pictures might help understand these for
2519 loops. Basically, we memmove the parts of the
2520 list that are *not* part of the slice: step-1
2521 items for each item that is part of the slice,
2522 and then tail end of the list that was not
2523 covered by the slice */
2524 for (cur = start, i = 0;
2525 cur < (size_t)stop;
2526 cur += step, i++) {
2527 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 if (cur + step >= (size_t)Py_SIZE(self)) {
2532 lim = Py_SIZE(self) - cur - 1;
2533 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 memmove(self->ob_item + cur - i,
2536 self->ob_item + cur + 1,
2537 lim * sizeof(PyObject *));
2538 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002539 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 if (cur < (size_t)Py_SIZE(self)) {
2541 memmove(self->ob_item + cur - slicelength,
2542 self->ob_item + cur,
2543 (Py_SIZE(self) - cur) *
2544 sizeof(PyObject *));
2545 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 Py_SIZE(self) -= slicelength;
2548 list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 for (i = 0; i < slicelength; i++) {
2551 Py_DECREF(garbage[i]);
2552 }
2553 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 return 0;
2556 }
2557 else {
2558 /* assign slice */
2559 PyObject *ins, *seq;
2560 PyObject **garbage, **seqitems, **selfitems;
2561 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 /* protect against a[::-1] = a */
2564 if (self == (PyListObject*)value) {
2565 seq = list_slice((PyListObject*)value, 0,
2566 PyList_GET_SIZE(value));
2567 }
2568 else {
2569 seq = PySequence_Fast(value,
2570 "must assign iterable "
2571 "to extended slice");
2572 }
2573 if (!seq)
2574 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2577 PyErr_Format(PyExc_ValueError,
2578 "attempt to assign sequence of "
2579 "size %zd to extended slice of "
2580 "size %zd",
2581 PySequence_Fast_GET_SIZE(seq),
2582 slicelength);
2583 Py_DECREF(seq);
2584 return -1;
2585 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 if (!slicelength) {
2588 Py_DECREF(seq);
2589 return 0;
2590 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 garbage = (PyObject**)
2593 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2594 if (!garbage) {
2595 Py_DECREF(seq);
2596 PyErr_NoMemory();
2597 return -1;
2598 }
Tim Peters3b01a122002-07-19 02:35:45 +00002599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 selfitems = self->ob_item;
2601 seqitems = PySequence_Fast_ITEMS(seq);
2602 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002603 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002604 garbage[i] = selfitems[cur];
2605 ins = seqitems[i];
2606 Py_INCREF(ins);
2607 selfitems[cur] = ins;
2608 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610 for (i = 0; i < slicelength; i++) {
2611 Py_DECREF(garbage[i]);
2612 }
Tim Peters3b01a122002-07-19 02:35:45 +00002613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 PyMem_FREE(garbage);
2615 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 return 0;
2618 }
2619 }
2620 else {
2621 PyErr_Format(PyExc_TypeError,
2622 "list indices must be integers, not %.200s",
2623 item->ob_type->tp_name);
2624 return -1;
2625 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002626}
2627
2628static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 (lenfunc)list_length,
2630 (binaryfunc)list_subscript,
2631 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002632};
2633
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002634PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2636 "list",
2637 sizeof(PyListObject),
2638 0,
2639 (destructor)list_dealloc, /* tp_dealloc */
2640 0, /* tp_print */
2641 0, /* tp_getattr */
2642 0, /* tp_setattr */
2643 0, /* tp_reserved */
2644 (reprfunc)list_repr, /* tp_repr */
2645 0, /* tp_as_number */
2646 &list_as_sequence, /* tp_as_sequence */
2647 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002648 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 0, /* tp_call */
2650 0, /* tp_str */
2651 PyObject_GenericGetAttr, /* tp_getattro */
2652 0, /* tp_setattro */
2653 0, /* tp_as_buffer */
2654 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2655 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2656 list_doc, /* tp_doc */
2657 (traverseproc)list_traverse, /* tp_traverse */
2658 (inquiry)list_clear, /* tp_clear */
2659 list_richcompare, /* tp_richcompare */
2660 0, /* tp_weaklistoffset */
2661 list_iter, /* tp_iter */
2662 0, /* tp_iternext */
2663 list_methods, /* tp_methods */
2664 0, /* tp_members */
2665 0, /* tp_getset */
2666 0, /* tp_base */
2667 0, /* tp_dict */
2668 0, /* tp_descr_get */
2669 0, /* tp_descr_set */
2670 0, /* tp_dictoffset */
2671 (initproc)list_init, /* tp_init */
2672 PyType_GenericAlloc, /* tp_alloc */
2673 PyType_GenericNew, /* tp_new */
2674 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002675};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002676
2677
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002678/*********************** List Iterator **************************/
2679
2680typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02002682 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002684} listiterobject;
2685
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002686static PyObject *list_iter(PyObject *);
2687static void listiter_dealloc(listiterobject *);
2688static int listiter_traverse(listiterobject *, visitproc, void *);
2689static PyObject *listiter_next(listiterobject *);
2690static PyObject *listiter_len(listiterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002691static PyObject *listiter_reduce_general(void *_it, int forward);
2692static PyObject *listiter_reduce(listiterobject *);
2693static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002694
Armin Rigof5b3e362006-02-11 21:32:43 +00002695PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002696PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2697PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002698
2699static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002701 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
2702 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002704};
2705
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002706PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2708 "list_iterator", /* tp_name */
2709 sizeof(listiterobject), /* tp_basicsize */
2710 0, /* tp_itemsize */
2711 /* methods */
2712 (destructor)listiter_dealloc, /* tp_dealloc */
2713 0, /* tp_print */
2714 0, /* tp_getattr */
2715 0, /* tp_setattr */
2716 0, /* tp_reserved */
2717 0, /* tp_repr */
2718 0, /* tp_as_number */
2719 0, /* tp_as_sequence */
2720 0, /* tp_as_mapping */
2721 0, /* tp_hash */
2722 0, /* tp_call */
2723 0, /* tp_str */
2724 PyObject_GenericGetAttr, /* tp_getattro */
2725 0, /* tp_setattro */
2726 0, /* tp_as_buffer */
2727 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2728 0, /* tp_doc */
2729 (traverseproc)listiter_traverse, /* tp_traverse */
2730 0, /* tp_clear */
2731 0, /* tp_richcompare */
2732 0, /* tp_weaklistoffset */
2733 PyObject_SelfIter, /* tp_iter */
2734 (iternextfunc)listiter_next, /* tp_iternext */
2735 listiter_methods, /* tp_methods */
2736 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002737};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002738
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002739
2740static PyObject *
2741list_iter(PyObject *seq)
2742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 if (!PyList_Check(seq)) {
2746 PyErr_BadInternalCall();
2747 return NULL;
2748 }
2749 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2750 if (it == NULL)
2751 return NULL;
2752 it->it_index = 0;
2753 Py_INCREF(seq);
2754 it->it_seq = (PyListObject *)seq;
2755 _PyObject_GC_TRACK(it);
2756 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002757}
2758
2759static void
2760listiter_dealloc(listiterobject *it)
2761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 _PyObject_GC_UNTRACK(it);
2763 Py_XDECREF(it->it_seq);
2764 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002765}
2766
2767static int
2768listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 Py_VISIT(it->it_seq);
2771 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002772}
2773
2774static PyObject *
2775listiter_next(listiterobject *it)
2776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 PyListObject *seq;
2778 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 assert(it != NULL);
2781 seq = it->it_seq;
2782 if (seq == NULL)
2783 return NULL;
2784 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 if (it->it_index < PyList_GET_SIZE(seq)) {
2787 item = PyList_GET_ITEM(seq, it->it_index);
2788 ++it->it_index;
2789 Py_INCREF(item);
2790 return item;
2791 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 Py_DECREF(seq);
2794 it->it_seq = NULL;
2795 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002796}
2797
2798static PyObject *
2799listiter_len(listiterobject *it)
2800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 Py_ssize_t len;
2802 if (it->it_seq) {
2803 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2804 if (len >= 0)
2805 return PyLong_FromSsize_t(len);
2806 }
2807 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002808}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002809
2810static PyObject *
2811listiter_reduce(listiterobject *it)
2812{
2813 return listiter_reduce_general(it, 1);
2814}
2815
2816static PyObject *
2817listiter_setstate(listiterobject *it, PyObject *state)
2818{
Victor Stinner7660b882013-06-24 23:59:24 +02002819 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002820 if (index == -1 && PyErr_Occurred())
2821 return NULL;
2822 if (it->it_seq != NULL) {
2823 if (index < 0)
2824 index = 0;
2825 it->it_index = index;
2826 }
2827 Py_RETURN_NONE;
2828}
2829
Raymond Hettinger1021c442003-11-07 15:38:09 +00002830/*********************** List Reverse Iterator **************************/
2831
2832typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 PyObject_HEAD
2834 Py_ssize_t it_index;
2835 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002836} listreviterobject;
2837
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002838static PyObject *list_reversed(PyListObject *, PyObject *);
2839static void listreviter_dealloc(listreviterobject *);
2840static int listreviter_traverse(listreviterobject *, visitproc, void *);
2841static PyObject *listreviter_next(listreviterobject *);
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002842static PyObject *listreviter_len(listreviterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002843static PyObject *listreviter_reduce(listreviterobject *);
2844static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002845
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002846static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002848 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
2849 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002851};
2852
Raymond Hettinger1021c442003-11-07 15:38:09 +00002853PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2855 "list_reverseiterator", /* tp_name */
2856 sizeof(listreviterobject), /* tp_basicsize */
2857 0, /* tp_itemsize */
2858 /* methods */
2859 (destructor)listreviter_dealloc, /* tp_dealloc */
2860 0, /* tp_print */
2861 0, /* tp_getattr */
2862 0, /* tp_setattr */
2863 0, /* tp_reserved */
2864 0, /* tp_repr */
2865 0, /* tp_as_number */
2866 0, /* tp_as_sequence */
2867 0, /* tp_as_mapping */
2868 0, /* tp_hash */
2869 0, /* tp_call */
2870 0, /* tp_str */
2871 PyObject_GenericGetAttr, /* tp_getattro */
2872 0, /* tp_setattro */
2873 0, /* tp_as_buffer */
2874 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2875 0, /* tp_doc */
2876 (traverseproc)listreviter_traverse, /* tp_traverse */
2877 0, /* tp_clear */
2878 0, /* tp_richcompare */
2879 0, /* tp_weaklistoffset */
2880 PyObject_SelfIter, /* tp_iter */
2881 (iternextfunc)listreviter_next, /* tp_iternext */
2882 listreviter_methods, /* tp_methods */
2883 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00002884};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002885
2886static PyObject *
2887list_reversed(PyListObject *seq, PyObject *unused)
2888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2892 if (it == NULL)
2893 return NULL;
2894 assert(PyList_Check(seq));
2895 it->it_index = PyList_GET_SIZE(seq) - 1;
2896 Py_INCREF(seq);
2897 it->it_seq = seq;
2898 PyObject_GC_Track(it);
2899 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002900}
2901
2902static void
2903listreviter_dealloc(listreviterobject *it)
2904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 PyObject_GC_UnTrack(it);
2906 Py_XDECREF(it->it_seq);
2907 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002908}
2909
2910static int
2911listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 Py_VISIT(it->it_seq);
2914 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002915}
2916
2917static PyObject *
2918listreviter_next(listreviterobject *it)
2919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 PyObject *item;
2921 Py_ssize_t index = it->it_index;
2922 PyListObject *seq = it->it_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2925 item = PyList_GET_ITEM(seq, index);
2926 it->it_index--;
2927 Py_INCREF(item);
2928 return item;
2929 }
2930 it->it_index = -1;
2931 if (seq != NULL) {
2932 it->it_seq = NULL;
2933 Py_DECREF(seq);
2934 }
2935 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002936}
2937
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002938static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002939listreviter_len(listreviterobject *it)
2940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 Py_ssize_t len = it->it_index + 1;
2942 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2943 len = 0;
2944 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002945}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002946
2947static PyObject *
2948listreviter_reduce(listreviterobject *it)
2949{
2950 return listiter_reduce_general(it, 0);
2951}
2952
2953static PyObject *
2954listreviter_setstate(listreviterobject *it, PyObject *state)
2955{
2956 Py_ssize_t index = PyLong_AsSsize_t(state);
2957 if (index == -1 && PyErr_Occurred())
2958 return NULL;
2959 if (it->it_seq != NULL) {
2960 if (index < -1)
2961 index = -1;
2962 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
2963 index = PyList_GET_SIZE(it->it_seq) - 1;
2964 it->it_index = index;
2965 }
2966 Py_RETURN_NONE;
2967}
2968
2969/* common pickling support */
2970
2971static PyObject *
2972listiter_reduce_general(void *_it, int forward)
2973{
2974 PyObject *list;
2975
2976 /* the objects are not the same, index is of different types! */
2977 if (forward) {
2978 listiterobject *it = (listiterobject *)_it;
2979 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02002980 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002981 it->it_seq, it->it_index);
2982 } else {
2983 listreviterobject *it = (listreviterobject *)_it;
2984 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +02002985 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("reversed"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002986 it->it_seq, it->it_index);
2987 }
2988 /* empty iterator, create an empty list */
2989 list = PyList_New(0);
2990 if (list == NULL)
2991 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02002992 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002993}