blob: 3d4a187f692902bd0875e2be26cf103793f56d04 [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06004#include "internal/pystate.h"
Antoine Pitrou0197ff92012-03-22 14:38:16 +01005#include "accu.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00006
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00007#ifdef STDC_HEADERS
8#include <stddef.h>
9#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000010#include <sys/types.h> /* For size_t */
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000011#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012
Serhiy Storchakafdd42c42017-03-11 09:19:20 +020013/*[clinic input]
14class list "PyListObject *" "&PyList_Type"
15[clinic start generated code]*/
16/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f9b222678f9f71e0]*/
17
18#include "clinic/listobject.c.h"
19
Tim Peters8d9eb102004-07-31 02:24:20 +000020/* Ensure ob_item has room for at least newsize elements, and set
21 * ob_size to newsize. If newsize > ob_size on entry, the content
22 * of the new slots at exit is undefined heap trash; it's the caller's
Ezio Melotti13925002011-03-16 11:05:33 +020023 * responsibility to overwrite them with sane values.
Tim Peters8d9eb102004-07-31 02:24:20 +000024 * The number of allocated elements may grow, shrink, or stay the same.
25 * Failure is impossible if newsize <= self.allocated on entry, although
26 * that partly relies on an assumption that the system realloc() never
27 * fails when passed a number of bytes <= the number of bytes last
28 * allocated (the C standard doesn't guarantee this, but it's hard to
29 * imagine a realloc implementation where it wouldn't be true).
30 * Note that self->ob_item may change, and even if newsize is less
31 * than ob_size on entry.
32 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000033static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000034list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 PyObject **items;
Xiang Zhang4cee0492017-02-22 12:32:30 +080037 size_t new_allocated, num_allocated_bytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 /* Bypass realloc() when a previous overallocation is large enough
41 to accommodate the newsize. If the newsize falls lower than half
42 the allocated size, then proceed with the realloc() to shrink the list.
43 */
44 if (allocated >= newsize && newsize >= (allocated >> 1)) {
45 assert(self->ob_item != NULL || newsize == 0);
46 Py_SIZE(self) = newsize;
47 return 0;
48 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 /* This over-allocates proportional to the list size, making room
51 * for additional growth. The over-allocation is mild, but is
52 * enough to give linear-time amortized behavior over a long
53 * sequence of appends() in the presence of a poorly-performing
54 * system realloc().
55 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
Xiang Zhang4cee0492017-02-22 12:32:30 +080056 * Note: new_allocated won't overflow because the largest possible value
57 * is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 */
Xiang Zhang4cee0492017-02-22 12:32:30 +080059 new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
60 if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 PyErr_NoMemory();
62 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (newsize == 0)
66 new_allocated = 0;
Xiang Zhang4cee0492017-02-22 12:32:30 +080067 num_allocated_bytes = new_allocated * sizeof(PyObject *);
68 items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 if (items == NULL) {
70 PyErr_NoMemory();
71 return -1;
72 }
73 self->ob_item = items;
74 Py_SIZE(self) = newsize;
75 self->allocated = new_allocated;
76 return 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000077}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000078
Christian Heimes77c02eb2008-02-09 02:18:51 +000079/* Debug statistic to compare allocations with reuse through the free list */
80#undef SHOW_ALLOC_COUNT
81#ifdef SHOW_ALLOC_COUNT
82static size_t count_alloc = 0;
83static size_t count_reuse = 0;
84
85static void
86show_alloc(void)
87{
Victor Stinnercaba55b2018-08-03 15:33:52 +020088 PyInterpreterState *interp = _PyInterpreterState_Get();
Eddie Elizondo745dc652018-02-21 20:55:18 -080089 if (!interp->core_config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030090 return;
Victor Stinner25420fe2017-11-20 18:12:22 -080091 }
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
94 count_alloc);
95 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
96 "d\n", count_reuse);
97 fprintf(stderr, "%.2f%% reuse rate\n\n",
98 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +000099}
100#endif
101
Raymond Hettinger0468e412004-05-05 05:37:53 +0000102/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000103#ifndef PyList_MAXFREELIST
104#define PyList_MAXFREELIST 80
105#endif
106static PyListObject *free_list[PyList_MAXFREELIST];
107static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000108
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100109int
110PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100113 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 while (numfree) {
115 op = free_list[--numfree];
116 assert(PyList_CheckExact(op));
117 PyObject_GC_Del(op);
118 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100119 return ret;
120}
121
122void
123PyList_Fini(void)
124{
125 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000126}
127
David Malcolm49526f42012-06-22 14:55:41 -0400128/* Print summary info about the state of the optimized allocator */
129void
130_PyList_DebugMallocStats(FILE *out)
131{
132 _PyDebugAllocatorStats(out,
133 "free PyListObject",
134 numfree, sizeof(PyListObject));
135}
136
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000137PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000138PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 PyListObject *op;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000141#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 static int initialized = 0;
143 if (!initialized) {
144 Py_AtExit(show_alloc);
145 initialized = 1;
146 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000147#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 if (size < 0) {
150 PyErr_BadInternalCall();
151 return NULL;
152 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (numfree) {
154 numfree--;
155 op = free_list[numfree];
156 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000157#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000159#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 } else {
161 op = PyObject_GC_New(PyListObject, &PyList_Type);
162 if (op == NULL)
163 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000164#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000166#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 }
168 if (size <= 0)
169 op->ob_item = NULL;
170 else {
Mark Dickinson5d132382016-08-21 08:55:15 +0100171 op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 if (op->ob_item == NULL) {
173 Py_DECREF(op);
174 return PyErr_NoMemory();
175 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 }
177 Py_SIZE(op) = size;
178 op->allocated = size;
179 _PyObject_GC_TRACK(op);
180 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181}
182
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500183static PyObject *
184list_new_prealloc(Py_ssize_t size)
185{
186 PyListObject *op = (PyListObject *) PyList_New(0);
187 if (size == 0 || op == NULL) {
188 return (PyObject *) op;
189 }
190 assert(op->ob_item == NULL);
191 op->ob_item = PyMem_New(PyObject *, size);
192 if (op->ob_item == NULL) {
193 Py_DECREF(op);
194 return PyErr_NoMemory();
195 }
196 op->allocated = size;
197 return (PyObject *) op;
198}
199
Martin v. Löwis18e16552006-02-15 17:27:45 +0000200Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000201PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 if (!PyList_Check(op)) {
204 PyErr_BadInternalCall();
205 return -1;
206 }
207 else
208 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209}
210
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000211static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000212
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000213PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000214PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (!PyList_Check(op)) {
217 PyErr_BadInternalCall();
218 return NULL;
219 }
220 if (i < 0 || i >= Py_SIZE(op)) {
221 if (indexerr == NULL) {
222 indexerr = PyUnicode_FromString(
223 "list index out of range");
224 if (indexerr == NULL)
225 return NULL;
226 }
227 PyErr_SetObject(PyExc_IndexError, indexerr);
228 return NULL;
229 }
230 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231}
232
233int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200234PyList_SetItem(PyObject *op, Py_ssize_t i,
235 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200237 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if (!PyList_Check(op)) {
239 Py_XDECREF(newitem);
240 PyErr_BadInternalCall();
241 return -1;
242 }
243 if (i < 0 || i >= Py_SIZE(op)) {
244 Py_XDECREF(newitem);
245 PyErr_SetString(PyExc_IndexError,
246 "list assignment index out of range");
247 return -1;
248 }
249 p = ((PyListObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300250 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252}
253
254static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000255ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 Py_ssize_t i, n = Py_SIZE(self);
258 PyObject **items;
259 if (v == NULL) {
260 PyErr_BadInternalCall();
261 return -1;
262 }
263 if (n == PY_SSIZE_T_MAX) {
264 PyErr_SetString(PyExc_OverflowError,
265 "cannot add more objects to list");
266 return -1;
267 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000268
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800269 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 if (where < 0) {
273 where += n;
274 if (where < 0)
275 where = 0;
276 }
277 if (where > n)
278 where = n;
279 items = self->ob_item;
280 for (i = n; --i >= where; )
281 items[i+1] = items[i];
282 Py_INCREF(v);
283 items[where] = v;
284 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285}
286
287int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000288PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 if (!PyList_Check(op)) {
291 PyErr_BadInternalCall();
292 return -1;
293 }
294 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295}
296
Raymond Hettinger40a03822004-04-12 13:05:09 +0000297static int
298app1(PyListObject *self, PyObject *v)
299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 assert (v != NULL);
303 if (n == PY_SSIZE_T_MAX) {
304 PyErr_SetString(PyExc_OverflowError,
305 "cannot add more objects to list");
306 return -1;
307 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000308
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800309 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 Py_INCREF(v);
313 PyList_SET_ITEM(self, n, v);
314 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000315}
316
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317int
Fred Drakea2f55112000-07-09 15:16:51 +0000318PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 if (PyList_Check(op) && (newitem != NULL))
321 return app1((PyListObject *)op, newitem);
322 PyErr_BadInternalCall();
323 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324}
325
326/* Methods */
327
328static void
Fred Drakea2f55112000-07-09 15:16:51 +0000329list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 Py_ssize_t i;
332 PyObject_GC_UnTrack(op);
333 Py_TRASHCAN_SAFE_BEGIN(op)
334 if (op->ob_item != NULL) {
335 /* Do it backwards, for Christian Tismer.
336 There's a simple test case where somehow this reduces
337 thrashing when a *very* large list is created and
338 immediately deleted. */
339 i = Py_SIZE(op);
340 while (--i >= 0) {
341 Py_XDECREF(op->ob_item[i]);
342 }
343 PyMem_FREE(op->ob_item);
344 }
345 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
346 free_list[numfree++] = op;
347 else
348 Py_TYPE(op)->tp_free((PyObject *)op);
349 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350}
351
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000352static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000353list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 Py_ssize_t i;
Victor Stinner5c733472013-11-18 21:11:57 +0100356 PyObject *s;
Victor Stinner5c733472013-11-18 21:11:57 +0100357 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200358
359 if (Py_SIZE(v) == 0) {
360 return PyUnicode_FromString("[]");
361 }
362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 i = Py_ReprEnter((PyObject*)v);
364 if (i != 0) {
365 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
366 }
Tim Petersa7259592001-06-16 05:11:17 +0000367
Victor Stinner5c733472013-11-18 21:11:57 +0100368 _PyUnicodeWriter_Init(&writer);
369 writer.overallocate = 1;
Victor Stinnerb8fb1972013-11-18 22:15:44 +0100370 /* "[" + "1" + ", 2" * (len - 1) + "]" */
371 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +0000372
Victor Stinner5c733472013-11-18 21:11:57 +0100373 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200374 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 /* Do repr() on each element. Note that this may mutate the list,
377 so must refetch the list size on each iteration. */
378 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100379 if (i > 0) {
Victor Stinner4a587072013-11-19 12:54:53 +0100380 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
Victor Stinner5c733472013-11-18 21:11:57 +0100381 goto error;
382 }
383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner5c733472013-11-18 21:11:57 +0100385 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200386 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100387
388 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
389 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200390 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100391 }
392 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 }
Victor Stinner5c733472013-11-18 21:11:57 +0100394
Victor Stinner4d3f1092013-11-19 12:09:00 +0100395 writer.overallocate = 0;
Victor Stinner5c733472013-11-18 21:11:57 +0100396 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200397 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100400 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200401
402error:
Victor Stinner5c733472013-11-18 21:11:57 +0100403 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200404 Py_ReprLeave((PyObject *)v);
405 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406}
407
Martin v. Löwis18e16552006-02-15 17:27:45 +0000408static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000409list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412}
413
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000414static int
Fred Drakea2f55112000-07-09 15:16:51 +0000415list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 Py_ssize_t i;
418 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
421 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
422 Py_EQ);
423 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000424}
425
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000426static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000427list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (i < 0 || i >= Py_SIZE(a)) {
430 if (indexerr == NULL) {
431 indexerr = PyUnicode_FromString(
432 "list index out of range");
433 if (indexerr == NULL)
434 return NULL;
435 }
436 PyErr_SetObject(PyExc_IndexError, indexerr);
437 return NULL;
438 }
439 Py_INCREF(a->ob_item[i]);
440 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441}
442
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000444list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 PyListObject *np;
447 PyObject **src, **dest;
448 Py_ssize_t i, len;
449 if (ilow < 0)
450 ilow = 0;
451 else if (ilow > Py_SIZE(a))
452 ilow = Py_SIZE(a);
453 if (ihigh < ilow)
454 ihigh = ilow;
455 else if (ihigh > Py_SIZE(a))
456 ihigh = Py_SIZE(a);
457 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500458 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (np == NULL)
460 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 src = a->ob_item + ilow;
463 dest = np->ob_item;
464 for (i = 0; i < len; i++) {
465 PyObject *v = src[i];
466 Py_INCREF(v);
467 dest[i] = v;
468 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500469 Py_SIZE(np) = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471}
472
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000474PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 if (!PyList_Check(a)) {
477 PyErr_BadInternalCall();
478 return NULL;
479 }
480 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000481}
482
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000484list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 Py_ssize_t size;
487 Py_ssize_t i;
488 PyObject **src, **dest;
489 PyListObject *np;
490 if (!PyList_Check(bb)) {
491 PyErr_Format(PyExc_TypeError,
492 "can only concatenate list (not \"%.200s\") to list",
493 bb->ob_type->tp_name);
494 return NULL;
495 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000497 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000499 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500500 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if (np == NULL) {
502 return NULL;
503 }
504 src = a->ob_item;
505 dest = np->ob_item;
506 for (i = 0; i < Py_SIZE(a); i++) {
507 PyObject *v = src[i];
508 Py_INCREF(v);
509 dest[i] = v;
510 }
511 src = b->ob_item;
512 dest = np->ob_item + Py_SIZE(a);
513 for (i = 0; i < Py_SIZE(b); i++) {
514 PyObject *v = src[i];
515 Py_INCREF(v);
516 dest[i] = v;
517 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500518 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520#undef b
521}
522
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000524list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 Py_ssize_t i, j;
527 Py_ssize_t size;
528 PyListObject *np;
529 PyObject **p, **items;
530 PyObject *elem;
531 if (n < 0)
532 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100533 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100535 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 if (size == 0)
537 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500538 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 if (np == NULL)
540 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500543 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 elem = a->ob_item[0];
545 for (i = 0; i < n; i++) {
546 items[i] = elem;
547 Py_INCREF(elem);
548 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500550 else {
551 p = np->ob_item;
552 items = a->ob_item;
553 for (i = 0; i < n; i++) {
554 for (j = 0; j < Py_SIZE(a); j++) {
555 *p = items[j];
556 Py_INCREF(*p);
557 p++;
558 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 }
560 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500561 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000563}
564
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200566_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 Py_ssize_t i;
569 PyObject **item = a->ob_item;
570 if (item != NULL) {
571 /* Because XDECREF can recursively invoke operations on
572 this list, we make it empty first. */
573 i = Py_SIZE(a);
574 Py_SIZE(a) = 0;
575 a->ob_item = NULL;
576 a->allocated = 0;
577 while (--i >= 0) {
578 Py_XDECREF(item[i]);
579 }
580 PyMem_FREE(item);
581 }
582 /* Never fails; the return value can be ignored.
583 Note that there is no guarantee that the list is actually empty
584 at this point, because XDECREF may have populated it again! */
585 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000586}
587
Tim Peters8fc4a912004-07-31 21:53:19 +0000588/* a[ilow:ihigh] = v if v != NULL.
589 * del a[ilow:ihigh] if v == NULL.
590 *
591 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
592 * guaranteed the call cannot fail.
593 */
Armin Rigo93677f02004-07-29 12:40:23 +0000594static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000595list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 /* Because [X]DECREF can recursively invoke list operations on
598 this list, we must postpone all [X]DECREF activity until
599 after the list is back in its canonical shape. Therefore
600 we must allocate an additional array, 'recycle', into which
601 we temporarily copy the items that are deleted from the
602 list. :-( */
603 PyObject *recycle_on_stack[8];
604 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
605 PyObject **item;
606 PyObject **vitem = NULL;
607 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
608 Py_ssize_t n; /* # of elements in replacement list */
609 Py_ssize_t norig; /* # of elements in list getting replaced */
610 Py_ssize_t d; /* Change in size */
611 Py_ssize_t k;
612 size_t s;
613 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000614#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 if (v == NULL)
616 n = 0;
617 else {
618 if (a == b) {
619 /* Special case "a[i:j] = a" -- copy b first */
620 v = list_slice(b, 0, Py_SIZE(b));
621 if (v == NULL)
622 return result;
623 result = list_ass_slice(a, ilow, ihigh, v);
624 Py_DECREF(v);
625 return result;
626 }
627 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
628 if(v_as_SF == NULL)
629 goto Error;
630 n = PySequence_Fast_GET_SIZE(v_as_SF);
631 vitem = PySequence_Fast_ITEMS(v_as_SF);
632 }
633 if (ilow < 0)
634 ilow = 0;
635 else if (ilow > Py_SIZE(a))
636 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 if (ihigh < ilow)
639 ihigh = ilow;
640 else if (ihigh > Py_SIZE(a))
641 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 norig = ihigh - ilow;
644 assert(norig >= 0);
645 d = n - norig;
646 if (Py_SIZE(a) + d == 0) {
647 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200648 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 }
650 item = a->ob_item;
651 /* recycle the items that we are about to remove */
652 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700653 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
654 if (s) {
655 if (s > sizeof(recycle_on_stack)) {
656 recycle = (PyObject **)PyMem_MALLOC(s);
657 if (recycle == NULL) {
658 PyErr_NoMemory();
659 goto Error;
660 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700662 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200666 Py_ssize_t tail;
667 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
668 memmove(&item[ihigh+d], &item[ihigh], tail);
669 if (list_resize(a, Py_SIZE(a) + d) < 0) {
670 memmove(&item[ihigh], &item[ihigh+d], tail);
671 memcpy(&item[ilow], recycle, s);
672 goto Error;
673 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 item = a->ob_item;
675 }
676 else if (d > 0) { /* Insert d items */
677 k = Py_SIZE(a);
678 if (list_resize(a, k+d) < 0)
679 goto Error;
680 item = a->ob_item;
681 memmove(&item[ihigh+d], &item[ihigh],
682 (k - ihigh)*sizeof(PyObject *));
683 }
684 for (k = 0; k < n; k++, ilow++) {
685 PyObject *w = vitem[k];
686 Py_XINCREF(w);
687 item[ilow] = w;
688 }
689 for (k = norig - 1; k >= 0; --k)
690 Py_XDECREF(recycle[k]);
691 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000692 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 if (recycle != recycle_on_stack)
694 PyMem_FREE(recycle);
695 Py_XDECREF(v_as_SF);
696 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000697#undef b
698}
699
Guido van Rossum234f9421993-06-17 12:35:49 +0000700int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000701PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 if (!PyList_Check(a)) {
704 PyErr_BadInternalCall();
705 return -1;
706 }
707 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000708}
709
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000710static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000711list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 PyObject **items;
714 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000715
716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 size = PyList_GET_SIZE(self);
718 if (size == 0 || n == 1) {
719 Py_INCREF(self);
720 return (PyObject *)self;
721 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 if (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200724 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 Py_INCREF(self);
726 return (PyObject *)self;
727 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 if (size > PY_SSIZE_T_MAX / n) {
730 return PyErr_NoMemory();
731 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000732
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800733 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 p = size;
737 items = self->ob_item;
738 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
739 for (j = 0; j < size; j++) {
740 PyObject *o = items[j];
741 Py_INCREF(o);
742 items[p++] = o;
743 }
744 }
745 Py_INCREF(self);
746 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000747}
748
Guido van Rossum4a450d01991-04-03 19:05:18 +0000749static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000750list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 if (i < 0 || i >= Py_SIZE(a)) {
753 PyErr_SetString(PyExc_IndexError,
754 "list assignment index out of range");
755 return -1;
756 }
757 if (v == NULL)
758 return list_ass_slice(a, i, i+1, v);
759 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300760 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000762}
763
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200764/*[clinic input]
765list.insert
766
767 index: Py_ssize_t
768 object: object
769 /
770
771Insert object before index.
772[clinic start generated code]*/
773
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000774static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200775list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
776/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000777{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200778 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 Py_RETURN_NONE;
780 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000781}
782
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200783/*[clinic input]
784list.clear
785
786Remove all items from list.
787[clinic start generated code]*/
788
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000789static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200790list_clear_impl(PyListObject *self)
791/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000792{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200793 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000794 Py_RETURN_NONE;
795}
796
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200797/*[clinic input]
798list.copy
799
800Return a shallow copy of the list.
801[clinic start generated code]*/
802
Eli Benderskycbbaa962011-02-25 05:47:53 +0000803static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200804list_copy_impl(PyListObject *self)
805/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000806{
807 return list_slice(self, 0, Py_SIZE(self));
808}
809
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200810/*[clinic input]
811list.append
812
813 object: object
814 /
815
816Append object to the end of the list.
817[clinic start generated code]*/
818
Eli Benderskycbbaa962011-02-25 05:47:53 +0000819static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200820list_append(PyListObject *self, PyObject *object)
821/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200823 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 Py_RETURN_NONE;
825 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000826}
827
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200828/*[clinic input]
829list.extend
830
831 iterable: object
832 /
833
834Extend list by appending elements from the iterable.
835[clinic start generated code]*/
836
Barry Warsawdedf6d61998-10-09 16:37:25 +0000837static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200838list_extend(PyListObject *self, PyObject *iterable)
839/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 PyObject *it; /* iter(v) */
842 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200843 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 Py_ssize_t mn; /* m + n */
845 Py_ssize_t i;
846 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 /* Special cases:
849 1) lists and tuples which can use PySequence_Fast ops
850 2) extending self to self requires making a copy first
851 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200852 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
853 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200855 iterable = PySequence_Fast(iterable, "argument must be iterable");
856 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200858 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200860 /* short circuit when iterable is empty */
861 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 Py_RETURN_NONE;
863 }
864 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000865 /* It should not be possible to allocate a list large enough to cause
866 an overflow on any relevant platform */
867 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800868 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200869 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 return NULL;
871 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200872 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 * situation a.extend(a), but the following code works
874 * in that case too. Just make sure to resize self
875 * before calling PySequence_Fast_ITEMS.
876 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200877 /* populate the end of self with iterable's items */
878 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 dest = self->ob_item + m;
880 for (i = 0; i < n; i++) {
881 PyObject *o = src[i];
882 Py_INCREF(o);
883 dest[i] = o;
884 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200885 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 Py_RETURN_NONE;
887 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000888
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200889 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 if (it == NULL)
891 return NULL;
892 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200895 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800896 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 Py_DECREF(it);
898 return NULL;
899 }
900 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000901 if (m > PY_SSIZE_T_MAX - n) {
902 /* m + n overflowed; on the chance that n lied, and there really
903 * is enough room, ignore it. If n was telling the truth, we'll
904 * eventually run out of memory during the loop.
905 */
906 }
907 else {
908 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800910 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 goto error;
912 /* Make the list sane again. */
913 Py_SIZE(self) = m;
914 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 /* Run iterator to exhaustion. */
917 for (;;) {
918 PyObject *item = iternext(it);
919 if (item == NULL) {
920 if (PyErr_Occurred()) {
921 if (PyErr_ExceptionMatches(PyExc_StopIteration))
922 PyErr_Clear();
923 else
924 goto error;
925 }
926 break;
927 }
928 if (Py_SIZE(self) < self->allocated) {
929 /* steals ref */
930 PyList_SET_ITEM(self, Py_SIZE(self), item);
931 ++Py_SIZE(self);
932 }
933 else {
934 int status = app1(self, item);
935 Py_DECREF(item); /* append creates a new ref */
936 if (status < 0)
937 goto error;
938 }
939 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200942 if (Py_SIZE(self) < self->allocated) {
943 if (list_resize(self, Py_SIZE(self)) < 0)
944 goto error;
945 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 Py_DECREF(it);
948 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000949
950 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 Py_DECREF(it);
952 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000953}
954
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000955PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200956_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000957{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200958 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000959}
960
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000961static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000962list_inplace_concat(PyListObject *self, PyObject *other)
963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000965
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200966 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 if (result == NULL)
968 return result;
969 Py_DECREF(result);
970 Py_INCREF(self);
971 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000972}
973
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200974/*[clinic input]
975list.pop
976
977 index: Py_ssize_t = -1
978 /
979
980Remove and return item at index (default last).
981
982Raises IndexError if list is empty or index is out of range.
983[clinic start generated code]*/
984
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000985static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200986list_pop_impl(PyListObject *self, Py_ssize_t index)
987/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 PyObject *v;
990 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 if (Py_SIZE(self) == 0) {
993 /* Special-case most common failure cause */
994 PyErr_SetString(PyExc_IndexError, "pop from empty list");
995 return NULL;
996 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200997 if (index < 0)
998 index += Py_SIZE(self);
999 if (index < 0 || index >= Py_SIZE(self)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1001 return NULL;
1002 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001003 v = self->ob_item[index];
1004 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001006 if (status >= 0)
1007 return v; /* and v now owns the reference the list had */
1008 else
1009 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 }
1011 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001012 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001013 if (status < 0) {
1014 Py_DECREF(v);
1015 return NULL;
1016 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001018}
1019
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001020/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1021static void
1022reverse_slice(PyObject **lo, PyObject **hi)
1023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 --hi;
1027 while (lo < hi) {
1028 PyObject *t = *lo;
1029 *lo = *hi;
1030 *hi = t;
1031 ++lo;
1032 --hi;
1033 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001034}
1035
Tim Petersa64dc242002-08-01 02:13:36 +00001036/* Lots of code for an adaptive, stable, natural mergesort. There are many
1037 * pieces to this algorithm; read listsort.txt for overviews and details.
1038 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001039
Daniel Stutzbach98338222010-12-02 21:55:33 +00001040/* A sortslice contains a pointer to an array of keys and a pointer to
1041 * an array of corresponding values. In other words, keys[i]
1042 * corresponds with values[i]. If values == NULL, then the keys are
1043 * also the values.
1044 *
1045 * Several convenience routines are provided here, so that keys and
1046 * values are always moved in sync.
1047 */
1048
1049typedef struct {
1050 PyObject **keys;
1051 PyObject **values;
1052} sortslice;
1053
1054Py_LOCAL_INLINE(void)
1055sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1056{
1057 s1->keys[i] = s2->keys[j];
1058 if (s1->values != NULL)
1059 s1->values[i] = s2->values[j];
1060}
1061
1062Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001063sortslice_copy_incr(sortslice *dst, sortslice *src)
1064{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001065 *dst->keys++ = *src->keys++;
1066 if (dst->values != NULL)
1067 *dst->values++ = *src->values++;
1068}
1069
1070Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001071sortslice_copy_decr(sortslice *dst, sortslice *src)
1072{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001073 *dst->keys-- = *src->keys--;
1074 if (dst->values != NULL)
1075 *dst->values-- = *src->values--;
1076}
1077
1078
1079Py_LOCAL_INLINE(void)
1080sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001081 Py_ssize_t n)
1082{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001083 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1084 if (s1->values != NULL)
1085 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1086}
1087
1088Py_LOCAL_INLINE(void)
1089sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001090 Py_ssize_t n)
1091{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001092 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1093 if (s1->values != NULL)
1094 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1095}
1096
1097Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001098sortslice_advance(sortslice *slice, Py_ssize_t n)
1099{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001100 slice->keys += n;
1101 if (slice->values != NULL)
1102 slice->values += n;
1103}
1104
embg1e34da42018-01-28 20:03:23 -07001105/* Comparison function: ms->key_compare, which is set at run-time in
1106 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001107 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1108 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001109
embg1e34da42018-01-28 20:03:23 -07001110#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001111
1112/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001113 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1114 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1115*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001116#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001118
embg1e34da42018-01-28 20:03:23 -07001119/* The maximum number of entries in a MergeState's pending-runs stack.
1120 * This is enough to sort arrays of size up to about
1121 * 32 * phi ** MAX_MERGE_PENDING
1122 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1123 * with 2**64 elements.
1124 */
1125#define MAX_MERGE_PENDING 85
1126
1127/* When we get into galloping mode, we stay there until both runs win less
1128 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1129 */
1130#define MIN_GALLOP 7
1131
1132/* Avoid malloc for small temp arrays. */
1133#define MERGESTATE_TEMP_SIZE 256
1134
1135/* One MergeState exists on the stack per invocation of mergesort. It's just
1136 * a convenient way to pass state around among the helper functions.
1137 */
1138struct s_slice {
1139 sortslice base;
1140 Py_ssize_t len;
1141};
1142
1143typedef struct s_MergeState MergeState;
1144struct s_MergeState {
1145 /* This controls when we get *into* galloping mode. It's initialized
1146 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1147 * random data, and lower for highly structured data.
1148 */
1149 Py_ssize_t min_gallop;
1150
1151 /* 'a' is temp storage to help with merges. It contains room for
1152 * alloced entries.
1153 */
1154 sortslice a; /* may point to temparray below */
1155 Py_ssize_t alloced;
1156
1157 /* A stack of n pending runs yet to be merged. Run #i starts at
1158 * address base[i] and extends for len[i] elements. It's always
1159 * true (so long as the indices are in bounds) that
1160 *
1161 * pending[i].base + pending[i].len == pending[i+1].base
1162 *
1163 * so we could cut the storage for this, but it's a minor amount,
1164 * and keeping all the info explicit simplifies the code.
1165 */
1166 int n;
1167 struct s_slice pending[MAX_MERGE_PENDING];
1168
1169 /* 'a' points to this when possible, rather than muck with malloc. */
1170 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1171
1172 /* This is the function we will use to compare two keys,
1173 * even when none of our special cases apply and we have to use
1174 * safe_object_compare. */
1175 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1176
1177 /* This function is used by unsafe_object_compare to optimize comparisons
1178 * when we know our list is type-homogeneous but we can't assume anything else.
1179 * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1180 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1181
1182 /* This function is used by unsafe_tuple_compare to compare the first elements
1183 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1184 * we can assume more, and use one of the special-case compares. */
1185 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1186};
1187
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001188/* binarysort is the best method for sorting small arrays: it does
1189 few compares, but can do data movement quadratic in the number of
1190 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001191 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001192 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001193 On entry, must have lo <= start <= hi, and that [lo, start) is already
1194 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001195 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001196 Even in case of error, the output slice will be some permutation of
1197 the input (nothing is lost or duplicated).
1198*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001199static int
embg1e34da42018-01-28 20:03:23 -07001200binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001201{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001202 Py_ssize_t k;
1203 PyObject **l, **p, **r;
1204 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001205
Daniel Stutzbach98338222010-12-02 21:55:33 +00001206 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001208 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 ++start;
1210 for (; start < hi; ++start) {
1211 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001212 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 r = start;
1214 pivot = *r;
1215 /* Invariants:
1216 * pivot >= all in [lo, l).
1217 * pivot < all in [r, start).
1218 * The second is vacuously true at the start.
1219 */
1220 assert(l < r);
1221 do {
1222 p = l + ((r - l) >> 1);
1223 IFLT(pivot, *p)
1224 r = p;
1225 else
1226 l = p+1;
1227 } while (l < r);
1228 assert(l == r);
1229 /* The invariants still hold, so pivot >= all in [lo, l) and
1230 pivot < all in [l, start), so pivot belongs at l. Note
1231 that if there are elements equal to pivot, l points to the
1232 first slot after them -- that's why this sort is stable.
1233 Slide over to make room.
1234 Caution: using memmove is much slower under MSVC 5;
1235 we're not usually moving many slots. */
1236 for (p = start; p > l; --p)
1237 *p = *(p-1);
1238 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001239 if (lo.values != NULL) {
1240 Py_ssize_t offset = lo.values - lo.keys;
1241 p = start + offset;
1242 pivot = *p;
1243 l += offset;
1244 for (p = start + offset; p > l; --p)
1245 *p = *(p-1);
1246 *l = pivot;
1247 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 }
1249 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001250
1251 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001253}
1254
Tim Petersa64dc242002-08-01 02:13:36 +00001255/*
1256Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1257is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001258
Tim Petersa64dc242002-08-01 02:13:36 +00001259 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001260
Tim Petersa64dc242002-08-01 02:13:36 +00001261or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001262
Tim Petersa64dc242002-08-01 02:13:36 +00001263 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001264
Tim Petersa64dc242002-08-01 02:13:36 +00001265Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1266For its intended use in a stable mergesort, the strictness of the defn of
1267"descending" is needed so that the caller can safely reverse a descending
1268sequence without violating stability (strict > ensures there are no equal
1269elements to get out of order).
1270
1271Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001272*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001273static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001274count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 Py_ssize_t k;
1277 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 assert(lo < hi);
1280 *descending = 0;
1281 ++lo;
1282 if (lo == hi)
1283 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 n = 2;
1286 IFLT(*lo, *(lo-1)) {
1287 *descending = 1;
1288 for (lo = lo+1; lo < hi; ++lo, ++n) {
1289 IFLT(*lo, *(lo-1))
1290 ;
1291 else
1292 break;
1293 }
1294 }
1295 else {
1296 for (lo = lo+1; lo < hi; ++lo, ++n) {
1297 IFLT(*lo, *(lo-1))
1298 break;
1299 }
1300 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001303fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001305}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001306
Tim Petersa64dc242002-08-01 02:13:36 +00001307/*
1308Locate the proper position of key in a sorted vector; if the vector contains
1309an element equal to key, return the position immediately to the left of
1310the leftmost equal element. [gallop_right() does the same except returns
1311the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001312
Tim Petersa64dc242002-08-01 02:13:36 +00001313"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001314
Tim Petersa64dc242002-08-01 02:13:36 +00001315"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1316hint is to the final result, the faster this runs.
1317
1318The return value is the int k in 0..n such that
1319
1320 a[k-1] < key <= a[k]
1321
1322pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1323key belongs at index k; or, IOW, the first k elements of a should precede
1324key, and the last n-k should follow key.
1325
1326Returns -1 on error. See listsort.txt for info on the method.
1327*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001328static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001329gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 Py_ssize_t ofs;
1332 Py_ssize_t lastofs;
1333 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 a += hint;
1338 lastofs = 0;
1339 ofs = 1;
1340 IFLT(*a, key) {
1341 /* a[hint] < key -- gallop right, until
1342 * a[hint + lastofs] < key <= a[hint + ofs]
1343 */
1344 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1345 while (ofs < maxofs) {
1346 IFLT(a[ofs], key) {
1347 lastofs = ofs;
1348 ofs = (ofs << 1) + 1;
1349 if (ofs <= 0) /* int overflow */
1350 ofs = maxofs;
1351 }
1352 else /* key <= a[hint + ofs] */
1353 break;
1354 }
1355 if (ofs > maxofs)
1356 ofs = maxofs;
1357 /* Translate back to offsets relative to &a[0]. */
1358 lastofs += hint;
1359 ofs += hint;
1360 }
1361 else {
1362 /* key <= a[hint] -- gallop left, until
1363 * a[hint - ofs] < key <= a[hint - lastofs]
1364 */
1365 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1366 while (ofs < maxofs) {
1367 IFLT(*(a-ofs), key)
1368 break;
1369 /* key <= a[hint - ofs] */
1370 lastofs = ofs;
1371 ofs = (ofs << 1) + 1;
1372 if (ofs <= 0) /* int overflow */
1373 ofs = maxofs;
1374 }
1375 if (ofs > maxofs)
1376 ofs = maxofs;
1377 /* Translate back to positive offsets relative to &a[0]. */
1378 k = lastofs;
1379 lastofs = hint - ofs;
1380 ofs = hint - k;
1381 }
1382 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1385 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1386 * right of lastofs but no farther right than ofs. Do a binary
1387 * search, with invariant a[lastofs-1] < key <= a[ofs].
1388 */
1389 ++lastofs;
1390 while (lastofs < ofs) {
1391 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 IFLT(a[m], key)
1394 lastofs = m+1; /* a[m] < key */
1395 else
1396 ofs = m; /* key <= a[m] */
1397 }
1398 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1399 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001400
1401fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001403}
1404
1405/*
1406Exactly like gallop_left(), except that if key already exists in a[0:n],
1407finds the position immediately to the right of the rightmost equal value.
1408
1409The return value is the int k in 0..n such that
1410
1411 a[k-1] <= key < a[k]
1412
1413or -1 if error.
1414
1415The code duplication is massive, but this is enough different given that
1416we're sticking to "<" comparisons that it's much harder to follow if
1417written as one routine with yet another "left or right?" flag.
1418*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001419static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001420gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 Py_ssize_t ofs;
1423 Py_ssize_t lastofs;
1424 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 a += hint;
1429 lastofs = 0;
1430 ofs = 1;
1431 IFLT(key, *a) {
1432 /* key < a[hint] -- gallop left, until
1433 * a[hint - ofs] <= key < a[hint - lastofs]
1434 */
1435 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1436 while (ofs < maxofs) {
1437 IFLT(key, *(a-ofs)) {
1438 lastofs = ofs;
1439 ofs = (ofs << 1) + 1;
1440 if (ofs <= 0) /* int overflow */
1441 ofs = maxofs;
1442 }
1443 else /* a[hint - ofs] <= key */
1444 break;
1445 }
1446 if (ofs > maxofs)
1447 ofs = maxofs;
1448 /* Translate back to positive offsets relative to &a[0]. */
1449 k = lastofs;
1450 lastofs = hint - ofs;
1451 ofs = hint - k;
1452 }
1453 else {
1454 /* a[hint] <= key -- gallop right, until
1455 * a[hint + lastofs] <= key < a[hint + ofs]
1456 */
1457 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1458 while (ofs < maxofs) {
1459 IFLT(key, a[ofs])
1460 break;
1461 /* a[hint + ofs] <= key */
1462 lastofs = ofs;
1463 ofs = (ofs << 1) + 1;
1464 if (ofs <= 0) /* int overflow */
1465 ofs = maxofs;
1466 }
1467 if (ofs > maxofs)
1468 ofs = maxofs;
1469 /* Translate back to offsets relative to &a[0]. */
1470 lastofs += hint;
1471 ofs += hint;
1472 }
1473 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1476 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1477 * right of lastofs but no farther right than ofs. Do a binary
1478 * search, with invariant a[lastofs-1] <= key < a[ofs].
1479 */
1480 ++lastofs;
1481 while (lastofs < ofs) {
1482 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 IFLT(key, a[m])
1485 ofs = m; /* key < a[m] */
1486 else
1487 lastofs = m+1; /* a[m] <= key */
1488 }
1489 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1490 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001491
1492fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001494}
1495
Tim Petersa64dc242002-08-01 02:13:36 +00001496/* Conceptually a MergeState's constructor. */
1497static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001498merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001501 if (has_keyfunc) {
1502 /* The temporary space for merging will need at most half the list
1503 * size rounded up. Use the minimum possible space so we can use the
1504 * rest of temparray for other things. In particular, if there is
1505 * enough extra space, listsort() will use it to store the keys.
1506 */
1507 ms->alloced = (list_size + 1) / 2;
1508
1509 /* ms->alloced describes how many keys will be stored at
1510 ms->temparray, but we also need to store the values. Hence,
1511 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1512 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1513 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1514 ms->a.values = &ms->temparray[ms->alloced];
1515 }
1516 else {
1517 ms->alloced = MERGESTATE_TEMP_SIZE;
1518 ms->a.values = NULL;
1519 }
1520 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 ms->n = 0;
1522 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001523}
1524
1525/* Free all the temp memory owned by the MergeState. This must be called
1526 * when you're done with a MergeState, and may be called before then if
1527 * you want to free the temp memory early.
1528 */
1529static void
1530merge_freemem(MergeState *ms)
1531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001533 if (ms->a.keys != ms->temparray)
1534 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001535}
1536
1537/* Ensure enough temp memory for 'need' array slots is available.
1538 * Returns 0 on success and -1 if the memory can't be gotten.
1539 */
1540static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001541merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001542{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001543 int multiplier;
1544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 assert(ms != NULL);
1546 if (need <= ms->alloced)
1547 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001548
1549 multiplier = ms->a.values != NULL ? 2 : 1;
1550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 /* Don't realloc! That can cost cycles to copy the old data, but
1552 * we don't care what's in the block.
1553 */
1554 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001555 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 PyErr_NoMemory();
1557 return -1;
1558 }
embg1e34da42018-01-28 20:03:23 -07001559 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001560 * sizeof(PyObject *));
1561 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001563 if (ms->a.values != NULL)
1564 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 return 0;
1566 }
1567 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001569}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1571 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001572
Daniel Stutzbach98338222010-12-02 21:55:33 +00001573/* Merge the na elements starting at ssa with the nb elements starting at
1574 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1575 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1576 * should have na <= nb. See listsort.txt for more info. Return 0 if
1577 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001578 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001579static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001580merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1581 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001584 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 int result = -1; /* guilty until proved innocent */
1586 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001587
Daniel Stutzbach98338222010-12-02 21:55:33 +00001588 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1589 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 if (MERGE_GETMEM(ms, na) < 0)
1591 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001592 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1593 dest = ssa;
1594 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001595
Daniel Stutzbach98338222010-12-02 21:55:33 +00001596 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 --nb;
1598 if (nb == 0)
1599 goto Succeed;
1600 if (na == 1)
1601 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 min_gallop = ms->min_gallop;
1604 for (;;) {
1605 Py_ssize_t acount = 0; /* # of times A won in a row */
1606 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 /* Do the straightforward thing until (if ever) one run
1609 * appears to win consistently.
1610 */
1611 for (;;) {
1612 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001613 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 if (k) {
1615 if (k < 0)
1616 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001617 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 ++bcount;
1619 acount = 0;
1620 --nb;
1621 if (nb == 0)
1622 goto Succeed;
1623 if (bcount >= min_gallop)
1624 break;
1625 }
1626 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001627 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 ++acount;
1629 bcount = 0;
1630 --na;
1631 if (na == 1)
1632 goto CopyB;
1633 if (acount >= min_gallop)
1634 break;
1635 }
1636 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 /* One run is winning so consistently that galloping may
1639 * be a huge win. So try that, and continue galloping until
1640 * (if ever) neither run appears to be winning consistently
1641 * anymore.
1642 */
1643 ++min_gallop;
1644 do {
1645 assert(na > 1 && nb > 0);
1646 min_gallop -= min_gallop > 1;
1647 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001648 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 acount = k;
1650 if (k) {
1651 if (k < 0)
1652 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001653 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1654 sortslice_advance(&dest, k);
1655 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 na -= k;
1657 if (na == 1)
1658 goto CopyB;
1659 /* na==0 is impossible now if the comparison
1660 * function is consistent, but we can't assume
1661 * that it is.
1662 */
1663 if (na == 0)
1664 goto Succeed;
1665 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001666 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 --nb;
1668 if (nb == 0)
1669 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001670
embg1e34da42018-01-28 20:03:23 -07001671 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 bcount = k;
1673 if (k) {
1674 if (k < 0)
1675 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001676 sortslice_memmove(&dest, 0, &ssb, 0, k);
1677 sortslice_advance(&dest, k);
1678 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 nb -= k;
1680 if (nb == 0)
1681 goto Succeed;
1682 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001683 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 --na;
1685 if (na == 1)
1686 goto CopyB;
1687 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1688 ++min_gallop; /* penalize it for leaving galloping mode */
1689 ms->min_gallop = min_gallop;
1690 }
Tim Petersa64dc242002-08-01 02:13:36 +00001691Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001693Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001695 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001697CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001699 /* The last element of ssa belongs at the end of the merge. */
1700 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1701 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001703}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001704
Daniel Stutzbach98338222010-12-02 21:55:33 +00001705/* Merge the na elements starting at pa with the nb elements starting at
1706 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1707 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1708 * should have na >= nb. See listsort.txt for more info. Return 0 if
1709 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001710 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001711static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001712merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1713 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001716 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001719
Daniel Stutzbach98338222010-12-02 21:55:33 +00001720 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1721 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 if (MERGE_GETMEM(ms, nb) < 0)
1723 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001724 dest = ssb;
1725 sortslice_advance(&dest, nb-1);
1726 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1727 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001729 ssb.keys = ms->a.keys + nb - 1;
1730 if (ssb.values != NULL)
1731 ssb.values = ms->a.values + nb - 1;
1732 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001733
Daniel Stutzbach98338222010-12-02 21:55:33 +00001734 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 --na;
1736 if (na == 0)
1737 goto Succeed;
1738 if (nb == 1)
1739 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 min_gallop = ms->min_gallop;
1742 for (;;) {
1743 Py_ssize_t acount = 0; /* # of times A won in a row */
1744 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 /* Do the straightforward thing until (if ever) one run
1747 * appears to win consistently.
1748 */
1749 for (;;) {
1750 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001751 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 if (k) {
1753 if (k < 0)
1754 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001755 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 ++acount;
1757 bcount = 0;
1758 --na;
1759 if (na == 0)
1760 goto Succeed;
1761 if (acount >= min_gallop)
1762 break;
1763 }
1764 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001765 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 ++bcount;
1767 acount = 0;
1768 --nb;
1769 if (nb == 1)
1770 goto CopyA;
1771 if (bcount >= min_gallop)
1772 break;
1773 }
1774 }
Tim Petersa64dc242002-08-01 02:13:36 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 /* One run is winning so consistently that galloping may
1777 * be a huge win. So try that, and continue galloping until
1778 * (if ever) neither run appears to be winning consistently
1779 * anymore.
1780 */
1781 ++min_gallop;
1782 do {
1783 assert(na > 0 && nb > 1);
1784 min_gallop -= min_gallop > 1;
1785 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001786 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (k < 0)
1788 goto Fail;
1789 k = na - k;
1790 acount = k;
1791 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001792 sortslice_advance(&dest, -k);
1793 sortslice_advance(&ssa, -k);
1794 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 na -= k;
1796 if (na == 0)
1797 goto Succeed;
1798 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001799 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 --nb;
1801 if (nb == 1)
1802 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001803
embg1e34da42018-01-28 20:03:23 -07001804 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 if (k < 0)
1806 goto Fail;
1807 k = nb - k;
1808 bcount = k;
1809 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001810 sortslice_advance(&dest, -k);
1811 sortslice_advance(&ssb, -k);
1812 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 nb -= k;
1814 if (nb == 1)
1815 goto CopyA;
1816 /* nb==0 is impossible now if the comparison
1817 * function is consistent, but we can't assume
1818 * that it is.
1819 */
1820 if (nb == 0)
1821 goto Succeed;
1822 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001823 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 --na;
1825 if (na == 0)
1826 goto Succeed;
1827 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1828 ++min_gallop; /* penalize it for leaving galloping mode */
1829 ms->min_gallop = min_gallop;
1830 }
Tim Petersa64dc242002-08-01 02:13:36 +00001831Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001833Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001835 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001837CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001839 /* The first element of ssb belongs at the front of the merge. */
1840 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1841 sortslice_advance(&dest, -na);
1842 sortslice_advance(&ssa, -na);
1843 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001845}
1846
1847/* Merge the two runs at stack indices i and i+1.
1848 * Returns 0 on success, -1 on error.
1849 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001850static Py_ssize_t
1851merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001852{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001853 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 Py_ssize_t na, nb;
1855 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 assert(ms != NULL);
1858 assert(ms->n >= 2);
1859 assert(i >= 0);
1860 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001861
Daniel Stutzbach98338222010-12-02 21:55:33 +00001862 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001864 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 nb = ms->pending[i+1].len;
1866 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001867 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 /* Record the length of the combined runs; if i is the 3rd-last
1870 * run now, also slide over the last run (which isn't involved
1871 * in this merge). The current run i+1 goes away in any case.
1872 */
1873 ms->pending[i].len = na + nb;
1874 if (i == ms->n - 3)
1875 ms->pending[i+1] = ms->pending[i+2];
1876 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 /* Where does b start in a? Elements in a before that can be
1879 * ignored (already in place).
1880 */
embg1e34da42018-01-28 20:03:23 -07001881 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 if (k < 0)
1883 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001884 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 na -= k;
1886 if (na == 0)
1887 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 /* Where does a end in b? Elements in b after that can be
1890 * ignored (already in place).
1891 */
embg1e34da42018-01-28 20:03:23 -07001892 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 if (nb <= 0)
1894 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 /* Merge what remains of the runs, using a temp array with
1897 * min(na, nb) elements.
1898 */
1899 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001900 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001902 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001903}
1904
1905/* Examine the stack of runs waiting to be merged, merging adjacent runs
1906 * until the stack invariants are re-established:
1907 *
1908 * 1. len[-3] > len[-2] + len[-1]
1909 * 2. len[-2] > len[-1]
1910 *
1911 * See listsort.txt for more info.
1912 *
1913 * Returns 0 on success, -1 on error.
1914 */
1915static int
1916merge_collapse(MergeState *ms)
1917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 assert(ms);
1921 while (ms->n > 1) {
1922 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001923 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1924 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 if (p[n-1].len < p[n+1].len)
1926 --n;
1927 if (merge_at(ms, n) < 0)
1928 return -1;
1929 }
1930 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001931 if (merge_at(ms, n) < 0)
1932 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 }
1934 else
1935 break;
1936 }
1937 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001938}
1939
1940/* Regardless of invariants, merge all runs on the stack until only one
1941 * remains. This is used at the end of the mergesort.
1942 *
1943 * Returns 0 on success, -1 on error.
1944 */
1945static int
1946merge_force_collapse(MergeState *ms)
1947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 assert(ms);
1951 while (ms->n > 1) {
1952 Py_ssize_t n = ms->n - 2;
1953 if (n > 0 && p[n-1].len < p[n+1].len)
1954 --n;
1955 if (merge_at(ms, n) < 0)
1956 return -1;
1957 }
1958 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001959}
1960
1961/* Compute a good value for the minimum run length; natural runs shorter
1962 * than this are boosted artificially via binary insertion.
1963 *
1964 * If n < 64, return n (it's too small to bother with fancy stuff).
1965 * Else if n is an exact power of 2, return 32.
1966 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1967 * strictly less than, an exact power of 2.
1968 *
1969 * See listsort.txt for more info.
1970 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001971static Py_ssize_t
1972merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 assert(n >= 0);
1977 while (n >= 64) {
1978 r |= n & 1;
1979 n >>= 1;
1980 }
1981 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001982}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001983
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001984static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001985reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001986{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001987 reverse_slice(s->keys, &s->keys[n]);
1988 if (s->values != NULL)
1989 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001990}
1991
embg1e34da42018-01-28 20:03:23 -07001992/* Here we define custom comparison functions to optimize for the cases one commonly
1993 * encounters in practice: homogeneous lists, often of one of the basic types. */
1994
1995/* This struct holds the comparison function and helper functions
1996 * selected in the pre-sort check. */
1997
1998/* These are the special case compare functions.
1999 * ms->key_compare will always point to one of these: */
2000
2001/* Heterogeneous compare: default, always safe to fall back on. */
2002static int
2003safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2004{
2005 /* No assumptions necessary! */
2006 return PyObject_RichCompareBool(v, w, Py_LT);
2007}
2008
2009/* Homogeneous compare: safe for any two compareable objects of the same type.
2010 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2011 * pre-sort check.)
2012 */
2013static int
2014unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2015{
2016 PyObject *res_obj; int res;
2017
2018 /* No assumptions, because we check first: */
2019 if (v->ob_type->tp_richcompare != ms->key_richcompare)
2020 return PyObject_RichCompareBool(v, w, Py_LT);
2021
2022 assert(ms->key_richcompare != NULL);
2023 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2024
2025 if (res_obj == Py_NotImplemented) {
2026 Py_DECREF(res_obj);
2027 return PyObject_RichCompareBool(v, w, Py_LT);
2028 }
2029 if (res_obj == NULL)
2030 return -1;
2031
2032 if (PyBool_Check(res_obj)) {
2033 res = (res_obj == Py_True);
2034 }
2035 else {
2036 res = PyObject_IsTrue(res_obj);
2037 }
2038 Py_DECREF(res_obj);
2039
2040 /* Note that we can't assert
2041 * res == PyObject_RichCompareBool(v, w, Py_LT);
2042 * because of evil compare functions like this:
2043 * lambda a, b: int(random.random() * 3) - 1)
2044 * (which is actually in test_sort.py) */
2045 return res;
2046}
2047
2048/* Latin string compare: safe for any two latin (one byte per char) strings. */
2049static int
2050unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2051{
Victor Stinner8017b802018-01-29 13:47:06 +01002052 Py_ssize_t len;
2053 int res;
embg1e34da42018-01-28 20:03:23 -07002054
2055 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2056 assert(v->ob_type == w->ob_type);
2057 assert(v->ob_type == &PyUnicode_Type);
2058 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2059 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2060
2061 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2062 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2063
2064 res = (res != 0 ?
2065 res < 0 :
2066 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2067
2068 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2069 return res;
2070}
2071
2072/* Bounded int compare: compare any two longs that fit in a single machine word. */
2073static int
2074unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2075{
2076 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2077
2078 /* Modified from Objects/longobject.c:long_compare, assuming: */
2079 assert(v->ob_type == w->ob_type);
2080 assert(v->ob_type == &PyLong_Type);
2081 assert(Py_ABS(Py_SIZE(v)) <= 1);
2082 assert(Py_ABS(Py_SIZE(w)) <= 1);
2083
2084 vl = (PyLongObject*)v;
2085 wl = (PyLongObject*)w;
2086
2087 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2088 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2089
2090 if (Py_SIZE(vl) < 0)
2091 v0 = -v0;
2092 if (Py_SIZE(wl) < 0)
2093 w0 = -w0;
2094
2095 res = v0 < w0;
2096 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2097 return res;
2098}
2099
2100/* Float compare: compare any two floats. */
2101static int
2102unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2103{
2104 int res;
2105
2106 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2107 assert(v->ob_type == w->ob_type);
2108 assert(v->ob_type == &PyFloat_Type);
2109
2110 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2111 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2112 return res;
2113}
2114
2115/* Tuple compare: compare *any* two tuples, using
2116 * ms->tuple_elem_compare to compare the first elements, which is set
2117 * using the same pre-sort check as we use for ms->key_compare,
2118 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2119 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2120 * that most tuple compares don't involve x[1:]. */
2121static int
2122unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2123{
2124 PyTupleObject *vt, *wt;
2125 Py_ssize_t i, vlen, wlen;
2126 int k;
2127
2128 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2129 assert(v->ob_type == w->ob_type);
2130 assert(v->ob_type == &PyTuple_Type);
2131 assert(Py_SIZE(v) > 0);
2132 assert(Py_SIZE(w) > 0);
2133
2134 vt = (PyTupleObject *)v;
2135 wt = (PyTupleObject *)w;
2136
2137 vlen = Py_SIZE(vt);
2138 wlen = Py_SIZE(wt);
2139
2140 for (i = 0; i < vlen && i < wlen; i++) {
2141 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2142 if (k < 0)
2143 return -1;
2144 if (!k)
2145 break;
2146 }
2147
2148 if (i >= vlen || i >= wlen)
2149 return vlen < wlen;
2150
2151 if (i == 0)
2152 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2153 else
2154 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2155}
2156
Tim Petersa64dc242002-08-01 02:13:36 +00002157/* An adaptive, stable, natural mergesort. See listsort.txt.
2158 * Returns Py_None on success, NULL on error. Even in case of error, the
2159 * list will be some permutation of its input state (nothing is lost or
2160 * duplicated).
2161 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002162/*[clinic input]
2163list.sort
2164
2165 *
2166 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002167 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002168
2169Stable sort *IN PLACE*.
2170[clinic start generated code]*/
2171
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002172static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002173list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002174/*[clinic end generated code: output=57b9f9c5e23fbe42 input=b0fcf743982c5b90]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 Py_ssize_t nremaining;
2178 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002179 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 Py_ssize_t saved_ob_size, saved_allocated;
2181 PyObject **saved_ob_item;
2182 PyObject **final_ob_item;
2183 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002185 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002188 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 if (keyfunc == Py_None)
2190 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 /* The list is temporarily made empty, so that mutations performed
2193 * by comparison functions can't affect the slice of memory we're
2194 * sorting (allowing mutations during sorting is a core-dump
2195 * factory, since ob_item may change).
2196 */
2197 saved_ob_size = Py_SIZE(self);
2198 saved_ob_item = self->ob_item;
2199 saved_allocated = self->allocated;
2200 Py_SIZE(self) = 0;
2201 self->ob_item = NULL;
2202 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002203
Daniel Stutzbach98338222010-12-02 21:55:33 +00002204 if (keyfunc == NULL) {
2205 keys = NULL;
2206 lo.keys = saved_ob_item;
2207 lo.values = NULL;
2208 }
2209 else {
2210 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2211 /* Leverage stack space we allocated but won't otherwise use */
2212 keys = &ms.temparray[saved_ob_size+1];
2213 else {
2214 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002215 if (keys == NULL) {
2216 PyErr_NoMemory();
2217 goto keyfunc_fail;
2218 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002220
2221 for (i = 0; i < saved_ob_size ; i++) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002222 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
2223 NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002224 if (keys[i] == NULL) {
2225 for (i=i-1 ; i>=0 ; i--)
2226 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002227 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002228 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002229 goto keyfunc_fail;
2230 }
2231 }
2232
2233 lo.keys = keys;
2234 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002236
embg1e34da42018-01-28 20:03:23 -07002237
2238 /* The pre-sort check: here's where we decide which compare function to use.
2239 * How much optimization is safe? We test for homogeneity with respect to
2240 * several properties that are expensive to check at compare-time, and
2241 * set ms appropriately. */
2242 if (saved_ob_size > 1) {
2243 /* Assume the first element is representative of the whole list. */
2244 int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2245 Py_SIZE(lo.keys[0]) > 0);
2246
2247 PyTypeObject* key_type = (keys_are_in_tuples ?
2248 PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2249 lo.keys[0]->ob_type);
2250
2251 int keys_are_all_same_type = 1;
2252 int strings_are_latin = 1;
2253 int ints_are_bounded = 1;
2254
2255 /* Prove that assumption by checking every key. */
2256 int i;
2257 for (i=0; i < saved_ob_size; i++) {
2258
2259 if (keys_are_in_tuples &&
2260 !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2261 keys_are_in_tuples = 0;
2262 keys_are_all_same_type = 0;
2263 break;
2264 }
2265
2266 /* Note: for lists of tuples, key is the first element of the tuple
2267 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2268 * for lists of tuples in the if-statement directly above. */
2269 PyObject *key = (keys_are_in_tuples ?
2270 PyTuple_GET_ITEM(lo.keys[i], 0) :
2271 lo.keys[i]);
2272
2273 if (key->ob_type != key_type) {
2274 keys_are_all_same_type = 0;
2275 break;
2276 }
2277
2278 if (key_type == &PyLong_Type) {
2279 if (ints_are_bounded && Py_ABS(Py_SIZE(key)) > 1)
2280 ints_are_bounded = 0;
2281 }
2282 else if (key_type == &PyUnicode_Type){
2283 if (strings_are_latin &&
2284 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND)
2285 strings_are_latin = 0;
2286 }
2287 }
2288
2289 /* Choose the best compare, given what we now know about the keys. */
2290 if (keys_are_all_same_type) {
2291
2292 if (key_type == &PyUnicode_Type && strings_are_latin) {
2293 ms.key_compare = unsafe_latin_compare;
2294 }
2295 else if (key_type == &PyLong_Type && ints_are_bounded) {
2296 ms.key_compare = unsafe_long_compare;
2297 }
2298 else if (key_type == &PyFloat_Type) {
2299 ms.key_compare = unsafe_float_compare;
2300 }
2301 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2302 ms.key_compare = unsafe_object_compare;
2303 }
2304 }
2305 else {
2306 ms.key_compare = safe_object_compare;
2307 }
2308
2309 if (keys_are_in_tuples) {
2310 /* Make sure we're not dealing with tuples of tuples
2311 * (remember: here, key_type refers list [key[0] for key in keys]) */
2312 if (key_type == &PyTuple_Type)
2313 ms.tuple_elem_compare = safe_object_compare;
2314 else
2315 ms.tuple_elem_compare = ms.key_compare;
2316
2317 ms.key_compare = unsafe_tuple_compare;
2318 }
2319 }
2320 /* End of pre-sort check: ms is now set properly! */
2321
Daniel Stutzbach98338222010-12-02 21:55:33 +00002322 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 nremaining = saved_ob_size;
2325 if (nremaining < 2)
2326 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002327
Benjamin Peterson05380642010-08-23 19:35:39 +00002328 /* Reverse sort stability achieved by initially reversing the list,
2329 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002330 if (reverse) {
2331 if (keys != NULL)
2332 reverse_slice(&keys[0], &keys[saved_ob_size]);
2333 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2334 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 /* March over the array once, left to right, finding natural runs,
2337 * and extending short natural runs to minrun elements.
2338 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 minrun = merge_compute_minrun(nremaining);
2340 do {
2341 int descending;
2342 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002345 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 if (n < 0)
2347 goto fail;
2348 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002349 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 /* If short, extend to min(minrun, nremaining). */
2351 if (n < minrun) {
2352 const Py_ssize_t force = nremaining <= minrun ?
2353 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002354 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 goto fail;
2356 n = force;
2357 }
2358 /* Push run onto pending-runs stack, and maybe merge. */
2359 assert(ms.n < MAX_MERGE_PENDING);
2360 ms.pending[ms.n].base = lo;
2361 ms.pending[ms.n].len = n;
2362 ++ms.n;
2363 if (merge_collapse(&ms) < 0)
2364 goto fail;
2365 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002366 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 nremaining -= n;
2368 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 if (merge_force_collapse(&ms) < 0)
2371 goto fail;
2372 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002373 assert(keys == NULL
2374 ? ms.pending[0].base.keys == saved_ob_item
2375 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002377 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002378
2379succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002381fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002382 if (keys != NULL) {
2383 for (i = 0; i < saved_ob_size; i++)
2384 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002385 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002386 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 if (self->allocated != -1 && result != NULL) {
2390 /* The user mucked with the list during the sort,
2391 * and we don't already have another error to report.
2392 */
2393 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2394 result = NULL;
2395 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 if (reverse && saved_ob_size > 1)
2398 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002401
Daniel Stutzbach98338222010-12-02 21:55:33 +00002402keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 final_ob_item = self->ob_item;
2404 i = Py_SIZE(self);
2405 Py_SIZE(self) = saved_ob_size;
2406 self->ob_item = saved_ob_item;
2407 self->allocated = saved_allocated;
2408 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002409 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 guarantee that the list is really empty when it returns */
2411 while (--i >= 0) {
2412 Py_XDECREF(final_ob_item[i]);
2413 }
2414 PyMem_FREE(final_ob_item);
2415 }
2416 Py_XINCREF(result);
2417 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002418}
Tim Peters330f9e92002-07-19 07:05:44 +00002419#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002420#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002421
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002422int
Fred Drakea2f55112000-07-09 15:16:51 +00002423PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 if (v == NULL || !PyList_Check(v)) {
2426 PyErr_BadInternalCall();
2427 return -1;
2428 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002429 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 if (v == NULL)
2431 return -1;
2432 Py_DECREF(v);
2433 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002434}
2435
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002436/*[clinic input]
2437list.reverse
2438
2439Reverse *IN PLACE*.
2440[clinic start generated code]*/
2441
Guido van Rossumb86c5492001-02-12 22:06:02 +00002442static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002443list_reverse_impl(PyListObject *self)
2444/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 if (Py_SIZE(self) > 1)
2447 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2448 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002449}
2450
Guido van Rossum84c76f51990-10-30 13:32:20 +00002451int
Fred Drakea2f55112000-07-09 15:16:51 +00002452PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 if (v == NULL || !PyList_Check(v)) {
2457 PyErr_BadInternalCall();
2458 return -1;
2459 }
2460 if (Py_SIZE(self) > 1)
2461 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2462 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002463}
2464
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002465PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002466PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 PyObject *w;
2469 PyObject **p, **q;
2470 Py_ssize_t n;
2471 if (v == NULL || !PyList_Check(v)) {
2472 PyErr_BadInternalCall();
2473 return NULL;
2474 }
2475 n = Py_SIZE(v);
2476 w = PyTuple_New(n);
2477 if (w == NULL)
2478 return NULL;
2479 p = ((PyTupleObject *)w)->ob_item;
2480 q = ((PyListObject *)v)->ob_item;
2481 while (--n >= 0) {
2482 Py_INCREF(*q);
2483 *p = *q;
2484 p++;
2485 q++;
2486 }
2487 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002488}
2489
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002490/*[clinic input]
2491list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002492
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002493 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002494 start: slice_index(accept={int}) = 0
2495 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002496 /
2497
2498Return first index of value.
2499
2500Raises ValueError if the value is not present.
2501[clinic start generated code]*/
2502
2503static PyObject *
2504list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2505 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002506/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002507{
2508 Py_ssize_t i;
2509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 if (start < 0) {
2511 start += Py_SIZE(self);
2512 if (start < 0)
2513 start = 0;
2514 }
2515 if (stop < 0) {
2516 stop += Py_SIZE(self);
2517 if (stop < 0)
2518 stop = 0;
2519 }
2520 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002521 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 if (cmp > 0)
2523 return PyLong_FromSsize_t(i);
2524 else if (cmp < 0)
2525 return NULL;
2526 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002527 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002529}
2530
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002531/*[clinic input]
2532list.count
2533
2534 value: object
2535 /
2536
2537Return number of occurrences of value.
2538[clinic start generated code]*/
2539
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002540static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002541list_count(PyListObject *self, PyObject *value)
2542/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 Py_ssize_t count = 0;
2545 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002548 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 if (cmp > 0)
2550 count++;
2551 else if (cmp < 0)
2552 return NULL;
2553 }
2554 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002555}
2556
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002557/*[clinic input]
2558list.remove
2559
2560 value: object
2561 /
2562
2563Remove first occurrence of value.
2564
2565Raises ValueError if the value is not present.
2566[clinic start generated code]*/
2567
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002568static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002569list_remove(PyListObject *self, PyObject *value)
2570/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002575 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 if (cmp > 0) {
2577 if (list_ass_slice(self, i, i+1,
2578 (PyObject *)NULL) == 0)
2579 Py_RETURN_NONE;
2580 return NULL;
2581 }
2582 else if (cmp < 0)
2583 return NULL;
2584 }
2585 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2586 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002587}
2588
Jeremy Hylton8caad492000-06-23 14:18:11 +00002589static int
2590list_traverse(PyListObject *o, visitproc visit, void *arg)
2591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 for (i = Py_SIZE(o); --i >= 0; )
2595 Py_VISIT(o->ob_item[i]);
2596 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002597}
2598
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002599static PyObject *
2600list_richcompare(PyObject *v, PyObject *w, int op)
2601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 PyListObject *vl, *wl;
2603 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002604
Brian Curtindfc80e32011-08-10 20:28:54 -05002605 if (!PyList_Check(v) || !PyList_Check(w))
2606 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 vl = (PyListObject *)v;
2609 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2612 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002614 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 else
stratakise8b19652017-11-02 11:32:54 +01002616 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 /* Search for the first index where items are different */
2620 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2621 int k = PyObject_RichCompareBool(vl->ob_item[i],
2622 wl->ob_item[i], Py_EQ);
2623 if (k < 0)
2624 return NULL;
2625 if (!k)
2626 break;
2627 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2630 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002631 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 /* We have an item that differs -- shortcuts for EQ/NE */
2635 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002636 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 }
2638 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002639 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 /* Compare the final item again using the proper operator */
2643 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002644}
2645
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002646/*[clinic input]
2647list.__init__
2648
2649 iterable: object(c_default="NULL") = ()
2650 /
2651
2652Built-in mutable sequence.
2653
2654If no argument is given, the constructor creates a new empty list.
2655The argument must be an iterable if specified.
2656[clinic start generated code]*/
2657
Tim Peters6d6c1a32001-08-02 04:15:00 +00002658static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002659list___init___impl(PyListObject *self, PyObject *iterable)
2660/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 /* Verify list invariants established by PyType_GenericAlloc() */
2663 assert(0 <= Py_SIZE(self));
2664 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2665 assert(self->ob_item != NULL ||
2666 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 /* Empty previous contents */
2669 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002670 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002672 if (iterable != NULL) {
2673 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 if (rv == NULL)
2675 return -1;
2676 Py_DECREF(rv);
2677 }
2678 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002679}
2680
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002681/*[clinic input]
2682list.__sizeof__
2683
2684Return the size of the list in memory, in bytes.
2685[clinic start generated code]*/
2686
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002687static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002688list___sizeof___impl(PyListObject *self)
2689/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002692
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002693 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002695}
2696
Raymond Hettinger1021c442003-11-07 15:38:09 +00002697static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002698static PyObject *list_subscript(PyListObject*, PyObject*);
2699
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002700static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002701 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2702 LIST___REVERSED___METHODDEF
2703 LIST___SIZEOF___METHODDEF
2704 LIST_CLEAR_METHODDEF
2705 LIST_COPY_METHODDEF
2706 LIST_APPEND_METHODDEF
2707 LIST_INSERT_METHODDEF
2708 LIST_EXTEND_METHODDEF
2709 LIST_POP_METHODDEF
2710 LIST_REMOVE_METHODDEF
2711 LIST_INDEX_METHODDEF
2712 LIST_COUNT_METHODDEF
2713 LIST_REVERSE_METHODDEF
2714 LIST_SORT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002716};
2717
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002718static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 (lenfunc)list_length, /* sq_length */
2720 (binaryfunc)list_concat, /* sq_concat */
2721 (ssizeargfunc)list_repeat, /* sq_repeat */
2722 (ssizeargfunc)list_item, /* sq_item */
2723 0, /* sq_slice */
2724 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2725 0, /* sq_ass_slice */
2726 (objobjproc)list_contains, /* sq_contains */
2727 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2728 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002729};
2730
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002731static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002732list_subscript(PyListObject* self, PyObject* item)
2733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 if (PyIndex_Check(item)) {
2735 Py_ssize_t i;
2736 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2737 if (i == -1 && PyErr_Occurred())
2738 return NULL;
2739 if (i < 0)
2740 i += PyList_GET_SIZE(self);
2741 return list_item(self, i);
2742 }
2743 else if (PySlice_Check(item)) {
2744 Py_ssize_t start, stop, step, slicelength, cur, i;
2745 PyObject* result;
2746 PyObject* it;
2747 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002748
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002749 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 return NULL;
2751 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002752 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2753 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 if (slicelength <= 0) {
2756 return PyList_New(0);
2757 }
2758 else if (step == 1) {
2759 return list_slice(self, start, stop);
2760 }
2761 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002762 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 src = self->ob_item;
2766 dest = ((PyListObject *)result)->ob_item;
2767 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002768 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 it = src[cur];
2770 Py_INCREF(it);
2771 dest[i] = it;
2772 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002773 Py_SIZE(result) = slicelength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 return result;
2775 }
2776 }
2777 else {
2778 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002779 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 item->ob_type->tp_name);
2781 return NULL;
2782 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002783}
2784
Tim Peters3b01a122002-07-19 02:35:45 +00002785static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002786list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 if (PyIndex_Check(item)) {
2789 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2790 if (i == -1 && PyErr_Occurred())
2791 return -1;
2792 if (i < 0)
2793 i += PyList_GET_SIZE(self);
2794 return list_ass_item(self, i, value);
2795 }
2796 else if (PySlice_Check(item)) {
2797 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002798
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002799 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 return -1;
2801 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002802 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2803 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 if (step == 1)
2806 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 /* Make sure s[5:2] = [..] inserts at the right place:
2809 before 5, not before 2. */
2810 if ((step < 0 && start < stop) ||
2811 (step > 0 && start > stop))
2812 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 if (value == NULL) {
2815 /* delete slice */
2816 PyObject **garbage;
2817 size_t cur;
2818 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002819 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 if (slicelength <= 0)
2822 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 if (step < 0) {
2825 stop = start + 1;
2826 start = stop + step*(slicelength - 1) - 1;
2827 step = -step;
2828 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 garbage = (PyObject**)
2831 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2832 if (!garbage) {
2833 PyErr_NoMemory();
2834 return -1;
2835 }
Tim Peters3b01a122002-07-19 02:35:45 +00002836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 /* drawing pictures might help understand these for
2838 loops. Basically, we memmove the parts of the
2839 list that are *not* part of the slice: step-1
2840 items for each item that is part of the slice,
2841 and then tail end of the list that was not
2842 covered by the slice */
2843 for (cur = start, i = 0;
2844 cur < (size_t)stop;
2845 cur += step, i++) {
2846 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 if (cur + step >= (size_t)Py_SIZE(self)) {
2851 lim = Py_SIZE(self) - cur - 1;
2852 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 memmove(self->ob_item + cur - i,
2855 self->ob_item + cur + 1,
2856 lim * sizeof(PyObject *));
2857 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002858 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 if (cur < (size_t)Py_SIZE(self)) {
2860 memmove(self->ob_item + cur - slicelength,
2861 self->ob_item + cur,
2862 (Py_SIZE(self) - cur) *
2863 sizeof(PyObject *));
2864 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002867 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 for (i = 0; i < slicelength; i++) {
2870 Py_DECREF(garbage[i]);
2871 }
2872 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002873
Victor Stinner35f28032013-11-21 12:16:35 +01002874 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 }
2876 else {
2877 /* assign slice */
2878 PyObject *ins, *seq;
2879 PyObject **garbage, **seqitems, **selfitems;
2880 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 /* protect against a[::-1] = a */
2883 if (self == (PyListObject*)value) {
2884 seq = list_slice((PyListObject*)value, 0,
2885 PyList_GET_SIZE(value));
2886 }
2887 else {
2888 seq = PySequence_Fast(value,
2889 "must assign iterable "
2890 "to extended slice");
2891 }
2892 if (!seq)
2893 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2896 PyErr_Format(PyExc_ValueError,
2897 "attempt to assign sequence of "
2898 "size %zd to extended slice of "
2899 "size %zd",
2900 PySequence_Fast_GET_SIZE(seq),
2901 slicelength);
2902 Py_DECREF(seq);
2903 return -1;
2904 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 if (!slicelength) {
2907 Py_DECREF(seq);
2908 return 0;
2909 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 garbage = (PyObject**)
2912 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2913 if (!garbage) {
2914 Py_DECREF(seq);
2915 PyErr_NoMemory();
2916 return -1;
2917 }
Tim Peters3b01a122002-07-19 02:35:45 +00002918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 selfitems = self->ob_item;
2920 seqitems = PySequence_Fast_ITEMS(seq);
2921 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002922 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 garbage[i] = selfitems[cur];
2924 ins = seqitems[i];
2925 Py_INCREF(ins);
2926 selfitems[cur] = ins;
2927 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 for (i = 0; i < slicelength; i++) {
2930 Py_DECREF(garbage[i]);
2931 }
Tim Peters3b01a122002-07-19 02:35:45 +00002932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 PyMem_FREE(garbage);
2934 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 return 0;
2937 }
2938 }
2939 else {
2940 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002941 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 item->ob_type->tp_name);
2943 return -1;
2944 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002945}
2946
2947static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 (lenfunc)list_length,
2949 (binaryfunc)list_subscript,
2950 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002951};
2952
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002953PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2955 "list",
2956 sizeof(PyListObject),
2957 0,
2958 (destructor)list_dealloc, /* tp_dealloc */
2959 0, /* tp_print */
2960 0, /* tp_getattr */
2961 0, /* tp_setattr */
2962 0, /* tp_reserved */
2963 (reprfunc)list_repr, /* tp_repr */
2964 0, /* tp_as_number */
2965 &list_as_sequence, /* tp_as_sequence */
2966 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002967 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 0, /* tp_call */
2969 0, /* tp_str */
2970 PyObject_GenericGetAttr, /* tp_getattro */
2971 0, /* tp_setattro */
2972 0, /* tp_as_buffer */
2973 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002974 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2975 list___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 (traverseproc)list_traverse, /* tp_traverse */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002977 (inquiry)_list_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 list_richcompare, /* tp_richcompare */
2979 0, /* tp_weaklistoffset */
2980 list_iter, /* tp_iter */
2981 0, /* tp_iternext */
2982 list_methods, /* tp_methods */
2983 0, /* tp_members */
2984 0, /* tp_getset */
2985 0, /* tp_base */
2986 0, /* tp_dict */
2987 0, /* tp_descr_get */
2988 0, /* tp_descr_set */
2989 0, /* tp_dictoffset */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002990 (initproc)list___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 PyType_GenericAlloc, /* tp_alloc */
2992 PyType_GenericNew, /* tp_new */
2993 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002994};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002995
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002996/*********************** List Iterator **************************/
2997
2998typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02003000 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003002} listiterobject;
3003
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003004static void listiter_dealloc(listiterobject *);
3005static int listiter_traverse(listiterobject *, visitproc, void *);
3006static PyObject *listiter_next(listiterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303007static PyObject *listiter_len(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003008static PyObject *listiter_reduce_general(void *_it, int forward);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303009static PyObject *listiter_reduce(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003010static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00003011
Armin Rigof5b3e362006-02-11 21:32:43 +00003012PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003013PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3014PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003015
3016static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003018 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3019 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00003021};
3022
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003023PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3025 "list_iterator", /* tp_name */
3026 sizeof(listiterobject), /* tp_basicsize */
3027 0, /* tp_itemsize */
3028 /* methods */
3029 (destructor)listiter_dealloc, /* tp_dealloc */
3030 0, /* tp_print */
3031 0, /* tp_getattr */
3032 0, /* tp_setattr */
3033 0, /* tp_reserved */
3034 0, /* tp_repr */
3035 0, /* tp_as_number */
3036 0, /* tp_as_sequence */
3037 0, /* tp_as_mapping */
3038 0, /* tp_hash */
3039 0, /* tp_call */
3040 0, /* tp_str */
3041 PyObject_GenericGetAttr, /* tp_getattro */
3042 0, /* tp_setattro */
3043 0, /* tp_as_buffer */
3044 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3045 0, /* tp_doc */
3046 (traverseproc)listiter_traverse, /* tp_traverse */
3047 0, /* tp_clear */
3048 0, /* tp_richcompare */
3049 0, /* tp_weaklistoffset */
3050 PyObject_SelfIter, /* tp_iter */
3051 (iternextfunc)listiter_next, /* tp_iternext */
3052 listiter_methods, /* tp_methods */
3053 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003054};
Raymond Hettinger1021c442003-11-07 15:38:09 +00003055
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003056
3057static PyObject *
3058list_iter(PyObject *seq)
3059{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 if (!PyList_Check(seq)) {
3063 PyErr_BadInternalCall();
3064 return NULL;
3065 }
3066 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3067 if (it == NULL)
3068 return NULL;
3069 it->it_index = 0;
3070 Py_INCREF(seq);
3071 it->it_seq = (PyListObject *)seq;
3072 _PyObject_GC_TRACK(it);
3073 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003074}
3075
3076static void
3077listiter_dealloc(listiterobject *it)
3078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 _PyObject_GC_UNTRACK(it);
3080 Py_XDECREF(it->it_seq);
3081 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003082}
3083
3084static int
3085listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 Py_VISIT(it->it_seq);
3088 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003089}
3090
3091static PyObject *
3092listiter_next(listiterobject *it)
3093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 PyListObject *seq;
3095 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 assert(it != NULL);
3098 seq = it->it_seq;
3099 if (seq == NULL)
3100 return NULL;
3101 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 if (it->it_index < PyList_GET_SIZE(seq)) {
3104 item = PyList_GET_ITEM(seq, it->it_index);
3105 ++it->it_index;
3106 Py_INCREF(item);
3107 return item;
3108 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003111 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003113}
3114
3115static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303116listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 Py_ssize_t len;
3119 if (it->it_seq) {
3120 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3121 if (len >= 0)
3122 return PyLong_FromSsize_t(len);
3123 }
3124 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003125}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003126
3127static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303128listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003129{
3130 return listiter_reduce_general(it, 1);
3131}
3132
3133static PyObject *
3134listiter_setstate(listiterobject *it, PyObject *state)
3135{
Victor Stinner7660b882013-06-24 23:59:24 +02003136 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003137 if (index == -1 && PyErr_Occurred())
3138 return NULL;
3139 if (it->it_seq != NULL) {
3140 if (index < 0)
3141 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003142 else if (index > PyList_GET_SIZE(it->it_seq))
3143 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003144 it->it_index = index;
3145 }
3146 Py_RETURN_NONE;
3147}
3148
Raymond Hettinger1021c442003-11-07 15:38:09 +00003149/*********************** List Reverse Iterator **************************/
3150
3151typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 PyObject_HEAD
3153 Py_ssize_t it_index;
3154 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00003155} listreviterobject;
3156
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003157static void listreviter_dealloc(listreviterobject *);
3158static int listreviter_traverse(listreviterobject *, visitproc, void *);
3159static PyObject *listreviter_next(listreviterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303160static PyObject *listreviter_len(listreviterobject *, PyObject *);
3161static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003162static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003163
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003164static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003166 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3167 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003169};
3170
Raymond Hettinger1021c442003-11-07 15:38:09 +00003171PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3173 "list_reverseiterator", /* tp_name */
3174 sizeof(listreviterobject), /* tp_basicsize */
3175 0, /* tp_itemsize */
3176 /* methods */
3177 (destructor)listreviter_dealloc, /* tp_dealloc */
3178 0, /* tp_print */
3179 0, /* tp_getattr */
3180 0, /* tp_setattr */
3181 0, /* tp_reserved */
3182 0, /* tp_repr */
3183 0, /* tp_as_number */
3184 0, /* tp_as_sequence */
3185 0, /* tp_as_mapping */
3186 0, /* tp_hash */
3187 0, /* tp_call */
3188 0, /* tp_str */
3189 PyObject_GenericGetAttr, /* tp_getattro */
3190 0, /* tp_setattro */
3191 0, /* tp_as_buffer */
3192 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3193 0, /* tp_doc */
3194 (traverseproc)listreviter_traverse, /* tp_traverse */
3195 0, /* tp_clear */
3196 0, /* tp_richcompare */
3197 0, /* tp_weaklistoffset */
3198 PyObject_SelfIter, /* tp_iter */
3199 (iternextfunc)listreviter_next, /* tp_iternext */
3200 listreviter_methods, /* tp_methods */
3201 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00003202};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003203
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003204/*[clinic input]
3205list.__reversed__
3206
3207Return a reverse iterator over the list.
3208[clinic start generated code]*/
3209
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003210static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003211list___reversed___impl(PyListObject *self)
3212/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3217 if (it == NULL)
3218 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003219 assert(PyList_Check(self));
3220 it->it_index = PyList_GET_SIZE(self) - 1;
3221 Py_INCREF(self);
3222 it->it_seq = self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 PyObject_GC_Track(it);
3224 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003225}
3226
3227static void
3228listreviter_dealloc(listreviterobject *it)
3229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 PyObject_GC_UnTrack(it);
3231 Py_XDECREF(it->it_seq);
3232 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003233}
3234
3235static int
3236listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 Py_VISIT(it->it_seq);
3239 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003240}
3241
3242static PyObject *
3243listreviter_next(listreviterobject *it)
3244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003246 Py_ssize_t index;
3247 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003248
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003249 assert(it != NULL);
3250 seq = it->it_seq;
3251 if (seq == NULL) {
3252 return NULL;
3253 }
3254 assert(PyList_Check(seq));
3255
3256 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 if (index>=0 && index < PyList_GET_SIZE(seq)) {
3258 item = PyList_GET_ITEM(seq, index);
3259 it->it_index--;
3260 Py_INCREF(item);
3261 return item;
3262 }
3263 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003264 it->it_seq = NULL;
3265 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003267}
3268
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003269static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303270listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 Py_ssize_t len = it->it_index + 1;
3273 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3274 len = 0;
3275 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003276}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003277
3278static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303279listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003280{
3281 return listiter_reduce_general(it, 0);
3282}
3283
3284static PyObject *
3285listreviter_setstate(listreviterobject *it, PyObject *state)
3286{
3287 Py_ssize_t index = PyLong_AsSsize_t(state);
3288 if (index == -1 && PyErr_Occurred())
3289 return NULL;
3290 if (it->it_seq != NULL) {
3291 if (index < -1)
3292 index = -1;
3293 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3294 index = PyList_GET_SIZE(it->it_seq) - 1;
3295 it->it_index = index;
3296 }
3297 Py_RETURN_NONE;
3298}
3299
3300/* common pickling support */
3301
3302static PyObject *
3303listiter_reduce_general(void *_it, int forward)
3304{
3305 PyObject *list;
3306
3307 /* the objects are not the same, index is of different types! */
3308 if (forward) {
3309 listiterobject *it = (listiterobject *)_it;
3310 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02003311 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003312 it->it_seq, it->it_index);
3313 } else {
3314 listreviterobject *it = (listreviterobject *)_it;
3315 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +02003316 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("reversed"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003317 it->it_seq, it->it_index);
3318 }
3319 /* empty iterator, create an empty list */
3320 list = PyList_New(0);
3321 if (list == NULL)
3322 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02003323 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003324}