blob: 45666fddfa9653d52e6a93c08b9dc68837fefbdd [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;
Victor Stinnerb8fb1972013-11-18 22:15:44 +0100362 /* "[" + "1" + ", 2" * (len - 1) + "]" */
363 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +0000364
Victor Stinner5c733472013-11-18 21:11:57 +0100365 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200366 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 /* Do repr() on each element. Note that this may mutate the list,
369 so must refetch the list size on each iteration. */
370 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100371 if (i > 0) {
372 if (_PyUnicodeWriter_WriteStr(&writer, sep) < 0)
373 goto error;
374 }
375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 if (Py_EnterRecursiveCall(" while getting the repr of a list"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200377 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 s = PyObject_Repr(v->ob_item[i]);
379 Py_LeaveRecursiveCall();
Victor Stinner5c733472013-11-18 21:11:57 +0100380 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200381 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100382
383 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
384 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200385 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100386 }
387 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 }
Victor Stinner5c733472013-11-18 21:11:57 +0100389
Victor Stinner4d3f1092013-11-19 12:09:00 +0100390 writer.overallocate = 0;
Victor Stinner5c733472013-11-18 21:11:57 +0100391 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200392 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100395 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200396
397error:
Victor Stinner5c733472013-11-18 21:11:57 +0100398 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200399 Py_ReprLeave((PyObject *)v);
400 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401}
402
Martin v. Löwis18e16552006-02-15 17:27:45 +0000403static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000404list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407}
408
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000409static int
Fred Drakea2f55112000-07-09 15:16:51 +0000410list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 Py_ssize_t i;
413 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
416 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
417 Py_EQ);
418 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000419}
420
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000421static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000422list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (i < 0 || i >= Py_SIZE(a)) {
425 if (indexerr == NULL) {
426 indexerr = PyUnicode_FromString(
427 "list index out of range");
428 if (indexerr == NULL)
429 return NULL;
430 }
431 PyErr_SetObject(PyExc_IndexError, indexerr);
432 return NULL;
433 }
434 Py_INCREF(a->ob_item[i]);
435 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436}
437
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000439list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 PyListObject *np;
442 PyObject **src, **dest;
443 Py_ssize_t i, len;
444 if (ilow < 0)
445 ilow = 0;
446 else if (ilow > Py_SIZE(a))
447 ilow = Py_SIZE(a);
448 if (ihigh < ilow)
449 ihigh = ilow;
450 else if (ihigh > Py_SIZE(a))
451 ihigh = Py_SIZE(a);
452 len = ihigh - ilow;
453 np = (PyListObject *) PyList_New(len);
454 if (np == NULL)
455 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 src = a->ob_item + ilow;
458 dest = np->ob_item;
459 for (i = 0; i < len; i++) {
460 PyObject *v = src[i];
461 Py_INCREF(v);
462 dest[i] = v;
463 }
464 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465}
466
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000467PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000468PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 if (!PyList_Check(a)) {
471 PyErr_BadInternalCall();
472 return NULL;
473 }
474 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000475}
476
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000478list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 Py_ssize_t size;
481 Py_ssize_t i;
482 PyObject **src, **dest;
483 PyListObject *np;
484 if (!PyList_Check(bb)) {
485 PyErr_Format(PyExc_TypeError,
486 "can only concatenate list (not \"%.200s\") to list",
487 bb->ob_type->tp_name);
488 return NULL;
489 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000490#define b ((PyListObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 size = Py_SIZE(a) + Py_SIZE(b);
492 if (size < 0)
493 return PyErr_NoMemory();
494 np = (PyListObject *) PyList_New(size);
495 if (np == NULL) {
496 return NULL;
497 }
498 src = a->ob_item;
499 dest = np->ob_item;
500 for (i = 0; i < Py_SIZE(a); i++) {
501 PyObject *v = src[i];
502 Py_INCREF(v);
503 dest[i] = v;
504 }
505 src = b->ob_item;
506 dest = np->ob_item + Py_SIZE(a);
507 for (i = 0; i < Py_SIZE(b); i++) {
508 PyObject *v = src[i];
509 Py_INCREF(v);
510 dest[i] = v;
511 }
512 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000513#undef b
514}
515
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000516static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000517list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 Py_ssize_t i, j;
520 Py_ssize_t size;
521 PyListObject *np;
522 PyObject **p, **items;
523 PyObject *elem;
524 if (n < 0)
525 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100526 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100528 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 if (size == 0)
530 return PyList_New(0);
531 np = (PyListObject *) PyList_New(size);
532 if (np == NULL)
533 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 items = np->ob_item;
536 if (Py_SIZE(a) == 1) {
537 elem = a->ob_item[0];
538 for (i = 0; i < n; i++) {
539 items[i] = elem;
540 Py_INCREF(elem);
541 }
542 return (PyObject *) np;
543 }
544 p = np->ob_item;
545 items = a->ob_item;
546 for (i = 0; i < n; i++) {
547 for (j = 0; j < Py_SIZE(a); j++) {
548 *p = items[j];
549 Py_INCREF(*p);
550 p++;
551 }
552 }
553 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000554}
555
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000556static int
Armin Rigo93677f02004-07-29 12:40:23 +0000557list_clear(PyListObject *a)
558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 Py_ssize_t i;
560 PyObject **item = a->ob_item;
561 if (item != NULL) {
562 /* Because XDECREF can recursively invoke operations on
563 this list, we make it empty first. */
564 i = Py_SIZE(a);
565 Py_SIZE(a) = 0;
566 a->ob_item = NULL;
567 a->allocated = 0;
568 while (--i >= 0) {
569 Py_XDECREF(item[i]);
570 }
571 PyMem_FREE(item);
572 }
573 /* Never fails; the return value can be ignored.
574 Note that there is no guarantee that the list is actually empty
575 at this point, because XDECREF may have populated it again! */
576 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000577}
578
Tim Peters8fc4a912004-07-31 21:53:19 +0000579/* a[ilow:ihigh] = v if v != NULL.
580 * del a[ilow:ihigh] if v == NULL.
581 *
582 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
583 * guaranteed the call cannot fail.
584 */
Armin Rigo93677f02004-07-29 12:40:23 +0000585static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000586list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 /* Because [X]DECREF can recursively invoke list operations on
589 this list, we must postpone all [X]DECREF activity until
590 after the list is back in its canonical shape. Therefore
591 we must allocate an additional array, 'recycle', into which
592 we temporarily copy the items that are deleted from the
593 list. :-( */
594 PyObject *recycle_on_stack[8];
595 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
596 PyObject **item;
597 PyObject **vitem = NULL;
598 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
599 Py_ssize_t n; /* # of elements in replacement list */
600 Py_ssize_t norig; /* # of elements in list getting replaced */
601 Py_ssize_t d; /* Change in size */
602 Py_ssize_t k;
603 size_t s;
604 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000605#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 if (v == NULL)
607 n = 0;
608 else {
609 if (a == b) {
610 /* Special case "a[i:j] = a" -- copy b first */
611 v = list_slice(b, 0, Py_SIZE(b));
612 if (v == NULL)
613 return result;
614 result = list_ass_slice(a, ilow, ihigh, v);
615 Py_DECREF(v);
616 return result;
617 }
618 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
619 if(v_as_SF == NULL)
620 goto Error;
621 n = PySequence_Fast_GET_SIZE(v_as_SF);
622 vitem = PySequence_Fast_ITEMS(v_as_SF);
623 }
624 if (ilow < 0)
625 ilow = 0;
626 else if (ilow > Py_SIZE(a))
627 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 if (ihigh < ilow)
630 ihigh = ilow;
631 else if (ihigh > Py_SIZE(a))
632 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 norig = ihigh - ilow;
635 assert(norig >= 0);
636 d = n - norig;
637 if (Py_SIZE(a) + d == 0) {
638 Py_XDECREF(v_as_SF);
639 return list_clear(a);
640 }
641 item = a->ob_item;
642 /* recycle the items that we are about to remove */
643 s = norig * sizeof(PyObject *);
644 if (s > sizeof(recycle_on_stack)) {
645 recycle = (PyObject **)PyMem_MALLOC(s);
646 if (recycle == NULL) {
647 PyErr_NoMemory();
648 goto Error;
649 }
650 }
651 memcpy(recycle, &item[ilow], s);
Tim Peters8d9eb102004-07-31 02:24:20 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200654 Py_ssize_t tail;
655 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
656 memmove(&item[ihigh+d], &item[ihigh], tail);
657 if (list_resize(a, Py_SIZE(a) + d) < 0) {
658 memmove(&item[ihigh], &item[ihigh+d], tail);
659 memcpy(&item[ilow], recycle, s);
660 goto Error;
661 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 item = a->ob_item;
663 }
664 else if (d > 0) { /* Insert d items */
665 k = Py_SIZE(a);
666 if (list_resize(a, k+d) < 0)
667 goto Error;
668 item = a->ob_item;
669 memmove(&item[ihigh+d], &item[ihigh],
670 (k - ihigh)*sizeof(PyObject *));
671 }
672 for (k = 0; k < n; k++, ilow++) {
673 PyObject *w = vitem[k];
674 Py_XINCREF(w);
675 item[ilow] = w;
676 }
677 for (k = norig - 1; k >= 0; --k)
678 Py_XDECREF(recycle[k]);
679 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000680 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 if (recycle != recycle_on_stack)
682 PyMem_FREE(recycle);
683 Py_XDECREF(v_as_SF);
684 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000685#undef b
686}
687
Guido van Rossum234f9421993-06-17 12:35:49 +0000688int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000689PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 if (!PyList_Check(a)) {
692 PyErr_BadInternalCall();
693 return -1;
694 }
695 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000696}
697
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000698static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000699list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 PyObject **items;
702 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000703
704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 size = PyList_GET_SIZE(self);
706 if (size == 0 || n == 1) {
707 Py_INCREF(self);
708 return (PyObject *)self;
709 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 if (n < 1) {
712 (void)list_clear(self);
713 Py_INCREF(self);
714 return (PyObject *)self;
715 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 if (size > PY_SSIZE_T_MAX / n) {
718 return PyErr_NoMemory();
719 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 if (list_resize(self, size*n) == -1)
722 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 p = size;
725 items = self->ob_item;
726 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
727 for (j = 0; j < size; j++) {
728 PyObject *o = items[j];
729 Py_INCREF(o);
730 items[p++] = o;
731 }
732 }
733 Py_INCREF(self);
734 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000735}
736
Guido van Rossum4a450d01991-04-03 19:05:18 +0000737static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000738list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 PyObject *old_value;
741 if (i < 0 || i >= Py_SIZE(a)) {
742 PyErr_SetString(PyExc_IndexError,
743 "list assignment index out of range");
744 return -1;
745 }
746 if (v == NULL)
747 return list_ass_slice(a, i, i+1, v);
748 Py_INCREF(v);
749 old_value = a->ob_item[i];
750 a->ob_item[i] = v;
751 Py_DECREF(old_value);
752 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000753}
754
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000755static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000756listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 Py_ssize_t i;
759 PyObject *v;
760 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
761 return NULL;
762 if (ins1(self, i, v) == 0)
763 Py_RETURN_NONE;
764 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000765}
766
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000767static PyObject *
Eli Benderskycbbaa962011-02-25 05:47:53 +0000768listclear(PyListObject *self)
769{
770 list_clear(self);
771 Py_RETURN_NONE;
772}
773
774static PyObject *
775listcopy(PyListObject *self)
776{
777 return list_slice(self, 0, Py_SIZE(self));
778}
779
780static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000781listappend(PyListObject *self, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 if (app1(self, v) == 0)
784 Py_RETURN_NONE;
785 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000786}
787
Barry Warsawdedf6d61998-10-09 16:37:25 +0000788static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000789listextend(PyListObject *self, PyObject *b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 PyObject *it; /* iter(v) */
792 Py_ssize_t m; /* size of self */
793 Py_ssize_t n; /* guess for size of b */
794 Py_ssize_t mn; /* m + n */
795 Py_ssize_t i;
796 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 /* Special cases:
799 1) lists and tuples which can use PySequence_Fast ops
800 2) extending self to self requires making a copy first
801 */
802 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
803 PyObject **src, **dest;
804 b = PySequence_Fast(b, "argument must be iterable");
805 if (!b)
806 return NULL;
807 n = PySequence_Fast_GET_SIZE(b);
808 if (n == 0) {
809 /* short circuit when b is empty */
810 Py_DECREF(b);
811 Py_RETURN_NONE;
812 }
813 m = Py_SIZE(self);
814 if (list_resize(self, m + n) == -1) {
815 Py_DECREF(b);
816 return NULL;
817 }
818 /* note that we may still have self == b here for the
819 * situation a.extend(a), but the following code works
820 * in that case too. Just make sure to resize self
821 * before calling PySequence_Fast_ITEMS.
822 */
823 /* populate the end of self with b's items */
824 src = PySequence_Fast_ITEMS(b);
825 dest = self->ob_item + m;
826 for (i = 0; i < n; i++) {
827 PyObject *o = src[i];
828 Py_INCREF(o);
829 dest[i] = o;
830 }
831 Py_DECREF(b);
832 Py_RETURN_NONE;
833 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 it = PyObject_GetIter(b);
836 if (it == NULL)
837 return NULL;
838 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 /* Guess a result list size. */
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200841 n = PyObject_LengthHint(b, 8);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 if (n == -1) {
843 Py_DECREF(it);
844 return NULL;
845 }
846 m = Py_SIZE(self);
847 mn = m + n;
848 if (mn >= m) {
849 /* Make room. */
850 if (list_resize(self, mn) == -1)
851 goto error;
852 /* Make the list sane again. */
853 Py_SIZE(self) = m;
854 }
855 /* Else m + n overflowed; on the chance that n lied, and there really
856 * is enough room, ignore it. If n was telling the truth, we'll
857 * eventually run out of memory during the loop.
858 */
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 /* Run iterator to exhaustion. */
861 for (;;) {
862 PyObject *item = iternext(it);
863 if (item == NULL) {
864 if (PyErr_Occurred()) {
865 if (PyErr_ExceptionMatches(PyExc_StopIteration))
866 PyErr_Clear();
867 else
868 goto error;
869 }
870 break;
871 }
872 if (Py_SIZE(self) < self->allocated) {
873 /* steals ref */
874 PyList_SET_ITEM(self, Py_SIZE(self), item);
875 ++Py_SIZE(self);
876 }
877 else {
878 int status = app1(self, item);
879 Py_DECREF(item); /* append creates a new ref */
880 if (status < 0)
881 goto error;
882 }
883 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200886 if (Py_SIZE(self) < self->allocated) {
887 if (list_resize(self, Py_SIZE(self)) < 0)
888 goto error;
889 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 Py_DECREF(it);
892 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000893
894 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 Py_DECREF(it);
896 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000897}
898
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000899PyObject *
900_PyList_Extend(PyListObject *self, PyObject *b)
901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 return listextend(self, b);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000903}
904
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000905static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000906list_inplace_concat(PyListObject *self, PyObject *other)
907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 result = listextend(self, other);
911 if (result == NULL)
912 return result;
913 Py_DECREF(result);
914 Py_INCREF(self);
915 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000916}
917
918static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000919listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 Py_ssize_t i = -1;
922 PyObject *v;
923 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if (!PyArg_ParseTuple(args, "|n:pop", &i))
926 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 if (Py_SIZE(self) == 0) {
929 /* Special-case most common failure cause */
930 PyErr_SetString(PyExc_IndexError, "pop from empty list");
931 return NULL;
932 }
933 if (i < 0)
934 i += Py_SIZE(self);
935 if (i < 0 || i >= Py_SIZE(self)) {
936 PyErr_SetString(PyExc_IndexError, "pop index out of range");
937 return NULL;
938 }
939 v = self->ob_item[i];
940 if (i == Py_SIZE(self) - 1) {
941 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +0200942 if (status >= 0)
943 return v; /* and v now owns the reference the list had */
944 else
945 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 }
947 Py_INCREF(v);
948 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +0200949 if (status < 0) {
950 Py_DECREF(v);
951 return NULL;
952 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000954}
955
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000956/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
957static void
958reverse_slice(PyObject **lo, PyObject **hi)
959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 --hi;
963 while (lo < hi) {
964 PyObject *t = *lo;
965 *lo = *hi;
966 *hi = t;
967 ++lo;
968 --hi;
969 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000970}
971
Tim Petersa64dc242002-08-01 02:13:36 +0000972/* Lots of code for an adaptive, stable, natural mergesort. There are many
973 * pieces to this algorithm; read listsort.txt for overviews and details.
974 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000975
Daniel Stutzbach98338222010-12-02 21:55:33 +0000976/* A sortslice contains a pointer to an array of keys and a pointer to
977 * an array of corresponding values. In other words, keys[i]
978 * corresponds with values[i]. If values == NULL, then the keys are
979 * also the values.
980 *
981 * Several convenience routines are provided here, so that keys and
982 * values are always moved in sync.
983 */
984
985typedef struct {
986 PyObject **keys;
987 PyObject **values;
988} sortslice;
989
990Py_LOCAL_INLINE(void)
991sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
992{
993 s1->keys[i] = s2->keys[j];
994 if (s1->values != NULL)
995 s1->values[i] = s2->values[j];
996}
997
998Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000999sortslice_copy_incr(sortslice *dst, sortslice *src)
1000{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001001 *dst->keys++ = *src->keys++;
1002 if (dst->values != NULL)
1003 *dst->values++ = *src->values++;
1004}
1005
1006Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001007sortslice_copy_decr(sortslice *dst, sortslice *src)
1008{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001009 *dst->keys-- = *src->keys--;
1010 if (dst->values != NULL)
1011 *dst->values-- = *src->values--;
1012}
1013
1014
1015Py_LOCAL_INLINE(void)
1016sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001017 Py_ssize_t n)
1018{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001019 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1020 if (s1->values != NULL)
1021 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1022}
1023
1024Py_LOCAL_INLINE(void)
1025sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001026 Py_ssize_t n)
1027{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001028 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1029 if (s1->values != NULL)
1030 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1031}
1032
1033Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001034sortslice_advance(sortslice *slice, Py_ssize_t n)
1035{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001036 slice->keys += n;
1037 if (slice->values != NULL)
1038 slice->values += n;
1039}
1040
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001041/* Comparison function: PyObject_RichCompareBool with Py_LT.
Tim Petersa64dc242002-08-01 02:13:36 +00001042 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1043 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001044
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001045#define ISLT(X, Y) (PyObject_RichCompareBool(X, Y, Py_LT))
Tim Peters66860f62002-08-04 17:47:26 +00001046
1047/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001048 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1049 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1050*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001051#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001053
1054/* binarysort is the best method for sorting small arrays: it does
1055 few compares, but can do data movement quadratic in the number of
1056 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001057 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001058 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001059 On entry, must have lo <= start <= hi, and that [lo, start) is already
1060 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001061 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001062 Even in case of error, the output slice will be some permutation of
1063 the input (nothing is lost or duplicated).
1064*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001065static int
Daniel Stutzbach98338222010-12-02 21:55:33 +00001066binarysort(sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001067{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001068 Py_ssize_t k;
1069 PyObject **l, **p, **r;
1070 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001071
Daniel Stutzbach98338222010-12-02 21:55:33 +00001072 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001074 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 ++start;
1076 for (; start < hi; ++start) {
1077 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001078 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 r = start;
1080 pivot = *r;
1081 /* Invariants:
1082 * pivot >= all in [lo, l).
1083 * pivot < all in [r, start).
1084 * The second is vacuously true at the start.
1085 */
1086 assert(l < r);
1087 do {
1088 p = l + ((r - l) >> 1);
1089 IFLT(pivot, *p)
1090 r = p;
1091 else
1092 l = p+1;
1093 } while (l < r);
1094 assert(l == r);
1095 /* The invariants still hold, so pivot >= all in [lo, l) and
1096 pivot < all in [l, start), so pivot belongs at l. Note
1097 that if there are elements equal to pivot, l points to the
1098 first slot after them -- that's why this sort is stable.
1099 Slide over to make room.
1100 Caution: using memmove is much slower under MSVC 5;
1101 we're not usually moving many slots. */
1102 for (p = start; p > l; --p)
1103 *p = *(p-1);
1104 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001105 if (lo.values != NULL) {
1106 Py_ssize_t offset = lo.values - lo.keys;
1107 p = start + offset;
1108 pivot = *p;
1109 l += offset;
1110 for (p = start + offset; p > l; --p)
1111 *p = *(p-1);
1112 *l = pivot;
1113 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 }
1115 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001116
1117 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001119}
1120
Tim Petersa64dc242002-08-01 02:13:36 +00001121/*
1122Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1123is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001124
Tim Petersa64dc242002-08-01 02:13:36 +00001125 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001126
Tim Petersa64dc242002-08-01 02:13:36 +00001127or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001128
Tim Petersa64dc242002-08-01 02:13:36 +00001129 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001130
Tim Petersa64dc242002-08-01 02:13:36 +00001131Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1132For its intended use in a stable mergesort, the strictness of the defn of
1133"descending" is needed so that the caller can safely reverse a descending
1134sequence without violating stability (strict > ensures there are no equal
1135elements to get out of order).
1136
1137Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001138*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001139static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001140count_run(PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 Py_ssize_t k;
1143 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 assert(lo < hi);
1146 *descending = 0;
1147 ++lo;
1148 if (lo == hi)
1149 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 n = 2;
1152 IFLT(*lo, *(lo-1)) {
1153 *descending = 1;
1154 for (lo = lo+1; lo < hi; ++lo, ++n) {
1155 IFLT(*lo, *(lo-1))
1156 ;
1157 else
1158 break;
1159 }
1160 }
1161 else {
1162 for (lo = lo+1; lo < hi; ++lo, ++n) {
1163 IFLT(*lo, *(lo-1))
1164 break;
1165 }
1166 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001169fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001171}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001172
Tim Petersa64dc242002-08-01 02:13:36 +00001173/*
1174Locate the proper position of key in a sorted vector; if the vector contains
1175an element equal to key, return the position immediately to the left of
1176the leftmost equal element. [gallop_right() does the same except returns
1177the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001178
Tim Petersa64dc242002-08-01 02:13:36 +00001179"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001180
Tim Petersa64dc242002-08-01 02:13:36 +00001181"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1182hint is to the final result, the faster this runs.
1183
1184The return value is the int k in 0..n such that
1185
1186 a[k-1] < key <= a[k]
1187
1188pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1189key belongs at index k; or, IOW, the first k elements of a should precede
1190key, and the last n-k should follow key.
1191
1192Returns -1 on error. See listsort.txt for info on the method.
1193*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001194static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001195gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 Py_ssize_t ofs;
1198 Py_ssize_t lastofs;
1199 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 a += hint;
1204 lastofs = 0;
1205 ofs = 1;
1206 IFLT(*a, key) {
1207 /* a[hint] < key -- gallop right, until
1208 * a[hint + lastofs] < key <= a[hint + ofs]
1209 */
1210 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1211 while (ofs < maxofs) {
1212 IFLT(a[ofs], key) {
1213 lastofs = ofs;
1214 ofs = (ofs << 1) + 1;
1215 if (ofs <= 0) /* int overflow */
1216 ofs = maxofs;
1217 }
1218 else /* key <= a[hint + ofs] */
1219 break;
1220 }
1221 if (ofs > maxofs)
1222 ofs = maxofs;
1223 /* Translate back to offsets relative to &a[0]. */
1224 lastofs += hint;
1225 ofs += hint;
1226 }
1227 else {
1228 /* key <= a[hint] -- gallop left, until
1229 * a[hint - ofs] < key <= a[hint - lastofs]
1230 */
1231 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1232 while (ofs < maxofs) {
1233 IFLT(*(a-ofs), key)
1234 break;
1235 /* key <= a[hint - ofs] */
1236 lastofs = ofs;
1237 ofs = (ofs << 1) + 1;
1238 if (ofs <= 0) /* int overflow */
1239 ofs = maxofs;
1240 }
1241 if (ofs > maxofs)
1242 ofs = maxofs;
1243 /* Translate back to positive offsets relative to &a[0]. */
1244 k = lastofs;
1245 lastofs = hint - ofs;
1246 ofs = hint - k;
1247 }
1248 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1251 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1252 * right of lastofs but no farther right than ofs. Do a binary
1253 * search, with invariant a[lastofs-1] < key <= a[ofs].
1254 */
1255 ++lastofs;
1256 while (lastofs < ofs) {
1257 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 IFLT(a[m], key)
1260 lastofs = m+1; /* a[m] < key */
1261 else
1262 ofs = m; /* key <= a[m] */
1263 }
1264 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1265 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001266
1267fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001269}
1270
1271/*
1272Exactly like gallop_left(), except that if key already exists in a[0:n],
1273finds the position immediately to the right of the rightmost equal value.
1274
1275The return value is the int k in 0..n such that
1276
1277 a[k-1] <= key < a[k]
1278
1279or -1 if error.
1280
1281The code duplication is massive, but this is enough different given that
1282we're sticking to "<" comparisons that it's much harder to follow if
1283written as one routine with yet another "left or right?" flag.
1284*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001285static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001286gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 Py_ssize_t ofs;
1289 Py_ssize_t lastofs;
1290 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 a += hint;
1295 lastofs = 0;
1296 ofs = 1;
1297 IFLT(key, *a) {
1298 /* key < a[hint] -- gallop left, until
1299 * a[hint - ofs] <= key < a[hint - lastofs]
1300 */
1301 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1302 while (ofs < maxofs) {
1303 IFLT(key, *(a-ofs)) {
1304 lastofs = ofs;
1305 ofs = (ofs << 1) + 1;
1306 if (ofs <= 0) /* int overflow */
1307 ofs = maxofs;
1308 }
1309 else /* a[hint - ofs] <= key */
1310 break;
1311 }
1312 if (ofs > maxofs)
1313 ofs = maxofs;
1314 /* Translate back to positive offsets relative to &a[0]. */
1315 k = lastofs;
1316 lastofs = hint - ofs;
1317 ofs = hint - k;
1318 }
1319 else {
1320 /* a[hint] <= key -- gallop right, until
1321 * a[hint + lastofs] <= key < a[hint + ofs]
1322 */
1323 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1324 while (ofs < maxofs) {
1325 IFLT(key, a[ofs])
1326 break;
1327 /* a[hint + ofs] <= key */
1328 lastofs = ofs;
1329 ofs = (ofs << 1) + 1;
1330 if (ofs <= 0) /* int overflow */
1331 ofs = maxofs;
1332 }
1333 if (ofs > maxofs)
1334 ofs = maxofs;
1335 /* Translate back to offsets relative to &a[0]. */
1336 lastofs += hint;
1337 ofs += hint;
1338 }
1339 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1342 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1343 * right of lastofs but no farther right than ofs. Do a binary
1344 * search, with invariant a[lastofs-1] <= key < a[ofs].
1345 */
1346 ++lastofs;
1347 while (lastofs < ofs) {
1348 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 IFLT(key, a[m])
1351 ofs = m; /* key < a[m] */
1352 else
1353 lastofs = m+1; /* a[m] <= key */
1354 }
1355 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1356 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001357
1358fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001360}
1361
1362/* The maximum number of entries in a MergeState's pending-runs stack.
1363 * This is enough to sort arrays of size up to about
1364 * 32 * phi ** MAX_MERGE_PENDING
1365 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1366 * with 2**64 elements.
1367 */
1368#define MAX_MERGE_PENDING 85
1369
Tim Peterse05f65a2002-08-10 05:21:15 +00001370/* When we get into galloping mode, we stay there until both runs win less
1371 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
Tim Petersa64dc242002-08-01 02:13:36 +00001372 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001373#define MIN_GALLOP 7
Tim Petersa64dc242002-08-01 02:13:36 +00001374
1375/* Avoid malloc for small temp arrays. */
1376#define MERGESTATE_TEMP_SIZE 256
1377
1378/* One MergeState exists on the stack per invocation of mergesort. It's just
1379 * a convenient way to pass state around among the helper functions.
1380 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001381struct s_slice {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001382 sortslice base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 Py_ssize_t len;
Tim Peterse05f65a2002-08-10 05:21:15 +00001384};
1385
Tim Petersa64dc242002-08-01 02:13:36 +00001386typedef struct s_MergeState {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 /* This controls when we get *into* galloping mode. It's initialized
1388 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1389 * random data, and lower for highly structured data.
1390 */
1391 Py_ssize_t min_gallop;
Tim Peterse05f65a2002-08-10 05:21:15 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 /* 'a' is temp storage to help with merges. It contains room for
1394 * alloced entries.
1395 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001396 sortslice a; /* may point to temparray below */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 Py_ssize_t alloced;
Tim Petersa64dc242002-08-01 02:13:36 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 /* A stack of n pending runs yet to be merged. Run #i starts at
1400 * address base[i] and extends for len[i] elements. It's always
1401 * true (so long as the indices are in bounds) that
1402 *
1403 * pending[i].base + pending[i].len == pending[i+1].base
1404 *
1405 * so we could cut the storage for this, but it's a minor amount,
1406 * and keeping all the info explicit simplifies the code.
1407 */
1408 int n;
1409 struct s_slice pending[MAX_MERGE_PENDING];
Tim Petersa64dc242002-08-01 02:13:36 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 /* 'a' points to this when possible, rather than muck with malloc. */
1412 PyObject *temparray[MERGESTATE_TEMP_SIZE];
Tim Petersa64dc242002-08-01 02:13:36 +00001413} MergeState;
1414
1415/* Conceptually a MergeState's constructor. */
1416static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001417merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001420 if (has_keyfunc) {
1421 /* The temporary space for merging will need at most half the list
1422 * size rounded up. Use the minimum possible space so we can use the
1423 * rest of temparray for other things. In particular, if there is
1424 * enough extra space, listsort() will use it to store the keys.
1425 */
1426 ms->alloced = (list_size + 1) / 2;
1427
1428 /* ms->alloced describes how many keys will be stored at
1429 ms->temparray, but we also need to store the values. Hence,
1430 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1431 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1432 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1433 ms->a.values = &ms->temparray[ms->alloced];
1434 }
1435 else {
1436 ms->alloced = MERGESTATE_TEMP_SIZE;
1437 ms->a.values = NULL;
1438 }
1439 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 ms->n = 0;
1441 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001442}
1443
1444/* Free all the temp memory owned by the MergeState. This must be called
1445 * when you're done with a MergeState, and may be called before then if
1446 * you want to free the temp memory early.
1447 */
1448static void
1449merge_freemem(MergeState *ms)
1450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001452 if (ms->a.keys != ms->temparray)
1453 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001454}
1455
1456/* Ensure enough temp memory for 'need' array slots is available.
1457 * Returns 0 on success and -1 if the memory can't be gotten.
1458 */
1459static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001460merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001461{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001462 int multiplier;
1463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 assert(ms != NULL);
1465 if (need <= ms->alloced)
1466 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001467
1468 multiplier = ms->a.values != NULL ? 2 : 1;
1469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 /* Don't realloc! That can cost cycles to copy the old data, but
1471 * we don't care what's in the block.
1472 */
1473 merge_freemem(ms);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001474 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 PyErr_NoMemory();
1476 return -1;
1477 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001478 ms->a.keys = (PyObject**)PyMem_Malloc(multiplier * need
1479 * sizeof(PyObject *));
1480 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001482 if (ms->a.values != NULL)
1483 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 return 0;
1485 }
1486 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001488}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1490 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001491
Daniel Stutzbach98338222010-12-02 21:55:33 +00001492/* Merge the na elements starting at ssa with the nb elements starting at
1493 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1494 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1495 * should have na <= nb. See listsort.txt for more info. Return 0 if
1496 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001497 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001498static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001499merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1500 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001503 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 int result = -1; /* guilty until proved innocent */
1505 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001506
Daniel Stutzbach98338222010-12-02 21:55:33 +00001507 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1508 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 if (MERGE_GETMEM(ms, na) < 0)
1510 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001511 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1512 dest = ssa;
1513 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001514
Daniel Stutzbach98338222010-12-02 21:55:33 +00001515 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 --nb;
1517 if (nb == 0)
1518 goto Succeed;
1519 if (na == 1)
1520 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 min_gallop = ms->min_gallop;
1523 for (;;) {
1524 Py_ssize_t acount = 0; /* # of times A won in a row */
1525 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 /* Do the straightforward thing until (if ever) one run
1528 * appears to win consistently.
1529 */
1530 for (;;) {
1531 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001532 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 if (k) {
1534 if (k < 0)
1535 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001536 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 ++bcount;
1538 acount = 0;
1539 --nb;
1540 if (nb == 0)
1541 goto Succeed;
1542 if (bcount >= min_gallop)
1543 break;
1544 }
1545 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001546 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 ++acount;
1548 bcount = 0;
1549 --na;
1550 if (na == 1)
1551 goto CopyB;
1552 if (acount >= min_gallop)
1553 break;
1554 }
1555 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 /* One run is winning so consistently that galloping may
1558 * be a huge win. So try that, and continue galloping until
1559 * (if ever) neither run appears to be winning consistently
1560 * anymore.
1561 */
1562 ++min_gallop;
1563 do {
1564 assert(na > 1 && nb > 0);
1565 min_gallop -= min_gallop > 1;
1566 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001567 k = gallop_right(ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 acount = k;
1569 if (k) {
1570 if (k < 0)
1571 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001572 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1573 sortslice_advance(&dest, k);
1574 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 na -= k;
1576 if (na == 1)
1577 goto CopyB;
1578 /* na==0 is impossible now if the comparison
1579 * function is consistent, but we can't assume
1580 * that it is.
1581 */
1582 if (na == 0)
1583 goto Succeed;
1584 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001585 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 --nb;
1587 if (nb == 0)
1588 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001589
Daniel Stutzbach98338222010-12-02 21:55:33 +00001590 k = gallop_left(ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 bcount = k;
1592 if (k) {
1593 if (k < 0)
1594 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001595 sortslice_memmove(&dest, 0, &ssb, 0, k);
1596 sortslice_advance(&dest, k);
1597 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 nb -= k;
1599 if (nb == 0)
1600 goto Succeed;
1601 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001602 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 --na;
1604 if (na == 1)
1605 goto CopyB;
1606 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1607 ++min_gallop; /* penalize it for leaving galloping mode */
1608 ms->min_gallop = min_gallop;
1609 }
Tim Petersa64dc242002-08-01 02:13:36 +00001610Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001612Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001614 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001616CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001618 /* The last element of ssa belongs at the end of the merge. */
1619 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1620 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001622}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001623
Daniel Stutzbach98338222010-12-02 21:55:33 +00001624/* Merge the na elements starting at pa with the nb elements starting at
1625 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1626 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1627 * should have na >= nb. See listsort.txt for more info. Return 0 if
1628 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001629 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001630static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001631merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1632 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001635 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001638
Daniel Stutzbach98338222010-12-02 21:55:33 +00001639 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1640 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 if (MERGE_GETMEM(ms, nb) < 0)
1642 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001643 dest = ssb;
1644 sortslice_advance(&dest, nb-1);
1645 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1646 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001648 ssb.keys = ms->a.keys + nb - 1;
1649 if (ssb.values != NULL)
1650 ssb.values = ms->a.values + nb - 1;
1651 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001652
Daniel Stutzbach98338222010-12-02 21:55:33 +00001653 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 --na;
1655 if (na == 0)
1656 goto Succeed;
1657 if (nb == 1)
1658 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 min_gallop = ms->min_gallop;
1661 for (;;) {
1662 Py_ssize_t acount = 0; /* # of times A won in a row */
1663 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 /* Do the straightforward thing until (if ever) one run
1666 * appears to win consistently.
1667 */
1668 for (;;) {
1669 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001670 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 if (k) {
1672 if (k < 0)
1673 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001674 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 ++acount;
1676 bcount = 0;
1677 --na;
1678 if (na == 0)
1679 goto Succeed;
1680 if (acount >= min_gallop)
1681 break;
1682 }
1683 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001684 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 ++bcount;
1686 acount = 0;
1687 --nb;
1688 if (nb == 1)
1689 goto CopyA;
1690 if (bcount >= min_gallop)
1691 break;
1692 }
1693 }
Tim Petersa64dc242002-08-01 02:13:36 +00001694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 /* One run is winning so consistently that galloping may
1696 * be a huge win. So try that, and continue galloping until
1697 * (if ever) neither run appears to be winning consistently
1698 * anymore.
1699 */
1700 ++min_gallop;
1701 do {
1702 assert(na > 0 && nb > 1);
1703 min_gallop -= min_gallop > 1;
1704 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001705 k = gallop_right(ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 if (k < 0)
1707 goto Fail;
1708 k = na - k;
1709 acount = k;
1710 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001711 sortslice_advance(&dest, -k);
1712 sortslice_advance(&ssa, -k);
1713 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 na -= k;
1715 if (na == 0)
1716 goto Succeed;
1717 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001718 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 --nb;
1720 if (nb == 1)
1721 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001722
Daniel Stutzbach98338222010-12-02 21:55:33 +00001723 k = gallop_left(ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 if (k < 0)
1725 goto Fail;
1726 k = nb - k;
1727 bcount = k;
1728 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001729 sortslice_advance(&dest, -k);
1730 sortslice_advance(&ssb, -k);
1731 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 nb -= k;
1733 if (nb == 1)
1734 goto CopyA;
1735 /* nb==0 is impossible now if the comparison
1736 * function is consistent, but we can't assume
1737 * that it is.
1738 */
1739 if (nb == 0)
1740 goto Succeed;
1741 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001742 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 --na;
1744 if (na == 0)
1745 goto Succeed;
1746 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1747 ++min_gallop; /* penalize it for leaving galloping mode */
1748 ms->min_gallop = min_gallop;
1749 }
Tim Petersa64dc242002-08-01 02:13:36 +00001750Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001752Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001754 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001756CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001758 /* The first element of ssb belongs at the front of the merge. */
1759 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1760 sortslice_advance(&dest, -na);
1761 sortslice_advance(&ssa, -na);
1762 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001764}
1765
1766/* Merge the two runs at stack indices i and i+1.
1767 * Returns 0 on success, -1 on error.
1768 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001769static Py_ssize_t
1770merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001771{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001772 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 Py_ssize_t na, nb;
1774 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 assert(ms != NULL);
1777 assert(ms->n >= 2);
1778 assert(i >= 0);
1779 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001780
Daniel Stutzbach98338222010-12-02 21:55:33 +00001781 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001783 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 nb = ms->pending[i+1].len;
1785 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001786 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 /* Record the length of the combined runs; if i is the 3rd-last
1789 * run now, also slide over the last run (which isn't involved
1790 * in this merge). The current run i+1 goes away in any case.
1791 */
1792 ms->pending[i].len = na + nb;
1793 if (i == ms->n - 3)
1794 ms->pending[i+1] = ms->pending[i+2];
1795 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 /* Where does b start in a? Elements in a before that can be
1798 * ignored (already in place).
1799 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001800 k = gallop_right(*ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 if (k < 0)
1802 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001803 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 na -= k;
1805 if (na == 0)
1806 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 /* Where does a end in b? Elements in b after that can be
1809 * ignored (already in place).
1810 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001811 nb = gallop_left(ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 if (nb <= 0)
1813 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 /* Merge what remains of the runs, using a temp array with
1816 * min(na, nb) elements.
1817 */
1818 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001819 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001821 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001822}
1823
1824/* Examine the stack of runs waiting to be merged, merging adjacent runs
1825 * until the stack invariants are re-established:
1826 *
1827 * 1. len[-3] > len[-2] + len[-1]
1828 * 2. len[-2] > len[-1]
1829 *
1830 * See listsort.txt for more info.
1831 *
1832 * Returns 0 on success, -1 on error.
1833 */
1834static int
1835merge_collapse(MergeState *ms)
1836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 assert(ms);
1840 while (ms->n > 1) {
1841 Py_ssize_t n = ms->n - 2;
1842 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
1843 if (p[n-1].len < p[n+1].len)
1844 --n;
1845 if (merge_at(ms, n) < 0)
1846 return -1;
1847 }
1848 else if (p[n].len <= p[n+1].len) {
1849 if (merge_at(ms, n) < 0)
1850 return -1;
1851 }
1852 else
1853 break;
1854 }
1855 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001856}
1857
1858/* Regardless of invariants, merge all runs on the stack until only one
1859 * remains. This is used at the end of the mergesort.
1860 *
1861 * Returns 0 on success, -1 on error.
1862 */
1863static int
1864merge_force_collapse(MergeState *ms)
1865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 assert(ms);
1869 while (ms->n > 1) {
1870 Py_ssize_t n = ms->n - 2;
1871 if (n > 0 && p[n-1].len < p[n+1].len)
1872 --n;
1873 if (merge_at(ms, n) < 0)
1874 return -1;
1875 }
1876 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001877}
1878
1879/* Compute a good value for the minimum run length; natural runs shorter
1880 * than this are boosted artificially via binary insertion.
1881 *
1882 * If n < 64, return n (it's too small to bother with fancy stuff).
1883 * Else if n is an exact power of 2, return 32.
1884 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1885 * strictly less than, an exact power of 2.
1886 *
1887 * See listsort.txt for more info.
1888 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001889static Py_ssize_t
1890merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 assert(n >= 0);
1895 while (n >= 64) {
1896 r |= n & 1;
1897 n >>= 1;
1898 }
1899 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001900}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001901
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001902static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001903reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001904{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001905 reverse_slice(s->keys, &s->keys[n]);
1906 if (s->values != NULL)
1907 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001908}
1909
Tim Petersa64dc242002-08-01 02:13:36 +00001910/* An adaptive, stable, natural mergesort. See listsort.txt.
1911 * Returns Py_None on success, NULL on error. Even in case of error, the
1912 * list will be some permutation of its input state (nothing is lost or
1913 * duplicated).
1914 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001915static PyObject *
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001916listsort(PyListObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 Py_ssize_t nremaining;
1920 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001921 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 Py_ssize_t saved_ob_size, saved_allocated;
1923 PyObject **saved_ob_item;
1924 PyObject **final_ob_item;
1925 PyObject *result = NULL; /* guilty until proved innocent */
1926 int reverse = 0;
1927 PyObject *keyfunc = NULL;
1928 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 static char *kwlist[] = {"key", "reverse", 0};
Daniel Stutzbach98338222010-12-02 21:55:33 +00001930 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 assert(self != NULL);
1933 assert (PyList_Check(self));
1934 if (args != NULL) {
1935 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort",
1936 kwlist, &keyfunc, &reverse))
1937 return NULL;
1938 if (Py_SIZE(args) > 0) {
1939 PyErr_SetString(PyExc_TypeError,
1940 "must use keyword argument for key function");
1941 return NULL;
1942 }
1943 }
1944 if (keyfunc == Py_None)
1945 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 /* The list is temporarily made empty, so that mutations performed
1948 * by comparison functions can't affect the slice of memory we're
1949 * sorting (allowing mutations during sorting is a core-dump
1950 * factory, since ob_item may change).
1951 */
1952 saved_ob_size = Py_SIZE(self);
1953 saved_ob_item = self->ob_item;
1954 saved_allocated = self->allocated;
1955 Py_SIZE(self) = 0;
1956 self->ob_item = NULL;
1957 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00001958
Daniel Stutzbach98338222010-12-02 21:55:33 +00001959 if (keyfunc == NULL) {
1960 keys = NULL;
1961 lo.keys = saved_ob_item;
1962 lo.values = NULL;
1963 }
1964 else {
1965 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
1966 /* Leverage stack space we allocated but won't otherwise use */
1967 keys = &ms.temparray[saved_ob_size+1];
1968 else {
1969 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
1970 if (keys == NULL)
1971 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001973
1974 for (i = 0; i < saved_ob_size ; i++) {
1975 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
1976 NULL);
1977 if (keys[i] == NULL) {
1978 for (i=i-1 ; i>=0 ; i--)
1979 Py_DECREF(keys[i]);
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00001980 if (keys != &ms.temparray[saved_ob_size+1])
1981 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001982 goto keyfunc_fail;
1983 }
1984 }
1985
1986 lo.keys = keys;
1987 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001989
Daniel Stutzbach98338222010-12-02 21:55:33 +00001990 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 nremaining = saved_ob_size;
1993 if (nremaining < 2)
1994 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00001995
Benjamin Peterson05380642010-08-23 19:35:39 +00001996 /* Reverse sort stability achieved by initially reversing the list,
1997 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001998 if (reverse) {
1999 if (keys != NULL)
2000 reverse_slice(&keys[0], &keys[saved_ob_size]);
2001 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2002 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 /* March over the array once, left to right, finding natural runs,
2005 * and extending short natural runs to minrun elements.
2006 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 minrun = merge_compute_minrun(nremaining);
2008 do {
2009 int descending;
2010 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 /* Identify next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002013 n = count_run(lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 if (n < 0)
2015 goto fail;
2016 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002017 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 /* If short, extend to min(minrun, nremaining). */
2019 if (n < minrun) {
2020 const Py_ssize_t force = nremaining <= minrun ?
2021 nremaining : minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002022 if (binarysort(lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 goto fail;
2024 n = force;
2025 }
2026 /* Push run onto pending-runs stack, and maybe merge. */
2027 assert(ms.n < MAX_MERGE_PENDING);
2028 ms.pending[ms.n].base = lo;
2029 ms.pending[ms.n].len = n;
2030 ++ms.n;
2031 if (merge_collapse(&ms) < 0)
2032 goto fail;
2033 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002034 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 nremaining -= n;
2036 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 if (merge_force_collapse(&ms) < 0)
2039 goto fail;
2040 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002041 assert(keys == NULL
2042 ? ms.pending[0].base.keys == saved_ob_item
2043 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002045 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002046
2047succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002049fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002050 if (keys != NULL) {
2051 for (i = 0; i < saved_ob_size; i++)
2052 Py_DECREF(keys[i]);
2053 if (keys != &ms.temparray[saved_ob_size+1])
2054 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 if (self->allocated != -1 && result != NULL) {
2058 /* The user mucked with the list during the sort,
2059 * and we don't already have another error to report.
2060 */
2061 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2062 result = NULL;
2063 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 if (reverse && saved_ob_size > 1)
2066 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002069
Daniel Stutzbach98338222010-12-02 21:55:33 +00002070keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 final_ob_item = self->ob_item;
2072 i = Py_SIZE(self);
2073 Py_SIZE(self) = saved_ob_size;
2074 self->ob_item = saved_ob_item;
2075 self->allocated = saved_allocated;
2076 if (final_ob_item != NULL) {
2077 /* we cannot use list_clear() for this because it does not
2078 guarantee that the list is really empty when it returns */
2079 while (--i >= 0) {
2080 Py_XDECREF(final_ob_item[i]);
2081 }
2082 PyMem_FREE(final_ob_item);
2083 }
2084 Py_XINCREF(result);
2085 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002086}
Tim Peters330f9e92002-07-19 07:05:44 +00002087#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002088#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002089
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002090int
Fred Drakea2f55112000-07-09 15:16:51 +00002091PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 if (v == NULL || !PyList_Check(v)) {
2094 PyErr_BadInternalCall();
2095 return -1;
2096 }
2097 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
2098 if (v == NULL)
2099 return -1;
2100 Py_DECREF(v);
2101 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002102}
2103
Guido van Rossumb86c5492001-02-12 22:06:02 +00002104static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002105listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 if (Py_SIZE(self) > 1)
2108 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2109 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002110}
2111
Guido van Rossum84c76f51990-10-30 13:32:20 +00002112int
Fred Drakea2f55112000-07-09 15:16:51 +00002113PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 if (v == NULL || !PyList_Check(v)) {
2118 PyErr_BadInternalCall();
2119 return -1;
2120 }
2121 if (Py_SIZE(self) > 1)
2122 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2123 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002124}
2125
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002126PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002127PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 PyObject *w;
2130 PyObject **p, **q;
2131 Py_ssize_t n;
2132 if (v == NULL || !PyList_Check(v)) {
2133 PyErr_BadInternalCall();
2134 return NULL;
2135 }
2136 n = Py_SIZE(v);
2137 w = PyTuple_New(n);
2138 if (w == NULL)
2139 return NULL;
2140 p = ((PyTupleObject *)w)->ob_item;
2141 q = ((PyListObject *)v)->ob_item;
2142 while (--n >= 0) {
2143 Py_INCREF(*q);
2144 *p = *q;
2145 p++;
2146 q++;
2147 }
2148 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002149}
2150
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002151static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002152listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002155 PyObject *v;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002156
Petri Lehtinenebfaabd2011-11-06 21:02:39 +02002157 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2158 _PyEval_SliceIndex, &start,
2159 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 return NULL;
2161 if (start < 0) {
2162 start += Py_SIZE(self);
2163 if (start < 0)
2164 start = 0;
2165 }
2166 if (stop < 0) {
2167 stop += Py_SIZE(self);
2168 if (stop < 0)
2169 stop = 0;
2170 }
2171 for (i = start; i < stop && i < Py_SIZE(self); i++) {
2172 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2173 if (cmp > 0)
2174 return PyLong_FromSsize_t(i);
2175 else if (cmp < 0)
2176 return NULL;
2177 }
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002178 PyErr_Format(PyExc_ValueError, "%R is not in list", v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002180}
2181
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002182static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002183listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 Py_ssize_t count = 0;
2186 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 for (i = 0; i < Py_SIZE(self); i++) {
2189 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2190 if (cmp > 0)
2191 count++;
2192 else if (cmp < 0)
2193 return NULL;
2194 }
2195 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002196}
2197
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002198static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002199listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 for (i = 0; i < Py_SIZE(self); i++) {
2204 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2205 if (cmp > 0) {
2206 if (list_ass_slice(self, i, i+1,
2207 (PyObject *)NULL) == 0)
2208 Py_RETURN_NONE;
2209 return NULL;
2210 }
2211 else if (cmp < 0)
2212 return NULL;
2213 }
2214 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2215 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002216}
2217
Jeremy Hylton8caad492000-06-23 14:18:11 +00002218static int
2219list_traverse(PyListObject *o, visitproc visit, void *arg)
2220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 for (i = Py_SIZE(o); --i >= 0; )
2224 Py_VISIT(o->ob_item[i]);
2225 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002226}
2227
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002228static PyObject *
2229list_richcompare(PyObject *v, PyObject *w, int op)
2230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 PyListObject *vl, *wl;
2232 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002233
Brian Curtindfc80e32011-08-10 20:28:54 -05002234 if (!PyList_Check(v) || !PyList_Check(w))
2235 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 vl = (PyListObject *)v;
2238 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2241 /* Shortcut: if the lengths differ, the lists differ */
2242 PyObject *res;
2243 if (op == Py_EQ)
2244 res = Py_False;
2245 else
2246 res = Py_True;
2247 Py_INCREF(res);
2248 return res;
2249 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 /* Search for the first index where items are different */
2252 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2253 int k = PyObject_RichCompareBool(vl->ob_item[i],
2254 wl->ob_item[i], Py_EQ);
2255 if (k < 0)
2256 return NULL;
2257 if (!k)
2258 break;
2259 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2262 /* No more items to compare -- compare sizes */
2263 Py_ssize_t vs = Py_SIZE(vl);
2264 Py_ssize_t ws = Py_SIZE(wl);
2265 int cmp;
2266 PyObject *res;
2267 switch (op) {
2268 case Py_LT: cmp = vs < ws; break;
2269 case Py_LE: cmp = vs <= ws; break;
2270 case Py_EQ: cmp = vs == ws; break;
2271 case Py_NE: cmp = vs != ws; break;
2272 case Py_GT: cmp = vs > ws; break;
2273 case Py_GE: cmp = vs >= ws; break;
2274 default: return NULL; /* cannot happen */
2275 }
2276 if (cmp)
2277 res = Py_True;
2278 else
2279 res = Py_False;
2280 Py_INCREF(res);
2281 return res;
2282 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 /* We have an item that differs -- shortcuts for EQ/NE */
2285 if (op == Py_EQ) {
2286 Py_INCREF(Py_False);
2287 return Py_False;
2288 }
2289 if (op == Py_NE) {
2290 Py_INCREF(Py_True);
2291 return Py_True;
2292 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 /* Compare the final item again using the proper operator */
2295 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002296}
2297
Tim Peters6d6c1a32001-08-02 04:15:00 +00002298static int
2299list_init(PyListObject *self, PyObject *args, PyObject *kw)
2300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 PyObject *arg = NULL;
2302 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2305 return -1;
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 /* Verify list invariants established by PyType_GenericAlloc() */
2308 assert(0 <= Py_SIZE(self));
2309 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2310 assert(self->ob_item != NULL ||
2311 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 /* Empty previous contents */
2314 if (self->ob_item != NULL) {
2315 (void)list_clear(self);
2316 }
2317 if (arg != NULL) {
2318 PyObject *rv = listextend(self, arg);
2319 if (rv == NULL)
2320 return -1;
2321 Py_DECREF(rv);
2322 }
2323 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002324}
2325
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002326static PyObject *
2327list_sizeof(PyListObject *self)
2328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 res = sizeof(PyListObject) + self->allocated * sizeof(void*);
2332 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002333}
2334
Raymond Hettinger1021c442003-11-07 15:38:09 +00002335static PyObject *list_iter(PyObject *seq);
2336static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2337
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002338PyDoc_STRVAR(getitem_doc,
2339"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002340PyDoc_STRVAR(reversed_doc,
2341"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002342PyDoc_STRVAR(sizeof_doc,
2343"L.__sizeof__() -- size of L in memory, in bytes");
Eli Benderskycbbaa962011-02-25 05:47:53 +00002344PyDoc_STRVAR(clear_doc,
2345"L.clear() -> None -- remove all items from L");
2346PyDoc_STRVAR(copy_doc,
2347"L.copy() -> list -- a shallow copy of L");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002348PyDoc_STRVAR(append_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002349"L.append(object) -> None -- append object to end");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002350PyDoc_STRVAR(extend_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002351"L.extend(iterable) -> None -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002352PyDoc_STRVAR(insert_doc,
2353"L.insert(index, object) -- insert object before index");
2354PyDoc_STRVAR(pop_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002355"L.pop([index]) -> item -- remove and return item at index (default last).\n"
2356"Raises IndexError if list is empty or index is out of range.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002357PyDoc_STRVAR(remove_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002358"L.remove(value) -> None -- remove first occurrence of value.\n"
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002359"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002360PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002361"L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2362"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002363PyDoc_STRVAR(count_doc,
2364"L.count(value) -> integer -- return number of occurrences of value");
2365PyDoc_STRVAR(reverse_doc,
2366"L.reverse() -- reverse *IN PLACE*");
2367PyDoc_STRVAR(sort_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002368"L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002369
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002370static PyObject *list_subscript(PyListObject*, PyObject*);
2371
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002372static PyMethodDef list_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
2374 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
2375 {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002376 {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc},
2377 {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 {"append", (PyCFunction)listappend, METH_O, append_doc},
2379 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002380 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
2382 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
2383 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
2384 {"count", (PyCFunction)listcount, METH_O, count_doc},
2385 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
2386 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
2387 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002388};
2389
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002390static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 (lenfunc)list_length, /* sq_length */
2392 (binaryfunc)list_concat, /* sq_concat */
2393 (ssizeargfunc)list_repeat, /* sq_repeat */
2394 (ssizeargfunc)list_item, /* sq_item */
2395 0, /* sq_slice */
2396 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2397 0, /* sq_ass_slice */
2398 (objobjproc)list_contains, /* sq_contains */
2399 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2400 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002401};
2402
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002403PyDoc_STRVAR(list_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002404"list() -> new empty list\n"
2405"list(iterable) -> new list initialized from iterable's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002406
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002407static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002408list_subscript(PyListObject* self, PyObject* item)
2409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 if (PyIndex_Check(item)) {
2411 Py_ssize_t i;
2412 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2413 if (i == -1 && PyErr_Occurred())
2414 return NULL;
2415 if (i < 0)
2416 i += PyList_GET_SIZE(self);
2417 return list_item(self, i);
2418 }
2419 else if (PySlice_Check(item)) {
2420 Py_ssize_t start, stop, step, slicelength, cur, i;
2421 PyObject* result;
2422 PyObject* it;
2423 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002424
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002425 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 &start, &stop, &step, &slicelength) < 0) {
2427 return NULL;
2428 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 if (slicelength <= 0) {
2431 return PyList_New(0);
2432 }
2433 else if (step == 1) {
2434 return list_slice(self, start, stop);
2435 }
2436 else {
2437 result = PyList_New(slicelength);
2438 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 src = self->ob_item;
2441 dest = ((PyListObject *)result)->ob_item;
2442 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002443 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 it = src[cur];
2445 Py_INCREF(it);
2446 dest[i] = it;
2447 }
Tim Peters3b01a122002-07-19 02:35:45 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 return result;
2450 }
2451 }
2452 else {
2453 PyErr_Format(PyExc_TypeError,
2454 "list indices must be integers, not %.200s",
2455 item->ob_type->tp_name);
2456 return NULL;
2457 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002458}
2459
Tim Peters3b01a122002-07-19 02:35:45 +00002460static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002461list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 if (PyIndex_Check(item)) {
2464 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2465 if (i == -1 && PyErr_Occurred())
2466 return -1;
2467 if (i < 0)
2468 i += PyList_GET_SIZE(self);
2469 return list_ass_item(self, i, value);
2470 }
2471 else if (PySlice_Check(item)) {
2472 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002473
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002474 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 &start, &stop, &step, &slicelength) < 0) {
2476 return -1;
2477 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 if (step == 1)
2480 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 /* Make sure s[5:2] = [..] inserts at the right place:
2483 before 5, not before 2. */
2484 if ((step < 0 && start < stop) ||
2485 (step > 0 && start > stop))
2486 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 if (value == NULL) {
2489 /* delete slice */
2490 PyObject **garbage;
2491 size_t cur;
2492 Py_ssize_t i;
Tim Peters3b01a122002-07-19 02:35:45 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 if (slicelength <= 0)
2495 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 if (step < 0) {
2498 stop = start + 1;
2499 start = stop + step*(slicelength - 1) - 1;
2500 step = -step;
2501 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 assert((size_t)slicelength <=
2504 PY_SIZE_MAX / sizeof(PyObject*));
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00002505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 garbage = (PyObject**)
2507 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2508 if (!garbage) {
2509 PyErr_NoMemory();
2510 return -1;
2511 }
Tim Peters3b01a122002-07-19 02:35:45 +00002512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 /* drawing pictures might help understand these for
2514 loops. Basically, we memmove the parts of the
2515 list that are *not* part of the slice: step-1
2516 items for each item that is part of the slice,
2517 and then tail end of the list that was not
2518 covered by the slice */
2519 for (cur = start, i = 0;
2520 cur < (size_t)stop;
2521 cur += step, i++) {
2522 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 if (cur + step >= (size_t)Py_SIZE(self)) {
2527 lim = Py_SIZE(self) - cur - 1;
2528 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 memmove(self->ob_item + cur - i,
2531 self->ob_item + cur + 1,
2532 lim * sizeof(PyObject *));
2533 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002534 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 if (cur < (size_t)Py_SIZE(self)) {
2536 memmove(self->ob_item + cur - slicelength,
2537 self->ob_item + cur,
2538 (Py_SIZE(self) - cur) *
2539 sizeof(PyObject *));
2540 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 Py_SIZE(self) -= slicelength;
2543 list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 for (i = 0; i < slicelength; i++) {
2546 Py_DECREF(garbage[i]);
2547 }
2548 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 return 0;
2551 }
2552 else {
2553 /* assign slice */
2554 PyObject *ins, *seq;
2555 PyObject **garbage, **seqitems, **selfitems;
2556 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 /* protect against a[::-1] = a */
2559 if (self == (PyListObject*)value) {
2560 seq = list_slice((PyListObject*)value, 0,
2561 PyList_GET_SIZE(value));
2562 }
2563 else {
2564 seq = PySequence_Fast(value,
2565 "must assign iterable "
2566 "to extended slice");
2567 }
2568 if (!seq)
2569 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2572 PyErr_Format(PyExc_ValueError,
2573 "attempt to assign sequence of "
2574 "size %zd to extended slice of "
2575 "size %zd",
2576 PySequence_Fast_GET_SIZE(seq),
2577 slicelength);
2578 Py_DECREF(seq);
2579 return -1;
2580 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 if (!slicelength) {
2583 Py_DECREF(seq);
2584 return 0;
2585 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 garbage = (PyObject**)
2588 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2589 if (!garbage) {
2590 Py_DECREF(seq);
2591 PyErr_NoMemory();
2592 return -1;
2593 }
Tim Peters3b01a122002-07-19 02:35:45 +00002594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 selfitems = self->ob_item;
2596 seqitems = PySequence_Fast_ITEMS(seq);
2597 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002598 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 garbage[i] = selfitems[cur];
2600 ins = seqitems[i];
2601 Py_INCREF(ins);
2602 selfitems[cur] = ins;
2603 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 for (i = 0; i < slicelength; i++) {
2606 Py_DECREF(garbage[i]);
2607 }
Tim Peters3b01a122002-07-19 02:35:45 +00002608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 PyMem_FREE(garbage);
2610 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 return 0;
2613 }
2614 }
2615 else {
2616 PyErr_Format(PyExc_TypeError,
2617 "list indices must be integers, not %.200s",
2618 item->ob_type->tp_name);
2619 return -1;
2620 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002621}
2622
2623static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 (lenfunc)list_length,
2625 (binaryfunc)list_subscript,
2626 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002627};
2628
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002629PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2631 "list",
2632 sizeof(PyListObject),
2633 0,
2634 (destructor)list_dealloc, /* tp_dealloc */
2635 0, /* tp_print */
2636 0, /* tp_getattr */
2637 0, /* tp_setattr */
2638 0, /* tp_reserved */
2639 (reprfunc)list_repr, /* tp_repr */
2640 0, /* tp_as_number */
2641 &list_as_sequence, /* tp_as_sequence */
2642 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002643 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 0, /* tp_call */
2645 0, /* tp_str */
2646 PyObject_GenericGetAttr, /* tp_getattro */
2647 0, /* tp_setattro */
2648 0, /* tp_as_buffer */
2649 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2650 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2651 list_doc, /* tp_doc */
2652 (traverseproc)list_traverse, /* tp_traverse */
2653 (inquiry)list_clear, /* tp_clear */
2654 list_richcompare, /* tp_richcompare */
2655 0, /* tp_weaklistoffset */
2656 list_iter, /* tp_iter */
2657 0, /* tp_iternext */
2658 list_methods, /* tp_methods */
2659 0, /* tp_members */
2660 0, /* tp_getset */
2661 0, /* tp_base */
2662 0, /* tp_dict */
2663 0, /* tp_descr_get */
2664 0, /* tp_descr_set */
2665 0, /* tp_dictoffset */
2666 (initproc)list_init, /* tp_init */
2667 PyType_GenericAlloc, /* tp_alloc */
2668 PyType_GenericNew, /* tp_new */
2669 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002670};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002671
2672
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002673/*********************** List Iterator **************************/
2674
2675typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02002677 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002679} listiterobject;
2680
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002681static PyObject *list_iter(PyObject *);
2682static void listiter_dealloc(listiterobject *);
2683static int listiter_traverse(listiterobject *, visitproc, void *);
2684static PyObject *listiter_next(listiterobject *);
2685static PyObject *listiter_len(listiterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002686static PyObject *listiter_reduce_general(void *_it, int forward);
2687static PyObject *listiter_reduce(listiterobject *);
2688static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002689
Armin Rigof5b3e362006-02-11 21:32:43 +00002690PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002691PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2692PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002693
2694static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002696 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
2697 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002699};
2700
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002701PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2703 "list_iterator", /* tp_name */
2704 sizeof(listiterobject), /* tp_basicsize */
2705 0, /* tp_itemsize */
2706 /* methods */
2707 (destructor)listiter_dealloc, /* tp_dealloc */
2708 0, /* tp_print */
2709 0, /* tp_getattr */
2710 0, /* tp_setattr */
2711 0, /* tp_reserved */
2712 0, /* tp_repr */
2713 0, /* tp_as_number */
2714 0, /* tp_as_sequence */
2715 0, /* tp_as_mapping */
2716 0, /* tp_hash */
2717 0, /* tp_call */
2718 0, /* tp_str */
2719 PyObject_GenericGetAttr, /* tp_getattro */
2720 0, /* tp_setattro */
2721 0, /* tp_as_buffer */
2722 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2723 0, /* tp_doc */
2724 (traverseproc)listiter_traverse, /* tp_traverse */
2725 0, /* tp_clear */
2726 0, /* tp_richcompare */
2727 0, /* tp_weaklistoffset */
2728 PyObject_SelfIter, /* tp_iter */
2729 (iternextfunc)listiter_next, /* tp_iternext */
2730 listiter_methods, /* tp_methods */
2731 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002732};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002733
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002734
2735static PyObject *
2736list_iter(PyObject *seq)
2737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 if (!PyList_Check(seq)) {
2741 PyErr_BadInternalCall();
2742 return NULL;
2743 }
2744 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2745 if (it == NULL)
2746 return NULL;
2747 it->it_index = 0;
2748 Py_INCREF(seq);
2749 it->it_seq = (PyListObject *)seq;
2750 _PyObject_GC_TRACK(it);
2751 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002752}
2753
2754static void
2755listiter_dealloc(listiterobject *it)
2756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 _PyObject_GC_UNTRACK(it);
2758 Py_XDECREF(it->it_seq);
2759 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002760}
2761
2762static int
2763listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 Py_VISIT(it->it_seq);
2766 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002767}
2768
2769static PyObject *
2770listiter_next(listiterobject *it)
2771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 PyListObject *seq;
2773 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 assert(it != NULL);
2776 seq = it->it_seq;
2777 if (seq == NULL)
2778 return NULL;
2779 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 if (it->it_index < PyList_GET_SIZE(seq)) {
2782 item = PyList_GET_ITEM(seq, it->it_index);
2783 ++it->it_index;
2784 Py_INCREF(item);
2785 return item;
2786 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 Py_DECREF(seq);
2789 it->it_seq = NULL;
2790 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002791}
2792
2793static PyObject *
2794listiter_len(listiterobject *it)
2795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 Py_ssize_t len;
2797 if (it->it_seq) {
2798 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2799 if (len >= 0)
2800 return PyLong_FromSsize_t(len);
2801 }
2802 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002803}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002804
2805static PyObject *
2806listiter_reduce(listiterobject *it)
2807{
2808 return listiter_reduce_general(it, 1);
2809}
2810
2811static PyObject *
2812listiter_setstate(listiterobject *it, PyObject *state)
2813{
Victor Stinner7660b882013-06-24 23:59:24 +02002814 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002815 if (index == -1 && PyErr_Occurred())
2816 return NULL;
2817 if (it->it_seq != NULL) {
2818 if (index < 0)
2819 index = 0;
2820 it->it_index = index;
2821 }
2822 Py_RETURN_NONE;
2823}
2824
Raymond Hettinger1021c442003-11-07 15:38:09 +00002825/*********************** List Reverse Iterator **************************/
2826
2827typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 PyObject_HEAD
2829 Py_ssize_t it_index;
2830 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002831} listreviterobject;
2832
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002833static PyObject *list_reversed(PyListObject *, PyObject *);
2834static void listreviter_dealloc(listreviterobject *);
2835static int listreviter_traverse(listreviterobject *, visitproc, void *);
2836static PyObject *listreviter_next(listreviterobject *);
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002837static PyObject *listreviter_len(listreviterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002838static PyObject *listreviter_reduce(listreviterobject *);
2839static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002840
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002841static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002843 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
2844 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002846};
2847
Raymond Hettinger1021c442003-11-07 15:38:09 +00002848PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2850 "list_reverseiterator", /* tp_name */
2851 sizeof(listreviterobject), /* tp_basicsize */
2852 0, /* tp_itemsize */
2853 /* methods */
2854 (destructor)listreviter_dealloc, /* tp_dealloc */
2855 0, /* tp_print */
2856 0, /* tp_getattr */
2857 0, /* tp_setattr */
2858 0, /* tp_reserved */
2859 0, /* tp_repr */
2860 0, /* tp_as_number */
2861 0, /* tp_as_sequence */
2862 0, /* tp_as_mapping */
2863 0, /* tp_hash */
2864 0, /* tp_call */
2865 0, /* tp_str */
2866 PyObject_GenericGetAttr, /* tp_getattro */
2867 0, /* tp_setattro */
2868 0, /* tp_as_buffer */
2869 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2870 0, /* tp_doc */
2871 (traverseproc)listreviter_traverse, /* tp_traverse */
2872 0, /* tp_clear */
2873 0, /* tp_richcompare */
2874 0, /* tp_weaklistoffset */
2875 PyObject_SelfIter, /* tp_iter */
2876 (iternextfunc)listreviter_next, /* tp_iternext */
2877 listreviter_methods, /* tp_methods */
2878 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00002879};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002880
2881static PyObject *
2882list_reversed(PyListObject *seq, PyObject *unused)
2883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2887 if (it == NULL)
2888 return NULL;
2889 assert(PyList_Check(seq));
2890 it->it_index = PyList_GET_SIZE(seq) - 1;
2891 Py_INCREF(seq);
2892 it->it_seq = seq;
2893 PyObject_GC_Track(it);
2894 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002895}
2896
2897static void
2898listreviter_dealloc(listreviterobject *it)
2899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 PyObject_GC_UnTrack(it);
2901 Py_XDECREF(it->it_seq);
2902 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002903}
2904
2905static int
2906listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 Py_VISIT(it->it_seq);
2909 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002910}
2911
2912static PyObject *
2913listreviter_next(listreviterobject *it)
2914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 PyObject *item;
2916 Py_ssize_t index = it->it_index;
2917 PyListObject *seq = it->it_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2920 item = PyList_GET_ITEM(seq, index);
2921 it->it_index--;
2922 Py_INCREF(item);
2923 return item;
2924 }
2925 it->it_index = -1;
2926 if (seq != NULL) {
2927 it->it_seq = NULL;
2928 Py_DECREF(seq);
2929 }
2930 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002931}
2932
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002933static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002934listreviter_len(listreviterobject *it)
2935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 Py_ssize_t len = it->it_index + 1;
2937 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2938 len = 0;
2939 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002940}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002941
2942static PyObject *
2943listreviter_reduce(listreviterobject *it)
2944{
2945 return listiter_reduce_general(it, 0);
2946}
2947
2948static PyObject *
2949listreviter_setstate(listreviterobject *it, PyObject *state)
2950{
2951 Py_ssize_t index = PyLong_AsSsize_t(state);
2952 if (index == -1 && PyErr_Occurred())
2953 return NULL;
2954 if (it->it_seq != NULL) {
2955 if (index < -1)
2956 index = -1;
2957 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
2958 index = PyList_GET_SIZE(it->it_seq) - 1;
2959 it->it_index = index;
2960 }
2961 Py_RETURN_NONE;
2962}
2963
2964/* common pickling support */
2965
2966static PyObject *
2967listiter_reduce_general(void *_it, int forward)
2968{
2969 PyObject *list;
2970
2971 /* the objects are not the same, index is of different types! */
2972 if (forward) {
2973 listiterobject *it = (listiterobject *)_it;
2974 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02002975 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002976 it->it_seq, it->it_index);
2977 } else {
2978 listreviterobject *it = (listreviterobject *)_it;
2979 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +02002980 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("reversed"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002981 it->it_seq, it->it_index);
2982 }
2983 /* empty iterator, create an empty list */
2984 list = PyList_New(0);
2985 if (list == NULL)
2986 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02002987 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002988}