blob: e85fa5c526355e780b425f988689b9bce8247371 [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
Pablo Galindo372d7052018-10-28 20:16:26 +000079static int
80list_preallocate_exact(PyListObject *self, Py_ssize_t size)
81{
82 assert(self->ob_item == NULL);
83
84 PyObject **items;
85 size_t allocated;
86
87 allocated = (size_t)size;
88 if (allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
89 PyErr_NoMemory();
90 return -1;
91 }
92
93 if (size == 0) {
94 allocated = 0;
95 }
96 items = (PyObject **)PyMem_New(PyObject*, allocated);
97 if (items == NULL) {
98 PyErr_NoMemory();
99 return -1;
100 }
101 self->ob_item = items;
102 self->allocated = allocated;
103 return 0;
104}
105
Christian Heimes77c02eb2008-02-09 02:18:51 +0000106/* Debug statistic to compare allocations with reuse through the free list */
107#undef SHOW_ALLOC_COUNT
108#ifdef SHOW_ALLOC_COUNT
109static size_t count_alloc = 0;
110static size_t count_reuse = 0;
111
112static void
113show_alloc(void)
114{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200115 PyInterpreterState *interp = _PyInterpreterState_Get();
Eddie Elizondo745dc652018-02-21 20:55:18 -0800116 if (!interp->core_config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300117 return;
Victor Stinner25420fe2017-11-20 18:12:22 -0800118 }
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
121 count_alloc);
122 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
123 "d\n", count_reuse);
124 fprintf(stderr, "%.2f%% reuse rate\n\n",
125 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +0000126}
127#endif
128
Raymond Hettinger0468e412004-05-05 05:37:53 +0000129/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000130#ifndef PyList_MAXFREELIST
131#define PyList_MAXFREELIST 80
132#endif
133static PyListObject *free_list[PyList_MAXFREELIST];
134static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000135
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100136int
137PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100140 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 while (numfree) {
142 op = free_list[--numfree];
143 assert(PyList_CheckExact(op));
144 PyObject_GC_Del(op);
145 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100146 return ret;
147}
148
149void
150PyList_Fini(void)
151{
152 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000153}
154
David Malcolm49526f42012-06-22 14:55:41 -0400155/* Print summary info about the state of the optimized allocator */
156void
157_PyList_DebugMallocStats(FILE *out)
158{
159 _PyDebugAllocatorStats(out,
160 "free PyListObject",
161 numfree, sizeof(PyListObject));
162}
163
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000165PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 PyListObject *op;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000168#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 static int initialized = 0;
170 if (!initialized) {
171 Py_AtExit(show_alloc);
172 initialized = 1;
173 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000174#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 if (size < 0) {
177 PyErr_BadInternalCall();
178 return NULL;
179 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 if (numfree) {
181 numfree--;
182 op = free_list[numfree];
183 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000184#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000186#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 } else {
188 op = PyObject_GC_New(PyListObject, &PyList_Type);
189 if (op == NULL)
190 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000191#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000193#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 }
195 if (size <= 0)
196 op->ob_item = NULL;
197 else {
Mark Dickinson5d132382016-08-21 08:55:15 +0100198 op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 if (op->ob_item == NULL) {
200 Py_DECREF(op);
201 return PyErr_NoMemory();
202 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 }
204 Py_SIZE(op) = size;
205 op->allocated = size;
206 _PyObject_GC_TRACK(op);
207 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208}
209
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500210static PyObject *
211list_new_prealloc(Py_ssize_t size)
212{
213 PyListObject *op = (PyListObject *) PyList_New(0);
214 if (size == 0 || op == NULL) {
215 return (PyObject *) op;
216 }
217 assert(op->ob_item == NULL);
218 op->ob_item = PyMem_New(PyObject *, size);
219 if (op->ob_item == NULL) {
220 Py_DECREF(op);
221 return PyErr_NoMemory();
222 }
223 op->allocated = size;
224 return (PyObject *) op;
225}
226
Martin v. Löwis18e16552006-02-15 17:27:45 +0000227Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000228PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 if (!PyList_Check(op)) {
231 PyErr_BadInternalCall();
232 return -1;
233 }
234 else
235 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236}
237
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700238static inline int
239valid_index(Py_ssize_t i, Py_ssize_t limit)
240{
241 /* The cast to size_t lets us use just a single comparison
242 to check whether i is in the range: 0 <= i < limit.
243
244 See: Section 14.2 "Bounds Checking" in the Agner Fog
245 optimization manual found at:
246 https://www.agner.org/optimize/optimizing_cpp.pdf
247 */
248 return (size_t) i < (size_t) limit;
249}
250
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000251static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000252
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000253PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000254PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 if (!PyList_Check(op)) {
257 PyErr_BadInternalCall();
258 return NULL;
259 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700260 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 if (indexerr == NULL) {
262 indexerr = PyUnicode_FromString(
263 "list index out of range");
264 if (indexerr == NULL)
265 return NULL;
266 }
267 PyErr_SetObject(PyExc_IndexError, indexerr);
268 return NULL;
269 }
270 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271}
272
273int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200274PyList_SetItem(PyObject *op, Py_ssize_t i,
275 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200277 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 if (!PyList_Check(op)) {
279 Py_XDECREF(newitem);
280 PyErr_BadInternalCall();
281 return -1;
282 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700283 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 Py_XDECREF(newitem);
285 PyErr_SetString(PyExc_IndexError,
286 "list assignment index out of range");
287 return -1;
288 }
289 p = ((PyListObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300290 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292}
293
294static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000295ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 Py_ssize_t i, n = Py_SIZE(self);
298 PyObject **items;
299 if (v == NULL) {
300 PyErr_BadInternalCall();
301 return -1;
302 }
303 if (n == PY_SSIZE_T_MAX) {
304 PyErr_SetString(PyExc_OverflowError,
305 "cannot add more objects to list");
306 return -1;
307 }
Tim Petersb38e2b62004-07-29 02:29:26 +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 Hettinger4bb95402004-02-13 11:36:39 +0000311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 if (where < 0) {
313 where += n;
314 if (where < 0)
315 where = 0;
316 }
317 if (where > n)
318 where = n;
319 items = self->ob_item;
320 for (i = n; --i >= where; )
321 items[i+1] = items[i];
322 Py_INCREF(v);
323 items[where] = v;
324 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325}
326
327int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000328PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 if (!PyList_Check(op)) {
331 PyErr_BadInternalCall();
332 return -1;
333 }
334 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335}
336
Raymond Hettinger40a03822004-04-12 13:05:09 +0000337static int
338app1(PyListObject *self, PyObject *v)
339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 assert (v != NULL);
343 if (n == PY_SSIZE_T_MAX) {
344 PyErr_SetString(PyExc_OverflowError,
345 "cannot add more objects to list");
346 return -1;
347 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000348
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800349 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 Py_INCREF(v);
353 PyList_SET_ITEM(self, n, v);
354 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000355}
356
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357int
Fred Drakea2f55112000-07-09 15:16:51 +0000358PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (PyList_Check(op) && (newitem != NULL))
361 return app1((PyListObject *)op, newitem);
362 PyErr_BadInternalCall();
363 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364}
365
366/* Methods */
367
368static void
Fred Drakea2f55112000-07-09 15:16:51 +0000369list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 Py_ssize_t i;
372 PyObject_GC_UnTrack(op);
373 Py_TRASHCAN_SAFE_BEGIN(op)
374 if (op->ob_item != NULL) {
375 /* Do it backwards, for Christian Tismer.
376 There's a simple test case where somehow this reduces
377 thrashing when a *very* large list is created and
378 immediately deleted. */
379 i = Py_SIZE(op);
380 while (--i >= 0) {
381 Py_XDECREF(op->ob_item[i]);
382 }
383 PyMem_FREE(op->ob_item);
384 }
385 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
386 free_list[numfree++] = op;
387 else
388 Py_TYPE(op)->tp_free((PyObject *)op);
389 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390}
391
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000392static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000393list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 Py_ssize_t i;
Victor Stinner5c733472013-11-18 21:11:57 +0100396 PyObject *s;
Victor Stinner5c733472013-11-18 21:11:57 +0100397 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200398
399 if (Py_SIZE(v) == 0) {
400 return PyUnicode_FromString("[]");
401 }
402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 i = Py_ReprEnter((PyObject*)v);
404 if (i != 0) {
405 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
406 }
Tim Petersa7259592001-06-16 05:11:17 +0000407
Victor Stinner5c733472013-11-18 21:11:57 +0100408 _PyUnicodeWriter_Init(&writer);
409 writer.overallocate = 1;
Victor Stinnerb8fb1972013-11-18 22:15:44 +0100410 /* "[" + "1" + ", 2" * (len - 1) + "]" */
411 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +0000412
Victor Stinner5c733472013-11-18 21:11:57 +0100413 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200414 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 /* Do repr() on each element. Note that this may mutate the list,
417 so must refetch the list size on each iteration. */
418 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100419 if (i > 0) {
Victor Stinner4a587072013-11-19 12:54:53 +0100420 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
Victor Stinner5c733472013-11-18 21:11:57 +0100421 goto error;
422 }
423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner5c733472013-11-18 21:11:57 +0100425 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200426 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100427
428 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
429 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200430 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100431 }
432 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 }
Victor Stinner5c733472013-11-18 21:11:57 +0100434
Victor Stinner4d3f1092013-11-19 12:09:00 +0100435 writer.overallocate = 0;
Victor Stinner5c733472013-11-18 21:11:57 +0100436 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200437 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100440 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200441
442error:
Victor Stinner5c733472013-11-18 21:11:57 +0100443 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200444 Py_ReprLeave((PyObject *)v);
445 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446}
447
Martin v. Löwis18e16552006-02-15 17:27:45 +0000448static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000449list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000452}
453
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000454static int
Fred Drakea2f55112000-07-09 15:16:51 +0000455list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 Py_ssize_t i;
458 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
461 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
462 Py_EQ);
463 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000464}
465
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000467list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700469 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 if (indexerr == NULL) {
471 indexerr = PyUnicode_FromString(
472 "list index out of range");
473 if (indexerr == NULL)
474 return NULL;
475 }
476 PyErr_SetObject(PyExc_IndexError, indexerr);
477 return NULL;
478 }
479 Py_INCREF(a->ob_item[i]);
480 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000481}
482
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000484list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 PyListObject *np;
487 PyObject **src, **dest;
488 Py_ssize_t i, len;
489 if (ilow < 0)
490 ilow = 0;
491 else if (ilow > Py_SIZE(a))
492 ilow = Py_SIZE(a);
493 if (ihigh < ilow)
494 ihigh = ilow;
495 else if (ihigh > Py_SIZE(a))
496 ihigh = Py_SIZE(a);
497 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500498 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (np == NULL)
500 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 src = a->ob_item + ilow;
503 dest = np->ob_item;
504 for (i = 0; i < len; i++) {
505 PyObject *v = src[i];
506 Py_INCREF(v);
507 dest[i] = v;
508 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500509 Py_SIZE(np) = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000511}
512
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000514PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 if (!PyList_Check(a)) {
517 PyErr_BadInternalCall();
518 return NULL;
519 }
520 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000521}
522
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000524list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 Py_ssize_t size;
527 Py_ssize_t i;
528 PyObject **src, **dest;
529 PyListObject *np;
530 if (!PyList_Check(bb)) {
531 PyErr_Format(PyExc_TypeError,
532 "can only concatenate list (not \"%.200s\") to list",
533 bb->ob_type->tp_name);
534 return NULL;
535 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000537 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000539 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500540 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 if (np == NULL) {
542 return NULL;
543 }
544 src = a->ob_item;
545 dest = np->ob_item;
546 for (i = 0; i < Py_SIZE(a); i++) {
547 PyObject *v = src[i];
548 Py_INCREF(v);
549 dest[i] = v;
550 }
551 src = b->ob_item;
552 dest = np->ob_item + Py_SIZE(a);
553 for (i = 0; i < Py_SIZE(b); i++) {
554 PyObject *v = src[i];
555 Py_INCREF(v);
556 dest[i] = v;
557 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500558 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560#undef b
561}
562
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000563static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000564list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 Py_ssize_t i, j;
567 Py_ssize_t size;
568 PyListObject *np;
569 PyObject **p, **items;
570 PyObject *elem;
571 if (n < 0)
572 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100573 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100575 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (size == 0)
577 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500578 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (np == NULL)
580 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500583 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 elem = a->ob_item[0];
585 for (i = 0; i < n; i++) {
586 items[i] = elem;
587 Py_INCREF(elem);
588 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500590 else {
591 p = np->ob_item;
592 items = a->ob_item;
593 for (i = 0; i < n; i++) {
594 for (j = 0; j < Py_SIZE(a); j++) {
595 *p = items[j];
596 Py_INCREF(*p);
597 p++;
598 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 }
600 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500601 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000603}
604
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000605static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200606_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 Py_ssize_t i;
609 PyObject **item = a->ob_item;
610 if (item != NULL) {
611 /* Because XDECREF can recursively invoke operations on
612 this list, we make it empty first. */
613 i = Py_SIZE(a);
614 Py_SIZE(a) = 0;
615 a->ob_item = NULL;
616 a->allocated = 0;
617 while (--i >= 0) {
618 Py_XDECREF(item[i]);
619 }
620 PyMem_FREE(item);
621 }
622 /* Never fails; the return value can be ignored.
623 Note that there is no guarantee that the list is actually empty
624 at this point, because XDECREF may have populated it again! */
625 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000626}
627
Tim Peters8fc4a912004-07-31 21:53:19 +0000628/* a[ilow:ihigh] = v if v != NULL.
629 * del a[ilow:ihigh] if v == NULL.
630 *
631 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
632 * guaranteed the call cannot fail.
633 */
Armin Rigo93677f02004-07-29 12:40:23 +0000634static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000635list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 /* Because [X]DECREF can recursively invoke list operations on
638 this list, we must postpone all [X]DECREF activity until
639 after the list is back in its canonical shape. Therefore
640 we must allocate an additional array, 'recycle', into which
641 we temporarily copy the items that are deleted from the
642 list. :-( */
643 PyObject *recycle_on_stack[8];
644 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
645 PyObject **item;
646 PyObject **vitem = NULL;
647 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
648 Py_ssize_t n; /* # of elements in replacement list */
649 Py_ssize_t norig; /* # of elements in list getting replaced */
650 Py_ssize_t d; /* Change in size */
651 Py_ssize_t k;
652 size_t s;
653 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 if (v == NULL)
656 n = 0;
657 else {
658 if (a == b) {
659 /* Special case "a[i:j] = a" -- copy b first */
660 v = list_slice(b, 0, Py_SIZE(b));
661 if (v == NULL)
662 return result;
663 result = list_ass_slice(a, ilow, ihigh, v);
664 Py_DECREF(v);
665 return result;
666 }
667 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
668 if(v_as_SF == NULL)
669 goto Error;
670 n = PySequence_Fast_GET_SIZE(v_as_SF);
671 vitem = PySequence_Fast_ITEMS(v_as_SF);
672 }
673 if (ilow < 0)
674 ilow = 0;
675 else if (ilow > Py_SIZE(a))
676 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (ihigh < ilow)
679 ihigh = ilow;
680 else if (ihigh > Py_SIZE(a))
681 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 norig = ihigh - ilow;
684 assert(norig >= 0);
685 d = n - norig;
686 if (Py_SIZE(a) + d == 0) {
687 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200688 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 }
690 item = a->ob_item;
691 /* recycle the items that we are about to remove */
692 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700693 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
694 if (s) {
695 if (s > sizeof(recycle_on_stack)) {
696 recycle = (PyObject **)PyMem_MALLOC(s);
697 if (recycle == NULL) {
698 PyErr_NoMemory();
699 goto Error;
700 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700702 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200706 Py_ssize_t tail;
707 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
708 memmove(&item[ihigh+d], &item[ihigh], tail);
709 if (list_resize(a, Py_SIZE(a) + d) < 0) {
710 memmove(&item[ihigh], &item[ihigh+d], tail);
711 memcpy(&item[ilow], recycle, s);
712 goto Error;
713 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 item = a->ob_item;
715 }
716 else if (d > 0) { /* Insert d items */
717 k = Py_SIZE(a);
718 if (list_resize(a, k+d) < 0)
719 goto Error;
720 item = a->ob_item;
721 memmove(&item[ihigh+d], &item[ihigh],
722 (k - ihigh)*sizeof(PyObject *));
723 }
724 for (k = 0; k < n; k++, ilow++) {
725 PyObject *w = vitem[k];
726 Py_XINCREF(w);
727 item[ilow] = w;
728 }
729 for (k = norig - 1; k >= 0; --k)
730 Py_XDECREF(recycle[k]);
731 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000732 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 if (recycle != recycle_on_stack)
734 PyMem_FREE(recycle);
735 Py_XDECREF(v_as_SF);
736 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000737#undef b
738}
739
Guido van Rossum234f9421993-06-17 12:35:49 +0000740int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000741PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 if (!PyList_Check(a)) {
744 PyErr_BadInternalCall();
745 return -1;
746 }
747 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000748}
749
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000750static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000751list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 PyObject **items;
754 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000755
756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 size = PyList_GET_SIZE(self);
758 if (size == 0 || n == 1) {
759 Py_INCREF(self);
760 return (PyObject *)self;
761 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 if (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200764 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 Py_INCREF(self);
766 return (PyObject *)self;
767 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 if (size > PY_SSIZE_T_MAX / n) {
770 return PyErr_NoMemory();
771 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000772
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800773 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 p = size;
777 items = self->ob_item;
778 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
779 for (j = 0; j < size; j++) {
780 PyObject *o = items[j];
781 Py_INCREF(o);
782 items[p++] = o;
783 }
784 }
785 Py_INCREF(self);
786 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000787}
788
Guido van Rossum4a450d01991-04-03 19:05:18 +0000789static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000790list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000791{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700792 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 PyErr_SetString(PyExc_IndexError,
794 "list assignment index out of range");
795 return -1;
796 }
797 if (v == NULL)
798 return list_ass_slice(a, i, i+1, v);
799 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300800 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000802}
803
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200804/*[clinic input]
805list.insert
806
807 index: Py_ssize_t
808 object: object
809 /
810
811Insert object before index.
812[clinic start generated code]*/
813
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000814static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200815list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
816/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000817{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200818 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 Py_RETURN_NONE;
820 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000821}
822
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200823/*[clinic input]
824list.clear
825
826Remove all items from list.
827[clinic start generated code]*/
828
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000829static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200830list_clear_impl(PyListObject *self)
831/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000832{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200833 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000834 Py_RETURN_NONE;
835}
836
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200837/*[clinic input]
838list.copy
839
840Return a shallow copy of the list.
841[clinic start generated code]*/
842
Eli Benderskycbbaa962011-02-25 05:47:53 +0000843static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200844list_copy_impl(PyListObject *self)
845/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000846{
847 return list_slice(self, 0, Py_SIZE(self));
848}
849
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200850/*[clinic input]
851list.append
852
853 object: object
854 /
855
856Append object to the end of the list.
857[clinic start generated code]*/
858
Eli Benderskycbbaa962011-02-25 05:47:53 +0000859static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200860list_append(PyListObject *self, PyObject *object)
861/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000862{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200863 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 Py_RETURN_NONE;
865 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000866}
867
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200868/*[clinic input]
869list.extend
870
871 iterable: object
872 /
873
874Extend list by appending elements from the iterable.
875[clinic start generated code]*/
876
Barry Warsawdedf6d61998-10-09 16:37:25 +0000877static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200878list_extend(PyListObject *self, PyObject *iterable)
879/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 PyObject *it; /* iter(v) */
882 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200883 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 Py_ssize_t mn; /* m + n */
885 Py_ssize_t i;
886 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 /* Special cases:
889 1) lists and tuples which can use PySequence_Fast ops
890 2) extending self to self requires making a copy first
891 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200892 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
893 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200895 iterable = PySequence_Fast(iterable, "argument must be iterable");
896 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200898 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200900 /* short circuit when iterable is empty */
901 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 Py_RETURN_NONE;
903 }
904 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000905 /* It should not be possible to allocate a list large enough to cause
906 an overflow on any relevant platform */
907 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800908 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200909 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 return NULL;
911 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200912 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 * situation a.extend(a), but the following code works
914 * in that case too. Just make sure to resize self
915 * before calling PySequence_Fast_ITEMS.
916 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200917 /* populate the end of self with iterable's items */
918 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 dest = self->ob_item + m;
920 for (i = 0; i < n; i++) {
921 PyObject *o = src[i];
922 Py_INCREF(o);
923 dest[i] = o;
924 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200925 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 Py_RETURN_NONE;
927 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000928
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200929 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 if (it == NULL)
931 return NULL;
932 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200935 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800936 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 Py_DECREF(it);
938 return NULL;
939 }
940 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000941 if (m > PY_SSIZE_T_MAX - n) {
942 /* m + n overflowed; on the chance that n lied, and there really
943 * is enough room, ignore it. If n was telling the truth, we'll
944 * eventually run out of memory during the loop.
945 */
946 }
947 else {
948 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800950 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 goto error;
952 /* Make the list sane again. */
953 Py_SIZE(self) = m;
954 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 /* Run iterator to exhaustion. */
957 for (;;) {
958 PyObject *item = iternext(it);
959 if (item == NULL) {
960 if (PyErr_Occurred()) {
961 if (PyErr_ExceptionMatches(PyExc_StopIteration))
962 PyErr_Clear();
963 else
964 goto error;
965 }
966 break;
967 }
968 if (Py_SIZE(self) < self->allocated) {
969 /* steals ref */
970 PyList_SET_ITEM(self, Py_SIZE(self), item);
971 ++Py_SIZE(self);
972 }
973 else {
974 int status = app1(self, item);
975 Py_DECREF(item); /* append creates a new ref */
976 if (status < 0)
977 goto error;
978 }
979 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200982 if (Py_SIZE(self) < self->allocated) {
983 if (list_resize(self, Py_SIZE(self)) < 0)
984 goto error;
985 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 Py_DECREF(it);
988 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000989
990 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 Py_DECREF(it);
992 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000993}
994
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000995PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200996_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000997{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200998 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000999}
1000
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001001static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001002list_inplace_concat(PyListObject *self, PyObject *other)
1003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001005
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001006 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 if (result == NULL)
1008 return result;
1009 Py_DECREF(result);
1010 Py_INCREF(self);
1011 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001012}
1013
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001014/*[clinic input]
1015list.pop
1016
1017 index: Py_ssize_t = -1
1018 /
1019
1020Remove and return item at index (default last).
1021
1022Raises IndexError if list is empty or index is out of range.
1023[clinic start generated code]*/
1024
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001025static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001026list_pop_impl(PyListObject *self, Py_ssize_t index)
1027/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 PyObject *v;
1030 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 if (Py_SIZE(self) == 0) {
1033 /* Special-case most common failure cause */
1034 PyErr_SetString(PyExc_IndexError, "pop from empty list");
1035 return NULL;
1036 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001037 if (index < 0)
1038 index += Py_SIZE(self);
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -07001039 if (!valid_index(index, Py_SIZE(self))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1041 return NULL;
1042 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001043 v = self->ob_item[index];
1044 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001046 if (status >= 0)
1047 return v; /* and v now owns the reference the list had */
1048 else
1049 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 }
1051 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001052 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001053 if (status < 0) {
1054 Py_DECREF(v);
1055 return NULL;
1056 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001058}
1059
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001060/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1061static void
1062reverse_slice(PyObject **lo, PyObject **hi)
1063{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 --hi;
1067 while (lo < hi) {
1068 PyObject *t = *lo;
1069 *lo = *hi;
1070 *hi = t;
1071 ++lo;
1072 --hi;
1073 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001074}
1075
Tim Petersa64dc242002-08-01 02:13:36 +00001076/* Lots of code for an adaptive, stable, natural mergesort. There are many
1077 * pieces to this algorithm; read listsort.txt for overviews and details.
1078 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001079
Daniel Stutzbach98338222010-12-02 21:55:33 +00001080/* A sortslice contains a pointer to an array of keys and a pointer to
1081 * an array of corresponding values. In other words, keys[i]
1082 * corresponds with values[i]. If values == NULL, then the keys are
1083 * also the values.
1084 *
1085 * Several convenience routines are provided here, so that keys and
1086 * values are always moved in sync.
1087 */
1088
1089typedef struct {
1090 PyObject **keys;
1091 PyObject **values;
1092} sortslice;
1093
1094Py_LOCAL_INLINE(void)
1095sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1096{
1097 s1->keys[i] = s2->keys[j];
1098 if (s1->values != NULL)
1099 s1->values[i] = s2->values[j];
1100}
1101
1102Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001103sortslice_copy_incr(sortslice *dst, sortslice *src)
1104{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001105 *dst->keys++ = *src->keys++;
1106 if (dst->values != NULL)
1107 *dst->values++ = *src->values++;
1108}
1109
1110Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001111sortslice_copy_decr(sortslice *dst, sortslice *src)
1112{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001113 *dst->keys-- = *src->keys--;
1114 if (dst->values != NULL)
1115 *dst->values-- = *src->values--;
1116}
1117
1118
1119Py_LOCAL_INLINE(void)
1120sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001121 Py_ssize_t n)
1122{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001123 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1124 if (s1->values != NULL)
1125 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1126}
1127
1128Py_LOCAL_INLINE(void)
1129sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001130 Py_ssize_t n)
1131{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001132 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1133 if (s1->values != NULL)
1134 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1135}
1136
1137Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001138sortslice_advance(sortslice *slice, Py_ssize_t n)
1139{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001140 slice->keys += n;
1141 if (slice->values != NULL)
1142 slice->values += n;
1143}
1144
embg1e34da42018-01-28 20:03:23 -07001145/* Comparison function: ms->key_compare, which is set at run-time in
1146 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001147 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1148 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001149
embg1e34da42018-01-28 20:03:23 -07001150#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001151
1152/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001153 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1154 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1155*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001156#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001158
embg1e34da42018-01-28 20:03:23 -07001159/* The maximum number of entries in a MergeState's pending-runs stack.
1160 * This is enough to sort arrays of size up to about
1161 * 32 * phi ** MAX_MERGE_PENDING
1162 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1163 * with 2**64 elements.
1164 */
1165#define MAX_MERGE_PENDING 85
1166
1167/* When we get into galloping mode, we stay there until both runs win less
1168 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1169 */
1170#define MIN_GALLOP 7
1171
1172/* Avoid malloc for small temp arrays. */
1173#define MERGESTATE_TEMP_SIZE 256
1174
1175/* One MergeState exists on the stack per invocation of mergesort. It's just
1176 * a convenient way to pass state around among the helper functions.
1177 */
1178struct s_slice {
1179 sortslice base;
1180 Py_ssize_t len;
1181};
1182
1183typedef struct s_MergeState MergeState;
1184struct s_MergeState {
1185 /* This controls when we get *into* galloping mode. It's initialized
1186 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1187 * random data, and lower for highly structured data.
1188 */
1189 Py_ssize_t min_gallop;
1190
1191 /* 'a' is temp storage to help with merges. It contains room for
1192 * alloced entries.
1193 */
1194 sortslice a; /* may point to temparray below */
1195 Py_ssize_t alloced;
1196
1197 /* A stack of n pending runs yet to be merged. Run #i starts at
1198 * address base[i] and extends for len[i] elements. It's always
1199 * true (so long as the indices are in bounds) that
1200 *
1201 * pending[i].base + pending[i].len == pending[i+1].base
1202 *
1203 * so we could cut the storage for this, but it's a minor amount,
1204 * and keeping all the info explicit simplifies the code.
1205 */
1206 int n;
1207 struct s_slice pending[MAX_MERGE_PENDING];
1208
1209 /* 'a' points to this when possible, rather than muck with malloc. */
1210 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1211
1212 /* This is the function we will use to compare two keys,
1213 * even when none of our special cases apply and we have to use
1214 * safe_object_compare. */
1215 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1216
1217 /* This function is used by unsafe_object_compare to optimize comparisons
1218 * when we know our list is type-homogeneous but we can't assume anything else.
1219 * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1220 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1221
1222 /* This function is used by unsafe_tuple_compare to compare the first elements
1223 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1224 * we can assume more, and use one of the special-case compares. */
1225 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1226};
1227
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001228/* binarysort is the best method for sorting small arrays: it does
1229 few compares, but can do data movement quadratic in the number of
1230 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001231 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001232 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001233 On entry, must have lo <= start <= hi, and that [lo, start) is already
1234 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001235 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001236 Even in case of error, the output slice will be some permutation of
1237 the input (nothing is lost or duplicated).
1238*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001239static int
embg1e34da42018-01-28 20:03:23 -07001240binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001241{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001242 Py_ssize_t k;
1243 PyObject **l, **p, **r;
1244 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001245
Daniel Stutzbach98338222010-12-02 21:55:33 +00001246 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001248 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 ++start;
1250 for (; start < hi; ++start) {
1251 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001252 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 r = start;
1254 pivot = *r;
1255 /* Invariants:
1256 * pivot >= all in [lo, l).
1257 * pivot < all in [r, start).
1258 * The second is vacuously true at the start.
1259 */
1260 assert(l < r);
1261 do {
1262 p = l + ((r - l) >> 1);
1263 IFLT(pivot, *p)
1264 r = p;
1265 else
1266 l = p+1;
1267 } while (l < r);
1268 assert(l == r);
1269 /* The invariants still hold, so pivot >= all in [lo, l) and
1270 pivot < all in [l, start), so pivot belongs at l. Note
1271 that if there are elements equal to pivot, l points to the
1272 first slot after them -- that's why this sort is stable.
1273 Slide over to make room.
1274 Caution: using memmove is much slower under MSVC 5;
1275 we're not usually moving many slots. */
1276 for (p = start; p > l; --p)
1277 *p = *(p-1);
1278 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001279 if (lo.values != NULL) {
1280 Py_ssize_t offset = lo.values - lo.keys;
1281 p = start + offset;
1282 pivot = *p;
1283 l += offset;
1284 for (p = start + offset; p > l; --p)
1285 *p = *(p-1);
1286 *l = pivot;
1287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 }
1289 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001290
1291 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001293}
1294
Tim Petersa64dc242002-08-01 02:13:36 +00001295/*
1296Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1297is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001298
Tim Petersa64dc242002-08-01 02:13:36 +00001299 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001300
Tim Petersa64dc242002-08-01 02:13:36 +00001301or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001302
Tim Petersa64dc242002-08-01 02:13:36 +00001303 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001304
Tim Petersa64dc242002-08-01 02:13:36 +00001305Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1306For its intended use in a stable mergesort, the strictness of the defn of
1307"descending" is needed so that the caller can safely reverse a descending
1308sequence without violating stability (strict > ensures there are no equal
1309elements to get out of order).
1310
1311Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001312*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001313static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001314count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 Py_ssize_t k;
1317 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 assert(lo < hi);
1320 *descending = 0;
1321 ++lo;
1322 if (lo == hi)
1323 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 n = 2;
1326 IFLT(*lo, *(lo-1)) {
1327 *descending = 1;
1328 for (lo = lo+1; lo < hi; ++lo, ++n) {
1329 IFLT(*lo, *(lo-1))
1330 ;
1331 else
1332 break;
1333 }
1334 }
1335 else {
1336 for (lo = lo+1; lo < hi; ++lo, ++n) {
1337 IFLT(*lo, *(lo-1))
1338 break;
1339 }
1340 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001343fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001345}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001346
Tim Petersa64dc242002-08-01 02:13:36 +00001347/*
1348Locate the proper position of key in a sorted vector; if the vector contains
1349an element equal to key, return the position immediately to the left of
1350the leftmost equal element. [gallop_right() does the same except returns
1351the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001352
Tim Petersa64dc242002-08-01 02:13:36 +00001353"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001354
Tim Petersa64dc242002-08-01 02:13:36 +00001355"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1356hint is to the final result, the faster this runs.
1357
1358The return value is the int k in 0..n such that
1359
1360 a[k-1] < key <= a[k]
1361
1362pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1363key belongs at index k; or, IOW, the first k elements of a should precede
1364key, and the last n-k should follow key.
1365
1366Returns -1 on error. See listsort.txt for info on the method.
1367*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001368static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001369gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 Py_ssize_t ofs;
1372 Py_ssize_t lastofs;
1373 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 a += hint;
1378 lastofs = 0;
1379 ofs = 1;
1380 IFLT(*a, key) {
1381 /* a[hint] < key -- gallop right, until
1382 * a[hint + lastofs] < key <= a[hint + ofs]
1383 */
1384 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1385 while (ofs < maxofs) {
1386 IFLT(a[ofs], key) {
1387 lastofs = ofs;
1388 ofs = (ofs << 1) + 1;
1389 if (ofs <= 0) /* int overflow */
1390 ofs = maxofs;
1391 }
1392 else /* key <= a[hint + ofs] */
1393 break;
1394 }
1395 if (ofs > maxofs)
1396 ofs = maxofs;
1397 /* Translate back to offsets relative to &a[0]. */
1398 lastofs += hint;
1399 ofs += hint;
1400 }
1401 else {
1402 /* key <= a[hint] -- gallop left, until
1403 * a[hint - ofs] < key <= a[hint - lastofs]
1404 */
1405 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1406 while (ofs < maxofs) {
1407 IFLT(*(a-ofs), key)
1408 break;
1409 /* key <= a[hint - ofs] */
1410 lastofs = ofs;
1411 ofs = (ofs << 1) + 1;
1412 if (ofs <= 0) /* int overflow */
1413 ofs = maxofs;
1414 }
1415 if (ofs > maxofs)
1416 ofs = maxofs;
1417 /* Translate back to positive offsets relative to &a[0]. */
1418 k = lastofs;
1419 lastofs = hint - ofs;
1420 ofs = hint - k;
1421 }
1422 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1425 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1426 * right of lastofs but no farther right than ofs. Do a binary
1427 * search, with invariant a[lastofs-1] < key <= a[ofs].
1428 */
1429 ++lastofs;
1430 while (lastofs < ofs) {
1431 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 IFLT(a[m], key)
1434 lastofs = m+1; /* a[m] < key */
1435 else
1436 ofs = m; /* key <= a[m] */
1437 }
1438 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1439 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001440
1441fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001443}
1444
1445/*
1446Exactly like gallop_left(), except that if key already exists in a[0:n],
1447finds the position immediately to the right of the rightmost equal value.
1448
1449The return value is the int k in 0..n such that
1450
1451 a[k-1] <= key < a[k]
1452
1453or -1 if error.
1454
1455The code duplication is massive, but this is enough different given that
1456we're sticking to "<" comparisons that it's much harder to follow if
1457written as one routine with yet another "left or right?" flag.
1458*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001459static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001460gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 Py_ssize_t ofs;
1463 Py_ssize_t lastofs;
1464 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 a += hint;
1469 lastofs = 0;
1470 ofs = 1;
1471 IFLT(key, *a) {
1472 /* key < a[hint] -- gallop left, until
1473 * a[hint - ofs] <= key < a[hint - lastofs]
1474 */
1475 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1476 while (ofs < maxofs) {
1477 IFLT(key, *(a-ofs)) {
1478 lastofs = ofs;
1479 ofs = (ofs << 1) + 1;
1480 if (ofs <= 0) /* int overflow */
1481 ofs = maxofs;
1482 }
1483 else /* a[hint - ofs] <= key */
1484 break;
1485 }
1486 if (ofs > maxofs)
1487 ofs = maxofs;
1488 /* Translate back to positive offsets relative to &a[0]. */
1489 k = lastofs;
1490 lastofs = hint - ofs;
1491 ofs = hint - k;
1492 }
1493 else {
1494 /* a[hint] <= key -- gallop right, until
1495 * a[hint + lastofs] <= key < a[hint + ofs]
1496 */
1497 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1498 while (ofs < maxofs) {
1499 IFLT(key, a[ofs])
1500 break;
1501 /* a[hint + ofs] <= key */
1502 lastofs = ofs;
1503 ofs = (ofs << 1) + 1;
1504 if (ofs <= 0) /* int overflow */
1505 ofs = maxofs;
1506 }
1507 if (ofs > maxofs)
1508 ofs = maxofs;
1509 /* Translate back to offsets relative to &a[0]. */
1510 lastofs += hint;
1511 ofs += hint;
1512 }
1513 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1516 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1517 * right of lastofs but no farther right than ofs. Do a binary
1518 * search, with invariant a[lastofs-1] <= key < a[ofs].
1519 */
1520 ++lastofs;
1521 while (lastofs < ofs) {
1522 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 IFLT(key, a[m])
1525 ofs = m; /* key < a[m] */
1526 else
1527 lastofs = m+1; /* a[m] <= key */
1528 }
1529 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1530 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001531
1532fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001534}
1535
Tim Petersa64dc242002-08-01 02:13:36 +00001536/* Conceptually a MergeState's constructor. */
1537static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001538merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001541 if (has_keyfunc) {
1542 /* The temporary space for merging will need at most half the list
1543 * size rounded up. Use the minimum possible space so we can use the
1544 * rest of temparray for other things. In particular, if there is
1545 * enough extra space, listsort() will use it to store the keys.
1546 */
1547 ms->alloced = (list_size + 1) / 2;
1548
1549 /* ms->alloced describes how many keys will be stored at
1550 ms->temparray, but we also need to store the values. Hence,
1551 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1552 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1553 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1554 ms->a.values = &ms->temparray[ms->alloced];
1555 }
1556 else {
1557 ms->alloced = MERGESTATE_TEMP_SIZE;
1558 ms->a.values = NULL;
1559 }
1560 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 ms->n = 0;
1562 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001563}
1564
1565/* Free all the temp memory owned by the MergeState. This must be called
1566 * when you're done with a MergeState, and may be called before then if
1567 * you want to free the temp memory early.
1568 */
1569static void
1570merge_freemem(MergeState *ms)
1571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001573 if (ms->a.keys != ms->temparray)
1574 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001575}
1576
1577/* Ensure enough temp memory for 'need' array slots is available.
1578 * Returns 0 on success and -1 if the memory can't be gotten.
1579 */
1580static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001581merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001582{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001583 int multiplier;
1584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 assert(ms != NULL);
1586 if (need <= ms->alloced)
1587 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001588
1589 multiplier = ms->a.values != NULL ? 2 : 1;
1590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 /* Don't realloc! That can cost cycles to copy the old data, but
1592 * we don't care what's in the block.
1593 */
1594 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001595 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 PyErr_NoMemory();
1597 return -1;
1598 }
embg1e34da42018-01-28 20:03:23 -07001599 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001600 * sizeof(PyObject *));
1601 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001603 if (ms->a.values != NULL)
1604 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 return 0;
1606 }
1607 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001609}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1611 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001612
Daniel Stutzbach98338222010-12-02 21:55:33 +00001613/* Merge the na elements starting at ssa with the nb elements starting at
1614 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1615 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1616 * should have na <= nb. See listsort.txt for more info. Return 0 if
1617 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001618 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001619static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001620merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1621 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001624 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 int result = -1; /* guilty until proved innocent */
1626 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001627
Daniel Stutzbach98338222010-12-02 21:55:33 +00001628 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1629 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 if (MERGE_GETMEM(ms, na) < 0)
1631 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001632 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1633 dest = ssa;
1634 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001635
Daniel Stutzbach98338222010-12-02 21:55:33 +00001636 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 --nb;
1638 if (nb == 0)
1639 goto Succeed;
1640 if (na == 1)
1641 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 min_gallop = ms->min_gallop;
1644 for (;;) {
1645 Py_ssize_t acount = 0; /* # of times A won in a row */
1646 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 /* Do the straightforward thing until (if ever) one run
1649 * appears to win consistently.
1650 */
1651 for (;;) {
1652 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001653 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 if (k) {
1655 if (k < 0)
1656 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001657 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 ++bcount;
1659 acount = 0;
1660 --nb;
1661 if (nb == 0)
1662 goto Succeed;
1663 if (bcount >= min_gallop)
1664 break;
1665 }
1666 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001667 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 ++acount;
1669 bcount = 0;
1670 --na;
1671 if (na == 1)
1672 goto CopyB;
1673 if (acount >= min_gallop)
1674 break;
1675 }
1676 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 /* One run is winning so consistently that galloping may
1679 * be a huge win. So try that, and continue galloping until
1680 * (if ever) neither run appears to be winning consistently
1681 * anymore.
1682 */
1683 ++min_gallop;
1684 do {
1685 assert(na > 1 && nb > 0);
1686 min_gallop -= min_gallop > 1;
1687 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001688 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 acount = k;
1690 if (k) {
1691 if (k < 0)
1692 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001693 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1694 sortslice_advance(&dest, k);
1695 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 na -= k;
1697 if (na == 1)
1698 goto CopyB;
1699 /* na==0 is impossible now if the comparison
1700 * function is consistent, but we can't assume
1701 * that it is.
1702 */
1703 if (na == 0)
1704 goto Succeed;
1705 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001706 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 --nb;
1708 if (nb == 0)
1709 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001710
embg1e34da42018-01-28 20:03:23 -07001711 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 bcount = k;
1713 if (k) {
1714 if (k < 0)
1715 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001716 sortslice_memmove(&dest, 0, &ssb, 0, k);
1717 sortslice_advance(&dest, k);
1718 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 nb -= k;
1720 if (nb == 0)
1721 goto Succeed;
1722 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001723 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 --na;
1725 if (na == 1)
1726 goto CopyB;
1727 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1728 ++min_gallop; /* penalize it for leaving galloping mode */
1729 ms->min_gallop = min_gallop;
1730 }
Tim Petersa64dc242002-08-01 02:13:36 +00001731Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001733Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001735 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001737CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001739 /* The last element of ssa belongs at the end of the merge. */
1740 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1741 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001743}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001744
Daniel Stutzbach98338222010-12-02 21:55:33 +00001745/* Merge the na elements starting at pa with the nb elements starting at
1746 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1747 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1748 * should have na >= nb. See listsort.txt for more info. Return 0 if
1749 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001750 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001751static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001752merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1753 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001756 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001759
Daniel Stutzbach98338222010-12-02 21:55:33 +00001760 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1761 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 if (MERGE_GETMEM(ms, nb) < 0)
1763 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001764 dest = ssb;
1765 sortslice_advance(&dest, nb-1);
1766 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1767 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001769 ssb.keys = ms->a.keys + nb - 1;
1770 if (ssb.values != NULL)
1771 ssb.values = ms->a.values + nb - 1;
1772 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001773
Daniel Stutzbach98338222010-12-02 21:55:33 +00001774 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 --na;
1776 if (na == 0)
1777 goto Succeed;
1778 if (nb == 1)
1779 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 min_gallop = ms->min_gallop;
1782 for (;;) {
1783 Py_ssize_t acount = 0; /* # of times A won in a row */
1784 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 /* Do the straightforward thing until (if ever) one run
1787 * appears to win consistently.
1788 */
1789 for (;;) {
1790 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001791 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 if (k) {
1793 if (k < 0)
1794 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001795 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 ++acount;
1797 bcount = 0;
1798 --na;
1799 if (na == 0)
1800 goto Succeed;
1801 if (acount >= min_gallop)
1802 break;
1803 }
1804 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001805 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 ++bcount;
1807 acount = 0;
1808 --nb;
1809 if (nb == 1)
1810 goto CopyA;
1811 if (bcount >= min_gallop)
1812 break;
1813 }
1814 }
Tim Petersa64dc242002-08-01 02:13:36 +00001815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 /* One run is winning so consistently that galloping may
1817 * be a huge win. So try that, and continue galloping until
1818 * (if ever) neither run appears to be winning consistently
1819 * anymore.
1820 */
1821 ++min_gallop;
1822 do {
1823 assert(na > 0 && nb > 1);
1824 min_gallop -= min_gallop > 1;
1825 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001826 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 if (k < 0)
1828 goto Fail;
1829 k = na - k;
1830 acount = k;
1831 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001832 sortslice_advance(&dest, -k);
1833 sortslice_advance(&ssa, -k);
1834 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 na -= k;
1836 if (na == 0)
1837 goto Succeed;
1838 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001839 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 --nb;
1841 if (nb == 1)
1842 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001843
embg1e34da42018-01-28 20:03:23 -07001844 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 if (k < 0)
1846 goto Fail;
1847 k = nb - k;
1848 bcount = k;
1849 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001850 sortslice_advance(&dest, -k);
1851 sortslice_advance(&ssb, -k);
1852 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 nb -= k;
1854 if (nb == 1)
1855 goto CopyA;
1856 /* nb==0 is impossible now if the comparison
1857 * function is consistent, but we can't assume
1858 * that it is.
1859 */
1860 if (nb == 0)
1861 goto Succeed;
1862 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001863 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 --na;
1865 if (na == 0)
1866 goto Succeed;
1867 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1868 ++min_gallop; /* penalize it for leaving galloping mode */
1869 ms->min_gallop = min_gallop;
1870 }
Tim Petersa64dc242002-08-01 02:13:36 +00001871Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001873Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001875 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001877CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001879 /* The first element of ssb belongs at the front of the merge. */
1880 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1881 sortslice_advance(&dest, -na);
1882 sortslice_advance(&ssa, -na);
1883 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001885}
1886
1887/* Merge the two runs at stack indices i and i+1.
1888 * Returns 0 on success, -1 on error.
1889 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001890static Py_ssize_t
1891merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001892{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001893 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 Py_ssize_t na, nb;
1895 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 assert(ms != NULL);
1898 assert(ms->n >= 2);
1899 assert(i >= 0);
1900 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001901
Daniel Stutzbach98338222010-12-02 21:55:33 +00001902 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001904 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 nb = ms->pending[i+1].len;
1906 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001907 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 /* Record the length of the combined runs; if i is the 3rd-last
1910 * run now, also slide over the last run (which isn't involved
1911 * in this merge). The current run i+1 goes away in any case.
1912 */
1913 ms->pending[i].len = na + nb;
1914 if (i == ms->n - 3)
1915 ms->pending[i+1] = ms->pending[i+2];
1916 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 /* Where does b start in a? Elements in a before that can be
1919 * ignored (already in place).
1920 */
embg1e34da42018-01-28 20:03:23 -07001921 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 if (k < 0)
1923 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001924 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 na -= k;
1926 if (na == 0)
1927 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 /* Where does a end in b? Elements in b after that can be
1930 * ignored (already in place).
1931 */
embg1e34da42018-01-28 20:03:23 -07001932 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 if (nb <= 0)
1934 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 /* Merge what remains of the runs, using a temp array with
1937 * min(na, nb) elements.
1938 */
1939 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001940 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001942 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001943}
1944
1945/* Examine the stack of runs waiting to be merged, merging adjacent runs
1946 * until the stack invariants are re-established:
1947 *
1948 * 1. len[-3] > len[-2] + len[-1]
1949 * 2. len[-2] > len[-1]
1950 *
1951 * See listsort.txt for more info.
1952 *
1953 * Returns 0 on success, -1 on error.
1954 */
1955static int
1956merge_collapse(MergeState *ms)
1957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 assert(ms);
1961 while (ms->n > 1) {
1962 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001963 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1964 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 if (p[n-1].len < p[n+1].len)
1966 --n;
1967 if (merge_at(ms, n) < 0)
1968 return -1;
1969 }
1970 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001971 if (merge_at(ms, n) < 0)
1972 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 }
1974 else
1975 break;
1976 }
1977 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001978}
1979
1980/* Regardless of invariants, merge all runs on the stack until only one
1981 * remains. This is used at the end of the mergesort.
1982 *
1983 * Returns 0 on success, -1 on error.
1984 */
1985static int
1986merge_force_collapse(MergeState *ms)
1987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 assert(ms);
1991 while (ms->n > 1) {
1992 Py_ssize_t n = ms->n - 2;
1993 if (n > 0 && p[n-1].len < p[n+1].len)
1994 --n;
1995 if (merge_at(ms, n) < 0)
1996 return -1;
1997 }
1998 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001999}
2000
2001/* Compute a good value for the minimum run length; natural runs shorter
2002 * than this are boosted artificially via binary insertion.
2003 *
2004 * If n < 64, return n (it's too small to bother with fancy stuff).
2005 * Else if n is an exact power of 2, return 32.
2006 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
2007 * strictly less than, an exact power of 2.
2008 *
2009 * See listsort.txt for more info.
2010 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002011static Py_ssize_t
2012merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00002013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00002015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 assert(n >= 0);
2017 while (n >= 64) {
2018 r |= n & 1;
2019 n >>= 1;
2020 }
2021 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002022}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00002023
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002024static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00002025reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002026{
Daniel Stutzbach98338222010-12-02 21:55:33 +00002027 reverse_slice(s->keys, &s->keys[n]);
2028 if (s->values != NULL)
2029 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002030}
2031
embg1e34da42018-01-28 20:03:23 -07002032/* Here we define custom comparison functions to optimize for the cases one commonly
2033 * encounters in practice: homogeneous lists, often of one of the basic types. */
2034
2035/* This struct holds the comparison function and helper functions
2036 * selected in the pre-sort check. */
2037
2038/* These are the special case compare functions.
2039 * ms->key_compare will always point to one of these: */
2040
2041/* Heterogeneous compare: default, always safe to fall back on. */
2042static int
2043safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2044{
2045 /* No assumptions necessary! */
2046 return PyObject_RichCompareBool(v, w, Py_LT);
2047}
2048
2049/* Homogeneous compare: safe for any two compareable objects of the same type.
2050 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2051 * pre-sort check.)
2052 */
2053static int
2054unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2055{
2056 PyObject *res_obj; int res;
2057
2058 /* No assumptions, because we check first: */
2059 if (v->ob_type->tp_richcompare != ms->key_richcompare)
2060 return PyObject_RichCompareBool(v, w, Py_LT);
2061
2062 assert(ms->key_richcompare != NULL);
2063 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2064
2065 if (res_obj == Py_NotImplemented) {
2066 Py_DECREF(res_obj);
2067 return PyObject_RichCompareBool(v, w, Py_LT);
2068 }
2069 if (res_obj == NULL)
2070 return -1;
2071
2072 if (PyBool_Check(res_obj)) {
2073 res = (res_obj == Py_True);
2074 }
2075 else {
2076 res = PyObject_IsTrue(res_obj);
2077 }
2078 Py_DECREF(res_obj);
2079
2080 /* Note that we can't assert
2081 * res == PyObject_RichCompareBool(v, w, Py_LT);
2082 * because of evil compare functions like this:
2083 * lambda a, b: int(random.random() * 3) - 1)
2084 * (which is actually in test_sort.py) */
2085 return res;
2086}
2087
2088/* Latin string compare: safe for any two latin (one byte per char) strings. */
2089static int
2090unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2091{
Victor Stinner8017b802018-01-29 13:47:06 +01002092 Py_ssize_t len;
2093 int res;
embg1e34da42018-01-28 20:03:23 -07002094
2095 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2096 assert(v->ob_type == w->ob_type);
2097 assert(v->ob_type == &PyUnicode_Type);
2098 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2099 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2100
2101 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2102 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2103
2104 res = (res != 0 ?
2105 res < 0 :
2106 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2107
2108 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2109 return res;
2110}
2111
2112/* Bounded int compare: compare any two longs that fit in a single machine word. */
2113static int
2114unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2115{
2116 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2117
2118 /* Modified from Objects/longobject.c:long_compare, assuming: */
2119 assert(v->ob_type == w->ob_type);
2120 assert(v->ob_type == &PyLong_Type);
2121 assert(Py_ABS(Py_SIZE(v)) <= 1);
2122 assert(Py_ABS(Py_SIZE(w)) <= 1);
2123
2124 vl = (PyLongObject*)v;
2125 wl = (PyLongObject*)w;
2126
2127 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2128 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2129
2130 if (Py_SIZE(vl) < 0)
2131 v0 = -v0;
2132 if (Py_SIZE(wl) < 0)
2133 w0 = -w0;
2134
2135 res = v0 < w0;
2136 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2137 return res;
2138}
2139
2140/* Float compare: compare any two floats. */
2141static int
2142unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2143{
2144 int res;
2145
2146 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2147 assert(v->ob_type == w->ob_type);
2148 assert(v->ob_type == &PyFloat_Type);
2149
2150 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2151 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2152 return res;
2153}
2154
2155/* Tuple compare: compare *any* two tuples, using
2156 * ms->tuple_elem_compare to compare the first elements, which is set
2157 * using the same pre-sort check as we use for ms->key_compare,
2158 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2159 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2160 * that most tuple compares don't involve x[1:]. */
2161static int
2162unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2163{
2164 PyTupleObject *vt, *wt;
2165 Py_ssize_t i, vlen, wlen;
2166 int k;
2167
2168 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2169 assert(v->ob_type == w->ob_type);
2170 assert(v->ob_type == &PyTuple_Type);
2171 assert(Py_SIZE(v) > 0);
2172 assert(Py_SIZE(w) > 0);
2173
2174 vt = (PyTupleObject *)v;
2175 wt = (PyTupleObject *)w;
2176
2177 vlen = Py_SIZE(vt);
2178 wlen = Py_SIZE(wt);
2179
2180 for (i = 0; i < vlen && i < wlen; i++) {
2181 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2182 if (k < 0)
2183 return -1;
2184 if (!k)
2185 break;
2186 }
2187
2188 if (i >= vlen || i >= wlen)
2189 return vlen < wlen;
2190
2191 if (i == 0)
2192 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2193 else
2194 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2195}
2196
Tim Petersa64dc242002-08-01 02:13:36 +00002197/* An adaptive, stable, natural mergesort. See listsort.txt.
2198 * Returns Py_None on success, NULL on error. Even in case of error, the
2199 * list will be some permutation of its input state (nothing is lost or
2200 * duplicated).
2201 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002202/*[clinic input]
2203list.sort
2204
2205 *
2206 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002207 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002208
2209Stable sort *IN PLACE*.
2210[clinic start generated code]*/
2211
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002212static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002213list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002214/*[clinic end generated code: output=57b9f9c5e23fbe42 input=b0fcf743982c5b90]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 Py_ssize_t nremaining;
2218 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002219 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 Py_ssize_t saved_ob_size, saved_allocated;
2221 PyObject **saved_ob_item;
2222 PyObject **final_ob_item;
2223 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002225 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002228 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 if (keyfunc == Py_None)
2230 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 /* The list is temporarily made empty, so that mutations performed
2233 * by comparison functions can't affect the slice of memory we're
2234 * sorting (allowing mutations during sorting is a core-dump
2235 * factory, since ob_item may change).
2236 */
2237 saved_ob_size = Py_SIZE(self);
2238 saved_ob_item = self->ob_item;
2239 saved_allocated = self->allocated;
2240 Py_SIZE(self) = 0;
2241 self->ob_item = NULL;
2242 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002243
Daniel Stutzbach98338222010-12-02 21:55:33 +00002244 if (keyfunc == NULL) {
2245 keys = NULL;
2246 lo.keys = saved_ob_item;
2247 lo.values = NULL;
2248 }
2249 else {
2250 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2251 /* Leverage stack space we allocated but won't otherwise use */
2252 keys = &ms.temparray[saved_ob_size+1];
2253 else {
2254 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002255 if (keys == NULL) {
2256 PyErr_NoMemory();
2257 goto keyfunc_fail;
2258 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002260
2261 for (i = 0; i < saved_ob_size ; i++) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002262 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
2263 NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002264 if (keys[i] == NULL) {
2265 for (i=i-1 ; i>=0 ; i--)
2266 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002267 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002268 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002269 goto keyfunc_fail;
2270 }
2271 }
2272
2273 lo.keys = keys;
2274 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002276
embg1e34da42018-01-28 20:03:23 -07002277
2278 /* The pre-sort check: here's where we decide which compare function to use.
2279 * How much optimization is safe? We test for homogeneity with respect to
2280 * several properties that are expensive to check at compare-time, and
2281 * set ms appropriately. */
2282 if (saved_ob_size > 1) {
2283 /* Assume the first element is representative of the whole list. */
2284 int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2285 Py_SIZE(lo.keys[0]) > 0);
2286
2287 PyTypeObject* key_type = (keys_are_in_tuples ?
2288 PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2289 lo.keys[0]->ob_type);
2290
2291 int keys_are_all_same_type = 1;
2292 int strings_are_latin = 1;
2293 int ints_are_bounded = 1;
2294
2295 /* Prove that assumption by checking every key. */
2296 int i;
2297 for (i=0; i < saved_ob_size; i++) {
2298
2299 if (keys_are_in_tuples &&
2300 !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2301 keys_are_in_tuples = 0;
2302 keys_are_all_same_type = 0;
2303 break;
2304 }
2305
2306 /* Note: for lists of tuples, key is the first element of the tuple
2307 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2308 * for lists of tuples in the if-statement directly above. */
2309 PyObject *key = (keys_are_in_tuples ?
2310 PyTuple_GET_ITEM(lo.keys[i], 0) :
2311 lo.keys[i]);
2312
2313 if (key->ob_type != key_type) {
2314 keys_are_all_same_type = 0;
2315 break;
2316 }
2317
2318 if (key_type == &PyLong_Type) {
2319 if (ints_are_bounded && Py_ABS(Py_SIZE(key)) > 1)
2320 ints_are_bounded = 0;
2321 }
2322 else if (key_type == &PyUnicode_Type){
2323 if (strings_are_latin &&
2324 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND)
2325 strings_are_latin = 0;
2326 }
2327 }
2328
2329 /* Choose the best compare, given what we now know about the keys. */
2330 if (keys_are_all_same_type) {
2331
2332 if (key_type == &PyUnicode_Type && strings_are_latin) {
2333 ms.key_compare = unsafe_latin_compare;
2334 }
2335 else if (key_type == &PyLong_Type && ints_are_bounded) {
2336 ms.key_compare = unsafe_long_compare;
2337 }
2338 else if (key_type == &PyFloat_Type) {
2339 ms.key_compare = unsafe_float_compare;
2340 }
2341 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2342 ms.key_compare = unsafe_object_compare;
2343 }
2344 }
2345 else {
2346 ms.key_compare = safe_object_compare;
2347 }
2348
2349 if (keys_are_in_tuples) {
2350 /* Make sure we're not dealing with tuples of tuples
2351 * (remember: here, key_type refers list [key[0] for key in keys]) */
2352 if (key_type == &PyTuple_Type)
2353 ms.tuple_elem_compare = safe_object_compare;
2354 else
2355 ms.tuple_elem_compare = ms.key_compare;
2356
2357 ms.key_compare = unsafe_tuple_compare;
2358 }
2359 }
2360 /* End of pre-sort check: ms is now set properly! */
2361
Daniel Stutzbach98338222010-12-02 21:55:33 +00002362 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 nremaining = saved_ob_size;
2365 if (nremaining < 2)
2366 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002367
Benjamin Peterson05380642010-08-23 19:35:39 +00002368 /* Reverse sort stability achieved by initially reversing the list,
2369 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002370 if (reverse) {
2371 if (keys != NULL)
2372 reverse_slice(&keys[0], &keys[saved_ob_size]);
2373 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2374 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 /* March over the array once, left to right, finding natural runs,
2377 * and extending short natural runs to minrun elements.
2378 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 minrun = merge_compute_minrun(nremaining);
2380 do {
2381 int descending;
2382 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002385 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 if (n < 0)
2387 goto fail;
2388 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002389 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 /* If short, extend to min(minrun, nremaining). */
2391 if (n < minrun) {
2392 const Py_ssize_t force = nremaining <= minrun ?
2393 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002394 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 goto fail;
2396 n = force;
2397 }
2398 /* Push run onto pending-runs stack, and maybe merge. */
2399 assert(ms.n < MAX_MERGE_PENDING);
2400 ms.pending[ms.n].base = lo;
2401 ms.pending[ms.n].len = n;
2402 ++ms.n;
2403 if (merge_collapse(&ms) < 0)
2404 goto fail;
2405 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002406 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 nremaining -= n;
2408 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 if (merge_force_collapse(&ms) < 0)
2411 goto fail;
2412 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002413 assert(keys == NULL
2414 ? ms.pending[0].base.keys == saved_ob_item
2415 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002417 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002418
2419succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002421fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002422 if (keys != NULL) {
2423 for (i = 0; i < saved_ob_size; i++)
2424 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002425 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002426 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 if (self->allocated != -1 && result != NULL) {
2430 /* The user mucked with the list during the sort,
2431 * and we don't already have another error to report.
2432 */
2433 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2434 result = NULL;
2435 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 if (reverse && saved_ob_size > 1)
2438 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002441
Daniel Stutzbach98338222010-12-02 21:55:33 +00002442keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 final_ob_item = self->ob_item;
2444 i = Py_SIZE(self);
2445 Py_SIZE(self) = saved_ob_size;
2446 self->ob_item = saved_ob_item;
2447 self->allocated = saved_allocated;
2448 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002449 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 guarantee that the list is really empty when it returns */
2451 while (--i >= 0) {
2452 Py_XDECREF(final_ob_item[i]);
2453 }
2454 PyMem_FREE(final_ob_item);
2455 }
2456 Py_XINCREF(result);
2457 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002458}
Tim Peters330f9e92002-07-19 07:05:44 +00002459#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002460#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002461
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002462int
Fred Drakea2f55112000-07-09 15:16:51 +00002463PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 if (v == NULL || !PyList_Check(v)) {
2466 PyErr_BadInternalCall();
2467 return -1;
2468 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002469 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 if (v == NULL)
2471 return -1;
2472 Py_DECREF(v);
2473 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002474}
2475
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002476/*[clinic input]
2477list.reverse
2478
2479Reverse *IN PLACE*.
2480[clinic start generated code]*/
2481
Guido van Rossumb86c5492001-02-12 22:06:02 +00002482static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002483list_reverse_impl(PyListObject *self)
2484/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 if (Py_SIZE(self) > 1)
2487 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2488 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002489}
2490
Guido van Rossum84c76f51990-10-30 13:32:20 +00002491int
Fred Drakea2f55112000-07-09 15:16:51 +00002492PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 if (v == NULL || !PyList_Check(v)) {
2497 PyErr_BadInternalCall();
2498 return -1;
2499 }
2500 if (Py_SIZE(self) > 1)
2501 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2502 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002503}
2504
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002505PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002506PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 PyObject *w;
2509 PyObject **p, **q;
2510 Py_ssize_t n;
2511 if (v == NULL || !PyList_Check(v)) {
2512 PyErr_BadInternalCall();
2513 return NULL;
2514 }
2515 n = Py_SIZE(v);
2516 w = PyTuple_New(n);
2517 if (w == NULL)
2518 return NULL;
2519 p = ((PyTupleObject *)w)->ob_item;
2520 q = ((PyListObject *)v)->ob_item;
2521 while (--n >= 0) {
2522 Py_INCREF(*q);
2523 *p = *q;
2524 p++;
2525 q++;
2526 }
2527 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002528}
2529
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002530/*[clinic input]
2531list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002532
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002533 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002534 start: slice_index(accept={int}) = 0
2535 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002536 /
2537
2538Return first index of value.
2539
2540Raises ValueError if the value is not present.
2541[clinic start generated code]*/
2542
2543static PyObject *
2544list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2545 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002546/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002547{
2548 Py_ssize_t i;
2549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 if (start < 0) {
2551 start += Py_SIZE(self);
2552 if (start < 0)
2553 start = 0;
2554 }
2555 if (stop < 0) {
2556 stop += Py_SIZE(self);
2557 if (stop < 0)
2558 stop = 0;
2559 }
2560 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002561 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 if (cmp > 0)
2563 return PyLong_FromSsize_t(i);
2564 else if (cmp < 0)
2565 return NULL;
2566 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002567 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002569}
2570
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002571/*[clinic input]
2572list.count
2573
2574 value: object
2575 /
2576
2577Return number of occurrences of value.
2578[clinic start generated code]*/
2579
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002580static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002581list_count(PyListObject *self, PyObject *value)
2582/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 Py_ssize_t count = 0;
2585 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002588 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 if (cmp > 0)
2590 count++;
2591 else if (cmp < 0)
2592 return NULL;
2593 }
2594 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002595}
2596
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002597/*[clinic input]
2598list.remove
2599
2600 value: object
2601 /
2602
2603Remove first occurrence of value.
2604
2605Raises ValueError if the value is not present.
2606[clinic start generated code]*/
2607
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002608static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002609list_remove(PyListObject *self, PyObject *value)
2610/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002615 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 if (cmp > 0) {
2617 if (list_ass_slice(self, i, i+1,
2618 (PyObject *)NULL) == 0)
2619 Py_RETURN_NONE;
2620 return NULL;
2621 }
2622 else if (cmp < 0)
2623 return NULL;
2624 }
2625 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2626 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002627}
2628
Jeremy Hylton8caad492000-06-23 14:18:11 +00002629static int
2630list_traverse(PyListObject *o, visitproc visit, void *arg)
2631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 for (i = Py_SIZE(o); --i >= 0; )
2635 Py_VISIT(o->ob_item[i]);
2636 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002637}
2638
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002639static PyObject *
2640list_richcompare(PyObject *v, PyObject *w, int op)
2641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 PyListObject *vl, *wl;
2643 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002644
Brian Curtindfc80e32011-08-10 20:28:54 -05002645 if (!PyList_Check(v) || !PyList_Check(w))
2646 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 vl = (PyListObject *)v;
2649 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2652 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002654 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 else
stratakise8b19652017-11-02 11:32:54 +01002656 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 /* Search for the first index where items are different */
2660 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2661 int k = PyObject_RichCompareBool(vl->ob_item[i],
2662 wl->ob_item[i], Py_EQ);
2663 if (k < 0)
2664 return NULL;
2665 if (!k)
2666 break;
2667 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2670 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002671 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 /* We have an item that differs -- shortcuts for EQ/NE */
2675 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002676 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 }
2678 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002679 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 /* Compare the final item again using the proper operator */
2683 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002684}
2685
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002686/*[clinic input]
2687list.__init__
2688
2689 iterable: object(c_default="NULL") = ()
2690 /
2691
2692Built-in mutable sequence.
2693
2694If no argument is given, the constructor creates a new empty list.
2695The argument must be an iterable if specified.
2696[clinic start generated code]*/
2697
Tim Peters6d6c1a32001-08-02 04:15:00 +00002698static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002699list___init___impl(PyListObject *self, PyObject *iterable)
2700/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 /* Verify list invariants established by PyType_GenericAlloc() */
2703 assert(0 <= Py_SIZE(self));
2704 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2705 assert(self->ob_item != NULL ||
2706 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 /* Empty previous contents */
2709 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002710 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002712 if (iterable != NULL) {
Pablo Galindo372d7052018-10-28 20:16:26 +00002713 if (_PyObject_HasLen(iterable)) {
2714 Py_ssize_t iter_len = PyObject_Size(iterable);
2715 if (iter_len == -1) {
2716 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2717 return -1;
2718 }
2719 PyErr_Clear();
2720 }
2721 if (iter_len > 0 && self->ob_item == NULL
2722 && list_preallocate_exact(self, iter_len)) {
2723 return -1;
2724 }
2725 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002726 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 if (rv == NULL)
2728 return -1;
2729 Py_DECREF(rv);
2730 }
2731 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002732}
2733
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002734/*[clinic input]
2735list.__sizeof__
2736
2737Return the size of the list in memory, in bytes.
2738[clinic start generated code]*/
2739
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002740static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002741list___sizeof___impl(PyListObject *self)
2742/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002745
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002746 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002748}
2749
Raymond Hettinger1021c442003-11-07 15:38:09 +00002750static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002751static PyObject *list_subscript(PyListObject*, PyObject*);
2752
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002753static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002754 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2755 LIST___REVERSED___METHODDEF
2756 LIST___SIZEOF___METHODDEF
2757 LIST_CLEAR_METHODDEF
2758 LIST_COPY_METHODDEF
2759 LIST_APPEND_METHODDEF
2760 LIST_INSERT_METHODDEF
2761 LIST_EXTEND_METHODDEF
2762 LIST_POP_METHODDEF
2763 LIST_REMOVE_METHODDEF
2764 LIST_INDEX_METHODDEF
2765 LIST_COUNT_METHODDEF
2766 LIST_REVERSE_METHODDEF
2767 LIST_SORT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002769};
2770
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002771static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 (lenfunc)list_length, /* sq_length */
2773 (binaryfunc)list_concat, /* sq_concat */
2774 (ssizeargfunc)list_repeat, /* sq_repeat */
2775 (ssizeargfunc)list_item, /* sq_item */
2776 0, /* sq_slice */
2777 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2778 0, /* sq_ass_slice */
2779 (objobjproc)list_contains, /* sq_contains */
2780 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2781 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002782};
2783
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002784static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002785list_subscript(PyListObject* self, PyObject* item)
2786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 if (PyIndex_Check(item)) {
2788 Py_ssize_t i;
2789 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2790 if (i == -1 && PyErr_Occurred())
2791 return NULL;
2792 if (i < 0)
2793 i += PyList_GET_SIZE(self);
2794 return list_item(self, i);
2795 }
2796 else if (PySlice_Check(item)) {
2797 Py_ssize_t start, stop, step, slicelength, cur, i;
2798 PyObject* result;
2799 PyObject* it;
2800 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002801
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002802 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 return NULL;
2804 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002805 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2806 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 if (slicelength <= 0) {
2809 return PyList_New(0);
2810 }
2811 else if (step == 1) {
2812 return list_slice(self, start, stop);
2813 }
2814 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002815 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 src = self->ob_item;
2819 dest = ((PyListObject *)result)->ob_item;
2820 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002821 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 it = src[cur];
2823 Py_INCREF(it);
2824 dest[i] = it;
2825 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002826 Py_SIZE(result) = slicelength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 return result;
2828 }
2829 }
2830 else {
2831 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002832 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 item->ob_type->tp_name);
2834 return NULL;
2835 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002836}
2837
Tim Peters3b01a122002-07-19 02:35:45 +00002838static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002839list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 if (PyIndex_Check(item)) {
2842 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2843 if (i == -1 && PyErr_Occurred())
2844 return -1;
2845 if (i < 0)
2846 i += PyList_GET_SIZE(self);
2847 return list_ass_item(self, i, value);
2848 }
2849 else if (PySlice_Check(item)) {
2850 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002851
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002852 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 return -1;
2854 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002855 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2856 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 if (step == 1)
2859 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 /* Make sure s[5:2] = [..] inserts at the right place:
2862 before 5, not before 2. */
2863 if ((step < 0 && start < stop) ||
2864 (step > 0 && start > stop))
2865 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 if (value == NULL) {
2868 /* delete slice */
2869 PyObject **garbage;
2870 size_t cur;
2871 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002872 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 if (slicelength <= 0)
2875 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 if (step < 0) {
2878 stop = start + 1;
2879 start = stop + step*(slicelength - 1) - 1;
2880 step = -step;
2881 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 garbage = (PyObject**)
2884 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2885 if (!garbage) {
2886 PyErr_NoMemory();
2887 return -1;
2888 }
Tim Peters3b01a122002-07-19 02:35:45 +00002889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 /* drawing pictures might help understand these for
2891 loops. Basically, we memmove the parts of the
2892 list that are *not* part of the slice: step-1
2893 items for each item that is part of the slice,
2894 and then tail end of the list that was not
2895 covered by the slice */
2896 for (cur = start, i = 0;
2897 cur < (size_t)stop;
2898 cur += step, i++) {
2899 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 if (cur + step >= (size_t)Py_SIZE(self)) {
2904 lim = Py_SIZE(self) - cur - 1;
2905 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 memmove(self->ob_item + cur - i,
2908 self->ob_item + cur + 1,
2909 lim * sizeof(PyObject *));
2910 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002911 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 if (cur < (size_t)Py_SIZE(self)) {
2913 memmove(self->ob_item + cur - slicelength,
2914 self->ob_item + cur,
2915 (Py_SIZE(self) - cur) *
2916 sizeof(PyObject *));
2917 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002920 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 for (i = 0; i < slicelength; i++) {
2923 Py_DECREF(garbage[i]);
2924 }
2925 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002926
Victor Stinner35f28032013-11-21 12:16:35 +01002927 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 }
2929 else {
2930 /* assign slice */
2931 PyObject *ins, *seq;
2932 PyObject **garbage, **seqitems, **selfitems;
2933 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002935 /* protect against a[::-1] = a */
2936 if (self == (PyListObject*)value) {
2937 seq = list_slice((PyListObject*)value, 0,
2938 PyList_GET_SIZE(value));
2939 }
2940 else {
2941 seq = PySequence_Fast(value,
2942 "must assign iterable "
2943 "to extended slice");
2944 }
2945 if (!seq)
2946 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2949 PyErr_Format(PyExc_ValueError,
2950 "attempt to assign sequence of "
2951 "size %zd to extended slice of "
2952 "size %zd",
2953 PySequence_Fast_GET_SIZE(seq),
2954 slicelength);
2955 Py_DECREF(seq);
2956 return -1;
2957 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 if (!slicelength) {
2960 Py_DECREF(seq);
2961 return 0;
2962 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 garbage = (PyObject**)
2965 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2966 if (!garbage) {
2967 Py_DECREF(seq);
2968 PyErr_NoMemory();
2969 return -1;
2970 }
Tim Peters3b01a122002-07-19 02:35:45 +00002971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 selfitems = self->ob_item;
2973 seqitems = PySequence_Fast_ITEMS(seq);
2974 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002975 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 garbage[i] = selfitems[cur];
2977 ins = seqitems[i];
2978 Py_INCREF(ins);
2979 selfitems[cur] = ins;
2980 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 for (i = 0; i < slicelength; i++) {
2983 Py_DECREF(garbage[i]);
2984 }
Tim Peters3b01a122002-07-19 02:35:45 +00002985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 PyMem_FREE(garbage);
2987 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 return 0;
2990 }
2991 }
2992 else {
2993 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002994 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 item->ob_type->tp_name);
2996 return -1;
2997 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002998}
2999
3000static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 (lenfunc)list_length,
3002 (binaryfunc)list_subscript,
3003 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003004};
3005
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003006PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3008 "list",
3009 sizeof(PyListObject),
3010 0,
3011 (destructor)list_dealloc, /* tp_dealloc */
3012 0, /* tp_print */
3013 0, /* tp_getattr */
3014 0, /* tp_setattr */
3015 0, /* tp_reserved */
3016 (reprfunc)list_repr, /* tp_repr */
3017 0, /* tp_as_number */
3018 &list_as_sequence, /* tp_as_sequence */
3019 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003020 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 0, /* tp_call */
3022 0, /* tp_str */
3023 PyObject_GenericGetAttr, /* tp_getattro */
3024 0, /* tp_setattro */
3025 0, /* tp_as_buffer */
3026 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003027 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
3028 list___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 (traverseproc)list_traverse, /* tp_traverse */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003030 (inquiry)_list_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 list_richcompare, /* tp_richcompare */
3032 0, /* tp_weaklistoffset */
3033 list_iter, /* tp_iter */
3034 0, /* tp_iternext */
3035 list_methods, /* tp_methods */
3036 0, /* tp_members */
3037 0, /* tp_getset */
3038 0, /* tp_base */
3039 0, /* tp_dict */
3040 0, /* tp_descr_get */
3041 0, /* tp_descr_set */
3042 0, /* tp_dictoffset */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003043 (initproc)list___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 PyType_GenericAlloc, /* tp_alloc */
3045 PyType_GenericNew, /* tp_new */
3046 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003047};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00003048
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003049/*********************** List Iterator **************************/
3050
3051typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02003053 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003055} listiterobject;
3056
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003057static void listiter_dealloc(listiterobject *);
3058static int listiter_traverse(listiterobject *, visitproc, void *);
3059static PyObject *listiter_next(listiterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303060static PyObject *listiter_len(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003061static PyObject *listiter_reduce_general(void *_it, int forward);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303062static PyObject *listiter_reduce(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003063static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00003064
Armin Rigof5b3e362006-02-11 21:32:43 +00003065PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003066PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3067PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003068
3069static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003071 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3072 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00003074};
3075
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003076PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3078 "list_iterator", /* tp_name */
3079 sizeof(listiterobject), /* tp_basicsize */
3080 0, /* tp_itemsize */
3081 /* methods */
3082 (destructor)listiter_dealloc, /* tp_dealloc */
3083 0, /* tp_print */
3084 0, /* tp_getattr */
3085 0, /* tp_setattr */
3086 0, /* tp_reserved */
3087 0, /* tp_repr */
3088 0, /* tp_as_number */
3089 0, /* tp_as_sequence */
3090 0, /* tp_as_mapping */
3091 0, /* tp_hash */
3092 0, /* tp_call */
3093 0, /* tp_str */
3094 PyObject_GenericGetAttr, /* tp_getattro */
3095 0, /* tp_setattro */
3096 0, /* tp_as_buffer */
3097 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3098 0, /* tp_doc */
3099 (traverseproc)listiter_traverse, /* tp_traverse */
3100 0, /* tp_clear */
3101 0, /* tp_richcompare */
3102 0, /* tp_weaklistoffset */
3103 PyObject_SelfIter, /* tp_iter */
3104 (iternextfunc)listiter_next, /* tp_iternext */
3105 listiter_methods, /* tp_methods */
3106 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003107};
Raymond Hettinger1021c442003-11-07 15:38:09 +00003108
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003109
3110static PyObject *
3111list_iter(PyObject *seq)
3112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 if (!PyList_Check(seq)) {
3116 PyErr_BadInternalCall();
3117 return NULL;
3118 }
3119 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3120 if (it == NULL)
3121 return NULL;
3122 it->it_index = 0;
3123 Py_INCREF(seq);
3124 it->it_seq = (PyListObject *)seq;
3125 _PyObject_GC_TRACK(it);
3126 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003127}
3128
3129static void
3130listiter_dealloc(listiterobject *it)
3131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 _PyObject_GC_UNTRACK(it);
3133 Py_XDECREF(it->it_seq);
3134 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003135}
3136
3137static int
3138listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 Py_VISIT(it->it_seq);
3141 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003142}
3143
3144static PyObject *
3145listiter_next(listiterobject *it)
3146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 PyListObject *seq;
3148 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 assert(it != NULL);
3151 seq = it->it_seq;
3152 if (seq == NULL)
3153 return NULL;
3154 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 if (it->it_index < PyList_GET_SIZE(seq)) {
3157 item = PyList_GET_ITEM(seq, it->it_index);
3158 ++it->it_index;
3159 Py_INCREF(item);
3160 return item;
3161 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003164 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003166}
3167
3168static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303169listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 Py_ssize_t len;
3172 if (it->it_seq) {
3173 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3174 if (len >= 0)
3175 return PyLong_FromSsize_t(len);
3176 }
3177 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003178}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003179
3180static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303181listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003182{
3183 return listiter_reduce_general(it, 1);
3184}
3185
3186static PyObject *
3187listiter_setstate(listiterobject *it, PyObject *state)
3188{
Victor Stinner7660b882013-06-24 23:59:24 +02003189 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003190 if (index == -1 && PyErr_Occurred())
3191 return NULL;
3192 if (it->it_seq != NULL) {
3193 if (index < 0)
3194 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003195 else if (index > PyList_GET_SIZE(it->it_seq))
3196 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003197 it->it_index = index;
3198 }
3199 Py_RETURN_NONE;
3200}
3201
Raymond Hettinger1021c442003-11-07 15:38:09 +00003202/*********************** List Reverse Iterator **************************/
3203
3204typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 PyObject_HEAD
3206 Py_ssize_t it_index;
3207 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00003208} listreviterobject;
3209
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003210static void listreviter_dealloc(listreviterobject *);
3211static int listreviter_traverse(listreviterobject *, visitproc, void *);
3212static PyObject *listreviter_next(listreviterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303213static PyObject *listreviter_len(listreviterobject *, PyObject *);
3214static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003215static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003216
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003217static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003219 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3220 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003222};
3223
Raymond Hettinger1021c442003-11-07 15:38:09 +00003224PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3226 "list_reverseiterator", /* tp_name */
3227 sizeof(listreviterobject), /* tp_basicsize */
3228 0, /* tp_itemsize */
3229 /* methods */
3230 (destructor)listreviter_dealloc, /* tp_dealloc */
3231 0, /* tp_print */
3232 0, /* tp_getattr */
3233 0, /* tp_setattr */
3234 0, /* tp_reserved */
3235 0, /* tp_repr */
3236 0, /* tp_as_number */
3237 0, /* tp_as_sequence */
3238 0, /* tp_as_mapping */
3239 0, /* tp_hash */
3240 0, /* tp_call */
3241 0, /* tp_str */
3242 PyObject_GenericGetAttr, /* tp_getattro */
3243 0, /* tp_setattro */
3244 0, /* tp_as_buffer */
3245 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3246 0, /* tp_doc */
3247 (traverseproc)listreviter_traverse, /* tp_traverse */
3248 0, /* tp_clear */
3249 0, /* tp_richcompare */
3250 0, /* tp_weaklistoffset */
3251 PyObject_SelfIter, /* tp_iter */
3252 (iternextfunc)listreviter_next, /* tp_iternext */
3253 listreviter_methods, /* tp_methods */
3254 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00003255};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003256
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003257/*[clinic input]
3258list.__reversed__
3259
3260Return a reverse iterator over the list.
3261[clinic start generated code]*/
3262
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003263static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003264list___reversed___impl(PyListObject *self)
3265/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3270 if (it == NULL)
3271 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003272 assert(PyList_Check(self));
3273 it->it_index = PyList_GET_SIZE(self) - 1;
3274 Py_INCREF(self);
3275 it->it_seq = self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 PyObject_GC_Track(it);
3277 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003278}
3279
3280static void
3281listreviter_dealloc(listreviterobject *it)
3282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 PyObject_GC_UnTrack(it);
3284 Py_XDECREF(it->it_seq);
3285 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003286}
3287
3288static int
3289listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 Py_VISIT(it->it_seq);
3292 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003293}
3294
3295static PyObject *
3296listreviter_next(listreviterobject *it)
3297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003299 Py_ssize_t index;
3300 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003301
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003302 assert(it != NULL);
3303 seq = it->it_seq;
3304 if (seq == NULL) {
3305 return NULL;
3306 }
3307 assert(PyList_Check(seq));
3308
3309 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 if (index>=0 && index < PyList_GET_SIZE(seq)) {
3311 item = PyList_GET_ITEM(seq, index);
3312 it->it_index--;
3313 Py_INCREF(item);
3314 return item;
3315 }
3316 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003317 it->it_seq = NULL;
3318 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003320}
3321
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003322static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303323listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 Py_ssize_t len = it->it_index + 1;
3326 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3327 len = 0;
3328 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003329}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003330
3331static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303332listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003333{
3334 return listiter_reduce_general(it, 0);
3335}
3336
3337static PyObject *
3338listreviter_setstate(listreviterobject *it, PyObject *state)
3339{
3340 Py_ssize_t index = PyLong_AsSsize_t(state);
3341 if (index == -1 && PyErr_Occurred())
3342 return NULL;
3343 if (it->it_seq != NULL) {
3344 if (index < -1)
3345 index = -1;
3346 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3347 index = PyList_GET_SIZE(it->it_seq) - 1;
3348 it->it_index = index;
3349 }
3350 Py_RETURN_NONE;
3351}
3352
3353/* common pickling support */
3354
3355static PyObject *
3356listiter_reduce_general(void *_it, int forward)
3357{
3358 PyObject *list;
3359
3360 /* the objects are not the same, index is of different types! */
3361 if (forward) {
3362 listiterobject *it = (listiterobject *)_it;
3363 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02003364 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003365 it->it_seq, it->it_index);
3366 } else {
3367 listreviterobject *it = (listreviterobject *)_it;
3368 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +02003369 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("reversed"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003370 it->it_seq, it->it_index);
3371 }
3372 /* empty iterator, create an empty list */
3373 list = PyList_New(0);
3374 if (list == NULL)
3375 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02003376 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003377}