blob: f9eb37064a9c975483a4517b158b1ea96db494a7 [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"
Sergey Fedoseev234531b2019-02-25 21:59:12 +05006#include "pycore_tupleobject.h"
Victor Stinnere281f7d2018-11-01 02:30:36 +01007#include "pycore_accu.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00008
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00009#ifdef STDC_HEADERS
10#include <stddef.h>
11#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000012#include <sys/types.h> /* For size_t */
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000013#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014
Serhiy Storchakafdd42c42017-03-11 09:19:20 +020015/*[clinic input]
16class list "PyListObject *" "&PyList_Type"
17[clinic start generated code]*/
18/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f9b222678f9f71e0]*/
19
20#include "clinic/listobject.c.h"
21
Tim Peters8d9eb102004-07-31 02:24:20 +000022/* Ensure ob_item has room for at least newsize elements, and set
23 * ob_size to newsize. If newsize > ob_size on entry, the content
24 * of the new slots at exit is undefined heap trash; it's the caller's
Ezio Melotti13925002011-03-16 11:05:33 +020025 * responsibility to overwrite them with sane values.
Tim Peters8d9eb102004-07-31 02:24:20 +000026 * The number of allocated elements may grow, shrink, or stay the same.
27 * Failure is impossible if newsize <= self.allocated on entry, although
28 * that partly relies on an assumption that the system realloc() never
29 * fails when passed a number of bytes <= the number of bytes last
30 * allocated (the C standard doesn't guarantee this, but it's hard to
31 * imagine a realloc implementation where it wouldn't be true).
32 * Note that self->ob_item may change, and even if newsize is less
33 * than ob_size on entry.
34 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000035static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000036list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 PyObject **items;
Xiang Zhang4cee0492017-02-22 12:32:30 +080039 size_t new_allocated, num_allocated_bytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 /* Bypass realloc() when a previous overallocation is large enough
43 to accommodate the newsize. If the newsize falls lower than half
44 the allocated size, then proceed with the realloc() to shrink the list.
45 */
46 if (allocated >= newsize && newsize >= (allocated >> 1)) {
47 assert(self->ob_item != NULL || newsize == 0);
Victor Stinner60ac6ed2020-02-07 23:18:08 +010048 Py_SET_SIZE(self, newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 return 0;
50 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 /* This over-allocates proportional to the list size, making room
53 * for additional growth. The over-allocation is mild, but is
54 * enough to give linear-time amortized behavior over a long
55 * sequence of appends() in the presence of a poorly-performing
56 * system realloc().
Serhiy Storchaka2fe815e2020-03-17 23:46:00 +020057 * Add padding to make the allocated size multiple of 4.
58 * The growth pattern is: 0, 4, 8, 16, 24, 32, 40, 52, 64, 76, ...
Xiang Zhang4cee0492017-02-22 12:32:30 +080059 * Note: new_allocated won't overflow because the largest possible value
60 * is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 */
Serhiy Storchaka2fe815e2020-03-17 23:46:00 +020062 new_allocated = ((size_t)newsize + (newsize >> 3) + 6) & ~(size_t)3;
63 /* Do not overallocate if the new size is closer to overalocated size
64 * than to the old size.
65 */
66 if (newsize - Py_SIZE(self) > (Py_ssize_t)(new_allocated - newsize))
67 new_allocated = ((size_t)newsize + 3) & ~(size_t)3;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 if (newsize == 0)
70 new_allocated = 0;
Xiang Zhang4cee0492017-02-22 12:32:30 +080071 num_allocated_bytes = new_allocated * sizeof(PyObject *);
72 items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 if (items == NULL) {
74 PyErr_NoMemory();
75 return -1;
76 }
77 self->ob_item = items;
Victor Stinner60ac6ed2020-02-07 23:18:08 +010078 Py_SET_SIZE(self, newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 self->allocated = new_allocated;
80 return 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000081}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000082
Pablo Galindo372d7052018-10-28 20:16:26 +000083static int
84list_preallocate_exact(PyListObject *self, Py_ssize_t size)
85{
86 assert(self->ob_item == NULL);
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050087 assert(size > 0);
Pablo Galindo372d7052018-10-28 20:16:26 +000088
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050089 PyObject **items = PyMem_New(PyObject*, size);
Pablo Galindo372d7052018-10-28 20:16:26 +000090 if (items == NULL) {
91 PyErr_NoMemory();
92 return -1;
93 }
94 self->ob_item = items;
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050095 self->allocated = size;
Pablo Galindo372d7052018-10-28 20:16:26 +000096 return 0;
97}
98
Raymond Hettinger0468e412004-05-05 05:37:53 +000099/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000100#ifndef PyList_MAXFREELIST
Victor Stinnerb7aa23d2020-05-06 19:05:27 +0200101# define PyList_MAXFREELIST 80
Christian Heimes2202f872008-02-06 14:31:34 +0000102#endif
Victor Stinnerb7aa23d2020-05-06 19:05:27 +0200103
Christian Heimes2202f872008-02-06 14:31:34 +0000104static PyListObject *free_list[PyList_MAXFREELIST];
105static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000106
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200107void
108_PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 while (numfree) {
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200111 PyListObject *op = free_list[--numfree];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 assert(PyList_CheckExact(op));
113 PyObject_GC_Del(op);
114 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100115}
116
117void
Victor Stinnerbed48172019-08-27 00:12:32 +0200118_PyList_Fini(void)
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100119{
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200120 _PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000121}
122
David Malcolm49526f42012-06-22 14:55:41 -0400123/* Print summary info about the state of the optimized allocator */
124void
125_PyList_DebugMallocStats(FILE *out)
126{
127 _PyDebugAllocatorStats(out,
128 "free PyListObject",
129 numfree, sizeof(PyListObject));
130}
131
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000132PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000133PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 PyListObject *op;
Tim Peters3986d4e2004-07-29 02:28:42 +0000136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 if (size < 0) {
138 PyErr_BadInternalCall();
139 return NULL;
140 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 if (numfree) {
142 numfree--;
143 op = free_list[numfree];
144 _Py_NewReference((PyObject *)op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 } else {
146 op = PyObject_GC_New(PyListObject, &PyList_Type);
147 if (op == NULL)
148 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 }
150 if (size <= 0)
151 op->ob_item = NULL;
152 else {
Mark Dickinson5d132382016-08-21 08:55:15 +0100153 op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 if (op->ob_item == NULL) {
155 Py_DECREF(op);
156 return PyErr_NoMemory();
157 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100159 Py_SET_SIZE(op, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 op->allocated = size;
161 _PyObject_GC_TRACK(op);
162 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163}
164
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500165static PyObject *
166list_new_prealloc(Py_ssize_t size)
167{
168 PyListObject *op = (PyListObject *) PyList_New(0);
169 if (size == 0 || op == NULL) {
170 return (PyObject *) op;
171 }
172 assert(op->ob_item == NULL);
173 op->ob_item = PyMem_New(PyObject *, size);
174 if (op->ob_item == NULL) {
175 Py_DECREF(op);
176 return PyErr_NoMemory();
177 }
178 op->allocated = size;
179 return (PyObject *) op;
180}
181
Martin v. Löwis18e16552006-02-15 17:27:45 +0000182Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000183PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 if (!PyList_Check(op)) {
186 PyErr_BadInternalCall();
187 return -1;
188 }
189 else
190 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191}
192
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700193static inline int
194valid_index(Py_ssize_t i, Py_ssize_t limit)
195{
196 /* The cast to size_t lets us use just a single comparison
197 to check whether i is in the range: 0 <= i < limit.
198
199 See: Section 14.2 "Bounds Checking" in the Agner Fog
200 optimization manual found at:
201 https://www.agner.org/optimize/optimizing_cpp.pdf
202 */
203 return (size_t) i < (size_t) limit;
204}
205
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000206static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000207
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000209PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 if (!PyList_Check(op)) {
212 PyErr_BadInternalCall();
213 return NULL;
214 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700215 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (indexerr == NULL) {
217 indexerr = PyUnicode_FromString(
218 "list index out of range");
219 if (indexerr == NULL)
220 return NULL;
221 }
222 PyErr_SetObject(PyExc_IndexError, indexerr);
223 return NULL;
224 }
225 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226}
227
228int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200229PyList_SetItem(PyObject *op, Py_ssize_t i,
230 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200232 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 if (!PyList_Check(op)) {
234 Py_XDECREF(newitem);
235 PyErr_BadInternalCall();
236 return -1;
237 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700238 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 Py_XDECREF(newitem);
240 PyErr_SetString(PyExc_IndexError,
241 "list assignment index out of range");
242 return -1;
243 }
244 p = ((PyListObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300245 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247}
248
249static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000250ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 Py_ssize_t i, n = Py_SIZE(self);
253 PyObject **items;
254 if (v == NULL) {
255 PyErr_BadInternalCall();
256 return -1;
257 }
258 if (n == PY_SSIZE_T_MAX) {
259 PyErr_SetString(PyExc_OverflowError,
260 "cannot add more objects to list");
261 return -1;
262 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000263
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800264 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 if (where < 0) {
268 where += n;
269 if (where < 0)
270 where = 0;
271 }
272 if (where > n)
273 where = n;
274 items = self->ob_item;
275 for (i = n; --i >= where; )
276 items[i+1] = items[i];
277 Py_INCREF(v);
278 items[where] = v;
279 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280}
281
282int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000283PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (!PyList_Check(op)) {
286 PyErr_BadInternalCall();
287 return -1;
288 }
289 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290}
291
Raymond Hettinger40a03822004-04-12 13:05:09 +0000292static int
293app1(PyListObject *self, PyObject *v)
294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 assert (v != NULL);
298 if (n == PY_SSIZE_T_MAX) {
299 PyErr_SetString(PyExc_OverflowError,
300 "cannot add more objects to list");
301 return -1;
302 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000303
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800304 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 Py_INCREF(v);
308 PyList_SET_ITEM(self, n, v);
309 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000310}
311
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000312int
Fred Drakea2f55112000-07-09 15:16:51 +0000313PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 if (PyList_Check(op) && (newitem != NULL))
316 return app1((PyListObject *)op, newitem);
317 PyErr_BadInternalCall();
318 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319}
320
321/* Methods */
322
323static void
Fred Drakea2f55112000-07-09 15:16:51 +0000324list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 Py_ssize_t i;
327 PyObject_GC_UnTrack(op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200328 Py_TRASHCAN_BEGIN(op, list_dealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 if (op->ob_item != NULL) {
330 /* Do it backwards, for Christian Tismer.
331 There's a simple test case where somehow this reduces
332 thrashing when a *very* large list is created and
333 immediately deleted. */
334 i = Py_SIZE(op);
335 while (--i >= 0) {
336 Py_XDECREF(op->ob_item[i]);
337 }
338 PyMem_FREE(op->ob_item);
339 }
340 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
341 free_list[numfree++] = op;
342 else
343 Py_TYPE(op)->tp_free((PyObject *)op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200344 Py_TRASHCAN_END
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345}
346
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000347static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000348list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 Py_ssize_t i;
Victor Stinner5c733472013-11-18 21:11:57 +0100351 PyObject *s;
Victor Stinner5c733472013-11-18 21:11:57 +0100352 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200353
354 if (Py_SIZE(v) == 0) {
355 return PyUnicode_FromString("[]");
356 }
357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 i = Py_ReprEnter((PyObject*)v);
359 if (i != 0) {
360 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
361 }
Tim Petersa7259592001-06-16 05:11:17 +0000362
Victor Stinner5c733472013-11-18 21:11:57 +0100363 _PyUnicodeWriter_Init(&writer);
364 writer.overallocate = 1;
Victor Stinnerb8fb1972013-11-18 22:15:44 +0100365 /* "[" + "1" + ", 2" * (len - 1) + "]" */
366 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +0000367
Victor Stinner5c733472013-11-18 21:11:57 +0100368 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200369 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 /* Do repr() on each element. Note that this may mutate the list,
372 so must refetch the list size on each iteration. */
373 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100374 if (i > 0) {
Victor Stinner4a587072013-11-19 12:54:53 +0100375 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
Victor Stinner5c733472013-11-18 21:11:57 +0100376 goto error;
377 }
378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner5c733472013-11-18 21:11:57 +0100380 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200381 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100382
383 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
384 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200385 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100386 }
387 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 }
Victor Stinner5c733472013-11-18 21:11:57 +0100389
Victor Stinner4d3f1092013-11-19 12:09:00 +0100390 writer.overallocate = 0;
Victor Stinner5c733472013-11-18 21:11:57 +0100391 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200392 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100395 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200396
397error:
Victor Stinner5c733472013-11-18 21:11:57 +0100398 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200399 Py_ReprLeave((PyObject *)v);
400 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401}
402
Martin v. Löwis18e16552006-02-15 17:27:45 +0000403static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000404list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407}
408
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000409static int
Fred Drakea2f55112000-07-09 15:16:51 +0000410list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000411{
Dong-hee Na4dbf2d82020-01-28 00:02:23 +0900412 PyObject *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 Py_ssize_t i;
414 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000415
Dong-hee Na4dbf2d82020-01-28 00:02:23 +0900416 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) {
417 item = PyList_GET_ITEM(a, i);
418 Py_INCREF(item);
Dong-hee Na9e1ed512020-01-28 02:04:25 +0900419 cmp = PyObject_RichCompareBool(item, el, Py_EQ);
Dong-hee Na4dbf2d82020-01-28 00:02:23 +0900420 Py_DECREF(item);
421 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000423}
424
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000426list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000427{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700428 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (indexerr == NULL) {
430 indexerr = PyUnicode_FromString(
431 "list index out of range");
432 if (indexerr == NULL)
433 return NULL;
434 }
435 PyErr_SetObject(PyExc_IndexError, indexerr);
436 return NULL;
437 }
438 Py_INCREF(a->ob_item[i]);
439 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440}
441
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000443list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 PyListObject *np;
446 PyObject **src, **dest;
447 Py_ssize_t i, len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500449 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 if (np == NULL)
451 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 src = a->ob_item + ilow;
454 dest = np->ob_item;
455 for (i = 0; i < len; i++) {
456 PyObject *v = src[i];
457 Py_INCREF(v);
458 dest[i] = v;
459 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100460 Py_SET_SIZE(np, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000462}
463
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000465PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 if (!PyList_Check(a)) {
468 PyErr_BadInternalCall();
469 return NULL;
470 }
Sergey Fedoseevef1b88b2019-02-21 11:51:52 +0500471 if (ilow < 0) {
472 ilow = 0;
473 }
474 else if (ilow > Py_SIZE(a)) {
475 ilow = Py_SIZE(a);
476 }
477 if (ihigh < ilow) {
478 ihigh = ilow;
479 }
480 else if (ihigh > Py_SIZE(a)) {
481 ihigh = Py_SIZE(a);
482 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000484}
485
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000486static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000487list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 Py_ssize_t size;
490 Py_ssize_t i;
491 PyObject **src, **dest;
492 PyListObject *np;
493 if (!PyList_Check(bb)) {
494 PyErr_Format(PyExc_TypeError,
495 "can only concatenate list (not \"%.200s\") to list",
Victor Stinner58ac7002020-02-07 03:04:21 +0100496 Py_TYPE(bb)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return NULL;
498 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000499#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000500 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000502 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500503 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 if (np == NULL) {
505 return NULL;
506 }
507 src = a->ob_item;
508 dest = np->ob_item;
509 for (i = 0; i < Py_SIZE(a); i++) {
510 PyObject *v = src[i];
511 Py_INCREF(v);
512 dest[i] = v;
513 }
514 src = b->ob_item;
515 dest = np->ob_item + Py_SIZE(a);
516 for (i = 0; i < Py_SIZE(b); i++) {
517 PyObject *v = src[i];
518 Py_INCREF(v);
519 dest[i] = v;
520 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100521 Py_SET_SIZE(np, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000523#undef b
524}
525
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000527list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 Py_ssize_t i, j;
530 Py_ssize_t size;
531 PyListObject *np;
532 PyObject **p, **items;
533 PyObject *elem;
534 if (n < 0)
535 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100536 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100538 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 if (size == 0)
540 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500541 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (np == NULL)
543 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500546 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 elem = a->ob_item[0];
548 for (i = 0; i < n; i++) {
549 items[i] = elem;
550 Py_INCREF(elem);
551 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500553 else {
554 p = np->ob_item;
555 items = a->ob_item;
556 for (i = 0; i < n; i++) {
557 for (j = 0; j < Py_SIZE(a); j++) {
558 *p = items[j];
559 Py_INCREF(*p);
560 p++;
561 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 }
563 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100564 Py_SET_SIZE(np, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000566}
567
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200569_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 Py_ssize_t i;
572 PyObject **item = a->ob_item;
573 if (item != NULL) {
574 /* Because XDECREF can recursively invoke operations on
575 this list, we make it empty first. */
576 i = Py_SIZE(a);
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100577 Py_SET_SIZE(a, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 a->ob_item = NULL;
579 a->allocated = 0;
580 while (--i >= 0) {
581 Py_XDECREF(item[i]);
582 }
583 PyMem_FREE(item);
584 }
585 /* Never fails; the return value can be ignored.
586 Note that there is no guarantee that the list is actually empty
587 at this point, because XDECREF may have populated it again! */
588 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000589}
590
Tim Peters8fc4a912004-07-31 21:53:19 +0000591/* a[ilow:ihigh] = v if v != NULL.
592 * del a[ilow:ihigh] if v == NULL.
593 *
594 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
595 * guaranteed the call cannot fail.
596 */
Armin Rigo93677f02004-07-29 12:40:23 +0000597static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000598list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 /* Because [X]DECREF can recursively invoke list operations on
601 this list, we must postpone all [X]DECREF activity until
602 after the list is back in its canonical shape. Therefore
603 we must allocate an additional array, 'recycle', into which
604 we temporarily copy the items that are deleted from the
605 list. :-( */
606 PyObject *recycle_on_stack[8];
607 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
608 PyObject **item;
609 PyObject **vitem = NULL;
610 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
611 Py_ssize_t n; /* # of elements in replacement list */
612 Py_ssize_t norig; /* # of elements in list getting replaced */
613 Py_ssize_t d; /* Change in size */
614 Py_ssize_t k;
615 size_t s;
616 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000617#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 if (v == NULL)
619 n = 0;
620 else {
621 if (a == b) {
622 /* Special case "a[i:j] = a" -- copy b first */
623 v = list_slice(b, 0, Py_SIZE(b));
624 if (v == NULL)
625 return result;
626 result = list_ass_slice(a, ilow, ihigh, v);
627 Py_DECREF(v);
628 return result;
629 }
630 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
631 if(v_as_SF == NULL)
632 goto Error;
633 n = PySequence_Fast_GET_SIZE(v_as_SF);
634 vitem = PySequence_Fast_ITEMS(v_as_SF);
635 }
636 if (ilow < 0)
637 ilow = 0;
638 else if (ilow > Py_SIZE(a))
639 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 if (ihigh < ilow)
642 ihigh = ilow;
643 else if (ihigh > Py_SIZE(a))
644 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 norig = ihigh - ilow;
647 assert(norig >= 0);
648 d = n - norig;
649 if (Py_SIZE(a) + d == 0) {
650 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200651 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 }
653 item = a->ob_item;
654 /* recycle the items that we are about to remove */
655 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700656 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
657 if (s) {
658 if (s > sizeof(recycle_on_stack)) {
659 recycle = (PyObject **)PyMem_MALLOC(s);
660 if (recycle == NULL) {
661 PyErr_NoMemory();
662 goto Error;
663 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700665 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200669 Py_ssize_t tail;
670 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
671 memmove(&item[ihigh+d], &item[ihigh], tail);
672 if (list_resize(a, Py_SIZE(a) + d) < 0) {
673 memmove(&item[ihigh], &item[ihigh+d], tail);
674 memcpy(&item[ilow], recycle, s);
675 goto Error;
676 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 item = a->ob_item;
678 }
679 else if (d > 0) { /* Insert d items */
680 k = Py_SIZE(a);
681 if (list_resize(a, k+d) < 0)
682 goto Error;
683 item = a->ob_item;
684 memmove(&item[ihigh+d], &item[ihigh],
685 (k - ihigh)*sizeof(PyObject *));
686 }
687 for (k = 0; k < n; k++, ilow++) {
688 PyObject *w = vitem[k];
689 Py_XINCREF(w);
690 item[ilow] = w;
691 }
692 for (k = norig - 1; k >= 0; --k)
693 Py_XDECREF(recycle[k]);
694 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000695 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 if (recycle != recycle_on_stack)
697 PyMem_FREE(recycle);
698 Py_XDECREF(v_as_SF);
699 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000700#undef b
701}
702
Guido van Rossum234f9421993-06-17 12:35:49 +0000703int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000704PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if (!PyList_Check(a)) {
707 PyErr_BadInternalCall();
708 return -1;
709 }
710 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000711}
712
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000713static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000714list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 PyObject **items;
717 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000718
719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 size = PyList_GET_SIZE(self);
721 if (size == 0 || n == 1) {
722 Py_INCREF(self);
723 return (PyObject *)self;
724 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 if (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200727 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 Py_INCREF(self);
729 return (PyObject *)self;
730 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 if (size > PY_SSIZE_T_MAX / n) {
733 return PyErr_NoMemory();
734 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000735
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800736 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 p = size;
740 items = self->ob_item;
741 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
742 for (j = 0; j < size; j++) {
743 PyObject *o = items[j];
744 Py_INCREF(o);
745 items[p++] = o;
746 }
747 }
748 Py_INCREF(self);
749 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000750}
751
Guido van Rossum4a450d01991-04-03 19:05:18 +0000752static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000753list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000754{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700755 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 PyErr_SetString(PyExc_IndexError,
757 "list assignment index out of range");
758 return -1;
759 }
760 if (v == NULL)
761 return list_ass_slice(a, i, i+1, v);
762 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300763 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000765}
766
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200767/*[clinic input]
768list.insert
769
770 index: Py_ssize_t
771 object: object
772 /
773
774Insert object before index.
775[clinic start generated code]*/
776
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000777static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200778list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
779/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200781 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 Py_RETURN_NONE;
783 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000784}
785
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200786/*[clinic input]
787list.clear
788
789Remove all items from list.
790[clinic start generated code]*/
791
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000792static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200793list_clear_impl(PyListObject *self)
794/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000795{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200796 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000797 Py_RETURN_NONE;
798}
799
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200800/*[clinic input]
801list.copy
802
803Return a shallow copy of the list.
804[clinic start generated code]*/
805
Eli Benderskycbbaa962011-02-25 05:47:53 +0000806static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200807list_copy_impl(PyListObject *self)
808/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000809{
810 return list_slice(self, 0, Py_SIZE(self));
811}
812
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200813/*[clinic input]
814list.append
815
816 object: object
817 /
818
819Append object to the end of the list.
820[clinic start generated code]*/
821
Eli Benderskycbbaa962011-02-25 05:47:53 +0000822static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200823list_append(PyListObject *self, PyObject *object)
824/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000825{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200826 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 Py_RETURN_NONE;
828 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000829}
830
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200831/*[clinic input]
832list.extend
833
834 iterable: object
835 /
836
837Extend list by appending elements from the iterable.
838[clinic start generated code]*/
839
Barry Warsawdedf6d61998-10-09 16:37:25 +0000840static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200841list_extend(PyListObject *self, PyObject *iterable)
842/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 PyObject *it; /* iter(v) */
845 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200846 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 Py_ssize_t mn; /* m + n */
848 Py_ssize_t i;
849 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 /* Special cases:
852 1) lists and tuples which can use PySequence_Fast ops
853 2) extending self to self requires making a copy first
854 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200855 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
856 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200858 iterable = PySequence_Fast(iterable, "argument must be iterable");
859 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200861 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200863 /* short circuit when iterable is empty */
864 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 Py_RETURN_NONE;
866 }
867 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000868 /* It should not be possible to allocate a list large enough to cause
869 an overflow on any relevant platform */
870 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800871 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200872 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 return NULL;
874 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200875 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 * situation a.extend(a), but the following code works
877 * in that case too. Just make sure to resize self
878 * before calling PySequence_Fast_ITEMS.
879 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200880 /* populate the end of self with iterable's items */
881 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 dest = self->ob_item + m;
883 for (i = 0; i < n; i++) {
884 PyObject *o = src[i];
885 Py_INCREF(o);
886 dest[i] = o;
887 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200888 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 Py_RETURN_NONE;
890 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000891
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200892 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 if (it == NULL)
894 return NULL;
Victor Stinner58ac7002020-02-07 03:04:21 +0100895 iternext = *Py_TYPE(it)->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200898 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800899 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 Py_DECREF(it);
901 return NULL;
902 }
903 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000904 if (m > PY_SSIZE_T_MAX - n) {
905 /* m + n overflowed; on the chance that n lied, and there really
906 * is enough room, ignore it. If n was telling the truth, we'll
907 * eventually run out of memory during the loop.
908 */
909 }
910 else {
911 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800913 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 goto error;
915 /* Make the list sane again. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100916 Py_SET_SIZE(self, m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 /* Run iterator to exhaustion. */
920 for (;;) {
921 PyObject *item = iternext(it);
922 if (item == NULL) {
923 if (PyErr_Occurred()) {
924 if (PyErr_ExceptionMatches(PyExc_StopIteration))
925 PyErr_Clear();
926 else
927 goto error;
928 }
929 break;
930 }
931 if (Py_SIZE(self) < self->allocated) {
932 /* steals ref */
933 PyList_SET_ITEM(self, Py_SIZE(self), item);
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100934 Py_SET_SIZE(self, Py_SIZE(self) + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 }
936 else {
937 int status = app1(self, item);
938 Py_DECREF(item); /* append creates a new ref */
939 if (status < 0)
940 goto error;
941 }
942 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200945 if (Py_SIZE(self) < self->allocated) {
946 if (list_resize(self, Py_SIZE(self)) < 0)
947 goto error;
948 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 Py_DECREF(it);
951 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000952
953 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 Py_DECREF(it);
955 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000956}
957
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000958PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200959_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000960{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200961 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000962}
963
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000964static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000965list_inplace_concat(PyListObject *self, PyObject *other)
966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000968
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200969 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 if (result == NULL)
971 return result;
972 Py_DECREF(result);
973 Py_INCREF(self);
974 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000975}
976
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200977/*[clinic input]
978list.pop
979
980 index: Py_ssize_t = -1
981 /
982
983Remove and return item at index (default last).
984
985Raises IndexError if list is empty or index is out of range.
986[clinic start generated code]*/
987
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000988static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200989list_pop_impl(PyListObject *self, Py_ssize_t index)
990/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000991{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 PyObject *v;
993 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 if (Py_SIZE(self) == 0) {
996 /* Special-case most common failure cause */
997 PyErr_SetString(PyExc_IndexError, "pop from empty list");
998 return NULL;
999 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001000 if (index < 0)
1001 index += Py_SIZE(self);
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -07001002 if (!valid_index(index, Py_SIZE(self))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1004 return NULL;
1005 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001006 v = self->ob_item[index];
1007 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001009 if (status >= 0)
1010 return v; /* and v now owns the reference the list had */
1011 else
1012 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 }
1014 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001015 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001016 if (status < 0) {
1017 Py_DECREF(v);
1018 return NULL;
1019 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001021}
1022
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001023/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1024static void
1025reverse_slice(PyObject **lo, PyObject **hi)
1026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 --hi;
1030 while (lo < hi) {
1031 PyObject *t = *lo;
1032 *lo = *hi;
1033 *hi = t;
1034 ++lo;
1035 --hi;
1036 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001037}
1038
Tim Petersa64dc242002-08-01 02:13:36 +00001039/* Lots of code for an adaptive, stable, natural mergesort. There are many
1040 * pieces to this algorithm; read listsort.txt for overviews and details.
1041 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001042
Daniel Stutzbach98338222010-12-02 21:55:33 +00001043/* A sortslice contains a pointer to an array of keys and a pointer to
1044 * an array of corresponding values. In other words, keys[i]
1045 * corresponds with values[i]. If values == NULL, then the keys are
1046 * also the values.
1047 *
1048 * Several convenience routines are provided here, so that keys and
1049 * values are always moved in sync.
1050 */
1051
1052typedef struct {
1053 PyObject **keys;
1054 PyObject **values;
1055} sortslice;
1056
1057Py_LOCAL_INLINE(void)
1058sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1059{
1060 s1->keys[i] = s2->keys[j];
1061 if (s1->values != NULL)
1062 s1->values[i] = s2->values[j];
1063}
1064
1065Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001066sortslice_copy_incr(sortslice *dst, sortslice *src)
1067{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001068 *dst->keys++ = *src->keys++;
1069 if (dst->values != NULL)
1070 *dst->values++ = *src->values++;
1071}
1072
1073Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001074sortslice_copy_decr(sortslice *dst, sortslice *src)
1075{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001076 *dst->keys-- = *src->keys--;
1077 if (dst->values != NULL)
1078 *dst->values-- = *src->values--;
1079}
1080
1081
1082Py_LOCAL_INLINE(void)
1083sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001084 Py_ssize_t n)
1085{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001086 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1087 if (s1->values != NULL)
1088 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1089}
1090
1091Py_LOCAL_INLINE(void)
1092sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001093 Py_ssize_t n)
1094{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001095 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1096 if (s1->values != NULL)
1097 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1098}
1099
1100Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001101sortslice_advance(sortslice *slice, Py_ssize_t n)
1102{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001103 slice->keys += n;
1104 if (slice->values != NULL)
1105 slice->values += n;
1106}
1107
embg1e34da42018-01-28 20:03:23 -07001108/* Comparison function: ms->key_compare, which is set at run-time in
1109 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001110 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1111 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001112
embg1e34da42018-01-28 20:03:23 -07001113#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001114
1115/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001116 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1117 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1118*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001119#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001121
embg1e34da42018-01-28 20:03:23 -07001122/* The maximum number of entries in a MergeState's pending-runs stack.
1123 * This is enough to sort arrays of size up to about
1124 * 32 * phi ** MAX_MERGE_PENDING
1125 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1126 * with 2**64 elements.
1127 */
1128#define MAX_MERGE_PENDING 85
1129
1130/* When we get into galloping mode, we stay there until both runs win less
1131 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1132 */
1133#define MIN_GALLOP 7
1134
1135/* Avoid malloc for small temp arrays. */
1136#define MERGESTATE_TEMP_SIZE 256
1137
1138/* One MergeState exists on the stack per invocation of mergesort. It's just
1139 * a convenient way to pass state around among the helper functions.
1140 */
1141struct s_slice {
1142 sortslice base;
1143 Py_ssize_t len;
1144};
1145
1146typedef struct s_MergeState MergeState;
1147struct s_MergeState {
1148 /* This controls when we get *into* galloping mode. It's initialized
1149 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1150 * random data, and lower for highly structured data.
1151 */
1152 Py_ssize_t min_gallop;
1153
1154 /* 'a' is temp storage to help with merges. It contains room for
1155 * alloced entries.
1156 */
1157 sortslice a; /* may point to temparray below */
1158 Py_ssize_t alloced;
1159
1160 /* A stack of n pending runs yet to be merged. Run #i starts at
1161 * address base[i] and extends for len[i] elements. It's always
1162 * true (so long as the indices are in bounds) that
1163 *
1164 * pending[i].base + pending[i].len == pending[i+1].base
1165 *
1166 * so we could cut the storage for this, but it's a minor amount,
1167 * and keeping all the info explicit simplifies the code.
1168 */
1169 int n;
1170 struct s_slice pending[MAX_MERGE_PENDING];
1171
1172 /* 'a' points to this when possible, rather than muck with malloc. */
1173 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1174
1175 /* This is the function we will use to compare two keys,
1176 * even when none of our special cases apply and we have to use
1177 * safe_object_compare. */
1178 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1179
1180 /* This function is used by unsafe_object_compare to optimize comparisons
1181 * when we know our list is type-homogeneous but we can't assume anything else.
Victor Stinner58ac7002020-02-07 03:04:21 +01001182 * In the pre-sort check it is set equal to Py_TYPE(key)->tp_richcompare */
embg1e34da42018-01-28 20:03:23 -07001183 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1184
1185 /* This function is used by unsafe_tuple_compare to compare the first elements
1186 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1187 * we can assume more, and use one of the special-case compares. */
1188 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1189};
1190
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001191/* binarysort is the best method for sorting small arrays: it does
1192 few compares, but can do data movement quadratic in the number of
1193 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001194 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001195 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001196 On entry, must have lo <= start <= hi, and that [lo, start) is already
1197 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001198 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001199 Even in case of error, the output slice will be some permutation of
1200 the input (nothing is lost or duplicated).
1201*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001202static int
embg1e34da42018-01-28 20:03:23 -07001203binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001204{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001205 Py_ssize_t k;
1206 PyObject **l, **p, **r;
1207 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001208
Daniel Stutzbach98338222010-12-02 21:55:33 +00001209 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001211 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 ++start;
1213 for (; start < hi; ++start) {
1214 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001215 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 r = start;
1217 pivot = *r;
1218 /* Invariants:
1219 * pivot >= all in [lo, l).
1220 * pivot < all in [r, start).
1221 * The second is vacuously true at the start.
1222 */
1223 assert(l < r);
1224 do {
1225 p = l + ((r - l) >> 1);
1226 IFLT(pivot, *p)
1227 r = p;
1228 else
1229 l = p+1;
1230 } while (l < r);
1231 assert(l == r);
1232 /* The invariants still hold, so pivot >= all in [lo, l) and
1233 pivot < all in [l, start), so pivot belongs at l. Note
1234 that if there are elements equal to pivot, l points to the
1235 first slot after them -- that's why this sort is stable.
1236 Slide over to make room.
1237 Caution: using memmove is much slower under MSVC 5;
1238 we're not usually moving many slots. */
1239 for (p = start; p > l; --p)
1240 *p = *(p-1);
1241 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001242 if (lo.values != NULL) {
1243 Py_ssize_t offset = lo.values - lo.keys;
1244 p = start + offset;
1245 pivot = *p;
1246 l += offset;
1247 for (p = start + offset; p > l; --p)
1248 *p = *(p-1);
1249 *l = pivot;
1250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 }
1252 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001253
1254 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001256}
1257
Tim Petersa64dc242002-08-01 02:13:36 +00001258/*
1259Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1260is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001261
Tim Petersa64dc242002-08-01 02:13:36 +00001262 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001263
Tim Petersa64dc242002-08-01 02:13:36 +00001264or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001265
Tim Petersa64dc242002-08-01 02:13:36 +00001266 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001267
Tim Petersa64dc242002-08-01 02:13:36 +00001268Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1269For its intended use in a stable mergesort, the strictness of the defn of
1270"descending" is needed so that the caller can safely reverse a descending
1271sequence without violating stability (strict > ensures there are no equal
1272elements to get out of order).
1273
1274Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001275*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001276static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001277count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 Py_ssize_t k;
1280 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 assert(lo < hi);
1283 *descending = 0;
1284 ++lo;
1285 if (lo == hi)
1286 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 n = 2;
1289 IFLT(*lo, *(lo-1)) {
1290 *descending = 1;
1291 for (lo = lo+1; lo < hi; ++lo, ++n) {
1292 IFLT(*lo, *(lo-1))
1293 ;
1294 else
1295 break;
1296 }
1297 }
1298 else {
1299 for (lo = lo+1; lo < hi; ++lo, ++n) {
1300 IFLT(*lo, *(lo-1))
1301 break;
1302 }
1303 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001306fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001308}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001309
Tim Petersa64dc242002-08-01 02:13:36 +00001310/*
1311Locate the proper position of key in a sorted vector; if the vector contains
1312an element equal to key, return the position immediately to the left of
1313the leftmost equal element. [gallop_right() does the same except returns
1314the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001315
Tim Petersa64dc242002-08-01 02:13:36 +00001316"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001317
Tim Petersa64dc242002-08-01 02:13:36 +00001318"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1319hint is to the final result, the faster this runs.
1320
1321The return value is the int k in 0..n such that
1322
1323 a[k-1] < key <= a[k]
1324
1325pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1326key belongs at index k; or, IOW, the first k elements of a should precede
1327key, and the last n-k should follow key.
1328
1329Returns -1 on error. See listsort.txt for info on the method.
1330*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001331static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001332gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 Py_ssize_t ofs;
1335 Py_ssize_t lastofs;
1336 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 a += hint;
1341 lastofs = 0;
1342 ofs = 1;
1343 IFLT(*a, key) {
1344 /* a[hint] < key -- gallop right, until
1345 * a[hint + lastofs] < key <= a[hint + ofs]
1346 */
1347 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1348 while (ofs < maxofs) {
1349 IFLT(a[ofs], key) {
1350 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001351 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 }
1354 else /* key <= a[hint + ofs] */
1355 break;
1356 }
1357 if (ofs > maxofs)
1358 ofs = maxofs;
1359 /* Translate back to offsets relative to &a[0]. */
1360 lastofs += hint;
1361 ofs += hint;
1362 }
1363 else {
1364 /* key <= a[hint] -- gallop left, until
1365 * a[hint - ofs] < key <= a[hint - lastofs]
1366 */
1367 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1368 while (ofs < maxofs) {
1369 IFLT(*(a-ofs), key)
1370 break;
1371 /* key <= a[hint - ofs] */
1372 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001373 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 }
1376 if (ofs > maxofs)
1377 ofs = maxofs;
1378 /* Translate back to positive offsets relative to &a[0]. */
1379 k = lastofs;
1380 lastofs = hint - ofs;
1381 ofs = hint - k;
1382 }
1383 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1386 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1387 * right of lastofs but no farther right than ofs. Do a binary
1388 * search, with invariant a[lastofs-1] < key <= a[ofs].
1389 */
1390 ++lastofs;
1391 while (lastofs < ofs) {
1392 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 IFLT(a[m], key)
1395 lastofs = m+1; /* a[m] < key */
1396 else
1397 ofs = m; /* key <= a[m] */
1398 }
1399 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1400 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001401
1402fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001404}
1405
1406/*
1407Exactly like gallop_left(), except that if key already exists in a[0:n],
1408finds the position immediately to the right of the rightmost equal value.
1409
1410The return value is the int k in 0..n such that
1411
1412 a[k-1] <= key < a[k]
1413
1414or -1 if error.
1415
1416The code duplication is massive, but this is enough different given that
1417we're sticking to "<" comparisons that it's much harder to follow if
1418written as one routine with yet another "left or right?" flag.
1419*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001420static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001421gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 Py_ssize_t ofs;
1424 Py_ssize_t lastofs;
1425 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 a += hint;
1430 lastofs = 0;
1431 ofs = 1;
1432 IFLT(key, *a) {
1433 /* key < a[hint] -- gallop left, until
1434 * a[hint - ofs] <= key < a[hint - lastofs]
1435 */
1436 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1437 while (ofs < maxofs) {
1438 IFLT(key, *(a-ofs)) {
1439 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001440 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 }
1443 else /* a[hint - ofs] <= key */
1444 break;
1445 }
1446 if (ofs > maxofs)
1447 ofs = maxofs;
1448 /* Translate back to positive offsets relative to &a[0]. */
1449 k = lastofs;
1450 lastofs = hint - ofs;
1451 ofs = hint - k;
1452 }
1453 else {
1454 /* a[hint] <= key -- gallop right, until
1455 * a[hint + lastofs] <= key < a[hint + ofs]
1456 */
1457 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1458 while (ofs < maxofs) {
1459 IFLT(key, a[ofs])
1460 break;
1461 /* a[hint + ofs] <= key */
1462 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001463 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 }
1466 if (ofs > maxofs)
1467 ofs = maxofs;
1468 /* Translate back to offsets relative to &a[0]. */
1469 lastofs += hint;
1470 ofs += hint;
1471 }
1472 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1475 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1476 * right of lastofs but no farther right than ofs. Do a binary
1477 * search, with invariant a[lastofs-1] <= key < a[ofs].
1478 */
1479 ++lastofs;
1480 while (lastofs < ofs) {
1481 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 IFLT(key, a[m])
1484 ofs = m; /* key < a[m] */
1485 else
1486 lastofs = m+1; /* a[m] <= key */
1487 }
1488 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1489 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001490
1491fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001493}
1494
Tim Petersa64dc242002-08-01 02:13:36 +00001495/* Conceptually a MergeState's constructor. */
1496static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001497merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001500 if (has_keyfunc) {
1501 /* The temporary space for merging will need at most half the list
1502 * size rounded up. Use the minimum possible space so we can use the
1503 * rest of temparray for other things. In particular, if there is
1504 * enough extra space, listsort() will use it to store the keys.
1505 */
1506 ms->alloced = (list_size + 1) / 2;
1507
1508 /* ms->alloced describes how many keys will be stored at
1509 ms->temparray, but we also need to store the values. Hence,
1510 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1511 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1512 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1513 ms->a.values = &ms->temparray[ms->alloced];
1514 }
1515 else {
1516 ms->alloced = MERGESTATE_TEMP_SIZE;
1517 ms->a.values = NULL;
1518 }
1519 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 ms->n = 0;
1521 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001522}
1523
1524/* Free all the temp memory owned by the MergeState. This must be called
1525 * when you're done with a MergeState, and may be called before then if
1526 * you want to free the temp memory early.
1527 */
1528static void
1529merge_freemem(MergeState *ms)
1530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001532 if (ms->a.keys != ms->temparray)
1533 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001534}
1535
1536/* Ensure enough temp memory for 'need' array slots is available.
1537 * Returns 0 on success and -1 if the memory can't be gotten.
1538 */
1539static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001540merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001541{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001542 int multiplier;
1543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 assert(ms != NULL);
1545 if (need <= ms->alloced)
1546 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001547
1548 multiplier = ms->a.values != NULL ? 2 : 1;
1549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 /* Don't realloc! That can cost cycles to copy the old data, but
1551 * we don't care what's in the block.
1552 */
1553 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001554 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 PyErr_NoMemory();
1556 return -1;
1557 }
embg1e34da42018-01-28 20:03:23 -07001558 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001559 * sizeof(PyObject *));
1560 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001562 if (ms->a.values != NULL)
1563 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 return 0;
1565 }
1566 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001568}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1570 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001571
Daniel Stutzbach98338222010-12-02 21:55:33 +00001572/* Merge the na elements starting at ssa with the nb elements starting at
1573 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1574 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1575 * should have na <= nb. See listsort.txt for more info. Return 0 if
1576 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001577 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001578static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001579merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1580 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001583 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 int result = -1; /* guilty until proved innocent */
1585 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001586
Daniel Stutzbach98338222010-12-02 21:55:33 +00001587 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1588 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 if (MERGE_GETMEM(ms, na) < 0)
1590 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001591 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1592 dest = ssa;
1593 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001594
Daniel Stutzbach98338222010-12-02 21:55:33 +00001595 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 --nb;
1597 if (nb == 0)
1598 goto Succeed;
1599 if (na == 1)
1600 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 min_gallop = ms->min_gallop;
1603 for (;;) {
1604 Py_ssize_t acount = 0; /* # of times A won in a row */
1605 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 /* Do the straightforward thing until (if ever) one run
1608 * appears to win consistently.
1609 */
1610 for (;;) {
1611 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001612 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 if (k) {
1614 if (k < 0)
1615 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001616 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 ++bcount;
1618 acount = 0;
1619 --nb;
1620 if (nb == 0)
1621 goto Succeed;
1622 if (bcount >= min_gallop)
1623 break;
1624 }
1625 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001626 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 ++acount;
1628 bcount = 0;
1629 --na;
1630 if (na == 1)
1631 goto CopyB;
1632 if (acount >= min_gallop)
1633 break;
1634 }
1635 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 /* One run is winning so consistently that galloping may
1638 * be a huge win. So try that, and continue galloping until
1639 * (if ever) neither run appears to be winning consistently
1640 * anymore.
1641 */
1642 ++min_gallop;
1643 do {
1644 assert(na > 1 && nb > 0);
1645 min_gallop -= min_gallop > 1;
1646 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001647 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 acount = k;
1649 if (k) {
1650 if (k < 0)
1651 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001652 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1653 sortslice_advance(&dest, k);
1654 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 na -= k;
1656 if (na == 1)
1657 goto CopyB;
1658 /* na==0 is impossible now if the comparison
1659 * function is consistent, but we can't assume
1660 * that it is.
1661 */
1662 if (na == 0)
1663 goto Succeed;
1664 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001665 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 --nb;
1667 if (nb == 0)
1668 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001669
embg1e34da42018-01-28 20:03:23 -07001670 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 bcount = k;
1672 if (k) {
1673 if (k < 0)
1674 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001675 sortslice_memmove(&dest, 0, &ssb, 0, k);
1676 sortslice_advance(&dest, k);
1677 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 nb -= k;
1679 if (nb == 0)
1680 goto Succeed;
1681 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001682 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 --na;
1684 if (na == 1)
1685 goto CopyB;
1686 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1687 ++min_gallop; /* penalize it for leaving galloping mode */
1688 ms->min_gallop = min_gallop;
1689 }
Tim Petersa64dc242002-08-01 02:13:36 +00001690Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001692Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001694 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001696CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001698 /* The last element of ssa belongs at the end of the merge. */
1699 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1700 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001702}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001703
Daniel Stutzbach98338222010-12-02 21:55:33 +00001704/* Merge the na elements starting at pa with the nb elements starting at
1705 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1706 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1707 * should have na >= nb. See listsort.txt for more info. Return 0 if
1708 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001709 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001710static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001711merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1712 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001715 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001718
Daniel Stutzbach98338222010-12-02 21:55:33 +00001719 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1720 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 if (MERGE_GETMEM(ms, nb) < 0)
1722 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001723 dest = ssb;
1724 sortslice_advance(&dest, nb-1);
1725 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1726 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001728 ssb.keys = ms->a.keys + nb - 1;
1729 if (ssb.values != NULL)
1730 ssb.values = ms->a.values + nb - 1;
1731 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001732
Daniel Stutzbach98338222010-12-02 21:55:33 +00001733 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 --na;
1735 if (na == 0)
1736 goto Succeed;
1737 if (nb == 1)
1738 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 min_gallop = ms->min_gallop;
1741 for (;;) {
1742 Py_ssize_t acount = 0; /* # of times A won in a row */
1743 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 /* Do the straightforward thing until (if ever) one run
1746 * appears to win consistently.
1747 */
1748 for (;;) {
1749 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001750 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 if (k) {
1752 if (k < 0)
1753 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001754 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 ++acount;
1756 bcount = 0;
1757 --na;
1758 if (na == 0)
1759 goto Succeed;
1760 if (acount >= min_gallop)
1761 break;
1762 }
1763 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001764 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 ++bcount;
1766 acount = 0;
1767 --nb;
1768 if (nb == 1)
1769 goto CopyA;
1770 if (bcount >= min_gallop)
1771 break;
1772 }
1773 }
Tim Petersa64dc242002-08-01 02:13:36 +00001774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 /* One run is winning so consistently that galloping may
1776 * be a huge win. So try that, and continue galloping until
1777 * (if ever) neither run appears to be winning consistently
1778 * anymore.
1779 */
1780 ++min_gallop;
1781 do {
1782 assert(na > 0 && nb > 1);
1783 min_gallop -= min_gallop > 1;
1784 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001785 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 if (k < 0)
1787 goto Fail;
1788 k = na - k;
1789 acount = k;
1790 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001791 sortslice_advance(&dest, -k);
1792 sortslice_advance(&ssa, -k);
1793 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 na -= k;
1795 if (na == 0)
1796 goto Succeed;
1797 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001798 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 --nb;
1800 if (nb == 1)
1801 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001802
embg1e34da42018-01-28 20:03:23 -07001803 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 if (k < 0)
1805 goto Fail;
1806 k = nb - k;
1807 bcount = k;
1808 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001809 sortslice_advance(&dest, -k);
1810 sortslice_advance(&ssb, -k);
1811 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 nb -= k;
1813 if (nb == 1)
1814 goto CopyA;
1815 /* nb==0 is impossible now if the comparison
1816 * function is consistent, but we can't assume
1817 * that it is.
1818 */
1819 if (nb == 0)
1820 goto Succeed;
1821 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001822 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 --na;
1824 if (na == 0)
1825 goto Succeed;
1826 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1827 ++min_gallop; /* penalize it for leaving galloping mode */
1828 ms->min_gallop = min_gallop;
1829 }
Tim Petersa64dc242002-08-01 02:13:36 +00001830Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001832Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001834 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001836CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001838 /* The first element of ssb belongs at the front of the merge. */
1839 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1840 sortslice_advance(&dest, -na);
1841 sortslice_advance(&ssa, -na);
1842 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001844}
1845
1846/* Merge the two runs at stack indices i and i+1.
1847 * Returns 0 on success, -1 on error.
1848 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001849static Py_ssize_t
1850merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001851{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001852 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 Py_ssize_t na, nb;
1854 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 assert(ms != NULL);
1857 assert(ms->n >= 2);
1858 assert(i >= 0);
1859 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001860
Daniel Stutzbach98338222010-12-02 21:55:33 +00001861 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001863 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 nb = ms->pending[i+1].len;
1865 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001866 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 /* Record the length of the combined runs; if i is the 3rd-last
1869 * run now, also slide over the last run (which isn't involved
1870 * in this merge). The current run i+1 goes away in any case.
1871 */
1872 ms->pending[i].len = na + nb;
1873 if (i == ms->n - 3)
1874 ms->pending[i+1] = ms->pending[i+2];
1875 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 /* Where does b start in a? Elements in a before that can be
1878 * ignored (already in place).
1879 */
embg1e34da42018-01-28 20:03:23 -07001880 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 if (k < 0)
1882 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001883 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 na -= k;
1885 if (na == 0)
1886 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 /* Where does a end in b? Elements in b after that can be
1889 * ignored (already in place).
1890 */
embg1e34da42018-01-28 20:03:23 -07001891 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 if (nb <= 0)
1893 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 /* Merge what remains of the runs, using a temp array with
1896 * min(na, nb) elements.
1897 */
1898 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001899 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001901 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001902}
1903
1904/* Examine the stack of runs waiting to be merged, merging adjacent runs
1905 * until the stack invariants are re-established:
1906 *
1907 * 1. len[-3] > len[-2] + len[-1]
1908 * 2. len[-2] > len[-1]
1909 *
1910 * See listsort.txt for more info.
1911 *
1912 * Returns 0 on success, -1 on error.
1913 */
1914static int
1915merge_collapse(MergeState *ms)
1916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 assert(ms);
1920 while (ms->n > 1) {
1921 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001922 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1923 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 if (p[n-1].len < p[n+1].len)
1925 --n;
1926 if (merge_at(ms, n) < 0)
1927 return -1;
1928 }
1929 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001930 if (merge_at(ms, n) < 0)
1931 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 }
1933 else
1934 break;
1935 }
1936 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001937}
1938
1939/* Regardless of invariants, merge all runs on the stack until only one
1940 * remains. This is used at the end of the mergesort.
1941 *
1942 * Returns 0 on success, -1 on error.
1943 */
1944static int
1945merge_force_collapse(MergeState *ms)
1946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 assert(ms);
1950 while (ms->n > 1) {
1951 Py_ssize_t n = ms->n - 2;
1952 if (n > 0 && p[n-1].len < p[n+1].len)
1953 --n;
1954 if (merge_at(ms, n) < 0)
1955 return -1;
1956 }
1957 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001958}
1959
1960/* Compute a good value for the minimum run length; natural runs shorter
1961 * than this are boosted artificially via binary insertion.
1962 *
1963 * If n < 64, return n (it's too small to bother with fancy stuff).
1964 * Else if n is an exact power of 2, return 32.
1965 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1966 * strictly less than, an exact power of 2.
1967 *
1968 * See listsort.txt for more info.
1969 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001970static Py_ssize_t
1971merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 assert(n >= 0);
1976 while (n >= 64) {
1977 r |= n & 1;
1978 n >>= 1;
1979 }
1980 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001981}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001982
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001983static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001984reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001985{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001986 reverse_slice(s->keys, &s->keys[n]);
1987 if (s->values != NULL)
1988 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001989}
1990
embg1e34da42018-01-28 20:03:23 -07001991/* Here we define custom comparison functions to optimize for the cases one commonly
1992 * encounters in practice: homogeneous lists, often of one of the basic types. */
1993
1994/* This struct holds the comparison function and helper functions
1995 * selected in the pre-sort check. */
1996
1997/* These are the special case compare functions.
1998 * ms->key_compare will always point to one of these: */
1999
2000/* Heterogeneous compare: default, always safe to fall back on. */
2001static int
2002safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2003{
2004 /* No assumptions necessary! */
2005 return PyObject_RichCompareBool(v, w, Py_LT);
2006}
2007
2008/* Homogeneous compare: safe for any two compareable objects of the same type.
2009 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2010 * pre-sort check.)
2011 */
2012static int
2013unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2014{
2015 PyObject *res_obj; int res;
2016
2017 /* No assumptions, because we check first: */
Victor Stinner58ac7002020-02-07 03:04:21 +01002018 if (Py_TYPE(v)->tp_richcompare != ms->key_richcompare)
embg1e34da42018-01-28 20:03:23 -07002019 return PyObject_RichCompareBool(v, w, Py_LT);
2020
2021 assert(ms->key_richcompare != NULL);
2022 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2023
2024 if (res_obj == Py_NotImplemented) {
2025 Py_DECREF(res_obj);
2026 return PyObject_RichCompareBool(v, w, Py_LT);
2027 }
2028 if (res_obj == NULL)
2029 return -1;
2030
2031 if (PyBool_Check(res_obj)) {
2032 res = (res_obj == Py_True);
2033 }
2034 else {
2035 res = PyObject_IsTrue(res_obj);
2036 }
2037 Py_DECREF(res_obj);
2038
2039 /* Note that we can't assert
2040 * res == PyObject_RichCompareBool(v, w, Py_LT);
2041 * because of evil compare functions like this:
2042 * lambda a, b: int(random.random() * 3) - 1)
2043 * (which is actually in test_sort.py) */
2044 return res;
2045}
2046
2047/* Latin string compare: safe for any two latin (one byte per char) strings. */
2048static int
2049unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2050{
Victor Stinner8017b802018-01-29 13:47:06 +01002051 Py_ssize_t len;
2052 int res;
embg1e34da42018-01-28 20:03:23 -07002053
2054 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
Andy Lesterdffe4c02020-03-04 07:15:20 -06002055 assert(Py_IS_TYPE(v, &PyUnicode_Type));
2056 assert(Py_IS_TYPE(w, &PyUnicode_Type));
embg1e34da42018-01-28 20:03:23 -07002057 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2058 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2059
2060 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2061 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2062
2063 res = (res != 0 ?
2064 res < 0 :
2065 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2066
2067 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2068 return res;
2069}
2070
2071/* Bounded int compare: compare any two longs that fit in a single machine word. */
2072static int
2073unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2074{
2075 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2076
2077 /* Modified from Objects/longobject.c:long_compare, assuming: */
Andy Lesterdffe4c02020-03-04 07:15:20 -06002078 assert(Py_IS_TYPE(v, &PyLong_Type));
2079 assert(Py_IS_TYPE(w, &PyLong_Type));
embg1e34da42018-01-28 20:03:23 -07002080 assert(Py_ABS(Py_SIZE(v)) <= 1);
2081 assert(Py_ABS(Py_SIZE(w)) <= 1);
2082
2083 vl = (PyLongObject*)v;
2084 wl = (PyLongObject*)w;
2085
2086 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2087 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2088
2089 if (Py_SIZE(vl) < 0)
2090 v0 = -v0;
2091 if (Py_SIZE(wl) < 0)
2092 w0 = -w0;
2093
2094 res = v0 < w0;
2095 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2096 return res;
2097}
2098
2099/* Float compare: compare any two floats. */
2100static int
2101unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2102{
2103 int res;
2104
2105 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
Andy Lesterdffe4c02020-03-04 07:15:20 -06002106 assert(Py_IS_TYPE(v, &PyFloat_Type));
2107 assert(Py_IS_TYPE(w, &PyFloat_Type));
embg1e34da42018-01-28 20:03:23 -07002108
2109 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2110 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2111 return res;
2112}
2113
2114/* Tuple compare: compare *any* two tuples, using
2115 * ms->tuple_elem_compare to compare the first elements, which is set
2116 * using the same pre-sort check as we use for ms->key_compare,
2117 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2118 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2119 * that most tuple compares don't involve x[1:]. */
2120static int
2121unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2122{
2123 PyTupleObject *vt, *wt;
2124 Py_ssize_t i, vlen, wlen;
2125 int k;
2126
2127 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
Andy Lesterdffe4c02020-03-04 07:15:20 -06002128 assert(Py_IS_TYPE(v, &PyTuple_Type));
2129 assert(Py_IS_TYPE(w, &PyTuple_Type));
embg1e34da42018-01-28 20:03:23 -07002130 assert(Py_SIZE(v) > 0);
2131 assert(Py_SIZE(w) > 0);
2132
2133 vt = (PyTupleObject *)v;
2134 wt = (PyTupleObject *)w;
2135
2136 vlen = Py_SIZE(vt);
2137 wlen = Py_SIZE(wt);
2138
2139 for (i = 0; i < vlen && i < wlen; i++) {
2140 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2141 if (k < 0)
2142 return -1;
2143 if (!k)
2144 break;
2145 }
2146
2147 if (i >= vlen || i >= wlen)
2148 return vlen < wlen;
2149
2150 if (i == 0)
2151 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2152 else
2153 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2154}
2155
Tim Petersa64dc242002-08-01 02:13:36 +00002156/* An adaptive, stable, natural mergesort. See listsort.txt.
2157 * Returns Py_None on success, NULL on error. Even in case of error, the
2158 * list will be some permutation of its input state (nothing is lost or
2159 * duplicated).
2160 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002161/*[clinic input]
2162list.sort
2163
2164 *
2165 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002166 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002167
Tim Hoffmann5c224762019-06-01 06:10:02 +02002168Sort the list in ascending order and return None.
2169
2170The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
2171order of two equal elements is maintained).
2172
2173If a key function is given, apply it once to each list item and sort them,
2174ascending or descending, according to their function values.
2175
2176The reverse flag can be set to sort in descending order.
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002177[clinic start generated code]*/
2178
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002179static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002180list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Tim Hoffmann5c224762019-06-01 06:10:02 +02002181/*[clinic end generated code: output=57b9f9c5e23fbe42 input=cb56cd179a713060]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 Py_ssize_t nremaining;
2185 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002186 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 Py_ssize_t saved_ob_size, saved_allocated;
2188 PyObject **saved_ob_item;
2189 PyObject **final_ob_item;
2190 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002192 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002195 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 if (keyfunc == Py_None)
2197 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 /* The list is temporarily made empty, so that mutations performed
2200 * by comparison functions can't affect the slice of memory we're
2201 * sorting (allowing mutations during sorting is a core-dump
2202 * factory, since ob_item may change).
2203 */
2204 saved_ob_size = Py_SIZE(self);
2205 saved_ob_item = self->ob_item;
2206 saved_allocated = self->allocated;
Victor Stinner60ac6ed2020-02-07 23:18:08 +01002207 Py_SET_SIZE(self, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 self->ob_item = NULL;
2209 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002210
Daniel Stutzbach98338222010-12-02 21:55:33 +00002211 if (keyfunc == NULL) {
2212 keys = NULL;
2213 lo.keys = saved_ob_item;
2214 lo.values = NULL;
2215 }
2216 else {
2217 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2218 /* Leverage stack space we allocated but won't otherwise use */
2219 keys = &ms.temparray[saved_ob_size+1];
2220 else {
2221 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002222 if (keys == NULL) {
2223 PyErr_NoMemory();
2224 goto keyfunc_fail;
2225 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002227
2228 for (i = 0; i < saved_ob_size ; i++) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002229 keys[i] = PyObject_CallOneArg(keyfunc, saved_ob_item[i]);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002230 if (keys[i] == NULL) {
2231 for (i=i-1 ; i>=0 ; i--)
2232 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002233 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002234 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002235 goto keyfunc_fail;
2236 }
2237 }
2238
2239 lo.keys = keys;
2240 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002242
embg1e34da42018-01-28 20:03:23 -07002243
2244 /* The pre-sort check: here's where we decide which compare function to use.
2245 * How much optimization is safe? We test for homogeneity with respect to
2246 * several properties that are expensive to check at compare-time, and
2247 * set ms appropriately. */
2248 if (saved_ob_size > 1) {
2249 /* Assume the first element is representative of the whole list. */
Andy Lesterdffe4c02020-03-04 07:15:20 -06002250 int keys_are_in_tuples = (Py_IS_TYPE(lo.keys[0], &PyTuple_Type) &&
embg1e34da42018-01-28 20:03:23 -07002251 Py_SIZE(lo.keys[0]) > 0);
2252
2253 PyTypeObject* key_type = (keys_are_in_tuples ?
Victor Stinner58ac7002020-02-07 03:04:21 +01002254 Py_TYPE(PyTuple_GET_ITEM(lo.keys[0], 0)) :
2255 Py_TYPE(lo.keys[0]));
embg1e34da42018-01-28 20:03:23 -07002256
2257 int keys_are_all_same_type = 1;
2258 int strings_are_latin = 1;
2259 int ints_are_bounded = 1;
2260
2261 /* Prove that assumption by checking every key. */
embg1e34da42018-01-28 20:03:23 -07002262 for (i=0; i < saved_ob_size; i++) {
2263
2264 if (keys_are_in_tuples &&
Andy Lesterdffe4c02020-03-04 07:15:20 -06002265 !(Py_IS_TYPE(lo.keys[i], &PyTuple_Type) && Py_SIZE(lo.keys[i]) != 0)) {
embg1e34da42018-01-28 20:03:23 -07002266 keys_are_in_tuples = 0;
2267 keys_are_all_same_type = 0;
2268 break;
2269 }
2270
2271 /* Note: for lists of tuples, key is the first element of the tuple
2272 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2273 * for lists of tuples in the if-statement directly above. */
2274 PyObject *key = (keys_are_in_tuples ?
2275 PyTuple_GET_ITEM(lo.keys[i], 0) :
2276 lo.keys[i]);
2277
Andy Lesterdffe4c02020-03-04 07:15:20 -06002278 if (!Py_IS_TYPE(key, key_type)) {
embg1e34da42018-01-28 20:03:23 -07002279 keys_are_all_same_type = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002280 /* If keys are in tuple we must loop over the whole list to make
2281 sure all items are tuples */
2282 if (!keys_are_in_tuples) {
2283 break;
2284 }
embg1e34da42018-01-28 20:03:23 -07002285 }
2286
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002287 if (keys_are_all_same_type) {
2288 if (key_type == &PyLong_Type &&
2289 ints_are_bounded &&
2290 Py_ABS(Py_SIZE(key)) > 1) {
2291
embg1e34da42018-01-28 20:03:23 -07002292 ints_are_bounded = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002293 }
2294 else if (key_type == &PyUnicode_Type &&
2295 strings_are_latin &&
2296 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {
2297
2298 strings_are_latin = 0;
2299 }
2300 }
embg1e34da42018-01-28 20:03:23 -07002301 }
embg1e34da42018-01-28 20:03:23 -07002302
2303 /* Choose the best compare, given what we now know about the keys. */
2304 if (keys_are_all_same_type) {
2305
2306 if (key_type == &PyUnicode_Type && strings_are_latin) {
2307 ms.key_compare = unsafe_latin_compare;
2308 }
2309 else if (key_type == &PyLong_Type && ints_are_bounded) {
2310 ms.key_compare = unsafe_long_compare;
2311 }
2312 else if (key_type == &PyFloat_Type) {
2313 ms.key_compare = unsafe_float_compare;
2314 }
2315 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2316 ms.key_compare = unsafe_object_compare;
2317 }
Zackery Spytzebc793d2019-02-21 00:47:14 -07002318 else {
2319 ms.key_compare = safe_object_compare;
2320 }
embg1e34da42018-01-28 20:03:23 -07002321 }
2322 else {
2323 ms.key_compare = safe_object_compare;
2324 }
2325
2326 if (keys_are_in_tuples) {
2327 /* Make sure we're not dealing with tuples of tuples
2328 * (remember: here, key_type refers list [key[0] for key in keys]) */
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002329 if (key_type == &PyTuple_Type) {
embg1e34da42018-01-28 20:03:23 -07002330 ms.tuple_elem_compare = safe_object_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002331 }
2332 else {
embg1e34da42018-01-28 20:03:23 -07002333 ms.tuple_elem_compare = ms.key_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002334 }
embg1e34da42018-01-28 20:03:23 -07002335
2336 ms.key_compare = unsafe_tuple_compare;
2337 }
2338 }
2339 /* End of pre-sort check: ms is now set properly! */
2340
Daniel Stutzbach98338222010-12-02 21:55:33 +00002341 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 nremaining = saved_ob_size;
2344 if (nremaining < 2)
2345 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002346
Benjamin Peterson05380642010-08-23 19:35:39 +00002347 /* Reverse sort stability achieved by initially reversing the list,
2348 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002349 if (reverse) {
2350 if (keys != NULL)
2351 reverse_slice(&keys[0], &keys[saved_ob_size]);
2352 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2353 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 /* March over the array once, left to right, finding natural runs,
2356 * and extending short natural runs to minrun elements.
2357 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 minrun = merge_compute_minrun(nremaining);
2359 do {
2360 int descending;
2361 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002364 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 if (n < 0)
2366 goto fail;
2367 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002368 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 /* If short, extend to min(minrun, nremaining). */
2370 if (n < minrun) {
2371 const Py_ssize_t force = nremaining <= minrun ?
2372 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002373 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 goto fail;
2375 n = force;
2376 }
2377 /* Push run onto pending-runs stack, and maybe merge. */
2378 assert(ms.n < MAX_MERGE_PENDING);
2379 ms.pending[ms.n].base = lo;
2380 ms.pending[ms.n].len = n;
2381 ++ms.n;
2382 if (merge_collapse(&ms) < 0)
2383 goto fail;
2384 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002385 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 nremaining -= n;
2387 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 if (merge_force_collapse(&ms) < 0)
2390 goto fail;
2391 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002392 assert(keys == NULL
2393 ? ms.pending[0].base.keys == saved_ob_item
2394 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002396 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002397
2398succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002400fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002401 if (keys != NULL) {
2402 for (i = 0; i < saved_ob_size; i++)
2403 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002404 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002405 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 if (self->allocated != -1 && result != NULL) {
2409 /* The user mucked with the list during the sort,
2410 * and we don't already have another error to report.
2411 */
2412 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2413 result = NULL;
2414 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 if (reverse && saved_ob_size > 1)
2417 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002420
Daniel Stutzbach98338222010-12-02 21:55:33 +00002421keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 final_ob_item = self->ob_item;
2423 i = Py_SIZE(self);
Victor Stinner60ac6ed2020-02-07 23:18:08 +01002424 Py_SET_SIZE(self, saved_ob_size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 self->ob_item = saved_ob_item;
2426 self->allocated = saved_allocated;
2427 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002428 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 guarantee that the list is really empty when it returns */
2430 while (--i >= 0) {
2431 Py_XDECREF(final_ob_item[i]);
2432 }
2433 PyMem_FREE(final_ob_item);
2434 }
2435 Py_XINCREF(result);
2436 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002437}
Tim Peters330f9e92002-07-19 07:05:44 +00002438#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002439#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002440
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002441int
Fred Drakea2f55112000-07-09 15:16:51 +00002442PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 if (v == NULL || !PyList_Check(v)) {
2445 PyErr_BadInternalCall();
2446 return -1;
2447 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002448 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 if (v == NULL)
2450 return -1;
2451 Py_DECREF(v);
2452 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002453}
2454
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002455/*[clinic input]
2456list.reverse
2457
2458Reverse *IN PLACE*.
2459[clinic start generated code]*/
2460
Guido van Rossumb86c5492001-02-12 22:06:02 +00002461static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002462list_reverse_impl(PyListObject *self)
2463/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 if (Py_SIZE(self) > 1)
2466 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2467 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002468}
2469
Guido van Rossum84c76f51990-10-30 13:32:20 +00002470int
Fred Drakea2f55112000-07-09 15:16:51 +00002471PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 if (v == NULL || !PyList_Check(v)) {
2476 PyErr_BadInternalCall();
2477 return -1;
2478 }
2479 if (Py_SIZE(self) > 1)
2480 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2481 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002482}
2483
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002484PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002485PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 if (v == NULL || !PyList_Check(v)) {
2488 PyErr_BadInternalCall();
2489 return NULL;
2490 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +05002491 return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002492}
2493
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002494/*[clinic input]
2495list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002496
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002497 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002498 start: slice_index(accept={int}) = 0
2499 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002500 /
2501
2502Return first index of value.
2503
2504Raises ValueError if the value is not present.
2505[clinic start generated code]*/
2506
2507static PyObject *
2508list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2509 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002510/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002511{
2512 Py_ssize_t i;
2513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 if (start < 0) {
2515 start += Py_SIZE(self);
2516 if (start < 0)
2517 start = 0;
2518 }
2519 if (stop < 0) {
2520 stop += Py_SIZE(self);
2521 if (stop < 0)
2522 stop = 0;
2523 }
2524 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Zackery Spytzd9e561d2019-12-30 12:32:58 -07002525 PyObject *obj = self->ob_item[i];
2526 Py_INCREF(obj);
2527 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2528 Py_DECREF(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 if (cmp > 0)
2530 return PyLong_FromSsize_t(i);
2531 else if (cmp < 0)
2532 return NULL;
2533 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002534 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002536}
2537
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002538/*[clinic input]
2539list.count
2540
2541 value: object
2542 /
2543
2544Return number of occurrences of value.
2545[clinic start generated code]*/
2546
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002547static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002548list_count(PyListObject *self, PyObject *value)
2549/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 Py_ssize_t count = 0;
2552 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 for (i = 0; i < Py_SIZE(self); i++) {
Zackery Spytzd9e561d2019-12-30 12:32:58 -07002555 PyObject *obj = self->ob_item[i];
Dong-hee Na14d80d02020-01-23 02:36:54 +09002556 if (obj == value) {
2557 count++;
2558 continue;
2559 }
Zackery Spytzd9e561d2019-12-30 12:32:58 -07002560 Py_INCREF(obj);
2561 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2562 Py_DECREF(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 if (cmp > 0)
2564 count++;
2565 else if (cmp < 0)
2566 return NULL;
2567 }
2568 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002569}
2570
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002571/*[clinic input]
2572list.remove
2573
2574 value: object
2575 /
2576
2577Remove first occurrence of value.
2578
2579Raises ValueError if the value is not present.
2580[clinic start generated code]*/
2581
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002582static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002583list_remove(PyListObject *self, PyObject *value)
2584/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 for (i = 0; i < Py_SIZE(self); i++) {
Zackery Spytzd9e561d2019-12-30 12:32:58 -07002589 PyObject *obj = self->ob_item[i];
2590 Py_INCREF(obj);
2591 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2592 Py_DECREF(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 if (cmp > 0) {
2594 if (list_ass_slice(self, i, i+1,
2595 (PyObject *)NULL) == 0)
2596 Py_RETURN_NONE;
2597 return NULL;
2598 }
2599 else if (cmp < 0)
2600 return NULL;
2601 }
2602 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2603 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002604}
2605
Jeremy Hylton8caad492000-06-23 14:18:11 +00002606static int
2607list_traverse(PyListObject *o, visitproc visit, void *arg)
2608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 for (i = Py_SIZE(o); --i >= 0; )
2612 Py_VISIT(o->ob_item[i]);
2613 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002614}
2615
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002616static PyObject *
2617list_richcompare(PyObject *v, PyObject *w, int op)
2618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 PyListObject *vl, *wl;
2620 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002621
Brian Curtindfc80e32011-08-10 20:28:54 -05002622 if (!PyList_Check(v) || !PyList_Check(w))
2623 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 vl = (PyListObject *)v;
2626 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2629 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002631 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 else
stratakise8b19652017-11-02 11:32:54 +01002633 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636 /* Search for the first index where items are different */
2637 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002638 PyObject *vitem = vl->ob_item[i];
2639 PyObject *witem = wl->ob_item[i];
Inada Naokidfef9862019-12-31 10:58:37 +09002640 if (vitem == witem) {
2641 continue;
2642 }
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002643
2644 Py_INCREF(vitem);
2645 Py_INCREF(witem);
sweeneydebe7ead62020-02-26 02:00:35 -05002646 int k = PyObject_RichCompareBool(vitem, witem, Py_EQ);
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002647 Py_DECREF(vitem);
2648 Py_DECREF(witem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 if (k < 0)
2650 return NULL;
2651 if (!k)
2652 break;
2653 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2656 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002657 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 /* We have an item that differs -- shortcuts for EQ/NE */
2661 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002662 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 }
2664 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002665 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 /* Compare the final item again using the proper operator */
2669 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002670}
2671
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002672/*[clinic input]
2673list.__init__
2674
2675 iterable: object(c_default="NULL") = ()
2676 /
2677
2678Built-in mutable sequence.
2679
2680If no argument is given, the constructor creates a new empty list.
2681The argument must be an iterable if specified.
2682[clinic start generated code]*/
2683
Tim Peters6d6c1a32001-08-02 04:15:00 +00002684static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002685list___init___impl(PyListObject *self, PyObject *iterable)
2686/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 /* Verify list invariants established by PyType_GenericAlloc() */
2689 assert(0 <= Py_SIZE(self));
2690 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2691 assert(self->ob_item != NULL ||
2692 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 /* Empty previous contents */
2695 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002696 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002698 if (iterable != NULL) {
Pablo Galindo372d7052018-10-28 20:16:26 +00002699 if (_PyObject_HasLen(iterable)) {
2700 Py_ssize_t iter_len = PyObject_Size(iterable);
2701 if (iter_len == -1) {
2702 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2703 return -1;
2704 }
2705 PyErr_Clear();
2706 }
2707 if (iter_len > 0 && self->ob_item == NULL
2708 && list_preallocate_exact(self, iter_len)) {
2709 return -1;
2710 }
2711 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002712 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 if (rv == NULL)
2714 return -1;
2715 Py_DECREF(rv);
2716 }
2717 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002718}
2719
Petr Viktorince105542020-03-30 14:16:16 +02002720static PyObject *
2721list_vectorcall(PyObject *type, PyObject * const*args,
2722 size_t nargsf, PyObject *kwnames)
2723{
2724 if (!_PyArg_NoKwnames("list", kwnames)) {
2725 return NULL;
2726 }
2727 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
2728 if (!_PyArg_CheckPositional("list", nargs, 0, 1)) {
2729 return NULL;
2730 }
2731
2732 assert(PyType_Check(type));
2733 PyObject *list = PyType_GenericAlloc((PyTypeObject *)type, 0);
2734 if (list == NULL) {
2735 return NULL;
2736 }
2737 if (nargs) {
2738 if (list___init___impl((PyListObject *)list, args[0])) {
2739 Py_DECREF(list);
2740 return NULL;
2741 }
2742 }
2743 return list;
2744}
2745
2746
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002747/*[clinic input]
2748list.__sizeof__
2749
2750Return the size of the list in memory, in bytes.
2751[clinic start generated code]*/
2752
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002753static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002754list___sizeof___impl(PyListObject *self)
2755/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002758
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002759 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002761}
2762
Raymond Hettinger1021c442003-11-07 15:38:09 +00002763static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002764static PyObject *list_subscript(PyListObject*, PyObject*);
2765
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002766static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002767 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2768 LIST___REVERSED___METHODDEF
2769 LIST___SIZEOF___METHODDEF
2770 LIST_CLEAR_METHODDEF
2771 LIST_COPY_METHODDEF
2772 LIST_APPEND_METHODDEF
2773 LIST_INSERT_METHODDEF
2774 LIST_EXTEND_METHODDEF
2775 LIST_POP_METHODDEF
2776 LIST_REMOVE_METHODDEF
2777 LIST_INDEX_METHODDEF
2778 LIST_COUNT_METHODDEF
2779 LIST_REVERSE_METHODDEF
2780 LIST_SORT_METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -07002781 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002783};
2784
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002785static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 (lenfunc)list_length, /* sq_length */
2787 (binaryfunc)list_concat, /* sq_concat */
2788 (ssizeargfunc)list_repeat, /* sq_repeat */
2789 (ssizeargfunc)list_item, /* sq_item */
2790 0, /* sq_slice */
2791 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2792 0, /* sq_ass_slice */
2793 (objobjproc)list_contains, /* sq_contains */
2794 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2795 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002796};
2797
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002798static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002799list_subscript(PyListObject* self, PyObject* item)
2800{
Victor Stinnera15e2602020-04-08 02:01:56 +02002801 if (_PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 Py_ssize_t i;
2803 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2804 if (i == -1 && PyErr_Occurred())
2805 return NULL;
2806 if (i < 0)
2807 i += PyList_GET_SIZE(self);
2808 return list_item(self, i);
2809 }
2810 else if (PySlice_Check(item)) {
HongWeipeng3c87a662019-09-08 18:15:56 +08002811 Py_ssize_t start, stop, step, slicelength, i;
2812 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 PyObject* result;
2814 PyObject* it;
2815 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002816
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002817 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 return NULL;
2819 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002820 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2821 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 if (slicelength <= 0) {
2824 return PyList_New(0);
2825 }
2826 else if (step == 1) {
2827 return list_slice(self, start, stop);
2828 }
2829 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002830 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 src = self->ob_item;
2834 dest = ((PyListObject *)result)->ob_item;
2835 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002836 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 it = src[cur];
2838 Py_INCREF(it);
2839 dest[i] = it;
2840 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +01002841 Py_SET_SIZE(result, slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 return result;
2843 }
2844 }
2845 else {
2846 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002847 "list indices must be integers or slices, not %.200s",
Victor Stinner58ac7002020-02-07 03:04:21 +01002848 Py_TYPE(item)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 return NULL;
2850 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002851}
2852
Tim Peters3b01a122002-07-19 02:35:45 +00002853static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002854list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2855{
Victor Stinnera15e2602020-04-08 02:01:56 +02002856 if (_PyIndex_Check(item)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2858 if (i == -1 && PyErr_Occurred())
2859 return -1;
2860 if (i < 0)
2861 i += PyList_GET_SIZE(self);
2862 return list_ass_item(self, i, value);
2863 }
2864 else if (PySlice_Check(item)) {
2865 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002866
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002867 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 return -1;
2869 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002870 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2871 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 if (step == 1)
2874 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 /* Make sure s[5:2] = [..] inserts at the right place:
2877 before 5, not before 2. */
2878 if ((step < 0 && start < stop) ||
2879 (step > 0 && start > stop))
2880 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 if (value == NULL) {
2883 /* delete slice */
2884 PyObject **garbage;
2885 size_t cur;
2886 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002887 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 if (slicelength <= 0)
2890 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 if (step < 0) {
2893 stop = start + 1;
2894 start = stop + step*(slicelength - 1) - 1;
2895 step = -step;
2896 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 garbage = (PyObject**)
2899 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2900 if (!garbage) {
2901 PyErr_NoMemory();
2902 return -1;
2903 }
Tim Peters3b01a122002-07-19 02:35:45 +00002904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 /* drawing pictures might help understand these for
2906 loops. Basically, we memmove the parts of the
2907 list that are *not* part of the slice: step-1
2908 items for each item that is part of the slice,
2909 and then tail end of the list that was not
2910 covered by the slice */
2911 for (cur = start, i = 0;
2912 cur < (size_t)stop;
2913 cur += step, i++) {
2914 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 if (cur + step >= (size_t)Py_SIZE(self)) {
2919 lim = Py_SIZE(self) - cur - 1;
2920 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 memmove(self->ob_item + cur - i,
2923 self->ob_item + cur + 1,
2924 lim * sizeof(PyObject *));
2925 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002926 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 if (cur < (size_t)Py_SIZE(self)) {
2928 memmove(self->ob_item + cur - slicelength,
2929 self->ob_item + cur,
2930 (Py_SIZE(self) - cur) *
2931 sizeof(PyObject *));
2932 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002933
Victor Stinner60ac6ed2020-02-07 23:18:08 +01002934 Py_SET_SIZE(self, Py_SIZE(self) - slicelength);
Victor Stinner35f28032013-11-21 12:16:35 +01002935 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 for (i = 0; i < slicelength; i++) {
2938 Py_DECREF(garbage[i]);
2939 }
2940 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002941
Victor Stinner35f28032013-11-21 12:16:35 +01002942 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 }
2944 else {
2945 /* assign slice */
2946 PyObject *ins, *seq;
2947 PyObject **garbage, **seqitems, **selfitems;
HongWeipeng3c87a662019-09-08 18:15:56 +08002948 Py_ssize_t i;
2949 size_t cur;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 /* protect against a[::-1] = a */
2952 if (self == (PyListObject*)value) {
2953 seq = list_slice((PyListObject*)value, 0,
2954 PyList_GET_SIZE(value));
2955 }
2956 else {
2957 seq = PySequence_Fast(value,
2958 "must assign iterable "
2959 "to extended slice");
2960 }
2961 if (!seq)
2962 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2965 PyErr_Format(PyExc_ValueError,
2966 "attempt to assign sequence of "
2967 "size %zd to extended slice of "
2968 "size %zd",
2969 PySequence_Fast_GET_SIZE(seq),
2970 slicelength);
2971 Py_DECREF(seq);
2972 return -1;
2973 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 if (!slicelength) {
2976 Py_DECREF(seq);
2977 return 0;
2978 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 garbage = (PyObject**)
2981 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2982 if (!garbage) {
2983 Py_DECREF(seq);
2984 PyErr_NoMemory();
2985 return -1;
2986 }
Tim Peters3b01a122002-07-19 02:35:45 +00002987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 selfitems = self->ob_item;
2989 seqitems = PySequence_Fast_ITEMS(seq);
2990 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002991 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 garbage[i] = selfitems[cur];
2993 ins = seqitems[i];
2994 Py_INCREF(ins);
2995 selfitems[cur] = ins;
2996 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 for (i = 0; i < slicelength; i++) {
2999 Py_DECREF(garbage[i]);
3000 }
Tim Peters3b01a122002-07-19 02:35:45 +00003001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 PyMem_FREE(garbage);
3003 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00003004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 return 0;
3006 }
3007 }
3008 else {
3009 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04003010 "list indices must be integers or slices, not %.200s",
Victor Stinner58ac7002020-02-07 03:04:21 +01003011 Py_TYPE(item)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 return -1;
3013 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003014}
3015
3016static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 (lenfunc)list_length,
3018 (binaryfunc)list_subscript,
3019 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003020};
3021
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003022PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3024 "list",
3025 sizeof(PyListObject),
3026 0,
3027 (destructor)list_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003028 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 0, /* tp_getattr */
3030 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003031 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 (reprfunc)list_repr, /* tp_repr */
3033 0, /* tp_as_number */
3034 &list_as_sequence, /* tp_as_sequence */
3035 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003036 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 0, /* tp_call */
3038 0, /* tp_str */
3039 PyObject_GenericGetAttr, /* tp_getattro */
3040 0, /* tp_setattro */
3041 0, /* tp_as_buffer */
3042 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003043 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
3044 list___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 (traverseproc)list_traverse, /* tp_traverse */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003046 (inquiry)_list_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 list_richcompare, /* tp_richcompare */
3048 0, /* tp_weaklistoffset */
3049 list_iter, /* tp_iter */
3050 0, /* tp_iternext */
3051 list_methods, /* tp_methods */
3052 0, /* tp_members */
3053 0, /* tp_getset */
3054 0, /* tp_base */
3055 0, /* tp_dict */
3056 0, /* tp_descr_get */
3057 0, /* tp_descr_set */
3058 0, /* tp_dictoffset */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003059 (initproc)list___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 PyType_GenericAlloc, /* tp_alloc */
3061 PyType_GenericNew, /* tp_new */
3062 PyObject_GC_Del, /* tp_free */
Petr Viktorince105542020-03-30 14:16:16 +02003063 .tp_vectorcall = list_vectorcall,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003064};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00003065
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003066/*********************** List Iterator **************************/
3067
3068typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02003070 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003072} listiterobject;
3073
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003074static void listiter_dealloc(listiterobject *);
3075static int listiter_traverse(listiterobject *, visitproc, void *);
3076static PyObject *listiter_next(listiterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303077static PyObject *listiter_len(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003078static PyObject *listiter_reduce_general(void *_it, int forward);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303079static PyObject *listiter_reduce(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003080static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00003081
Armin Rigof5b3e362006-02-11 21:32:43 +00003082PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003083PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3084PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003085
3086static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003088 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3089 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00003091};
3092
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003093PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3095 "list_iterator", /* tp_name */
3096 sizeof(listiterobject), /* tp_basicsize */
3097 0, /* tp_itemsize */
3098 /* methods */
3099 (destructor)listiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003100 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 0, /* tp_getattr */
3102 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003103 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 0, /* tp_repr */
3105 0, /* tp_as_number */
3106 0, /* tp_as_sequence */
3107 0, /* tp_as_mapping */
3108 0, /* tp_hash */
3109 0, /* tp_call */
3110 0, /* tp_str */
3111 PyObject_GenericGetAttr, /* tp_getattro */
3112 0, /* tp_setattro */
3113 0, /* tp_as_buffer */
3114 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3115 0, /* tp_doc */
3116 (traverseproc)listiter_traverse, /* tp_traverse */
3117 0, /* tp_clear */
3118 0, /* tp_richcompare */
3119 0, /* tp_weaklistoffset */
3120 PyObject_SelfIter, /* tp_iter */
3121 (iternextfunc)listiter_next, /* tp_iternext */
3122 listiter_methods, /* tp_methods */
3123 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003124};
Raymond Hettinger1021c442003-11-07 15:38:09 +00003125
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003126
3127static PyObject *
3128list_iter(PyObject *seq)
3129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 if (!PyList_Check(seq)) {
3133 PyErr_BadInternalCall();
3134 return NULL;
3135 }
3136 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3137 if (it == NULL)
3138 return NULL;
3139 it->it_index = 0;
3140 Py_INCREF(seq);
3141 it->it_seq = (PyListObject *)seq;
3142 _PyObject_GC_TRACK(it);
3143 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003144}
3145
3146static void
3147listiter_dealloc(listiterobject *it)
3148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 _PyObject_GC_UNTRACK(it);
3150 Py_XDECREF(it->it_seq);
3151 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003152}
3153
3154static int
3155listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 Py_VISIT(it->it_seq);
3158 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003159}
3160
3161static PyObject *
3162listiter_next(listiterobject *it)
3163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 PyListObject *seq;
3165 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 assert(it != NULL);
3168 seq = it->it_seq;
3169 if (seq == NULL)
3170 return NULL;
3171 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 if (it->it_index < PyList_GET_SIZE(seq)) {
3174 item = PyList_GET_ITEM(seq, it->it_index);
3175 ++it->it_index;
3176 Py_INCREF(item);
3177 return item;
3178 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003181 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003183}
3184
3185static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303186listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 Py_ssize_t len;
3189 if (it->it_seq) {
3190 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3191 if (len >= 0)
3192 return PyLong_FromSsize_t(len);
3193 }
3194 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003195}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003196
3197static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303198listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003199{
3200 return listiter_reduce_general(it, 1);
3201}
3202
3203static PyObject *
3204listiter_setstate(listiterobject *it, PyObject *state)
3205{
Victor Stinner7660b882013-06-24 23:59:24 +02003206 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003207 if (index == -1 && PyErr_Occurred())
3208 return NULL;
3209 if (it->it_seq != NULL) {
3210 if (index < 0)
3211 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003212 else if (index > PyList_GET_SIZE(it->it_seq))
3213 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003214 it->it_index = index;
3215 }
3216 Py_RETURN_NONE;
3217}
3218
Raymond Hettinger1021c442003-11-07 15:38:09 +00003219/*********************** List Reverse Iterator **************************/
3220
3221typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 PyObject_HEAD
3223 Py_ssize_t it_index;
3224 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00003225} listreviterobject;
3226
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003227static void listreviter_dealloc(listreviterobject *);
3228static int listreviter_traverse(listreviterobject *, visitproc, void *);
3229static PyObject *listreviter_next(listreviterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303230static PyObject *listreviter_len(listreviterobject *, PyObject *);
3231static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003232static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003233
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003234static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003236 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3237 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003239};
3240
Raymond Hettinger1021c442003-11-07 15:38:09 +00003241PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3243 "list_reverseiterator", /* tp_name */
3244 sizeof(listreviterobject), /* tp_basicsize */
3245 0, /* tp_itemsize */
3246 /* methods */
3247 (destructor)listreviter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003248 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 0, /* tp_getattr */
3250 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003251 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 0, /* tp_repr */
3253 0, /* tp_as_number */
3254 0, /* tp_as_sequence */
3255 0, /* tp_as_mapping */
3256 0, /* tp_hash */
3257 0, /* tp_call */
3258 0, /* tp_str */
3259 PyObject_GenericGetAttr, /* tp_getattro */
3260 0, /* tp_setattro */
3261 0, /* tp_as_buffer */
3262 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3263 0, /* tp_doc */
3264 (traverseproc)listreviter_traverse, /* tp_traverse */
3265 0, /* tp_clear */
3266 0, /* tp_richcompare */
3267 0, /* tp_weaklistoffset */
3268 PyObject_SelfIter, /* tp_iter */
3269 (iternextfunc)listreviter_next, /* tp_iternext */
3270 listreviter_methods, /* tp_methods */
3271 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00003272};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003273
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003274/*[clinic input]
3275list.__reversed__
3276
3277Return a reverse iterator over the list.
3278[clinic start generated code]*/
3279
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003280static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003281list___reversed___impl(PyListObject *self)
3282/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3287 if (it == NULL)
3288 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003289 assert(PyList_Check(self));
3290 it->it_index = PyList_GET_SIZE(self) - 1;
3291 Py_INCREF(self);
3292 it->it_seq = self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 PyObject_GC_Track(it);
3294 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003295}
3296
3297static void
3298listreviter_dealloc(listreviterobject *it)
3299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 PyObject_GC_UnTrack(it);
3301 Py_XDECREF(it->it_seq);
3302 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003303}
3304
3305static int
3306listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 Py_VISIT(it->it_seq);
3309 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003310}
3311
3312static PyObject *
3313listreviter_next(listreviterobject *it)
3314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003316 Py_ssize_t index;
3317 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003318
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003319 assert(it != NULL);
3320 seq = it->it_seq;
3321 if (seq == NULL) {
3322 return NULL;
3323 }
3324 assert(PyList_Check(seq));
3325
3326 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 if (index>=0 && index < PyList_GET_SIZE(seq)) {
3328 item = PyList_GET_ITEM(seq, index);
3329 it->it_index--;
3330 Py_INCREF(item);
3331 return item;
3332 }
3333 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003334 it->it_seq = NULL;
3335 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003337}
3338
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003339static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303340listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 Py_ssize_t len = it->it_index + 1;
3343 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3344 len = 0;
3345 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003346}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003347
3348static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303349listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003350{
3351 return listiter_reduce_general(it, 0);
3352}
3353
3354static PyObject *
3355listreviter_setstate(listreviterobject *it, PyObject *state)
3356{
3357 Py_ssize_t index = PyLong_AsSsize_t(state);
3358 if (index == -1 && PyErr_Occurred())
3359 return NULL;
3360 if (it->it_seq != NULL) {
3361 if (index < -1)
3362 index = -1;
3363 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3364 index = PyList_GET_SIZE(it->it_seq) - 1;
3365 it->it_index = index;
3366 }
3367 Py_RETURN_NONE;
3368}
3369
3370/* common pickling support */
3371
3372static PyObject *
3373listiter_reduce_general(void *_it, int forward)
3374{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003375 _Py_IDENTIFIER(iter);
3376 _Py_IDENTIFIER(reversed);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003377 PyObject *list;
3378
3379 /* the objects are not the same, index is of different types! */
3380 if (forward) {
3381 listiterobject *it = (listiterobject *)_it;
3382 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003383 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003384 it->it_seq, it->it_index);
3385 } else {
3386 listreviterobject *it = (listreviterobject *)_it;
3387 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003388 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_reversed),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003389 it->it_seq, it->it_index);
3390 }
3391 /* empty iterator, create an empty list */
3392 list = PyList_New(0);
3393 if (list == NULL)
3394 return NULL;
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003395 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003396}