blob: 7058fe4396eec4ce9a7a9530cd591ec5aac656b1 [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"
Victor Stinnera15e2602020-04-08 02:01:56 +02004#include "pycore_abstract.h" // _PyIndex_Check()
Victor Stinnerbcda8f12018-11-21 22:27:47 +01005#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pystate.h"
Sergey Fedoseev234531b2019-02-25 21:59:12 +05007#include "pycore_tupleobject.h"
Victor Stinnere281f7d2018-11-01 02:30:36 +01008#include "pycore_accu.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00009
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000010#ifdef STDC_HEADERS
11#include <stddef.h>
12#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000013#include <sys/types.h> /* For size_t */
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000014#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015
Serhiy Storchakafdd42c42017-03-11 09:19:20 +020016/*[clinic input]
17class list "PyListObject *" "&PyList_Type"
18[clinic start generated code]*/
19/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f9b222678f9f71e0]*/
20
21#include "clinic/listobject.c.h"
22
Tim Peters8d9eb102004-07-31 02:24:20 +000023/* Ensure ob_item has room for at least newsize elements, and set
24 * ob_size to newsize. If newsize > ob_size on entry, the content
25 * of the new slots at exit is undefined heap trash; it's the caller's
Ezio Melotti13925002011-03-16 11:05:33 +020026 * responsibility to overwrite them with sane values.
Tim Peters8d9eb102004-07-31 02:24:20 +000027 * The number of allocated elements may grow, shrink, or stay the same.
28 * Failure is impossible if newsize <= self.allocated on entry, although
29 * that partly relies on an assumption that the system realloc() never
30 * fails when passed a number of bytes <= the number of bytes last
31 * allocated (the C standard doesn't guarantee this, but it's hard to
32 * imagine a realloc implementation where it wouldn't be true).
33 * Note that self->ob_item may change, and even if newsize is less
34 * than ob_size on entry.
35 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000036static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000037list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000038{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 PyObject **items;
Xiang Zhang4cee0492017-02-22 12:32:30 +080040 size_t new_allocated, num_allocated_bytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 /* Bypass realloc() when a previous overallocation is large enough
44 to accommodate the newsize. If the newsize falls lower than half
45 the allocated size, then proceed with the realloc() to shrink the list.
46 */
47 if (allocated >= newsize && newsize >= (allocated >> 1)) {
48 assert(self->ob_item != NULL || newsize == 0);
Victor Stinner60ac6ed2020-02-07 23:18:08 +010049 Py_SET_SIZE(self, newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 return 0;
51 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +000052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 /* This over-allocates proportional to the list size, making room
54 * for additional growth. The over-allocation is mild, but is
55 * enough to give linear-time amortized behavior over a long
56 * sequence of appends() in the presence of a poorly-performing
57 * system realloc().
Serhiy Storchaka2fe815e2020-03-17 23:46:00 +020058 * Add padding to make the allocated size multiple of 4.
59 * The growth pattern is: 0, 4, 8, 16, 24, 32, 40, 52, 64, 76, ...
Xiang Zhang4cee0492017-02-22 12:32:30 +080060 * Note: new_allocated won't overflow because the largest possible value
61 * is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 */
Serhiy Storchaka2fe815e2020-03-17 23:46:00 +020063 new_allocated = ((size_t)newsize + (newsize >> 3) + 6) & ~(size_t)3;
64 /* Do not overallocate if the new size is closer to overalocated size
65 * than to the old size.
66 */
67 if (newsize - Py_SIZE(self) > (Py_ssize_t)(new_allocated - newsize))
68 new_allocated = ((size_t)newsize + 3) & ~(size_t)3;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 if (newsize == 0)
71 new_allocated = 0;
Xiang Zhang4cee0492017-02-22 12:32:30 +080072 num_allocated_bytes = new_allocated * sizeof(PyObject *);
73 items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 if (items == NULL) {
75 PyErr_NoMemory();
76 return -1;
77 }
78 self->ob_item = items;
Victor Stinner60ac6ed2020-02-07 23:18:08 +010079 Py_SET_SIZE(self, newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 self->allocated = new_allocated;
81 return 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000082}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000083
Pablo Galindo372d7052018-10-28 20:16:26 +000084static int
85list_preallocate_exact(PyListObject *self, Py_ssize_t size)
86{
87 assert(self->ob_item == NULL);
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050088 assert(size > 0);
Pablo Galindo372d7052018-10-28 20:16:26 +000089
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050090 PyObject **items = PyMem_New(PyObject*, size);
Pablo Galindo372d7052018-10-28 20:16:26 +000091 if (items == NULL) {
92 PyErr_NoMemory();
93 return -1;
94 }
95 self->ob_item = items;
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050096 self->allocated = size;
Pablo Galindo372d7052018-10-28 20:16:26 +000097 return 0;
98}
99
Raymond Hettinger0468e412004-05-05 05:37:53 +0000100/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000101#ifndef PyList_MAXFREELIST
102#define PyList_MAXFREELIST 80
103#endif
104static PyListObject *free_list[PyList_MAXFREELIST];
105static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000106
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100107int
108PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100111 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 while (numfree) {
113 op = free_list[--numfree];
114 assert(PyList_CheckExact(op));
115 PyObject_GC_Del(op);
116 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100117 return ret;
118}
119
120void
Victor Stinnerbed48172019-08-27 00:12:32 +0200121_PyList_Fini(void)
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100122{
123 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000124}
125
David Malcolm49526f42012-06-22 14:55:41 -0400126/* Print summary info about the state of the optimized allocator */
127void
128_PyList_DebugMallocStats(FILE *out)
129{
130 _PyDebugAllocatorStats(out,
131 "free PyListObject",
132 numfree, sizeof(PyListObject));
133}
134
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000135PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000136PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 PyListObject *op;
Tim Peters3986d4e2004-07-29 02:28:42 +0000139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 if (size < 0) {
141 PyErr_BadInternalCall();
142 return NULL;
143 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 if (numfree) {
145 numfree--;
146 op = free_list[numfree];
147 _Py_NewReference((PyObject *)op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 } else {
149 op = PyObject_GC_New(PyListObject, &PyList_Type);
150 if (op == NULL)
151 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 }
153 if (size <= 0)
154 op->ob_item = NULL;
155 else {
Mark Dickinson5d132382016-08-21 08:55:15 +0100156 op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 if (op->ob_item == NULL) {
158 Py_DECREF(op);
159 return PyErr_NoMemory();
160 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100162 Py_SET_SIZE(op, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 op->allocated = size;
164 _PyObject_GC_TRACK(op);
165 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166}
167
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500168static PyObject *
169list_new_prealloc(Py_ssize_t size)
170{
171 PyListObject *op = (PyListObject *) PyList_New(0);
172 if (size == 0 || op == NULL) {
173 return (PyObject *) op;
174 }
175 assert(op->ob_item == NULL);
176 op->ob_item = PyMem_New(PyObject *, size);
177 if (op->ob_item == NULL) {
178 Py_DECREF(op);
179 return PyErr_NoMemory();
180 }
181 op->allocated = size;
182 return (PyObject *) op;
183}
184
Martin v. Löwis18e16552006-02-15 17:27:45 +0000185Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000186PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 if (!PyList_Check(op)) {
189 PyErr_BadInternalCall();
190 return -1;
191 }
192 else
193 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194}
195
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700196static inline int
197valid_index(Py_ssize_t i, Py_ssize_t limit)
198{
199 /* The cast to size_t lets us use just a single comparison
200 to check whether i is in the range: 0 <= i < limit.
201
202 See: Section 14.2 "Bounds Checking" in the Agner Fog
203 optimization manual found at:
204 https://www.agner.org/optimize/optimizing_cpp.pdf
205 */
206 return (size_t) i < (size_t) limit;
207}
208
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000209static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000210
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000212PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if (!PyList_Check(op)) {
215 PyErr_BadInternalCall();
216 return NULL;
217 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700218 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 if (indexerr == NULL) {
220 indexerr = PyUnicode_FromString(
221 "list index out of range");
222 if (indexerr == NULL)
223 return NULL;
224 }
225 PyErr_SetObject(PyExc_IndexError, indexerr);
226 return NULL;
227 }
228 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229}
230
231int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200232PyList_SetItem(PyObject *op, Py_ssize_t i,
233 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200235 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 if (!PyList_Check(op)) {
237 Py_XDECREF(newitem);
238 PyErr_BadInternalCall();
239 return -1;
240 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700241 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 Py_XDECREF(newitem);
243 PyErr_SetString(PyExc_IndexError,
244 "list assignment index out of range");
245 return -1;
246 }
247 p = ((PyListObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300248 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250}
251
252static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000253ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 Py_ssize_t i, n = Py_SIZE(self);
256 PyObject **items;
257 if (v == NULL) {
258 PyErr_BadInternalCall();
259 return -1;
260 }
261 if (n == PY_SSIZE_T_MAX) {
262 PyErr_SetString(PyExc_OverflowError,
263 "cannot add more objects to list");
264 return -1;
265 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000266
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800267 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (where < 0) {
271 where += n;
272 if (where < 0)
273 where = 0;
274 }
275 if (where > n)
276 where = n;
277 items = self->ob_item;
278 for (i = n; --i >= where; )
279 items[i+1] = items[i];
280 Py_INCREF(v);
281 items[where] = v;
282 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283}
284
285int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000286PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 if (!PyList_Check(op)) {
289 PyErr_BadInternalCall();
290 return -1;
291 }
292 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000293}
294
Raymond Hettinger40a03822004-04-12 13:05:09 +0000295static int
296app1(PyListObject *self, PyObject *v)
297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 assert (v != NULL);
301 if (n == PY_SSIZE_T_MAX) {
302 PyErr_SetString(PyExc_OverflowError,
303 "cannot add more objects to list");
304 return -1;
305 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000306
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800307 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 Py_INCREF(v);
311 PyList_SET_ITEM(self, n, v);
312 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000313}
314
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315int
Fred Drakea2f55112000-07-09 15:16:51 +0000316PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 if (PyList_Check(op) && (newitem != NULL))
319 return app1((PyListObject *)op, newitem);
320 PyErr_BadInternalCall();
321 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322}
323
324/* Methods */
325
326static void
Fred Drakea2f55112000-07-09 15:16:51 +0000327list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 Py_ssize_t i;
330 PyObject_GC_UnTrack(op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200331 Py_TRASHCAN_BEGIN(op, list_dealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 if (op->ob_item != NULL) {
333 /* Do it backwards, for Christian Tismer.
334 There's a simple test case where somehow this reduces
335 thrashing when a *very* large list is created and
336 immediately deleted. */
337 i = Py_SIZE(op);
338 while (--i >= 0) {
339 Py_XDECREF(op->ob_item[i]);
340 }
341 PyMem_FREE(op->ob_item);
342 }
343 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
344 free_list[numfree++] = op;
345 else
346 Py_TYPE(op)->tp_free((PyObject *)op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200347 Py_TRASHCAN_END
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348}
349
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000350static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000351list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 Py_ssize_t i;
Victor Stinner5c733472013-11-18 21:11:57 +0100354 PyObject *s;
Victor Stinner5c733472013-11-18 21:11:57 +0100355 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200356
357 if (Py_SIZE(v) == 0) {
358 return PyUnicode_FromString("[]");
359 }
360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 i = Py_ReprEnter((PyObject*)v);
362 if (i != 0) {
363 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
364 }
Tim Petersa7259592001-06-16 05:11:17 +0000365
Victor Stinner5c733472013-11-18 21:11:57 +0100366 _PyUnicodeWriter_Init(&writer);
367 writer.overallocate = 1;
Victor Stinnerb8fb1972013-11-18 22:15:44 +0100368 /* "[" + "1" + ", 2" * (len - 1) + "]" */
369 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +0000370
Victor Stinner5c733472013-11-18 21:11:57 +0100371 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200372 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 /* Do repr() on each element. Note that this may mutate the list,
375 so must refetch the list size on each iteration. */
376 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100377 if (i > 0) {
Victor Stinner4a587072013-11-19 12:54:53 +0100378 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
Victor Stinner5c733472013-11-18 21:11:57 +0100379 goto error;
380 }
381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner5c733472013-11-18 21:11:57 +0100383 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200384 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100385
386 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
387 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200388 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100389 }
390 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 }
Victor Stinner5c733472013-11-18 21:11:57 +0100392
Victor Stinner4d3f1092013-11-19 12:09:00 +0100393 writer.overallocate = 0;
Victor Stinner5c733472013-11-18 21:11:57 +0100394 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200395 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100398 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200399
400error:
Victor Stinner5c733472013-11-18 21:11:57 +0100401 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200402 Py_ReprLeave((PyObject *)v);
403 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000404}
405
Martin v. Löwis18e16552006-02-15 17:27:45 +0000406static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000407list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410}
411
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000412static int
Fred Drakea2f55112000-07-09 15:16:51 +0000413list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000414{
Dong-hee Na4dbf2d82020-01-28 00:02:23 +0900415 PyObject *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 Py_ssize_t i;
417 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000418
Dong-hee Na4dbf2d82020-01-28 00:02:23 +0900419 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) {
420 item = PyList_GET_ITEM(a, i);
421 Py_INCREF(item);
Dong-hee Na9e1ed512020-01-28 02:04:25 +0900422 cmp = PyObject_RichCompareBool(item, el, Py_EQ);
Dong-hee Na4dbf2d82020-01-28 00:02:23 +0900423 Py_DECREF(item);
424 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000426}
427
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000428static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000429list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700431 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (indexerr == NULL) {
433 indexerr = PyUnicode_FromString(
434 "list index out of range");
435 if (indexerr == NULL)
436 return NULL;
437 }
438 PyErr_SetObject(PyExc_IndexError, indexerr);
439 return NULL;
440 }
441 Py_INCREF(a->ob_item[i]);
442 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443}
444
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000446list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 PyListObject *np;
449 PyObject **src, **dest;
450 Py_ssize_t i, len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500452 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (np == NULL)
454 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 src = a->ob_item + ilow;
457 dest = np->ob_item;
458 for (i = 0; i < len; i++) {
459 PyObject *v = src[i];
460 Py_INCREF(v);
461 dest[i] = v;
462 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100463 Py_SET_SIZE(np, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465}
466
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000467PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000468PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 if (!PyList_Check(a)) {
471 PyErr_BadInternalCall();
472 return NULL;
473 }
Sergey Fedoseevef1b88b2019-02-21 11:51:52 +0500474 if (ilow < 0) {
475 ilow = 0;
476 }
477 else if (ilow > Py_SIZE(a)) {
478 ilow = Py_SIZE(a);
479 }
480 if (ihigh < ilow) {
481 ihigh = ilow;
482 }
483 else if (ihigh > Py_SIZE(a)) {
484 ihigh = Py_SIZE(a);
485 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000487}
488
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000489static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000490list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 Py_ssize_t size;
493 Py_ssize_t i;
494 PyObject **src, **dest;
495 PyListObject *np;
496 if (!PyList_Check(bb)) {
497 PyErr_Format(PyExc_TypeError,
498 "can only concatenate list (not \"%.200s\") to list",
Victor Stinner58ac7002020-02-07 03:04:21 +0100499 Py_TYPE(bb)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 return NULL;
501 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000502#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000503 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000505 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500506 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 if (np == NULL) {
508 return NULL;
509 }
510 src = a->ob_item;
511 dest = np->ob_item;
512 for (i = 0; i < Py_SIZE(a); i++) {
513 PyObject *v = src[i];
514 Py_INCREF(v);
515 dest[i] = v;
516 }
517 src = b->ob_item;
518 dest = np->ob_item + Py_SIZE(a);
519 for (i = 0; i < Py_SIZE(b); i++) {
520 PyObject *v = src[i];
521 Py_INCREF(v);
522 dest[i] = v;
523 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100524 Py_SET_SIZE(np, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000526#undef b
527}
528
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000529static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000530list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 Py_ssize_t i, j;
533 Py_ssize_t size;
534 PyListObject *np;
535 PyObject **p, **items;
536 PyObject *elem;
537 if (n < 0)
538 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100539 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100541 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (size == 0)
543 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500544 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (np == NULL)
546 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500549 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 elem = a->ob_item[0];
551 for (i = 0; i < n; i++) {
552 items[i] = elem;
553 Py_INCREF(elem);
554 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500556 else {
557 p = np->ob_item;
558 items = a->ob_item;
559 for (i = 0; i < n; i++) {
560 for (j = 0; j < Py_SIZE(a); j++) {
561 *p = items[j];
562 Py_INCREF(*p);
563 p++;
564 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 }
566 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100567 Py_SET_SIZE(np, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000569}
570
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200572_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 Py_ssize_t i;
575 PyObject **item = a->ob_item;
576 if (item != NULL) {
577 /* Because XDECREF can recursively invoke operations on
578 this list, we make it empty first. */
579 i = Py_SIZE(a);
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100580 Py_SET_SIZE(a, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 a->ob_item = NULL;
582 a->allocated = 0;
583 while (--i >= 0) {
584 Py_XDECREF(item[i]);
585 }
586 PyMem_FREE(item);
587 }
588 /* Never fails; the return value can be ignored.
589 Note that there is no guarantee that the list is actually empty
590 at this point, because XDECREF may have populated it again! */
591 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000592}
593
Tim Peters8fc4a912004-07-31 21:53:19 +0000594/* a[ilow:ihigh] = v if v != NULL.
595 * del a[ilow:ihigh] if v == NULL.
596 *
597 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
598 * guaranteed the call cannot fail.
599 */
Armin Rigo93677f02004-07-29 12:40:23 +0000600static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000601list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 /* Because [X]DECREF can recursively invoke list operations on
604 this list, we must postpone all [X]DECREF activity until
605 after the list is back in its canonical shape. Therefore
606 we must allocate an additional array, 'recycle', into which
607 we temporarily copy the items that are deleted from the
608 list. :-( */
609 PyObject *recycle_on_stack[8];
610 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
611 PyObject **item;
612 PyObject **vitem = NULL;
613 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
614 Py_ssize_t n; /* # of elements in replacement list */
615 Py_ssize_t norig; /* # of elements in list getting replaced */
616 Py_ssize_t d; /* Change in size */
617 Py_ssize_t k;
618 size_t s;
619 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 if (v == NULL)
622 n = 0;
623 else {
624 if (a == b) {
625 /* Special case "a[i:j] = a" -- copy b first */
626 v = list_slice(b, 0, Py_SIZE(b));
627 if (v == NULL)
628 return result;
629 result = list_ass_slice(a, ilow, ihigh, v);
630 Py_DECREF(v);
631 return result;
632 }
633 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
634 if(v_as_SF == NULL)
635 goto Error;
636 n = PySequence_Fast_GET_SIZE(v_as_SF);
637 vitem = PySequence_Fast_ITEMS(v_as_SF);
638 }
639 if (ilow < 0)
640 ilow = 0;
641 else if (ilow > Py_SIZE(a))
642 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 if (ihigh < ilow)
645 ihigh = ilow;
646 else if (ihigh > Py_SIZE(a))
647 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 norig = ihigh - ilow;
650 assert(norig >= 0);
651 d = n - norig;
652 if (Py_SIZE(a) + d == 0) {
653 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200654 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 }
656 item = a->ob_item;
657 /* recycle the items that we are about to remove */
658 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700659 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
660 if (s) {
661 if (s > sizeof(recycle_on_stack)) {
662 recycle = (PyObject **)PyMem_MALLOC(s);
663 if (recycle == NULL) {
664 PyErr_NoMemory();
665 goto Error;
666 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700668 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200672 Py_ssize_t tail;
673 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
674 memmove(&item[ihigh+d], &item[ihigh], tail);
675 if (list_resize(a, Py_SIZE(a) + d) < 0) {
676 memmove(&item[ihigh], &item[ihigh+d], tail);
677 memcpy(&item[ilow], recycle, s);
678 goto Error;
679 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 item = a->ob_item;
681 }
682 else if (d > 0) { /* Insert d items */
683 k = Py_SIZE(a);
684 if (list_resize(a, k+d) < 0)
685 goto Error;
686 item = a->ob_item;
687 memmove(&item[ihigh+d], &item[ihigh],
688 (k - ihigh)*sizeof(PyObject *));
689 }
690 for (k = 0; k < n; k++, ilow++) {
691 PyObject *w = vitem[k];
692 Py_XINCREF(w);
693 item[ilow] = w;
694 }
695 for (k = norig - 1; k >= 0; --k)
696 Py_XDECREF(recycle[k]);
697 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000698 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 if (recycle != recycle_on_stack)
700 PyMem_FREE(recycle);
701 Py_XDECREF(v_as_SF);
702 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703#undef b
704}
705
Guido van Rossum234f9421993-06-17 12:35:49 +0000706int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000707PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 if (!PyList_Check(a)) {
710 PyErr_BadInternalCall();
711 return -1;
712 }
713 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000714}
715
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000716static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000717list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 PyObject **items;
720 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000721
722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 size = PyList_GET_SIZE(self);
724 if (size == 0 || n == 1) {
725 Py_INCREF(self);
726 return (PyObject *)self;
727 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 if (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200730 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 Py_INCREF(self);
732 return (PyObject *)self;
733 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 if (size > PY_SSIZE_T_MAX / n) {
736 return PyErr_NoMemory();
737 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000738
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800739 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 p = size;
743 items = self->ob_item;
744 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
745 for (j = 0; j < size; j++) {
746 PyObject *o = items[j];
747 Py_INCREF(o);
748 items[p++] = o;
749 }
750 }
751 Py_INCREF(self);
752 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000753}
754
Guido van Rossum4a450d01991-04-03 19:05:18 +0000755static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000756list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000757{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700758 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 PyErr_SetString(PyExc_IndexError,
760 "list assignment index out of range");
761 return -1;
762 }
763 if (v == NULL)
764 return list_ass_slice(a, i, i+1, v);
765 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300766 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000768}
769
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200770/*[clinic input]
771list.insert
772
773 index: Py_ssize_t
774 object: object
775 /
776
777Insert object before index.
778[clinic start generated code]*/
779
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000780static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200781list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
782/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000783{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200784 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 Py_RETURN_NONE;
786 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000787}
788
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200789/*[clinic input]
790list.clear
791
792Remove all items from list.
793[clinic start generated code]*/
794
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000795static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200796list_clear_impl(PyListObject *self)
797/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000798{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200799 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000800 Py_RETURN_NONE;
801}
802
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200803/*[clinic input]
804list.copy
805
806Return a shallow copy of the list.
807[clinic start generated code]*/
808
Eli Benderskycbbaa962011-02-25 05:47:53 +0000809static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200810list_copy_impl(PyListObject *self)
811/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000812{
813 return list_slice(self, 0, Py_SIZE(self));
814}
815
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200816/*[clinic input]
817list.append
818
819 object: object
820 /
821
822Append object to the end of the list.
823[clinic start generated code]*/
824
Eli Benderskycbbaa962011-02-25 05:47:53 +0000825static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200826list_append(PyListObject *self, PyObject *object)
827/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000828{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200829 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 Py_RETURN_NONE;
831 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000832}
833
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200834/*[clinic input]
835list.extend
836
837 iterable: object
838 /
839
840Extend list by appending elements from the iterable.
841[clinic start generated code]*/
842
Barry Warsawdedf6d61998-10-09 16:37:25 +0000843static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200844list_extend(PyListObject *self, PyObject *iterable)
845/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 PyObject *it; /* iter(v) */
848 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200849 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 Py_ssize_t mn; /* m + n */
851 Py_ssize_t i;
852 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 /* Special cases:
855 1) lists and tuples which can use PySequence_Fast ops
856 2) extending self to self requires making a copy first
857 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200858 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
859 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200861 iterable = PySequence_Fast(iterable, "argument must be iterable");
862 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200864 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200866 /* short circuit when iterable is empty */
867 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 Py_RETURN_NONE;
869 }
870 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000871 /* It should not be possible to allocate a list large enough to cause
872 an overflow on any relevant platform */
873 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800874 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200875 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 return NULL;
877 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200878 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 * situation a.extend(a), but the following code works
880 * in that case too. Just make sure to resize self
881 * before calling PySequence_Fast_ITEMS.
882 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200883 /* populate the end of self with iterable's items */
884 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 dest = self->ob_item + m;
886 for (i = 0; i < n; i++) {
887 PyObject *o = src[i];
888 Py_INCREF(o);
889 dest[i] = o;
890 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200891 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 Py_RETURN_NONE;
893 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000894
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200895 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 if (it == NULL)
897 return NULL;
Victor Stinner58ac7002020-02-07 03:04:21 +0100898 iternext = *Py_TYPE(it)->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200901 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800902 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 Py_DECREF(it);
904 return NULL;
905 }
906 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000907 if (m > PY_SSIZE_T_MAX - n) {
908 /* m + n overflowed; on the chance that n lied, and there really
909 * is enough room, ignore it. If n was telling the truth, we'll
910 * eventually run out of memory during the loop.
911 */
912 }
913 else {
914 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800916 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 goto error;
918 /* Make the list sane again. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100919 Py_SET_SIZE(self, m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 /* Run iterator to exhaustion. */
923 for (;;) {
924 PyObject *item = iternext(it);
925 if (item == NULL) {
926 if (PyErr_Occurred()) {
927 if (PyErr_ExceptionMatches(PyExc_StopIteration))
928 PyErr_Clear();
929 else
930 goto error;
931 }
932 break;
933 }
934 if (Py_SIZE(self) < self->allocated) {
935 /* steals ref */
936 PyList_SET_ITEM(self, Py_SIZE(self), item);
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100937 Py_SET_SIZE(self, Py_SIZE(self) + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 }
939 else {
940 int status = app1(self, item);
941 Py_DECREF(item); /* append creates a new ref */
942 if (status < 0)
943 goto error;
944 }
945 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200948 if (Py_SIZE(self) < self->allocated) {
949 if (list_resize(self, Py_SIZE(self)) < 0)
950 goto error;
951 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 Py_DECREF(it);
954 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000955
956 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 Py_DECREF(it);
958 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000959}
960
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000961PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200962_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000963{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200964 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000965}
966
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000967static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000968list_inplace_concat(PyListObject *self, PyObject *other)
969{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000971
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200972 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 if (result == NULL)
974 return result;
975 Py_DECREF(result);
976 Py_INCREF(self);
977 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000978}
979
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200980/*[clinic input]
981list.pop
982
983 index: Py_ssize_t = -1
984 /
985
986Remove and return item at index (default last).
987
988Raises IndexError if list is empty or index is out of range.
989[clinic start generated code]*/
990
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000991static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200992list_pop_impl(PyListObject *self, Py_ssize_t index)
993/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 PyObject *v;
996 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 if (Py_SIZE(self) == 0) {
999 /* Special-case most common failure cause */
1000 PyErr_SetString(PyExc_IndexError, "pop from empty list");
1001 return NULL;
1002 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001003 if (index < 0)
1004 index += Py_SIZE(self);
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -07001005 if (!valid_index(index, Py_SIZE(self))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1007 return NULL;
1008 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001009 v = self->ob_item[index];
1010 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001012 if (status >= 0)
1013 return v; /* and v now owns the reference the list had */
1014 else
1015 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 }
1017 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001018 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001019 if (status < 0) {
1020 Py_DECREF(v);
1021 return NULL;
1022 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001024}
1025
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001026/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1027static void
1028reverse_slice(PyObject **lo, PyObject **hi)
1029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 --hi;
1033 while (lo < hi) {
1034 PyObject *t = *lo;
1035 *lo = *hi;
1036 *hi = t;
1037 ++lo;
1038 --hi;
1039 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001040}
1041
Tim Petersa64dc242002-08-01 02:13:36 +00001042/* Lots of code for an adaptive, stable, natural mergesort. There are many
1043 * pieces to this algorithm; read listsort.txt for overviews and details.
1044 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001045
Daniel Stutzbach98338222010-12-02 21:55:33 +00001046/* A sortslice contains a pointer to an array of keys and a pointer to
1047 * an array of corresponding values. In other words, keys[i]
1048 * corresponds with values[i]. If values == NULL, then the keys are
1049 * also the values.
1050 *
1051 * Several convenience routines are provided here, so that keys and
1052 * values are always moved in sync.
1053 */
1054
1055typedef struct {
1056 PyObject **keys;
1057 PyObject **values;
1058} sortslice;
1059
1060Py_LOCAL_INLINE(void)
1061sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1062{
1063 s1->keys[i] = s2->keys[j];
1064 if (s1->values != NULL)
1065 s1->values[i] = s2->values[j];
1066}
1067
1068Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001069sortslice_copy_incr(sortslice *dst, sortslice *src)
1070{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001071 *dst->keys++ = *src->keys++;
1072 if (dst->values != NULL)
1073 *dst->values++ = *src->values++;
1074}
1075
1076Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001077sortslice_copy_decr(sortslice *dst, sortslice *src)
1078{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001079 *dst->keys-- = *src->keys--;
1080 if (dst->values != NULL)
1081 *dst->values-- = *src->values--;
1082}
1083
1084
1085Py_LOCAL_INLINE(void)
1086sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001087 Py_ssize_t n)
1088{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001089 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1090 if (s1->values != NULL)
1091 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1092}
1093
1094Py_LOCAL_INLINE(void)
1095sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001096 Py_ssize_t n)
1097{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001098 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1099 if (s1->values != NULL)
1100 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1101}
1102
1103Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001104sortslice_advance(sortslice *slice, Py_ssize_t n)
1105{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001106 slice->keys += n;
1107 if (slice->values != NULL)
1108 slice->values += n;
1109}
1110
embg1e34da42018-01-28 20:03:23 -07001111/* Comparison function: ms->key_compare, which is set at run-time in
1112 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001113 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1114 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001115
embg1e34da42018-01-28 20:03:23 -07001116#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001117
1118/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001119 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1120 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1121*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001122#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001124
embg1e34da42018-01-28 20:03:23 -07001125/* The maximum number of entries in a MergeState's pending-runs stack.
1126 * This is enough to sort arrays of size up to about
1127 * 32 * phi ** MAX_MERGE_PENDING
1128 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1129 * with 2**64 elements.
1130 */
1131#define MAX_MERGE_PENDING 85
1132
1133/* When we get into galloping mode, we stay there until both runs win less
1134 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1135 */
1136#define MIN_GALLOP 7
1137
1138/* Avoid malloc for small temp arrays. */
1139#define MERGESTATE_TEMP_SIZE 256
1140
1141/* One MergeState exists on the stack per invocation of mergesort. It's just
1142 * a convenient way to pass state around among the helper functions.
1143 */
1144struct s_slice {
1145 sortslice base;
1146 Py_ssize_t len;
1147};
1148
1149typedef struct s_MergeState MergeState;
1150struct s_MergeState {
1151 /* This controls when we get *into* galloping mode. It's initialized
1152 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1153 * random data, and lower for highly structured data.
1154 */
1155 Py_ssize_t min_gallop;
1156
1157 /* 'a' is temp storage to help with merges. It contains room for
1158 * alloced entries.
1159 */
1160 sortslice a; /* may point to temparray below */
1161 Py_ssize_t alloced;
1162
1163 /* A stack of n pending runs yet to be merged. Run #i starts at
1164 * address base[i] and extends for len[i] elements. It's always
1165 * true (so long as the indices are in bounds) that
1166 *
1167 * pending[i].base + pending[i].len == pending[i+1].base
1168 *
1169 * so we could cut the storage for this, but it's a minor amount,
1170 * and keeping all the info explicit simplifies the code.
1171 */
1172 int n;
1173 struct s_slice pending[MAX_MERGE_PENDING];
1174
1175 /* 'a' points to this when possible, rather than muck with malloc. */
1176 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1177
1178 /* This is the function we will use to compare two keys,
1179 * even when none of our special cases apply and we have to use
1180 * safe_object_compare. */
1181 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1182
1183 /* This function is used by unsafe_object_compare to optimize comparisons
1184 * when we know our list is type-homogeneous but we can't assume anything else.
Victor Stinner58ac7002020-02-07 03:04:21 +01001185 * In the pre-sort check it is set equal to Py_TYPE(key)->tp_richcompare */
embg1e34da42018-01-28 20:03:23 -07001186 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1187
1188 /* This function is used by unsafe_tuple_compare to compare the first elements
1189 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1190 * we can assume more, and use one of the special-case compares. */
1191 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1192};
1193
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001194/* binarysort is the best method for sorting small arrays: it does
1195 few compares, but can do data movement quadratic in the number of
1196 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001197 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001198 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001199 On entry, must have lo <= start <= hi, and that [lo, start) is already
1200 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001201 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001202 Even in case of error, the output slice will be some permutation of
1203 the input (nothing is lost or duplicated).
1204*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001205static int
embg1e34da42018-01-28 20:03:23 -07001206binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001207{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001208 Py_ssize_t k;
1209 PyObject **l, **p, **r;
1210 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001211
Daniel Stutzbach98338222010-12-02 21:55:33 +00001212 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001214 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 ++start;
1216 for (; start < hi; ++start) {
1217 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001218 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 r = start;
1220 pivot = *r;
1221 /* Invariants:
1222 * pivot >= all in [lo, l).
1223 * pivot < all in [r, start).
1224 * The second is vacuously true at the start.
1225 */
1226 assert(l < r);
1227 do {
1228 p = l + ((r - l) >> 1);
1229 IFLT(pivot, *p)
1230 r = p;
1231 else
1232 l = p+1;
1233 } while (l < r);
1234 assert(l == r);
1235 /* The invariants still hold, so pivot >= all in [lo, l) and
1236 pivot < all in [l, start), so pivot belongs at l. Note
1237 that if there are elements equal to pivot, l points to the
1238 first slot after them -- that's why this sort is stable.
1239 Slide over to make room.
1240 Caution: using memmove is much slower under MSVC 5;
1241 we're not usually moving many slots. */
1242 for (p = start; p > l; --p)
1243 *p = *(p-1);
1244 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001245 if (lo.values != NULL) {
1246 Py_ssize_t offset = lo.values - lo.keys;
1247 p = start + offset;
1248 pivot = *p;
1249 l += offset;
1250 for (p = start + offset; p > l; --p)
1251 *p = *(p-1);
1252 *l = pivot;
1253 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 }
1255 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001256
1257 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001259}
1260
Tim Petersa64dc242002-08-01 02:13:36 +00001261/*
1262Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1263is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001264
Tim Petersa64dc242002-08-01 02:13:36 +00001265 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001266
Tim Petersa64dc242002-08-01 02:13:36 +00001267or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001268
Tim Petersa64dc242002-08-01 02:13:36 +00001269 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001270
Tim Petersa64dc242002-08-01 02:13:36 +00001271Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1272For its intended use in a stable mergesort, the strictness of the defn of
1273"descending" is needed so that the caller can safely reverse a descending
1274sequence without violating stability (strict > ensures there are no equal
1275elements to get out of order).
1276
1277Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001278*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001279static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001280count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 Py_ssize_t k;
1283 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 assert(lo < hi);
1286 *descending = 0;
1287 ++lo;
1288 if (lo == hi)
1289 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 n = 2;
1292 IFLT(*lo, *(lo-1)) {
1293 *descending = 1;
1294 for (lo = lo+1; lo < hi; ++lo, ++n) {
1295 IFLT(*lo, *(lo-1))
1296 ;
1297 else
1298 break;
1299 }
1300 }
1301 else {
1302 for (lo = lo+1; lo < hi; ++lo, ++n) {
1303 IFLT(*lo, *(lo-1))
1304 break;
1305 }
1306 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001309fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001311}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001312
Tim Petersa64dc242002-08-01 02:13:36 +00001313/*
1314Locate the proper position of key in a sorted vector; if the vector contains
1315an element equal to key, return the position immediately to the left of
1316the leftmost equal element. [gallop_right() does the same except returns
1317the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001318
Tim Petersa64dc242002-08-01 02:13:36 +00001319"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001320
Tim Petersa64dc242002-08-01 02:13:36 +00001321"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1322hint is to the final result, the faster this runs.
1323
1324The return value is the int k in 0..n such that
1325
1326 a[k-1] < key <= a[k]
1327
1328pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1329key belongs at index k; or, IOW, the first k elements of a should precede
1330key, and the last n-k should follow key.
1331
1332Returns -1 on error. See listsort.txt for info on the method.
1333*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001334static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001335gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 Py_ssize_t ofs;
1338 Py_ssize_t lastofs;
1339 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 a += hint;
1344 lastofs = 0;
1345 ofs = 1;
1346 IFLT(*a, key) {
1347 /* a[hint] < key -- gallop right, until
1348 * a[hint + lastofs] < key <= a[hint + ofs]
1349 */
1350 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1351 while (ofs < maxofs) {
1352 IFLT(a[ofs], key) {
1353 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001354 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 }
1357 else /* key <= a[hint + ofs] */
1358 break;
1359 }
1360 if (ofs > maxofs)
1361 ofs = maxofs;
1362 /* Translate back to offsets relative to &a[0]. */
1363 lastofs += hint;
1364 ofs += hint;
1365 }
1366 else {
1367 /* key <= a[hint] -- gallop left, until
1368 * a[hint - ofs] < key <= a[hint - lastofs]
1369 */
1370 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1371 while (ofs < maxofs) {
1372 IFLT(*(a-ofs), key)
1373 break;
1374 /* key <= a[hint - ofs] */
1375 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001376 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 }
1379 if (ofs > maxofs)
1380 ofs = maxofs;
1381 /* Translate back to positive offsets relative to &a[0]. */
1382 k = lastofs;
1383 lastofs = hint - ofs;
1384 ofs = hint - k;
1385 }
1386 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1389 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1390 * right of lastofs but no farther right than ofs. Do a binary
1391 * search, with invariant a[lastofs-1] < key <= a[ofs].
1392 */
1393 ++lastofs;
1394 while (lastofs < ofs) {
1395 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 IFLT(a[m], key)
1398 lastofs = m+1; /* a[m] < key */
1399 else
1400 ofs = m; /* key <= a[m] */
1401 }
1402 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1403 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001404
1405fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001407}
1408
1409/*
1410Exactly like gallop_left(), except that if key already exists in a[0:n],
1411finds the position immediately to the right of the rightmost equal value.
1412
1413The return value is the int k in 0..n such that
1414
1415 a[k-1] <= key < a[k]
1416
1417or -1 if error.
1418
1419The code duplication is massive, but this is enough different given that
1420we're sticking to "<" comparisons that it's much harder to follow if
1421written as one routine with yet another "left or right?" flag.
1422*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001423static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001424gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 Py_ssize_t ofs;
1427 Py_ssize_t lastofs;
1428 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 a += hint;
1433 lastofs = 0;
1434 ofs = 1;
1435 IFLT(key, *a) {
1436 /* key < a[hint] -- gallop left, until
1437 * a[hint - ofs] <= key < a[hint - lastofs]
1438 */
1439 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1440 while (ofs < maxofs) {
1441 IFLT(key, *(a-ofs)) {
1442 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001443 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 }
1446 else /* a[hint - ofs] <= key */
1447 break;
1448 }
1449 if (ofs > maxofs)
1450 ofs = maxofs;
1451 /* Translate back to positive offsets relative to &a[0]. */
1452 k = lastofs;
1453 lastofs = hint - ofs;
1454 ofs = hint - k;
1455 }
1456 else {
1457 /* a[hint] <= key -- gallop right, until
1458 * a[hint + lastofs] <= key < a[hint + ofs]
1459 */
1460 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1461 while (ofs < maxofs) {
1462 IFLT(key, a[ofs])
1463 break;
1464 /* a[hint + ofs] <= key */
1465 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001466 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 }
1469 if (ofs > maxofs)
1470 ofs = maxofs;
1471 /* Translate back to offsets relative to &a[0]. */
1472 lastofs += hint;
1473 ofs += hint;
1474 }
1475 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1478 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1479 * right of lastofs but no farther right than ofs. Do a binary
1480 * search, with invariant a[lastofs-1] <= key < a[ofs].
1481 */
1482 ++lastofs;
1483 while (lastofs < ofs) {
1484 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 IFLT(key, a[m])
1487 ofs = m; /* key < a[m] */
1488 else
1489 lastofs = m+1; /* a[m] <= key */
1490 }
1491 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1492 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001493
1494fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001496}
1497
Tim Petersa64dc242002-08-01 02:13:36 +00001498/* Conceptually a MergeState's constructor. */
1499static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001500merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001503 if (has_keyfunc) {
1504 /* The temporary space for merging will need at most half the list
1505 * size rounded up. Use the minimum possible space so we can use the
1506 * rest of temparray for other things. In particular, if there is
1507 * enough extra space, listsort() will use it to store the keys.
1508 */
1509 ms->alloced = (list_size + 1) / 2;
1510
1511 /* ms->alloced describes how many keys will be stored at
1512 ms->temparray, but we also need to store the values. Hence,
1513 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1514 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1515 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1516 ms->a.values = &ms->temparray[ms->alloced];
1517 }
1518 else {
1519 ms->alloced = MERGESTATE_TEMP_SIZE;
1520 ms->a.values = NULL;
1521 }
1522 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 ms->n = 0;
1524 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001525}
1526
1527/* Free all the temp memory owned by the MergeState. This must be called
1528 * when you're done with a MergeState, and may be called before then if
1529 * you want to free the temp memory early.
1530 */
1531static void
1532merge_freemem(MergeState *ms)
1533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001535 if (ms->a.keys != ms->temparray)
1536 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001537}
1538
1539/* Ensure enough temp memory for 'need' array slots is available.
1540 * Returns 0 on success and -1 if the memory can't be gotten.
1541 */
1542static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001543merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001544{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001545 int multiplier;
1546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 assert(ms != NULL);
1548 if (need <= ms->alloced)
1549 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001550
1551 multiplier = ms->a.values != NULL ? 2 : 1;
1552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 /* Don't realloc! That can cost cycles to copy the old data, but
1554 * we don't care what's in the block.
1555 */
1556 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001557 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 PyErr_NoMemory();
1559 return -1;
1560 }
embg1e34da42018-01-28 20:03:23 -07001561 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001562 * sizeof(PyObject *));
1563 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001565 if (ms->a.values != NULL)
1566 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 return 0;
1568 }
1569 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001571}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1573 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001574
Daniel Stutzbach98338222010-12-02 21:55:33 +00001575/* Merge the na elements starting at ssa with the nb elements starting at
1576 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1577 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1578 * should have na <= nb. See listsort.txt for more info. Return 0 if
1579 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001580 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001581static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001582merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1583 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001586 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 int result = -1; /* guilty until proved innocent */
1588 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001589
Daniel Stutzbach98338222010-12-02 21:55:33 +00001590 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1591 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 if (MERGE_GETMEM(ms, na) < 0)
1593 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001594 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1595 dest = ssa;
1596 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001597
Daniel Stutzbach98338222010-12-02 21:55:33 +00001598 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 --nb;
1600 if (nb == 0)
1601 goto Succeed;
1602 if (na == 1)
1603 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 min_gallop = ms->min_gallop;
1606 for (;;) {
1607 Py_ssize_t acount = 0; /* # of times A won in a row */
1608 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 /* Do the straightforward thing until (if ever) one run
1611 * appears to win consistently.
1612 */
1613 for (;;) {
1614 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001615 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 if (k) {
1617 if (k < 0)
1618 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001619 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 ++bcount;
1621 acount = 0;
1622 --nb;
1623 if (nb == 0)
1624 goto Succeed;
1625 if (bcount >= min_gallop)
1626 break;
1627 }
1628 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001629 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 ++acount;
1631 bcount = 0;
1632 --na;
1633 if (na == 1)
1634 goto CopyB;
1635 if (acount >= min_gallop)
1636 break;
1637 }
1638 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 /* One run is winning so consistently that galloping may
1641 * be a huge win. So try that, and continue galloping until
1642 * (if ever) neither run appears to be winning consistently
1643 * anymore.
1644 */
1645 ++min_gallop;
1646 do {
1647 assert(na > 1 && nb > 0);
1648 min_gallop -= min_gallop > 1;
1649 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001650 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 acount = k;
1652 if (k) {
1653 if (k < 0)
1654 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001655 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1656 sortslice_advance(&dest, k);
1657 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 na -= k;
1659 if (na == 1)
1660 goto CopyB;
1661 /* na==0 is impossible now if the comparison
1662 * function is consistent, but we can't assume
1663 * that it is.
1664 */
1665 if (na == 0)
1666 goto Succeed;
1667 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001668 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 --nb;
1670 if (nb == 0)
1671 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001672
embg1e34da42018-01-28 20:03:23 -07001673 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 bcount = k;
1675 if (k) {
1676 if (k < 0)
1677 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001678 sortslice_memmove(&dest, 0, &ssb, 0, k);
1679 sortslice_advance(&dest, k);
1680 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 nb -= k;
1682 if (nb == 0)
1683 goto Succeed;
1684 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001685 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 --na;
1687 if (na == 1)
1688 goto CopyB;
1689 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1690 ++min_gallop; /* penalize it for leaving galloping mode */
1691 ms->min_gallop = min_gallop;
1692 }
Tim Petersa64dc242002-08-01 02:13:36 +00001693Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001695Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001697 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001699CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001701 /* The last element of ssa belongs at the end of the merge. */
1702 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1703 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001705}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001706
Daniel Stutzbach98338222010-12-02 21:55:33 +00001707/* Merge the na elements starting at pa with the nb elements starting at
1708 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1709 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1710 * should have na >= nb. See listsort.txt for more info. Return 0 if
1711 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001712 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001713static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001714merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1715 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001718 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001721
Daniel Stutzbach98338222010-12-02 21:55:33 +00001722 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1723 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 if (MERGE_GETMEM(ms, nb) < 0)
1725 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001726 dest = ssb;
1727 sortslice_advance(&dest, nb-1);
1728 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1729 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001731 ssb.keys = ms->a.keys + nb - 1;
1732 if (ssb.values != NULL)
1733 ssb.values = ms->a.values + nb - 1;
1734 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001735
Daniel Stutzbach98338222010-12-02 21:55:33 +00001736 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 --na;
1738 if (na == 0)
1739 goto Succeed;
1740 if (nb == 1)
1741 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 min_gallop = ms->min_gallop;
1744 for (;;) {
1745 Py_ssize_t acount = 0; /* # of times A won in a row */
1746 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 /* Do the straightforward thing until (if ever) one run
1749 * appears to win consistently.
1750 */
1751 for (;;) {
1752 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001753 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 if (k) {
1755 if (k < 0)
1756 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001757 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 ++acount;
1759 bcount = 0;
1760 --na;
1761 if (na == 0)
1762 goto Succeed;
1763 if (acount >= min_gallop)
1764 break;
1765 }
1766 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001767 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 ++bcount;
1769 acount = 0;
1770 --nb;
1771 if (nb == 1)
1772 goto CopyA;
1773 if (bcount >= min_gallop)
1774 break;
1775 }
1776 }
Tim Petersa64dc242002-08-01 02:13:36 +00001777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 /* One run is winning so consistently that galloping may
1779 * be a huge win. So try that, and continue galloping until
1780 * (if ever) neither run appears to be winning consistently
1781 * anymore.
1782 */
1783 ++min_gallop;
1784 do {
1785 assert(na > 0 && nb > 1);
1786 min_gallop -= min_gallop > 1;
1787 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001788 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 if (k < 0)
1790 goto Fail;
1791 k = na - k;
1792 acount = k;
1793 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001794 sortslice_advance(&dest, -k);
1795 sortslice_advance(&ssa, -k);
1796 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 na -= k;
1798 if (na == 0)
1799 goto Succeed;
1800 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001801 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 --nb;
1803 if (nb == 1)
1804 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001805
embg1e34da42018-01-28 20:03:23 -07001806 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 if (k < 0)
1808 goto Fail;
1809 k = nb - k;
1810 bcount = k;
1811 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001812 sortslice_advance(&dest, -k);
1813 sortslice_advance(&ssb, -k);
1814 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 nb -= k;
1816 if (nb == 1)
1817 goto CopyA;
1818 /* nb==0 is impossible now if the comparison
1819 * function is consistent, but we can't assume
1820 * that it is.
1821 */
1822 if (nb == 0)
1823 goto Succeed;
1824 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001825 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 --na;
1827 if (na == 0)
1828 goto Succeed;
1829 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1830 ++min_gallop; /* penalize it for leaving galloping mode */
1831 ms->min_gallop = min_gallop;
1832 }
Tim Petersa64dc242002-08-01 02:13:36 +00001833Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001835Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001837 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001839CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001841 /* The first element of ssb belongs at the front of the merge. */
1842 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1843 sortslice_advance(&dest, -na);
1844 sortslice_advance(&ssa, -na);
1845 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001847}
1848
1849/* Merge the two runs at stack indices i and i+1.
1850 * Returns 0 on success, -1 on error.
1851 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001852static Py_ssize_t
1853merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001854{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001855 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 Py_ssize_t na, nb;
1857 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 assert(ms != NULL);
1860 assert(ms->n >= 2);
1861 assert(i >= 0);
1862 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001863
Daniel Stutzbach98338222010-12-02 21:55:33 +00001864 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001866 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 nb = ms->pending[i+1].len;
1868 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001869 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 /* Record the length of the combined runs; if i is the 3rd-last
1872 * run now, also slide over the last run (which isn't involved
1873 * in this merge). The current run i+1 goes away in any case.
1874 */
1875 ms->pending[i].len = na + nb;
1876 if (i == ms->n - 3)
1877 ms->pending[i+1] = ms->pending[i+2];
1878 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 /* Where does b start in a? Elements in a before that can be
1881 * ignored (already in place).
1882 */
embg1e34da42018-01-28 20:03:23 -07001883 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 if (k < 0)
1885 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001886 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 na -= k;
1888 if (na == 0)
1889 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 /* Where does a end in b? Elements in b after that can be
1892 * ignored (already in place).
1893 */
embg1e34da42018-01-28 20:03:23 -07001894 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 if (nb <= 0)
1896 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 /* Merge what remains of the runs, using a temp array with
1899 * min(na, nb) elements.
1900 */
1901 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001902 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001904 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001905}
1906
1907/* Examine the stack of runs waiting to be merged, merging adjacent runs
1908 * until the stack invariants are re-established:
1909 *
1910 * 1. len[-3] > len[-2] + len[-1]
1911 * 2. len[-2] > len[-1]
1912 *
1913 * See listsort.txt for more info.
1914 *
1915 * Returns 0 on success, -1 on error.
1916 */
1917static int
1918merge_collapse(MergeState *ms)
1919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 assert(ms);
1923 while (ms->n > 1) {
1924 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001925 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1926 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 if (p[n-1].len < p[n+1].len)
1928 --n;
1929 if (merge_at(ms, n) < 0)
1930 return -1;
1931 }
1932 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001933 if (merge_at(ms, n) < 0)
1934 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 }
1936 else
1937 break;
1938 }
1939 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001940}
1941
1942/* Regardless of invariants, merge all runs on the stack until only one
1943 * remains. This is used at the end of the mergesort.
1944 *
1945 * Returns 0 on success, -1 on error.
1946 */
1947static int
1948merge_force_collapse(MergeState *ms)
1949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 assert(ms);
1953 while (ms->n > 1) {
1954 Py_ssize_t n = ms->n - 2;
1955 if (n > 0 && p[n-1].len < p[n+1].len)
1956 --n;
1957 if (merge_at(ms, n) < 0)
1958 return -1;
1959 }
1960 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001961}
1962
1963/* Compute a good value for the minimum run length; natural runs shorter
1964 * than this are boosted artificially via binary insertion.
1965 *
1966 * If n < 64, return n (it's too small to bother with fancy stuff).
1967 * Else if n is an exact power of 2, return 32.
1968 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1969 * strictly less than, an exact power of 2.
1970 *
1971 * See listsort.txt for more info.
1972 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001973static Py_ssize_t
1974merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 assert(n >= 0);
1979 while (n >= 64) {
1980 r |= n & 1;
1981 n >>= 1;
1982 }
1983 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001984}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001985
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001986static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001987reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001988{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001989 reverse_slice(s->keys, &s->keys[n]);
1990 if (s->values != NULL)
1991 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001992}
1993
embg1e34da42018-01-28 20:03:23 -07001994/* Here we define custom comparison functions to optimize for the cases one commonly
1995 * encounters in practice: homogeneous lists, often of one of the basic types. */
1996
1997/* This struct holds the comparison function and helper functions
1998 * selected in the pre-sort check. */
1999
2000/* These are the special case compare functions.
2001 * ms->key_compare will always point to one of these: */
2002
2003/* Heterogeneous compare: default, always safe to fall back on. */
2004static int
2005safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2006{
2007 /* No assumptions necessary! */
2008 return PyObject_RichCompareBool(v, w, Py_LT);
2009}
2010
2011/* Homogeneous compare: safe for any two compareable objects of the same type.
2012 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2013 * pre-sort check.)
2014 */
2015static int
2016unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2017{
2018 PyObject *res_obj; int res;
2019
2020 /* No assumptions, because we check first: */
Victor Stinner58ac7002020-02-07 03:04:21 +01002021 if (Py_TYPE(v)->tp_richcompare != ms->key_richcompare)
embg1e34da42018-01-28 20:03:23 -07002022 return PyObject_RichCompareBool(v, w, Py_LT);
2023
2024 assert(ms->key_richcompare != NULL);
2025 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2026
2027 if (res_obj == Py_NotImplemented) {
2028 Py_DECREF(res_obj);
2029 return PyObject_RichCompareBool(v, w, Py_LT);
2030 }
2031 if (res_obj == NULL)
2032 return -1;
2033
2034 if (PyBool_Check(res_obj)) {
2035 res = (res_obj == Py_True);
2036 }
2037 else {
2038 res = PyObject_IsTrue(res_obj);
2039 }
2040 Py_DECREF(res_obj);
2041
2042 /* Note that we can't assert
2043 * res == PyObject_RichCompareBool(v, w, Py_LT);
2044 * because of evil compare functions like this:
2045 * lambda a, b: int(random.random() * 3) - 1)
2046 * (which is actually in test_sort.py) */
2047 return res;
2048}
2049
2050/* Latin string compare: safe for any two latin (one byte per char) strings. */
2051static int
2052unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2053{
Victor Stinner8017b802018-01-29 13:47:06 +01002054 Py_ssize_t len;
2055 int res;
embg1e34da42018-01-28 20:03:23 -07002056
2057 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
Andy Lesterdffe4c02020-03-04 07:15:20 -06002058 assert(Py_IS_TYPE(v, &PyUnicode_Type));
2059 assert(Py_IS_TYPE(w, &PyUnicode_Type));
embg1e34da42018-01-28 20:03:23 -07002060 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2061 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2062
2063 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2064 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2065
2066 res = (res != 0 ?
2067 res < 0 :
2068 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2069
2070 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2071 return res;
2072}
2073
2074/* Bounded int compare: compare any two longs that fit in a single machine word. */
2075static int
2076unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2077{
2078 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2079
2080 /* Modified from Objects/longobject.c:long_compare, assuming: */
Andy Lesterdffe4c02020-03-04 07:15:20 -06002081 assert(Py_IS_TYPE(v, &PyLong_Type));
2082 assert(Py_IS_TYPE(w, &PyLong_Type));
embg1e34da42018-01-28 20:03:23 -07002083 assert(Py_ABS(Py_SIZE(v)) <= 1);
2084 assert(Py_ABS(Py_SIZE(w)) <= 1);
2085
2086 vl = (PyLongObject*)v;
2087 wl = (PyLongObject*)w;
2088
2089 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2090 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2091
2092 if (Py_SIZE(vl) < 0)
2093 v0 = -v0;
2094 if (Py_SIZE(wl) < 0)
2095 w0 = -w0;
2096
2097 res = v0 < w0;
2098 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2099 return res;
2100}
2101
2102/* Float compare: compare any two floats. */
2103static int
2104unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2105{
2106 int res;
2107
2108 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
Andy Lesterdffe4c02020-03-04 07:15:20 -06002109 assert(Py_IS_TYPE(v, &PyFloat_Type));
2110 assert(Py_IS_TYPE(w, &PyFloat_Type));
embg1e34da42018-01-28 20:03:23 -07002111
2112 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2113 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2114 return res;
2115}
2116
2117/* Tuple compare: compare *any* two tuples, using
2118 * ms->tuple_elem_compare to compare the first elements, which is set
2119 * using the same pre-sort check as we use for ms->key_compare,
2120 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2121 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2122 * that most tuple compares don't involve x[1:]. */
2123static int
2124unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2125{
2126 PyTupleObject *vt, *wt;
2127 Py_ssize_t i, vlen, wlen;
2128 int k;
2129
2130 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
Andy Lesterdffe4c02020-03-04 07:15:20 -06002131 assert(Py_IS_TYPE(v, &PyTuple_Type));
2132 assert(Py_IS_TYPE(w, &PyTuple_Type));
embg1e34da42018-01-28 20:03:23 -07002133 assert(Py_SIZE(v) > 0);
2134 assert(Py_SIZE(w) > 0);
2135
2136 vt = (PyTupleObject *)v;
2137 wt = (PyTupleObject *)w;
2138
2139 vlen = Py_SIZE(vt);
2140 wlen = Py_SIZE(wt);
2141
2142 for (i = 0; i < vlen && i < wlen; i++) {
2143 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2144 if (k < 0)
2145 return -1;
2146 if (!k)
2147 break;
2148 }
2149
2150 if (i >= vlen || i >= wlen)
2151 return vlen < wlen;
2152
2153 if (i == 0)
2154 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2155 else
2156 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2157}
2158
Tim Petersa64dc242002-08-01 02:13:36 +00002159/* An adaptive, stable, natural mergesort. See listsort.txt.
2160 * Returns Py_None on success, NULL on error. Even in case of error, the
2161 * list will be some permutation of its input state (nothing is lost or
2162 * duplicated).
2163 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002164/*[clinic input]
2165list.sort
2166
2167 *
2168 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002169 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002170
Tim Hoffmann5c224762019-06-01 06:10:02 +02002171Sort the list in ascending order and return None.
2172
2173The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
2174order of two equal elements is maintained).
2175
2176If a key function is given, apply it once to each list item and sort them,
2177ascending or descending, according to their function values.
2178
2179The reverse flag can be set to sort in descending order.
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002180[clinic start generated code]*/
2181
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002182static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002183list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Tim Hoffmann5c224762019-06-01 06:10:02 +02002184/*[clinic end generated code: output=57b9f9c5e23fbe42 input=cb56cd179a713060]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 Py_ssize_t nremaining;
2188 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002189 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 Py_ssize_t saved_ob_size, saved_allocated;
2191 PyObject **saved_ob_item;
2192 PyObject **final_ob_item;
2193 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002195 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002198 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 if (keyfunc == Py_None)
2200 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 /* The list is temporarily made empty, so that mutations performed
2203 * by comparison functions can't affect the slice of memory we're
2204 * sorting (allowing mutations during sorting is a core-dump
2205 * factory, since ob_item may change).
2206 */
2207 saved_ob_size = Py_SIZE(self);
2208 saved_ob_item = self->ob_item;
2209 saved_allocated = self->allocated;
Victor Stinner60ac6ed2020-02-07 23:18:08 +01002210 Py_SET_SIZE(self, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 self->ob_item = NULL;
2212 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002213
Daniel Stutzbach98338222010-12-02 21:55:33 +00002214 if (keyfunc == NULL) {
2215 keys = NULL;
2216 lo.keys = saved_ob_item;
2217 lo.values = NULL;
2218 }
2219 else {
2220 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2221 /* Leverage stack space we allocated but won't otherwise use */
2222 keys = &ms.temparray[saved_ob_size+1];
2223 else {
2224 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002225 if (keys == NULL) {
2226 PyErr_NoMemory();
2227 goto keyfunc_fail;
2228 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002230
2231 for (i = 0; i < saved_ob_size ; i++) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002232 keys[i] = PyObject_CallOneArg(keyfunc, saved_ob_item[i]);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002233 if (keys[i] == NULL) {
2234 for (i=i-1 ; i>=0 ; i--)
2235 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002236 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002237 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002238 goto keyfunc_fail;
2239 }
2240 }
2241
2242 lo.keys = keys;
2243 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002245
embg1e34da42018-01-28 20:03:23 -07002246
2247 /* The pre-sort check: here's where we decide which compare function to use.
2248 * How much optimization is safe? We test for homogeneity with respect to
2249 * several properties that are expensive to check at compare-time, and
2250 * set ms appropriately. */
2251 if (saved_ob_size > 1) {
2252 /* Assume the first element is representative of the whole list. */
Andy Lesterdffe4c02020-03-04 07:15:20 -06002253 int keys_are_in_tuples = (Py_IS_TYPE(lo.keys[0], &PyTuple_Type) &&
embg1e34da42018-01-28 20:03:23 -07002254 Py_SIZE(lo.keys[0]) > 0);
2255
2256 PyTypeObject* key_type = (keys_are_in_tuples ?
Victor Stinner58ac7002020-02-07 03:04:21 +01002257 Py_TYPE(PyTuple_GET_ITEM(lo.keys[0], 0)) :
2258 Py_TYPE(lo.keys[0]));
embg1e34da42018-01-28 20:03:23 -07002259
2260 int keys_are_all_same_type = 1;
2261 int strings_are_latin = 1;
2262 int ints_are_bounded = 1;
2263
2264 /* Prove that assumption by checking every key. */
embg1e34da42018-01-28 20:03:23 -07002265 for (i=0; i < saved_ob_size; i++) {
2266
2267 if (keys_are_in_tuples &&
Andy Lesterdffe4c02020-03-04 07:15:20 -06002268 !(Py_IS_TYPE(lo.keys[i], &PyTuple_Type) && Py_SIZE(lo.keys[i]) != 0)) {
embg1e34da42018-01-28 20:03:23 -07002269 keys_are_in_tuples = 0;
2270 keys_are_all_same_type = 0;
2271 break;
2272 }
2273
2274 /* Note: for lists of tuples, key is the first element of the tuple
2275 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2276 * for lists of tuples in the if-statement directly above. */
2277 PyObject *key = (keys_are_in_tuples ?
2278 PyTuple_GET_ITEM(lo.keys[i], 0) :
2279 lo.keys[i]);
2280
Andy Lesterdffe4c02020-03-04 07:15:20 -06002281 if (!Py_IS_TYPE(key, key_type)) {
embg1e34da42018-01-28 20:03:23 -07002282 keys_are_all_same_type = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002283 /* If keys are in tuple we must loop over the whole list to make
2284 sure all items are tuples */
2285 if (!keys_are_in_tuples) {
2286 break;
2287 }
embg1e34da42018-01-28 20:03:23 -07002288 }
2289
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002290 if (keys_are_all_same_type) {
2291 if (key_type == &PyLong_Type &&
2292 ints_are_bounded &&
2293 Py_ABS(Py_SIZE(key)) > 1) {
2294
embg1e34da42018-01-28 20:03:23 -07002295 ints_are_bounded = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002296 }
2297 else if (key_type == &PyUnicode_Type &&
2298 strings_are_latin &&
2299 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {
2300
2301 strings_are_latin = 0;
2302 }
2303 }
embg1e34da42018-01-28 20:03:23 -07002304 }
embg1e34da42018-01-28 20:03:23 -07002305
2306 /* Choose the best compare, given what we now know about the keys. */
2307 if (keys_are_all_same_type) {
2308
2309 if (key_type == &PyUnicode_Type && strings_are_latin) {
2310 ms.key_compare = unsafe_latin_compare;
2311 }
2312 else if (key_type == &PyLong_Type && ints_are_bounded) {
2313 ms.key_compare = unsafe_long_compare;
2314 }
2315 else if (key_type == &PyFloat_Type) {
2316 ms.key_compare = unsafe_float_compare;
2317 }
2318 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2319 ms.key_compare = unsafe_object_compare;
2320 }
Zackery Spytzebc793d2019-02-21 00:47:14 -07002321 else {
2322 ms.key_compare = safe_object_compare;
2323 }
embg1e34da42018-01-28 20:03:23 -07002324 }
2325 else {
2326 ms.key_compare = safe_object_compare;
2327 }
2328
2329 if (keys_are_in_tuples) {
2330 /* Make sure we're not dealing with tuples of tuples
2331 * (remember: here, key_type refers list [key[0] for key in keys]) */
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002332 if (key_type == &PyTuple_Type) {
embg1e34da42018-01-28 20:03:23 -07002333 ms.tuple_elem_compare = safe_object_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002334 }
2335 else {
embg1e34da42018-01-28 20:03:23 -07002336 ms.tuple_elem_compare = ms.key_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002337 }
embg1e34da42018-01-28 20:03:23 -07002338
2339 ms.key_compare = unsafe_tuple_compare;
2340 }
2341 }
2342 /* End of pre-sort check: ms is now set properly! */
2343
Daniel Stutzbach98338222010-12-02 21:55:33 +00002344 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 nremaining = saved_ob_size;
2347 if (nremaining < 2)
2348 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002349
Benjamin Peterson05380642010-08-23 19:35:39 +00002350 /* Reverse sort stability achieved by initially reversing the list,
2351 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002352 if (reverse) {
2353 if (keys != NULL)
2354 reverse_slice(&keys[0], &keys[saved_ob_size]);
2355 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2356 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 /* March over the array once, left to right, finding natural runs,
2359 * and extending short natural runs to minrun elements.
2360 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 minrun = merge_compute_minrun(nremaining);
2362 do {
2363 int descending;
2364 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002367 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 if (n < 0)
2369 goto fail;
2370 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002371 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 /* If short, extend to min(minrun, nremaining). */
2373 if (n < minrun) {
2374 const Py_ssize_t force = nremaining <= minrun ?
2375 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002376 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 goto fail;
2378 n = force;
2379 }
2380 /* Push run onto pending-runs stack, and maybe merge. */
2381 assert(ms.n < MAX_MERGE_PENDING);
2382 ms.pending[ms.n].base = lo;
2383 ms.pending[ms.n].len = n;
2384 ++ms.n;
2385 if (merge_collapse(&ms) < 0)
2386 goto fail;
2387 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002388 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 nremaining -= n;
2390 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 if (merge_force_collapse(&ms) < 0)
2393 goto fail;
2394 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002395 assert(keys == NULL
2396 ? ms.pending[0].base.keys == saved_ob_item
2397 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002399 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002400
2401succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002403fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002404 if (keys != NULL) {
2405 for (i = 0; i < saved_ob_size; i++)
2406 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002407 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002408 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 if (self->allocated != -1 && result != NULL) {
2412 /* The user mucked with the list during the sort,
2413 * and we don't already have another error to report.
2414 */
2415 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2416 result = NULL;
2417 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 if (reverse && saved_ob_size > 1)
2420 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002423
Daniel Stutzbach98338222010-12-02 21:55:33 +00002424keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 final_ob_item = self->ob_item;
2426 i = Py_SIZE(self);
Victor Stinner60ac6ed2020-02-07 23:18:08 +01002427 Py_SET_SIZE(self, saved_ob_size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 self->ob_item = saved_ob_item;
2429 self->allocated = saved_allocated;
2430 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002431 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 guarantee that the list is really empty when it returns */
2433 while (--i >= 0) {
2434 Py_XDECREF(final_ob_item[i]);
2435 }
2436 PyMem_FREE(final_ob_item);
2437 }
2438 Py_XINCREF(result);
2439 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002440}
Tim Peters330f9e92002-07-19 07:05:44 +00002441#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002442#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002443
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002444int
Fred Drakea2f55112000-07-09 15:16:51 +00002445PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 if (v == NULL || !PyList_Check(v)) {
2448 PyErr_BadInternalCall();
2449 return -1;
2450 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002451 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 if (v == NULL)
2453 return -1;
2454 Py_DECREF(v);
2455 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002456}
2457
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002458/*[clinic input]
2459list.reverse
2460
2461Reverse *IN PLACE*.
2462[clinic start generated code]*/
2463
Guido van Rossumb86c5492001-02-12 22:06:02 +00002464static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002465list_reverse_impl(PyListObject *self)
2466/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 if (Py_SIZE(self) > 1)
2469 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2470 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002471}
2472
Guido van Rossum84c76f51990-10-30 13:32:20 +00002473int
Fred Drakea2f55112000-07-09 15:16:51 +00002474PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 if (v == NULL || !PyList_Check(v)) {
2479 PyErr_BadInternalCall();
2480 return -1;
2481 }
2482 if (Py_SIZE(self) > 1)
2483 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2484 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002485}
2486
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002487PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002488PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 if (v == NULL || !PyList_Check(v)) {
2491 PyErr_BadInternalCall();
2492 return NULL;
2493 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +05002494 return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002495}
2496
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002497/*[clinic input]
2498list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002499
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002500 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002501 start: slice_index(accept={int}) = 0
2502 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002503 /
2504
2505Return first index of value.
2506
2507Raises ValueError if the value is not present.
2508[clinic start generated code]*/
2509
2510static PyObject *
2511list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2512 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002513/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002514{
2515 Py_ssize_t i;
2516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 if (start < 0) {
2518 start += Py_SIZE(self);
2519 if (start < 0)
2520 start = 0;
2521 }
2522 if (stop < 0) {
2523 stop += Py_SIZE(self);
2524 if (stop < 0)
2525 stop = 0;
2526 }
2527 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Zackery Spytzd9e561d2019-12-30 12:32:58 -07002528 PyObject *obj = self->ob_item[i];
2529 Py_INCREF(obj);
2530 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2531 Py_DECREF(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 if (cmp > 0)
2533 return PyLong_FromSsize_t(i);
2534 else if (cmp < 0)
2535 return NULL;
2536 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002537 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002539}
2540
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002541/*[clinic input]
2542list.count
2543
2544 value: object
2545 /
2546
2547Return number of occurrences of value.
2548[clinic start generated code]*/
2549
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002550static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002551list_count(PyListObject *self, PyObject *value)
2552/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 Py_ssize_t count = 0;
2555 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 for (i = 0; i < Py_SIZE(self); i++) {
Zackery Spytzd9e561d2019-12-30 12:32:58 -07002558 PyObject *obj = self->ob_item[i];
Dong-hee Na14d80d02020-01-23 02:36:54 +09002559 if (obj == value) {
2560 count++;
2561 continue;
2562 }
Zackery Spytzd9e561d2019-12-30 12:32:58 -07002563 Py_INCREF(obj);
2564 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2565 Py_DECREF(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 if (cmp > 0)
2567 count++;
2568 else if (cmp < 0)
2569 return NULL;
2570 }
2571 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002572}
2573
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002574/*[clinic input]
2575list.remove
2576
2577 value: object
2578 /
2579
2580Remove first occurrence of value.
2581
2582Raises ValueError if the value is not present.
2583[clinic start generated code]*/
2584
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002585static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002586list_remove(PyListObject *self, PyObject *value)
2587/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 for (i = 0; i < Py_SIZE(self); i++) {
Zackery Spytzd9e561d2019-12-30 12:32:58 -07002592 PyObject *obj = self->ob_item[i];
2593 Py_INCREF(obj);
2594 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2595 Py_DECREF(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 if (cmp > 0) {
2597 if (list_ass_slice(self, i, i+1,
2598 (PyObject *)NULL) == 0)
2599 Py_RETURN_NONE;
2600 return NULL;
2601 }
2602 else if (cmp < 0)
2603 return NULL;
2604 }
2605 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2606 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002607}
2608
Jeremy Hylton8caad492000-06-23 14:18:11 +00002609static int
2610list_traverse(PyListObject *o, visitproc visit, void *arg)
2611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 for (i = Py_SIZE(o); --i >= 0; )
2615 Py_VISIT(o->ob_item[i]);
2616 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002617}
2618
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002619static PyObject *
2620list_richcompare(PyObject *v, PyObject *w, int op)
2621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 PyListObject *vl, *wl;
2623 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002624
Brian Curtindfc80e32011-08-10 20:28:54 -05002625 if (!PyList_Check(v) || !PyList_Check(w))
2626 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 vl = (PyListObject *)v;
2629 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2632 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002634 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 else
stratakise8b19652017-11-02 11:32:54 +01002636 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 /* Search for the first index where items are different */
2640 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002641 PyObject *vitem = vl->ob_item[i];
2642 PyObject *witem = wl->ob_item[i];
Inada Naokidfef9862019-12-31 10:58:37 +09002643 if (vitem == witem) {
2644 continue;
2645 }
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002646
2647 Py_INCREF(vitem);
2648 Py_INCREF(witem);
sweeneydebe7ead62020-02-26 02:00:35 -05002649 int k = PyObject_RichCompareBool(vitem, witem, Py_EQ);
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002650 Py_DECREF(vitem);
2651 Py_DECREF(witem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 if (k < 0)
2653 return NULL;
2654 if (!k)
2655 break;
2656 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2659 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002660 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 /* We have an item that differs -- shortcuts for EQ/NE */
2664 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002665 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 }
2667 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002668 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 /* Compare the final item again using the proper operator */
2672 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002673}
2674
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002675/*[clinic input]
2676list.__init__
2677
2678 iterable: object(c_default="NULL") = ()
2679 /
2680
2681Built-in mutable sequence.
2682
2683If no argument is given, the constructor creates a new empty list.
2684The argument must be an iterable if specified.
2685[clinic start generated code]*/
2686
Tim Peters6d6c1a32001-08-02 04:15:00 +00002687static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002688list___init___impl(PyListObject *self, PyObject *iterable)
2689/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 /* Verify list invariants established by PyType_GenericAlloc() */
2692 assert(0 <= Py_SIZE(self));
2693 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2694 assert(self->ob_item != NULL ||
2695 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 /* Empty previous contents */
2698 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002699 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002701 if (iterable != NULL) {
Pablo Galindo372d7052018-10-28 20:16:26 +00002702 if (_PyObject_HasLen(iterable)) {
2703 Py_ssize_t iter_len = PyObject_Size(iterable);
2704 if (iter_len == -1) {
2705 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2706 return -1;
2707 }
2708 PyErr_Clear();
2709 }
2710 if (iter_len > 0 && self->ob_item == NULL
2711 && list_preallocate_exact(self, iter_len)) {
2712 return -1;
2713 }
2714 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002715 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 if (rv == NULL)
2717 return -1;
2718 Py_DECREF(rv);
2719 }
2720 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002721}
2722
Petr Viktorince105542020-03-30 14:16:16 +02002723static PyObject *
2724list_vectorcall(PyObject *type, PyObject * const*args,
2725 size_t nargsf, PyObject *kwnames)
2726{
2727 if (!_PyArg_NoKwnames("list", kwnames)) {
2728 return NULL;
2729 }
2730 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
2731 if (!_PyArg_CheckPositional("list", nargs, 0, 1)) {
2732 return NULL;
2733 }
2734
2735 assert(PyType_Check(type));
2736 PyObject *list = PyType_GenericAlloc((PyTypeObject *)type, 0);
2737 if (list == NULL) {
2738 return NULL;
2739 }
2740 if (nargs) {
2741 if (list___init___impl((PyListObject *)list, args[0])) {
2742 Py_DECREF(list);
2743 return NULL;
2744 }
2745 }
2746 return list;
2747}
2748
2749
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002750/*[clinic input]
2751list.__sizeof__
2752
2753Return the size of the list in memory, in bytes.
2754[clinic start generated code]*/
2755
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002756static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002757list___sizeof___impl(PyListObject *self)
2758/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002761
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002762 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002764}
2765
Raymond Hettinger1021c442003-11-07 15:38:09 +00002766static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002767static PyObject *list_subscript(PyListObject*, PyObject*);
2768
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002769static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002770 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2771 LIST___REVERSED___METHODDEF
2772 LIST___SIZEOF___METHODDEF
2773 LIST_CLEAR_METHODDEF
2774 LIST_COPY_METHODDEF
2775 LIST_APPEND_METHODDEF
2776 LIST_INSERT_METHODDEF
2777 LIST_EXTEND_METHODDEF
2778 LIST_POP_METHODDEF
2779 LIST_REMOVE_METHODDEF
2780 LIST_INDEX_METHODDEF
2781 LIST_COUNT_METHODDEF
2782 LIST_REVERSE_METHODDEF
2783 LIST_SORT_METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -07002784 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002786};
2787
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002788static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 (lenfunc)list_length, /* sq_length */
2790 (binaryfunc)list_concat, /* sq_concat */
2791 (ssizeargfunc)list_repeat, /* sq_repeat */
2792 (ssizeargfunc)list_item, /* sq_item */
2793 0, /* sq_slice */
2794 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2795 0, /* sq_ass_slice */
2796 (objobjproc)list_contains, /* sq_contains */
2797 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2798 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002799};
2800
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002801static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002802list_subscript(PyListObject* self, PyObject* item)
2803{
Victor Stinnera15e2602020-04-08 02:01:56 +02002804 if (_PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 Py_ssize_t i;
2806 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2807 if (i == -1 && PyErr_Occurred())
2808 return NULL;
2809 if (i < 0)
2810 i += PyList_GET_SIZE(self);
2811 return list_item(self, i);
2812 }
2813 else if (PySlice_Check(item)) {
HongWeipeng3c87a662019-09-08 18:15:56 +08002814 Py_ssize_t start, stop, step, slicelength, i;
2815 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 PyObject* result;
2817 PyObject* it;
2818 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002819
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002820 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 return NULL;
2822 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002823 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2824 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 if (slicelength <= 0) {
2827 return PyList_New(0);
2828 }
2829 else if (step == 1) {
2830 return list_slice(self, start, stop);
2831 }
2832 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002833 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 src = self->ob_item;
2837 dest = ((PyListObject *)result)->ob_item;
2838 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002839 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 it = src[cur];
2841 Py_INCREF(it);
2842 dest[i] = it;
2843 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +01002844 Py_SET_SIZE(result, slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 return result;
2846 }
2847 }
2848 else {
2849 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002850 "list indices must be integers or slices, not %.200s",
Victor Stinner58ac7002020-02-07 03:04:21 +01002851 Py_TYPE(item)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 return NULL;
2853 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002854}
2855
Tim Peters3b01a122002-07-19 02:35:45 +00002856static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002857list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2858{
Victor Stinnera15e2602020-04-08 02:01:56 +02002859 if (_PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2861 if (i == -1 && PyErr_Occurred())
2862 return -1;
2863 if (i < 0)
2864 i += PyList_GET_SIZE(self);
2865 return list_ass_item(self, i, value);
2866 }
2867 else if (PySlice_Check(item)) {
2868 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002869
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002870 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 return -1;
2872 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002873 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2874 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 if (step == 1)
2877 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 /* Make sure s[5:2] = [..] inserts at the right place:
2880 before 5, not before 2. */
2881 if ((step < 0 && start < stop) ||
2882 (step > 0 && start > stop))
2883 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 if (value == NULL) {
2886 /* delete slice */
2887 PyObject **garbage;
2888 size_t cur;
2889 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002890 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 if (slicelength <= 0)
2893 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 if (step < 0) {
2896 stop = start + 1;
2897 start = stop + step*(slicelength - 1) - 1;
2898 step = -step;
2899 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 garbage = (PyObject**)
2902 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2903 if (!garbage) {
2904 PyErr_NoMemory();
2905 return -1;
2906 }
Tim Peters3b01a122002-07-19 02:35:45 +00002907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 /* drawing pictures might help understand these for
2909 loops. Basically, we memmove the parts of the
2910 list that are *not* part of the slice: step-1
2911 items for each item that is part of the slice,
2912 and then tail end of the list that was not
2913 covered by the slice */
2914 for (cur = start, i = 0;
2915 cur < (size_t)stop;
2916 cur += step, i++) {
2917 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 if (cur + step >= (size_t)Py_SIZE(self)) {
2922 lim = Py_SIZE(self) - cur - 1;
2923 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 memmove(self->ob_item + cur - i,
2926 self->ob_item + cur + 1,
2927 lim * sizeof(PyObject *));
2928 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002929 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 if (cur < (size_t)Py_SIZE(self)) {
2931 memmove(self->ob_item + cur - slicelength,
2932 self->ob_item + cur,
2933 (Py_SIZE(self) - cur) *
2934 sizeof(PyObject *));
2935 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002936
Victor Stinner60ac6ed2020-02-07 23:18:08 +01002937 Py_SET_SIZE(self, Py_SIZE(self) - slicelength);
Victor Stinner35f28032013-11-21 12:16:35 +01002938 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 for (i = 0; i < slicelength; i++) {
2941 Py_DECREF(garbage[i]);
2942 }
2943 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002944
Victor Stinner35f28032013-11-21 12:16:35 +01002945 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 }
2947 else {
2948 /* assign slice */
2949 PyObject *ins, *seq;
2950 PyObject **garbage, **seqitems, **selfitems;
HongWeipeng3c87a662019-09-08 18:15:56 +08002951 Py_ssize_t i;
2952 size_t cur;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 /* protect against a[::-1] = a */
2955 if (self == (PyListObject*)value) {
2956 seq = list_slice((PyListObject*)value, 0,
2957 PyList_GET_SIZE(value));
2958 }
2959 else {
2960 seq = PySequence_Fast(value,
2961 "must assign iterable "
2962 "to extended slice");
2963 }
2964 if (!seq)
2965 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2968 PyErr_Format(PyExc_ValueError,
2969 "attempt to assign sequence of "
2970 "size %zd to extended slice of "
2971 "size %zd",
2972 PySequence_Fast_GET_SIZE(seq),
2973 slicelength);
2974 Py_DECREF(seq);
2975 return -1;
2976 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 if (!slicelength) {
2979 Py_DECREF(seq);
2980 return 0;
2981 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 garbage = (PyObject**)
2984 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2985 if (!garbage) {
2986 Py_DECREF(seq);
2987 PyErr_NoMemory();
2988 return -1;
2989 }
Tim Peters3b01a122002-07-19 02:35:45 +00002990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 selfitems = self->ob_item;
2992 seqitems = PySequence_Fast_ITEMS(seq);
2993 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002994 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 garbage[i] = selfitems[cur];
2996 ins = seqitems[i];
2997 Py_INCREF(ins);
2998 selfitems[cur] = ins;
2999 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 for (i = 0; i < slicelength; i++) {
3002 Py_DECREF(garbage[i]);
3003 }
Tim Peters3b01a122002-07-19 02:35:45 +00003004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 PyMem_FREE(garbage);
3006 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00003007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 return 0;
3009 }
3010 }
3011 else {
3012 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04003013 "list indices must be integers or slices, not %.200s",
Victor Stinner58ac7002020-02-07 03:04:21 +01003014 Py_TYPE(item)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 return -1;
3016 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003017}
3018
3019static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 (lenfunc)list_length,
3021 (binaryfunc)list_subscript,
3022 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003023};
3024
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003025PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3027 "list",
3028 sizeof(PyListObject),
3029 0,
3030 (destructor)list_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003031 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 0, /* tp_getattr */
3033 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003034 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 (reprfunc)list_repr, /* tp_repr */
3036 0, /* tp_as_number */
3037 &list_as_sequence, /* tp_as_sequence */
3038 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003039 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 0, /* tp_call */
3041 0, /* tp_str */
3042 PyObject_GenericGetAttr, /* tp_getattro */
3043 0, /* tp_setattro */
3044 0, /* tp_as_buffer */
3045 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003046 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
3047 list___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 (traverseproc)list_traverse, /* tp_traverse */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003049 (inquiry)_list_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 list_richcompare, /* tp_richcompare */
3051 0, /* tp_weaklistoffset */
3052 list_iter, /* tp_iter */
3053 0, /* tp_iternext */
3054 list_methods, /* tp_methods */
3055 0, /* tp_members */
3056 0, /* tp_getset */
3057 0, /* tp_base */
3058 0, /* tp_dict */
3059 0, /* tp_descr_get */
3060 0, /* tp_descr_set */
3061 0, /* tp_dictoffset */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003062 (initproc)list___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 PyType_GenericAlloc, /* tp_alloc */
3064 PyType_GenericNew, /* tp_new */
3065 PyObject_GC_Del, /* tp_free */
Petr Viktorince105542020-03-30 14:16:16 +02003066 .tp_vectorcall = list_vectorcall,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003067};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00003068
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003069/*********************** List Iterator **************************/
3070
3071typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02003073 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003075} listiterobject;
3076
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003077static void listiter_dealloc(listiterobject *);
3078static int listiter_traverse(listiterobject *, visitproc, void *);
3079static PyObject *listiter_next(listiterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303080static PyObject *listiter_len(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003081static PyObject *listiter_reduce_general(void *_it, int forward);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303082static PyObject *listiter_reduce(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003083static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00003084
Armin Rigof5b3e362006-02-11 21:32:43 +00003085PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003086PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3087PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003088
3089static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003091 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3092 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00003094};
3095
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003096PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3098 "list_iterator", /* tp_name */
3099 sizeof(listiterobject), /* tp_basicsize */
3100 0, /* tp_itemsize */
3101 /* methods */
3102 (destructor)listiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003103 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 0, /* tp_getattr */
3105 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003106 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 0, /* tp_repr */
3108 0, /* tp_as_number */
3109 0, /* tp_as_sequence */
3110 0, /* tp_as_mapping */
3111 0, /* tp_hash */
3112 0, /* tp_call */
3113 0, /* tp_str */
3114 PyObject_GenericGetAttr, /* tp_getattro */
3115 0, /* tp_setattro */
3116 0, /* tp_as_buffer */
3117 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3118 0, /* tp_doc */
3119 (traverseproc)listiter_traverse, /* tp_traverse */
3120 0, /* tp_clear */
3121 0, /* tp_richcompare */
3122 0, /* tp_weaklistoffset */
3123 PyObject_SelfIter, /* tp_iter */
3124 (iternextfunc)listiter_next, /* tp_iternext */
3125 listiter_methods, /* tp_methods */
3126 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003127};
Raymond Hettinger1021c442003-11-07 15:38:09 +00003128
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003129
3130static PyObject *
3131list_iter(PyObject *seq)
3132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 if (!PyList_Check(seq)) {
3136 PyErr_BadInternalCall();
3137 return NULL;
3138 }
3139 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3140 if (it == NULL)
3141 return NULL;
3142 it->it_index = 0;
3143 Py_INCREF(seq);
3144 it->it_seq = (PyListObject *)seq;
3145 _PyObject_GC_TRACK(it);
3146 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003147}
3148
3149static void
3150listiter_dealloc(listiterobject *it)
3151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 _PyObject_GC_UNTRACK(it);
3153 Py_XDECREF(it->it_seq);
3154 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003155}
3156
3157static int
3158listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 Py_VISIT(it->it_seq);
3161 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003162}
3163
3164static PyObject *
3165listiter_next(listiterobject *it)
3166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 PyListObject *seq;
3168 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 assert(it != NULL);
3171 seq = it->it_seq;
3172 if (seq == NULL)
3173 return NULL;
3174 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 if (it->it_index < PyList_GET_SIZE(seq)) {
3177 item = PyList_GET_ITEM(seq, it->it_index);
3178 ++it->it_index;
3179 Py_INCREF(item);
3180 return item;
3181 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003184 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003186}
3187
3188static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303189listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 Py_ssize_t len;
3192 if (it->it_seq) {
3193 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3194 if (len >= 0)
3195 return PyLong_FromSsize_t(len);
3196 }
3197 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003198}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003199
3200static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303201listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003202{
3203 return listiter_reduce_general(it, 1);
3204}
3205
3206static PyObject *
3207listiter_setstate(listiterobject *it, PyObject *state)
3208{
Victor Stinner7660b882013-06-24 23:59:24 +02003209 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003210 if (index == -1 && PyErr_Occurred())
3211 return NULL;
3212 if (it->it_seq != NULL) {
3213 if (index < 0)
3214 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003215 else if (index > PyList_GET_SIZE(it->it_seq))
3216 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003217 it->it_index = index;
3218 }
3219 Py_RETURN_NONE;
3220}
3221
Raymond Hettinger1021c442003-11-07 15:38:09 +00003222/*********************** List Reverse Iterator **************************/
3223
3224typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 PyObject_HEAD
3226 Py_ssize_t it_index;
3227 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00003228} listreviterobject;
3229
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003230static void listreviter_dealloc(listreviterobject *);
3231static int listreviter_traverse(listreviterobject *, visitproc, void *);
3232static PyObject *listreviter_next(listreviterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303233static PyObject *listreviter_len(listreviterobject *, PyObject *);
3234static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003235static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003236
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003237static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003239 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3240 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003242};
3243
Raymond Hettinger1021c442003-11-07 15:38:09 +00003244PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3246 "list_reverseiterator", /* tp_name */
3247 sizeof(listreviterobject), /* tp_basicsize */
3248 0, /* tp_itemsize */
3249 /* methods */
3250 (destructor)listreviter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003251 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 0, /* tp_getattr */
3253 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003254 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 0, /* tp_repr */
3256 0, /* tp_as_number */
3257 0, /* tp_as_sequence */
3258 0, /* tp_as_mapping */
3259 0, /* tp_hash */
3260 0, /* tp_call */
3261 0, /* tp_str */
3262 PyObject_GenericGetAttr, /* tp_getattro */
3263 0, /* tp_setattro */
3264 0, /* tp_as_buffer */
3265 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3266 0, /* tp_doc */
3267 (traverseproc)listreviter_traverse, /* tp_traverse */
3268 0, /* tp_clear */
3269 0, /* tp_richcompare */
3270 0, /* tp_weaklistoffset */
3271 PyObject_SelfIter, /* tp_iter */
3272 (iternextfunc)listreviter_next, /* tp_iternext */
3273 listreviter_methods, /* tp_methods */
3274 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00003275};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003276
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003277/*[clinic input]
3278list.__reversed__
3279
3280Return a reverse iterator over the list.
3281[clinic start generated code]*/
3282
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003283static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003284list___reversed___impl(PyListObject *self)
3285/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3290 if (it == NULL)
3291 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003292 assert(PyList_Check(self));
3293 it->it_index = PyList_GET_SIZE(self) - 1;
3294 Py_INCREF(self);
3295 it->it_seq = self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 PyObject_GC_Track(it);
3297 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003298}
3299
3300static void
3301listreviter_dealloc(listreviterobject *it)
3302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 PyObject_GC_UnTrack(it);
3304 Py_XDECREF(it->it_seq);
3305 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003306}
3307
3308static int
3309listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 Py_VISIT(it->it_seq);
3312 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003313}
3314
3315static PyObject *
3316listreviter_next(listreviterobject *it)
3317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003319 Py_ssize_t index;
3320 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003321
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003322 assert(it != NULL);
3323 seq = it->it_seq;
3324 if (seq == NULL) {
3325 return NULL;
3326 }
3327 assert(PyList_Check(seq));
3328
3329 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 if (index>=0 && index < PyList_GET_SIZE(seq)) {
3331 item = PyList_GET_ITEM(seq, index);
3332 it->it_index--;
3333 Py_INCREF(item);
3334 return item;
3335 }
3336 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003337 it->it_seq = NULL;
3338 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003340}
3341
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003342static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303343listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 Py_ssize_t len = it->it_index + 1;
3346 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3347 len = 0;
3348 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003349}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003350
3351static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303352listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003353{
3354 return listiter_reduce_general(it, 0);
3355}
3356
3357static PyObject *
3358listreviter_setstate(listreviterobject *it, PyObject *state)
3359{
3360 Py_ssize_t index = PyLong_AsSsize_t(state);
3361 if (index == -1 && PyErr_Occurred())
3362 return NULL;
3363 if (it->it_seq != NULL) {
3364 if (index < -1)
3365 index = -1;
3366 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3367 index = PyList_GET_SIZE(it->it_seq) - 1;
3368 it->it_index = index;
3369 }
3370 Py_RETURN_NONE;
3371}
3372
3373/* common pickling support */
3374
3375static PyObject *
3376listiter_reduce_general(void *_it, int forward)
3377{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003378 _Py_IDENTIFIER(iter);
3379 _Py_IDENTIFIER(reversed);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003380 PyObject *list;
3381
3382 /* the objects are not the same, index is of different types! */
3383 if (forward) {
3384 listiterobject *it = (listiterobject *)_it;
3385 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003386 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003387 it->it_seq, it->it_index);
3388 } else {
3389 listreviterobject *it = (listreviterobject *)_it;
3390 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003391 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_reversed),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003392 it->it_seq, it->it_index);
3393 }
3394 /* empty iterator, create an empty list */
3395 list = PyList_New(0);
3396 if (list == NULL)
3397 return NULL;
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003398 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003399}