blob: 30444089ffaf96122b581dff08caf8a25f5d8311 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* List object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01004#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01005#include "pycore_pystate.h"
Sergey Fedoseev234531b2019-02-25 21:59:12 +05006#include "pycore_tupleobject.h"
Victor Stinnere281f7d2018-11-01 02:30:36 +01007#include "pycore_accu.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00008
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00009#ifdef STDC_HEADERS
10#include <stddef.h>
11#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000012#include <sys/types.h> /* For size_t */
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000013#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014
Serhiy Storchakafdd42c42017-03-11 09:19:20 +020015/*[clinic input]
16class list "PyListObject *" "&PyList_Type"
17[clinic start generated code]*/
18/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f9b222678f9f71e0]*/
19
20#include "clinic/listobject.c.h"
21
Tim Peters8d9eb102004-07-31 02:24:20 +000022/* Ensure ob_item has room for at least newsize elements, and set
23 * ob_size to newsize. If newsize > ob_size on entry, the content
24 * of the new slots at exit is undefined heap trash; it's the caller's
Ezio Melotti13925002011-03-16 11:05:33 +020025 * responsibility to overwrite them with sane values.
Tim Peters8d9eb102004-07-31 02:24:20 +000026 * The number of allocated elements may grow, shrink, or stay the same.
27 * Failure is impossible if newsize <= self.allocated on entry, although
28 * that partly relies on an assumption that the system realloc() never
29 * fails when passed a number of bytes <= the number of bytes last
30 * allocated (the C standard doesn't guarantee this, but it's hard to
31 * imagine a realloc implementation where it wouldn't be true).
32 * Note that self->ob_item may change, and even if newsize is less
33 * than ob_size on entry.
34 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000035static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000036list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 PyObject **items;
Xiang Zhang4cee0492017-02-22 12:32:30 +080039 size_t new_allocated, num_allocated_bytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 /* Bypass realloc() when a previous overallocation is large enough
43 to accommodate the newsize. If the newsize falls lower than half
44 the allocated size, then proceed with the realloc() to shrink the list.
45 */
46 if (allocated >= newsize && newsize >= (allocated >> 1)) {
47 assert(self->ob_item != NULL || newsize == 0);
48 Py_SIZE(self) = newsize;
49 return 0;
50 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 /* This over-allocates proportional to the list size, making room
53 * for additional growth. The over-allocation is mild, but is
54 * enough to give linear-time amortized behavior over a long
55 * sequence of appends() in the presence of a poorly-performing
56 * system realloc().
57 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
Xiang Zhang4cee0492017-02-22 12:32:30 +080058 * Note: new_allocated won't overflow because the largest possible value
59 * is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 */
Xiang Zhang4cee0492017-02-22 12:32:30 +080061 new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
62 if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 PyErr_NoMemory();
64 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 if (newsize == 0)
68 new_allocated = 0;
Xiang Zhang4cee0492017-02-22 12:32:30 +080069 num_allocated_bytes = new_allocated * sizeof(PyObject *);
70 items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (items == NULL) {
72 PyErr_NoMemory();
73 return -1;
74 }
75 self->ob_item = items;
76 Py_SIZE(self) = newsize;
77 self->allocated = new_allocated;
78 return 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000079}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000080
Pablo Galindo372d7052018-10-28 20:16:26 +000081static int
82list_preallocate_exact(PyListObject *self, Py_ssize_t size)
83{
84 assert(self->ob_item == NULL);
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050085 assert(size > 0);
Pablo Galindo372d7052018-10-28 20:16:26 +000086
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050087 PyObject **items = PyMem_New(PyObject*, size);
Pablo Galindo372d7052018-10-28 20:16:26 +000088 if (items == NULL) {
89 PyErr_NoMemory();
90 return -1;
91 }
92 self->ob_item = items;
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050093 self->allocated = size;
Pablo Galindo372d7052018-10-28 20:16:26 +000094 return 0;
95}
96
Christian Heimes77c02eb2008-02-09 02:18:51 +000097/* Debug statistic to compare allocations with reuse through the free list */
98#undef SHOW_ALLOC_COUNT
99#ifdef SHOW_ALLOC_COUNT
100static size_t count_alloc = 0;
101static size_t count_reuse = 0;
102
103static void
104show_alloc(void)
105{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200106 PyInterpreterState *interp = _PyInterpreterState_Get();
Victor Stinner331a6a52019-05-27 16:39:22 +0200107 if (!interp->config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300108 return;
Victor Stinner25420fe2017-11-20 18:12:22 -0800109 }
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
112 count_alloc);
113 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
114 "d\n", count_reuse);
115 fprintf(stderr, "%.2f%% reuse rate\n\n",
116 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +0000117}
118#endif
119
Raymond Hettinger0468e412004-05-05 05:37:53 +0000120/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000121#ifndef PyList_MAXFREELIST
122#define PyList_MAXFREELIST 80
123#endif
124static PyListObject *free_list[PyList_MAXFREELIST];
125static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000126
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100127int
128PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100131 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 while (numfree) {
133 op = free_list[--numfree];
134 assert(PyList_CheckExact(op));
135 PyObject_GC_Del(op);
136 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100137 return ret;
138}
139
140void
141PyList_Fini(void)
142{
143 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000144}
145
David Malcolm49526f42012-06-22 14:55:41 -0400146/* Print summary info about the state of the optimized allocator */
147void
148_PyList_DebugMallocStats(FILE *out)
149{
150 _PyDebugAllocatorStats(out,
151 "free PyListObject",
152 numfree, sizeof(PyListObject));
153}
154
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000155PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000156PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 PyListObject *op;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000159#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 static int initialized = 0;
161 if (!initialized) {
162 Py_AtExit(show_alloc);
163 initialized = 1;
164 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000165#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 if (size < 0) {
168 PyErr_BadInternalCall();
169 return NULL;
170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (numfree) {
172 numfree--;
173 op = free_list[numfree];
174 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000175#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000177#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 } else {
179 op = PyObject_GC_New(PyListObject, &PyList_Type);
180 if (op == NULL)
181 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000182#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000184#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 }
186 if (size <= 0)
187 op->ob_item = NULL;
188 else {
Mark Dickinson5d132382016-08-21 08:55:15 +0100189 op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 if (op->ob_item == NULL) {
191 Py_DECREF(op);
192 return PyErr_NoMemory();
193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 }
195 Py_SIZE(op) = size;
196 op->allocated = size;
197 _PyObject_GC_TRACK(op);
198 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199}
200
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500201static PyObject *
202list_new_prealloc(Py_ssize_t size)
203{
204 PyListObject *op = (PyListObject *) PyList_New(0);
205 if (size == 0 || op == NULL) {
206 return (PyObject *) op;
207 }
208 assert(op->ob_item == NULL);
209 op->ob_item = PyMem_New(PyObject *, size);
210 if (op->ob_item == NULL) {
211 Py_DECREF(op);
212 return PyErr_NoMemory();
213 }
214 op->allocated = size;
215 return (PyObject *) op;
216}
217
Martin v. Löwis18e16552006-02-15 17:27:45 +0000218Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000219PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 if (!PyList_Check(op)) {
222 PyErr_BadInternalCall();
223 return -1;
224 }
225 else
226 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227}
228
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700229static inline int
230valid_index(Py_ssize_t i, Py_ssize_t limit)
231{
232 /* The cast to size_t lets us use just a single comparison
233 to check whether i is in the range: 0 <= i < limit.
234
235 See: Section 14.2 "Bounds Checking" in the Agner Fog
236 optimization manual found at:
237 https://www.agner.org/optimize/optimizing_cpp.pdf
238 */
239 return (size_t) i < (size_t) limit;
240}
241
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000242static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000243
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000245PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (!PyList_Check(op)) {
248 PyErr_BadInternalCall();
249 return NULL;
250 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700251 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 if (indexerr == NULL) {
253 indexerr = PyUnicode_FromString(
254 "list index out of range");
255 if (indexerr == NULL)
256 return NULL;
257 }
258 PyErr_SetObject(PyExc_IndexError, indexerr);
259 return NULL;
260 }
261 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262}
263
264int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200265PyList_SetItem(PyObject *op, Py_ssize_t i,
266 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200268 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 if (!PyList_Check(op)) {
270 Py_XDECREF(newitem);
271 PyErr_BadInternalCall();
272 return -1;
273 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700274 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 Py_XDECREF(newitem);
276 PyErr_SetString(PyExc_IndexError,
277 "list assignment index out of range");
278 return -1;
279 }
280 p = ((PyListObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300281 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283}
284
285static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000286ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_ssize_t i, n = Py_SIZE(self);
289 PyObject **items;
290 if (v == NULL) {
291 PyErr_BadInternalCall();
292 return -1;
293 }
294 if (n == PY_SSIZE_T_MAX) {
295 PyErr_SetString(PyExc_OverflowError,
296 "cannot add more objects to list");
297 return -1;
298 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000299
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800300 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (where < 0) {
304 where += n;
305 if (where < 0)
306 where = 0;
307 }
308 if (where > n)
309 where = n;
310 items = self->ob_item;
311 for (i = n; --i >= where; )
312 items[i+1] = items[i];
313 Py_INCREF(v);
314 items[where] = v;
315 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316}
317
318int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000319PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 if (!PyList_Check(op)) {
322 PyErr_BadInternalCall();
323 return -1;
324 }
325 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326}
327
Raymond Hettinger40a03822004-04-12 13:05:09 +0000328static int
329app1(PyListObject *self, PyObject *v)
330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 assert (v != NULL);
334 if (n == PY_SSIZE_T_MAX) {
335 PyErr_SetString(PyExc_OverflowError,
336 "cannot add more objects to list");
337 return -1;
338 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000339
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800340 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 Py_INCREF(v);
344 PyList_SET_ITEM(self, n, v);
345 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000346}
347
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348int
Fred Drakea2f55112000-07-09 15:16:51 +0000349PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 if (PyList_Check(op) && (newitem != NULL))
352 return app1((PyListObject *)op, newitem);
353 PyErr_BadInternalCall();
354 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355}
356
357/* Methods */
358
359static void
Fred Drakea2f55112000-07-09 15:16:51 +0000360list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 Py_ssize_t i;
363 PyObject_GC_UnTrack(op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200364 Py_TRASHCAN_BEGIN(op, list_dealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 if (op->ob_item != NULL) {
366 /* Do it backwards, for Christian Tismer.
367 There's a simple test case where somehow this reduces
368 thrashing when a *very* large list is created and
369 immediately deleted. */
370 i = Py_SIZE(op);
371 while (--i >= 0) {
372 Py_XDECREF(op->ob_item[i]);
373 }
374 PyMem_FREE(op->ob_item);
375 }
376 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
377 free_list[numfree++] = op;
378 else
379 Py_TYPE(op)->tp_free((PyObject *)op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200380 Py_TRASHCAN_END
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381}
382
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000384list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 Py_ssize_t i;
Victor Stinner5c733472013-11-18 21:11:57 +0100387 PyObject *s;
Victor Stinner5c733472013-11-18 21:11:57 +0100388 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200389
390 if (Py_SIZE(v) == 0) {
391 return PyUnicode_FromString("[]");
392 }
393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 i = Py_ReprEnter((PyObject*)v);
395 if (i != 0) {
396 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
397 }
Tim Petersa7259592001-06-16 05:11:17 +0000398
Victor Stinner5c733472013-11-18 21:11:57 +0100399 _PyUnicodeWriter_Init(&writer);
400 writer.overallocate = 1;
Victor Stinnerb8fb1972013-11-18 22:15:44 +0100401 /* "[" + "1" + ", 2" * (len - 1) + "]" */
402 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +0000403
Victor Stinner5c733472013-11-18 21:11:57 +0100404 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200405 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 /* Do repr() on each element. Note that this may mutate the list,
408 so must refetch the list size on each iteration. */
409 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100410 if (i > 0) {
Victor Stinner4a587072013-11-19 12:54:53 +0100411 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
Victor Stinner5c733472013-11-18 21:11:57 +0100412 goto error;
413 }
414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner5c733472013-11-18 21:11:57 +0100416 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200417 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100418
419 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
420 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200421 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100422 }
423 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 }
Victor Stinner5c733472013-11-18 21:11:57 +0100425
Victor Stinner4d3f1092013-11-19 12:09:00 +0100426 writer.overallocate = 0;
Victor Stinner5c733472013-11-18 21:11:57 +0100427 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200428 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100431 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200432
433error:
Victor Stinner5c733472013-11-18 21:11:57 +0100434 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200435 Py_ReprLeave((PyObject *)v);
436 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437}
438
Martin v. Löwis18e16552006-02-15 17:27:45 +0000439static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000440list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443}
444
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000445static int
Fred Drakea2f55112000-07-09 15:16:51 +0000446list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000447{
Dong-hee Naf64abd12020-02-17 18:13:52 +0900448 PyObject *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 Py_ssize_t i;
450 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000451
Dong-hee Naf64abd12020-02-17 18:13:52 +0900452 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) {
453 item = PyList_GET_ITEM(a, i);
454 Py_INCREF(item);
455 cmp = PyObject_RichCompareBool(el, item, Py_EQ);
456 Py_DECREF(item);
457 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000459}
460
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000462list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700464 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (indexerr == NULL) {
466 indexerr = PyUnicode_FromString(
467 "list index out of range");
468 if (indexerr == NULL)
469 return NULL;
470 }
471 PyErr_SetObject(PyExc_IndexError, indexerr);
472 return NULL;
473 }
474 Py_INCREF(a->ob_item[i]);
475 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476}
477
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000478static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000479list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 PyListObject *np;
482 PyObject **src, **dest;
483 Py_ssize_t i, len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500485 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 if (np == NULL)
487 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 src = a->ob_item + ilow;
490 dest = np->ob_item;
491 for (i = 0; i < len; i++) {
492 PyObject *v = src[i];
493 Py_INCREF(v);
494 dest[i] = v;
495 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500496 Py_SIZE(np) = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498}
499
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000500PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000501PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (!PyList_Check(a)) {
504 PyErr_BadInternalCall();
505 return NULL;
506 }
Sergey Fedoseevef1b88b2019-02-21 11:51:52 +0500507 if (ilow < 0) {
508 ilow = 0;
509 }
510 else if (ilow > Py_SIZE(a)) {
511 ilow = Py_SIZE(a);
512 }
513 if (ihigh < ilow) {
514 ihigh = ilow;
515 }
516 else if (ihigh > Py_SIZE(a)) {
517 ihigh = Py_SIZE(a);
518 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000520}
521
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000522static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000523list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 Py_ssize_t size;
526 Py_ssize_t i;
527 PyObject **src, **dest;
528 PyListObject *np;
529 if (!PyList_Check(bb)) {
530 PyErr_Format(PyExc_TypeError,
531 "can only concatenate list (not \"%.200s\") to list",
532 bb->ob_type->tp_name);
533 return NULL;
534 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000535#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000536 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000538 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500539 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (np == NULL) {
541 return NULL;
542 }
543 src = a->ob_item;
544 dest = np->ob_item;
545 for (i = 0; i < Py_SIZE(a); i++) {
546 PyObject *v = src[i];
547 Py_INCREF(v);
548 dest[i] = v;
549 }
550 src = b->ob_item;
551 dest = np->ob_item + Py_SIZE(a);
552 for (i = 0; i < Py_SIZE(b); i++) {
553 PyObject *v = src[i];
554 Py_INCREF(v);
555 dest[i] = v;
556 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500557 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000559#undef b
560}
561
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000563list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 Py_ssize_t i, j;
566 Py_ssize_t size;
567 PyListObject *np;
568 PyObject **p, **items;
569 PyObject *elem;
570 if (n < 0)
571 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100572 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100574 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 if (size == 0)
576 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500577 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 if (np == NULL)
579 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500582 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 elem = a->ob_item[0];
584 for (i = 0; i < n; i++) {
585 items[i] = elem;
586 Py_INCREF(elem);
587 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500589 else {
590 p = np->ob_item;
591 items = a->ob_item;
592 for (i = 0; i < n; i++) {
593 for (j = 0; j < Py_SIZE(a); j++) {
594 *p = items[j];
595 Py_INCREF(*p);
596 p++;
597 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 }
599 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500600 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000602}
603
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200605_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 Py_ssize_t i;
608 PyObject **item = a->ob_item;
609 if (item != NULL) {
610 /* Because XDECREF can recursively invoke operations on
611 this list, we make it empty first. */
612 i = Py_SIZE(a);
613 Py_SIZE(a) = 0;
614 a->ob_item = NULL;
615 a->allocated = 0;
616 while (--i >= 0) {
617 Py_XDECREF(item[i]);
618 }
619 PyMem_FREE(item);
620 }
621 /* Never fails; the return value can be ignored.
622 Note that there is no guarantee that the list is actually empty
623 at this point, because XDECREF may have populated it again! */
624 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000625}
626
Tim Peters8fc4a912004-07-31 21:53:19 +0000627/* a[ilow:ihigh] = v if v != NULL.
628 * del a[ilow:ihigh] if v == NULL.
629 *
630 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
631 * guaranteed the call cannot fail.
632 */
Armin Rigo93677f02004-07-29 12:40:23 +0000633static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000634list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 /* Because [X]DECREF can recursively invoke list operations on
637 this list, we must postpone all [X]DECREF activity until
638 after the list is back in its canonical shape. Therefore
639 we must allocate an additional array, 'recycle', into which
640 we temporarily copy the items that are deleted from the
641 list. :-( */
642 PyObject *recycle_on_stack[8];
643 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
644 PyObject **item;
645 PyObject **vitem = NULL;
646 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
647 Py_ssize_t n; /* # of elements in replacement list */
648 Py_ssize_t norig; /* # of elements in list getting replaced */
649 Py_ssize_t d; /* Change in size */
650 Py_ssize_t k;
651 size_t s;
652 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 if (v == NULL)
655 n = 0;
656 else {
657 if (a == b) {
658 /* Special case "a[i:j] = a" -- copy b first */
659 v = list_slice(b, 0, Py_SIZE(b));
660 if (v == NULL)
661 return result;
662 result = list_ass_slice(a, ilow, ihigh, v);
663 Py_DECREF(v);
664 return result;
665 }
666 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
667 if(v_as_SF == NULL)
668 goto Error;
669 n = PySequence_Fast_GET_SIZE(v_as_SF);
670 vitem = PySequence_Fast_ITEMS(v_as_SF);
671 }
672 if (ilow < 0)
673 ilow = 0;
674 else if (ilow > Py_SIZE(a))
675 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 if (ihigh < ilow)
678 ihigh = ilow;
679 else if (ihigh > Py_SIZE(a))
680 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 norig = ihigh - ilow;
683 assert(norig >= 0);
684 d = n - norig;
685 if (Py_SIZE(a) + d == 0) {
686 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200687 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 }
689 item = a->ob_item;
690 /* recycle the items that we are about to remove */
691 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700692 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
693 if (s) {
694 if (s > sizeof(recycle_on_stack)) {
695 recycle = (PyObject **)PyMem_MALLOC(s);
696 if (recycle == NULL) {
697 PyErr_NoMemory();
698 goto Error;
699 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700701 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200705 Py_ssize_t tail;
706 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
707 memmove(&item[ihigh+d], &item[ihigh], tail);
708 if (list_resize(a, Py_SIZE(a) + d) < 0) {
709 memmove(&item[ihigh], &item[ihigh+d], tail);
710 memcpy(&item[ilow], recycle, s);
711 goto Error;
712 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 item = a->ob_item;
714 }
715 else if (d > 0) { /* Insert d items */
716 k = Py_SIZE(a);
717 if (list_resize(a, k+d) < 0)
718 goto Error;
719 item = a->ob_item;
720 memmove(&item[ihigh+d], &item[ihigh],
721 (k - ihigh)*sizeof(PyObject *));
722 }
723 for (k = 0; k < n; k++, ilow++) {
724 PyObject *w = vitem[k];
725 Py_XINCREF(w);
726 item[ilow] = w;
727 }
728 for (k = norig - 1; k >= 0; --k)
729 Py_XDECREF(recycle[k]);
730 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000731 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 if (recycle != recycle_on_stack)
733 PyMem_FREE(recycle);
734 Py_XDECREF(v_as_SF);
735 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000736#undef b
737}
738
Guido van Rossum234f9421993-06-17 12:35:49 +0000739int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000740PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 if (!PyList_Check(a)) {
743 PyErr_BadInternalCall();
744 return -1;
745 }
746 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000747}
748
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000749static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000750list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 PyObject **items;
753 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000754
755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 size = PyList_GET_SIZE(self);
757 if (size == 0 || n == 1) {
758 Py_INCREF(self);
759 return (PyObject *)self;
760 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 if (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200763 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 Py_INCREF(self);
765 return (PyObject *)self;
766 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 if (size > PY_SSIZE_T_MAX / n) {
769 return PyErr_NoMemory();
770 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000771
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800772 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 p = size;
776 items = self->ob_item;
777 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
778 for (j = 0; j < size; j++) {
779 PyObject *o = items[j];
780 Py_INCREF(o);
781 items[p++] = o;
782 }
783 }
784 Py_INCREF(self);
785 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000786}
787
Guido van Rossum4a450d01991-04-03 19:05:18 +0000788static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000789list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000790{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700791 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 PyErr_SetString(PyExc_IndexError,
793 "list assignment index out of range");
794 return -1;
795 }
796 if (v == NULL)
797 return list_ass_slice(a, i, i+1, v);
798 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300799 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000801}
802
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200803/*[clinic input]
804list.insert
805
806 index: Py_ssize_t
807 object: object
808 /
809
810Insert object before index.
811[clinic start generated code]*/
812
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000813static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200814list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
815/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200817 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 Py_RETURN_NONE;
819 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000820}
821
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200822/*[clinic input]
823list.clear
824
825Remove all items from list.
826[clinic start generated code]*/
827
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000828static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200829list_clear_impl(PyListObject *self)
830/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000831{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200832 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000833 Py_RETURN_NONE;
834}
835
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200836/*[clinic input]
837list.copy
838
839Return a shallow copy of the list.
840[clinic start generated code]*/
841
Eli Benderskycbbaa962011-02-25 05:47:53 +0000842static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200843list_copy_impl(PyListObject *self)
844/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000845{
846 return list_slice(self, 0, Py_SIZE(self));
847}
848
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200849/*[clinic input]
850list.append
851
852 object: object
853 /
854
855Append object to the end of the list.
856[clinic start generated code]*/
857
Eli Benderskycbbaa962011-02-25 05:47:53 +0000858static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200859list_append(PyListObject *self, PyObject *object)
860/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000861{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200862 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 Py_RETURN_NONE;
864 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865}
866
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200867/*[clinic input]
868list.extend
869
870 iterable: object
871 /
872
873Extend list by appending elements from the iterable.
874[clinic start generated code]*/
875
Barry Warsawdedf6d61998-10-09 16:37:25 +0000876static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200877list_extend(PyListObject *self, PyObject *iterable)
878/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 PyObject *it; /* iter(v) */
881 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200882 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 Py_ssize_t mn; /* m + n */
884 Py_ssize_t i;
885 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 /* Special cases:
888 1) lists and tuples which can use PySequence_Fast ops
889 2) extending self to self requires making a copy first
890 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200891 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
892 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200894 iterable = PySequence_Fast(iterable, "argument must be iterable");
895 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200897 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200899 /* short circuit when iterable is empty */
900 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 Py_RETURN_NONE;
902 }
903 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000904 /* It should not be possible to allocate a list large enough to cause
905 an overflow on any relevant platform */
906 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800907 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200908 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 return NULL;
910 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200911 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 * situation a.extend(a), but the following code works
913 * in that case too. Just make sure to resize self
914 * before calling PySequence_Fast_ITEMS.
915 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200916 /* populate the end of self with iterable's items */
917 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 dest = self->ob_item + m;
919 for (i = 0; i < n; i++) {
920 PyObject *o = src[i];
921 Py_INCREF(o);
922 dest[i] = o;
923 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200924 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 Py_RETURN_NONE;
926 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000927
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200928 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 if (it == NULL)
930 return NULL;
931 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200934 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800935 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 Py_DECREF(it);
937 return NULL;
938 }
939 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000940 if (m > PY_SSIZE_T_MAX - n) {
941 /* m + n overflowed; on the chance that n lied, and there really
942 * is enough room, ignore it. If n was telling the truth, we'll
943 * eventually run out of memory during the loop.
944 */
945 }
946 else {
947 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800949 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 goto error;
951 /* Make the list sane again. */
952 Py_SIZE(self) = m;
953 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 /* Run iterator to exhaustion. */
956 for (;;) {
957 PyObject *item = iternext(it);
958 if (item == NULL) {
959 if (PyErr_Occurred()) {
960 if (PyErr_ExceptionMatches(PyExc_StopIteration))
961 PyErr_Clear();
962 else
963 goto error;
964 }
965 break;
966 }
967 if (Py_SIZE(self) < self->allocated) {
968 /* steals ref */
969 PyList_SET_ITEM(self, Py_SIZE(self), item);
970 ++Py_SIZE(self);
971 }
972 else {
973 int status = app1(self, item);
974 Py_DECREF(item); /* append creates a new ref */
975 if (status < 0)
976 goto error;
977 }
978 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200981 if (Py_SIZE(self) < self->allocated) {
982 if (list_resize(self, Py_SIZE(self)) < 0)
983 goto error;
984 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 Py_DECREF(it);
987 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000988
989 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 Py_DECREF(it);
991 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000992}
993
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000994PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200995_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000996{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200997 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000998}
999
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001000static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001001list_inplace_concat(PyListObject *self, PyObject *other)
1002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001004
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001005 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 if (result == NULL)
1007 return result;
1008 Py_DECREF(result);
1009 Py_INCREF(self);
1010 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001011}
1012
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001013/*[clinic input]
1014list.pop
1015
1016 index: Py_ssize_t = -1
1017 /
1018
1019Remove and return item at index (default last).
1020
1021Raises IndexError if list is empty or index is out of range.
1022[clinic start generated code]*/
1023
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001024static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001025list_pop_impl(PyListObject *self, Py_ssize_t index)
1026/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 PyObject *v;
1029 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 if (Py_SIZE(self) == 0) {
1032 /* Special-case most common failure cause */
1033 PyErr_SetString(PyExc_IndexError, "pop from empty list");
1034 return NULL;
1035 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001036 if (index < 0)
1037 index += Py_SIZE(self);
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -07001038 if (!valid_index(index, Py_SIZE(self))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1040 return NULL;
1041 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001042 v = self->ob_item[index];
1043 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001045 if (status >= 0)
1046 return v; /* and v now owns the reference the list had */
1047 else
1048 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 }
1050 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001051 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001052 if (status < 0) {
1053 Py_DECREF(v);
1054 return NULL;
1055 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001057}
1058
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001059/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1060static void
1061reverse_slice(PyObject **lo, PyObject **hi)
1062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 --hi;
1066 while (lo < hi) {
1067 PyObject *t = *lo;
1068 *lo = *hi;
1069 *hi = t;
1070 ++lo;
1071 --hi;
1072 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001073}
1074
Tim Petersa64dc242002-08-01 02:13:36 +00001075/* Lots of code for an adaptive, stable, natural mergesort. There are many
1076 * pieces to this algorithm; read listsort.txt for overviews and details.
1077 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001078
Daniel Stutzbach98338222010-12-02 21:55:33 +00001079/* A sortslice contains a pointer to an array of keys and a pointer to
1080 * an array of corresponding values. In other words, keys[i]
1081 * corresponds with values[i]. If values == NULL, then the keys are
1082 * also the values.
1083 *
1084 * Several convenience routines are provided here, so that keys and
1085 * values are always moved in sync.
1086 */
1087
1088typedef struct {
1089 PyObject **keys;
1090 PyObject **values;
1091} sortslice;
1092
1093Py_LOCAL_INLINE(void)
1094sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1095{
1096 s1->keys[i] = s2->keys[j];
1097 if (s1->values != NULL)
1098 s1->values[i] = s2->values[j];
1099}
1100
1101Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001102sortslice_copy_incr(sortslice *dst, sortslice *src)
1103{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001104 *dst->keys++ = *src->keys++;
1105 if (dst->values != NULL)
1106 *dst->values++ = *src->values++;
1107}
1108
1109Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001110sortslice_copy_decr(sortslice *dst, sortslice *src)
1111{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001112 *dst->keys-- = *src->keys--;
1113 if (dst->values != NULL)
1114 *dst->values-- = *src->values--;
1115}
1116
1117
1118Py_LOCAL_INLINE(void)
1119sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001120 Py_ssize_t n)
1121{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001122 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1123 if (s1->values != NULL)
1124 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1125}
1126
1127Py_LOCAL_INLINE(void)
1128sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001129 Py_ssize_t n)
1130{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001131 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1132 if (s1->values != NULL)
1133 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1134}
1135
1136Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001137sortslice_advance(sortslice *slice, Py_ssize_t n)
1138{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001139 slice->keys += n;
1140 if (slice->values != NULL)
1141 slice->values += n;
1142}
1143
embg1e34da42018-01-28 20:03:23 -07001144/* Comparison function: ms->key_compare, which is set at run-time in
1145 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001146 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1147 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001148
embg1e34da42018-01-28 20:03:23 -07001149#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001150
1151/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001152 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1153 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1154*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001155#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001157
embg1e34da42018-01-28 20:03:23 -07001158/* The maximum number of entries in a MergeState's pending-runs stack.
1159 * This is enough to sort arrays of size up to about
1160 * 32 * phi ** MAX_MERGE_PENDING
1161 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1162 * with 2**64 elements.
1163 */
1164#define MAX_MERGE_PENDING 85
1165
1166/* When we get into galloping mode, we stay there until both runs win less
1167 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1168 */
1169#define MIN_GALLOP 7
1170
1171/* Avoid malloc for small temp arrays. */
1172#define MERGESTATE_TEMP_SIZE 256
1173
1174/* One MergeState exists on the stack per invocation of mergesort. It's just
1175 * a convenient way to pass state around among the helper functions.
1176 */
1177struct s_slice {
1178 sortslice base;
1179 Py_ssize_t len;
1180};
1181
1182typedef struct s_MergeState MergeState;
1183struct s_MergeState {
1184 /* This controls when we get *into* galloping mode. It's initialized
1185 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1186 * random data, and lower for highly structured data.
1187 */
1188 Py_ssize_t min_gallop;
1189
1190 /* 'a' is temp storage to help with merges. It contains room for
1191 * alloced entries.
1192 */
1193 sortslice a; /* may point to temparray below */
1194 Py_ssize_t alloced;
1195
1196 /* A stack of n pending runs yet to be merged. Run #i starts at
1197 * address base[i] and extends for len[i] elements. It's always
1198 * true (so long as the indices are in bounds) that
1199 *
1200 * pending[i].base + pending[i].len == pending[i+1].base
1201 *
1202 * so we could cut the storage for this, but it's a minor amount,
1203 * and keeping all the info explicit simplifies the code.
1204 */
1205 int n;
1206 struct s_slice pending[MAX_MERGE_PENDING];
1207
1208 /* 'a' points to this when possible, rather than muck with malloc. */
1209 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1210
1211 /* This is the function we will use to compare two keys,
1212 * even when none of our special cases apply and we have to use
1213 * safe_object_compare. */
1214 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1215
1216 /* This function is used by unsafe_object_compare to optimize comparisons
1217 * when we know our list is type-homogeneous but we can't assume anything else.
1218 * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1219 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1220
1221 /* This function is used by unsafe_tuple_compare to compare the first elements
1222 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1223 * we can assume more, and use one of the special-case compares. */
1224 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1225};
1226
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001227/* binarysort is the best method for sorting small arrays: it does
1228 few compares, but can do data movement quadratic in the number of
1229 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001230 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001231 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001232 On entry, must have lo <= start <= hi, and that [lo, start) is already
1233 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001234 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001235 Even in case of error, the output slice will be some permutation of
1236 the input (nothing is lost or duplicated).
1237*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001238static int
embg1e34da42018-01-28 20:03:23 -07001239binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001240{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001241 Py_ssize_t k;
1242 PyObject **l, **p, **r;
1243 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001244
Daniel Stutzbach98338222010-12-02 21:55:33 +00001245 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001247 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 ++start;
1249 for (; start < hi; ++start) {
1250 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001251 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 r = start;
1253 pivot = *r;
1254 /* Invariants:
1255 * pivot >= all in [lo, l).
1256 * pivot < all in [r, start).
1257 * The second is vacuously true at the start.
1258 */
1259 assert(l < r);
1260 do {
1261 p = l + ((r - l) >> 1);
1262 IFLT(pivot, *p)
1263 r = p;
1264 else
1265 l = p+1;
1266 } while (l < r);
1267 assert(l == r);
1268 /* The invariants still hold, so pivot >= all in [lo, l) and
1269 pivot < all in [l, start), so pivot belongs at l. Note
1270 that if there are elements equal to pivot, l points to the
1271 first slot after them -- that's why this sort is stable.
1272 Slide over to make room.
1273 Caution: using memmove is much slower under MSVC 5;
1274 we're not usually moving many slots. */
1275 for (p = start; p > l; --p)
1276 *p = *(p-1);
1277 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001278 if (lo.values != NULL) {
1279 Py_ssize_t offset = lo.values - lo.keys;
1280 p = start + offset;
1281 pivot = *p;
1282 l += offset;
1283 for (p = start + offset; p > l; --p)
1284 *p = *(p-1);
1285 *l = pivot;
1286 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 }
1288 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001289
1290 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001292}
1293
Tim Petersa64dc242002-08-01 02:13:36 +00001294/*
1295Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1296is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001297
Tim Petersa64dc242002-08-01 02:13:36 +00001298 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001299
Tim Petersa64dc242002-08-01 02:13:36 +00001300or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001301
Tim Petersa64dc242002-08-01 02:13:36 +00001302 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001303
Tim Petersa64dc242002-08-01 02:13:36 +00001304Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1305For its intended use in a stable mergesort, the strictness of the defn of
1306"descending" is needed so that the caller can safely reverse a descending
1307sequence without violating stability (strict > ensures there are no equal
1308elements to get out of order).
1309
1310Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001311*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001312static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001313count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 Py_ssize_t k;
1316 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 assert(lo < hi);
1319 *descending = 0;
1320 ++lo;
1321 if (lo == hi)
1322 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 n = 2;
1325 IFLT(*lo, *(lo-1)) {
1326 *descending = 1;
1327 for (lo = lo+1; lo < hi; ++lo, ++n) {
1328 IFLT(*lo, *(lo-1))
1329 ;
1330 else
1331 break;
1332 }
1333 }
1334 else {
1335 for (lo = lo+1; lo < hi; ++lo, ++n) {
1336 IFLT(*lo, *(lo-1))
1337 break;
1338 }
1339 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001342fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001344}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001345
Tim Petersa64dc242002-08-01 02:13:36 +00001346/*
1347Locate the proper position of key in a sorted vector; if the vector contains
1348an element equal to key, return the position immediately to the left of
1349the leftmost equal element. [gallop_right() does the same except returns
1350the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001351
Tim Petersa64dc242002-08-01 02:13:36 +00001352"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001353
Tim Petersa64dc242002-08-01 02:13:36 +00001354"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1355hint is to the final result, the faster this runs.
1356
1357The return value is the int k in 0..n such that
1358
1359 a[k-1] < key <= a[k]
1360
1361pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1362key belongs at index k; or, IOW, the first k elements of a should precede
1363key, and the last n-k should follow key.
1364
1365Returns -1 on error. See listsort.txt for info on the method.
1366*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001367static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001368gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 Py_ssize_t ofs;
1371 Py_ssize_t lastofs;
1372 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 a += hint;
1377 lastofs = 0;
1378 ofs = 1;
1379 IFLT(*a, key) {
1380 /* a[hint] < key -- gallop right, until
1381 * a[hint + lastofs] < key <= a[hint + ofs]
1382 */
1383 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1384 while (ofs < maxofs) {
1385 IFLT(a[ofs], key) {
1386 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001387 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 }
1390 else /* key <= a[hint + ofs] */
1391 break;
1392 }
1393 if (ofs > maxofs)
1394 ofs = maxofs;
1395 /* Translate back to offsets relative to &a[0]. */
1396 lastofs += hint;
1397 ofs += hint;
1398 }
1399 else {
1400 /* key <= a[hint] -- gallop left, until
1401 * a[hint - ofs] < key <= a[hint - lastofs]
1402 */
1403 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1404 while (ofs < maxofs) {
1405 IFLT(*(a-ofs), key)
1406 break;
1407 /* key <= a[hint - ofs] */
1408 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001409 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 }
1412 if (ofs > maxofs)
1413 ofs = maxofs;
1414 /* Translate back to positive offsets relative to &a[0]. */
1415 k = lastofs;
1416 lastofs = hint - ofs;
1417 ofs = hint - k;
1418 }
1419 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1422 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1423 * right of lastofs but no farther right than ofs. Do a binary
1424 * search, with invariant a[lastofs-1] < key <= a[ofs].
1425 */
1426 ++lastofs;
1427 while (lastofs < ofs) {
1428 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 IFLT(a[m], key)
1431 lastofs = m+1; /* a[m] < key */
1432 else
1433 ofs = m; /* key <= a[m] */
1434 }
1435 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1436 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001437
1438fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001440}
1441
1442/*
1443Exactly like gallop_left(), except that if key already exists in a[0:n],
1444finds the position immediately to the right of the rightmost equal value.
1445
1446The return value is the int k in 0..n such that
1447
1448 a[k-1] <= key < a[k]
1449
1450or -1 if error.
1451
1452The code duplication is massive, but this is enough different given that
1453we're sticking to "<" comparisons that it's much harder to follow if
1454written as one routine with yet another "left or right?" flag.
1455*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001456static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001457gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 Py_ssize_t ofs;
1460 Py_ssize_t lastofs;
1461 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 a += hint;
1466 lastofs = 0;
1467 ofs = 1;
1468 IFLT(key, *a) {
1469 /* key < a[hint] -- gallop left, until
1470 * a[hint - ofs] <= key < a[hint - lastofs]
1471 */
1472 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1473 while (ofs < maxofs) {
1474 IFLT(key, *(a-ofs)) {
1475 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001476 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 }
1479 else /* a[hint - ofs] <= key */
1480 break;
1481 }
1482 if (ofs > maxofs)
1483 ofs = maxofs;
1484 /* Translate back to positive offsets relative to &a[0]. */
1485 k = lastofs;
1486 lastofs = hint - ofs;
1487 ofs = hint - k;
1488 }
1489 else {
1490 /* a[hint] <= key -- gallop right, until
1491 * a[hint + lastofs] <= key < a[hint + ofs]
1492 */
1493 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1494 while (ofs < maxofs) {
1495 IFLT(key, a[ofs])
1496 break;
1497 /* a[hint + ofs] <= key */
1498 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001499 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 }
1502 if (ofs > maxofs)
1503 ofs = maxofs;
1504 /* Translate back to offsets relative to &a[0]. */
1505 lastofs += hint;
1506 ofs += hint;
1507 }
1508 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1511 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1512 * right of lastofs but no farther right than ofs. Do a binary
1513 * search, with invariant a[lastofs-1] <= key < a[ofs].
1514 */
1515 ++lastofs;
1516 while (lastofs < ofs) {
1517 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 IFLT(key, a[m])
1520 ofs = m; /* key < a[m] */
1521 else
1522 lastofs = m+1; /* a[m] <= key */
1523 }
1524 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1525 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001526
1527fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001529}
1530
Tim Petersa64dc242002-08-01 02:13:36 +00001531/* Conceptually a MergeState's constructor. */
1532static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001533merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001536 if (has_keyfunc) {
1537 /* The temporary space for merging will need at most half the list
1538 * size rounded up. Use the minimum possible space so we can use the
1539 * rest of temparray for other things. In particular, if there is
1540 * enough extra space, listsort() will use it to store the keys.
1541 */
1542 ms->alloced = (list_size + 1) / 2;
1543
1544 /* ms->alloced describes how many keys will be stored at
1545 ms->temparray, but we also need to store the values. Hence,
1546 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1547 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1548 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1549 ms->a.values = &ms->temparray[ms->alloced];
1550 }
1551 else {
1552 ms->alloced = MERGESTATE_TEMP_SIZE;
1553 ms->a.values = NULL;
1554 }
1555 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 ms->n = 0;
1557 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001558}
1559
1560/* Free all the temp memory owned by the MergeState. This must be called
1561 * when you're done with a MergeState, and may be called before then if
1562 * you want to free the temp memory early.
1563 */
1564static void
1565merge_freemem(MergeState *ms)
1566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001568 if (ms->a.keys != ms->temparray)
1569 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001570}
1571
1572/* Ensure enough temp memory for 'need' array slots is available.
1573 * Returns 0 on success and -1 if the memory can't be gotten.
1574 */
1575static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001576merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001577{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001578 int multiplier;
1579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 assert(ms != NULL);
1581 if (need <= ms->alloced)
1582 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001583
1584 multiplier = ms->a.values != NULL ? 2 : 1;
1585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 /* Don't realloc! That can cost cycles to copy the old data, but
1587 * we don't care what's in the block.
1588 */
1589 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001590 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 PyErr_NoMemory();
1592 return -1;
1593 }
embg1e34da42018-01-28 20:03:23 -07001594 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001595 * sizeof(PyObject *));
1596 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001598 if (ms->a.values != NULL)
1599 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 return 0;
1601 }
1602 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001604}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1606 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001607
Daniel Stutzbach98338222010-12-02 21:55:33 +00001608/* Merge the na elements starting at ssa with the nb elements starting at
1609 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1610 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1611 * should have na <= nb. See listsort.txt for more info. Return 0 if
1612 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001613 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001614static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001615merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1616 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001619 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 int result = -1; /* guilty until proved innocent */
1621 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001622
Daniel Stutzbach98338222010-12-02 21:55:33 +00001623 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1624 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 if (MERGE_GETMEM(ms, na) < 0)
1626 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001627 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1628 dest = ssa;
1629 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001630
Daniel Stutzbach98338222010-12-02 21:55:33 +00001631 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 --nb;
1633 if (nb == 0)
1634 goto Succeed;
1635 if (na == 1)
1636 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 min_gallop = ms->min_gallop;
1639 for (;;) {
1640 Py_ssize_t acount = 0; /* # of times A won in a row */
1641 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 /* Do the straightforward thing until (if ever) one run
1644 * appears to win consistently.
1645 */
1646 for (;;) {
1647 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001648 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if (k) {
1650 if (k < 0)
1651 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001652 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 ++bcount;
1654 acount = 0;
1655 --nb;
1656 if (nb == 0)
1657 goto Succeed;
1658 if (bcount >= min_gallop)
1659 break;
1660 }
1661 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001662 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 ++acount;
1664 bcount = 0;
1665 --na;
1666 if (na == 1)
1667 goto CopyB;
1668 if (acount >= min_gallop)
1669 break;
1670 }
1671 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 /* One run is winning so consistently that galloping may
1674 * be a huge win. So try that, and continue galloping until
1675 * (if ever) neither run appears to be winning consistently
1676 * anymore.
1677 */
1678 ++min_gallop;
1679 do {
1680 assert(na > 1 && nb > 0);
1681 min_gallop -= min_gallop > 1;
1682 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001683 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 acount = k;
1685 if (k) {
1686 if (k < 0)
1687 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001688 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1689 sortslice_advance(&dest, k);
1690 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 na -= k;
1692 if (na == 1)
1693 goto CopyB;
1694 /* na==0 is impossible now if the comparison
1695 * function is consistent, but we can't assume
1696 * that it is.
1697 */
1698 if (na == 0)
1699 goto Succeed;
1700 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001701 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 --nb;
1703 if (nb == 0)
1704 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001705
embg1e34da42018-01-28 20:03:23 -07001706 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 bcount = k;
1708 if (k) {
1709 if (k < 0)
1710 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001711 sortslice_memmove(&dest, 0, &ssb, 0, k);
1712 sortslice_advance(&dest, k);
1713 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 nb -= k;
1715 if (nb == 0)
1716 goto Succeed;
1717 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001718 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 --na;
1720 if (na == 1)
1721 goto CopyB;
1722 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1723 ++min_gallop; /* penalize it for leaving galloping mode */
1724 ms->min_gallop = min_gallop;
1725 }
Tim Petersa64dc242002-08-01 02:13:36 +00001726Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001728Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001730 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001732CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001734 /* The last element of ssa belongs at the end of the merge. */
1735 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1736 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001738}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001739
Daniel Stutzbach98338222010-12-02 21:55:33 +00001740/* Merge the na elements starting at pa with the nb elements starting at
1741 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1742 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1743 * should have na >= nb. See listsort.txt for more info. Return 0 if
1744 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001745 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001746static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001747merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1748 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001751 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001754
Daniel Stutzbach98338222010-12-02 21:55:33 +00001755 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1756 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 if (MERGE_GETMEM(ms, nb) < 0)
1758 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001759 dest = ssb;
1760 sortslice_advance(&dest, nb-1);
1761 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1762 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001764 ssb.keys = ms->a.keys + nb - 1;
1765 if (ssb.values != NULL)
1766 ssb.values = ms->a.values + nb - 1;
1767 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001768
Daniel Stutzbach98338222010-12-02 21:55:33 +00001769 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 --na;
1771 if (na == 0)
1772 goto Succeed;
1773 if (nb == 1)
1774 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 min_gallop = ms->min_gallop;
1777 for (;;) {
1778 Py_ssize_t acount = 0; /* # of times A won in a row */
1779 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 /* Do the straightforward thing until (if ever) one run
1782 * appears to win consistently.
1783 */
1784 for (;;) {
1785 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001786 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (k) {
1788 if (k < 0)
1789 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001790 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 ++acount;
1792 bcount = 0;
1793 --na;
1794 if (na == 0)
1795 goto Succeed;
1796 if (acount >= min_gallop)
1797 break;
1798 }
1799 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001800 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 ++bcount;
1802 acount = 0;
1803 --nb;
1804 if (nb == 1)
1805 goto CopyA;
1806 if (bcount >= min_gallop)
1807 break;
1808 }
1809 }
Tim Petersa64dc242002-08-01 02:13:36 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 /* One run is winning so consistently that galloping may
1812 * be a huge win. So try that, and continue galloping until
1813 * (if ever) neither run appears to be winning consistently
1814 * anymore.
1815 */
1816 ++min_gallop;
1817 do {
1818 assert(na > 0 && nb > 1);
1819 min_gallop -= min_gallop > 1;
1820 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001821 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 if (k < 0)
1823 goto Fail;
1824 k = na - k;
1825 acount = k;
1826 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001827 sortslice_advance(&dest, -k);
1828 sortslice_advance(&ssa, -k);
1829 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 na -= k;
1831 if (na == 0)
1832 goto Succeed;
1833 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001834 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 --nb;
1836 if (nb == 1)
1837 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001838
embg1e34da42018-01-28 20:03:23 -07001839 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 if (k < 0)
1841 goto Fail;
1842 k = nb - k;
1843 bcount = k;
1844 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001845 sortslice_advance(&dest, -k);
1846 sortslice_advance(&ssb, -k);
1847 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 nb -= k;
1849 if (nb == 1)
1850 goto CopyA;
1851 /* nb==0 is impossible now if the comparison
1852 * function is consistent, but we can't assume
1853 * that it is.
1854 */
1855 if (nb == 0)
1856 goto Succeed;
1857 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001858 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 --na;
1860 if (na == 0)
1861 goto Succeed;
1862 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1863 ++min_gallop; /* penalize it for leaving galloping mode */
1864 ms->min_gallop = min_gallop;
1865 }
Tim Petersa64dc242002-08-01 02:13:36 +00001866Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001868Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001870 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001872CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001874 /* The first element of ssb belongs at the front of the merge. */
1875 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1876 sortslice_advance(&dest, -na);
1877 sortslice_advance(&ssa, -na);
1878 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001880}
1881
1882/* Merge the two runs at stack indices i and i+1.
1883 * Returns 0 on success, -1 on error.
1884 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001885static Py_ssize_t
1886merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001887{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001888 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 Py_ssize_t na, nb;
1890 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 assert(ms != NULL);
1893 assert(ms->n >= 2);
1894 assert(i >= 0);
1895 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001896
Daniel Stutzbach98338222010-12-02 21:55:33 +00001897 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001899 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 nb = ms->pending[i+1].len;
1901 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001902 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 /* Record the length of the combined runs; if i is the 3rd-last
1905 * run now, also slide over the last run (which isn't involved
1906 * in this merge). The current run i+1 goes away in any case.
1907 */
1908 ms->pending[i].len = na + nb;
1909 if (i == ms->n - 3)
1910 ms->pending[i+1] = ms->pending[i+2];
1911 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 /* Where does b start in a? Elements in a before that can be
1914 * ignored (already in place).
1915 */
embg1e34da42018-01-28 20:03:23 -07001916 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 if (k < 0)
1918 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001919 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 na -= k;
1921 if (na == 0)
1922 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 /* Where does a end in b? Elements in b after that can be
1925 * ignored (already in place).
1926 */
embg1e34da42018-01-28 20:03:23 -07001927 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 if (nb <= 0)
1929 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 /* Merge what remains of the runs, using a temp array with
1932 * min(na, nb) elements.
1933 */
1934 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001935 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001937 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001938}
1939
1940/* Examine the stack of runs waiting to be merged, merging adjacent runs
1941 * until the stack invariants are re-established:
1942 *
1943 * 1. len[-3] > len[-2] + len[-1]
1944 * 2. len[-2] > len[-1]
1945 *
1946 * See listsort.txt for more info.
1947 *
1948 * Returns 0 on success, -1 on error.
1949 */
1950static int
1951merge_collapse(MergeState *ms)
1952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 assert(ms);
1956 while (ms->n > 1) {
1957 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001958 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1959 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 if (p[n-1].len < p[n+1].len)
1961 --n;
1962 if (merge_at(ms, n) < 0)
1963 return -1;
1964 }
1965 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001966 if (merge_at(ms, n) < 0)
1967 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 }
1969 else
1970 break;
1971 }
1972 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001973}
1974
1975/* Regardless of invariants, merge all runs on the stack until only one
1976 * remains. This is used at the end of the mergesort.
1977 *
1978 * Returns 0 on success, -1 on error.
1979 */
1980static int
1981merge_force_collapse(MergeState *ms)
1982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 assert(ms);
1986 while (ms->n > 1) {
1987 Py_ssize_t n = ms->n - 2;
1988 if (n > 0 && p[n-1].len < p[n+1].len)
1989 --n;
1990 if (merge_at(ms, n) < 0)
1991 return -1;
1992 }
1993 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001994}
1995
1996/* Compute a good value for the minimum run length; natural runs shorter
1997 * than this are boosted artificially via binary insertion.
1998 *
1999 * If n < 64, return n (it's too small to bother with fancy stuff).
2000 * Else if n is an exact power of 2, return 32.
2001 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
2002 * strictly less than, an exact power of 2.
2003 *
2004 * See listsort.txt for more info.
2005 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002006static Py_ssize_t
2007merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00002008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00002010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 assert(n >= 0);
2012 while (n >= 64) {
2013 r |= n & 1;
2014 n >>= 1;
2015 }
2016 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002017}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00002018
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002019static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00002020reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002021{
Daniel Stutzbach98338222010-12-02 21:55:33 +00002022 reverse_slice(s->keys, &s->keys[n]);
2023 if (s->values != NULL)
2024 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002025}
2026
embg1e34da42018-01-28 20:03:23 -07002027/* Here we define custom comparison functions to optimize for the cases one commonly
2028 * encounters in practice: homogeneous lists, often of one of the basic types. */
2029
2030/* This struct holds the comparison function and helper functions
2031 * selected in the pre-sort check. */
2032
2033/* These are the special case compare functions.
2034 * ms->key_compare will always point to one of these: */
2035
2036/* Heterogeneous compare: default, always safe to fall back on. */
2037static int
2038safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2039{
2040 /* No assumptions necessary! */
2041 return PyObject_RichCompareBool(v, w, Py_LT);
2042}
2043
2044/* Homogeneous compare: safe for any two compareable objects of the same type.
2045 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2046 * pre-sort check.)
2047 */
2048static int
2049unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2050{
2051 PyObject *res_obj; int res;
2052
2053 /* No assumptions, because we check first: */
2054 if (v->ob_type->tp_richcompare != ms->key_richcompare)
2055 return PyObject_RichCompareBool(v, w, Py_LT);
2056
2057 assert(ms->key_richcompare != NULL);
2058 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2059
2060 if (res_obj == Py_NotImplemented) {
2061 Py_DECREF(res_obj);
2062 return PyObject_RichCompareBool(v, w, Py_LT);
2063 }
2064 if (res_obj == NULL)
2065 return -1;
2066
2067 if (PyBool_Check(res_obj)) {
2068 res = (res_obj == Py_True);
2069 }
2070 else {
2071 res = PyObject_IsTrue(res_obj);
2072 }
2073 Py_DECREF(res_obj);
2074
2075 /* Note that we can't assert
2076 * res == PyObject_RichCompareBool(v, w, Py_LT);
2077 * because of evil compare functions like this:
2078 * lambda a, b: int(random.random() * 3) - 1)
2079 * (which is actually in test_sort.py) */
2080 return res;
2081}
2082
2083/* Latin string compare: safe for any two latin (one byte per char) strings. */
2084static int
2085unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2086{
Victor Stinner8017b802018-01-29 13:47:06 +01002087 Py_ssize_t len;
2088 int res;
embg1e34da42018-01-28 20:03:23 -07002089
2090 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2091 assert(v->ob_type == w->ob_type);
2092 assert(v->ob_type == &PyUnicode_Type);
2093 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2094 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2095
2096 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2097 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2098
2099 res = (res != 0 ?
2100 res < 0 :
2101 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2102
2103 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2104 return res;
2105}
2106
2107/* Bounded int compare: compare any two longs that fit in a single machine word. */
2108static int
2109unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2110{
2111 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2112
2113 /* Modified from Objects/longobject.c:long_compare, assuming: */
2114 assert(v->ob_type == w->ob_type);
2115 assert(v->ob_type == &PyLong_Type);
2116 assert(Py_ABS(Py_SIZE(v)) <= 1);
2117 assert(Py_ABS(Py_SIZE(w)) <= 1);
2118
2119 vl = (PyLongObject*)v;
2120 wl = (PyLongObject*)w;
2121
2122 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2123 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2124
2125 if (Py_SIZE(vl) < 0)
2126 v0 = -v0;
2127 if (Py_SIZE(wl) < 0)
2128 w0 = -w0;
2129
2130 res = v0 < w0;
2131 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2132 return res;
2133}
2134
2135/* Float compare: compare any two floats. */
2136static int
2137unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2138{
2139 int res;
2140
2141 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2142 assert(v->ob_type == w->ob_type);
2143 assert(v->ob_type == &PyFloat_Type);
2144
2145 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2146 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2147 return res;
2148}
2149
2150/* Tuple compare: compare *any* two tuples, using
2151 * ms->tuple_elem_compare to compare the first elements, which is set
2152 * using the same pre-sort check as we use for ms->key_compare,
2153 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2154 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2155 * that most tuple compares don't involve x[1:]. */
2156static int
2157unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2158{
2159 PyTupleObject *vt, *wt;
2160 Py_ssize_t i, vlen, wlen;
2161 int k;
2162
2163 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2164 assert(v->ob_type == w->ob_type);
2165 assert(v->ob_type == &PyTuple_Type);
2166 assert(Py_SIZE(v) > 0);
2167 assert(Py_SIZE(w) > 0);
2168
2169 vt = (PyTupleObject *)v;
2170 wt = (PyTupleObject *)w;
2171
2172 vlen = Py_SIZE(vt);
2173 wlen = Py_SIZE(wt);
2174
2175 for (i = 0; i < vlen && i < wlen; i++) {
2176 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2177 if (k < 0)
2178 return -1;
2179 if (!k)
2180 break;
2181 }
2182
2183 if (i >= vlen || i >= wlen)
2184 return vlen < wlen;
2185
2186 if (i == 0)
2187 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2188 else
2189 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2190}
2191
Tim Petersa64dc242002-08-01 02:13:36 +00002192/* An adaptive, stable, natural mergesort. See listsort.txt.
2193 * Returns Py_None on success, NULL on error. Even in case of error, the
2194 * list will be some permutation of its input state (nothing is lost or
2195 * duplicated).
2196 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002197/*[clinic input]
2198list.sort
2199
2200 *
2201 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002202 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002203
Tim Hoffmann5c224762019-06-01 06:10:02 +02002204Sort the list in ascending order and return None.
2205
2206The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
2207order of two equal elements is maintained).
2208
2209If a key function is given, apply it once to each list item and sort them,
2210ascending or descending, according to their function values.
2211
2212The reverse flag can be set to sort in descending order.
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002213[clinic start generated code]*/
2214
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002215static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002216list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Tim Hoffmann5c224762019-06-01 06:10:02 +02002217/*[clinic end generated code: output=57b9f9c5e23fbe42 input=cb56cd179a713060]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 Py_ssize_t nremaining;
2221 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002222 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 Py_ssize_t saved_ob_size, saved_allocated;
2224 PyObject **saved_ob_item;
2225 PyObject **final_ob_item;
2226 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002228 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002231 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 if (keyfunc == Py_None)
2233 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 /* The list is temporarily made empty, so that mutations performed
2236 * by comparison functions can't affect the slice of memory we're
2237 * sorting (allowing mutations during sorting is a core-dump
2238 * factory, since ob_item may change).
2239 */
2240 saved_ob_size = Py_SIZE(self);
2241 saved_ob_item = self->ob_item;
2242 saved_allocated = self->allocated;
2243 Py_SIZE(self) = 0;
2244 self->ob_item = NULL;
2245 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002246
Daniel Stutzbach98338222010-12-02 21:55:33 +00002247 if (keyfunc == NULL) {
2248 keys = NULL;
2249 lo.keys = saved_ob_item;
2250 lo.values = NULL;
2251 }
2252 else {
2253 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2254 /* Leverage stack space we allocated but won't otherwise use */
2255 keys = &ms.temparray[saved_ob_size+1];
2256 else {
2257 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002258 if (keys == NULL) {
2259 PyErr_NoMemory();
2260 goto keyfunc_fail;
2261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002263
2264 for (i = 0; i < saved_ob_size ; i++) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002265 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
2266 NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002267 if (keys[i] == NULL) {
2268 for (i=i-1 ; i>=0 ; i--)
2269 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002270 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002271 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002272 goto keyfunc_fail;
2273 }
2274 }
2275
2276 lo.keys = keys;
2277 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002279
embg1e34da42018-01-28 20:03:23 -07002280
2281 /* The pre-sort check: here's where we decide which compare function to use.
2282 * How much optimization is safe? We test for homogeneity with respect to
2283 * several properties that are expensive to check at compare-time, and
2284 * set ms appropriately. */
2285 if (saved_ob_size > 1) {
2286 /* Assume the first element is representative of the whole list. */
2287 int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2288 Py_SIZE(lo.keys[0]) > 0);
2289
2290 PyTypeObject* key_type = (keys_are_in_tuples ?
2291 PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2292 lo.keys[0]->ob_type);
2293
2294 int keys_are_all_same_type = 1;
2295 int strings_are_latin = 1;
2296 int ints_are_bounded = 1;
2297
2298 /* Prove that assumption by checking every key. */
embg1e34da42018-01-28 20:03:23 -07002299 for (i=0; i < saved_ob_size; i++) {
2300
2301 if (keys_are_in_tuples &&
2302 !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2303 keys_are_in_tuples = 0;
2304 keys_are_all_same_type = 0;
2305 break;
2306 }
2307
2308 /* Note: for lists of tuples, key is the first element of the tuple
2309 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2310 * for lists of tuples in the if-statement directly above. */
2311 PyObject *key = (keys_are_in_tuples ?
2312 PyTuple_GET_ITEM(lo.keys[i], 0) :
2313 lo.keys[i]);
2314
2315 if (key->ob_type != key_type) {
2316 keys_are_all_same_type = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002317 /* If keys are in tuple we must loop over the whole list to make
2318 sure all items are tuples */
2319 if (!keys_are_in_tuples) {
2320 break;
2321 }
embg1e34da42018-01-28 20:03:23 -07002322 }
2323
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002324 if (keys_are_all_same_type) {
2325 if (key_type == &PyLong_Type &&
2326 ints_are_bounded &&
2327 Py_ABS(Py_SIZE(key)) > 1) {
2328
embg1e34da42018-01-28 20:03:23 -07002329 ints_are_bounded = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002330 }
2331 else if (key_type == &PyUnicode_Type &&
2332 strings_are_latin &&
2333 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {
2334
2335 strings_are_latin = 0;
2336 }
2337 }
embg1e34da42018-01-28 20:03:23 -07002338 }
embg1e34da42018-01-28 20:03:23 -07002339
2340 /* Choose the best compare, given what we now know about the keys. */
2341 if (keys_are_all_same_type) {
2342
2343 if (key_type == &PyUnicode_Type && strings_are_latin) {
2344 ms.key_compare = unsafe_latin_compare;
2345 }
2346 else if (key_type == &PyLong_Type && ints_are_bounded) {
2347 ms.key_compare = unsafe_long_compare;
2348 }
2349 else if (key_type == &PyFloat_Type) {
2350 ms.key_compare = unsafe_float_compare;
2351 }
2352 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2353 ms.key_compare = unsafe_object_compare;
2354 }
Zackery Spytzebc793d2019-02-21 00:47:14 -07002355 else {
2356 ms.key_compare = safe_object_compare;
2357 }
embg1e34da42018-01-28 20:03:23 -07002358 }
2359 else {
2360 ms.key_compare = safe_object_compare;
2361 }
2362
2363 if (keys_are_in_tuples) {
2364 /* Make sure we're not dealing with tuples of tuples
2365 * (remember: here, key_type refers list [key[0] for key in keys]) */
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002366 if (key_type == &PyTuple_Type) {
embg1e34da42018-01-28 20:03:23 -07002367 ms.tuple_elem_compare = safe_object_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002368 }
2369 else {
embg1e34da42018-01-28 20:03:23 -07002370 ms.tuple_elem_compare = ms.key_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002371 }
embg1e34da42018-01-28 20:03:23 -07002372
2373 ms.key_compare = unsafe_tuple_compare;
2374 }
2375 }
2376 /* End of pre-sort check: ms is now set properly! */
2377
Daniel Stutzbach98338222010-12-02 21:55:33 +00002378 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 nremaining = saved_ob_size;
2381 if (nremaining < 2)
2382 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002383
Benjamin Peterson05380642010-08-23 19:35:39 +00002384 /* Reverse sort stability achieved by initially reversing the list,
2385 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002386 if (reverse) {
2387 if (keys != NULL)
2388 reverse_slice(&keys[0], &keys[saved_ob_size]);
2389 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2390 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 /* March over the array once, left to right, finding natural runs,
2393 * and extending short natural runs to minrun elements.
2394 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 minrun = merge_compute_minrun(nremaining);
2396 do {
2397 int descending;
2398 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002401 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 if (n < 0)
2403 goto fail;
2404 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002405 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 /* If short, extend to min(minrun, nremaining). */
2407 if (n < minrun) {
2408 const Py_ssize_t force = nremaining <= minrun ?
2409 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002410 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 goto fail;
2412 n = force;
2413 }
2414 /* Push run onto pending-runs stack, and maybe merge. */
2415 assert(ms.n < MAX_MERGE_PENDING);
2416 ms.pending[ms.n].base = lo;
2417 ms.pending[ms.n].len = n;
2418 ++ms.n;
2419 if (merge_collapse(&ms) < 0)
2420 goto fail;
2421 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002422 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 nremaining -= n;
2424 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 if (merge_force_collapse(&ms) < 0)
2427 goto fail;
2428 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002429 assert(keys == NULL
2430 ? ms.pending[0].base.keys == saved_ob_item
2431 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002433 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002434
2435succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002437fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002438 if (keys != NULL) {
2439 for (i = 0; i < saved_ob_size; i++)
2440 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002441 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002442 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 if (self->allocated != -1 && result != NULL) {
2446 /* The user mucked with the list during the sort,
2447 * and we don't already have another error to report.
2448 */
2449 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2450 result = NULL;
2451 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 if (reverse && saved_ob_size > 1)
2454 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002457
Daniel Stutzbach98338222010-12-02 21:55:33 +00002458keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 final_ob_item = self->ob_item;
2460 i = Py_SIZE(self);
2461 Py_SIZE(self) = saved_ob_size;
2462 self->ob_item = saved_ob_item;
2463 self->allocated = saved_allocated;
2464 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002465 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 guarantee that the list is really empty when it returns */
2467 while (--i >= 0) {
2468 Py_XDECREF(final_ob_item[i]);
2469 }
2470 PyMem_FREE(final_ob_item);
2471 }
2472 Py_XINCREF(result);
2473 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002474}
Tim Peters330f9e92002-07-19 07:05:44 +00002475#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002476#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002477
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002478int
Fred Drakea2f55112000-07-09 15:16:51 +00002479PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 if (v == NULL || !PyList_Check(v)) {
2482 PyErr_BadInternalCall();
2483 return -1;
2484 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002485 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 if (v == NULL)
2487 return -1;
2488 Py_DECREF(v);
2489 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002490}
2491
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002492/*[clinic input]
2493list.reverse
2494
2495Reverse *IN PLACE*.
2496[clinic start generated code]*/
2497
Guido van Rossumb86c5492001-02-12 22:06:02 +00002498static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002499list_reverse_impl(PyListObject *self)
2500/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 if (Py_SIZE(self) > 1)
2503 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2504 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002505}
2506
Guido van Rossum84c76f51990-10-30 13:32:20 +00002507int
Fred Drakea2f55112000-07-09 15:16:51 +00002508PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 if (v == NULL || !PyList_Check(v)) {
2513 PyErr_BadInternalCall();
2514 return -1;
2515 }
2516 if (Py_SIZE(self) > 1)
2517 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2518 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002519}
2520
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002521PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002522PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 if (v == NULL || !PyList_Check(v)) {
2525 PyErr_BadInternalCall();
2526 return NULL;
2527 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +05002528 return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002529}
2530
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002531/*[clinic input]
2532list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002533
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002534 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002535 start: slice_index(accept={int}) = 0
2536 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002537 /
2538
2539Return first index of value.
2540
2541Raises ValueError if the value is not present.
2542[clinic start generated code]*/
2543
2544static PyObject *
2545list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2546 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002547/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002548{
2549 Py_ssize_t i;
2550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 if (start < 0) {
2552 start += Py_SIZE(self);
2553 if (start < 0)
2554 start = 0;
2555 }
2556 if (stop < 0) {
2557 stop += Py_SIZE(self);
2558 if (stop < 0)
2559 stop = 0;
2560 }
2561 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Miss Islington (bot)fcaf14c2019-12-30 11:51:06 -08002562 PyObject *obj = self->ob_item[i];
2563 Py_INCREF(obj);
2564 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2565 Py_DECREF(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 if (cmp > 0)
2567 return PyLong_FromSsize_t(i);
2568 else if (cmp < 0)
2569 return NULL;
2570 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002571 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002573}
2574
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002575/*[clinic input]
2576list.count
2577
2578 value: object
2579 /
2580
2581Return number of occurrences of value.
2582[clinic start generated code]*/
2583
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002584static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002585list_count(PyListObject *self, PyObject *value)
2586/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 Py_ssize_t count = 0;
2589 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 for (i = 0; i < Py_SIZE(self); i++) {
Miss Islington (bot)fcaf14c2019-12-30 11:51:06 -08002592 PyObject *obj = self->ob_item[i];
Miss Islington (bot)fdb21602020-01-22 10:11:22 -08002593 if (obj == value) {
2594 count++;
2595 continue;
2596 }
Miss Islington (bot)fcaf14c2019-12-30 11:51:06 -08002597 Py_INCREF(obj);
2598 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2599 Py_DECREF(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 if (cmp > 0)
2601 count++;
2602 else if (cmp < 0)
2603 return NULL;
2604 }
2605 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002606}
2607
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002608/*[clinic input]
2609list.remove
2610
2611 value: object
2612 /
2613
2614Remove first occurrence of value.
2615
2616Raises ValueError if the value is not present.
2617[clinic start generated code]*/
2618
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002619static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002620list_remove(PyListObject *self, PyObject *value)
2621/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 for (i = 0; i < Py_SIZE(self); i++) {
Miss Islington (bot)fcaf14c2019-12-30 11:51:06 -08002626 PyObject *obj = self->ob_item[i];
2627 Py_INCREF(obj);
2628 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
2629 Py_DECREF(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 if (cmp > 0) {
2631 if (list_ass_slice(self, i, i+1,
2632 (PyObject *)NULL) == 0)
2633 Py_RETURN_NONE;
2634 return NULL;
2635 }
2636 else if (cmp < 0)
2637 return NULL;
2638 }
2639 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2640 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002641}
2642
Jeremy Hylton8caad492000-06-23 14:18:11 +00002643static int
2644list_traverse(PyListObject *o, visitproc visit, void *arg)
2645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 for (i = Py_SIZE(o); --i >= 0; )
2649 Py_VISIT(o->ob_item[i]);
2650 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002651}
2652
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002653static PyObject *
2654list_richcompare(PyObject *v, PyObject *w, int op)
2655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 PyListObject *vl, *wl;
2657 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002658
Brian Curtindfc80e32011-08-10 20:28:54 -05002659 if (!PyList_Check(v) || !PyList_Check(w))
2660 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 vl = (PyListObject *)v;
2663 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2666 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002668 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 else
stratakise8b19652017-11-02 11:32:54 +01002670 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 /* Search for the first index where items are different */
2674 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
Dong-hee Na2ee87912019-12-31 13:15:36 +09002675 PyObject *vitem = vl->ob_item[i];
2676 PyObject *witem = wl->ob_item[i];
2677 if (vitem == witem) {
2678 continue;
2679 }
2680
2681 Py_INCREF(vitem);
2682 Py_INCREF(witem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 int k = PyObject_RichCompareBool(vl->ob_item[i],
2684 wl->ob_item[i], Py_EQ);
Dong-hee Na2ee87912019-12-31 13:15:36 +09002685 Py_DECREF(vitem);
2686 Py_DECREF(witem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 if (k < 0)
2688 return NULL;
2689 if (!k)
2690 break;
2691 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2694 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002695 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 /* We have an item that differs -- shortcuts for EQ/NE */
2699 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002700 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 }
2702 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002703 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 /* Compare the final item again using the proper operator */
2707 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002708}
2709
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002710/*[clinic input]
2711list.__init__
2712
2713 iterable: object(c_default="NULL") = ()
2714 /
2715
2716Built-in mutable sequence.
2717
2718If no argument is given, the constructor creates a new empty list.
2719The argument must be an iterable if specified.
2720[clinic start generated code]*/
2721
Tim Peters6d6c1a32001-08-02 04:15:00 +00002722static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002723list___init___impl(PyListObject *self, PyObject *iterable)
2724/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 /* Verify list invariants established by PyType_GenericAlloc() */
2727 assert(0 <= Py_SIZE(self));
2728 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2729 assert(self->ob_item != NULL ||
2730 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 /* Empty previous contents */
2733 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002734 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002736 if (iterable != NULL) {
Pablo Galindo372d7052018-10-28 20:16:26 +00002737 if (_PyObject_HasLen(iterable)) {
2738 Py_ssize_t iter_len = PyObject_Size(iterable);
2739 if (iter_len == -1) {
2740 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2741 return -1;
2742 }
2743 PyErr_Clear();
2744 }
2745 if (iter_len > 0 && self->ob_item == NULL
2746 && list_preallocate_exact(self, iter_len)) {
2747 return -1;
2748 }
2749 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002750 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 if (rv == NULL)
2752 return -1;
2753 Py_DECREF(rv);
2754 }
2755 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002756}
2757
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002758/*[clinic input]
2759list.__sizeof__
2760
2761Return the size of the list in memory, in bytes.
2762[clinic start generated code]*/
2763
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002764static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002765list___sizeof___impl(PyListObject *self)
2766/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002769
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002770 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002772}
2773
Raymond Hettinger1021c442003-11-07 15:38:09 +00002774static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002775static PyObject *list_subscript(PyListObject*, PyObject*);
2776
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002777static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002778 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2779 LIST___REVERSED___METHODDEF
2780 LIST___SIZEOF___METHODDEF
2781 LIST_CLEAR_METHODDEF
2782 LIST_COPY_METHODDEF
2783 LIST_APPEND_METHODDEF
2784 LIST_INSERT_METHODDEF
2785 LIST_EXTEND_METHODDEF
2786 LIST_POP_METHODDEF
2787 LIST_REMOVE_METHODDEF
2788 LIST_INDEX_METHODDEF
2789 LIST_COUNT_METHODDEF
2790 LIST_REVERSE_METHODDEF
2791 LIST_SORT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002793};
2794
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002795static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 (lenfunc)list_length, /* sq_length */
2797 (binaryfunc)list_concat, /* sq_concat */
2798 (ssizeargfunc)list_repeat, /* sq_repeat */
2799 (ssizeargfunc)list_item, /* sq_item */
2800 0, /* sq_slice */
2801 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2802 0, /* sq_ass_slice */
2803 (objobjproc)list_contains, /* sq_contains */
2804 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2805 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002806};
2807
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002808static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002809list_subscript(PyListObject* self, PyObject* item)
2810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 if (PyIndex_Check(item)) {
2812 Py_ssize_t i;
2813 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2814 if (i == -1 && PyErr_Occurred())
2815 return NULL;
2816 if (i < 0)
2817 i += PyList_GET_SIZE(self);
2818 return list_item(self, i);
2819 }
2820 else if (PySlice_Check(item)) {
2821 Py_ssize_t start, stop, step, slicelength, cur, i;
2822 PyObject* result;
2823 PyObject* it;
2824 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002825
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002826 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 return NULL;
2828 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002829 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2830 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 if (slicelength <= 0) {
2833 return PyList_New(0);
2834 }
2835 else if (step == 1) {
2836 return list_slice(self, start, stop);
2837 }
2838 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002839 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 src = self->ob_item;
2843 dest = ((PyListObject *)result)->ob_item;
2844 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002845 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 it = src[cur];
2847 Py_INCREF(it);
2848 dest[i] = it;
2849 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002850 Py_SIZE(result) = slicelength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 return result;
2852 }
2853 }
2854 else {
2855 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002856 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 item->ob_type->tp_name);
2858 return NULL;
2859 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002860}
2861
Tim Peters3b01a122002-07-19 02:35:45 +00002862static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002863list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 if (PyIndex_Check(item)) {
2866 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2867 if (i == -1 && PyErr_Occurred())
2868 return -1;
2869 if (i < 0)
2870 i += PyList_GET_SIZE(self);
2871 return list_ass_item(self, i, value);
2872 }
2873 else if (PySlice_Check(item)) {
2874 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002875
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002876 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 return -1;
2878 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002879 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2880 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 if (step == 1)
2883 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 /* Make sure s[5:2] = [..] inserts at the right place:
2886 before 5, not before 2. */
2887 if ((step < 0 && start < stop) ||
2888 (step > 0 && start > stop))
2889 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 if (value == NULL) {
2892 /* delete slice */
2893 PyObject **garbage;
2894 size_t cur;
2895 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002896 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 if (slicelength <= 0)
2899 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 if (step < 0) {
2902 stop = start + 1;
2903 start = stop + step*(slicelength - 1) - 1;
2904 step = -step;
2905 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 garbage = (PyObject**)
2908 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2909 if (!garbage) {
2910 PyErr_NoMemory();
2911 return -1;
2912 }
Tim Peters3b01a122002-07-19 02:35:45 +00002913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 /* drawing pictures might help understand these for
2915 loops. Basically, we memmove the parts of the
2916 list that are *not* part of the slice: step-1
2917 items for each item that is part of the slice,
2918 and then tail end of the list that was not
2919 covered by the slice */
2920 for (cur = start, i = 0;
2921 cur < (size_t)stop;
2922 cur += step, i++) {
2923 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 if (cur + step >= (size_t)Py_SIZE(self)) {
2928 lim = Py_SIZE(self) - cur - 1;
2929 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 memmove(self->ob_item + cur - i,
2932 self->ob_item + cur + 1,
2933 lim * sizeof(PyObject *));
2934 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002935 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 if (cur < (size_t)Py_SIZE(self)) {
2937 memmove(self->ob_item + cur - slicelength,
2938 self->ob_item + cur,
2939 (Py_SIZE(self) - cur) *
2940 sizeof(PyObject *));
2941 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002944 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 for (i = 0; i < slicelength; i++) {
2947 Py_DECREF(garbage[i]);
2948 }
2949 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002950
Victor Stinner35f28032013-11-21 12:16:35 +01002951 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 }
2953 else {
2954 /* assign slice */
2955 PyObject *ins, *seq;
2956 PyObject **garbage, **seqitems, **selfitems;
2957 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 /* protect against a[::-1] = a */
2960 if (self == (PyListObject*)value) {
2961 seq = list_slice((PyListObject*)value, 0,
2962 PyList_GET_SIZE(value));
2963 }
2964 else {
2965 seq = PySequence_Fast(value,
2966 "must assign iterable "
2967 "to extended slice");
2968 }
2969 if (!seq)
2970 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2973 PyErr_Format(PyExc_ValueError,
2974 "attempt to assign sequence of "
2975 "size %zd to extended slice of "
2976 "size %zd",
2977 PySequence_Fast_GET_SIZE(seq),
2978 slicelength);
2979 Py_DECREF(seq);
2980 return -1;
2981 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 if (!slicelength) {
2984 Py_DECREF(seq);
2985 return 0;
2986 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 garbage = (PyObject**)
2989 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2990 if (!garbage) {
2991 Py_DECREF(seq);
2992 PyErr_NoMemory();
2993 return -1;
2994 }
Tim Peters3b01a122002-07-19 02:35:45 +00002995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 selfitems = self->ob_item;
2997 seqitems = PySequence_Fast_ITEMS(seq);
2998 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002999 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 garbage[i] = selfitems[cur];
3001 ins = seqitems[i];
3002 Py_INCREF(ins);
3003 selfitems[cur] = ins;
3004 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 for (i = 0; i < slicelength; i++) {
3007 Py_DECREF(garbage[i]);
3008 }
Tim Peters3b01a122002-07-19 02:35:45 +00003009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 PyMem_FREE(garbage);
3011 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00003012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 return 0;
3014 }
3015 }
3016 else {
3017 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04003018 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 item->ob_type->tp_name);
3020 return -1;
3021 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003022}
3023
3024static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 (lenfunc)list_length,
3026 (binaryfunc)list_subscript,
3027 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003028};
3029
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003030PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3032 "list",
3033 sizeof(PyListObject),
3034 0,
3035 (destructor)list_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003036 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 0, /* tp_getattr */
3038 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003039 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 (reprfunc)list_repr, /* tp_repr */
3041 0, /* tp_as_number */
3042 &list_as_sequence, /* tp_as_sequence */
3043 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003044 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 0, /* tp_call */
3046 0, /* tp_str */
3047 PyObject_GenericGetAttr, /* tp_getattro */
3048 0, /* tp_setattro */
3049 0, /* tp_as_buffer */
3050 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003051 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
3052 list___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 (traverseproc)list_traverse, /* tp_traverse */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003054 (inquiry)_list_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 list_richcompare, /* tp_richcompare */
3056 0, /* tp_weaklistoffset */
3057 list_iter, /* tp_iter */
3058 0, /* tp_iternext */
3059 list_methods, /* tp_methods */
3060 0, /* tp_members */
3061 0, /* tp_getset */
3062 0, /* tp_base */
3063 0, /* tp_dict */
3064 0, /* tp_descr_get */
3065 0, /* tp_descr_set */
3066 0, /* tp_dictoffset */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003067 (initproc)list___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 PyType_GenericAlloc, /* tp_alloc */
3069 PyType_GenericNew, /* tp_new */
3070 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003071};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00003072
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003073/*********************** List Iterator **************************/
3074
3075typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02003077 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003079} listiterobject;
3080
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003081static void listiter_dealloc(listiterobject *);
3082static int listiter_traverse(listiterobject *, visitproc, void *);
3083static PyObject *listiter_next(listiterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303084static PyObject *listiter_len(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003085static PyObject *listiter_reduce_general(void *_it, int forward);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303086static PyObject *listiter_reduce(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003087static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00003088
Armin Rigof5b3e362006-02-11 21:32:43 +00003089PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003090PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3091PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003092
3093static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003095 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3096 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00003098};
3099
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003100PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3102 "list_iterator", /* tp_name */
3103 sizeof(listiterobject), /* tp_basicsize */
3104 0, /* tp_itemsize */
3105 /* methods */
3106 (destructor)listiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003107 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 0, /* tp_getattr */
3109 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003110 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 0, /* tp_repr */
3112 0, /* tp_as_number */
3113 0, /* tp_as_sequence */
3114 0, /* tp_as_mapping */
3115 0, /* tp_hash */
3116 0, /* tp_call */
3117 0, /* tp_str */
3118 PyObject_GenericGetAttr, /* tp_getattro */
3119 0, /* tp_setattro */
3120 0, /* tp_as_buffer */
3121 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3122 0, /* tp_doc */
3123 (traverseproc)listiter_traverse, /* tp_traverse */
3124 0, /* tp_clear */
3125 0, /* tp_richcompare */
3126 0, /* tp_weaklistoffset */
3127 PyObject_SelfIter, /* tp_iter */
3128 (iternextfunc)listiter_next, /* tp_iternext */
3129 listiter_methods, /* tp_methods */
3130 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003131};
Raymond Hettinger1021c442003-11-07 15:38:09 +00003132
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003133
3134static PyObject *
3135list_iter(PyObject *seq)
3136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 if (!PyList_Check(seq)) {
3140 PyErr_BadInternalCall();
3141 return NULL;
3142 }
3143 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3144 if (it == NULL)
3145 return NULL;
3146 it->it_index = 0;
3147 Py_INCREF(seq);
3148 it->it_seq = (PyListObject *)seq;
3149 _PyObject_GC_TRACK(it);
3150 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003151}
3152
3153static void
3154listiter_dealloc(listiterobject *it)
3155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 _PyObject_GC_UNTRACK(it);
3157 Py_XDECREF(it->it_seq);
3158 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003159}
3160
3161static int
3162listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 Py_VISIT(it->it_seq);
3165 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003166}
3167
3168static PyObject *
3169listiter_next(listiterobject *it)
3170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 PyListObject *seq;
3172 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 assert(it != NULL);
3175 seq = it->it_seq;
3176 if (seq == NULL)
3177 return NULL;
3178 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 if (it->it_index < PyList_GET_SIZE(seq)) {
3181 item = PyList_GET_ITEM(seq, it->it_index);
3182 ++it->it_index;
3183 Py_INCREF(item);
3184 return item;
3185 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003188 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003190}
3191
3192static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303193listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 Py_ssize_t len;
3196 if (it->it_seq) {
3197 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3198 if (len >= 0)
3199 return PyLong_FromSsize_t(len);
3200 }
3201 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003202}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003203
3204static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303205listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003206{
3207 return listiter_reduce_general(it, 1);
3208}
3209
3210static PyObject *
3211listiter_setstate(listiterobject *it, PyObject *state)
3212{
Victor Stinner7660b882013-06-24 23:59:24 +02003213 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003214 if (index == -1 && PyErr_Occurred())
3215 return NULL;
3216 if (it->it_seq != NULL) {
3217 if (index < 0)
3218 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003219 else if (index > PyList_GET_SIZE(it->it_seq))
3220 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003221 it->it_index = index;
3222 }
3223 Py_RETURN_NONE;
3224}
3225
Raymond Hettinger1021c442003-11-07 15:38:09 +00003226/*********************** List Reverse Iterator **************************/
3227
3228typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 PyObject_HEAD
3230 Py_ssize_t it_index;
3231 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00003232} listreviterobject;
3233
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003234static void listreviter_dealloc(listreviterobject *);
3235static int listreviter_traverse(listreviterobject *, visitproc, void *);
3236static PyObject *listreviter_next(listreviterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303237static PyObject *listreviter_len(listreviterobject *, PyObject *);
3238static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003239static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003240
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003241static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003243 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3244 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003246};
3247
Raymond Hettinger1021c442003-11-07 15:38:09 +00003248PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3250 "list_reverseiterator", /* tp_name */
3251 sizeof(listreviterobject), /* tp_basicsize */
3252 0, /* tp_itemsize */
3253 /* methods */
3254 (destructor)listreviter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003255 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 0, /* tp_getattr */
3257 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003258 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 0, /* tp_repr */
3260 0, /* tp_as_number */
3261 0, /* tp_as_sequence */
3262 0, /* tp_as_mapping */
3263 0, /* tp_hash */
3264 0, /* tp_call */
3265 0, /* tp_str */
3266 PyObject_GenericGetAttr, /* tp_getattro */
3267 0, /* tp_setattro */
3268 0, /* tp_as_buffer */
3269 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3270 0, /* tp_doc */
3271 (traverseproc)listreviter_traverse, /* tp_traverse */
3272 0, /* tp_clear */
3273 0, /* tp_richcompare */
3274 0, /* tp_weaklistoffset */
3275 PyObject_SelfIter, /* tp_iter */
3276 (iternextfunc)listreviter_next, /* tp_iternext */
3277 listreviter_methods, /* tp_methods */
3278 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00003279};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003280
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003281/*[clinic input]
3282list.__reversed__
3283
3284Return a reverse iterator over the list.
3285[clinic start generated code]*/
3286
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003287static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003288list___reversed___impl(PyListObject *self)
3289/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3294 if (it == NULL)
3295 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003296 assert(PyList_Check(self));
3297 it->it_index = PyList_GET_SIZE(self) - 1;
3298 Py_INCREF(self);
3299 it->it_seq = self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 PyObject_GC_Track(it);
3301 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003302}
3303
3304static void
3305listreviter_dealloc(listreviterobject *it)
3306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 PyObject_GC_UnTrack(it);
3308 Py_XDECREF(it->it_seq);
3309 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003310}
3311
3312static int
3313listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 Py_VISIT(it->it_seq);
3316 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003317}
3318
3319static PyObject *
3320listreviter_next(listreviterobject *it)
3321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003323 Py_ssize_t index;
3324 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003325
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003326 assert(it != NULL);
3327 seq = it->it_seq;
3328 if (seq == NULL) {
3329 return NULL;
3330 }
3331 assert(PyList_Check(seq));
3332
3333 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 if (index>=0 && index < PyList_GET_SIZE(seq)) {
3335 item = PyList_GET_ITEM(seq, index);
3336 it->it_index--;
3337 Py_INCREF(item);
3338 return item;
3339 }
3340 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003341 it->it_seq = NULL;
3342 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003344}
3345
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003346static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303347listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 Py_ssize_t len = it->it_index + 1;
3350 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3351 len = 0;
3352 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003353}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003354
3355static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303356listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003357{
3358 return listiter_reduce_general(it, 0);
3359}
3360
3361static PyObject *
3362listreviter_setstate(listreviterobject *it, PyObject *state)
3363{
3364 Py_ssize_t index = PyLong_AsSsize_t(state);
3365 if (index == -1 && PyErr_Occurred())
3366 return NULL;
3367 if (it->it_seq != NULL) {
3368 if (index < -1)
3369 index = -1;
3370 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3371 index = PyList_GET_SIZE(it->it_seq) - 1;
3372 it->it_index = index;
3373 }
3374 Py_RETURN_NONE;
3375}
3376
3377/* common pickling support */
3378
3379static PyObject *
3380listiter_reduce_general(void *_it, int forward)
3381{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003382 _Py_IDENTIFIER(iter);
3383 _Py_IDENTIFIER(reversed);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003384 PyObject *list;
3385
3386 /* the objects are not the same, index is of different types! */
3387 if (forward) {
3388 listiterobject *it = (listiterobject *)_it;
3389 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003390 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003391 it->it_seq, it->it_index);
3392 } else {
3393 listreviterobject *it = (listreviterobject *)_it;
3394 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003395 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_reversed),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003396 it->it_seq, it->it_index);
3397 }
3398 /* empty iterator, create an empty list */
3399 list = PyList_New(0);
3400 if (list == NULL)
3401 return NULL;
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003402 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003403}