blob: fa26444f847fc47a050259afe222dda4beedc372 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* List object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06004#include "internal/pystate.h"
Antoine Pitrou0197ff92012-03-22 14:38:16 +01005#include "accu.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00006
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00007#ifdef STDC_HEADERS
8#include <stddef.h>
9#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000010#include <sys/types.h> /* For size_t */
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000011#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012
Serhiy Storchakafdd42c42017-03-11 09:19:20 +020013/*[clinic input]
14class list "PyListObject *" "&PyList_Type"
15[clinic start generated code]*/
16/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f9b222678f9f71e0]*/
17
18#include "clinic/listobject.c.h"
19
Tim Peters8d9eb102004-07-31 02:24:20 +000020/* Ensure ob_item has room for at least newsize elements, and set
21 * ob_size to newsize. If newsize > ob_size on entry, the content
22 * of the new slots at exit is undefined heap trash; it's the caller's
Ezio Melotti13925002011-03-16 11:05:33 +020023 * responsibility to overwrite them with sane values.
Tim Peters8d9eb102004-07-31 02:24:20 +000024 * The number of allocated elements may grow, shrink, or stay the same.
25 * Failure is impossible if newsize <= self.allocated on entry, although
26 * that partly relies on an assumption that the system realloc() never
27 * fails when passed a number of bytes <= the number of bytes last
28 * allocated (the C standard doesn't guarantee this, but it's hard to
29 * imagine a realloc implementation where it wouldn't be true).
30 * Note that self->ob_item may change, and even if newsize is less
31 * than ob_size on entry.
32 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000033static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000034list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 PyObject **items;
Xiang Zhang4cee0492017-02-22 12:32:30 +080037 size_t new_allocated, num_allocated_bytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 /* Bypass realloc() when a previous overallocation is large enough
41 to accommodate the newsize. If the newsize falls lower than half
42 the allocated size, then proceed with the realloc() to shrink the list.
43 */
44 if (allocated >= newsize && newsize >= (allocated >> 1)) {
45 assert(self->ob_item != NULL || newsize == 0);
46 Py_SIZE(self) = newsize;
47 return 0;
48 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 /* This over-allocates proportional to the list size, making room
51 * for additional growth. The over-allocation is mild, but is
52 * enough to give linear-time amortized behavior over a long
53 * sequence of appends() in the presence of a poorly-performing
54 * system realloc().
55 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
Xiang Zhang4cee0492017-02-22 12:32:30 +080056 * Note: new_allocated won't overflow because the largest possible value
57 * is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 */
Xiang Zhang4cee0492017-02-22 12:32:30 +080059 new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
60 if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 PyErr_NoMemory();
62 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (newsize == 0)
66 new_allocated = 0;
Xiang Zhang4cee0492017-02-22 12:32:30 +080067 num_allocated_bytes = new_allocated * sizeof(PyObject *);
68 items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 if (items == NULL) {
70 PyErr_NoMemory();
71 return -1;
72 }
73 self->ob_item = items;
74 Py_SIZE(self) = newsize;
75 self->allocated = new_allocated;
76 return 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000077}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000078
Christian Heimes77c02eb2008-02-09 02:18:51 +000079/* Debug statistic to compare allocations with reuse through the free list */
80#undef SHOW_ALLOC_COUNT
81#ifdef SHOW_ALLOC_COUNT
82static size_t count_alloc = 0;
83static size_t count_reuse = 0;
84
85static void
86show_alloc(void)
87{
Victor Stinnercaba55b2018-08-03 15:33:52 +020088 PyInterpreterState *interp = _PyInterpreterState_Get();
Eddie Elizondo745dc652018-02-21 20:55:18 -080089 if (!interp->core_config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030090 return;
Victor Stinner25420fe2017-11-20 18:12:22 -080091 }
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
94 count_alloc);
95 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
96 "d\n", count_reuse);
97 fprintf(stderr, "%.2f%% reuse rate\n\n",
98 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +000099}
100#endif
101
Raymond Hettinger0468e412004-05-05 05:37:53 +0000102/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000103#ifndef PyList_MAXFREELIST
104#define PyList_MAXFREELIST 80
105#endif
106static PyListObject *free_list[PyList_MAXFREELIST];
107static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000108
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100109int
110PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100113 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 while (numfree) {
115 op = free_list[--numfree];
116 assert(PyList_CheckExact(op));
117 PyObject_GC_Del(op);
118 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100119 return ret;
120}
121
122void
123PyList_Fini(void)
124{
125 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000126}
127
David Malcolm49526f42012-06-22 14:55:41 -0400128/* Print summary info about the state of the optimized allocator */
129void
130_PyList_DebugMallocStats(FILE *out)
131{
132 _PyDebugAllocatorStats(out,
133 "free PyListObject",
134 numfree, sizeof(PyListObject));
135}
136
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000137PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000138PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 PyListObject *op;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000141#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 static int initialized = 0;
143 if (!initialized) {
144 Py_AtExit(show_alloc);
145 initialized = 1;
146 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000147#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 if (size < 0) {
150 PyErr_BadInternalCall();
151 return NULL;
152 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (numfree) {
154 numfree--;
155 op = free_list[numfree];
156 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000157#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000159#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 } else {
161 op = PyObject_GC_New(PyListObject, &PyList_Type);
162 if (op == NULL)
163 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000164#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000166#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 }
168 if (size <= 0)
169 op->ob_item = NULL;
170 else {
Mark Dickinson5d132382016-08-21 08:55:15 +0100171 op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 if (op->ob_item == NULL) {
173 Py_DECREF(op);
174 return PyErr_NoMemory();
175 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 }
177 Py_SIZE(op) = size;
178 op->allocated = size;
179 _PyObject_GC_TRACK(op);
180 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181}
182
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500183static PyObject *
184list_new_prealloc(Py_ssize_t size)
185{
186 PyListObject *op = (PyListObject *) PyList_New(0);
187 if (size == 0 || op == NULL) {
188 return (PyObject *) op;
189 }
190 assert(op->ob_item == NULL);
191 op->ob_item = PyMem_New(PyObject *, size);
192 if (op->ob_item == NULL) {
193 Py_DECREF(op);
194 return PyErr_NoMemory();
195 }
196 op->allocated = size;
197 return (PyObject *) op;
198}
199
Martin v. Löwis18e16552006-02-15 17:27:45 +0000200Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000201PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 if (!PyList_Check(op)) {
204 PyErr_BadInternalCall();
205 return -1;
206 }
207 else
208 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209}
210
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700211static inline int
212valid_index(Py_ssize_t i, Py_ssize_t limit)
213{
214 /* The cast to size_t lets us use just a single comparison
215 to check whether i is in the range: 0 <= i < limit.
216
217 See: Section 14.2 "Bounds Checking" in the Agner Fog
218 optimization manual found at:
219 https://www.agner.org/optimize/optimizing_cpp.pdf
220 */
221 return (size_t) i < (size_t) limit;
222}
223
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000224static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000225
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000227PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if (!PyList_Check(op)) {
230 PyErr_BadInternalCall();
231 return NULL;
232 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700233 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 if (indexerr == NULL) {
235 indexerr = PyUnicode_FromString(
236 "list index out of range");
237 if (indexerr == NULL)
238 return NULL;
239 }
240 PyErr_SetObject(PyExc_IndexError, indexerr);
241 return NULL;
242 }
243 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244}
245
246int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200247PyList_SetItem(PyObject *op, Py_ssize_t i,
248 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200250 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 if (!PyList_Check(op)) {
252 Py_XDECREF(newitem);
253 PyErr_BadInternalCall();
254 return -1;
255 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700256 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 Py_XDECREF(newitem);
258 PyErr_SetString(PyExc_IndexError,
259 "list assignment index out of range");
260 return -1;
261 }
262 p = ((PyListObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300263 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265}
266
267static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000268ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 Py_ssize_t i, n = Py_SIZE(self);
271 PyObject **items;
272 if (v == NULL) {
273 PyErr_BadInternalCall();
274 return -1;
275 }
276 if (n == PY_SSIZE_T_MAX) {
277 PyErr_SetString(PyExc_OverflowError,
278 "cannot add more objects to list");
279 return -1;
280 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000281
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800282 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (where < 0) {
286 where += n;
287 if (where < 0)
288 where = 0;
289 }
290 if (where > n)
291 where = n;
292 items = self->ob_item;
293 for (i = n; --i >= where; )
294 items[i+1] = items[i];
295 Py_INCREF(v);
296 items[where] = v;
297 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298}
299
300int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000301PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (!PyList_Check(op)) {
304 PyErr_BadInternalCall();
305 return -1;
306 }
307 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308}
309
Raymond Hettinger40a03822004-04-12 13:05:09 +0000310static int
311app1(PyListObject *self, PyObject *v)
312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 assert (v != NULL);
316 if (n == PY_SSIZE_T_MAX) {
317 PyErr_SetString(PyExc_OverflowError,
318 "cannot add more objects to list");
319 return -1;
320 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000321
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800322 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 Py_INCREF(v);
326 PyList_SET_ITEM(self, n, v);
327 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000328}
329
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330int
Fred Drakea2f55112000-07-09 15:16:51 +0000331PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 if (PyList_Check(op) && (newitem != NULL))
334 return app1((PyListObject *)op, newitem);
335 PyErr_BadInternalCall();
336 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337}
338
339/* Methods */
340
341static void
Fred Drakea2f55112000-07-09 15:16:51 +0000342list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 Py_ssize_t i;
345 PyObject_GC_UnTrack(op);
346 Py_TRASHCAN_SAFE_BEGIN(op)
347 if (op->ob_item != NULL) {
348 /* Do it backwards, for Christian Tismer.
349 There's a simple test case where somehow this reduces
350 thrashing when a *very* large list is created and
351 immediately deleted. */
352 i = Py_SIZE(op);
353 while (--i >= 0) {
354 Py_XDECREF(op->ob_item[i]);
355 }
356 PyMem_FREE(op->ob_item);
357 }
358 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
359 free_list[numfree++] = op;
360 else
361 Py_TYPE(op)->tp_free((PyObject *)op);
362 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363}
364
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000365static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000366list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 Py_ssize_t i;
Victor Stinner5c733472013-11-18 21:11:57 +0100369 PyObject *s;
Victor Stinner5c733472013-11-18 21:11:57 +0100370 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200371
372 if (Py_SIZE(v) == 0) {
373 return PyUnicode_FromString("[]");
374 }
375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 i = Py_ReprEnter((PyObject*)v);
377 if (i != 0) {
378 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
379 }
Tim Petersa7259592001-06-16 05:11:17 +0000380
Victor Stinner5c733472013-11-18 21:11:57 +0100381 _PyUnicodeWriter_Init(&writer);
382 writer.overallocate = 1;
Victor Stinnerb8fb1972013-11-18 22:15:44 +0100383 /* "[" + "1" + ", 2" * (len - 1) + "]" */
384 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +0000385
Victor Stinner5c733472013-11-18 21:11:57 +0100386 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200387 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 /* Do repr() on each element. Note that this may mutate the list,
390 so must refetch the list size on each iteration. */
391 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100392 if (i > 0) {
Victor Stinner4a587072013-11-19 12:54:53 +0100393 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
Victor Stinner5c733472013-11-18 21:11:57 +0100394 goto error;
395 }
396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner5c733472013-11-18 21:11:57 +0100398 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200399 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100400
401 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
402 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200403 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100404 }
405 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 }
Victor Stinner5c733472013-11-18 21:11:57 +0100407
Victor Stinner4d3f1092013-11-19 12:09:00 +0100408 writer.overallocate = 0;
Victor Stinner5c733472013-11-18 21:11:57 +0100409 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200410 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100413 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200414
415error:
Victor Stinner5c733472013-11-18 21:11:57 +0100416 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200417 Py_ReprLeave((PyObject *)v);
418 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419}
420
Martin v. Löwis18e16552006-02-15 17:27:45 +0000421static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000422list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425}
426
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000427static int
Fred Drakea2f55112000-07-09 15:16:51 +0000428list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 Py_ssize_t i;
431 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
434 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
435 Py_EQ);
436 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000437}
438
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000440list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700442 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (indexerr == NULL) {
444 indexerr = PyUnicode_FromString(
445 "list index out of range");
446 if (indexerr == NULL)
447 return NULL;
448 }
449 PyErr_SetObject(PyExc_IndexError, indexerr);
450 return NULL;
451 }
452 Py_INCREF(a->ob_item[i]);
453 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000454}
455
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000457list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 PyListObject *np;
460 PyObject **src, **dest;
461 Py_ssize_t i, len;
462 if (ilow < 0)
463 ilow = 0;
464 else if (ilow > Py_SIZE(a))
465 ilow = Py_SIZE(a);
466 if (ihigh < ilow)
467 ihigh = ilow;
468 else if (ihigh > Py_SIZE(a))
469 ihigh = Py_SIZE(a);
470 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500471 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 if (np == NULL)
473 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 src = a->ob_item + ilow;
476 dest = np->ob_item;
477 for (i = 0; i < len; i++) {
478 PyObject *v = src[i];
479 Py_INCREF(v);
480 dest[i] = v;
481 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500482 Py_SIZE(np) = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484}
485
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000486PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000487PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 if (!PyList_Check(a)) {
490 PyErr_BadInternalCall();
491 return NULL;
492 }
493 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000494}
495
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000497list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 Py_ssize_t size;
500 Py_ssize_t i;
501 PyObject **src, **dest;
502 PyListObject *np;
503 if (!PyList_Check(bb)) {
504 PyErr_Format(PyExc_TypeError,
505 "can only concatenate list (not \"%.200s\") to list",
506 bb->ob_type->tp_name);
507 return NULL;
508 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000509#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000510 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000512 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500513 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 if (np == NULL) {
515 return NULL;
516 }
517 src = a->ob_item;
518 dest = np->ob_item;
519 for (i = 0; i < Py_SIZE(a); i++) {
520 PyObject *v = src[i];
521 Py_INCREF(v);
522 dest[i] = v;
523 }
524 src = b->ob_item;
525 dest = np->ob_item + Py_SIZE(a);
526 for (i = 0; i < Py_SIZE(b); i++) {
527 PyObject *v = src[i];
528 Py_INCREF(v);
529 dest[i] = v;
530 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500531 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000533#undef b
534}
535
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000537list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 Py_ssize_t i, j;
540 Py_ssize_t size;
541 PyListObject *np;
542 PyObject **p, **items;
543 PyObject *elem;
544 if (n < 0)
545 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100546 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100548 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 if (size == 0)
550 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500551 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if (np == NULL)
553 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500556 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 elem = a->ob_item[0];
558 for (i = 0; i < n; i++) {
559 items[i] = elem;
560 Py_INCREF(elem);
561 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500563 else {
564 p = np->ob_item;
565 items = a->ob_item;
566 for (i = 0; i < n; i++) {
567 for (j = 0; j < Py_SIZE(a); j++) {
568 *p = items[j];
569 Py_INCREF(*p);
570 p++;
571 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 }
573 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500574 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000576}
577
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200579_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 Py_ssize_t i;
582 PyObject **item = a->ob_item;
583 if (item != NULL) {
584 /* Because XDECREF can recursively invoke operations on
585 this list, we make it empty first. */
586 i = Py_SIZE(a);
587 Py_SIZE(a) = 0;
588 a->ob_item = NULL;
589 a->allocated = 0;
590 while (--i >= 0) {
591 Py_XDECREF(item[i]);
592 }
593 PyMem_FREE(item);
594 }
595 /* Never fails; the return value can be ignored.
596 Note that there is no guarantee that the list is actually empty
597 at this point, because XDECREF may have populated it again! */
598 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000599}
600
Tim Peters8fc4a912004-07-31 21:53:19 +0000601/* a[ilow:ihigh] = v if v != NULL.
602 * del a[ilow:ihigh] if v == NULL.
603 *
604 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
605 * guaranteed the call cannot fail.
606 */
Armin Rigo93677f02004-07-29 12:40:23 +0000607static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000608list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 /* Because [X]DECREF can recursively invoke list operations on
611 this list, we must postpone all [X]DECREF activity until
612 after the list is back in its canonical shape. Therefore
613 we must allocate an additional array, 'recycle', into which
614 we temporarily copy the items that are deleted from the
615 list. :-( */
616 PyObject *recycle_on_stack[8];
617 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
618 PyObject **item;
619 PyObject **vitem = NULL;
620 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
621 Py_ssize_t n; /* # of elements in replacement list */
622 Py_ssize_t norig; /* # of elements in list getting replaced */
623 Py_ssize_t d; /* Change in size */
624 Py_ssize_t k;
625 size_t s;
626 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 if (v == NULL)
629 n = 0;
630 else {
631 if (a == b) {
632 /* Special case "a[i:j] = a" -- copy b first */
633 v = list_slice(b, 0, Py_SIZE(b));
634 if (v == NULL)
635 return result;
636 result = list_ass_slice(a, ilow, ihigh, v);
637 Py_DECREF(v);
638 return result;
639 }
640 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
641 if(v_as_SF == NULL)
642 goto Error;
643 n = PySequence_Fast_GET_SIZE(v_as_SF);
644 vitem = PySequence_Fast_ITEMS(v_as_SF);
645 }
646 if (ilow < 0)
647 ilow = 0;
648 else if (ilow > Py_SIZE(a))
649 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 if (ihigh < ilow)
652 ihigh = ilow;
653 else if (ihigh > Py_SIZE(a))
654 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 norig = ihigh - ilow;
657 assert(norig >= 0);
658 d = n - norig;
659 if (Py_SIZE(a) + d == 0) {
660 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200661 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 }
663 item = a->ob_item;
664 /* recycle the items that we are about to remove */
665 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700666 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
667 if (s) {
668 if (s > sizeof(recycle_on_stack)) {
669 recycle = (PyObject **)PyMem_MALLOC(s);
670 if (recycle == NULL) {
671 PyErr_NoMemory();
672 goto Error;
673 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700675 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200679 Py_ssize_t tail;
680 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
681 memmove(&item[ihigh+d], &item[ihigh], tail);
682 if (list_resize(a, Py_SIZE(a) + d) < 0) {
683 memmove(&item[ihigh], &item[ihigh+d], tail);
684 memcpy(&item[ilow], recycle, s);
685 goto Error;
686 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 item = a->ob_item;
688 }
689 else if (d > 0) { /* Insert d items */
690 k = Py_SIZE(a);
691 if (list_resize(a, k+d) < 0)
692 goto Error;
693 item = a->ob_item;
694 memmove(&item[ihigh+d], &item[ihigh],
695 (k - ihigh)*sizeof(PyObject *));
696 }
697 for (k = 0; k < n; k++, ilow++) {
698 PyObject *w = vitem[k];
699 Py_XINCREF(w);
700 item[ilow] = w;
701 }
702 for (k = norig - 1; k >= 0; --k)
703 Py_XDECREF(recycle[k]);
704 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000705 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if (recycle != recycle_on_stack)
707 PyMem_FREE(recycle);
708 Py_XDECREF(v_as_SF);
709 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000710#undef b
711}
712
Guido van Rossum234f9421993-06-17 12:35:49 +0000713int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000714PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 if (!PyList_Check(a)) {
717 PyErr_BadInternalCall();
718 return -1;
719 }
720 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000721}
722
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000723static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000724list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 PyObject **items;
727 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000728
729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 size = PyList_GET_SIZE(self);
731 if (size == 0 || n == 1) {
732 Py_INCREF(self);
733 return (PyObject *)self;
734 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 if (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200737 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 Py_INCREF(self);
739 return (PyObject *)self;
740 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 if (size > PY_SSIZE_T_MAX / n) {
743 return PyErr_NoMemory();
744 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000745
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800746 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 p = size;
750 items = self->ob_item;
751 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
752 for (j = 0; j < size; j++) {
753 PyObject *o = items[j];
754 Py_INCREF(o);
755 items[p++] = o;
756 }
757 }
758 Py_INCREF(self);
759 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000760}
761
Guido van Rossum4a450d01991-04-03 19:05:18 +0000762static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000763list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000764{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700765 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 PyErr_SetString(PyExc_IndexError,
767 "list assignment index out of range");
768 return -1;
769 }
770 if (v == NULL)
771 return list_ass_slice(a, i, i+1, v);
772 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300773 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000775}
776
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200777/*[clinic input]
778list.insert
779
780 index: Py_ssize_t
781 object: object
782 /
783
784Insert object before index.
785[clinic start generated code]*/
786
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000787static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200788list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
789/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000790{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200791 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 Py_RETURN_NONE;
793 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000794}
795
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200796/*[clinic input]
797list.clear
798
799Remove all items from list.
800[clinic start generated code]*/
801
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000802static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200803list_clear_impl(PyListObject *self)
804/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000805{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200806 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000807 Py_RETURN_NONE;
808}
809
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200810/*[clinic input]
811list.copy
812
813Return a shallow copy of the list.
814[clinic start generated code]*/
815
Eli Benderskycbbaa962011-02-25 05:47:53 +0000816static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200817list_copy_impl(PyListObject *self)
818/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000819{
820 return list_slice(self, 0, Py_SIZE(self));
821}
822
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200823/*[clinic input]
824list.append
825
826 object: object
827 /
828
829Append object to the end of the list.
830[clinic start generated code]*/
831
Eli Benderskycbbaa962011-02-25 05:47:53 +0000832static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200833list_append(PyListObject *self, PyObject *object)
834/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000835{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200836 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 Py_RETURN_NONE;
838 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839}
840
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200841/*[clinic input]
842list.extend
843
844 iterable: object
845 /
846
847Extend list by appending elements from the iterable.
848[clinic start generated code]*/
849
Barry Warsawdedf6d61998-10-09 16:37:25 +0000850static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200851list_extend(PyListObject *self, PyObject *iterable)
852/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PyObject *it; /* iter(v) */
855 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200856 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 Py_ssize_t mn; /* m + n */
858 Py_ssize_t i;
859 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 /* Special cases:
862 1) lists and tuples which can use PySequence_Fast ops
863 2) extending self to self requires making a copy first
864 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200865 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
866 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200868 iterable = PySequence_Fast(iterable, "argument must be iterable");
869 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200871 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200873 /* short circuit when iterable is empty */
874 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 Py_RETURN_NONE;
876 }
877 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000878 /* It should not be possible to allocate a list large enough to cause
879 an overflow on any relevant platform */
880 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800881 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200882 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 return NULL;
884 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200885 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 * situation a.extend(a), but the following code works
887 * in that case too. Just make sure to resize self
888 * before calling PySequence_Fast_ITEMS.
889 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200890 /* populate the end of self with iterable's items */
891 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 dest = self->ob_item + m;
893 for (i = 0; i < n; i++) {
894 PyObject *o = src[i];
895 Py_INCREF(o);
896 dest[i] = o;
897 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200898 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 Py_RETURN_NONE;
900 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000901
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200902 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 if (it == NULL)
904 return NULL;
905 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200908 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800909 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 Py_DECREF(it);
911 return NULL;
912 }
913 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000914 if (m > PY_SSIZE_T_MAX - n) {
915 /* m + n overflowed; on the chance that n lied, and there really
916 * is enough room, ignore it. If n was telling the truth, we'll
917 * eventually run out of memory during the loop.
918 */
919 }
920 else {
921 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800923 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 goto error;
925 /* Make the list sane again. */
926 Py_SIZE(self) = m;
927 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 /* Run iterator to exhaustion. */
930 for (;;) {
931 PyObject *item = iternext(it);
932 if (item == NULL) {
933 if (PyErr_Occurred()) {
934 if (PyErr_ExceptionMatches(PyExc_StopIteration))
935 PyErr_Clear();
936 else
937 goto error;
938 }
939 break;
940 }
941 if (Py_SIZE(self) < self->allocated) {
942 /* steals ref */
943 PyList_SET_ITEM(self, Py_SIZE(self), item);
944 ++Py_SIZE(self);
945 }
946 else {
947 int status = app1(self, item);
948 Py_DECREF(item); /* append creates a new ref */
949 if (status < 0)
950 goto error;
951 }
952 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200955 if (Py_SIZE(self) < self->allocated) {
956 if (list_resize(self, Py_SIZE(self)) < 0)
957 goto error;
958 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 Py_DECREF(it);
961 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000962
963 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 Py_DECREF(it);
965 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000966}
967
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000968PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200969_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000970{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200971 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000972}
973
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000974static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000975list_inplace_concat(PyListObject *self, PyObject *other)
976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000978
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200979 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 if (result == NULL)
981 return result;
982 Py_DECREF(result);
983 Py_INCREF(self);
984 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000985}
986
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200987/*[clinic input]
988list.pop
989
990 index: Py_ssize_t = -1
991 /
992
993Remove and return item at index (default last).
994
995Raises IndexError if list is empty or index is out of range.
996[clinic start generated code]*/
997
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000998static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200999list_pop_impl(PyListObject *self, Py_ssize_t index)
1000/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 PyObject *v;
1003 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 if (Py_SIZE(self) == 0) {
1006 /* Special-case most common failure cause */
1007 PyErr_SetString(PyExc_IndexError, "pop from empty list");
1008 return NULL;
1009 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001010 if (index < 0)
1011 index += Py_SIZE(self);
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -07001012 if (!valid_index(index, Py_SIZE(self))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1014 return NULL;
1015 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001016 v = self->ob_item[index];
1017 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001019 if (status >= 0)
1020 return v; /* and v now owns the reference the list had */
1021 else
1022 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 }
1024 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001025 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001026 if (status < 0) {
1027 Py_DECREF(v);
1028 return NULL;
1029 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001031}
1032
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001033/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1034static void
1035reverse_slice(PyObject **lo, PyObject **hi)
1036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 --hi;
1040 while (lo < hi) {
1041 PyObject *t = *lo;
1042 *lo = *hi;
1043 *hi = t;
1044 ++lo;
1045 --hi;
1046 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001047}
1048
Tim Petersa64dc242002-08-01 02:13:36 +00001049/* Lots of code for an adaptive, stable, natural mergesort. There are many
1050 * pieces to this algorithm; read listsort.txt for overviews and details.
1051 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001052
Daniel Stutzbach98338222010-12-02 21:55:33 +00001053/* A sortslice contains a pointer to an array of keys and a pointer to
1054 * an array of corresponding values. In other words, keys[i]
1055 * corresponds with values[i]. If values == NULL, then the keys are
1056 * also the values.
1057 *
1058 * Several convenience routines are provided here, so that keys and
1059 * values are always moved in sync.
1060 */
1061
1062typedef struct {
1063 PyObject **keys;
1064 PyObject **values;
1065} sortslice;
1066
1067Py_LOCAL_INLINE(void)
1068sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1069{
1070 s1->keys[i] = s2->keys[j];
1071 if (s1->values != NULL)
1072 s1->values[i] = s2->values[j];
1073}
1074
1075Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001076sortslice_copy_incr(sortslice *dst, sortslice *src)
1077{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001078 *dst->keys++ = *src->keys++;
1079 if (dst->values != NULL)
1080 *dst->values++ = *src->values++;
1081}
1082
1083Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001084sortslice_copy_decr(sortslice *dst, sortslice *src)
1085{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001086 *dst->keys-- = *src->keys--;
1087 if (dst->values != NULL)
1088 *dst->values-- = *src->values--;
1089}
1090
1091
1092Py_LOCAL_INLINE(void)
1093sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001094 Py_ssize_t n)
1095{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001096 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1097 if (s1->values != NULL)
1098 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1099}
1100
1101Py_LOCAL_INLINE(void)
1102sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001103 Py_ssize_t n)
1104{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001105 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1106 if (s1->values != NULL)
1107 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1108}
1109
1110Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001111sortslice_advance(sortslice *slice, Py_ssize_t n)
1112{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001113 slice->keys += n;
1114 if (slice->values != NULL)
1115 slice->values += n;
1116}
1117
embg1e34da42018-01-28 20:03:23 -07001118/* Comparison function: ms->key_compare, which is set at run-time in
1119 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001120 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1121 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001122
embg1e34da42018-01-28 20:03:23 -07001123#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001124
1125/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001126 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1127 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1128*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001129#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001131
embg1e34da42018-01-28 20:03:23 -07001132/* The maximum number of entries in a MergeState's pending-runs stack.
1133 * This is enough to sort arrays of size up to about
1134 * 32 * phi ** MAX_MERGE_PENDING
1135 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1136 * with 2**64 elements.
1137 */
1138#define MAX_MERGE_PENDING 85
1139
1140/* When we get into galloping mode, we stay there until both runs win less
1141 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1142 */
1143#define MIN_GALLOP 7
1144
1145/* Avoid malloc for small temp arrays. */
1146#define MERGESTATE_TEMP_SIZE 256
1147
1148/* One MergeState exists on the stack per invocation of mergesort. It's just
1149 * a convenient way to pass state around among the helper functions.
1150 */
1151struct s_slice {
1152 sortslice base;
1153 Py_ssize_t len;
1154};
1155
1156typedef struct s_MergeState MergeState;
1157struct s_MergeState {
1158 /* This controls when we get *into* galloping mode. It's initialized
1159 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1160 * random data, and lower for highly structured data.
1161 */
1162 Py_ssize_t min_gallop;
1163
1164 /* 'a' is temp storage to help with merges. It contains room for
1165 * alloced entries.
1166 */
1167 sortslice a; /* may point to temparray below */
1168 Py_ssize_t alloced;
1169
1170 /* A stack of n pending runs yet to be merged. Run #i starts at
1171 * address base[i] and extends for len[i] elements. It's always
1172 * true (so long as the indices are in bounds) that
1173 *
1174 * pending[i].base + pending[i].len == pending[i+1].base
1175 *
1176 * so we could cut the storage for this, but it's a minor amount,
1177 * and keeping all the info explicit simplifies the code.
1178 */
1179 int n;
1180 struct s_slice pending[MAX_MERGE_PENDING];
1181
1182 /* 'a' points to this when possible, rather than muck with malloc. */
1183 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1184
1185 /* This is the function we will use to compare two keys,
1186 * even when none of our special cases apply and we have to use
1187 * safe_object_compare. */
1188 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1189
1190 /* This function is used by unsafe_object_compare to optimize comparisons
1191 * when we know our list is type-homogeneous but we can't assume anything else.
1192 * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1193 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1194
1195 /* This function is used by unsafe_tuple_compare to compare the first elements
1196 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1197 * we can assume more, and use one of the special-case compares. */
1198 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1199};
1200
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001201/* binarysort is the best method for sorting small arrays: it does
1202 few compares, but can do data movement quadratic in the number of
1203 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001204 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001205 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001206 On entry, must have lo <= start <= hi, and that [lo, start) is already
1207 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001208 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001209 Even in case of error, the output slice will be some permutation of
1210 the input (nothing is lost or duplicated).
1211*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001212static int
embg1e34da42018-01-28 20:03:23 -07001213binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001214{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001215 Py_ssize_t k;
1216 PyObject **l, **p, **r;
1217 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001218
Daniel Stutzbach98338222010-12-02 21:55:33 +00001219 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001221 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 ++start;
1223 for (; start < hi; ++start) {
1224 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001225 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 r = start;
1227 pivot = *r;
1228 /* Invariants:
1229 * pivot >= all in [lo, l).
1230 * pivot < all in [r, start).
1231 * The second is vacuously true at the start.
1232 */
1233 assert(l < r);
1234 do {
1235 p = l + ((r - l) >> 1);
1236 IFLT(pivot, *p)
1237 r = p;
1238 else
1239 l = p+1;
1240 } while (l < r);
1241 assert(l == r);
1242 /* The invariants still hold, so pivot >= all in [lo, l) and
1243 pivot < all in [l, start), so pivot belongs at l. Note
1244 that if there are elements equal to pivot, l points to the
1245 first slot after them -- that's why this sort is stable.
1246 Slide over to make room.
1247 Caution: using memmove is much slower under MSVC 5;
1248 we're not usually moving many slots. */
1249 for (p = start; p > l; --p)
1250 *p = *(p-1);
1251 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001252 if (lo.values != NULL) {
1253 Py_ssize_t offset = lo.values - lo.keys;
1254 p = start + offset;
1255 pivot = *p;
1256 l += offset;
1257 for (p = start + offset; p > l; --p)
1258 *p = *(p-1);
1259 *l = pivot;
1260 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 }
1262 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001263
1264 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001266}
1267
Tim Petersa64dc242002-08-01 02:13:36 +00001268/*
1269Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1270is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001271
Tim Petersa64dc242002-08-01 02:13:36 +00001272 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001273
Tim Petersa64dc242002-08-01 02:13:36 +00001274or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001275
Tim Petersa64dc242002-08-01 02:13:36 +00001276 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001277
Tim Petersa64dc242002-08-01 02:13:36 +00001278Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1279For its intended use in a stable mergesort, the strictness of the defn of
1280"descending" is needed so that the caller can safely reverse a descending
1281sequence without violating stability (strict > ensures there are no equal
1282elements to get out of order).
1283
1284Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001285*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001286static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001287count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 Py_ssize_t k;
1290 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 assert(lo < hi);
1293 *descending = 0;
1294 ++lo;
1295 if (lo == hi)
1296 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 n = 2;
1299 IFLT(*lo, *(lo-1)) {
1300 *descending = 1;
1301 for (lo = lo+1; lo < hi; ++lo, ++n) {
1302 IFLT(*lo, *(lo-1))
1303 ;
1304 else
1305 break;
1306 }
1307 }
1308 else {
1309 for (lo = lo+1; lo < hi; ++lo, ++n) {
1310 IFLT(*lo, *(lo-1))
1311 break;
1312 }
1313 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001316fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001318}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001319
Tim Petersa64dc242002-08-01 02:13:36 +00001320/*
1321Locate the proper position of key in a sorted vector; if the vector contains
1322an element equal to key, return the position immediately to the left of
1323the leftmost equal element. [gallop_right() does the same except returns
1324the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001325
Tim Petersa64dc242002-08-01 02:13:36 +00001326"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001327
Tim Petersa64dc242002-08-01 02:13:36 +00001328"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1329hint is to the final result, the faster this runs.
1330
1331The return value is the int k in 0..n such that
1332
1333 a[k-1] < key <= a[k]
1334
1335pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1336key belongs at index k; or, IOW, the first k elements of a should precede
1337key, and the last n-k should follow key.
1338
1339Returns -1 on error. See listsort.txt for info on the method.
1340*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001341static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001342gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 Py_ssize_t ofs;
1345 Py_ssize_t lastofs;
1346 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 a += hint;
1351 lastofs = 0;
1352 ofs = 1;
1353 IFLT(*a, key) {
1354 /* a[hint] < key -- gallop right, until
1355 * a[hint + lastofs] < key <= a[hint + ofs]
1356 */
1357 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1358 while (ofs < maxofs) {
1359 IFLT(a[ofs], key) {
1360 lastofs = ofs;
1361 ofs = (ofs << 1) + 1;
1362 if (ofs <= 0) /* int overflow */
1363 ofs = maxofs;
1364 }
1365 else /* key <= a[hint + ofs] */
1366 break;
1367 }
1368 if (ofs > maxofs)
1369 ofs = maxofs;
1370 /* Translate back to offsets relative to &a[0]. */
1371 lastofs += hint;
1372 ofs += hint;
1373 }
1374 else {
1375 /* key <= a[hint] -- gallop left, until
1376 * a[hint - ofs] < key <= a[hint - lastofs]
1377 */
1378 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1379 while (ofs < maxofs) {
1380 IFLT(*(a-ofs), key)
1381 break;
1382 /* key <= a[hint - ofs] */
1383 lastofs = ofs;
1384 ofs = (ofs << 1) + 1;
1385 if (ofs <= 0) /* int overflow */
1386 ofs = maxofs;
1387 }
1388 if (ofs > maxofs)
1389 ofs = maxofs;
1390 /* Translate back to positive offsets relative to &a[0]. */
1391 k = lastofs;
1392 lastofs = hint - ofs;
1393 ofs = hint - k;
1394 }
1395 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1398 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1399 * right of lastofs but no farther right than ofs. Do a binary
1400 * search, with invariant a[lastofs-1] < key <= a[ofs].
1401 */
1402 ++lastofs;
1403 while (lastofs < ofs) {
1404 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 IFLT(a[m], key)
1407 lastofs = m+1; /* a[m] < key */
1408 else
1409 ofs = m; /* key <= a[m] */
1410 }
1411 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1412 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001413
1414fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001416}
1417
1418/*
1419Exactly like gallop_left(), except that if key already exists in a[0:n],
1420finds the position immediately to the right of the rightmost equal value.
1421
1422The return value is the int k in 0..n such that
1423
1424 a[k-1] <= key < a[k]
1425
1426or -1 if error.
1427
1428The code duplication is massive, but this is enough different given that
1429we're sticking to "<" comparisons that it's much harder to follow if
1430written as one routine with yet another "left or right?" flag.
1431*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001432static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001433gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 Py_ssize_t ofs;
1436 Py_ssize_t lastofs;
1437 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 a += hint;
1442 lastofs = 0;
1443 ofs = 1;
1444 IFLT(key, *a) {
1445 /* key < a[hint] -- gallop left, until
1446 * a[hint - ofs] <= key < a[hint - lastofs]
1447 */
1448 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1449 while (ofs < maxofs) {
1450 IFLT(key, *(a-ofs)) {
1451 lastofs = ofs;
1452 ofs = (ofs << 1) + 1;
1453 if (ofs <= 0) /* int overflow */
1454 ofs = maxofs;
1455 }
1456 else /* a[hint - ofs] <= key */
1457 break;
1458 }
1459 if (ofs > maxofs)
1460 ofs = maxofs;
1461 /* Translate back to positive offsets relative to &a[0]. */
1462 k = lastofs;
1463 lastofs = hint - ofs;
1464 ofs = hint - k;
1465 }
1466 else {
1467 /* a[hint] <= key -- gallop right, until
1468 * a[hint + lastofs] <= key < a[hint + ofs]
1469 */
1470 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1471 while (ofs < maxofs) {
1472 IFLT(key, a[ofs])
1473 break;
1474 /* a[hint + ofs] <= key */
1475 lastofs = ofs;
1476 ofs = (ofs << 1) + 1;
1477 if (ofs <= 0) /* int overflow */
1478 ofs = maxofs;
1479 }
1480 if (ofs > maxofs)
1481 ofs = maxofs;
1482 /* Translate back to offsets relative to &a[0]. */
1483 lastofs += hint;
1484 ofs += hint;
1485 }
1486 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1489 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1490 * right of lastofs but no farther right than ofs. Do a binary
1491 * search, with invariant a[lastofs-1] <= key < a[ofs].
1492 */
1493 ++lastofs;
1494 while (lastofs < ofs) {
1495 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 IFLT(key, a[m])
1498 ofs = m; /* key < a[m] */
1499 else
1500 lastofs = m+1; /* a[m] <= key */
1501 }
1502 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1503 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001504
1505fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001507}
1508
Tim Petersa64dc242002-08-01 02:13:36 +00001509/* Conceptually a MergeState's constructor. */
1510static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001511merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001514 if (has_keyfunc) {
1515 /* The temporary space for merging will need at most half the list
1516 * size rounded up. Use the minimum possible space so we can use the
1517 * rest of temparray for other things. In particular, if there is
1518 * enough extra space, listsort() will use it to store the keys.
1519 */
1520 ms->alloced = (list_size + 1) / 2;
1521
1522 /* ms->alloced describes how many keys will be stored at
1523 ms->temparray, but we also need to store the values. Hence,
1524 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1525 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1526 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1527 ms->a.values = &ms->temparray[ms->alloced];
1528 }
1529 else {
1530 ms->alloced = MERGESTATE_TEMP_SIZE;
1531 ms->a.values = NULL;
1532 }
1533 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 ms->n = 0;
1535 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001536}
1537
1538/* Free all the temp memory owned by the MergeState. This must be called
1539 * when you're done with a MergeState, and may be called before then if
1540 * you want to free the temp memory early.
1541 */
1542static void
1543merge_freemem(MergeState *ms)
1544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001546 if (ms->a.keys != ms->temparray)
1547 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001548}
1549
1550/* Ensure enough temp memory for 'need' array slots is available.
1551 * Returns 0 on success and -1 if the memory can't be gotten.
1552 */
1553static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001554merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001555{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001556 int multiplier;
1557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 assert(ms != NULL);
1559 if (need <= ms->alloced)
1560 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001561
1562 multiplier = ms->a.values != NULL ? 2 : 1;
1563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 /* Don't realloc! That can cost cycles to copy the old data, but
1565 * we don't care what's in the block.
1566 */
1567 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001568 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 PyErr_NoMemory();
1570 return -1;
1571 }
embg1e34da42018-01-28 20:03:23 -07001572 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001573 * sizeof(PyObject *));
1574 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001576 if (ms->a.values != NULL)
1577 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 return 0;
1579 }
1580 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001582}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1584 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001585
Daniel Stutzbach98338222010-12-02 21:55:33 +00001586/* Merge the na elements starting at ssa with the nb elements starting at
1587 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1588 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1589 * should have na <= nb. See listsort.txt for more info. Return 0 if
1590 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001591 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001592static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001593merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1594 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001597 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 int result = -1; /* guilty until proved innocent */
1599 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001600
Daniel Stutzbach98338222010-12-02 21:55:33 +00001601 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1602 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 if (MERGE_GETMEM(ms, na) < 0)
1604 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001605 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1606 dest = ssa;
1607 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001608
Daniel Stutzbach98338222010-12-02 21:55:33 +00001609 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 --nb;
1611 if (nb == 0)
1612 goto Succeed;
1613 if (na == 1)
1614 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 min_gallop = ms->min_gallop;
1617 for (;;) {
1618 Py_ssize_t acount = 0; /* # of times A won in a row */
1619 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 /* Do the straightforward thing until (if ever) one run
1622 * appears to win consistently.
1623 */
1624 for (;;) {
1625 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001626 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 if (k) {
1628 if (k < 0)
1629 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001630 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 ++bcount;
1632 acount = 0;
1633 --nb;
1634 if (nb == 0)
1635 goto Succeed;
1636 if (bcount >= min_gallop)
1637 break;
1638 }
1639 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001640 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 ++acount;
1642 bcount = 0;
1643 --na;
1644 if (na == 1)
1645 goto CopyB;
1646 if (acount >= min_gallop)
1647 break;
1648 }
1649 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 /* One run is winning so consistently that galloping may
1652 * be a huge win. So try that, and continue galloping until
1653 * (if ever) neither run appears to be winning consistently
1654 * anymore.
1655 */
1656 ++min_gallop;
1657 do {
1658 assert(na > 1 && nb > 0);
1659 min_gallop -= min_gallop > 1;
1660 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001661 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 acount = k;
1663 if (k) {
1664 if (k < 0)
1665 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001666 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1667 sortslice_advance(&dest, k);
1668 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 na -= k;
1670 if (na == 1)
1671 goto CopyB;
1672 /* na==0 is impossible now if the comparison
1673 * function is consistent, but we can't assume
1674 * that it is.
1675 */
1676 if (na == 0)
1677 goto Succeed;
1678 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001679 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 --nb;
1681 if (nb == 0)
1682 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001683
embg1e34da42018-01-28 20:03:23 -07001684 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 bcount = k;
1686 if (k) {
1687 if (k < 0)
1688 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001689 sortslice_memmove(&dest, 0, &ssb, 0, k);
1690 sortslice_advance(&dest, k);
1691 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 nb -= k;
1693 if (nb == 0)
1694 goto Succeed;
1695 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001696 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 --na;
1698 if (na == 1)
1699 goto CopyB;
1700 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1701 ++min_gallop; /* penalize it for leaving galloping mode */
1702 ms->min_gallop = min_gallop;
1703 }
Tim Petersa64dc242002-08-01 02:13:36 +00001704Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001706Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001708 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001710CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001712 /* The last element of ssa belongs at the end of the merge. */
1713 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1714 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001716}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001717
Daniel Stutzbach98338222010-12-02 21:55:33 +00001718/* Merge the na elements starting at pa with the nb elements starting at
1719 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1720 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1721 * should have na >= nb. See listsort.txt for more info. Return 0 if
1722 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001723 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001724static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001725merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1726 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001729 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001732
Daniel Stutzbach98338222010-12-02 21:55:33 +00001733 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1734 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 if (MERGE_GETMEM(ms, nb) < 0)
1736 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001737 dest = ssb;
1738 sortslice_advance(&dest, nb-1);
1739 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1740 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001742 ssb.keys = ms->a.keys + nb - 1;
1743 if (ssb.values != NULL)
1744 ssb.values = ms->a.values + nb - 1;
1745 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001746
Daniel Stutzbach98338222010-12-02 21:55:33 +00001747 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 --na;
1749 if (na == 0)
1750 goto Succeed;
1751 if (nb == 1)
1752 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 min_gallop = ms->min_gallop;
1755 for (;;) {
1756 Py_ssize_t acount = 0; /* # of times A won in a row */
1757 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 /* Do the straightforward thing until (if ever) one run
1760 * appears to win consistently.
1761 */
1762 for (;;) {
1763 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001764 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 if (k) {
1766 if (k < 0)
1767 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001768 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 ++acount;
1770 bcount = 0;
1771 --na;
1772 if (na == 0)
1773 goto Succeed;
1774 if (acount >= min_gallop)
1775 break;
1776 }
1777 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001778 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 ++bcount;
1780 acount = 0;
1781 --nb;
1782 if (nb == 1)
1783 goto CopyA;
1784 if (bcount >= min_gallop)
1785 break;
1786 }
1787 }
Tim Petersa64dc242002-08-01 02:13:36 +00001788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 /* One run is winning so consistently that galloping may
1790 * be a huge win. So try that, and continue galloping until
1791 * (if ever) neither run appears to be winning consistently
1792 * anymore.
1793 */
1794 ++min_gallop;
1795 do {
1796 assert(na > 0 && nb > 1);
1797 min_gallop -= min_gallop > 1;
1798 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001799 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 if (k < 0)
1801 goto Fail;
1802 k = na - k;
1803 acount = k;
1804 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001805 sortslice_advance(&dest, -k);
1806 sortslice_advance(&ssa, -k);
1807 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 na -= k;
1809 if (na == 0)
1810 goto Succeed;
1811 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001812 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 --nb;
1814 if (nb == 1)
1815 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001816
embg1e34da42018-01-28 20:03:23 -07001817 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 if (k < 0)
1819 goto Fail;
1820 k = nb - k;
1821 bcount = k;
1822 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001823 sortslice_advance(&dest, -k);
1824 sortslice_advance(&ssb, -k);
1825 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 nb -= k;
1827 if (nb == 1)
1828 goto CopyA;
1829 /* nb==0 is impossible now if the comparison
1830 * function is consistent, but we can't assume
1831 * that it is.
1832 */
1833 if (nb == 0)
1834 goto Succeed;
1835 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001836 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 --na;
1838 if (na == 0)
1839 goto Succeed;
1840 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1841 ++min_gallop; /* penalize it for leaving galloping mode */
1842 ms->min_gallop = min_gallop;
1843 }
Tim Petersa64dc242002-08-01 02:13:36 +00001844Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001846Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001848 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001850CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001852 /* The first element of ssb belongs at the front of the merge. */
1853 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1854 sortslice_advance(&dest, -na);
1855 sortslice_advance(&ssa, -na);
1856 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001858}
1859
1860/* Merge the two runs at stack indices i and i+1.
1861 * Returns 0 on success, -1 on error.
1862 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001863static Py_ssize_t
1864merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001865{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001866 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 Py_ssize_t na, nb;
1868 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 assert(ms != NULL);
1871 assert(ms->n >= 2);
1872 assert(i >= 0);
1873 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001874
Daniel Stutzbach98338222010-12-02 21:55:33 +00001875 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001877 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 nb = ms->pending[i+1].len;
1879 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001880 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 /* Record the length of the combined runs; if i is the 3rd-last
1883 * run now, also slide over the last run (which isn't involved
1884 * in this merge). The current run i+1 goes away in any case.
1885 */
1886 ms->pending[i].len = na + nb;
1887 if (i == ms->n - 3)
1888 ms->pending[i+1] = ms->pending[i+2];
1889 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 /* Where does b start in a? Elements in a before that can be
1892 * ignored (already in place).
1893 */
embg1e34da42018-01-28 20:03:23 -07001894 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 if (k < 0)
1896 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001897 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 na -= k;
1899 if (na == 0)
1900 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 /* Where does a end in b? Elements in b after that can be
1903 * ignored (already in place).
1904 */
embg1e34da42018-01-28 20:03:23 -07001905 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 if (nb <= 0)
1907 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 /* Merge what remains of the runs, using a temp array with
1910 * min(na, nb) elements.
1911 */
1912 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001913 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001915 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001916}
1917
1918/* Examine the stack of runs waiting to be merged, merging adjacent runs
1919 * until the stack invariants are re-established:
1920 *
1921 * 1. len[-3] > len[-2] + len[-1]
1922 * 2. len[-2] > len[-1]
1923 *
1924 * See listsort.txt for more info.
1925 *
1926 * Returns 0 on success, -1 on error.
1927 */
1928static int
1929merge_collapse(MergeState *ms)
1930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 assert(ms);
1934 while (ms->n > 1) {
1935 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001936 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1937 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 if (p[n-1].len < p[n+1].len)
1939 --n;
1940 if (merge_at(ms, n) < 0)
1941 return -1;
1942 }
1943 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001944 if (merge_at(ms, n) < 0)
1945 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 }
1947 else
1948 break;
1949 }
1950 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001951}
1952
1953/* Regardless of invariants, merge all runs on the stack until only one
1954 * remains. This is used at the end of the mergesort.
1955 *
1956 * Returns 0 on success, -1 on error.
1957 */
1958static int
1959merge_force_collapse(MergeState *ms)
1960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 assert(ms);
1964 while (ms->n > 1) {
1965 Py_ssize_t n = ms->n - 2;
1966 if (n > 0 && p[n-1].len < p[n+1].len)
1967 --n;
1968 if (merge_at(ms, n) < 0)
1969 return -1;
1970 }
1971 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001972}
1973
1974/* Compute a good value for the minimum run length; natural runs shorter
1975 * than this are boosted artificially via binary insertion.
1976 *
1977 * If n < 64, return n (it's too small to bother with fancy stuff).
1978 * Else if n is an exact power of 2, return 32.
1979 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1980 * strictly less than, an exact power of 2.
1981 *
1982 * See listsort.txt for more info.
1983 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001984static Py_ssize_t
1985merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 assert(n >= 0);
1990 while (n >= 64) {
1991 r |= n & 1;
1992 n >>= 1;
1993 }
1994 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001995}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001996
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001997static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001998reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001999{
Daniel Stutzbach98338222010-12-02 21:55:33 +00002000 reverse_slice(s->keys, &s->keys[n]);
2001 if (s->values != NULL)
2002 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002003}
2004
embg1e34da42018-01-28 20:03:23 -07002005/* Here we define custom comparison functions to optimize for the cases one commonly
2006 * encounters in practice: homogeneous lists, often of one of the basic types. */
2007
2008/* This struct holds the comparison function and helper functions
2009 * selected in the pre-sort check. */
2010
2011/* These are the special case compare functions.
2012 * ms->key_compare will always point to one of these: */
2013
2014/* Heterogeneous compare: default, always safe to fall back on. */
2015static int
2016safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2017{
2018 /* No assumptions necessary! */
2019 return PyObject_RichCompareBool(v, w, Py_LT);
2020}
2021
2022/* Homogeneous compare: safe for any two compareable objects of the same type.
2023 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2024 * pre-sort check.)
2025 */
2026static int
2027unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2028{
2029 PyObject *res_obj; int res;
2030
2031 /* No assumptions, because we check first: */
2032 if (v->ob_type->tp_richcompare != ms->key_richcompare)
2033 return PyObject_RichCompareBool(v, w, Py_LT);
2034
2035 assert(ms->key_richcompare != NULL);
2036 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2037
2038 if (res_obj == Py_NotImplemented) {
2039 Py_DECREF(res_obj);
2040 return PyObject_RichCompareBool(v, w, Py_LT);
2041 }
2042 if (res_obj == NULL)
2043 return -1;
2044
2045 if (PyBool_Check(res_obj)) {
2046 res = (res_obj == Py_True);
2047 }
2048 else {
2049 res = PyObject_IsTrue(res_obj);
2050 }
2051 Py_DECREF(res_obj);
2052
2053 /* Note that we can't assert
2054 * res == PyObject_RichCompareBool(v, w, Py_LT);
2055 * because of evil compare functions like this:
2056 * lambda a, b: int(random.random() * 3) - 1)
2057 * (which is actually in test_sort.py) */
2058 return res;
2059}
2060
2061/* Latin string compare: safe for any two latin (one byte per char) strings. */
2062static int
2063unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2064{
Victor Stinner8017b802018-01-29 13:47:06 +01002065 Py_ssize_t len;
2066 int res;
embg1e34da42018-01-28 20:03:23 -07002067
2068 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2069 assert(v->ob_type == w->ob_type);
2070 assert(v->ob_type == &PyUnicode_Type);
2071 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2072 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2073
2074 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2075 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2076
2077 res = (res != 0 ?
2078 res < 0 :
2079 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2080
2081 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2082 return res;
2083}
2084
2085/* Bounded int compare: compare any two longs that fit in a single machine word. */
2086static int
2087unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2088{
2089 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2090
2091 /* Modified from Objects/longobject.c:long_compare, assuming: */
2092 assert(v->ob_type == w->ob_type);
2093 assert(v->ob_type == &PyLong_Type);
2094 assert(Py_ABS(Py_SIZE(v)) <= 1);
2095 assert(Py_ABS(Py_SIZE(w)) <= 1);
2096
2097 vl = (PyLongObject*)v;
2098 wl = (PyLongObject*)w;
2099
2100 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2101 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2102
2103 if (Py_SIZE(vl) < 0)
2104 v0 = -v0;
2105 if (Py_SIZE(wl) < 0)
2106 w0 = -w0;
2107
2108 res = v0 < w0;
2109 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2110 return res;
2111}
2112
2113/* Float compare: compare any two floats. */
2114static int
2115unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2116{
2117 int res;
2118
2119 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2120 assert(v->ob_type == w->ob_type);
2121 assert(v->ob_type == &PyFloat_Type);
2122
2123 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2124 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2125 return res;
2126}
2127
2128/* Tuple compare: compare *any* two tuples, using
2129 * ms->tuple_elem_compare to compare the first elements, which is set
2130 * using the same pre-sort check as we use for ms->key_compare,
2131 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2132 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2133 * that most tuple compares don't involve x[1:]. */
2134static int
2135unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2136{
2137 PyTupleObject *vt, *wt;
2138 Py_ssize_t i, vlen, wlen;
2139 int k;
2140
2141 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2142 assert(v->ob_type == w->ob_type);
2143 assert(v->ob_type == &PyTuple_Type);
2144 assert(Py_SIZE(v) > 0);
2145 assert(Py_SIZE(w) > 0);
2146
2147 vt = (PyTupleObject *)v;
2148 wt = (PyTupleObject *)w;
2149
2150 vlen = Py_SIZE(vt);
2151 wlen = Py_SIZE(wt);
2152
2153 for (i = 0; i < vlen && i < wlen; i++) {
2154 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2155 if (k < 0)
2156 return -1;
2157 if (!k)
2158 break;
2159 }
2160
2161 if (i >= vlen || i >= wlen)
2162 return vlen < wlen;
2163
2164 if (i == 0)
2165 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2166 else
2167 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2168}
2169
Tim Petersa64dc242002-08-01 02:13:36 +00002170/* An adaptive, stable, natural mergesort. See listsort.txt.
2171 * Returns Py_None on success, NULL on error. Even in case of error, the
2172 * list will be some permutation of its input state (nothing is lost or
2173 * duplicated).
2174 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002175/*[clinic input]
2176list.sort
2177
2178 *
2179 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002180 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002181
2182Stable sort *IN PLACE*.
2183[clinic start generated code]*/
2184
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002185static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002186list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002187/*[clinic end generated code: output=57b9f9c5e23fbe42 input=b0fcf743982c5b90]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 Py_ssize_t nremaining;
2191 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002192 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 Py_ssize_t saved_ob_size, saved_allocated;
2194 PyObject **saved_ob_item;
2195 PyObject **final_ob_item;
2196 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002198 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002201 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 if (keyfunc == Py_None)
2203 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 /* The list is temporarily made empty, so that mutations performed
2206 * by comparison functions can't affect the slice of memory we're
2207 * sorting (allowing mutations during sorting is a core-dump
2208 * factory, since ob_item may change).
2209 */
2210 saved_ob_size = Py_SIZE(self);
2211 saved_ob_item = self->ob_item;
2212 saved_allocated = self->allocated;
2213 Py_SIZE(self) = 0;
2214 self->ob_item = NULL;
2215 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002216
Daniel Stutzbach98338222010-12-02 21:55:33 +00002217 if (keyfunc == NULL) {
2218 keys = NULL;
2219 lo.keys = saved_ob_item;
2220 lo.values = NULL;
2221 }
2222 else {
2223 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2224 /* Leverage stack space we allocated but won't otherwise use */
2225 keys = &ms.temparray[saved_ob_size+1];
2226 else {
2227 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002228 if (keys == NULL) {
2229 PyErr_NoMemory();
2230 goto keyfunc_fail;
2231 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002233
2234 for (i = 0; i < saved_ob_size ; i++) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002235 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
2236 NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002237 if (keys[i] == NULL) {
2238 for (i=i-1 ; i>=0 ; i--)
2239 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002240 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002241 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002242 goto keyfunc_fail;
2243 }
2244 }
2245
2246 lo.keys = keys;
2247 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002249
embg1e34da42018-01-28 20:03:23 -07002250
2251 /* The pre-sort check: here's where we decide which compare function to use.
2252 * How much optimization is safe? We test for homogeneity with respect to
2253 * several properties that are expensive to check at compare-time, and
2254 * set ms appropriately. */
2255 if (saved_ob_size > 1) {
2256 /* Assume the first element is representative of the whole list. */
2257 int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2258 Py_SIZE(lo.keys[0]) > 0);
2259
2260 PyTypeObject* key_type = (keys_are_in_tuples ?
2261 PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2262 lo.keys[0]->ob_type);
2263
2264 int keys_are_all_same_type = 1;
2265 int strings_are_latin = 1;
2266 int ints_are_bounded = 1;
2267
2268 /* Prove that assumption by checking every key. */
2269 int i;
2270 for (i=0; i < saved_ob_size; i++) {
2271
2272 if (keys_are_in_tuples &&
2273 !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2274 keys_are_in_tuples = 0;
2275 keys_are_all_same_type = 0;
2276 break;
2277 }
2278
2279 /* Note: for lists of tuples, key is the first element of the tuple
2280 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2281 * for lists of tuples in the if-statement directly above. */
2282 PyObject *key = (keys_are_in_tuples ?
2283 PyTuple_GET_ITEM(lo.keys[i], 0) :
2284 lo.keys[i]);
2285
2286 if (key->ob_type != key_type) {
2287 keys_are_all_same_type = 0;
2288 break;
2289 }
2290
2291 if (key_type == &PyLong_Type) {
2292 if (ints_are_bounded && Py_ABS(Py_SIZE(key)) > 1)
2293 ints_are_bounded = 0;
2294 }
2295 else if (key_type == &PyUnicode_Type){
2296 if (strings_are_latin &&
2297 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND)
2298 strings_are_latin = 0;
2299 }
2300 }
2301
2302 /* Choose the best compare, given what we now know about the keys. */
2303 if (keys_are_all_same_type) {
2304
2305 if (key_type == &PyUnicode_Type && strings_are_latin) {
2306 ms.key_compare = unsafe_latin_compare;
2307 }
2308 else if (key_type == &PyLong_Type && ints_are_bounded) {
2309 ms.key_compare = unsafe_long_compare;
2310 }
2311 else if (key_type == &PyFloat_Type) {
2312 ms.key_compare = unsafe_float_compare;
2313 }
2314 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2315 ms.key_compare = unsafe_object_compare;
2316 }
2317 }
2318 else {
2319 ms.key_compare = safe_object_compare;
2320 }
2321
2322 if (keys_are_in_tuples) {
2323 /* Make sure we're not dealing with tuples of tuples
2324 * (remember: here, key_type refers list [key[0] for key in keys]) */
2325 if (key_type == &PyTuple_Type)
2326 ms.tuple_elem_compare = safe_object_compare;
2327 else
2328 ms.tuple_elem_compare = ms.key_compare;
2329
2330 ms.key_compare = unsafe_tuple_compare;
2331 }
2332 }
2333 /* End of pre-sort check: ms is now set properly! */
2334
Daniel Stutzbach98338222010-12-02 21:55:33 +00002335 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 nremaining = saved_ob_size;
2338 if (nremaining < 2)
2339 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002340
Benjamin Peterson05380642010-08-23 19:35:39 +00002341 /* Reverse sort stability achieved by initially reversing the list,
2342 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002343 if (reverse) {
2344 if (keys != NULL)
2345 reverse_slice(&keys[0], &keys[saved_ob_size]);
2346 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2347 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 /* March over the array once, left to right, finding natural runs,
2350 * and extending short natural runs to minrun elements.
2351 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 minrun = merge_compute_minrun(nremaining);
2353 do {
2354 int descending;
2355 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002358 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 if (n < 0)
2360 goto fail;
2361 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002362 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 /* If short, extend to min(minrun, nremaining). */
2364 if (n < minrun) {
2365 const Py_ssize_t force = nremaining <= minrun ?
2366 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002367 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 goto fail;
2369 n = force;
2370 }
2371 /* Push run onto pending-runs stack, and maybe merge. */
2372 assert(ms.n < MAX_MERGE_PENDING);
2373 ms.pending[ms.n].base = lo;
2374 ms.pending[ms.n].len = n;
2375 ++ms.n;
2376 if (merge_collapse(&ms) < 0)
2377 goto fail;
2378 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002379 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 nremaining -= n;
2381 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 if (merge_force_collapse(&ms) < 0)
2384 goto fail;
2385 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002386 assert(keys == NULL
2387 ? ms.pending[0].base.keys == saved_ob_item
2388 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002390 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002391
2392succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002394fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002395 if (keys != NULL) {
2396 for (i = 0; i < saved_ob_size; i++)
2397 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002398 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002399 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 if (self->allocated != -1 && result != NULL) {
2403 /* The user mucked with the list during the sort,
2404 * and we don't already have another error to report.
2405 */
2406 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2407 result = NULL;
2408 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 if (reverse && saved_ob_size > 1)
2411 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002414
Daniel Stutzbach98338222010-12-02 21:55:33 +00002415keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 final_ob_item = self->ob_item;
2417 i = Py_SIZE(self);
2418 Py_SIZE(self) = saved_ob_size;
2419 self->ob_item = saved_ob_item;
2420 self->allocated = saved_allocated;
2421 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002422 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 guarantee that the list is really empty when it returns */
2424 while (--i >= 0) {
2425 Py_XDECREF(final_ob_item[i]);
2426 }
2427 PyMem_FREE(final_ob_item);
2428 }
2429 Py_XINCREF(result);
2430 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002431}
Tim Peters330f9e92002-07-19 07:05:44 +00002432#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002433#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002434
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002435int
Fred Drakea2f55112000-07-09 15:16:51 +00002436PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 if (v == NULL || !PyList_Check(v)) {
2439 PyErr_BadInternalCall();
2440 return -1;
2441 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002442 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 if (v == NULL)
2444 return -1;
2445 Py_DECREF(v);
2446 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002447}
2448
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002449/*[clinic input]
2450list.reverse
2451
2452Reverse *IN PLACE*.
2453[clinic start generated code]*/
2454
Guido van Rossumb86c5492001-02-12 22:06:02 +00002455static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002456list_reverse_impl(PyListObject *self)
2457/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 if (Py_SIZE(self) > 1)
2460 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2461 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002462}
2463
Guido van Rossum84c76f51990-10-30 13:32:20 +00002464int
Fred Drakea2f55112000-07-09 15:16:51 +00002465PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 if (v == NULL || !PyList_Check(v)) {
2470 PyErr_BadInternalCall();
2471 return -1;
2472 }
2473 if (Py_SIZE(self) > 1)
2474 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2475 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002476}
2477
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002478PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002479PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 PyObject *w;
2482 PyObject **p, **q;
2483 Py_ssize_t n;
2484 if (v == NULL || !PyList_Check(v)) {
2485 PyErr_BadInternalCall();
2486 return NULL;
2487 }
2488 n = Py_SIZE(v);
2489 w = PyTuple_New(n);
2490 if (w == NULL)
2491 return NULL;
2492 p = ((PyTupleObject *)w)->ob_item;
2493 q = ((PyListObject *)v)->ob_item;
2494 while (--n >= 0) {
2495 Py_INCREF(*q);
2496 *p = *q;
2497 p++;
2498 q++;
2499 }
2500 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002501}
2502
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002503/*[clinic input]
2504list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002505
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002506 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002507 start: slice_index(accept={int}) = 0
2508 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002509 /
2510
2511Return first index of value.
2512
2513Raises ValueError if the value is not present.
2514[clinic start generated code]*/
2515
2516static PyObject *
2517list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2518 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002519/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002520{
2521 Py_ssize_t i;
2522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 if (start < 0) {
2524 start += Py_SIZE(self);
2525 if (start < 0)
2526 start = 0;
2527 }
2528 if (stop < 0) {
2529 stop += Py_SIZE(self);
2530 if (stop < 0)
2531 stop = 0;
2532 }
2533 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002534 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 if (cmp > 0)
2536 return PyLong_FromSsize_t(i);
2537 else if (cmp < 0)
2538 return NULL;
2539 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002540 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002542}
2543
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002544/*[clinic input]
2545list.count
2546
2547 value: object
2548 /
2549
2550Return number of occurrences of value.
2551[clinic start generated code]*/
2552
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002553static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002554list_count(PyListObject *self, PyObject *value)
2555/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 Py_ssize_t count = 0;
2558 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002561 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 if (cmp > 0)
2563 count++;
2564 else if (cmp < 0)
2565 return NULL;
2566 }
2567 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002568}
2569
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002570/*[clinic input]
2571list.remove
2572
2573 value: object
2574 /
2575
2576Remove first occurrence of value.
2577
2578Raises ValueError if the value is not present.
2579[clinic start generated code]*/
2580
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002581static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002582list_remove(PyListObject *self, PyObject *value)
2583/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002588 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 if (cmp > 0) {
2590 if (list_ass_slice(self, i, i+1,
2591 (PyObject *)NULL) == 0)
2592 Py_RETURN_NONE;
2593 return NULL;
2594 }
2595 else if (cmp < 0)
2596 return NULL;
2597 }
2598 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2599 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002600}
2601
Jeremy Hylton8caad492000-06-23 14:18:11 +00002602static int
2603list_traverse(PyListObject *o, visitproc visit, void *arg)
2604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 for (i = Py_SIZE(o); --i >= 0; )
2608 Py_VISIT(o->ob_item[i]);
2609 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002610}
2611
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002612static PyObject *
2613list_richcompare(PyObject *v, PyObject *w, int op)
2614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 PyListObject *vl, *wl;
2616 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002617
Brian Curtindfc80e32011-08-10 20:28:54 -05002618 if (!PyList_Check(v) || !PyList_Check(w))
2619 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 vl = (PyListObject *)v;
2622 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2625 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002627 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 else
stratakise8b19652017-11-02 11:32:54 +01002629 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 /* Search for the first index where items are different */
2633 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2634 int k = PyObject_RichCompareBool(vl->ob_item[i],
2635 wl->ob_item[i], Py_EQ);
2636 if (k < 0)
2637 return NULL;
2638 if (!k)
2639 break;
2640 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2643 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002644 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 /* We have an item that differs -- shortcuts for EQ/NE */
2648 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002649 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 }
2651 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002652 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 /* Compare the final item again using the proper operator */
2656 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002657}
2658
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002659/*[clinic input]
2660list.__init__
2661
2662 iterable: object(c_default="NULL") = ()
2663 /
2664
2665Built-in mutable sequence.
2666
2667If no argument is given, the constructor creates a new empty list.
2668The argument must be an iterable if specified.
2669[clinic start generated code]*/
2670
Tim Peters6d6c1a32001-08-02 04:15:00 +00002671static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002672list___init___impl(PyListObject *self, PyObject *iterable)
2673/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 /* Verify list invariants established by PyType_GenericAlloc() */
2676 assert(0 <= Py_SIZE(self));
2677 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2678 assert(self->ob_item != NULL ||
2679 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 /* Empty previous contents */
2682 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002683 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002685 if (iterable != NULL) {
2686 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 if (rv == NULL)
2688 return -1;
2689 Py_DECREF(rv);
2690 }
2691 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002692}
2693
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002694/*[clinic input]
2695list.__sizeof__
2696
2697Return the size of the list in memory, in bytes.
2698[clinic start generated code]*/
2699
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002700static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002701list___sizeof___impl(PyListObject *self)
2702/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002705
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002706 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002708}
2709
Raymond Hettinger1021c442003-11-07 15:38:09 +00002710static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002711static PyObject *list_subscript(PyListObject*, PyObject*);
2712
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002713static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002714 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2715 LIST___REVERSED___METHODDEF
2716 LIST___SIZEOF___METHODDEF
2717 LIST_CLEAR_METHODDEF
2718 LIST_COPY_METHODDEF
2719 LIST_APPEND_METHODDEF
2720 LIST_INSERT_METHODDEF
2721 LIST_EXTEND_METHODDEF
2722 LIST_POP_METHODDEF
2723 LIST_REMOVE_METHODDEF
2724 LIST_INDEX_METHODDEF
2725 LIST_COUNT_METHODDEF
2726 LIST_REVERSE_METHODDEF
2727 LIST_SORT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002729};
2730
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002731static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 (lenfunc)list_length, /* sq_length */
2733 (binaryfunc)list_concat, /* sq_concat */
2734 (ssizeargfunc)list_repeat, /* sq_repeat */
2735 (ssizeargfunc)list_item, /* sq_item */
2736 0, /* sq_slice */
2737 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2738 0, /* sq_ass_slice */
2739 (objobjproc)list_contains, /* sq_contains */
2740 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2741 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002742};
2743
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002744static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002745list_subscript(PyListObject* self, PyObject* item)
2746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 if (PyIndex_Check(item)) {
2748 Py_ssize_t i;
2749 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2750 if (i == -1 && PyErr_Occurred())
2751 return NULL;
2752 if (i < 0)
2753 i += PyList_GET_SIZE(self);
2754 return list_item(self, i);
2755 }
2756 else if (PySlice_Check(item)) {
2757 Py_ssize_t start, stop, step, slicelength, cur, i;
2758 PyObject* result;
2759 PyObject* it;
2760 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002761
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002762 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 return NULL;
2764 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002765 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2766 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 if (slicelength <= 0) {
2769 return PyList_New(0);
2770 }
2771 else if (step == 1) {
2772 return list_slice(self, start, stop);
2773 }
2774 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002775 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 src = self->ob_item;
2779 dest = ((PyListObject *)result)->ob_item;
2780 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002781 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 it = src[cur];
2783 Py_INCREF(it);
2784 dest[i] = it;
2785 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002786 Py_SIZE(result) = slicelength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 return result;
2788 }
2789 }
2790 else {
2791 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002792 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 item->ob_type->tp_name);
2794 return NULL;
2795 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002796}
2797
Tim Peters3b01a122002-07-19 02:35:45 +00002798static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002799list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 if (PyIndex_Check(item)) {
2802 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2803 if (i == -1 && PyErr_Occurred())
2804 return -1;
2805 if (i < 0)
2806 i += PyList_GET_SIZE(self);
2807 return list_ass_item(self, i, value);
2808 }
2809 else if (PySlice_Check(item)) {
2810 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002811
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002812 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 return -1;
2814 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002815 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2816 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 if (step == 1)
2819 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 /* Make sure s[5:2] = [..] inserts at the right place:
2822 before 5, not before 2. */
2823 if ((step < 0 && start < stop) ||
2824 (step > 0 && start > stop))
2825 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 if (value == NULL) {
2828 /* delete slice */
2829 PyObject **garbage;
2830 size_t cur;
2831 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002832 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 if (slicelength <= 0)
2835 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 if (step < 0) {
2838 stop = start + 1;
2839 start = stop + step*(slicelength - 1) - 1;
2840 step = -step;
2841 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 garbage = (PyObject**)
2844 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2845 if (!garbage) {
2846 PyErr_NoMemory();
2847 return -1;
2848 }
Tim Peters3b01a122002-07-19 02:35:45 +00002849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 /* drawing pictures might help understand these for
2851 loops. Basically, we memmove the parts of the
2852 list that are *not* part of the slice: step-1
2853 items for each item that is part of the slice,
2854 and then tail end of the list that was not
2855 covered by the slice */
2856 for (cur = start, i = 0;
2857 cur < (size_t)stop;
2858 cur += step, i++) {
2859 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 if (cur + step >= (size_t)Py_SIZE(self)) {
2864 lim = Py_SIZE(self) - cur - 1;
2865 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 memmove(self->ob_item + cur - i,
2868 self->ob_item + cur + 1,
2869 lim * sizeof(PyObject *));
2870 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002871 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 if (cur < (size_t)Py_SIZE(self)) {
2873 memmove(self->ob_item + cur - slicelength,
2874 self->ob_item + cur,
2875 (Py_SIZE(self) - cur) *
2876 sizeof(PyObject *));
2877 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002880 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 for (i = 0; i < slicelength; i++) {
2883 Py_DECREF(garbage[i]);
2884 }
2885 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002886
Victor Stinner35f28032013-11-21 12:16:35 +01002887 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 }
2889 else {
2890 /* assign slice */
2891 PyObject *ins, *seq;
2892 PyObject **garbage, **seqitems, **selfitems;
2893 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 /* protect against a[::-1] = a */
2896 if (self == (PyListObject*)value) {
2897 seq = list_slice((PyListObject*)value, 0,
2898 PyList_GET_SIZE(value));
2899 }
2900 else {
2901 seq = PySequence_Fast(value,
2902 "must assign iterable "
2903 "to extended slice");
2904 }
2905 if (!seq)
2906 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2909 PyErr_Format(PyExc_ValueError,
2910 "attempt to assign sequence of "
2911 "size %zd to extended slice of "
2912 "size %zd",
2913 PySequence_Fast_GET_SIZE(seq),
2914 slicelength);
2915 Py_DECREF(seq);
2916 return -1;
2917 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 if (!slicelength) {
2920 Py_DECREF(seq);
2921 return 0;
2922 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 garbage = (PyObject**)
2925 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2926 if (!garbage) {
2927 Py_DECREF(seq);
2928 PyErr_NoMemory();
2929 return -1;
2930 }
Tim Peters3b01a122002-07-19 02:35:45 +00002931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 selfitems = self->ob_item;
2933 seqitems = PySequence_Fast_ITEMS(seq);
2934 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002935 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 garbage[i] = selfitems[cur];
2937 ins = seqitems[i];
2938 Py_INCREF(ins);
2939 selfitems[cur] = ins;
2940 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 for (i = 0; i < slicelength; i++) {
2943 Py_DECREF(garbage[i]);
2944 }
Tim Peters3b01a122002-07-19 02:35:45 +00002945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 PyMem_FREE(garbage);
2947 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 return 0;
2950 }
2951 }
2952 else {
2953 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002954 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 item->ob_type->tp_name);
2956 return -1;
2957 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002958}
2959
2960static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 (lenfunc)list_length,
2962 (binaryfunc)list_subscript,
2963 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002964};
2965
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002966PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2968 "list",
2969 sizeof(PyListObject),
2970 0,
2971 (destructor)list_dealloc, /* tp_dealloc */
2972 0, /* tp_print */
2973 0, /* tp_getattr */
2974 0, /* tp_setattr */
2975 0, /* tp_reserved */
2976 (reprfunc)list_repr, /* tp_repr */
2977 0, /* tp_as_number */
2978 &list_as_sequence, /* tp_as_sequence */
2979 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002980 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 0, /* tp_call */
2982 0, /* tp_str */
2983 PyObject_GenericGetAttr, /* tp_getattro */
2984 0, /* tp_setattro */
2985 0, /* tp_as_buffer */
2986 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002987 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2988 list___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 (traverseproc)list_traverse, /* tp_traverse */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002990 (inquiry)_list_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 list_richcompare, /* tp_richcompare */
2992 0, /* tp_weaklistoffset */
2993 list_iter, /* tp_iter */
2994 0, /* tp_iternext */
2995 list_methods, /* tp_methods */
2996 0, /* tp_members */
2997 0, /* tp_getset */
2998 0, /* tp_base */
2999 0, /* tp_dict */
3000 0, /* tp_descr_get */
3001 0, /* tp_descr_set */
3002 0, /* tp_dictoffset */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003003 (initproc)list___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 PyType_GenericAlloc, /* tp_alloc */
3005 PyType_GenericNew, /* tp_new */
3006 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003007};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00003008
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003009/*********************** List Iterator **************************/
3010
3011typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02003013 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003015} listiterobject;
3016
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003017static void listiter_dealloc(listiterobject *);
3018static int listiter_traverse(listiterobject *, visitproc, void *);
3019static PyObject *listiter_next(listiterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303020static PyObject *listiter_len(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003021static PyObject *listiter_reduce_general(void *_it, int forward);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303022static PyObject *listiter_reduce(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003023static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00003024
Armin Rigof5b3e362006-02-11 21:32:43 +00003025PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003026PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3027PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003028
3029static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003031 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3032 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00003034};
3035
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003036PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3038 "list_iterator", /* tp_name */
3039 sizeof(listiterobject), /* tp_basicsize */
3040 0, /* tp_itemsize */
3041 /* methods */
3042 (destructor)listiter_dealloc, /* tp_dealloc */
3043 0, /* tp_print */
3044 0, /* tp_getattr */
3045 0, /* tp_setattr */
3046 0, /* tp_reserved */
3047 0, /* tp_repr */
3048 0, /* tp_as_number */
3049 0, /* tp_as_sequence */
3050 0, /* tp_as_mapping */
3051 0, /* tp_hash */
3052 0, /* tp_call */
3053 0, /* tp_str */
3054 PyObject_GenericGetAttr, /* tp_getattro */
3055 0, /* tp_setattro */
3056 0, /* tp_as_buffer */
3057 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3058 0, /* tp_doc */
3059 (traverseproc)listiter_traverse, /* tp_traverse */
3060 0, /* tp_clear */
3061 0, /* tp_richcompare */
3062 0, /* tp_weaklistoffset */
3063 PyObject_SelfIter, /* tp_iter */
3064 (iternextfunc)listiter_next, /* tp_iternext */
3065 listiter_methods, /* tp_methods */
3066 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003067};
Raymond Hettinger1021c442003-11-07 15:38:09 +00003068
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003069
3070static PyObject *
3071list_iter(PyObject *seq)
3072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 if (!PyList_Check(seq)) {
3076 PyErr_BadInternalCall();
3077 return NULL;
3078 }
3079 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3080 if (it == NULL)
3081 return NULL;
3082 it->it_index = 0;
3083 Py_INCREF(seq);
3084 it->it_seq = (PyListObject *)seq;
3085 _PyObject_GC_TRACK(it);
3086 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003087}
3088
3089static void
3090listiter_dealloc(listiterobject *it)
3091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 _PyObject_GC_UNTRACK(it);
3093 Py_XDECREF(it->it_seq);
3094 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003095}
3096
3097static int
3098listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 Py_VISIT(it->it_seq);
3101 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003102}
3103
3104static PyObject *
3105listiter_next(listiterobject *it)
3106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 PyListObject *seq;
3108 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 assert(it != NULL);
3111 seq = it->it_seq;
3112 if (seq == NULL)
3113 return NULL;
3114 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 if (it->it_index < PyList_GET_SIZE(seq)) {
3117 item = PyList_GET_ITEM(seq, it->it_index);
3118 ++it->it_index;
3119 Py_INCREF(item);
3120 return item;
3121 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003124 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003126}
3127
3128static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303129listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 Py_ssize_t len;
3132 if (it->it_seq) {
3133 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3134 if (len >= 0)
3135 return PyLong_FromSsize_t(len);
3136 }
3137 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003138}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003139
3140static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303141listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003142{
3143 return listiter_reduce_general(it, 1);
3144}
3145
3146static PyObject *
3147listiter_setstate(listiterobject *it, PyObject *state)
3148{
Victor Stinner7660b882013-06-24 23:59:24 +02003149 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003150 if (index == -1 && PyErr_Occurred())
3151 return NULL;
3152 if (it->it_seq != NULL) {
3153 if (index < 0)
3154 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003155 else if (index > PyList_GET_SIZE(it->it_seq))
3156 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003157 it->it_index = index;
3158 }
3159 Py_RETURN_NONE;
3160}
3161
Raymond Hettinger1021c442003-11-07 15:38:09 +00003162/*********************** List Reverse Iterator **************************/
3163
3164typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 PyObject_HEAD
3166 Py_ssize_t it_index;
3167 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00003168} listreviterobject;
3169
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003170static void listreviter_dealloc(listreviterobject *);
3171static int listreviter_traverse(listreviterobject *, visitproc, void *);
3172static PyObject *listreviter_next(listreviterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303173static PyObject *listreviter_len(listreviterobject *, PyObject *);
3174static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003175static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003176
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003177static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003179 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3180 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003182};
3183
Raymond Hettinger1021c442003-11-07 15:38:09 +00003184PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3186 "list_reverseiterator", /* tp_name */
3187 sizeof(listreviterobject), /* tp_basicsize */
3188 0, /* tp_itemsize */
3189 /* methods */
3190 (destructor)listreviter_dealloc, /* tp_dealloc */
3191 0, /* tp_print */
3192 0, /* tp_getattr */
3193 0, /* tp_setattr */
3194 0, /* tp_reserved */
3195 0, /* tp_repr */
3196 0, /* tp_as_number */
3197 0, /* tp_as_sequence */
3198 0, /* tp_as_mapping */
3199 0, /* tp_hash */
3200 0, /* tp_call */
3201 0, /* tp_str */
3202 PyObject_GenericGetAttr, /* tp_getattro */
3203 0, /* tp_setattro */
3204 0, /* tp_as_buffer */
3205 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3206 0, /* tp_doc */
3207 (traverseproc)listreviter_traverse, /* tp_traverse */
3208 0, /* tp_clear */
3209 0, /* tp_richcompare */
3210 0, /* tp_weaklistoffset */
3211 PyObject_SelfIter, /* tp_iter */
3212 (iternextfunc)listreviter_next, /* tp_iternext */
3213 listreviter_methods, /* tp_methods */
3214 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00003215};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003216
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003217/*[clinic input]
3218list.__reversed__
3219
3220Return a reverse iterator over the list.
3221[clinic start generated code]*/
3222
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003223static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003224list___reversed___impl(PyListObject *self)
3225/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3230 if (it == NULL)
3231 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003232 assert(PyList_Check(self));
3233 it->it_index = PyList_GET_SIZE(self) - 1;
3234 Py_INCREF(self);
3235 it->it_seq = self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 PyObject_GC_Track(it);
3237 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003238}
3239
3240static void
3241listreviter_dealloc(listreviterobject *it)
3242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 PyObject_GC_UnTrack(it);
3244 Py_XDECREF(it->it_seq);
3245 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003246}
3247
3248static int
3249listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 Py_VISIT(it->it_seq);
3252 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003253}
3254
3255static PyObject *
3256listreviter_next(listreviterobject *it)
3257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003259 Py_ssize_t index;
3260 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003261
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003262 assert(it != NULL);
3263 seq = it->it_seq;
3264 if (seq == NULL) {
3265 return NULL;
3266 }
3267 assert(PyList_Check(seq));
3268
3269 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 if (index>=0 && index < PyList_GET_SIZE(seq)) {
3271 item = PyList_GET_ITEM(seq, index);
3272 it->it_index--;
3273 Py_INCREF(item);
3274 return item;
3275 }
3276 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003277 it->it_seq = NULL;
3278 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003280}
3281
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003282static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303283listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 Py_ssize_t len = it->it_index + 1;
3286 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3287 len = 0;
3288 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003289}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003290
3291static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303292listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003293{
3294 return listiter_reduce_general(it, 0);
3295}
3296
3297static PyObject *
3298listreviter_setstate(listreviterobject *it, PyObject *state)
3299{
3300 Py_ssize_t index = PyLong_AsSsize_t(state);
3301 if (index == -1 && PyErr_Occurred())
3302 return NULL;
3303 if (it->it_seq != NULL) {
3304 if (index < -1)
3305 index = -1;
3306 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3307 index = PyList_GET_SIZE(it->it_seq) - 1;
3308 it->it_index = index;
3309 }
3310 Py_RETURN_NONE;
3311}
3312
3313/* common pickling support */
3314
3315static PyObject *
3316listiter_reduce_general(void *_it, int forward)
3317{
3318 PyObject *list;
3319
3320 /* the objects are not the same, index is of different types! */
3321 if (forward) {
3322 listiterobject *it = (listiterobject *)_it;
3323 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02003324 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003325 it->it_seq, it->it_index);
3326 } else {
3327 listreviterobject *it = (listreviterobject *)_it;
3328 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +02003329 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("reversed"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003330 it->it_seq, it->it_index);
3331 }
3332 /* empty iterator, create an empty list */
3333 list = PyList_New(0);
3334 if (list == NULL)
3335 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02003336 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003337}