blob: f8bf45e5f8cda262d263d173b6b20ec873a5d96c [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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 Py_ssize_t i;
449 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
452 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
453 Py_EQ);
454 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000455}
456
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000458list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700460 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (indexerr == NULL) {
462 indexerr = PyUnicode_FromString(
463 "list index out of range");
464 if (indexerr == NULL)
465 return NULL;
466 }
467 PyErr_SetObject(PyExc_IndexError, indexerr);
468 return NULL;
469 }
470 Py_INCREF(a->ob_item[i]);
471 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472}
473
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000474static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000475list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 PyListObject *np;
478 PyObject **src, **dest;
479 Py_ssize_t i, len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500481 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (np == NULL)
483 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 src = a->ob_item + ilow;
486 dest = np->ob_item;
487 for (i = 0; i < len; i++) {
488 PyObject *v = src[i];
489 Py_INCREF(v);
490 dest[i] = v;
491 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500492 Py_SIZE(np) = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494}
495
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000497PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (!PyList_Check(a)) {
500 PyErr_BadInternalCall();
501 return NULL;
502 }
Sergey Fedoseevef1b88b2019-02-21 11:51:52 +0500503 if (ilow < 0) {
504 ilow = 0;
505 }
506 else if (ilow > Py_SIZE(a)) {
507 ilow = Py_SIZE(a);
508 }
509 if (ihigh < ilow) {
510 ihigh = ilow;
511 }
512 else if (ihigh > Py_SIZE(a)) {
513 ihigh = Py_SIZE(a);
514 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000516}
517
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000518static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000519list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 Py_ssize_t size;
522 Py_ssize_t i;
523 PyObject **src, **dest;
524 PyListObject *np;
525 if (!PyList_Check(bb)) {
526 PyErr_Format(PyExc_TypeError,
527 "can only concatenate list (not \"%.200s\") to list",
528 bb->ob_type->tp_name);
529 return NULL;
530 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000531#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000532 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000534 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500535 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 if (np == NULL) {
537 return NULL;
538 }
539 src = a->ob_item;
540 dest = np->ob_item;
541 for (i = 0; i < Py_SIZE(a); i++) {
542 PyObject *v = src[i];
543 Py_INCREF(v);
544 dest[i] = v;
545 }
546 src = b->ob_item;
547 dest = np->ob_item + Py_SIZE(a);
548 for (i = 0; i < Py_SIZE(b); i++) {
549 PyObject *v = src[i];
550 Py_INCREF(v);
551 dest[i] = v;
552 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500553 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555#undef b
556}
557
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000559list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 Py_ssize_t i, j;
562 Py_ssize_t size;
563 PyListObject *np;
564 PyObject **p, **items;
565 PyObject *elem;
566 if (n < 0)
567 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100568 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100570 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 if (size == 0)
572 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500573 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 if (np == NULL)
575 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500578 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 elem = a->ob_item[0];
580 for (i = 0; i < n; i++) {
581 items[i] = elem;
582 Py_INCREF(elem);
583 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500585 else {
586 p = np->ob_item;
587 items = a->ob_item;
588 for (i = 0; i < n; i++) {
589 for (j = 0; j < Py_SIZE(a); j++) {
590 *p = items[j];
591 Py_INCREF(*p);
592 p++;
593 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 }
595 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500596 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000598}
599
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000600static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200601_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 Py_ssize_t i;
604 PyObject **item = a->ob_item;
605 if (item != NULL) {
606 /* Because XDECREF can recursively invoke operations on
607 this list, we make it empty first. */
608 i = Py_SIZE(a);
609 Py_SIZE(a) = 0;
610 a->ob_item = NULL;
611 a->allocated = 0;
612 while (--i >= 0) {
613 Py_XDECREF(item[i]);
614 }
615 PyMem_FREE(item);
616 }
617 /* Never fails; the return value can be ignored.
618 Note that there is no guarantee that the list is actually empty
619 at this point, because XDECREF may have populated it again! */
620 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000621}
622
Tim Peters8fc4a912004-07-31 21:53:19 +0000623/* a[ilow:ihigh] = v if v != NULL.
624 * del a[ilow:ihigh] if v == NULL.
625 *
626 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
627 * guaranteed the call cannot fail.
628 */
Armin Rigo93677f02004-07-29 12:40:23 +0000629static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000630list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 /* Because [X]DECREF can recursively invoke list operations on
633 this list, we must postpone all [X]DECREF activity until
634 after the list is back in its canonical shape. Therefore
635 we must allocate an additional array, 'recycle', into which
636 we temporarily copy the items that are deleted from the
637 list. :-( */
638 PyObject *recycle_on_stack[8];
639 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
640 PyObject **item;
641 PyObject **vitem = NULL;
642 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
643 Py_ssize_t n; /* # of elements in replacement list */
644 Py_ssize_t norig; /* # of elements in list getting replaced */
645 Py_ssize_t d; /* Change in size */
646 Py_ssize_t k;
647 size_t s;
648 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 if (v == NULL)
651 n = 0;
652 else {
653 if (a == b) {
654 /* Special case "a[i:j] = a" -- copy b first */
655 v = list_slice(b, 0, Py_SIZE(b));
656 if (v == NULL)
657 return result;
658 result = list_ass_slice(a, ilow, ihigh, v);
659 Py_DECREF(v);
660 return result;
661 }
662 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
663 if(v_as_SF == NULL)
664 goto Error;
665 n = PySequence_Fast_GET_SIZE(v_as_SF);
666 vitem = PySequence_Fast_ITEMS(v_as_SF);
667 }
668 if (ilow < 0)
669 ilow = 0;
670 else if (ilow > Py_SIZE(a))
671 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 if (ihigh < ilow)
674 ihigh = ilow;
675 else if (ihigh > Py_SIZE(a))
676 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 norig = ihigh - ilow;
679 assert(norig >= 0);
680 d = n - norig;
681 if (Py_SIZE(a) + d == 0) {
682 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200683 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 }
685 item = a->ob_item;
686 /* recycle the items that we are about to remove */
687 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700688 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
689 if (s) {
690 if (s > sizeof(recycle_on_stack)) {
691 recycle = (PyObject **)PyMem_MALLOC(s);
692 if (recycle == NULL) {
693 PyErr_NoMemory();
694 goto Error;
695 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700697 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200701 Py_ssize_t tail;
702 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
703 memmove(&item[ihigh+d], &item[ihigh], tail);
704 if (list_resize(a, Py_SIZE(a) + d) < 0) {
705 memmove(&item[ihigh], &item[ihigh+d], tail);
706 memcpy(&item[ilow], recycle, s);
707 goto Error;
708 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 item = a->ob_item;
710 }
711 else if (d > 0) { /* Insert d items */
712 k = Py_SIZE(a);
713 if (list_resize(a, k+d) < 0)
714 goto Error;
715 item = a->ob_item;
716 memmove(&item[ihigh+d], &item[ihigh],
717 (k - ihigh)*sizeof(PyObject *));
718 }
719 for (k = 0; k < n; k++, ilow++) {
720 PyObject *w = vitem[k];
721 Py_XINCREF(w);
722 item[ilow] = w;
723 }
724 for (k = norig - 1; k >= 0; --k)
725 Py_XDECREF(recycle[k]);
726 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000727 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 if (recycle != recycle_on_stack)
729 PyMem_FREE(recycle);
730 Py_XDECREF(v_as_SF);
731 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000732#undef b
733}
734
Guido van Rossum234f9421993-06-17 12:35:49 +0000735int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000736PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 if (!PyList_Check(a)) {
739 PyErr_BadInternalCall();
740 return -1;
741 }
742 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000743}
744
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000745static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000746list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 PyObject **items;
749 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000750
751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 size = PyList_GET_SIZE(self);
753 if (size == 0 || n == 1) {
754 Py_INCREF(self);
755 return (PyObject *)self;
756 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 if (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200759 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 Py_INCREF(self);
761 return (PyObject *)self;
762 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 if (size > PY_SSIZE_T_MAX / n) {
765 return PyErr_NoMemory();
766 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000767
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800768 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 p = size;
772 items = self->ob_item;
773 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
774 for (j = 0; j < size; j++) {
775 PyObject *o = items[j];
776 Py_INCREF(o);
777 items[p++] = o;
778 }
779 }
780 Py_INCREF(self);
781 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000782}
783
Guido van Rossum4a450d01991-04-03 19:05:18 +0000784static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000785list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000786{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700787 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyErr_SetString(PyExc_IndexError,
789 "list assignment index out of range");
790 return -1;
791 }
792 if (v == NULL)
793 return list_ass_slice(a, i, i+1, v);
794 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300795 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000797}
798
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200799/*[clinic input]
800list.insert
801
802 index: Py_ssize_t
803 object: object
804 /
805
806Insert object before index.
807[clinic start generated code]*/
808
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000809static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200810list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
811/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000812{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200813 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 Py_RETURN_NONE;
815 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816}
817
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200818/*[clinic input]
819list.clear
820
821Remove all items from list.
822[clinic start generated code]*/
823
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000824static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200825list_clear_impl(PyListObject *self)
826/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000827{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200828 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000829 Py_RETURN_NONE;
830}
831
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200832/*[clinic input]
833list.copy
834
835Return a shallow copy of the list.
836[clinic start generated code]*/
837
Eli Benderskycbbaa962011-02-25 05:47:53 +0000838static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200839list_copy_impl(PyListObject *self)
840/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000841{
842 return list_slice(self, 0, Py_SIZE(self));
843}
844
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200845/*[clinic input]
846list.append
847
848 object: object
849 /
850
851Append object to the end of the list.
852[clinic start generated code]*/
853
Eli Benderskycbbaa962011-02-25 05:47:53 +0000854static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200855list_append(PyListObject *self, PyObject *object)
856/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000857{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200858 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 Py_RETURN_NONE;
860 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000861}
862
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200863/*[clinic input]
864list.extend
865
866 iterable: object
867 /
868
869Extend list by appending elements from the iterable.
870[clinic start generated code]*/
871
Barry Warsawdedf6d61998-10-09 16:37:25 +0000872static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200873list_extend(PyListObject *self, PyObject *iterable)
874/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 PyObject *it; /* iter(v) */
877 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200878 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 Py_ssize_t mn; /* m + n */
880 Py_ssize_t i;
881 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 /* Special cases:
884 1) lists and tuples which can use PySequence_Fast ops
885 2) extending self to self requires making a copy first
886 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200887 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
888 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200890 iterable = PySequence_Fast(iterable, "argument must be iterable");
891 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200893 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200895 /* short circuit when iterable is empty */
896 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 Py_RETURN_NONE;
898 }
899 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000900 /* It should not be possible to allocate a list large enough to cause
901 an overflow on any relevant platform */
902 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800903 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200904 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 return NULL;
906 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200907 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 * situation a.extend(a), but the following code works
909 * in that case too. Just make sure to resize self
910 * before calling PySequence_Fast_ITEMS.
911 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200912 /* populate the end of self with iterable's items */
913 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 dest = self->ob_item + m;
915 for (i = 0; i < n; i++) {
916 PyObject *o = src[i];
917 Py_INCREF(o);
918 dest[i] = o;
919 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200920 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 Py_RETURN_NONE;
922 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000923
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200924 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if (it == NULL)
926 return NULL;
927 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200930 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800931 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 Py_DECREF(it);
933 return NULL;
934 }
935 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000936 if (m > PY_SSIZE_T_MAX - n) {
937 /* m + n overflowed; on the chance that n lied, and there really
938 * is enough room, ignore it. If n was telling the truth, we'll
939 * eventually run out of memory during the loop.
940 */
941 }
942 else {
943 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800945 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 goto error;
947 /* Make the list sane again. */
948 Py_SIZE(self) = m;
949 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 /* Run iterator to exhaustion. */
952 for (;;) {
953 PyObject *item = iternext(it);
954 if (item == NULL) {
955 if (PyErr_Occurred()) {
956 if (PyErr_ExceptionMatches(PyExc_StopIteration))
957 PyErr_Clear();
958 else
959 goto error;
960 }
961 break;
962 }
963 if (Py_SIZE(self) < self->allocated) {
964 /* steals ref */
965 PyList_SET_ITEM(self, Py_SIZE(self), item);
966 ++Py_SIZE(self);
967 }
968 else {
969 int status = app1(self, item);
970 Py_DECREF(item); /* append creates a new ref */
971 if (status < 0)
972 goto error;
973 }
974 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200977 if (Py_SIZE(self) < self->allocated) {
978 if (list_resize(self, Py_SIZE(self)) < 0)
979 goto error;
980 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 Py_DECREF(it);
983 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000984
985 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 Py_DECREF(it);
987 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000988}
989
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000990PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200991_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000992{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200993 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000994}
995
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000996static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000997list_inplace_concat(PyListObject *self, PyObject *other)
998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001000
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001001 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 if (result == NULL)
1003 return result;
1004 Py_DECREF(result);
1005 Py_INCREF(self);
1006 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001007}
1008
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001009/*[clinic input]
1010list.pop
1011
1012 index: Py_ssize_t = -1
1013 /
1014
1015Remove and return item at index (default last).
1016
1017Raises IndexError if list is empty or index is out of range.
1018[clinic start generated code]*/
1019
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001020static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001021list_pop_impl(PyListObject *self, Py_ssize_t index)
1022/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 PyObject *v;
1025 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 if (Py_SIZE(self) == 0) {
1028 /* Special-case most common failure cause */
1029 PyErr_SetString(PyExc_IndexError, "pop from empty list");
1030 return NULL;
1031 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001032 if (index < 0)
1033 index += Py_SIZE(self);
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -07001034 if (!valid_index(index, Py_SIZE(self))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1036 return NULL;
1037 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001038 v = self->ob_item[index];
1039 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001041 if (status >= 0)
1042 return v; /* and v now owns the reference the list had */
1043 else
1044 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 }
1046 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001047 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001048 if (status < 0) {
1049 Py_DECREF(v);
1050 return NULL;
1051 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001053}
1054
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001055/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1056static void
1057reverse_slice(PyObject **lo, PyObject **hi)
1058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 --hi;
1062 while (lo < hi) {
1063 PyObject *t = *lo;
1064 *lo = *hi;
1065 *hi = t;
1066 ++lo;
1067 --hi;
1068 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001069}
1070
Tim Petersa64dc242002-08-01 02:13:36 +00001071/* Lots of code for an adaptive, stable, natural mergesort. There are many
1072 * pieces to this algorithm; read listsort.txt for overviews and details.
1073 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001074
Daniel Stutzbach98338222010-12-02 21:55:33 +00001075/* A sortslice contains a pointer to an array of keys and a pointer to
1076 * an array of corresponding values. In other words, keys[i]
1077 * corresponds with values[i]. If values == NULL, then the keys are
1078 * also the values.
1079 *
1080 * Several convenience routines are provided here, so that keys and
1081 * values are always moved in sync.
1082 */
1083
1084typedef struct {
1085 PyObject **keys;
1086 PyObject **values;
1087} sortslice;
1088
1089Py_LOCAL_INLINE(void)
1090sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1091{
1092 s1->keys[i] = s2->keys[j];
1093 if (s1->values != NULL)
1094 s1->values[i] = s2->values[j];
1095}
1096
1097Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001098sortslice_copy_incr(sortslice *dst, sortslice *src)
1099{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001100 *dst->keys++ = *src->keys++;
1101 if (dst->values != NULL)
1102 *dst->values++ = *src->values++;
1103}
1104
1105Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001106sortslice_copy_decr(sortslice *dst, sortslice *src)
1107{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001108 *dst->keys-- = *src->keys--;
1109 if (dst->values != NULL)
1110 *dst->values-- = *src->values--;
1111}
1112
1113
1114Py_LOCAL_INLINE(void)
1115sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001116 Py_ssize_t n)
1117{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001118 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1119 if (s1->values != NULL)
1120 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1121}
1122
1123Py_LOCAL_INLINE(void)
1124sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001125 Py_ssize_t n)
1126{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001127 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1128 if (s1->values != NULL)
1129 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1130}
1131
1132Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001133sortslice_advance(sortslice *slice, Py_ssize_t n)
1134{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001135 slice->keys += n;
1136 if (slice->values != NULL)
1137 slice->values += n;
1138}
1139
embg1e34da42018-01-28 20:03:23 -07001140/* Comparison function: ms->key_compare, which is set at run-time in
1141 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001142 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1143 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001144
embg1e34da42018-01-28 20:03:23 -07001145#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001146
1147/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001148 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1149 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1150*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001151#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001153
embg1e34da42018-01-28 20:03:23 -07001154/* The maximum number of entries in a MergeState's pending-runs stack.
1155 * This is enough to sort arrays of size up to about
1156 * 32 * phi ** MAX_MERGE_PENDING
1157 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1158 * with 2**64 elements.
1159 */
1160#define MAX_MERGE_PENDING 85
1161
1162/* When we get into galloping mode, we stay there until both runs win less
1163 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1164 */
1165#define MIN_GALLOP 7
1166
1167/* Avoid malloc for small temp arrays. */
1168#define MERGESTATE_TEMP_SIZE 256
1169
1170/* One MergeState exists on the stack per invocation of mergesort. It's just
1171 * a convenient way to pass state around among the helper functions.
1172 */
1173struct s_slice {
1174 sortslice base;
1175 Py_ssize_t len;
1176};
1177
1178typedef struct s_MergeState MergeState;
1179struct s_MergeState {
1180 /* This controls when we get *into* galloping mode. It's initialized
1181 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1182 * random data, and lower for highly structured data.
1183 */
1184 Py_ssize_t min_gallop;
1185
1186 /* 'a' is temp storage to help with merges. It contains room for
1187 * alloced entries.
1188 */
1189 sortslice a; /* may point to temparray below */
1190 Py_ssize_t alloced;
1191
1192 /* A stack of n pending runs yet to be merged. Run #i starts at
1193 * address base[i] and extends for len[i] elements. It's always
1194 * true (so long as the indices are in bounds) that
1195 *
1196 * pending[i].base + pending[i].len == pending[i+1].base
1197 *
1198 * so we could cut the storage for this, but it's a minor amount,
1199 * and keeping all the info explicit simplifies the code.
1200 */
1201 int n;
1202 struct s_slice pending[MAX_MERGE_PENDING];
1203
1204 /* 'a' points to this when possible, rather than muck with malloc. */
1205 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1206
1207 /* This is the function we will use to compare two keys,
1208 * even when none of our special cases apply and we have to use
1209 * safe_object_compare. */
1210 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1211
1212 /* This function is used by unsafe_object_compare to optimize comparisons
1213 * when we know our list is type-homogeneous but we can't assume anything else.
1214 * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1215 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1216
1217 /* This function is used by unsafe_tuple_compare to compare the first elements
1218 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1219 * we can assume more, and use one of the special-case compares. */
1220 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1221};
1222
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001223/* binarysort is the best method for sorting small arrays: it does
1224 few compares, but can do data movement quadratic in the number of
1225 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001226 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001227 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001228 On entry, must have lo <= start <= hi, and that [lo, start) is already
1229 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001230 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001231 Even in case of error, the output slice will be some permutation of
1232 the input (nothing is lost or duplicated).
1233*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001234static int
embg1e34da42018-01-28 20:03:23 -07001235binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001236{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001237 Py_ssize_t k;
1238 PyObject **l, **p, **r;
1239 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001240
Daniel Stutzbach98338222010-12-02 21:55:33 +00001241 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001243 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 ++start;
1245 for (; start < hi; ++start) {
1246 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001247 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 r = start;
1249 pivot = *r;
1250 /* Invariants:
1251 * pivot >= all in [lo, l).
1252 * pivot < all in [r, start).
1253 * The second is vacuously true at the start.
1254 */
1255 assert(l < r);
1256 do {
1257 p = l + ((r - l) >> 1);
1258 IFLT(pivot, *p)
1259 r = p;
1260 else
1261 l = p+1;
1262 } while (l < r);
1263 assert(l == r);
1264 /* The invariants still hold, so pivot >= all in [lo, l) and
1265 pivot < all in [l, start), so pivot belongs at l. Note
1266 that if there are elements equal to pivot, l points to the
1267 first slot after them -- that's why this sort is stable.
1268 Slide over to make room.
1269 Caution: using memmove is much slower under MSVC 5;
1270 we're not usually moving many slots. */
1271 for (p = start; p > l; --p)
1272 *p = *(p-1);
1273 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001274 if (lo.values != NULL) {
1275 Py_ssize_t offset = lo.values - lo.keys;
1276 p = start + offset;
1277 pivot = *p;
1278 l += offset;
1279 for (p = start + offset; p > l; --p)
1280 *p = *(p-1);
1281 *l = pivot;
1282 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 }
1284 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001285
1286 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001288}
1289
Tim Petersa64dc242002-08-01 02:13:36 +00001290/*
1291Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1292is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001293
Tim Petersa64dc242002-08-01 02:13:36 +00001294 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001295
Tim Petersa64dc242002-08-01 02:13:36 +00001296or the longest descending 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] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001299
Tim Petersa64dc242002-08-01 02:13:36 +00001300Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1301For its intended use in a stable mergesort, the strictness of the defn of
1302"descending" is needed so that the caller can safely reverse a descending
1303sequence without violating stability (strict > ensures there are no equal
1304elements to get out of order).
1305
1306Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001307*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001308static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001309count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 Py_ssize_t k;
1312 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 assert(lo < hi);
1315 *descending = 0;
1316 ++lo;
1317 if (lo == hi)
1318 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 n = 2;
1321 IFLT(*lo, *(lo-1)) {
1322 *descending = 1;
1323 for (lo = lo+1; lo < hi; ++lo, ++n) {
1324 IFLT(*lo, *(lo-1))
1325 ;
1326 else
1327 break;
1328 }
1329 }
1330 else {
1331 for (lo = lo+1; lo < hi; ++lo, ++n) {
1332 IFLT(*lo, *(lo-1))
1333 break;
1334 }
1335 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001338fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001340}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001341
Tim Petersa64dc242002-08-01 02:13:36 +00001342/*
1343Locate the proper position of key in a sorted vector; if the vector contains
1344an element equal to key, return the position immediately to the left of
1345the leftmost equal element. [gallop_right() does the same except returns
1346the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001347
Tim Petersa64dc242002-08-01 02:13:36 +00001348"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001349
Tim Petersa64dc242002-08-01 02:13:36 +00001350"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1351hint is to the final result, the faster this runs.
1352
1353The return value is the int k in 0..n such that
1354
1355 a[k-1] < key <= a[k]
1356
1357pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1358key belongs at index k; or, IOW, the first k elements of a should precede
1359key, and the last n-k should follow key.
1360
1361Returns -1 on error. See listsort.txt for info on the method.
1362*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001363static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001364gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 Py_ssize_t ofs;
1367 Py_ssize_t lastofs;
1368 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 a += hint;
1373 lastofs = 0;
1374 ofs = 1;
1375 IFLT(*a, key) {
1376 /* a[hint] < key -- gallop right, until
1377 * a[hint + lastofs] < key <= a[hint + ofs]
1378 */
1379 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1380 while (ofs < maxofs) {
1381 IFLT(a[ofs], key) {
1382 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001383 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 }
1386 else /* key <= a[hint + ofs] */
1387 break;
1388 }
1389 if (ofs > maxofs)
1390 ofs = maxofs;
1391 /* Translate back to offsets relative to &a[0]. */
1392 lastofs += hint;
1393 ofs += hint;
1394 }
1395 else {
1396 /* key <= a[hint] -- gallop left, until
1397 * a[hint - ofs] < key <= a[hint - lastofs]
1398 */
1399 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1400 while (ofs < maxofs) {
1401 IFLT(*(a-ofs), key)
1402 break;
1403 /* key <= a[hint - ofs] */
1404 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001405 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 }
1408 if (ofs > maxofs)
1409 ofs = maxofs;
1410 /* Translate back to positive offsets relative to &a[0]. */
1411 k = lastofs;
1412 lastofs = hint - ofs;
1413 ofs = hint - k;
1414 }
1415 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1418 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1419 * right of lastofs but no farther right than ofs. Do a binary
1420 * search, with invariant a[lastofs-1] < key <= a[ofs].
1421 */
1422 ++lastofs;
1423 while (lastofs < ofs) {
1424 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 IFLT(a[m], key)
1427 lastofs = m+1; /* a[m] < key */
1428 else
1429 ofs = m; /* key <= a[m] */
1430 }
1431 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1432 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001433
1434fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001436}
1437
1438/*
1439Exactly like gallop_left(), except that if key already exists in a[0:n],
1440finds the position immediately to the right of the rightmost equal value.
1441
1442The return value is the int k in 0..n such that
1443
1444 a[k-1] <= key < a[k]
1445
1446or -1 if error.
1447
1448The code duplication is massive, but this is enough different given that
1449we're sticking to "<" comparisons that it's much harder to follow if
1450written as one routine with yet another "left or right?" flag.
1451*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001452static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001453gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 Py_ssize_t ofs;
1456 Py_ssize_t lastofs;
1457 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 a += hint;
1462 lastofs = 0;
1463 ofs = 1;
1464 IFLT(key, *a) {
1465 /* key < a[hint] -- gallop left, until
1466 * a[hint - ofs] <= key < a[hint - lastofs]
1467 */
1468 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1469 while (ofs < maxofs) {
1470 IFLT(key, *(a-ofs)) {
1471 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001472 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 }
1475 else /* a[hint - ofs] <= key */
1476 break;
1477 }
1478 if (ofs > maxofs)
1479 ofs = maxofs;
1480 /* Translate back to positive offsets relative to &a[0]. */
1481 k = lastofs;
1482 lastofs = hint - ofs;
1483 ofs = hint - k;
1484 }
1485 else {
1486 /* a[hint] <= key -- gallop right, until
1487 * a[hint + lastofs] <= key < a[hint + ofs]
1488 */
1489 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1490 while (ofs < maxofs) {
1491 IFLT(key, a[ofs])
1492 break;
1493 /* a[hint + ofs] <= key */
1494 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001495 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 }
1498 if (ofs > maxofs)
1499 ofs = maxofs;
1500 /* Translate back to offsets relative to &a[0]. */
1501 lastofs += hint;
1502 ofs += hint;
1503 }
1504 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1507 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1508 * right of lastofs but no farther right than ofs. Do a binary
1509 * search, with invariant a[lastofs-1] <= key < a[ofs].
1510 */
1511 ++lastofs;
1512 while (lastofs < ofs) {
1513 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 IFLT(key, a[m])
1516 ofs = m; /* key < a[m] */
1517 else
1518 lastofs = m+1; /* a[m] <= key */
1519 }
1520 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1521 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001522
1523fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001525}
1526
Tim Petersa64dc242002-08-01 02:13:36 +00001527/* Conceptually a MergeState's constructor. */
1528static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001529merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001532 if (has_keyfunc) {
1533 /* The temporary space for merging will need at most half the list
1534 * size rounded up. Use the minimum possible space so we can use the
1535 * rest of temparray for other things. In particular, if there is
1536 * enough extra space, listsort() will use it to store the keys.
1537 */
1538 ms->alloced = (list_size + 1) / 2;
1539
1540 /* ms->alloced describes how many keys will be stored at
1541 ms->temparray, but we also need to store the values. Hence,
1542 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1543 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1544 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1545 ms->a.values = &ms->temparray[ms->alloced];
1546 }
1547 else {
1548 ms->alloced = MERGESTATE_TEMP_SIZE;
1549 ms->a.values = NULL;
1550 }
1551 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 ms->n = 0;
1553 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001554}
1555
1556/* Free all the temp memory owned by the MergeState. This must be called
1557 * when you're done with a MergeState, and may be called before then if
1558 * you want to free the temp memory early.
1559 */
1560static void
1561merge_freemem(MergeState *ms)
1562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001564 if (ms->a.keys != ms->temparray)
1565 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001566}
1567
1568/* Ensure enough temp memory for 'need' array slots is available.
1569 * Returns 0 on success and -1 if the memory can't be gotten.
1570 */
1571static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001572merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001573{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001574 int multiplier;
1575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 assert(ms != NULL);
1577 if (need <= ms->alloced)
1578 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001579
1580 multiplier = ms->a.values != NULL ? 2 : 1;
1581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 /* Don't realloc! That can cost cycles to copy the old data, but
1583 * we don't care what's in the block.
1584 */
1585 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001586 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 PyErr_NoMemory();
1588 return -1;
1589 }
embg1e34da42018-01-28 20:03:23 -07001590 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001591 * sizeof(PyObject *));
1592 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001594 if (ms->a.values != NULL)
1595 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 return 0;
1597 }
1598 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001600}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1602 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001603
Daniel Stutzbach98338222010-12-02 21:55:33 +00001604/* Merge the na elements starting at ssa with the nb elements starting at
1605 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1606 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1607 * should have na <= nb. See listsort.txt for more info. Return 0 if
1608 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001609 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001610static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001611merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1612 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001615 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 int result = -1; /* guilty until proved innocent */
1617 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001618
Daniel Stutzbach98338222010-12-02 21:55:33 +00001619 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1620 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 if (MERGE_GETMEM(ms, na) < 0)
1622 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001623 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1624 dest = ssa;
1625 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001626
Daniel Stutzbach98338222010-12-02 21:55:33 +00001627 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 --nb;
1629 if (nb == 0)
1630 goto Succeed;
1631 if (na == 1)
1632 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 min_gallop = ms->min_gallop;
1635 for (;;) {
1636 Py_ssize_t acount = 0; /* # of times A won in a row */
1637 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 /* Do the straightforward thing until (if ever) one run
1640 * appears to win consistently.
1641 */
1642 for (;;) {
1643 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001644 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 if (k) {
1646 if (k < 0)
1647 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001648 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 ++bcount;
1650 acount = 0;
1651 --nb;
1652 if (nb == 0)
1653 goto Succeed;
1654 if (bcount >= min_gallop)
1655 break;
1656 }
1657 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001658 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 ++acount;
1660 bcount = 0;
1661 --na;
1662 if (na == 1)
1663 goto CopyB;
1664 if (acount >= min_gallop)
1665 break;
1666 }
1667 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 /* One run is winning so consistently that galloping may
1670 * be a huge win. So try that, and continue galloping until
1671 * (if ever) neither run appears to be winning consistently
1672 * anymore.
1673 */
1674 ++min_gallop;
1675 do {
1676 assert(na > 1 && nb > 0);
1677 min_gallop -= min_gallop > 1;
1678 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001679 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 acount = k;
1681 if (k) {
1682 if (k < 0)
1683 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001684 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1685 sortslice_advance(&dest, k);
1686 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 na -= k;
1688 if (na == 1)
1689 goto CopyB;
1690 /* na==0 is impossible now if the comparison
1691 * function is consistent, but we can't assume
1692 * that it is.
1693 */
1694 if (na == 0)
1695 goto Succeed;
1696 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001697 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 --nb;
1699 if (nb == 0)
1700 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001701
embg1e34da42018-01-28 20:03:23 -07001702 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 bcount = k;
1704 if (k) {
1705 if (k < 0)
1706 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001707 sortslice_memmove(&dest, 0, &ssb, 0, k);
1708 sortslice_advance(&dest, k);
1709 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 nb -= k;
1711 if (nb == 0)
1712 goto Succeed;
1713 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001714 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 --na;
1716 if (na == 1)
1717 goto CopyB;
1718 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1719 ++min_gallop; /* penalize it for leaving galloping mode */
1720 ms->min_gallop = min_gallop;
1721 }
Tim Petersa64dc242002-08-01 02:13:36 +00001722Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001724Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001726 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001728CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001730 /* The last element of ssa belongs at the end of the merge. */
1731 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1732 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001734}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001735
Daniel Stutzbach98338222010-12-02 21:55:33 +00001736/* Merge the na elements starting at pa with the nb elements starting at
1737 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1738 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1739 * should have na >= nb. See listsort.txt for more info. Return 0 if
1740 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001741 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001742static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001743merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1744 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001747 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001750
Daniel Stutzbach98338222010-12-02 21:55:33 +00001751 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1752 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if (MERGE_GETMEM(ms, nb) < 0)
1754 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001755 dest = ssb;
1756 sortslice_advance(&dest, nb-1);
1757 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1758 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001760 ssb.keys = ms->a.keys + nb - 1;
1761 if (ssb.values != NULL)
1762 ssb.values = ms->a.values + nb - 1;
1763 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001764
Daniel Stutzbach98338222010-12-02 21:55:33 +00001765 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 --na;
1767 if (na == 0)
1768 goto Succeed;
1769 if (nb == 1)
1770 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 min_gallop = ms->min_gallop;
1773 for (;;) {
1774 Py_ssize_t acount = 0; /* # of times A won in a row */
1775 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 /* Do the straightforward thing until (if ever) one run
1778 * appears to win consistently.
1779 */
1780 for (;;) {
1781 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001782 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 if (k) {
1784 if (k < 0)
1785 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001786 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 ++acount;
1788 bcount = 0;
1789 --na;
1790 if (na == 0)
1791 goto Succeed;
1792 if (acount >= min_gallop)
1793 break;
1794 }
1795 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001796 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 ++bcount;
1798 acount = 0;
1799 --nb;
1800 if (nb == 1)
1801 goto CopyA;
1802 if (bcount >= min_gallop)
1803 break;
1804 }
1805 }
Tim Petersa64dc242002-08-01 02:13:36 +00001806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 /* One run is winning so consistently that galloping may
1808 * be a huge win. So try that, and continue galloping until
1809 * (if ever) neither run appears to be winning consistently
1810 * anymore.
1811 */
1812 ++min_gallop;
1813 do {
1814 assert(na > 0 && nb > 1);
1815 min_gallop -= min_gallop > 1;
1816 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001817 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 if (k < 0)
1819 goto Fail;
1820 k = na - k;
1821 acount = k;
1822 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001823 sortslice_advance(&dest, -k);
1824 sortslice_advance(&ssa, -k);
1825 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 na -= k;
1827 if (na == 0)
1828 goto Succeed;
1829 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001830 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 --nb;
1832 if (nb == 1)
1833 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001834
embg1e34da42018-01-28 20:03:23 -07001835 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 if (k < 0)
1837 goto Fail;
1838 k = nb - k;
1839 bcount = k;
1840 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001841 sortslice_advance(&dest, -k);
1842 sortslice_advance(&ssb, -k);
1843 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 nb -= k;
1845 if (nb == 1)
1846 goto CopyA;
1847 /* nb==0 is impossible now if the comparison
1848 * function is consistent, but we can't assume
1849 * that it is.
1850 */
1851 if (nb == 0)
1852 goto Succeed;
1853 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001854 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 --na;
1856 if (na == 0)
1857 goto Succeed;
1858 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1859 ++min_gallop; /* penalize it for leaving galloping mode */
1860 ms->min_gallop = min_gallop;
1861 }
Tim Petersa64dc242002-08-01 02:13:36 +00001862Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001864Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001866 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001868CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001870 /* The first element of ssb belongs at the front of the merge. */
1871 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1872 sortslice_advance(&dest, -na);
1873 sortslice_advance(&ssa, -na);
1874 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001876}
1877
1878/* Merge the two runs at stack indices i and i+1.
1879 * Returns 0 on success, -1 on error.
1880 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001881static Py_ssize_t
1882merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001883{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001884 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 Py_ssize_t na, nb;
1886 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 assert(ms != NULL);
1889 assert(ms->n >= 2);
1890 assert(i >= 0);
1891 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001892
Daniel Stutzbach98338222010-12-02 21:55:33 +00001893 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001895 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 nb = ms->pending[i+1].len;
1897 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001898 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 /* Record the length of the combined runs; if i is the 3rd-last
1901 * run now, also slide over the last run (which isn't involved
1902 * in this merge). The current run i+1 goes away in any case.
1903 */
1904 ms->pending[i].len = na + nb;
1905 if (i == ms->n - 3)
1906 ms->pending[i+1] = ms->pending[i+2];
1907 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 /* Where does b start in a? Elements in a before that can be
1910 * ignored (already in place).
1911 */
embg1e34da42018-01-28 20:03:23 -07001912 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 if (k < 0)
1914 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001915 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 na -= k;
1917 if (na == 0)
1918 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 /* Where does a end in b? Elements in b after that can be
1921 * ignored (already in place).
1922 */
embg1e34da42018-01-28 20:03:23 -07001923 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 if (nb <= 0)
1925 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 /* Merge what remains of the runs, using a temp array with
1928 * min(na, nb) elements.
1929 */
1930 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001931 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001933 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001934}
1935
1936/* Examine the stack of runs waiting to be merged, merging adjacent runs
1937 * until the stack invariants are re-established:
1938 *
1939 * 1. len[-3] > len[-2] + len[-1]
1940 * 2. len[-2] > len[-1]
1941 *
1942 * See listsort.txt for more info.
1943 *
1944 * Returns 0 on success, -1 on error.
1945 */
1946static int
1947merge_collapse(MergeState *ms)
1948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 assert(ms);
1952 while (ms->n > 1) {
1953 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001954 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1955 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 if (p[n-1].len < p[n+1].len)
1957 --n;
1958 if (merge_at(ms, n) < 0)
1959 return -1;
1960 }
1961 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001962 if (merge_at(ms, n) < 0)
1963 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 }
1965 else
1966 break;
1967 }
1968 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001969}
1970
1971/* Regardless of invariants, merge all runs on the stack until only one
1972 * remains. This is used at the end of the mergesort.
1973 *
1974 * Returns 0 on success, -1 on error.
1975 */
1976static int
1977merge_force_collapse(MergeState *ms)
1978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 assert(ms);
1982 while (ms->n > 1) {
1983 Py_ssize_t n = ms->n - 2;
1984 if (n > 0 && p[n-1].len < p[n+1].len)
1985 --n;
1986 if (merge_at(ms, n) < 0)
1987 return -1;
1988 }
1989 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001990}
1991
1992/* Compute a good value for the minimum run length; natural runs shorter
1993 * than this are boosted artificially via binary insertion.
1994 *
1995 * If n < 64, return n (it's too small to bother with fancy stuff).
1996 * Else if n is an exact power of 2, return 32.
1997 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1998 * strictly less than, an exact power of 2.
1999 *
2000 * See listsort.txt for more info.
2001 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002002static Py_ssize_t
2003merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00002004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00002006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 assert(n >= 0);
2008 while (n >= 64) {
2009 r |= n & 1;
2010 n >>= 1;
2011 }
2012 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002013}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00002014
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002015static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00002016reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002017{
Daniel Stutzbach98338222010-12-02 21:55:33 +00002018 reverse_slice(s->keys, &s->keys[n]);
2019 if (s->values != NULL)
2020 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002021}
2022
embg1e34da42018-01-28 20:03:23 -07002023/* Here we define custom comparison functions to optimize for the cases one commonly
2024 * encounters in practice: homogeneous lists, often of one of the basic types. */
2025
2026/* This struct holds the comparison function and helper functions
2027 * selected in the pre-sort check. */
2028
2029/* These are the special case compare functions.
2030 * ms->key_compare will always point to one of these: */
2031
2032/* Heterogeneous compare: default, always safe to fall back on. */
2033static int
2034safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2035{
2036 /* No assumptions necessary! */
2037 return PyObject_RichCompareBool(v, w, Py_LT);
2038}
2039
2040/* Homogeneous compare: safe for any two compareable objects of the same type.
2041 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2042 * pre-sort check.)
2043 */
2044static int
2045unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2046{
2047 PyObject *res_obj; int res;
2048
2049 /* No assumptions, because we check first: */
2050 if (v->ob_type->tp_richcompare != ms->key_richcompare)
2051 return PyObject_RichCompareBool(v, w, Py_LT);
2052
2053 assert(ms->key_richcompare != NULL);
2054 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2055
2056 if (res_obj == Py_NotImplemented) {
2057 Py_DECREF(res_obj);
2058 return PyObject_RichCompareBool(v, w, Py_LT);
2059 }
2060 if (res_obj == NULL)
2061 return -1;
2062
2063 if (PyBool_Check(res_obj)) {
2064 res = (res_obj == Py_True);
2065 }
2066 else {
2067 res = PyObject_IsTrue(res_obj);
2068 }
2069 Py_DECREF(res_obj);
2070
2071 /* Note that we can't assert
2072 * res == PyObject_RichCompareBool(v, w, Py_LT);
2073 * because of evil compare functions like this:
2074 * lambda a, b: int(random.random() * 3) - 1)
2075 * (which is actually in test_sort.py) */
2076 return res;
2077}
2078
2079/* Latin string compare: safe for any two latin (one byte per char) strings. */
2080static int
2081unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2082{
Victor Stinner8017b802018-01-29 13:47:06 +01002083 Py_ssize_t len;
2084 int res;
embg1e34da42018-01-28 20:03:23 -07002085
2086 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2087 assert(v->ob_type == w->ob_type);
2088 assert(v->ob_type == &PyUnicode_Type);
2089 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2090 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2091
2092 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2093 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2094
2095 res = (res != 0 ?
2096 res < 0 :
2097 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2098
2099 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2100 return res;
2101}
2102
2103/* Bounded int compare: compare any two longs that fit in a single machine word. */
2104static int
2105unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2106{
2107 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2108
2109 /* Modified from Objects/longobject.c:long_compare, assuming: */
2110 assert(v->ob_type == w->ob_type);
2111 assert(v->ob_type == &PyLong_Type);
2112 assert(Py_ABS(Py_SIZE(v)) <= 1);
2113 assert(Py_ABS(Py_SIZE(w)) <= 1);
2114
2115 vl = (PyLongObject*)v;
2116 wl = (PyLongObject*)w;
2117
2118 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2119 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2120
2121 if (Py_SIZE(vl) < 0)
2122 v0 = -v0;
2123 if (Py_SIZE(wl) < 0)
2124 w0 = -w0;
2125
2126 res = v0 < w0;
2127 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2128 return res;
2129}
2130
2131/* Float compare: compare any two floats. */
2132static int
2133unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2134{
2135 int res;
2136
2137 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2138 assert(v->ob_type == w->ob_type);
2139 assert(v->ob_type == &PyFloat_Type);
2140
2141 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2142 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2143 return res;
2144}
2145
2146/* Tuple compare: compare *any* two tuples, using
2147 * ms->tuple_elem_compare to compare the first elements, which is set
2148 * using the same pre-sort check as we use for ms->key_compare,
2149 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2150 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2151 * that most tuple compares don't involve x[1:]. */
2152static int
2153unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2154{
2155 PyTupleObject *vt, *wt;
2156 Py_ssize_t i, vlen, wlen;
2157 int k;
2158
2159 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2160 assert(v->ob_type == w->ob_type);
2161 assert(v->ob_type == &PyTuple_Type);
2162 assert(Py_SIZE(v) > 0);
2163 assert(Py_SIZE(w) > 0);
2164
2165 vt = (PyTupleObject *)v;
2166 wt = (PyTupleObject *)w;
2167
2168 vlen = Py_SIZE(vt);
2169 wlen = Py_SIZE(wt);
2170
2171 for (i = 0; i < vlen && i < wlen; i++) {
2172 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2173 if (k < 0)
2174 return -1;
2175 if (!k)
2176 break;
2177 }
2178
2179 if (i >= vlen || i >= wlen)
2180 return vlen < wlen;
2181
2182 if (i == 0)
2183 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2184 else
2185 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2186}
2187
Tim Petersa64dc242002-08-01 02:13:36 +00002188/* An adaptive, stable, natural mergesort. See listsort.txt.
2189 * Returns Py_None on success, NULL on error. Even in case of error, the
2190 * list will be some permutation of its input state (nothing is lost or
2191 * duplicated).
2192 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002193/*[clinic input]
2194list.sort
2195
2196 *
2197 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002198 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002199
Tim Hoffmann5c224762019-06-01 06:10:02 +02002200Sort the list in ascending order and return None.
2201
2202The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
2203order of two equal elements is maintained).
2204
2205If a key function is given, apply it once to each list item and sort them,
2206ascending or descending, according to their function values.
2207
2208The reverse flag can be set to sort in descending order.
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002209[clinic start generated code]*/
2210
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002211static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002212list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Tim Hoffmann5c224762019-06-01 06:10:02 +02002213/*[clinic end generated code: output=57b9f9c5e23fbe42 input=cb56cd179a713060]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 Py_ssize_t nremaining;
2217 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002218 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 Py_ssize_t saved_ob_size, saved_allocated;
2220 PyObject **saved_ob_item;
2221 PyObject **final_ob_item;
2222 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002224 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002227 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 if (keyfunc == Py_None)
2229 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 /* The list is temporarily made empty, so that mutations performed
2232 * by comparison functions can't affect the slice of memory we're
2233 * sorting (allowing mutations during sorting is a core-dump
2234 * factory, since ob_item may change).
2235 */
2236 saved_ob_size = Py_SIZE(self);
2237 saved_ob_item = self->ob_item;
2238 saved_allocated = self->allocated;
2239 Py_SIZE(self) = 0;
2240 self->ob_item = NULL;
2241 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002242
Daniel Stutzbach98338222010-12-02 21:55:33 +00002243 if (keyfunc == NULL) {
2244 keys = NULL;
2245 lo.keys = saved_ob_item;
2246 lo.values = NULL;
2247 }
2248 else {
2249 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2250 /* Leverage stack space we allocated but won't otherwise use */
2251 keys = &ms.temparray[saved_ob_size+1];
2252 else {
2253 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002254 if (keys == NULL) {
2255 PyErr_NoMemory();
2256 goto keyfunc_fail;
2257 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002259
2260 for (i = 0; i < saved_ob_size ; i++) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002261 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
2262 NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002263 if (keys[i] == NULL) {
2264 for (i=i-1 ; i>=0 ; i--)
2265 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002266 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002267 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002268 goto keyfunc_fail;
2269 }
2270 }
2271
2272 lo.keys = keys;
2273 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002275
embg1e34da42018-01-28 20:03:23 -07002276
2277 /* The pre-sort check: here's where we decide which compare function to use.
2278 * How much optimization is safe? We test for homogeneity with respect to
2279 * several properties that are expensive to check at compare-time, and
2280 * set ms appropriately. */
2281 if (saved_ob_size > 1) {
2282 /* Assume the first element is representative of the whole list. */
2283 int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2284 Py_SIZE(lo.keys[0]) > 0);
2285
2286 PyTypeObject* key_type = (keys_are_in_tuples ?
2287 PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2288 lo.keys[0]->ob_type);
2289
2290 int keys_are_all_same_type = 1;
2291 int strings_are_latin = 1;
2292 int ints_are_bounded = 1;
2293
2294 /* Prove that assumption by checking every key. */
embg1e34da42018-01-28 20:03:23 -07002295 for (i=0; i < saved_ob_size; i++) {
2296
2297 if (keys_are_in_tuples &&
2298 !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2299 keys_are_in_tuples = 0;
2300 keys_are_all_same_type = 0;
2301 break;
2302 }
2303
2304 /* Note: for lists of tuples, key is the first element of the tuple
2305 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2306 * for lists of tuples in the if-statement directly above. */
2307 PyObject *key = (keys_are_in_tuples ?
2308 PyTuple_GET_ITEM(lo.keys[i], 0) :
2309 lo.keys[i]);
2310
2311 if (key->ob_type != key_type) {
2312 keys_are_all_same_type = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002313 /* If keys are in tuple we must loop over the whole list to make
2314 sure all items are tuples */
2315 if (!keys_are_in_tuples) {
2316 break;
2317 }
embg1e34da42018-01-28 20:03:23 -07002318 }
2319
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002320 if (keys_are_all_same_type) {
2321 if (key_type == &PyLong_Type &&
2322 ints_are_bounded &&
2323 Py_ABS(Py_SIZE(key)) > 1) {
2324
embg1e34da42018-01-28 20:03:23 -07002325 ints_are_bounded = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002326 }
2327 else if (key_type == &PyUnicode_Type &&
2328 strings_are_latin &&
2329 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {
2330
2331 strings_are_latin = 0;
2332 }
2333 }
embg1e34da42018-01-28 20:03:23 -07002334 }
embg1e34da42018-01-28 20:03:23 -07002335
2336 /* Choose the best compare, given what we now know about the keys. */
2337 if (keys_are_all_same_type) {
2338
2339 if (key_type == &PyUnicode_Type && strings_are_latin) {
2340 ms.key_compare = unsafe_latin_compare;
2341 }
2342 else if (key_type == &PyLong_Type && ints_are_bounded) {
2343 ms.key_compare = unsafe_long_compare;
2344 }
2345 else if (key_type == &PyFloat_Type) {
2346 ms.key_compare = unsafe_float_compare;
2347 }
2348 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2349 ms.key_compare = unsafe_object_compare;
2350 }
Zackery Spytzebc793d2019-02-21 00:47:14 -07002351 else {
2352 ms.key_compare = safe_object_compare;
2353 }
embg1e34da42018-01-28 20:03:23 -07002354 }
2355 else {
2356 ms.key_compare = safe_object_compare;
2357 }
2358
2359 if (keys_are_in_tuples) {
2360 /* Make sure we're not dealing with tuples of tuples
2361 * (remember: here, key_type refers list [key[0] for key in keys]) */
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002362 if (key_type == &PyTuple_Type) {
embg1e34da42018-01-28 20:03:23 -07002363 ms.tuple_elem_compare = safe_object_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002364 }
2365 else {
embg1e34da42018-01-28 20:03:23 -07002366 ms.tuple_elem_compare = ms.key_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002367 }
embg1e34da42018-01-28 20:03:23 -07002368
2369 ms.key_compare = unsafe_tuple_compare;
2370 }
2371 }
2372 /* End of pre-sort check: ms is now set properly! */
2373
Daniel Stutzbach98338222010-12-02 21:55:33 +00002374 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 nremaining = saved_ob_size;
2377 if (nremaining < 2)
2378 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002379
Benjamin Peterson05380642010-08-23 19:35:39 +00002380 /* Reverse sort stability achieved by initially reversing the list,
2381 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002382 if (reverse) {
2383 if (keys != NULL)
2384 reverse_slice(&keys[0], &keys[saved_ob_size]);
2385 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2386 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 /* March over the array once, left to right, finding natural runs,
2389 * and extending short natural runs to minrun elements.
2390 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 minrun = merge_compute_minrun(nremaining);
2392 do {
2393 int descending;
2394 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002397 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 if (n < 0)
2399 goto fail;
2400 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002401 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 /* If short, extend to min(minrun, nremaining). */
2403 if (n < minrun) {
2404 const Py_ssize_t force = nremaining <= minrun ?
2405 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002406 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 goto fail;
2408 n = force;
2409 }
2410 /* Push run onto pending-runs stack, and maybe merge. */
2411 assert(ms.n < MAX_MERGE_PENDING);
2412 ms.pending[ms.n].base = lo;
2413 ms.pending[ms.n].len = n;
2414 ++ms.n;
2415 if (merge_collapse(&ms) < 0)
2416 goto fail;
2417 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002418 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 nremaining -= n;
2420 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 if (merge_force_collapse(&ms) < 0)
2423 goto fail;
2424 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002425 assert(keys == NULL
2426 ? ms.pending[0].base.keys == saved_ob_item
2427 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002429 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002430
2431succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002433fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002434 if (keys != NULL) {
2435 for (i = 0; i < saved_ob_size; i++)
2436 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002437 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002438 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 if (self->allocated != -1 && result != NULL) {
2442 /* The user mucked with the list during the sort,
2443 * and we don't already have another error to report.
2444 */
2445 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2446 result = NULL;
2447 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 if (reverse && saved_ob_size > 1)
2450 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002453
Daniel Stutzbach98338222010-12-02 21:55:33 +00002454keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 final_ob_item = self->ob_item;
2456 i = Py_SIZE(self);
2457 Py_SIZE(self) = saved_ob_size;
2458 self->ob_item = saved_ob_item;
2459 self->allocated = saved_allocated;
2460 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002461 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 guarantee that the list is really empty when it returns */
2463 while (--i >= 0) {
2464 Py_XDECREF(final_ob_item[i]);
2465 }
2466 PyMem_FREE(final_ob_item);
2467 }
2468 Py_XINCREF(result);
2469 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002470}
Tim Peters330f9e92002-07-19 07:05:44 +00002471#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002472#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002473
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002474int
Fred Drakea2f55112000-07-09 15:16:51 +00002475PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 if (v == NULL || !PyList_Check(v)) {
2478 PyErr_BadInternalCall();
2479 return -1;
2480 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002481 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 if (v == NULL)
2483 return -1;
2484 Py_DECREF(v);
2485 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002486}
2487
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002488/*[clinic input]
2489list.reverse
2490
2491Reverse *IN PLACE*.
2492[clinic start generated code]*/
2493
Guido van Rossumb86c5492001-02-12 22:06:02 +00002494static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002495list_reverse_impl(PyListObject *self)
2496/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 if (Py_SIZE(self) > 1)
2499 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2500 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002501}
2502
Guido van Rossum84c76f51990-10-30 13:32:20 +00002503int
Fred Drakea2f55112000-07-09 15:16:51 +00002504PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 if (v == NULL || !PyList_Check(v)) {
2509 PyErr_BadInternalCall();
2510 return -1;
2511 }
2512 if (Py_SIZE(self) > 1)
2513 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2514 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002515}
2516
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002517PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002518PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 if (v == NULL || !PyList_Check(v)) {
2521 PyErr_BadInternalCall();
2522 return NULL;
2523 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +05002524 return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002525}
2526
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002527/*[clinic input]
2528list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002529
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002530 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002531 start: slice_index(accept={int}) = 0
2532 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002533 /
2534
2535Return first index of value.
2536
2537Raises ValueError if the value is not present.
2538[clinic start generated code]*/
2539
2540static PyObject *
2541list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2542 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002543/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002544{
2545 Py_ssize_t i;
2546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 if (start < 0) {
2548 start += Py_SIZE(self);
2549 if (start < 0)
2550 start = 0;
2551 }
2552 if (stop < 0) {
2553 stop += Py_SIZE(self);
2554 if (stop < 0)
2555 stop = 0;
2556 }
2557 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002558 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 if (cmp > 0)
2560 return PyLong_FromSsize_t(i);
2561 else if (cmp < 0)
2562 return NULL;
2563 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002564 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002566}
2567
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002568/*[clinic input]
2569list.count
2570
2571 value: object
2572 /
2573
2574Return number of occurrences of value.
2575[clinic start generated code]*/
2576
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002577static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002578list_count(PyListObject *self, PyObject *value)
2579/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002581 Py_ssize_t count = 0;
2582 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002585 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 if (cmp > 0)
2587 count++;
2588 else if (cmp < 0)
2589 return NULL;
2590 }
2591 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002592}
2593
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002594/*[clinic input]
2595list.remove
2596
2597 value: object
2598 /
2599
2600Remove first occurrence of value.
2601
2602Raises ValueError if the value is not present.
2603[clinic start generated code]*/
2604
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002605static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002606list_remove(PyListObject *self, PyObject *value)
2607/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002612 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 if (cmp > 0) {
2614 if (list_ass_slice(self, i, i+1,
2615 (PyObject *)NULL) == 0)
2616 Py_RETURN_NONE;
2617 return NULL;
2618 }
2619 else if (cmp < 0)
2620 return NULL;
2621 }
2622 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2623 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002624}
2625
Jeremy Hylton8caad492000-06-23 14:18:11 +00002626static int
2627list_traverse(PyListObject *o, visitproc visit, void *arg)
2628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 for (i = Py_SIZE(o); --i >= 0; )
2632 Py_VISIT(o->ob_item[i]);
2633 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002634}
2635
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002636static PyObject *
2637list_richcompare(PyObject *v, PyObject *w, int op)
2638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 PyListObject *vl, *wl;
2640 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002641
Brian Curtindfc80e32011-08-10 20:28:54 -05002642 if (!PyList_Check(v) || !PyList_Check(w))
2643 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 vl = (PyListObject *)v;
2646 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2649 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002651 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 else
stratakise8b19652017-11-02 11:32:54 +01002653 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 /* Search for the first index where items are different */
2657 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2658 int k = PyObject_RichCompareBool(vl->ob_item[i],
2659 wl->ob_item[i], Py_EQ);
2660 if (k < 0)
2661 return NULL;
2662 if (!k)
2663 break;
2664 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2667 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002668 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 /* We have an item that differs -- shortcuts for EQ/NE */
2672 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002673 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 }
2675 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002676 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 /* Compare the final item again using the proper operator */
2680 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002681}
2682
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002683/*[clinic input]
2684list.__init__
2685
2686 iterable: object(c_default="NULL") = ()
2687 /
2688
2689Built-in mutable sequence.
2690
2691If no argument is given, the constructor creates a new empty list.
2692The argument must be an iterable if specified.
2693[clinic start generated code]*/
2694
Tim Peters6d6c1a32001-08-02 04:15:00 +00002695static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002696list___init___impl(PyListObject *self, PyObject *iterable)
2697/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 /* Verify list invariants established by PyType_GenericAlloc() */
2700 assert(0 <= Py_SIZE(self));
2701 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2702 assert(self->ob_item != NULL ||
2703 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 /* Empty previous contents */
2706 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002707 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002709 if (iterable != NULL) {
Pablo Galindo372d7052018-10-28 20:16:26 +00002710 if (_PyObject_HasLen(iterable)) {
2711 Py_ssize_t iter_len = PyObject_Size(iterable);
2712 if (iter_len == -1) {
2713 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2714 return -1;
2715 }
2716 PyErr_Clear();
2717 }
2718 if (iter_len > 0 && self->ob_item == NULL
2719 && list_preallocate_exact(self, iter_len)) {
2720 return -1;
2721 }
2722 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002723 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 if (rv == NULL)
2725 return -1;
2726 Py_DECREF(rv);
2727 }
2728 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002729}
2730
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002731/*[clinic input]
2732list.__sizeof__
2733
2734Return the size of the list in memory, in bytes.
2735[clinic start generated code]*/
2736
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002737static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002738list___sizeof___impl(PyListObject *self)
2739/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002742
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002743 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002745}
2746
Raymond Hettinger1021c442003-11-07 15:38:09 +00002747static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002748static PyObject *list_subscript(PyListObject*, PyObject*);
2749
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002750static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002751 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2752 LIST___REVERSED___METHODDEF
2753 LIST___SIZEOF___METHODDEF
2754 LIST_CLEAR_METHODDEF
2755 LIST_COPY_METHODDEF
2756 LIST_APPEND_METHODDEF
2757 LIST_INSERT_METHODDEF
2758 LIST_EXTEND_METHODDEF
2759 LIST_POP_METHODDEF
2760 LIST_REMOVE_METHODDEF
2761 LIST_INDEX_METHODDEF
2762 LIST_COUNT_METHODDEF
2763 LIST_REVERSE_METHODDEF
2764 LIST_SORT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002766};
2767
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002768static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 (lenfunc)list_length, /* sq_length */
2770 (binaryfunc)list_concat, /* sq_concat */
2771 (ssizeargfunc)list_repeat, /* sq_repeat */
2772 (ssizeargfunc)list_item, /* sq_item */
2773 0, /* sq_slice */
2774 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2775 0, /* sq_ass_slice */
2776 (objobjproc)list_contains, /* sq_contains */
2777 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2778 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002779};
2780
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002781static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002782list_subscript(PyListObject* self, PyObject* item)
2783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 if (PyIndex_Check(item)) {
2785 Py_ssize_t i;
2786 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2787 if (i == -1 && PyErr_Occurred())
2788 return NULL;
2789 if (i < 0)
2790 i += PyList_GET_SIZE(self);
2791 return list_item(self, i);
2792 }
2793 else if (PySlice_Check(item)) {
2794 Py_ssize_t start, stop, step, slicelength, cur, i;
2795 PyObject* result;
2796 PyObject* it;
2797 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002798
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002799 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 return NULL;
2801 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002802 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2803 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 if (slicelength <= 0) {
2806 return PyList_New(0);
2807 }
2808 else if (step == 1) {
2809 return list_slice(self, start, stop);
2810 }
2811 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002812 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 src = self->ob_item;
2816 dest = ((PyListObject *)result)->ob_item;
2817 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002818 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 it = src[cur];
2820 Py_INCREF(it);
2821 dest[i] = it;
2822 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002823 Py_SIZE(result) = slicelength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 return result;
2825 }
2826 }
2827 else {
2828 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002829 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 item->ob_type->tp_name);
2831 return NULL;
2832 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002833}
2834
Tim Peters3b01a122002-07-19 02:35:45 +00002835static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002836list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 if (PyIndex_Check(item)) {
2839 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2840 if (i == -1 && PyErr_Occurred())
2841 return -1;
2842 if (i < 0)
2843 i += PyList_GET_SIZE(self);
2844 return list_ass_item(self, i, value);
2845 }
2846 else if (PySlice_Check(item)) {
2847 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002848
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002849 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 return -1;
2851 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002852 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2853 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 if (step == 1)
2856 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 /* Make sure s[5:2] = [..] inserts at the right place:
2859 before 5, not before 2. */
2860 if ((step < 0 && start < stop) ||
2861 (step > 0 && start > stop))
2862 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 if (value == NULL) {
2865 /* delete slice */
2866 PyObject **garbage;
2867 size_t cur;
2868 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002869 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 if (slicelength <= 0)
2872 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 if (step < 0) {
2875 stop = start + 1;
2876 start = stop + step*(slicelength - 1) - 1;
2877 step = -step;
2878 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 garbage = (PyObject**)
2881 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2882 if (!garbage) {
2883 PyErr_NoMemory();
2884 return -1;
2885 }
Tim Peters3b01a122002-07-19 02:35:45 +00002886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 /* drawing pictures might help understand these for
2888 loops. Basically, we memmove the parts of the
2889 list that are *not* part of the slice: step-1
2890 items for each item that is part of the slice,
2891 and then tail end of the list that was not
2892 covered by the slice */
2893 for (cur = start, i = 0;
2894 cur < (size_t)stop;
2895 cur += step, i++) {
2896 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 if (cur + step >= (size_t)Py_SIZE(self)) {
2901 lim = Py_SIZE(self) - cur - 1;
2902 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 memmove(self->ob_item + cur - i,
2905 self->ob_item + cur + 1,
2906 lim * sizeof(PyObject *));
2907 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002908 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 if (cur < (size_t)Py_SIZE(self)) {
2910 memmove(self->ob_item + cur - slicelength,
2911 self->ob_item + cur,
2912 (Py_SIZE(self) - cur) *
2913 sizeof(PyObject *));
2914 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002917 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 for (i = 0; i < slicelength; i++) {
2920 Py_DECREF(garbage[i]);
2921 }
2922 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002923
Victor Stinner35f28032013-11-21 12:16:35 +01002924 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 }
2926 else {
2927 /* assign slice */
2928 PyObject *ins, *seq;
2929 PyObject **garbage, **seqitems, **selfitems;
2930 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 /* protect against a[::-1] = a */
2933 if (self == (PyListObject*)value) {
2934 seq = list_slice((PyListObject*)value, 0,
2935 PyList_GET_SIZE(value));
2936 }
2937 else {
2938 seq = PySequence_Fast(value,
2939 "must assign iterable "
2940 "to extended slice");
2941 }
2942 if (!seq)
2943 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2946 PyErr_Format(PyExc_ValueError,
2947 "attempt to assign sequence of "
2948 "size %zd to extended slice of "
2949 "size %zd",
2950 PySequence_Fast_GET_SIZE(seq),
2951 slicelength);
2952 Py_DECREF(seq);
2953 return -1;
2954 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 if (!slicelength) {
2957 Py_DECREF(seq);
2958 return 0;
2959 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 garbage = (PyObject**)
2962 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2963 if (!garbage) {
2964 Py_DECREF(seq);
2965 PyErr_NoMemory();
2966 return -1;
2967 }
Tim Peters3b01a122002-07-19 02:35:45 +00002968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 selfitems = self->ob_item;
2970 seqitems = PySequence_Fast_ITEMS(seq);
2971 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002972 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 garbage[i] = selfitems[cur];
2974 ins = seqitems[i];
2975 Py_INCREF(ins);
2976 selfitems[cur] = ins;
2977 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 for (i = 0; i < slicelength; i++) {
2980 Py_DECREF(garbage[i]);
2981 }
Tim Peters3b01a122002-07-19 02:35:45 +00002982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 PyMem_FREE(garbage);
2984 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 return 0;
2987 }
2988 }
2989 else {
2990 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002991 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 item->ob_type->tp_name);
2993 return -1;
2994 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002995}
2996
2997static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 (lenfunc)list_length,
2999 (binaryfunc)list_subscript,
3000 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003001};
3002
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003003PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3005 "list",
3006 sizeof(PyListObject),
3007 0,
3008 (destructor)list_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003009 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 0, /* tp_getattr */
3011 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003012 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 (reprfunc)list_repr, /* tp_repr */
3014 0, /* tp_as_number */
3015 &list_as_sequence, /* tp_as_sequence */
3016 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003017 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 0, /* tp_call */
3019 0, /* tp_str */
3020 PyObject_GenericGetAttr, /* tp_getattro */
3021 0, /* tp_setattro */
3022 0, /* tp_as_buffer */
3023 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003024 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
3025 list___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 (traverseproc)list_traverse, /* tp_traverse */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003027 (inquiry)_list_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 list_richcompare, /* tp_richcompare */
3029 0, /* tp_weaklistoffset */
3030 list_iter, /* tp_iter */
3031 0, /* tp_iternext */
3032 list_methods, /* tp_methods */
3033 0, /* tp_members */
3034 0, /* tp_getset */
3035 0, /* tp_base */
3036 0, /* tp_dict */
3037 0, /* tp_descr_get */
3038 0, /* tp_descr_set */
3039 0, /* tp_dictoffset */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003040 (initproc)list___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 PyType_GenericAlloc, /* tp_alloc */
3042 PyType_GenericNew, /* tp_new */
3043 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003044};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00003045
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003046/*********************** List Iterator **************************/
3047
3048typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02003050 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003052} listiterobject;
3053
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003054static void listiter_dealloc(listiterobject *);
3055static int listiter_traverse(listiterobject *, visitproc, void *);
3056static PyObject *listiter_next(listiterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303057static PyObject *listiter_len(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003058static PyObject *listiter_reduce_general(void *_it, int forward);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303059static PyObject *listiter_reduce(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003060static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00003061
Armin Rigof5b3e362006-02-11 21:32:43 +00003062PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003063PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3064PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003065
3066static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003068 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3069 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00003071};
3072
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003073PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3075 "list_iterator", /* tp_name */
3076 sizeof(listiterobject), /* tp_basicsize */
3077 0, /* tp_itemsize */
3078 /* methods */
3079 (destructor)listiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003080 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 0, /* tp_getattr */
3082 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003083 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 0, /* tp_repr */
3085 0, /* tp_as_number */
3086 0, /* tp_as_sequence */
3087 0, /* tp_as_mapping */
3088 0, /* tp_hash */
3089 0, /* tp_call */
3090 0, /* tp_str */
3091 PyObject_GenericGetAttr, /* tp_getattro */
3092 0, /* tp_setattro */
3093 0, /* tp_as_buffer */
3094 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3095 0, /* tp_doc */
3096 (traverseproc)listiter_traverse, /* tp_traverse */
3097 0, /* tp_clear */
3098 0, /* tp_richcompare */
3099 0, /* tp_weaklistoffset */
3100 PyObject_SelfIter, /* tp_iter */
3101 (iternextfunc)listiter_next, /* tp_iternext */
3102 listiter_methods, /* tp_methods */
3103 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003104};
Raymond Hettinger1021c442003-11-07 15:38:09 +00003105
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003106
3107static PyObject *
3108list_iter(PyObject *seq)
3109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 if (!PyList_Check(seq)) {
3113 PyErr_BadInternalCall();
3114 return NULL;
3115 }
3116 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3117 if (it == NULL)
3118 return NULL;
3119 it->it_index = 0;
3120 Py_INCREF(seq);
3121 it->it_seq = (PyListObject *)seq;
3122 _PyObject_GC_TRACK(it);
3123 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003124}
3125
3126static void
3127listiter_dealloc(listiterobject *it)
3128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 _PyObject_GC_UNTRACK(it);
3130 Py_XDECREF(it->it_seq);
3131 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003132}
3133
3134static int
3135listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 Py_VISIT(it->it_seq);
3138 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003139}
3140
3141static PyObject *
3142listiter_next(listiterobject *it)
3143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 PyListObject *seq;
3145 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 assert(it != NULL);
3148 seq = it->it_seq;
3149 if (seq == NULL)
3150 return NULL;
3151 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 if (it->it_index < PyList_GET_SIZE(seq)) {
3154 item = PyList_GET_ITEM(seq, it->it_index);
3155 ++it->it_index;
3156 Py_INCREF(item);
3157 return item;
3158 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003161 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003163}
3164
3165static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303166listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 Py_ssize_t len;
3169 if (it->it_seq) {
3170 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3171 if (len >= 0)
3172 return PyLong_FromSsize_t(len);
3173 }
3174 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003175}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003176
3177static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303178listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003179{
3180 return listiter_reduce_general(it, 1);
3181}
3182
3183static PyObject *
3184listiter_setstate(listiterobject *it, PyObject *state)
3185{
Victor Stinner7660b882013-06-24 23:59:24 +02003186 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003187 if (index == -1 && PyErr_Occurred())
3188 return NULL;
3189 if (it->it_seq != NULL) {
3190 if (index < 0)
3191 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003192 else if (index > PyList_GET_SIZE(it->it_seq))
3193 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003194 it->it_index = index;
3195 }
3196 Py_RETURN_NONE;
3197}
3198
Raymond Hettinger1021c442003-11-07 15:38:09 +00003199/*********************** List Reverse Iterator **************************/
3200
3201typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 PyObject_HEAD
3203 Py_ssize_t it_index;
3204 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00003205} listreviterobject;
3206
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003207static void listreviter_dealloc(listreviterobject *);
3208static int listreviter_traverse(listreviterobject *, visitproc, void *);
3209static PyObject *listreviter_next(listreviterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303210static PyObject *listreviter_len(listreviterobject *, PyObject *);
3211static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003212static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003213
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003214static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003216 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3217 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003219};
3220
Raymond Hettinger1021c442003-11-07 15:38:09 +00003221PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3223 "list_reverseiterator", /* tp_name */
3224 sizeof(listreviterobject), /* tp_basicsize */
3225 0, /* tp_itemsize */
3226 /* methods */
3227 (destructor)listreviter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003228 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 0, /* tp_getattr */
3230 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003231 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 0, /* tp_repr */
3233 0, /* tp_as_number */
3234 0, /* tp_as_sequence */
3235 0, /* tp_as_mapping */
3236 0, /* tp_hash */
3237 0, /* tp_call */
3238 0, /* tp_str */
3239 PyObject_GenericGetAttr, /* tp_getattro */
3240 0, /* tp_setattro */
3241 0, /* tp_as_buffer */
3242 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3243 0, /* tp_doc */
3244 (traverseproc)listreviter_traverse, /* tp_traverse */
3245 0, /* tp_clear */
3246 0, /* tp_richcompare */
3247 0, /* tp_weaklistoffset */
3248 PyObject_SelfIter, /* tp_iter */
3249 (iternextfunc)listreviter_next, /* tp_iternext */
3250 listreviter_methods, /* tp_methods */
3251 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00003252};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003253
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003254/*[clinic input]
3255list.__reversed__
3256
3257Return a reverse iterator over the list.
3258[clinic start generated code]*/
3259
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003260static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003261list___reversed___impl(PyListObject *self)
3262/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3267 if (it == NULL)
3268 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003269 assert(PyList_Check(self));
3270 it->it_index = PyList_GET_SIZE(self) - 1;
3271 Py_INCREF(self);
3272 it->it_seq = self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 PyObject_GC_Track(it);
3274 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003275}
3276
3277static void
3278listreviter_dealloc(listreviterobject *it)
3279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 PyObject_GC_UnTrack(it);
3281 Py_XDECREF(it->it_seq);
3282 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003283}
3284
3285static int
3286listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 Py_VISIT(it->it_seq);
3289 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003290}
3291
3292static PyObject *
3293listreviter_next(listreviterobject *it)
3294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003296 Py_ssize_t index;
3297 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003298
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003299 assert(it != NULL);
3300 seq = it->it_seq;
3301 if (seq == NULL) {
3302 return NULL;
3303 }
3304 assert(PyList_Check(seq));
3305
3306 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 if (index>=0 && index < PyList_GET_SIZE(seq)) {
3308 item = PyList_GET_ITEM(seq, index);
3309 it->it_index--;
3310 Py_INCREF(item);
3311 return item;
3312 }
3313 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003314 it->it_seq = NULL;
3315 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003317}
3318
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003319static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303320listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 Py_ssize_t len = it->it_index + 1;
3323 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3324 len = 0;
3325 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003326}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003327
3328static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303329listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003330{
3331 return listiter_reduce_general(it, 0);
3332}
3333
3334static PyObject *
3335listreviter_setstate(listreviterobject *it, PyObject *state)
3336{
3337 Py_ssize_t index = PyLong_AsSsize_t(state);
3338 if (index == -1 && PyErr_Occurred())
3339 return NULL;
3340 if (it->it_seq != NULL) {
3341 if (index < -1)
3342 index = -1;
3343 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3344 index = PyList_GET_SIZE(it->it_seq) - 1;
3345 it->it_index = index;
3346 }
3347 Py_RETURN_NONE;
3348}
3349
3350/* common pickling support */
3351
3352static PyObject *
3353listiter_reduce_general(void *_it, int forward)
3354{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003355 _Py_IDENTIFIER(iter);
3356 _Py_IDENTIFIER(reversed);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003357 PyObject *list;
3358
3359 /* the objects are not the same, index is of different types! */
3360 if (forward) {
3361 listiterobject *it = (listiterobject *)_it;
3362 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003363 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003364 it->it_seq, it->it_index);
3365 } else {
3366 listreviterobject *it = (listreviterobject *)_it;
3367 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003368 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_reversed),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003369 it->it_seq, it->it_index);
3370 }
3371 /* empty iterator, create an empty list */
3372 list = PyList_New(0);
3373 if (list == NULL)
3374 return NULL;
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003375 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003376}