blob: 0b2c8c1dc27a5f9991065e92f294c7ae53237603 [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{
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030085 PyObject *xoptions, *value;
86 _Py_IDENTIFIER(showalloccount);
87
88 xoptions = PySys_GetXOptions();
89 if (xoptions == NULL)
90 return;
91 value = _PyDict_GetItemId(xoptions, &PyId_showalloccount);
92 if (value != Py_True)
93 return;
94
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
96 count_alloc);
97 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
98 "d\n", count_reuse);
99 fprintf(stderr, "%.2f%% reuse rate\n\n",
100 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +0000101}
102#endif
103
Raymond Hettinger0468e412004-05-05 05:37:53 +0000104/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000105#ifndef PyList_MAXFREELIST
106#define PyList_MAXFREELIST 80
107#endif
108static PyListObject *free_list[PyList_MAXFREELIST];
109static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000110
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100111int
112PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100115 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 while (numfree) {
117 op = free_list[--numfree];
118 assert(PyList_CheckExact(op));
119 PyObject_GC_Del(op);
120 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100121 return ret;
122}
123
124void
125PyList_Fini(void)
126{
127 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000128}
129
David Malcolm49526f42012-06-22 14:55:41 -0400130/* Print summary info about the state of the optimized allocator */
131void
132_PyList_DebugMallocStats(FILE *out)
133{
134 _PyDebugAllocatorStats(out,
135 "free PyListObject",
136 numfree, sizeof(PyListObject));
137}
138
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000139PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000140PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 PyListObject *op;
143 size_t nbytes;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000144#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 static int initialized = 0;
146 if (!initialized) {
147 Py_AtExit(show_alloc);
148 initialized = 1;
149 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000150#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 if (size < 0) {
153 PyErr_BadInternalCall();
154 return NULL;
155 }
156 /* Check for overflow without an actual overflow,
157 * which can cause compiler to optimise out */
158 if ((size_t)size > PY_SIZE_MAX / sizeof(PyObject *))
159 return PyErr_NoMemory();
160 nbytes = size * sizeof(PyObject *);
161 if (numfree) {
162 numfree--;
163 op = free_list[numfree];
164 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000165#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000167#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 } else {
169 op = PyObject_GC_New(PyListObject, &PyList_Type);
170 if (op == NULL)
171 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000172#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000174#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 }
176 if (size <= 0)
177 op->ob_item = NULL;
178 else {
179 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
180 if (op->ob_item == NULL) {
181 Py_DECREF(op);
182 return PyErr_NoMemory();
183 }
184 memset(op->ob_item, 0, nbytes);
185 }
186 Py_SIZE(op) = size;
187 op->allocated = size;
188 _PyObject_GC_TRACK(op);
189 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190}
191
Martin v. Löwis18e16552006-02-15 17:27:45 +0000192Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000193PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 if (!PyList_Check(op)) {
196 PyErr_BadInternalCall();
197 return -1;
198 }
199 else
200 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000201}
202
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000203static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000204
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000206PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 if (!PyList_Check(op)) {
209 PyErr_BadInternalCall();
210 return NULL;
211 }
212 if (i < 0 || i >= Py_SIZE(op)) {
213 if (indexerr == NULL) {
214 indexerr = PyUnicode_FromString(
215 "list index out of range");
216 if (indexerr == NULL)
217 return NULL;
218 }
219 PyErr_SetObject(PyExc_IndexError, indexerr);
220 return NULL;
221 }
222 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223}
224
225int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200226PyList_SetItem(PyObject *op, Py_ssize_t i,
227 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200229 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 if (!PyList_Check(op)) {
231 Py_XDECREF(newitem);
232 PyErr_BadInternalCall();
233 return -1;
234 }
235 if (i < 0 || i >= Py_SIZE(op)) {
236 Py_XDECREF(newitem);
237 PyErr_SetString(PyExc_IndexError,
238 "list assignment index out of range");
239 return -1;
240 }
241 p = ((PyListObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300242 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244}
245
246static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000247ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 Py_ssize_t i, n = Py_SIZE(self);
250 PyObject **items;
251 if (v == NULL) {
252 PyErr_BadInternalCall();
253 return -1;
254 }
255 if (n == PY_SSIZE_T_MAX) {
256 PyErr_SetString(PyExc_OverflowError,
257 "cannot add more objects to list");
258 return -1;
259 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000260
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800261 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 if (where < 0) {
265 where += n;
266 if (where < 0)
267 where = 0;
268 }
269 if (where > n)
270 where = n;
271 items = self->ob_item;
272 for (i = n; --i >= where; )
273 items[i+1] = items[i];
274 Py_INCREF(v);
275 items[where] = v;
276 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277}
278
279int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000280PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 if (!PyList_Check(op)) {
283 PyErr_BadInternalCall();
284 return -1;
285 }
286 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287}
288
Raymond Hettinger40a03822004-04-12 13:05:09 +0000289static int
290app1(PyListObject *self, PyObject *v)
291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 assert (v != NULL);
295 if (n == PY_SSIZE_T_MAX) {
296 PyErr_SetString(PyExc_OverflowError,
297 "cannot add more objects to list");
298 return -1;
299 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000300
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800301 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 Py_INCREF(v);
305 PyList_SET_ITEM(self, n, v);
306 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000307}
308
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309int
Fred Drakea2f55112000-07-09 15:16:51 +0000310PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 if (PyList_Check(op) && (newitem != NULL))
313 return app1((PyListObject *)op, newitem);
314 PyErr_BadInternalCall();
315 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316}
317
318/* Methods */
319
320static void
Fred Drakea2f55112000-07-09 15:16:51 +0000321list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 Py_ssize_t i;
324 PyObject_GC_UnTrack(op);
325 Py_TRASHCAN_SAFE_BEGIN(op)
326 if (op->ob_item != NULL) {
327 /* Do it backwards, for Christian Tismer.
328 There's a simple test case where somehow this reduces
329 thrashing when a *very* large list is created and
330 immediately deleted. */
331 i = Py_SIZE(op);
332 while (--i >= 0) {
333 Py_XDECREF(op->ob_item[i]);
334 }
335 PyMem_FREE(op->ob_item);
336 }
337 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
338 free_list[numfree++] = op;
339 else
340 Py_TYPE(op)->tp_free((PyObject *)op);
341 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342}
343
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000344static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000345list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 Py_ssize_t i;
Victor Stinner5c733472013-11-18 21:11:57 +0100348 PyObject *s;
Victor Stinner5c733472013-11-18 21:11:57 +0100349 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200350
351 if (Py_SIZE(v) == 0) {
352 return PyUnicode_FromString("[]");
353 }
354
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) {
Victor Stinner4a587072013-11-19 12:54:53 +0100372 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
Victor Stinner5c733472013-11-18 21:11:57 +0100373 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)
Martin Panterb93d8632016-07-25 02:39:20 +0000491 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000493 size = Py_SIZE(a) + Py_SIZE(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 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
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800721 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 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 if (i < 0 || i >= Py_SIZE(a)) {
741 PyErr_SetString(PyExc_IndexError,
742 "list assignment index out of range");
743 return -1;
744 }
745 if (v == NULL)
746 return list_ass_slice(a, i, i+1, v);
747 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300748 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000750}
751
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000752static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000753listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 Py_ssize_t i;
756 PyObject *v;
757 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
758 return NULL;
759 if (ins1(self, i, v) == 0)
760 Py_RETURN_NONE;
761 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000762}
763
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000764static PyObject *
Eli Benderskycbbaa962011-02-25 05:47:53 +0000765listclear(PyListObject *self)
766{
767 list_clear(self);
768 Py_RETURN_NONE;
769}
770
771static PyObject *
772listcopy(PyListObject *self)
773{
774 return list_slice(self, 0, Py_SIZE(self));
775}
776
777static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000778listappend(PyListObject *self, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 if (app1(self, v) == 0)
781 Py_RETURN_NONE;
782 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000783}
784
Barry Warsawdedf6d61998-10-09 16:37:25 +0000785static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000786listextend(PyListObject *self, PyObject *b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyObject *it; /* iter(v) */
789 Py_ssize_t m; /* size of self */
790 Py_ssize_t n; /* guess for size of b */
791 Py_ssize_t mn; /* m + n */
792 Py_ssize_t i;
793 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 /* Special cases:
796 1) lists and tuples which can use PySequence_Fast ops
797 2) extending self to self requires making a copy first
798 */
799 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
800 PyObject **src, **dest;
801 b = PySequence_Fast(b, "argument must be iterable");
802 if (!b)
803 return NULL;
804 n = PySequence_Fast_GET_SIZE(b);
805 if (n == 0) {
806 /* short circuit when b is empty */
807 Py_DECREF(b);
808 Py_RETURN_NONE;
809 }
810 m = Py_SIZE(self);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800811 if (list_resize(self, m + n) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 Py_DECREF(b);
813 return NULL;
814 }
815 /* note that we may still have self == b here for the
816 * situation a.extend(a), but the following code works
817 * in that case too. Just make sure to resize self
818 * before calling PySequence_Fast_ITEMS.
819 */
820 /* populate the end of self with b's items */
821 src = PySequence_Fast_ITEMS(b);
822 dest = self->ob_item + m;
823 for (i = 0; i < n; i++) {
824 PyObject *o = src[i];
825 Py_INCREF(o);
826 dest[i] = o;
827 }
828 Py_DECREF(b);
829 Py_RETURN_NONE;
830 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 it = PyObject_GetIter(b);
833 if (it == NULL)
834 return NULL;
835 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 /* Guess a result list size. */
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200838 n = PyObject_LengthHint(b, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800839 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 Py_DECREF(it);
841 return NULL;
842 }
843 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000844 if (m > PY_SSIZE_T_MAX - n) {
845 /* m + n overflowed; on the chance that n lied, and there really
846 * is enough room, ignore it. If n was telling the truth, we'll
847 * eventually run out of memory during the loop.
848 */
849 }
850 else {
851 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800853 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 goto error;
855 /* Make the list sane again. */
856 Py_SIZE(self) = m;
857 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 /* Run iterator to exhaustion. */
860 for (;;) {
861 PyObject *item = iternext(it);
862 if (item == NULL) {
863 if (PyErr_Occurred()) {
864 if (PyErr_ExceptionMatches(PyExc_StopIteration))
865 PyErr_Clear();
866 else
867 goto error;
868 }
869 break;
870 }
871 if (Py_SIZE(self) < self->allocated) {
872 /* steals ref */
873 PyList_SET_ITEM(self, Py_SIZE(self), item);
874 ++Py_SIZE(self);
875 }
876 else {
877 int status = app1(self, item);
878 Py_DECREF(item); /* append creates a new ref */
879 if (status < 0)
880 goto error;
881 }
882 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200885 if (Py_SIZE(self) < self->allocated) {
886 if (list_resize(self, Py_SIZE(self)) < 0)
887 goto error;
888 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 Py_DECREF(it);
891 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000892
893 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 Py_DECREF(it);
895 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000896}
897
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000898PyObject *
899_PyList_Extend(PyListObject *self, PyObject *b)
900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 return listextend(self, b);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000902}
903
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000904static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000905list_inplace_concat(PyListObject *self, PyObject *other)
906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 result = listextend(self, other);
910 if (result == NULL)
911 return result;
912 Py_DECREF(result);
913 Py_INCREF(self);
914 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000915}
916
917static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000918listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 Py_ssize_t i = -1;
921 PyObject *v;
922 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 if (!PyArg_ParseTuple(args, "|n:pop", &i))
925 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 if (Py_SIZE(self) == 0) {
928 /* Special-case most common failure cause */
929 PyErr_SetString(PyExc_IndexError, "pop from empty list");
930 return NULL;
931 }
932 if (i < 0)
933 i += Py_SIZE(self);
934 if (i < 0 || i >= Py_SIZE(self)) {
935 PyErr_SetString(PyExc_IndexError, "pop index out of range");
936 return NULL;
937 }
938 v = self->ob_item[i];
939 if (i == Py_SIZE(self) - 1) {
940 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +0200941 if (status >= 0)
942 return v; /* and v now owns the reference the list had */
943 else
944 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 }
946 Py_INCREF(v);
947 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +0200948 if (status < 0) {
949 Py_DECREF(v);
950 return NULL;
951 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000953}
954
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000955/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
956static void
957reverse_slice(PyObject **lo, PyObject **hi)
958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 --hi;
962 while (lo < hi) {
963 PyObject *t = *lo;
964 *lo = *hi;
965 *hi = t;
966 ++lo;
967 --hi;
968 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000969}
970
Tim Petersa64dc242002-08-01 02:13:36 +0000971/* Lots of code for an adaptive, stable, natural mergesort. There are many
972 * pieces to this algorithm; read listsort.txt for overviews and details.
973 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000974
Daniel Stutzbach98338222010-12-02 21:55:33 +0000975/* A sortslice contains a pointer to an array of keys and a pointer to
976 * an array of corresponding values. In other words, keys[i]
977 * corresponds with values[i]. If values == NULL, then the keys are
978 * also the values.
979 *
980 * Several convenience routines are provided here, so that keys and
981 * values are always moved in sync.
982 */
983
984typedef struct {
985 PyObject **keys;
986 PyObject **values;
987} sortslice;
988
989Py_LOCAL_INLINE(void)
990sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
991{
992 s1->keys[i] = s2->keys[j];
993 if (s1->values != NULL)
994 s1->values[i] = s2->values[j];
995}
996
997Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000998sortslice_copy_incr(sortslice *dst, sortslice *src)
999{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001000 *dst->keys++ = *src->keys++;
1001 if (dst->values != NULL)
1002 *dst->values++ = *src->values++;
1003}
1004
1005Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001006sortslice_copy_decr(sortslice *dst, sortslice *src)
1007{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001008 *dst->keys-- = *src->keys--;
1009 if (dst->values != NULL)
1010 *dst->values-- = *src->values--;
1011}
1012
1013
1014Py_LOCAL_INLINE(void)
1015sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001016 Py_ssize_t n)
1017{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001018 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1019 if (s1->values != NULL)
1020 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1021}
1022
1023Py_LOCAL_INLINE(void)
1024sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001025 Py_ssize_t n)
1026{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001027 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1028 if (s1->values != NULL)
1029 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1030}
1031
1032Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001033sortslice_advance(sortslice *slice, Py_ssize_t n)
1034{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001035 slice->keys += n;
1036 if (slice->values != NULL)
1037 slice->values += n;
1038}
1039
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001040/* Comparison function: PyObject_RichCompareBool with Py_LT.
Tim Petersa64dc242002-08-01 02:13:36 +00001041 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1042 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001043
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001044#define ISLT(X, Y) (PyObject_RichCompareBool(X, Y, Py_LT))
Tim Peters66860f62002-08-04 17:47:26 +00001045
1046/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001047 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1048 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1049*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001050#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001052
1053/* binarysort is the best method for sorting small arrays: it does
1054 few compares, but can do data movement quadratic in the number of
1055 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001056 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001057 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001058 On entry, must have lo <= start <= hi, and that [lo, start) is already
1059 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001060 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001061 Even in case of error, the output slice will be some permutation of
1062 the input (nothing is lost or duplicated).
1063*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001064static int
Daniel Stutzbach98338222010-12-02 21:55:33 +00001065binarysort(sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001066{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001067 Py_ssize_t k;
1068 PyObject **l, **p, **r;
1069 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001070
Daniel Stutzbach98338222010-12-02 21:55:33 +00001071 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001073 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 ++start;
1075 for (; start < hi; ++start) {
1076 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001077 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 r = start;
1079 pivot = *r;
1080 /* Invariants:
1081 * pivot >= all in [lo, l).
1082 * pivot < all in [r, start).
1083 * The second is vacuously true at the start.
1084 */
1085 assert(l < r);
1086 do {
1087 p = l + ((r - l) >> 1);
1088 IFLT(pivot, *p)
1089 r = p;
1090 else
1091 l = p+1;
1092 } while (l < r);
1093 assert(l == r);
1094 /* The invariants still hold, so pivot >= all in [lo, l) and
1095 pivot < all in [l, start), so pivot belongs at l. Note
1096 that if there are elements equal to pivot, l points to the
1097 first slot after them -- that's why this sort is stable.
1098 Slide over to make room.
1099 Caution: using memmove is much slower under MSVC 5;
1100 we're not usually moving many slots. */
1101 for (p = start; p > l; --p)
1102 *p = *(p-1);
1103 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001104 if (lo.values != NULL) {
1105 Py_ssize_t offset = lo.values - lo.keys;
1106 p = start + offset;
1107 pivot = *p;
1108 l += offset;
1109 for (p = start + offset; p > l; --p)
1110 *p = *(p-1);
1111 *l = pivot;
1112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 }
1114 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001115
1116 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001118}
1119
Tim Petersa64dc242002-08-01 02:13:36 +00001120/*
1121Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1122is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001123
Tim Petersa64dc242002-08-01 02:13:36 +00001124 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001125
Tim Petersa64dc242002-08-01 02:13:36 +00001126or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001127
Tim Petersa64dc242002-08-01 02:13:36 +00001128 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001129
Tim Petersa64dc242002-08-01 02:13:36 +00001130Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1131For its intended use in a stable mergesort, the strictness of the defn of
1132"descending" is needed so that the caller can safely reverse a descending
1133sequence without violating stability (strict > ensures there are no equal
1134elements to get out of order).
1135
1136Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001137*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001138static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001139count_run(PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 Py_ssize_t k;
1142 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 assert(lo < hi);
1145 *descending = 0;
1146 ++lo;
1147 if (lo == hi)
1148 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 n = 2;
1151 IFLT(*lo, *(lo-1)) {
1152 *descending = 1;
1153 for (lo = lo+1; lo < hi; ++lo, ++n) {
1154 IFLT(*lo, *(lo-1))
1155 ;
1156 else
1157 break;
1158 }
1159 }
1160 else {
1161 for (lo = lo+1; lo < hi; ++lo, ++n) {
1162 IFLT(*lo, *(lo-1))
1163 break;
1164 }
1165 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001168fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001170}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001171
Tim Petersa64dc242002-08-01 02:13:36 +00001172/*
1173Locate the proper position of key in a sorted vector; if the vector contains
1174an element equal to key, return the position immediately to the left of
1175the leftmost equal element. [gallop_right() does the same except returns
1176the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001177
Tim Petersa64dc242002-08-01 02:13:36 +00001178"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001179
Tim Petersa64dc242002-08-01 02:13:36 +00001180"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1181hint is to the final result, the faster this runs.
1182
1183The return value is the int k in 0..n such that
1184
1185 a[k-1] < key <= a[k]
1186
1187pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1188key belongs at index k; or, IOW, the first k elements of a should precede
1189key, and the last n-k should follow key.
1190
1191Returns -1 on error. See listsort.txt for info on the method.
1192*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001193static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001194gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 Py_ssize_t ofs;
1197 Py_ssize_t lastofs;
1198 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 a += hint;
1203 lastofs = 0;
1204 ofs = 1;
1205 IFLT(*a, key) {
1206 /* a[hint] < key -- gallop right, until
1207 * a[hint + lastofs] < key <= a[hint + ofs]
1208 */
1209 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1210 while (ofs < maxofs) {
1211 IFLT(a[ofs], key) {
1212 lastofs = ofs;
1213 ofs = (ofs << 1) + 1;
1214 if (ofs <= 0) /* int overflow */
1215 ofs = maxofs;
1216 }
1217 else /* key <= a[hint + ofs] */
1218 break;
1219 }
1220 if (ofs > maxofs)
1221 ofs = maxofs;
1222 /* Translate back to offsets relative to &a[0]. */
1223 lastofs += hint;
1224 ofs += hint;
1225 }
1226 else {
1227 /* key <= a[hint] -- gallop left, until
1228 * a[hint - ofs] < key <= a[hint - lastofs]
1229 */
1230 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1231 while (ofs < maxofs) {
1232 IFLT(*(a-ofs), key)
1233 break;
1234 /* key <= a[hint - ofs] */
1235 lastofs = ofs;
1236 ofs = (ofs << 1) + 1;
1237 if (ofs <= 0) /* int overflow */
1238 ofs = maxofs;
1239 }
1240 if (ofs > maxofs)
1241 ofs = maxofs;
1242 /* Translate back to positive offsets relative to &a[0]. */
1243 k = lastofs;
1244 lastofs = hint - ofs;
1245 ofs = hint - k;
1246 }
1247 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1250 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1251 * right of lastofs but no farther right than ofs. Do a binary
1252 * search, with invariant a[lastofs-1] < key <= a[ofs].
1253 */
1254 ++lastofs;
1255 while (lastofs < ofs) {
1256 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 IFLT(a[m], key)
1259 lastofs = m+1; /* a[m] < key */
1260 else
1261 ofs = m; /* key <= a[m] */
1262 }
1263 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1264 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001265
1266fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001268}
1269
1270/*
1271Exactly like gallop_left(), except that if key already exists in a[0:n],
1272finds the position immediately to the right of the rightmost equal value.
1273
1274The return value is the int k in 0..n such that
1275
1276 a[k-1] <= key < a[k]
1277
1278or -1 if error.
1279
1280The code duplication is massive, but this is enough different given that
1281we're sticking to "<" comparisons that it's much harder to follow if
1282written as one routine with yet another "left or right?" flag.
1283*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001284static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001285gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 Py_ssize_t ofs;
1288 Py_ssize_t lastofs;
1289 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 a += hint;
1294 lastofs = 0;
1295 ofs = 1;
1296 IFLT(key, *a) {
1297 /* key < a[hint] -- gallop left, until
1298 * a[hint - ofs] <= key < a[hint - lastofs]
1299 */
1300 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1301 while (ofs < maxofs) {
1302 IFLT(key, *(a-ofs)) {
1303 lastofs = ofs;
1304 ofs = (ofs << 1) + 1;
1305 if (ofs <= 0) /* int overflow */
1306 ofs = maxofs;
1307 }
1308 else /* a[hint - ofs] <= key */
1309 break;
1310 }
1311 if (ofs > maxofs)
1312 ofs = maxofs;
1313 /* Translate back to positive offsets relative to &a[0]. */
1314 k = lastofs;
1315 lastofs = hint - ofs;
1316 ofs = hint - k;
1317 }
1318 else {
1319 /* a[hint] <= key -- gallop right, until
1320 * a[hint + lastofs] <= key < a[hint + ofs]
1321 */
1322 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1323 while (ofs < maxofs) {
1324 IFLT(key, a[ofs])
1325 break;
1326 /* a[hint + ofs] <= key */
1327 lastofs = ofs;
1328 ofs = (ofs << 1) + 1;
1329 if (ofs <= 0) /* int overflow */
1330 ofs = maxofs;
1331 }
1332 if (ofs > maxofs)
1333 ofs = maxofs;
1334 /* Translate back to offsets relative to &a[0]. */
1335 lastofs += hint;
1336 ofs += hint;
1337 }
1338 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1341 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1342 * right of lastofs but no farther right than ofs. Do a binary
1343 * search, with invariant a[lastofs-1] <= key < a[ofs].
1344 */
1345 ++lastofs;
1346 while (lastofs < ofs) {
1347 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 IFLT(key, a[m])
1350 ofs = m; /* key < a[m] */
1351 else
1352 lastofs = m+1; /* a[m] <= key */
1353 }
1354 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1355 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001356
1357fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001359}
1360
1361/* The maximum number of entries in a MergeState's pending-runs stack.
1362 * This is enough to sort arrays of size up to about
1363 * 32 * phi ** MAX_MERGE_PENDING
1364 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1365 * with 2**64 elements.
1366 */
1367#define MAX_MERGE_PENDING 85
1368
Tim Peterse05f65a2002-08-10 05:21:15 +00001369/* When we get into galloping mode, we stay there until both runs win less
1370 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
Tim Petersa64dc242002-08-01 02:13:36 +00001371 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001372#define MIN_GALLOP 7
Tim Petersa64dc242002-08-01 02:13:36 +00001373
1374/* Avoid malloc for small temp arrays. */
1375#define MERGESTATE_TEMP_SIZE 256
1376
1377/* One MergeState exists on the stack per invocation of mergesort. It's just
1378 * a convenient way to pass state around among the helper functions.
1379 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001380struct s_slice {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001381 sortslice base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 Py_ssize_t len;
Tim Peterse05f65a2002-08-10 05:21:15 +00001383};
1384
Tim Petersa64dc242002-08-01 02:13:36 +00001385typedef struct s_MergeState {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 /* This controls when we get *into* galloping mode. It's initialized
1387 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1388 * random data, and lower for highly structured data.
1389 */
1390 Py_ssize_t min_gallop;
Tim Peterse05f65a2002-08-10 05:21:15 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 /* 'a' is temp storage to help with merges. It contains room for
1393 * alloced entries.
1394 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001395 sortslice a; /* may point to temparray below */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 Py_ssize_t alloced;
Tim Petersa64dc242002-08-01 02:13:36 +00001397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 /* A stack of n pending runs yet to be merged. Run #i starts at
1399 * address base[i] and extends for len[i] elements. It's always
1400 * true (so long as the indices are in bounds) that
1401 *
1402 * pending[i].base + pending[i].len == pending[i+1].base
1403 *
1404 * so we could cut the storage for this, but it's a minor amount,
1405 * and keeping all the info explicit simplifies the code.
1406 */
1407 int n;
1408 struct s_slice pending[MAX_MERGE_PENDING];
Tim Petersa64dc242002-08-01 02:13:36 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 /* 'a' points to this when possible, rather than muck with malloc. */
1411 PyObject *temparray[MERGESTATE_TEMP_SIZE];
Tim Petersa64dc242002-08-01 02:13:36 +00001412} MergeState;
1413
1414/* Conceptually a MergeState's constructor. */
1415static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001416merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001419 if (has_keyfunc) {
1420 /* The temporary space for merging will need at most half the list
1421 * size rounded up. Use the minimum possible space so we can use the
1422 * rest of temparray for other things. In particular, if there is
1423 * enough extra space, listsort() will use it to store the keys.
1424 */
1425 ms->alloced = (list_size + 1) / 2;
1426
1427 /* ms->alloced describes how many keys will be stored at
1428 ms->temparray, but we also need to store the values. Hence,
1429 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1430 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1431 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1432 ms->a.values = &ms->temparray[ms->alloced];
1433 }
1434 else {
1435 ms->alloced = MERGESTATE_TEMP_SIZE;
1436 ms->a.values = NULL;
1437 }
1438 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 ms->n = 0;
1440 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001441}
1442
1443/* Free all the temp memory owned by the MergeState. This must be called
1444 * when you're done with a MergeState, and may be called before then if
1445 * you want to free the temp memory early.
1446 */
1447static void
1448merge_freemem(MergeState *ms)
1449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001451 if (ms->a.keys != ms->temparray)
1452 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001453}
1454
1455/* Ensure enough temp memory for 'need' array slots is available.
1456 * Returns 0 on success and -1 if the memory can't be gotten.
1457 */
1458static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001459merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001460{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001461 int multiplier;
1462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 assert(ms != NULL);
1464 if (need <= ms->alloced)
1465 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001466
1467 multiplier = ms->a.values != NULL ? 2 : 1;
1468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 /* Don't realloc! That can cost cycles to copy the old data, but
1470 * we don't care what's in the block.
1471 */
1472 merge_freemem(ms);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001473 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 PyErr_NoMemory();
1475 return -1;
1476 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001477 ms->a.keys = (PyObject**)PyMem_Malloc(multiplier * need
1478 * sizeof(PyObject *));
1479 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001481 if (ms->a.values != NULL)
1482 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 return 0;
1484 }
1485 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001487}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1489 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001490
Daniel Stutzbach98338222010-12-02 21:55:33 +00001491/* Merge the na elements starting at ssa with the nb elements starting at
1492 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1493 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1494 * should have na <= nb. See listsort.txt for more info. Return 0 if
1495 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001496 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001497static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001498merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1499 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001502 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 int result = -1; /* guilty until proved innocent */
1504 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001505
Daniel Stutzbach98338222010-12-02 21:55:33 +00001506 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1507 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 if (MERGE_GETMEM(ms, na) < 0)
1509 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001510 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1511 dest = ssa;
1512 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001513
Daniel Stutzbach98338222010-12-02 21:55:33 +00001514 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 --nb;
1516 if (nb == 0)
1517 goto Succeed;
1518 if (na == 1)
1519 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 min_gallop = ms->min_gallop;
1522 for (;;) {
1523 Py_ssize_t acount = 0; /* # of times A won in a row */
1524 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 /* Do the straightforward thing until (if ever) one run
1527 * appears to win consistently.
1528 */
1529 for (;;) {
1530 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001531 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 if (k) {
1533 if (k < 0)
1534 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001535 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 ++bcount;
1537 acount = 0;
1538 --nb;
1539 if (nb == 0)
1540 goto Succeed;
1541 if (bcount >= min_gallop)
1542 break;
1543 }
1544 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001545 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 ++acount;
1547 bcount = 0;
1548 --na;
1549 if (na == 1)
1550 goto CopyB;
1551 if (acount >= min_gallop)
1552 break;
1553 }
1554 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 /* One run is winning so consistently that galloping may
1557 * be a huge win. So try that, and continue galloping until
1558 * (if ever) neither run appears to be winning consistently
1559 * anymore.
1560 */
1561 ++min_gallop;
1562 do {
1563 assert(na > 1 && nb > 0);
1564 min_gallop -= min_gallop > 1;
1565 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001566 k = gallop_right(ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 acount = k;
1568 if (k) {
1569 if (k < 0)
1570 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001571 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1572 sortslice_advance(&dest, k);
1573 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 na -= k;
1575 if (na == 1)
1576 goto CopyB;
1577 /* na==0 is impossible now if the comparison
1578 * function is consistent, but we can't assume
1579 * that it is.
1580 */
1581 if (na == 0)
1582 goto Succeed;
1583 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001584 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 --nb;
1586 if (nb == 0)
1587 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001588
Daniel Stutzbach98338222010-12-02 21:55:33 +00001589 k = gallop_left(ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 bcount = k;
1591 if (k) {
1592 if (k < 0)
1593 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001594 sortslice_memmove(&dest, 0, &ssb, 0, k);
1595 sortslice_advance(&dest, k);
1596 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 nb -= k;
1598 if (nb == 0)
1599 goto Succeed;
1600 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001601 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 --na;
1603 if (na == 1)
1604 goto CopyB;
1605 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1606 ++min_gallop; /* penalize it for leaving galloping mode */
1607 ms->min_gallop = min_gallop;
1608 }
Tim Petersa64dc242002-08-01 02:13:36 +00001609Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001611Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001613 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001615CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001617 /* The last element of ssa belongs at the end of the merge. */
1618 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1619 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001621}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001622
Daniel Stutzbach98338222010-12-02 21:55:33 +00001623/* Merge the na elements starting at pa with the nb elements starting at
1624 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1625 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1626 * should have na >= nb. See listsort.txt for more info. Return 0 if
1627 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001628 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001629static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001630merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1631 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001634 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001637
Daniel Stutzbach98338222010-12-02 21:55:33 +00001638 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1639 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 if (MERGE_GETMEM(ms, nb) < 0)
1641 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001642 dest = ssb;
1643 sortslice_advance(&dest, nb-1);
1644 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1645 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001647 ssb.keys = ms->a.keys + nb - 1;
1648 if (ssb.values != NULL)
1649 ssb.values = ms->a.values + nb - 1;
1650 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001651
Daniel Stutzbach98338222010-12-02 21:55:33 +00001652 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 --na;
1654 if (na == 0)
1655 goto Succeed;
1656 if (nb == 1)
1657 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 min_gallop = ms->min_gallop;
1660 for (;;) {
1661 Py_ssize_t acount = 0; /* # of times A won in a row */
1662 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 /* Do the straightforward thing until (if ever) one run
1665 * appears to win consistently.
1666 */
1667 for (;;) {
1668 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001669 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 if (k) {
1671 if (k < 0)
1672 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001673 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 ++acount;
1675 bcount = 0;
1676 --na;
1677 if (na == 0)
1678 goto Succeed;
1679 if (acount >= min_gallop)
1680 break;
1681 }
1682 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001683 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 ++bcount;
1685 acount = 0;
1686 --nb;
1687 if (nb == 1)
1688 goto CopyA;
1689 if (bcount >= min_gallop)
1690 break;
1691 }
1692 }
Tim Petersa64dc242002-08-01 02:13:36 +00001693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 /* One run is winning so consistently that galloping may
1695 * be a huge win. So try that, and continue galloping until
1696 * (if ever) neither run appears to be winning consistently
1697 * anymore.
1698 */
1699 ++min_gallop;
1700 do {
1701 assert(na > 0 && nb > 1);
1702 min_gallop -= min_gallop > 1;
1703 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001704 k = gallop_right(ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 if (k < 0)
1706 goto Fail;
1707 k = na - k;
1708 acount = k;
1709 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001710 sortslice_advance(&dest, -k);
1711 sortslice_advance(&ssa, -k);
1712 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 na -= k;
1714 if (na == 0)
1715 goto Succeed;
1716 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001717 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 --nb;
1719 if (nb == 1)
1720 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001721
Daniel Stutzbach98338222010-12-02 21:55:33 +00001722 k = gallop_left(ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 if (k < 0)
1724 goto Fail;
1725 k = nb - k;
1726 bcount = k;
1727 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001728 sortslice_advance(&dest, -k);
1729 sortslice_advance(&ssb, -k);
1730 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 nb -= k;
1732 if (nb == 1)
1733 goto CopyA;
1734 /* nb==0 is impossible now if the comparison
1735 * function is consistent, but we can't assume
1736 * that it is.
1737 */
1738 if (nb == 0)
1739 goto Succeed;
1740 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001741 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 --na;
1743 if (na == 0)
1744 goto Succeed;
1745 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1746 ++min_gallop; /* penalize it for leaving galloping mode */
1747 ms->min_gallop = min_gallop;
1748 }
Tim Petersa64dc242002-08-01 02:13:36 +00001749Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001751Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001753 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001755CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001757 /* The first element of ssb belongs at the front of the merge. */
1758 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1759 sortslice_advance(&dest, -na);
1760 sortslice_advance(&ssa, -na);
1761 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001763}
1764
1765/* Merge the two runs at stack indices i and i+1.
1766 * Returns 0 on success, -1 on error.
1767 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001768static Py_ssize_t
1769merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001770{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001771 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 Py_ssize_t na, nb;
1773 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 assert(ms != NULL);
1776 assert(ms->n >= 2);
1777 assert(i >= 0);
1778 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001779
Daniel Stutzbach98338222010-12-02 21:55:33 +00001780 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001782 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 nb = ms->pending[i+1].len;
1784 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001785 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 /* Record the length of the combined runs; if i is the 3rd-last
1788 * run now, also slide over the last run (which isn't involved
1789 * in this merge). The current run i+1 goes away in any case.
1790 */
1791 ms->pending[i].len = na + nb;
1792 if (i == ms->n - 3)
1793 ms->pending[i+1] = ms->pending[i+2];
1794 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 /* Where does b start in a? Elements in a before that can be
1797 * ignored (already in place).
1798 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001799 k = gallop_right(*ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 if (k < 0)
1801 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001802 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 na -= k;
1804 if (na == 0)
1805 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 /* Where does a end in b? Elements in b after that can be
1808 * ignored (already in place).
1809 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001810 nb = gallop_left(ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 if (nb <= 0)
1812 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 /* Merge what remains of the runs, using a temp array with
1815 * min(na, nb) elements.
1816 */
1817 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001818 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001820 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001821}
1822
1823/* Examine the stack of runs waiting to be merged, merging adjacent runs
1824 * until the stack invariants are re-established:
1825 *
1826 * 1. len[-3] > len[-2] + len[-1]
1827 * 2. len[-2] > len[-1]
1828 *
1829 * See listsort.txt for more info.
1830 *
1831 * Returns 0 on success, -1 on error.
1832 */
1833static int
1834merge_collapse(MergeState *ms)
1835{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 assert(ms);
1839 while (ms->n > 1) {
1840 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001841 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1842 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 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);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04001970 if (keys == NULL) {
1971 PyErr_NoMemory();
1972 goto keyfunc_fail;
1973 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001975
1976 for (i = 0; i < saved_ob_size ; i++) {
1977 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
1978 NULL);
1979 if (keys[i] == NULL) {
1980 for (i=i-1 ; i>=0 ; i--)
1981 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05001982 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00001983 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001984 goto keyfunc_fail;
1985 }
1986 }
1987
1988 lo.keys = keys;
1989 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001991
Daniel Stutzbach98338222010-12-02 21:55:33 +00001992 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 nremaining = saved_ob_size;
1995 if (nremaining < 2)
1996 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00001997
Benjamin Peterson05380642010-08-23 19:35:39 +00001998 /* Reverse sort stability achieved by initially reversing the list,
1999 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002000 if (reverse) {
2001 if (keys != NULL)
2002 reverse_slice(&keys[0], &keys[saved_ob_size]);
2003 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2004 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 /* March over the array once, left to right, finding natural runs,
2007 * and extending short natural runs to minrun elements.
2008 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 minrun = merge_compute_minrun(nremaining);
2010 do {
2011 int descending;
2012 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 /* Identify next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002015 n = count_run(lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 if (n < 0)
2017 goto fail;
2018 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002019 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 /* If short, extend to min(minrun, nremaining). */
2021 if (n < minrun) {
2022 const Py_ssize_t force = nremaining <= minrun ?
2023 nremaining : minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002024 if (binarysort(lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 goto fail;
2026 n = force;
2027 }
2028 /* Push run onto pending-runs stack, and maybe merge. */
2029 assert(ms.n < MAX_MERGE_PENDING);
2030 ms.pending[ms.n].base = lo;
2031 ms.pending[ms.n].len = n;
2032 ++ms.n;
2033 if (merge_collapse(&ms) < 0)
2034 goto fail;
2035 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002036 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 nremaining -= n;
2038 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 if (merge_force_collapse(&ms) < 0)
2041 goto fail;
2042 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002043 assert(keys == NULL
2044 ? ms.pending[0].base.keys == saved_ob_item
2045 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002047 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002048
2049succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002051fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002052 if (keys != NULL) {
2053 for (i = 0; i < saved_ob_size; i++)
2054 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002055 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002056 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 if (self->allocated != -1 && result != NULL) {
2060 /* The user mucked with the list during the sort,
2061 * and we don't already have another error to report.
2062 */
2063 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2064 result = NULL;
2065 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 if (reverse && saved_ob_size > 1)
2068 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002071
Daniel Stutzbach98338222010-12-02 21:55:33 +00002072keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 final_ob_item = self->ob_item;
2074 i = Py_SIZE(self);
2075 Py_SIZE(self) = saved_ob_size;
2076 self->ob_item = saved_ob_item;
2077 self->allocated = saved_allocated;
2078 if (final_ob_item != NULL) {
2079 /* we cannot use list_clear() for this because it does not
2080 guarantee that the list is really empty when it returns */
2081 while (--i >= 0) {
2082 Py_XDECREF(final_ob_item[i]);
2083 }
2084 PyMem_FREE(final_ob_item);
2085 }
2086 Py_XINCREF(result);
2087 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002088}
Tim Peters330f9e92002-07-19 07:05:44 +00002089#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002090#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002091
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002092int
Fred Drakea2f55112000-07-09 15:16:51 +00002093PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 if (v == NULL || !PyList_Check(v)) {
2096 PyErr_BadInternalCall();
2097 return -1;
2098 }
2099 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
2100 if (v == NULL)
2101 return -1;
2102 Py_DECREF(v);
2103 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002104}
2105
Guido van Rossumb86c5492001-02-12 22:06:02 +00002106static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002107listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 if (Py_SIZE(self) > 1)
2110 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2111 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002112}
2113
Guido van Rossum84c76f51990-10-30 13:32:20 +00002114int
Fred Drakea2f55112000-07-09 15:16:51 +00002115PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 if (v == NULL || !PyList_Check(v)) {
2120 PyErr_BadInternalCall();
2121 return -1;
2122 }
2123 if (Py_SIZE(self) > 1)
2124 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2125 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002126}
2127
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002128PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002129PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 PyObject *w;
2132 PyObject **p, **q;
2133 Py_ssize_t n;
2134 if (v == NULL || !PyList_Check(v)) {
2135 PyErr_BadInternalCall();
2136 return NULL;
2137 }
2138 n = Py_SIZE(v);
2139 w = PyTuple_New(n);
2140 if (w == NULL)
2141 return NULL;
2142 p = ((PyTupleObject *)w)->ob_item;
2143 q = ((PyListObject *)v)->ob_item;
2144 while (--n >= 0) {
2145 Py_INCREF(*q);
2146 *p = *q;
2147 p++;
2148 q++;
2149 }
2150 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002151}
2152
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002153static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002154listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002157 PyObject *v;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002158
Petri Lehtinenebfaabd2011-11-06 21:02:39 +02002159 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2160 _PyEval_SliceIndex, &start,
2161 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 return NULL;
2163 if (start < 0) {
2164 start += Py_SIZE(self);
2165 if (start < 0)
2166 start = 0;
2167 }
2168 if (stop < 0) {
2169 stop += Py_SIZE(self);
2170 if (stop < 0)
2171 stop = 0;
2172 }
2173 for (i = start; i < stop && i < Py_SIZE(self); i++) {
2174 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2175 if (cmp > 0)
2176 return PyLong_FromSsize_t(i);
2177 else if (cmp < 0)
2178 return NULL;
2179 }
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002180 PyErr_Format(PyExc_ValueError, "%R is not in list", v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002182}
2183
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002184static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002185listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 Py_ssize_t count = 0;
2188 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 for (i = 0; i < Py_SIZE(self); i++) {
2191 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2192 if (cmp > 0)
2193 count++;
2194 else if (cmp < 0)
2195 return NULL;
2196 }
2197 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002198}
2199
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002200static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002201listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 for (i = 0; i < Py_SIZE(self); i++) {
2206 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2207 if (cmp > 0) {
2208 if (list_ass_slice(self, i, i+1,
2209 (PyObject *)NULL) == 0)
2210 Py_RETURN_NONE;
2211 return NULL;
2212 }
2213 else if (cmp < 0)
2214 return NULL;
2215 }
2216 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2217 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002218}
2219
Jeremy Hylton8caad492000-06-23 14:18:11 +00002220static int
2221list_traverse(PyListObject *o, visitproc visit, void *arg)
2222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 for (i = Py_SIZE(o); --i >= 0; )
2226 Py_VISIT(o->ob_item[i]);
2227 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002228}
2229
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002230static PyObject *
2231list_richcompare(PyObject *v, PyObject *w, int op)
2232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 PyListObject *vl, *wl;
2234 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002235
Brian Curtindfc80e32011-08-10 20:28:54 -05002236 if (!PyList_Check(v) || !PyList_Check(w))
2237 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 vl = (PyListObject *)v;
2240 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2243 /* Shortcut: if the lengths differ, the lists differ */
2244 PyObject *res;
2245 if (op == Py_EQ)
2246 res = Py_False;
2247 else
2248 res = Py_True;
2249 Py_INCREF(res);
2250 return res;
2251 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 /* Search for the first index where items are different */
2254 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2255 int k = PyObject_RichCompareBool(vl->ob_item[i],
2256 wl->ob_item[i], Py_EQ);
2257 if (k < 0)
2258 return NULL;
2259 if (!k)
2260 break;
2261 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2264 /* No more items to compare -- compare sizes */
2265 Py_ssize_t vs = Py_SIZE(vl);
2266 Py_ssize_t ws = Py_SIZE(wl);
2267 int cmp;
2268 PyObject *res;
2269 switch (op) {
2270 case Py_LT: cmp = vs < ws; break;
2271 case Py_LE: cmp = vs <= ws; break;
2272 case Py_EQ: cmp = vs == ws; break;
2273 case Py_NE: cmp = vs != ws; break;
2274 case Py_GT: cmp = vs > ws; break;
2275 case Py_GE: cmp = vs >= ws; break;
2276 default: return NULL; /* cannot happen */
2277 }
2278 if (cmp)
2279 res = Py_True;
2280 else
2281 res = Py_False;
2282 Py_INCREF(res);
2283 return res;
2284 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 /* We have an item that differs -- shortcuts for EQ/NE */
2287 if (op == Py_EQ) {
2288 Py_INCREF(Py_False);
2289 return Py_False;
2290 }
2291 if (op == Py_NE) {
2292 Py_INCREF(Py_True);
2293 return Py_True;
2294 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 /* Compare the final item again using the proper operator */
2297 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002298}
2299
Tim Peters6d6c1a32001-08-02 04:15:00 +00002300static int
2301list_init(PyListObject *self, PyObject *args, PyObject *kw)
2302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 PyObject *arg = NULL;
2304 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2307 return -1;
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 /* Verify list invariants established by PyType_GenericAlloc() */
2310 assert(0 <= Py_SIZE(self));
2311 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2312 assert(self->ob_item != NULL ||
2313 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 /* Empty previous contents */
2316 if (self->ob_item != NULL) {
2317 (void)list_clear(self);
2318 }
2319 if (arg != NULL) {
2320 PyObject *rv = listextend(self, arg);
2321 if (rv == NULL)
2322 return -1;
2323 Py_DECREF(rv);
2324 }
2325 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002326}
2327
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002328static PyObject *
2329list_sizeof(PyListObject *self)
2330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002332
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002333 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002335}
2336
Raymond Hettinger1021c442003-11-07 15:38:09 +00002337static PyObject *list_iter(PyObject *seq);
2338static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2339
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002340PyDoc_STRVAR(getitem_doc,
2341"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002342PyDoc_STRVAR(reversed_doc,
2343"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002344PyDoc_STRVAR(sizeof_doc,
2345"L.__sizeof__() -- size of L in memory, in bytes");
Eli Benderskycbbaa962011-02-25 05:47:53 +00002346PyDoc_STRVAR(clear_doc,
2347"L.clear() -> None -- remove all items from L");
2348PyDoc_STRVAR(copy_doc,
2349"L.copy() -> list -- a shallow copy of L");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002350PyDoc_STRVAR(append_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002351"L.append(object) -> None -- append object to end");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002352PyDoc_STRVAR(extend_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002353"L.extend(iterable) -> None -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002354PyDoc_STRVAR(insert_doc,
2355"L.insert(index, object) -- insert object before index");
2356PyDoc_STRVAR(pop_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002357"L.pop([index]) -> item -- remove and return item at index (default last).\n"
2358"Raises IndexError if list is empty or index is out of range.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002359PyDoc_STRVAR(remove_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002360"L.remove(value) -> None -- remove first occurrence of value.\n"
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002361"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002362PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002363"L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2364"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002365PyDoc_STRVAR(count_doc,
2366"L.count(value) -> integer -- return number of occurrences of value");
2367PyDoc_STRVAR(reverse_doc,
2368"L.reverse() -- reverse *IN PLACE*");
2369PyDoc_STRVAR(sort_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002370"L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002371
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002372static PyObject *list_subscript(PyListObject*, PyObject*);
2373
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002374static PyMethodDef list_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
2376 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
2377 {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002378 {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc},
2379 {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 {"append", (PyCFunction)listappend, METH_O, append_doc},
2381 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002382 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
2384 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
2385 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
2386 {"count", (PyCFunction)listcount, METH_O, count_doc},
2387 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
2388 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
2389 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002390};
2391
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002392static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 (lenfunc)list_length, /* sq_length */
2394 (binaryfunc)list_concat, /* sq_concat */
2395 (ssizeargfunc)list_repeat, /* sq_repeat */
2396 (ssizeargfunc)list_item, /* sq_item */
2397 0, /* sq_slice */
2398 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2399 0, /* sq_ass_slice */
2400 (objobjproc)list_contains, /* sq_contains */
2401 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2402 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002403};
2404
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002405PyDoc_STRVAR(list_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002406"list() -> new empty list\n"
2407"list(iterable) -> new list initialized from iterable's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002408
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002409static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002410list_subscript(PyListObject* self, PyObject* item)
2411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 if (PyIndex_Check(item)) {
2413 Py_ssize_t i;
2414 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2415 if (i == -1 && PyErr_Occurred())
2416 return NULL;
2417 if (i < 0)
2418 i += PyList_GET_SIZE(self);
2419 return list_item(self, i);
2420 }
2421 else if (PySlice_Check(item)) {
2422 Py_ssize_t start, stop, step, slicelength, cur, i;
2423 PyObject* result;
2424 PyObject* it;
2425 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002426
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002427 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 &start, &stop, &step, &slicelength) < 0) {
2429 return NULL;
2430 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 if (slicelength <= 0) {
2433 return PyList_New(0);
2434 }
2435 else if (step == 1) {
2436 return list_slice(self, start, stop);
2437 }
2438 else {
2439 result = PyList_New(slicelength);
2440 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 src = self->ob_item;
2443 dest = ((PyListObject *)result)->ob_item;
2444 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002445 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 it = src[cur];
2447 Py_INCREF(it);
2448 dest[i] = it;
2449 }
Tim Peters3b01a122002-07-19 02:35:45 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 return result;
2452 }
2453 }
2454 else {
2455 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002456 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 item->ob_type->tp_name);
2458 return NULL;
2459 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002460}
2461
Tim Peters3b01a122002-07-19 02:35:45 +00002462static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002463list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 if (PyIndex_Check(item)) {
2466 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2467 if (i == -1 && PyErr_Occurred())
2468 return -1;
2469 if (i < 0)
2470 i += PyList_GET_SIZE(self);
2471 return list_ass_item(self, i, value);
2472 }
2473 else if (PySlice_Check(item)) {
2474 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002475
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002476 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 &start, &stop, &step, &slicelength) < 0) {
2478 return -1;
2479 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 if (step == 1)
2482 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 /* Make sure s[5:2] = [..] inserts at the right place:
2485 before 5, not before 2. */
2486 if ((step < 0 && start < stop) ||
2487 (step > 0 && start > stop))
2488 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 if (value == NULL) {
2491 /* delete slice */
2492 PyObject **garbage;
2493 size_t cur;
2494 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002495 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 if (slicelength <= 0)
2498 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 if (step < 0) {
2501 stop = start + 1;
2502 start = stop + step*(slicelength - 1) - 1;
2503 step = -step;
2504 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 assert((size_t)slicelength <=
2507 PY_SIZE_MAX / sizeof(PyObject*));
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00002508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 garbage = (PyObject**)
2510 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2511 if (!garbage) {
2512 PyErr_NoMemory();
2513 return -1;
2514 }
Tim Peters3b01a122002-07-19 02:35:45 +00002515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 /* drawing pictures might help understand these for
2517 loops. Basically, we memmove the parts of the
2518 list that are *not* part of the slice: step-1
2519 items for each item that is part of the slice,
2520 and then tail end of the list that was not
2521 covered by the slice */
2522 for (cur = start, i = 0;
2523 cur < (size_t)stop;
2524 cur += step, i++) {
2525 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 if (cur + step >= (size_t)Py_SIZE(self)) {
2530 lim = Py_SIZE(self) - cur - 1;
2531 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 memmove(self->ob_item + cur - i,
2534 self->ob_item + cur + 1,
2535 lim * sizeof(PyObject *));
2536 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002537 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 if (cur < (size_t)Py_SIZE(self)) {
2539 memmove(self->ob_item + cur - slicelength,
2540 self->ob_item + cur,
2541 (Py_SIZE(self) - cur) *
2542 sizeof(PyObject *));
2543 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002546 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 for (i = 0; i < slicelength; i++) {
2549 Py_DECREF(garbage[i]);
2550 }
2551 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002552
Victor Stinner35f28032013-11-21 12:16:35 +01002553 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 }
2555 else {
2556 /* assign slice */
2557 PyObject *ins, *seq;
2558 PyObject **garbage, **seqitems, **selfitems;
2559 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 /* protect against a[::-1] = a */
2562 if (self == (PyListObject*)value) {
2563 seq = list_slice((PyListObject*)value, 0,
2564 PyList_GET_SIZE(value));
2565 }
2566 else {
2567 seq = PySequence_Fast(value,
2568 "must assign iterable "
2569 "to extended slice");
2570 }
2571 if (!seq)
2572 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2575 PyErr_Format(PyExc_ValueError,
2576 "attempt to assign sequence of "
2577 "size %zd to extended slice of "
2578 "size %zd",
2579 PySequence_Fast_GET_SIZE(seq),
2580 slicelength);
2581 Py_DECREF(seq);
2582 return -1;
2583 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 if (!slicelength) {
2586 Py_DECREF(seq);
2587 return 0;
2588 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 garbage = (PyObject**)
2591 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2592 if (!garbage) {
2593 Py_DECREF(seq);
2594 PyErr_NoMemory();
2595 return -1;
2596 }
Tim Peters3b01a122002-07-19 02:35:45 +00002597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 selfitems = self->ob_item;
2599 seqitems = PySequence_Fast_ITEMS(seq);
2600 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002601 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 garbage[i] = selfitems[cur];
2603 ins = seqitems[i];
2604 Py_INCREF(ins);
2605 selfitems[cur] = ins;
2606 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 for (i = 0; i < slicelength; i++) {
2609 Py_DECREF(garbage[i]);
2610 }
Tim Peters3b01a122002-07-19 02:35:45 +00002611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 PyMem_FREE(garbage);
2613 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 return 0;
2616 }
2617 }
2618 else {
2619 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002620 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 item->ob_type->tp_name);
2622 return -1;
2623 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002624}
2625
2626static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 (lenfunc)list_length,
2628 (binaryfunc)list_subscript,
2629 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002630};
2631
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002632PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2634 "list",
2635 sizeof(PyListObject),
2636 0,
2637 (destructor)list_dealloc, /* tp_dealloc */
2638 0, /* tp_print */
2639 0, /* tp_getattr */
2640 0, /* tp_setattr */
2641 0, /* tp_reserved */
2642 (reprfunc)list_repr, /* tp_repr */
2643 0, /* tp_as_number */
2644 &list_as_sequence, /* tp_as_sequence */
2645 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002646 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 0, /* tp_call */
2648 0, /* tp_str */
2649 PyObject_GenericGetAttr, /* tp_getattro */
2650 0, /* tp_setattro */
2651 0, /* tp_as_buffer */
2652 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2653 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2654 list_doc, /* tp_doc */
2655 (traverseproc)list_traverse, /* tp_traverse */
2656 (inquiry)list_clear, /* tp_clear */
2657 list_richcompare, /* tp_richcompare */
2658 0, /* tp_weaklistoffset */
2659 list_iter, /* tp_iter */
2660 0, /* tp_iternext */
2661 list_methods, /* tp_methods */
2662 0, /* tp_members */
2663 0, /* tp_getset */
2664 0, /* tp_base */
2665 0, /* tp_dict */
2666 0, /* tp_descr_get */
2667 0, /* tp_descr_set */
2668 0, /* tp_dictoffset */
2669 (initproc)list_init, /* tp_init */
2670 PyType_GenericAlloc, /* tp_alloc */
2671 PyType_GenericNew, /* tp_new */
2672 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002673};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002674
2675
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002676/*********************** List Iterator **************************/
2677
2678typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02002680 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002682} listiterobject;
2683
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002684static PyObject *list_iter(PyObject *);
2685static void listiter_dealloc(listiterobject *);
2686static int listiter_traverse(listiterobject *, visitproc, void *);
2687static PyObject *listiter_next(listiterobject *);
2688static PyObject *listiter_len(listiterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002689static PyObject *listiter_reduce_general(void *_it, int forward);
2690static PyObject *listiter_reduce(listiterobject *);
2691static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002692
Armin Rigof5b3e362006-02-11 21:32:43 +00002693PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002694PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2695PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002696
2697static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002699 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
2700 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002702};
2703
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002704PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2706 "list_iterator", /* tp_name */
2707 sizeof(listiterobject), /* tp_basicsize */
2708 0, /* tp_itemsize */
2709 /* methods */
2710 (destructor)listiter_dealloc, /* tp_dealloc */
2711 0, /* tp_print */
2712 0, /* tp_getattr */
2713 0, /* tp_setattr */
2714 0, /* tp_reserved */
2715 0, /* tp_repr */
2716 0, /* tp_as_number */
2717 0, /* tp_as_sequence */
2718 0, /* tp_as_mapping */
2719 0, /* tp_hash */
2720 0, /* tp_call */
2721 0, /* tp_str */
2722 PyObject_GenericGetAttr, /* tp_getattro */
2723 0, /* tp_setattro */
2724 0, /* tp_as_buffer */
2725 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2726 0, /* tp_doc */
2727 (traverseproc)listiter_traverse, /* tp_traverse */
2728 0, /* tp_clear */
2729 0, /* tp_richcompare */
2730 0, /* tp_weaklistoffset */
2731 PyObject_SelfIter, /* tp_iter */
2732 (iternextfunc)listiter_next, /* tp_iternext */
2733 listiter_methods, /* tp_methods */
2734 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002735};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002736
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002737
2738static PyObject *
2739list_iter(PyObject *seq)
2740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 if (!PyList_Check(seq)) {
2744 PyErr_BadInternalCall();
2745 return NULL;
2746 }
2747 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2748 if (it == NULL)
2749 return NULL;
2750 it->it_index = 0;
2751 Py_INCREF(seq);
2752 it->it_seq = (PyListObject *)seq;
2753 _PyObject_GC_TRACK(it);
2754 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002755}
2756
2757static void
2758listiter_dealloc(listiterobject *it)
2759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 _PyObject_GC_UNTRACK(it);
2761 Py_XDECREF(it->it_seq);
2762 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002763}
2764
2765static int
2766listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 Py_VISIT(it->it_seq);
2769 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002770}
2771
2772static PyObject *
2773listiter_next(listiterobject *it)
2774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 PyListObject *seq;
2776 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 assert(it != NULL);
2779 seq = it->it_seq;
2780 if (seq == NULL)
2781 return NULL;
2782 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 if (it->it_index < PyList_GET_SIZE(seq)) {
2785 item = PyList_GET_ITEM(seq, it->it_index);
2786 ++it->it_index;
2787 Py_INCREF(item);
2788 return item;
2789 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002792 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002794}
2795
2796static PyObject *
2797listiter_len(listiterobject *it)
2798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 Py_ssize_t len;
2800 if (it->it_seq) {
2801 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2802 if (len >= 0)
2803 return PyLong_FromSsize_t(len);
2804 }
2805 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002806}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002807
2808static PyObject *
2809listiter_reduce(listiterobject *it)
2810{
2811 return listiter_reduce_general(it, 1);
2812}
2813
2814static PyObject *
2815listiter_setstate(listiterobject *it, PyObject *state)
2816{
Victor Stinner7660b882013-06-24 23:59:24 +02002817 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002818 if (index == -1 && PyErr_Occurred())
2819 return NULL;
2820 if (it->it_seq != NULL) {
2821 if (index < 0)
2822 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002823 else if (index > PyList_GET_SIZE(it->it_seq))
2824 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002825 it->it_index = index;
2826 }
2827 Py_RETURN_NONE;
2828}
2829
Raymond Hettinger1021c442003-11-07 15:38:09 +00002830/*********************** List Reverse Iterator **************************/
2831
2832typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 PyObject_HEAD
2834 Py_ssize_t it_index;
2835 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002836} listreviterobject;
2837
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002838static PyObject *list_reversed(PyListObject *, PyObject *);
2839static void listreviter_dealloc(listreviterobject *);
2840static int listreviter_traverse(listreviterobject *, visitproc, void *);
2841static PyObject *listreviter_next(listreviterobject *);
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002842static PyObject *listreviter_len(listreviterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002843static PyObject *listreviter_reduce(listreviterobject *);
2844static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002845
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002846static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002848 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
2849 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002851};
2852
Raymond Hettinger1021c442003-11-07 15:38:09 +00002853PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2855 "list_reverseiterator", /* tp_name */
2856 sizeof(listreviterobject), /* tp_basicsize */
2857 0, /* tp_itemsize */
2858 /* methods */
2859 (destructor)listreviter_dealloc, /* tp_dealloc */
2860 0, /* tp_print */
2861 0, /* tp_getattr */
2862 0, /* tp_setattr */
2863 0, /* tp_reserved */
2864 0, /* tp_repr */
2865 0, /* tp_as_number */
2866 0, /* tp_as_sequence */
2867 0, /* tp_as_mapping */
2868 0, /* tp_hash */
2869 0, /* tp_call */
2870 0, /* tp_str */
2871 PyObject_GenericGetAttr, /* tp_getattro */
2872 0, /* tp_setattro */
2873 0, /* tp_as_buffer */
2874 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2875 0, /* tp_doc */
2876 (traverseproc)listreviter_traverse, /* tp_traverse */
2877 0, /* tp_clear */
2878 0, /* tp_richcompare */
2879 0, /* tp_weaklistoffset */
2880 PyObject_SelfIter, /* tp_iter */
2881 (iternextfunc)listreviter_next, /* tp_iternext */
2882 listreviter_methods, /* tp_methods */
2883 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00002884};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002885
2886static PyObject *
2887list_reversed(PyListObject *seq, PyObject *unused)
2888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2892 if (it == NULL)
2893 return NULL;
2894 assert(PyList_Check(seq));
2895 it->it_index = PyList_GET_SIZE(seq) - 1;
2896 Py_INCREF(seq);
2897 it->it_seq = seq;
2898 PyObject_GC_Track(it);
2899 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002900}
2901
2902static void
2903listreviter_dealloc(listreviterobject *it)
2904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 PyObject_GC_UnTrack(it);
2906 Py_XDECREF(it->it_seq);
2907 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002908}
2909
2910static int
2911listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 Py_VISIT(it->it_seq);
2914 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002915}
2916
2917static PyObject *
2918listreviter_next(listreviterobject *it)
2919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002921 Py_ssize_t index;
2922 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002923
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002924 assert(it != NULL);
2925 seq = it->it_seq;
2926 if (seq == NULL) {
2927 return NULL;
2928 }
2929 assert(PyList_Check(seq));
2930
2931 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2933 item = PyList_GET_ITEM(seq, index);
2934 it->it_index--;
2935 Py_INCREF(item);
2936 return item;
2937 }
2938 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002939 it->it_seq = NULL;
2940 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002942}
2943
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002944static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002945listreviter_len(listreviterobject *it)
2946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 Py_ssize_t len = it->it_index + 1;
2948 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2949 len = 0;
2950 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002951}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002952
2953static PyObject *
2954listreviter_reduce(listreviterobject *it)
2955{
2956 return listiter_reduce_general(it, 0);
2957}
2958
2959static PyObject *
2960listreviter_setstate(listreviterobject *it, PyObject *state)
2961{
2962 Py_ssize_t index = PyLong_AsSsize_t(state);
2963 if (index == -1 && PyErr_Occurred())
2964 return NULL;
2965 if (it->it_seq != NULL) {
2966 if (index < -1)
2967 index = -1;
2968 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
2969 index = PyList_GET_SIZE(it->it_seq) - 1;
2970 it->it_index = index;
2971 }
2972 Py_RETURN_NONE;
2973}
2974
2975/* common pickling support */
2976
2977static PyObject *
2978listiter_reduce_general(void *_it, int forward)
2979{
2980 PyObject *list;
2981
2982 /* the objects are not the same, index is of different types! */
2983 if (forward) {
2984 listiterobject *it = (listiterobject *)_it;
2985 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02002986 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002987 it->it_seq, it->it_index);
2988 } else {
2989 listreviterobject *it = (listreviterobject *)_it;
2990 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +02002991 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("reversed"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002992 it->it_seq, it->it_index);
2993 }
2994 /* empty iterator, create an empty list */
2995 list = PyList_New(0);
2996 if (list == NULL)
2997 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02002998 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002999}