blob: 645742b801fac43b2a4de780636d506a0f80ad35 [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
Victor Stinnerbed48172019-08-27 00:12:32 +0200141_PyList_Fini(void)
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100142{
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)
Serhiy Storchaka18b711c2019-08-04 14:12:48 +0300452 cmp = PyObject_RichCompareBool(PyList_GET_ITEM(a, i), el, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000454}
455
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000457list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700459 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 if (indexerr == NULL) {
461 indexerr = PyUnicode_FromString(
462 "list index out of range");
463 if (indexerr == NULL)
464 return NULL;
465 }
466 PyErr_SetObject(PyExc_IndexError, indexerr);
467 return NULL;
468 }
469 Py_INCREF(a->ob_item[i]);
470 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471}
472
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000474list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 PyListObject *np;
477 PyObject **src, **dest;
478 Py_ssize_t i, len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500480 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 if (np == NULL)
482 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 src = a->ob_item + ilow;
485 dest = np->ob_item;
486 for (i = 0; i < len; i++) {
487 PyObject *v = src[i];
488 Py_INCREF(v);
489 dest[i] = v;
490 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500491 Py_SIZE(np) = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493}
494
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000495PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000496PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 if (!PyList_Check(a)) {
499 PyErr_BadInternalCall();
500 return NULL;
501 }
Sergey Fedoseevef1b88b2019-02-21 11:51:52 +0500502 if (ilow < 0) {
503 ilow = 0;
504 }
505 else if (ilow > Py_SIZE(a)) {
506 ilow = Py_SIZE(a);
507 }
508 if (ihigh < ilow) {
509 ihigh = ilow;
510 }
511 else if (ihigh > Py_SIZE(a)) {
512 ihigh = Py_SIZE(a);
513 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000515}
516
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000517static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000518list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 Py_ssize_t size;
521 Py_ssize_t i;
522 PyObject **src, **dest;
523 PyListObject *np;
524 if (!PyList_Check(bb)) {
525 PyErr_Format(PyExc_TypeError,
526 "can only concatenate list (not \"%.200s\") to list",
527 bb->ob_type->tp_name);
528 return NULL;
529 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000531 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000533 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500534 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 if (np == NULL) {
536 return NULL;
537 }
538 src = a->ob_item;
539 dest = np->ob_item;
540 for (i = 0; i < Py_SIZE(a); i++) {
541 PyObject *v = src[i];
542 Py_INCREF(v);
543 dest[i] = v;
544 }
545 src = b->ob_item;
546 dest = np->ob_item + Py_SIZE(a);
547 for (i = 0; i < Py_SIZE(b); i++) {
548 PyObject *v = src[i];
549 Py_INCREF(v);
550 dest[i] = v;
551 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500552 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554#undef b
555}
556
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000557static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000558list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 Py_ssize_t i, j;
561 Py_ssize_t size;
562 PyListObject *np;
563 PyObject **p, **items;
564 PyObject *elem;
565 if (n < 0)
566 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100567 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100569 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 if (size == 0)
571 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500572 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 if (np == NULL)
574 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500577 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 elem = a->ob_item[0];
579 for (i = 0; i < n; i++) {
580 items[i] = elem;
581 Py_INCREF(elem);
582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500584 else {
585 p = np->ob_item;
586 items = a->ob_item;
587 for (i = 0; i < n; i++) {
588 for (j = 0; j < Py_SIZE(a); j++) {
589 *p = items[j];
590 Py_INCREF(*p);
591 p++;
592 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 }
594 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500595 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000597}
598
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000599static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200600_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 Py_ssize_t i;
603 PyObject **item = a->ob_item;
604 if (item != NULL) {
605 /* Because XDECREF can recursively invoke operations on
606 this list, we make it empty first. */
607 i = Py_SIZE(a);
608 Py_SIZE(a) = 0;
609 a->ob_item = NULL;
610 a->allocated = 0;
611 while (--i >= 0) {
612 Py_XDECREF(item[i]);
613 }
614 PyMem_FREE(item);
615 }
616 /* Never fails; the return value can be ignored.
617 Note that there is no guarantee that the list is actually empty
618 at this point, because XDECREF may have populated it again! */
619 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000620}
621
Tim Peters8fc4a912004-07-31 21:53:19 +0000622/* a[ilow:ihigh] = v if v != NULL.
623 * del a[ilow:ihigh] if v == NULL.
624 *
625 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
626 * guaranteed the call cannot fail.
627 */
Armin Rigo93677f02004-07-29 12:40:23 +0000628static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000629list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 /* Because [X]DECREF can recursively invoke list operations on
632 this list, we must postpone all [X]DECREF activity until
633 after the list is back in its canonical shape. Therefore
634 we must allocate an additional array, 'recycle', into which
635 we temporarily copy the items that are deleted from the
636 list. :-( */
637 PyObject *recycle_on_stack[8];
638 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
639 PyObject **item;
640 PyObject **vitem = NULL;
641 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
642 Py_ssize_t n; /* # of elements in replacement list */
643 Py_ssize_t norig; /* # of elements in list getting replaced */
644 Py_ssize_t d; /* Change in size */
645 Py_ssize_t k;
646 size_t s;
647 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000648#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 if (v == NULL)
650 n = 0;
651 else {
652 if (a == b) {
653 /* Special case "a[i:j] = a" -- copy b first */
654 v = list_slice(b, 0, Py_SIZE(b));
655 if (v == NULL)
656 return result;
657 result = list_ass_slice(a, ilow, ihigh, v);
658 Py_DECREF(v);
659 return result;
660 }
661 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
662 if(v_as_SF == NULL)
663 goto Error;
664 n = PySequence_Fast_GET_SIZE(v_as_SF);
665 vitem = PySequence_Fast_ITEMS(v_as_SF);
666 }
667 if (ilow < 0)
668 ilow = 0;
669 else if (ilow > Py_SIZE(a))
670 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 if (ihigh < ilow)
673 ihigh = ilow;
674 else if (ihigh > Py_SIZE(a))
675 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 norig = ihigh - ilow;
678 assert(norig >= 0);
679 d = n - norig;
680 if (Py_SIZE(a) + d == 0) {
681 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200682 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 }
684 item = a->ob_item;
685 /* recycle the items that we are about to remove */
686 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700687 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
688 if (s) {
689 if (s > sizeof(recycle_on_stack)) {
690 recycle = (PyObject **)PyMem_MALLOC(s);
691 if (recycle == NULL) {
692 PyErr_NoMemory();
693 goto Error;
694 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700696 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200700 Py_ssize_t tail;
701 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
702 memmove(&item[ihigh+d], &item[ihigh], tail);
703 if (list_resize(a, Py_SIZE(a) + d) < 0) {
704 memmove(&item[ihigh], &item[ihigh+d], tail);
705 memcpy(&item[ilow], recycle, s);
706 goto Error;
707 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 item = a->ob_item;
709 }
710 else if (d > 0) { /* Insert d items */
711 k = Py_SIZE(a);
712 if (list_resize(a, k+d) < 0)
713 goto Error;
714 item = a->ob_item;
715 memmove(&item[ihigh+d], &item[ihigh],
716 (k - ihigh)*sizeof(PyObject *));
717 }
718 for (k = 0; k < n; k++, ilow++) {
719 PyObject *w = vitem[k];
720 Py_XINCREF(w);
721 item[ilow] = w;
722 }
723 for (k = norig - 1; k >= 0; --k)
724 Py_XDECREF(recycle[k]);
725 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000726 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 if (recycle != recycle_on_stack)
728 PyMem_FREE(recycle);
729 Py_XDECREF(v_as_SF);
730 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000731#undef b
732}
733
Guido van Rossum234f9421993-06-17 12:35:49 +0000734int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000735PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 if (!PyList_Check(a)) {
738 PyErr_BadInternalCall();
739 return -1;
740 }
741 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000742}
743
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000744static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000745list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 PyObject **items;
748 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000749
750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 size = PyList_GET_SIZE(self);
752 if (size == 0 || n == 1) {
753 Py_INCREF(self);
754 return (PyObject *)self;
755 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 if (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200758 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 Py_INCREF(self);
760 return (PyObject *)self;
761 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 if (size > PY_SSIZE_T_MAX / n) {
764 return PyErr_NoMemory();
765 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000766
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800767 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 p = size;
771 items = self->ob_item;
772 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
773 for (j = 0; j < size; j++) {
774 PyObject *o = items[j];
775 Py_INCREF(o);
776 items[p++] = o;
777 }
778 }
779 Py_INCREF(self);
780 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000781}
782
Guido van Rossum4a450d01991-04-03 19:05:18 +0000783static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000784list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000785{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700786 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 PyErr_SetString(PyExc_IndexError,
788 "list assignment index out of range");
789 return -1;
790 }
791 if (v == NULL)
792 return list_ass_slice(a, i, i+1, v);
793 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300794 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000796}
797
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200798/*[clinic input]
799list.insert
800
801 index: Py_ssize_t
802 object: object
803 /
804
805Insert object before index.
806[clinic start generated code]*/
807
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000808static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200809list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
810/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000811{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200812 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 Py_RETURN_NONE;
814 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000815}
816
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200817/*[clinic input]
818list.clear
819
820Remove all items from list.
821[clinic start generated code]*/
822
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000823static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200824list_clear_impl(PyListObject *self)
825/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000826{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200827 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000828 Py_RETURN_NONE;
829}
830
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200831/*[clinic input]
832list.copy
833
834Return a shallow copy of the list.
835[clinic start generated code]*/
836
Eli Benderskycbbaa962011-02-25 05:47:53 +0000837static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200838list_copy_impl(PyListObject *self)
839/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000840{
841 return list_slice(self, 0, Py_SIZE(self));
842}
843
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200844/*[clinic input]
845list.append
846
847 object: object
848 /
849
850Append object to the end of the list.
851[clinic start generated code]*/
852
Eli Benderskycbbaa962011-02-25 05:47:53 +0000853static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200854list_append(PyListObject *self, PyObject *object)
855/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000856{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200857 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 Py_RETURN_NONE;
859 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000860}
861
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200862/*[clinic input]
863list.extend
864
865 iterable: object
866 /
867
868Extend list by appending elements from the iterable.
869[clinic start generated code]*/
870
Barry Warsawdedf6d61998-10-09 16:37:25 +0000871static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200872list_extend(PyListObject *self, PyObject *iterable)
873/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000874{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 PyObject *it; /* iter(v) */
876 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200877 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 Py_ssize_t mn; /* m + n */
879 Py_ssize_t i;
880 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 /* Special cases:
883 1) lists and tuples which can use PySequence_Fast ops
884 2) extending self to self requires making a copy first
885 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200886 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
887 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200889 iterable = PySequence_Fast(iterable, "argument must be iterable");
890 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200892 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200894 /* short circuit when iterable is empty */
895 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 Py_RETURN_NONE;
897 }
898 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000899 /* It should not be possible to allocate a list large enough to cause
900 an overflow on any relevant platform */
901 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800902 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200903 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 return NULL;
905 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200906 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 * situation a.extend(a), but the following code works
908 * in that case too. Just make sure to resize self
909 * before calling PySequence_Fast_ITEMS.
910 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200911 /* populate the end of self with iterable's items */
912 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 dest = self->ob_item + m;
914 for (i = 0; i < n; i++) {
915 PyObject *o = src[i];
916 Py_INCREF(o);
917 dest[i] = o;
918 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200919 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 Py_RETURN_NONE;
921 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000922
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200923 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 if (it == NULL)
925 return NULL;
926 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200929 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800930 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 Py_DECREF(it);
932 return NULL;
933 }
934 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000935 if (m > PY_SSIZE_T_MAX - n) {
936 /* m + n overflowed; on the chance that n lied, and there really
937 * is enough room, ignore it. If n was telling the truth, we'll
938 * eventually run out of memory during the loop.
939 */
940 }
941 else {
942 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800944 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 goto error;
946 /* Make the list sane again. */
947 Py_SIZE(self) = m;
948 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 /* Run iterator to exhaustion. */
951 for (;;) {
952 PyObject *item = iternext(it);
953 if (item == NULL) {
954 if (PyErr_Occurred()) {
955 if (PyErr_ExceptionMatches(PyExc_StopIteration))
956 PyErr_Clear();
957 else
958 goto error;
959 }
960 break;
961 }
962 if (Py_SIZE(self) < self->allocated) {
963 /* steals ref */
964 PyList_SET_ITEM(self, Py_SIZE(self), item);
965 ++Py_SIZE(self);
966 }
967 else {
968 int status = app1(self, item);
969 Py_DECREF(item); /* append creates a new ref */
970 if (status < 0)
971 goto error;
972 }
973 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200976 if (Py_SIZE(self) < self->allocated) {
977 if (list_resize(self, Py_SIZE(self)) < 0)
978 goto error;
979 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 Py_DECREF(it);
982 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000983
984 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 Py_DECREF(it);
986 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000987}
988
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000989PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200990_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000991{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200992 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000993}
994
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000995static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000996list_inplace_concat(PyListObject *self, PyObject *other)
997{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000999
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001000 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 if (result == NULL)
1002 return result;
1003 Py_DECREF(result);
1004 Py_INCREF(self);
1005 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001006}
1007
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001008/*[clinic input]
1009list.pop
1010
1011 index: Py_ssize_t = -1
1012 /
1013
1014Remove and return item at index (default last).
1015
1016Raises IndexError if list is empty or index is out of range.
1017[clinic start generated code]*/
1018
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001019static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001020list_pop_impl(PyListObject *self, Py_ssize_t index)
1021/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 PyObject *v;
1024 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (Py_SIZE(self) == 0) {
1027 /* Special-case most common failure cause */
1028 PyErr_SetString(PyExc_IndexError, "pop from empty list");
1029 return NULL;
1030 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001031 if (index < 0)
1032 index += Py_SIZE(self);
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -07001033 if (!valid_index(index, Py_SIZE(self))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1035 return NULL;
1036 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001037 v = self->ob_item[index];
1038 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001040 if (status >= 0)
1041 return v; /* and v now owns the reference the list had */
1042 else
1043 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 }
1045 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001046 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001047 if (status < 0) {
1048 Py_DECREF(v);
1049 return NULL;
1050 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001052}
1053
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001054/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1055static void
1056reverse_slice(PyObject **lo, PyObject **hi)
1057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 --hi;
1061 while (lo < hi) {
1062 PyObject *t = *lo;
1063 *lo = *hi;
1064 *hi = t;
1065 ++lo;
1066 --hi;
1067 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001068}
1069
Tim Petersa64dc242002-08-01 02:13:36 +00001070/* Lots of code for an adaptive, stable, natural mergesort. There are many
1071 * pieces to this algorithm; read listsort.txt for overviews and details.
1072 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001073
Daniel Stutzbach98338222010-12-02 21:55:33 +00001074/* A sortslice contains a pointer to an array of keys and a pointer to
1075 * an array of corresponding values. In other words, keys[i]
1076 * corresponds with values[i]. If values == NULL, then the keys are
1077 * also the values.
1078 *
1079 * Several convenience routines are provided here, so that keys and
1080 * values are always moved in sync.
1081 */
1082
1083typedef struct {
1084 PyObject **keys;
1085 PyObject **values;
1086} sortslice;
1087
1088Py_LOCAL_INLINE(void)
1089sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1090{
1091 s1->keys[i] = s2->keys[j];
1092 if (s1->values != NULL)
1093 s1->values[i] = s2->values[j];
1094}
1095
1096Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001097sortslice_copy_incr(sortslice *dst, sortslice *src)
1098{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001099 *dst->keys++ = *src->keys++;
1100 if (dst->values != NULL)
1101 *dst->values++ = *src->values++;
1102}
1103
1104Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001105sortslice_copy_decr(sortslice *dst, sortslice *src)
1106{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001107 *dst->keys-- = *src->keys--;
1108 if (dst->values != NULL)
1109 *dst->values-- = *src->values--;
1110}
1111
1112
1113Py_LOCAL_INLINE(void)
1114sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001115 Py_ssize_t n)
1116{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001117 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1118 if (s1->values != NULL)
1119 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1120}
1121
1122Py_LOCAL_INLINE(void)
1123sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001124 Py_ssize_t n)
1125{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001126 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1127 if (s1->values != NULL)
1128 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1129}
1130
1131Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001132sortslice_advance(sortslice *slice, Py_ssize_t n)
1133{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001134 slice->keys += n;
1135 if (slice->values != NULL)
1136 slice->values += n;
1137}
1138
embg1e34da42018-01-28 20:03:23 -07001139/* Comparison function: ms->key_compare, which is set at run-time in
1140 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001141 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1142 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001143
embg1e34da42018-01-28 20:03:23 -07001144#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001145
1146/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001147 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1148 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1149*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001150#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001152
embg1e34da42018-01-28 20:03:23 -07001153/* The maximum number of entries in a MergeState's pending-runs stack.
1154 * This is enough to sort arrays of size up to about
1155 * 32 * phi ** MAX_MERGE_PENDING
1156 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1157 * with 2**64 elements.
1158 */
1159#define MAX_MERGE_PENDING 85
1160
1161/* When we get into galloping mode, we stay there until both runs win less
1162 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1163 */
1164#define MIN_GALLOP 7
1165
1166/* Avoid malloc for small temp arrays. */
1167#define MERGESTATE_TEMP_SIZE 256
1168
1169/* One MergeState exists on the stack per invocation of mergesort. It's just
1170 * a convenient way to pass state around among the helper functions.
1171 */
1172struct s_slice {
1173 sortslice base;
1174 Py_ssize_t len;
1175};
1176
1177typedef struct s_MergeState MergeState;
1178struct s_MergeState {
1179 /* This controls when we get *into* galloping mode. It's initialized
1180 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1181 * random data, and lower for highly structured data.
1182 */
1183 Py_ssize_t min_gallop;
1184
1185 /* 'a' is temp storage to help with merges. It contains room for
1186 * alloced entries.
1187 */
1188 sortslice a; /* may point to temparray below */
1189 Py_ssize_t alloced;
1190
1191 /* A stack of n pending runs yet to be merged. Run #i starts at
1192 * address base[i] and extends for len[i] elements. It's always
1193 * true (so long as the indices are in bounds) that
1194 *
1195 * pending[i].base + pending[i].len == pending[i+1].base
1196 *
1197 * so we could cut the storage for this, but it's a minor amount,
1198 * and keeping all the info explicit simplifies the code.
1199 */
1200 int n;
1201 struct s_slice pending[MAX_MERGE_PENDING];
1202
1203 /* 'a' points to this when possible, rather than muck with malloc. */
1204 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1205
1206 /* This is the function we will use to compare two keys,
1207 * even when none of our special cases apply and we have to use
1208 * safe_object_compare. */
1209 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1210
1211 /* This function is used by unsafe_object_compare to optimize comparisons
1212 * when we know our list is type-homogeneous but we can't assume anything else.
1213 * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1214 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1215
1216 /* This function is used by unsafe_tuple_compare to compare the first elements
1217 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1218 * we can assume more, and use one of the special-case compares. */
1219 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1220};
1221
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001222/* binarysort is the best method for sorting small arrays: it does
1223 few compares, but can do data movement quadratic in the number of
1224 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001225 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001226 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001227 On entry, must have lo <= start <= hi, and that [lo, start) is already
1228 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001229 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001230 Even in case of error, the output slice will be some permutation of
1231 the input (nothing is lost or duplicated).
1232*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001233static int
embg1e34da42018-01-28 20:03:23 -07001234binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001235{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001236 Py_ssize_t k;
1237 PyObject **l, **p, **r;
1238 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001239
Daniel Stutzbach98338222010-12-02 21:55:33 +00001240 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001242 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 ++start;
1244 for (; start < hi; ++start) {
1245 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001246 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 r = start;
1248 pivot = *r;
1249 /* Invariants:
1250 * pivot >= all in [lo, l).
1251 * pivot < all in [r, start).
1252 * The second is vacuously true at the start.
1253 */
1254 assert(l < r);
1255 do {
1256 p = l + ((r - l) >> 1);
1257 IFLT(pivot, *p)
1258 r = p;
1259 else
1260 l = p+1;
1261 } while (l < r);
1262 assert(l == r);
1263 /* The invariants still hold, so pivot >= all in [lo, l) and
1264 pivot < all in [l, start), so pivot belongs at l. Note
1265 that if there are elements equal to pivot, l points to the
1266 first slot after them -- that's why this sort is stable.
1267 Slide over to make room.
1268 Caution: using memmove is much slower under MSVC 5;
1269 we're not usually moving many slots. */
1270 for (p = start; p > l; --p)
1271 *p = *(p-1);
1272 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001273 if (lo.values != NULL) {
1274 Py_ssize_t offset = lo.values - lo.keys;
1275 p = start + offset;
1276 pivot = *p;
1277 l += offset;
1278 for (p = start + offset; p > l; --p)
1279 *p = *(p-1);
1280 *l = pivot;
1281 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 }
1283 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001284
1285 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001287}
1288
Tim Petersa64dc242002-08-01 02:13:36 +00001289/*
1290Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1291is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001292
Tim Petersa64dc242002-08-01 02:13:36 +00001293 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001294
Tim Petersa64dc242002-08-01 02:13:36 +00001295or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001296
Tim Petersa64dc242002-08-01 02:13:36 +00001297 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001298
Tim Petersa64dc242002-08-01 02:13:36 +00001299Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1300For its intended use in a stable mergesort, the strictness of the defn of
1301"descending" is needed so that the caller can safely reverse a descending
1302sequence without violating stability (strict > ensures there are no equal
1303elements to get out of order).
1304
1305Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001306*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001307static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001308count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 Py_ssize_t k;
1311 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 assert(lo < hi);
1314 *descending = 0;
1315 ++lo;
1316 if (lo == hi)
1317 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 n = 2;
1320 IFLT(*lo, *(lo-1)) {
1321 *descending = 1;
1322 for (lo = lo+1; lo < hi; ++lo, ++n) {
1323 IFLT(*lo, *(lo-1))
1324 ;
1325 else
1326 break;
1327 }
1328 }
1329 else {
1330 for (lo = lo+1; lo < hi; ++lo, ++n) {
1331 IFLT(*lo, *(lo-1))
1332 break;
1333 }
1334 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001337fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001339}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001340
Tim Petersa64dc242002-08-01 02:13:36 +00001341/*
1342Locate the proper position of key in a sorted vector; if the vector contains
1343an element equal to key, return the position immediately to the left of
1344the leftmost equal element. [gallop_right() does the same except returns
1345the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001346
Tim Petersa64dc242002-08-01 02:13:36 +00001347"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001348
Tim Petersa64dc242002-08-01 02:13:36 +00001349"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1350hint is to the final result, the faster this runs.
1351
1352The return value is the int k in 0..n such that
1353
1354 a[k-1] < key <= a[k]
1355
1356pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1357key belongs at index k; or, IOW, the first k elements of a should precede
1358key, and the last n-k should follow key.
1359
1360Returns -1 on error. See listsort.txt for info on the method.
1361*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001362static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001363gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 Py_ssize_t ofs;
1366 Py_ssize_t lastofs;
1367 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 a += hint;
1372 lastofs = 0;
1373 ofs = 1;
1374 IFLT(*a, key) {
1375 /* a[hint] < key -- gallop right, until
1376 * a[hint + lastofs] < key <= a[hint + ofs]
1377 */
1378 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1379 while (ofs < maxofs) {
1380 IFLT(a[ofs], key) {
1381 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001382 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 }
1385 else /* key <= a[hint + ofs] */
1386 break;
1387 }
1388 if (ofs > maxofs)
1389 ofs = maxofs;
1390 /* Translate back to offsets relative to &a[0]. */
1391 lastofs += hint;
1392 ofs += hint;
1393 }
1394 else {
1395 /* key <= a[hint] -- gallop left, until
1396 * a[hint - ofs] < key <= a[hint - lastofs]
1397 */
1398 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1399 while (ofs < maxofs) {
1400 IFLT(*(a-ofs), key)
1401 break;
1402 /* key <= a[hint - ofs] */
1403 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001404 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 }
1407 if (ofs > maxofs)
1408 ofs = maxofs;
1409 /* Translate back to positive offsets relative to &a[0]. */
1410 k = lastofs;
1411 lastofs = hint - ofs;
1412 ofs = hint - k;
1413 }
1414 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1417 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1418 * right of lastofs but no farther right than ofs. Do a binary
1419 * search, with invariant a[lastofs-1] < key <= a[ofs].
1420 */
1421 ++lastofs;
1422 while (lastofs < ofs) {
1423 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 IFLT(a[m], key)
1426 lastofs = m+1; /* a[m] < key */
1427 else
1428 ofs = m; /* key <= a[m] */
1429 }
1430 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1431 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001432
1433fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001435}
1436
1437/*
1438Exactly like gallop_left(), except that if key already exists in a[0:n],
1439finds the position immediately to the right of the rightmost equal value.
1440
1441The return value is the int k in 0..n such that
1442
1443 a[k-1] <= key < a[k]
1444
1445or -1 if error.
1446
1447The code duplication is massive, but this is enough different given that
1448we're sticking to "<" comparisons that it's much harder to follow if
1449written as one routine with yet another "left or right?" flag.
1450*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001451static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001452gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 Py_ssize_t ofs;
1455 Py_ssize_t lastofs;
1456 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 a += hint;
1461 lastofs = 0;
1462 ofs = 1;
1463 IFLT(key, *a) {
1464 /* key < a[hint] -- gallop left, until
1465 * a[hint - ofs] <= key < a[hint - lastofs]
1466 */
1467 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1468 while (ofs < maxofs) {
1469 IFLT(key, *(a-ofs)) {
1470 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001471 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 }
1474 else /* a[hint - ofs] <= key */
1475 break;
1476 }
1477 if (ofs > maxofs)
1478 ofs = maxofs;
1479 /* Translate back to positive offsets relative to &a[0]. */
1480 k = lastofs;
1481 lastofs = hint - ofs;
1482 ofs = hint - k;
1483 }
1484 else {
1485 /* a[hint] <= key -- gallop right, until
1486 * a[hint + lastofs] <= key < a[hint + ofs]
1487 */
1488 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1489 while (ofs < maxofs) {
1490 IFLT(key, a[ofs])
1491 break;
1492 /* a[hint + ofs] <= key */
1493 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001494 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 }
1497 if (ofs > maxofs)
1498 ofs = maxofs;
1499 /* Translate back to offsets relative to &a[0]. */
1500 lastofs += hint;
1501 ofs += hint;
1502 }
1503 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1506 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1507 * right of lastofs but no farther right than ofs. Do a binary
1508 * search, with invariant a[lastofs-1] <= key < a[ofs].
1509 */
1510 ++lastofs;
1511 while (lastofs < ofs) {
1512 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 IFLT(key, a[m])
1515 ofs = m; /* key < a[m] */
1516 else
1517 lastofs = m+1; /* a[m] <= key */
1518 }
1519 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1520 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001521
1522fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001524}
1525
Tim Petersa64dc242002-08-01 02:13:36 +00001526/* Conceptually a MergeState's constructor. */
1527static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001528merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001531 if (has_keyfunc) {
1532 /* The temporary space for merging will need at most half the list
1533 * size rounded up. Use the minimum possible space so we can use the
1534 * rest of temparray for other things. In particular, if there is
1535 * enough extra space, listsort() will use it to store the keys.
1536 */
1537 ms->alloced = (list_size + 1) / 2;
1538
1539 /* ms->alloced describes how many keys will be stored at
1540 ms->temparray, but we also need to store the values. Hence,
1541 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1542 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1543 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1544 ms->a.values = &ms->temparray[ms->alloced];
1545 }
1546 else {
1547 ms->alloced = MERGESTATE_TEMP_SIZE;
1548 ms->a.values = NULL;
1549 }
1550 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 ms->n = 0;
1552 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001553}
1554
1555/* Free all the temp memory owned by the MergeState. This must be called
1556 * when you're done with a MergeState, and may be called before then if
1557 * you want to free the temp memory early.
1558 */
1559static void
1560merge_freemem(MergeState *ms)
1561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001563 if (ms->a.keys != ms->temparray)
1564 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001565}
1566
1567/* Ensure enough temp memory for 'need' array slots is available.
1568 * Returns 0 on success and -1 if the memory can't be gotten.
1569 */
1570static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001571merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001572{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001573 int multiplier;
1574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 assert(ms != NULL);
1576 if (need <= ms->alloced)
1577 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001578
1579 multiplier = ms->a.values != NULL ? 2 : 1;
1580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 /* Don't realloc! That can cost cycles to copy the old data, but
1582 * we don't care what's in the block.
1583 */
1584 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001585 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 PyErr_NoMemory();
1587 return -1;
1588 }
embg1e34da42018-01-28 20:03:23 -07001589 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001590 * sizeof(PyObject *));
1591 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001593 if (ms->a.values != NULL)
1594 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 return 0;
1596 }
1597 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001599}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1601 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001602
Daniel Stutzbach98338222010-12-02 21:55:33 +00001603/* Merge the na elements starting at ssa with the nb elements starting at
1604 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1605 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1606 * should have na <= nb. See listsort.txt for more info. Return 0 if
1607 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001608 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001609static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001610merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1611 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001614 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 int result = -1; /* guilty until proved innocent */
1616 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001617
Daniel Stutzbach98338222010-12-02 21:55:33 +00001618 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1619 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 if (MERGE_GETMEM(ms, na) < 0)
1621 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001622 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1623 dest = ssa;
1624 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001625
Daniel Stutzbach98338222010-12-02 21:55:33 +00001626 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 --nb;
1628 if (nb == 0)
1629 goto Succeed;
1630 if (na == 1)
1631 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 min_gallop = ms->min_gallop;
1634 for (;;) {
1635 Py_ssize_t acount = 0; /* # of times A won in a row */
1636 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 /* Do the straightforward thing until (if ever) one run
1639 * appears to win consistently.
1640 */
1641 for (;;) {
1642 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001643 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 if (k) {
1645 if (k < 0)
1646 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001647 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 ++bcount;
1649 acount = 0;
1650 --nb;
1651 if (nb == 0)
1652 goto Succeed;
1653 if (bcount >= min_gallop)
1654 break;
1655 }
1656 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001657 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 ++acount;
1659 bcount = 0;
1660 --na;
1661 if (na == 1)
1662 goto CopyB;
1663 if (acount >= min_gallop)
1664 break;
1665 }
1666 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 /* One run is winning so consistently that galloping may
1669 * be a huge win. So try that, and continue galloping until
1670 * (if ever) neither run appears to be winning consistently
1671 * anymore.
1672 */
1673 ++min_gallop;
1674 do {
1675 assert(na > 1 && nb > 0);
1676 min_gallop -= min_gallop > 1;
1677 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001678 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 acount = k;
1680 if (k) {
1681 if (k < 0)
1682 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001683 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1684 sortslice_advance(&dest, k);
1685 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 na -= k;
1687 if (na == 1)
1688 goto CopyB;
1689 /* na==0 is impossible now if the comparison
1690 * function is consistent, but we can't assume
1691 * that it is.
1692 */
1693 if (na == 0)
1694 goto Succeed;
1695 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001696 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 --nb;
1698 if (nb == 0)
1699 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001700
embg1e34da42018-01-28 20:03:23 -07001701 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 bcount = k;
1703 if (k) {
1704 if (k < 0)
1705 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001706 sortslice_memmove(&dest, 0, &ssb, 0, k);
1707 sortslice_advance(&dest, k);
1708 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 nb -= k;
1710 if (nb == 0)
1711 goto Succeed;
1712 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001713 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 --na;
1715 if (na == 1)
1716 goto CopyB;
1717 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1718 ++min_gallop; /* penalize it for leaving galloping mode */
1719 ms->min_gallop = min_gallop;
1720 }
Tim Petersa64dc242002-08-01 02:13:36 +00001721Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001723Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001725 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001727CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001729 /* The last element of ssa belongs at the end of the merge. */
1730 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1731 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001733}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001734
Daniel Stutzbach98338222010-12-02 21:55:33 +00001735/* Merge the na elements starting at pa with the nb elements starting at
1736 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1737 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1738 * should have na >= nb. See listsort.txt for more info. Return 0 if
1739 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001740 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001741static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001742merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1743 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001746 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001749
Daniel Stutzbach98338222010-12-02 21:55:33 +00001750 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1751 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 if (MERGE_GETMEM(ms, nb) < 0)
1753 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001754 dest = ssb;
1755 sortslice_advance(&dest, nb-1);
1756 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1757 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001759 ssb.keys = ms->a.keys + nb - 1;
1760 if (ssb.values != NULL)
1761 ssb.values = ms->a.values + nb - 1;
1762 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001763
Daniel Stutzbach98338222010-12-02 21:55:33 +00001764 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 --na;
1766 if (na == 0)
1767 goto Succeed;
1768 if (nb == 1)
1769 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 min_gallop = ms->min_gallop;
1772 for (;;) {
1773 Py_ssize_t acount = 0; /* # of times A won in a row */
1774 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 /* Do the straightforward thing until (if ever) one run
1777 * appears to win consistently.
1778 */
1779 for (;;) {
1780 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001781 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 if (k) {
1783 if (k < 0)
1784 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001785 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 ++acount;
1787 bcount = 0;
1788 --na;
1789 if (na == 0)
1790 goto Succeed;
1791 if (acount >= min_gallop)
1792 break;
1793 }
1794 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001795 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 ++bcount;
1797 acount = 0;
1798 --nb;
1799 if (nb == 1)
1800 goto CopyA;
1801 if (bcount >= min_gallop)
1802 break;
1803 }
1804 }
Tim Petersa64dc242002-08-01 02:13:36 +00001805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 /* One run is winning so consistently that galloping may
1807 * be a huge win. So try that, and continue galloping until
1808 * (if ever) neither run appears to be winning consistently
1809 * anymore.
1810 */
1811 ++min_gallop;
1812 do {
1813 assert(na > 0 && nb > 1);
1814 min_gallop -= min_gallop > 1;
1815 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001816 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 if (k < 0)
1818 goto Fail;
1819 k = na - k;
1820 acount = k;
1821 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001822 sortslice_advance(&dest, -k);
1823 sortslice_advance(&ssa, -k);
1824 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 na -= k;
1826 if (na == 0)
1827 goto Succeed;
1828 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001829 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 --nb;
1831 if (nb == 1)
1832 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001833
embg1e34da42018-01-28 20:03:23 -07001834 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 if (k < 0)
1836 goto Fail;
1837 k = nb - k;
1838 bcount = k;
1839 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001840 sortslice_advance(&dest, -k);
1841 sortslice_advance(&ssb, -k);
1842 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 nb -= k;
1844 if (nb == 1)
1845 goto CopyA;
1846 /* nb==0 is impossible now if the comparison
1847 * function is consistent, but we can't assume
1848 * that it is.
1849 */
1850 if (nb == 0)
1851 goto Succeed;
1852 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001853 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 --na;
1855 if (na == 0)
1856 goto Succeed;
1857 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1858 ++min_gallop; /* penalize it for leaving galloping mode */
1859 ms->min_gallop = min_gallop;
1860 }
Tim Petersa64dc242002-08-01 02:13:36 +00001861Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001863Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001865 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001867CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001869 /* The first element of ssb belongs at the front of the merge. */
1870 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1871 sortslice_advance(&dest, -na);
1872 sortslice_advance(&ssa, -na);
1873 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001875}
1876
1877/* Merge the two runs at stack indices i and i+1.
1878 * Returns 0 on success, -1 on error.
1879 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001880static Py_ssize_t
1881merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001882{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001883 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 Py_ssize_t na, nb;
1885 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 assert(ms != NULL);
1888 assert(ms->n >= 2);
1889 assert(i >= 0);
1890 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001891
Daniel Stutzbach98338222010-12-02 21:55:33 +00001892 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001894 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 nb = ms->pending[i+1].len;
1896 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001897 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 /* Record the length of the combined runs; if i is the 3rd-last
1900 * run now, also slide over the last run (which isn't involved
1901 * in this merge). The current run i+1 goes away in any case.
1902 */
1903 ms->pending[i].len = na + nb;
1904 if (i == ms->n - 3)
1905 ms->pending[i+1] = ms->pending[i+2];
1906 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 /* Where does b start in a? Elements in a before that can be
1909 * ignored (already in place).
1910 */
embg1e34da42018-01-28 20:03:23 -07001911 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 if (k < 0)
1913 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001914 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 na -= k;
1916 if (na == 0)
1917 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 /* Where does a end in b? Elements in b after that can be
1920 * ignored (already in place).
1921 */
embg1e34da42018-01-28 20:03:23 -07001922 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 if (nb <= 0)
1924 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 /* Merge what remains of the runs, using a temp array with
1927 * min(na, nb) elements.
1928 */
1929 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001930 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001932 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001933}
1934
1935/* Examine the stack of runs waiting to be merged, merging adjacent runs
1936 * until the stack invariants are re-established:
1937 *
1938 * 1. len[-3] > len[-2] + len[-1]
1939 * 2. len[-2] > len[-1]
1940 *
1941 * See listsort.txt for more info.
1942 *
1943 * Returns 0 on success, -1 on error.
1944 */
1945static int
1946merge_collapse(MergeState *ms)
1947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 assert(ms);
1951 while (ms->n > 1) {
1952 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001953 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1954 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 if (p[n-1].len < p[n+1].len)
1956 --n;
1957 if (merge_at(ms, n) < 0)
1958 return -1;
1959 }
1960 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001961 if (merge_at(ms, n) < 0)
1962 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 }
1964 else
1965 break;
1966 }
1967 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001968}
1969
1970/* Regardless of invariants, merge all runs on the stack until only one
1971 * remains. This is used at the end of the mergesort.
1972 *
1973 * Returns 0 on success, -1 on error.
1974 */
1975static int
1976merge_force_collapse(MergeState *ms)
1977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 assert(ms);
1981 while (ms->n > 1) {
1982 Py_ssize_t n = ms->n - 2;
1983 if (n > 0 && p[n-1].len < p[n+1].len)
1984 --n;
1985 if (merge_at(ms, n) < 0)
1986 return -1;
1987 }
1988 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001989}
1990
1991/* Compute a good value for the minimum run length; natural runs shorter
1992 * than this are boosted artificially via binary insertion.
1993 *
1994 * If n < 64, return n (it's too small to bother with fancy stuff).
1995 * Else if n is an exact power of 2, return 32.
1996 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1997 * strictly less than, an exact power of 2.
1998 *
1999 * See listsort.txt for more info.
2000 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002001static Py_ssize_t
2002merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00002003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00002005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 assert(n >= 0);
2007 while (n >= 64) {
2008 r |= n & 1;
2009 n >>= 1;
2010 }
2011 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002012}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00002013
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002014static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00002015reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002016{
Daniel Stutzbach98338222010-12-02 21:55:33 +00002017 reverse_slice(s->keys, &s->keys[n]);
2018 if (s->values != NULL)
2019 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002020}
2021
embg1e34da42018-01-28 20:03:23 -07002022/* Here we define custom comparison functions to optimize for the cases one commonly
2023 * encounters in practice: homogeneous lists, often of one of the basic types. */
2024
2025/* This struct holds the comparison function and helper functions
2026 * selected in the pre-sort check. */
2027
2028/* These are the special case compare functions.
2029 * ms->key_compare will always point to one of these: */
2030
2031/* Heterogeneous compare: default, always safe to fall back on. */
2032static int
2033safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2034{
2035 /* No assumptions necessary! */
2036 return PyObject_RichCompareBool(v, w, Py_LT);
2037}
2038
2039/* Homogeneous compare: safe for any two compareable objects of the same type.
2040 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2041 * pre-sort check.)
2042 */
2043static int
2044unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2045{
2046 PyObject *res_obj; int res;
2047
2048 /* No assumptions, because we check first: */
2049 if (v->ob_type->tp_richcompare != ms->key_richcompare)
2050 return PyObject_RichCompareBool(v, w, Py_LT);
2051
2052 assert(ms->key_richcompare != NULL);
2053 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2054
2055 if (res_obj == Py_NotImplemented) {
2056 Py_DECREF(res_obj);
2057 return PyObject_RichCompareBool(v, w, Py_LT);
2058 }
2059 if (res_obj == NULL)
2060 return -1;
2061
2062 if (PyBool_Check(res_obj)) {
2063 res = (res_obj == Py_True);
2064 }
2065 else {
2066 res = PyObject_IsTrue(res_obj);
2067 }
2068 Py_DECREF(res_obj);
2069
2070 /* Note that we can't assert
2071 * res == PyObject_RichCompareBool(v, w, Py_LT);
2072 * because of evil compare functions like this:
2073 * lambda a, b: int(random.random() * 3) - 1)
2074 * (which is actually in test_sort.py) */
2075 return res;
2076}
2077
2078/* Latin string compare: safe for any two latin (one byte per char) strings. */
2079static int
2080unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2081{
Victor Stinner8017b802018-01-29 13:47:06 +01002082 Py_ssize_t len;
2083 int res;
embg1e34da42018-01-28 20:03:23 -07002084
2085 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2086 assert(v->ob_type == w->ob_type);
2087 assert(v->ob_type == &PyUnicode_Type);
2088 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2089 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2090
2091 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2092 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2093
2094 res = (res != 0 ?
2095 res < 0 :
2096 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2097
2098 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2099 return res;
2100}
2101
2102/* Bounded int compare: compare any two longs that fit in a single machine word. */
2103static int
2104unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2105{
2106 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2107
2108 /* Modified from Objects/longobject.c:long_compare, assuming: */
2109 assert(v->ob_type == w->ob_type);
2110 assert(v->ob_type == &PyLong_Type);
2111 assert(Py_ABS(Py_SIZE(v)) <= 1);
2112 assert(Py_ABS(Py_SIZE(w)) <= 1);
2113
2114 vl = (PyLongObject*)v;
2115 wl = (PyLongObject*)w;
2116
2117 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2118 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2119
2120 if (Py_SIZE(vl) < 0)
2121 v0 = -v0;
2122 if (Py_SIZE(wl) < 0)
2123 w0 = -w0;
2124
2125 res = v0 < w0;
2126 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2127 return res;
2128}
2129
2130/* Float compare: compare any two floats. */
2131static int
2132unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2133{
2134 int res;
2135
2136 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2137 assert(v->ob_type == w->ob_type);
2138 assert(v->ob_type == &PyFloat_Type);
2139
2140 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2141 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2142 return res;
2143}
2144
2145/* Tuple compare: compare *any* two tuples, using
2146 * ms->tuple_elem_compare to compare the first elements, which is set
2147 * using the same pre-sort check as we use for ms->key_compare,
2148 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2149 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2150 * that most tuple compares don't involve x[1:]. */
2151static int
2152unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2153{
2154 PyTupleObject *vt, *wt;
2155 Py_ssize_t i, vlen, wlen;
2156 int k;
2157
2158 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2159 assert(v->ob_type == w->ob_type);
2160 assert(v->ob_type == &PyTuple_Type);
2161 assert(Py_SIZE(v) > 0);
2162 assert(Py_SIZE(w) > 0);
2163
2164 vt = (PyTupleObject *)v;
2165 wt = (PyTupleObject *)w;
2166
2167 vlen = Py_SIZE(vt);
2168 wlen = Py_SIZE(wt);
2169
2170 for (i = 0; i < vlen && i < wlen; i++) {
2171 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2172 if (k < 0)
2173 return -1;
2174 if (!k)
2175 break;
2176 }
2177
2178 if (i >= vlen || i >= wlen)
2179 return vlen < wlen;
2180
2181 if (i == 0)
2182 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2183 else
2184 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2185}
2186
Tim Petersa64dc242002-08-01 02:13:36 +00002187/* An adaptive, stable, natural mergesort. See listsort.txt.
2188 * Returns Py_None on success, NULL on error. Even in case of error, the
2189 * list will be some permutation of its input state (nothing is lost or
2190 * duplicated).
2191 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002192/*[clinic input]
2193list.sort
2194
2195 *
2196 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002197 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002198
Tim Hoffmann5c224762019-06-01 06:10:02 +02002199Sort the list in ascending order and return None.
2200
2201The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
2202order of two equal elements is maintained).
2203
2204If a key function is given, apply it once to each list item and sort them,
2205ascending or descending, according to their function values.
2206
2207The reverse flag can be set to sort in descending order.
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002208[clinic start generated code]*/
2209
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002210static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002211list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Tim Hoffmann5c224762019-06-01 06:10:02 +02002212/*[clinic end generated code: output=57b9f9c5e23fbe42 input=cb56cd179a713060]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 Py_ssize_t nremaining;
2216 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002217 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 Py_ssize_t saved_ob_size, saved_allocated;
2219 PyObject **saved_ob_item;
2220 PyObject **final_ob_item;
2221 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002223 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002226 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 if (keyfunc == Py_None)
2228 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 /* The list is temporarily made empty, so that mutations performed
2231 * by comparison functions can't affect the slice of memory we're
2232 * sorting (allowing mutations during sorting is a core-dump
2233 * factory, since ob_item may change).
2234 */
2235 saved_ob_size = Py_SIZE(self);
2236 saved_ob_item = self->ob_item;
2237 saved_allocated = self->allocated;
2238 Py_SIZE(self) = 0;
2239 self->ob_item = NULL;
2240 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002241
Daniel Stutzbach98338222010-12-02 21:55:33 +00002242 if (keyfunc == NULL) {
2243 keys = NULL;
2244 lo.keys = saved_ob_item;
2245 lo.values = NULL;
2246 }
2247 else {
2248 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2249 /* Leverage stack space we allocated but won't otherwise use */
2250 keys = &ms.temparray[saved_ob_size+1];
2251 else {
2252 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002253 if (keys == NULL) {
2254 PyErr_NoMemory();
2255 goto keyfunc_fail;
2256 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002258
2259 for (i = 0; i < saved_ob_size ; i++) {
Jeroen Demeyer196a5302019-07-04 12:31:34 +02002260 keys[i] = _PyObject_CallOneArg(keyfunc, saved_ob_item[i]);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002261 if (keys[i] == NULL) {
2262 for (i=i-1 ; i>=0 ; i--)
2263 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002264 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002265 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002266 goto keyfunc_fail;
2267 }
2268 }
2269
2270 lo.keys = keys;
2271 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002273
embg1e34da42018-01-28 20:03:23 -07002274
2275 /* The pre-sort check: here's where we decide which compare function to use.
2276 * How much optimization is safe? We test for homogeneity with respect to
2277 * several properties that are expensive to check at compare-time, and
2278 * set ms appropriately. */
2279 if (saved_ob_size > 1) {
2280 /* Assume the first element is representative of the whole list. */
2281 int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2282 Py_SIZE(lo.keys[0]) > 0);
2283
2284 PyTypeObject* key_type = (keys_are_in_tuples ?
2285 PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2286 lo.keys[0]->ob_type);
2287
2288 int keys_are_all_same_type = 1;
2289 int strings_are_latin = 1;
2290 int ints_are_bounded = 1;
2291
2292 /* Prove that assumption by checking every key. */
embg1e34da42018-01-28 20:03:23 -07002293 for (i=0; i < saved_ob_size; i++) {
2294
2295 if (keys_are_in_tuples &&
2296 !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2297 keys_are_in_tuples = 0;
2298 keys_are_all_same_type = 0;
2299 break;
2300 }
2301
2302 /* Note: for lists of tuples, key is the first element of the tuple
2303 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2304 * for lists of tuples in the if-statement directly above. */
2305 PyObject *key = (keys_are_in_tuples ?
2306 PyTuple_GET_ITEM(lo.keys[i], 0) :
2307 lo.keys[i]);
2308
2309 if (key->ob_type != key_type) {
2310 keys_are_all_same_type = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002311 /* If keys are in tuple we must loop over the whole list to make
2312 sure all items are tuples */
2313 if (!keys_are_in_tuples) {
2314 break;
2315 }
embg1e34da42018-01-28 20:03:23 -07002316 }
2317
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002318 if (keys_are_all_same_type) {
2319 if (key_type == &PyLong_Type &&
2320 ints_are_bounded &&
2321 Py_ABS(Py_SIZE(key)) > 1) {
2322
embg1e34da42018-01-28 20:03:23 -07002323 ints_are_bounded = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002324 }
2325 else if (key_type == &PyUnicode_Type &&
2326 strings_are_latin &&
2327 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {
2328
2329 strings_are_latin = 0;
2330 }
2331 }
embg1e34da42018-01-28 20:03:23 -07002332 }
embg1e34da42018-01-28 20:03:23 -07002333
2334 /* Choose the best compare, given what we now know about the keys. */
2335 if (keys_are_all_same_type) {
2336
2337 if (key_type == &PyUnicode_Type && strings_are_latin) {
2338 ms.key_compare = unsafe_latin_compare;
2339 }
2340 else if (key_type == &PyLong_Type && ints_are_bounded) {
2341 ms.key_compare = unsafe_long_compare;
2342 }
2343 else if (key_type == &PyFloat_Type) {
2344 ms.key_compare = unsafe_float_compare;
2345 }
2346 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2347 ms.key_compare = unsafe_object_compare;
2348 }
Zackery Spytzebc793d2019-02-21 00:47:14 -07002349 else {
2350 ms.key_compare = safe_object_compare;
2351 }
embg1e34da42018-01-28 20:03:23 -07002352 }
2353 else {
2354 ms.key_compare = safe_object_compare;
2355 }
2356
2357 if (keys_are_in_tuples) {
2358 /* Make sure we're not dealing with tuples of tuples
2359 * (remember: here, key_type refers list [key[0] for key in keys]) */
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002360 if (key_type == &PyTuple_Type) {
embg1e34da42018-01-28 20:03:23 -07002361 ms.tuple_elem_compare = safe_object_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002362 }
2363 else {
embg1e34da42018-01-28 20:03:23 -07002364 ms.tuple_elem_compare = ms.key_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002365 }
embg1e34da42018-01-28 20:03:23 -07002366
2367 ms.key_compare = unsafe_tuple_compare;
2368 }
2369 }
2370 /* End of pre-sort check: ms is now set properly! */
2371
Daniel Stutzbach98338222010-12-02 21:55:33 +00002372 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 nremaining = saved_ob_size;
2375 if (nremaining < 2)
2376 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002377
Benjamin Peterson05380642010-08-23 19:35:39 +00002378 /* Reverse sort stability achieved by initially reversing the list,
2379 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002380 if (reverse) {
2381 if (keys != NULL)
2382 reverse_slice(&keys[0], &keys[saved_ob_size]);
2383 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2384 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 /* March over the array once, left to right, finding natural runs,
2387 * and extending short natural runs to minrun elements.
2388 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 minrun = merge_compute_minrun(nremaining);
2390 do {
2391 int descending;
2392 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002395 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 if (n < 0)
2397 goto fail;
2398 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002399 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 /* If short, extend to min(minrun, nremaining). */
2401 if (n < minrun) {
2402 const Py_ssize_t force = nremaining <= minrun ?
2403 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002404 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 goto fail;
2406 n = force;
2407 }
2408 /* Push run onto pending-runs stack, and maybe merge. */
2409 assert(ms.n < MAX_MERGE_PENDING);
2410 ms.pending[ms.n].base = lo;
2411 ms.pending[ms.n].len = n;
2412 ++ms.n;
2413 if (merge_collapse(&ms) < 0)
2414 goto fail;
2415 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002416 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 nremaining -= n;
2418 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 if (merge_force_collapse(&ms) < 0)
2421 goto fail;
2422 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002423 assert(keys == NULL
2424 ? ms.pending[0].base.keys == saved_ob_item
2425 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002427 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002428
2429succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002431fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002432 if (keys != NULL) {
2433 for (i = 0; i < saved_ob_size; i++)
2434 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002435 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002436 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 if (self->allocated != -1 && result != NULL) {
2440 /* The user mucked with the list during the sort,
2441 * and we don't already have another error to report.
2442 */
2443 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2444 result = NULL;
2445 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 if (reverse && saved_ob_size > 1)
2448 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002451
Daniel Stutzbach98338222010-12-02 21:55:33 +00002452keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 final_ob_item = self->ob_item;
2454 i = Py_SIZE(self);
2455 Py_SIZE(self) = saved_ob_size;
2456 self->ob_item = saved_ob_item;
2457 self->allocated = saved_allocated;
2458 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002459 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 guarantee that the list is really empty when it returns */
2461 while (--i >= 0) {
2462 Py_XDECREF(final_ob_item[i]);
2463 }
2464 PyMem_FREE(final_ob_item);
2465 }
2466 Py_XINCREF(result);
2467 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002468}
Tim Peters330f9e92002-07-19 07:05:44 +00002469#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002470#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002471
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002472int
Fred Drakea2f55112000-07-09 15:16:51 +00002473PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 if (v == NULL || !PyList_Check(v)) {
2476 PyErr_BadInternalCall();
2477 return -1;
2478 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002479 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 if (v == NULL)
2481 return -1;
2482 Py_DECREF(v);
2483 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002484}
2485
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002486/*[clinic input]
2487list.reverse
2488
2489Reverse *IN PLACE*.
2490[clinic start generated code]*/
2491
Guido van Rossumb86c5492001-02-12 22:06:02 +00002492static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002493list_reverse_impl(PyListObject *self)
2494/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 if (Py_SIZE(self) > 1)
2497 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2498 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002499}
2500
Guido van Rossum84c76f51990-10-30 13:32:20 +00002501int
Fred Drakea2f55112000-07-09 15:16:51 +00002502PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 if (v == NULL || !PyList_Check(v)) {
2507 PyErr_BadInternalCall();
2508 return -1;
2509 }
2510 if (Py_SIZE(self) > 1)
2511 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2512 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002513}
2514
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002515PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002516PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 if (v == NULL || !PyList_Check(v)) {
2519 PyErr_BadInternalCall();
2520 return NULL;
2521 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +05002522 return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002523}
2524
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002525/*[clinic input]
2526list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002527
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002528 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002529 start: slice_index(accept={int}) = 0
2530 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002531 /
2532
2533Return first index of value.
2534
2535Raises ValueError if the value is not present.
2536[clinic start generated code]*/
2537
2538static PyObject *
2539list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2540 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002541/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002542{
2543 Py_ssize_t i;
2544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 if (start < 0) {
2546 start += Py_SIZE(self);
2547 if (start < 0)
2548 start = 0;
2549 }
2550 if (stop < 0) {
2551 stop += Py_SIZE(self);
2552 if (stop < 0)
2553 stop = 0;
2554 }
2555 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002556 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 if (cmp > 0)
2558 return PyLong_FromSsize_t(i);
2559 else if (cmp < 0)
2560 return NULL;
2561 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002562 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002564}
2565
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002566/*[clinic input]
2567list.count
2568
2569 value: object
2570 /
2571
2572Return number of occurrences of value.
2573[clinic start generated code]*/
2574
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002575static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002576list_count(PyListObject *self, PyObject *value)
2577/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 Py_ssize_t count = 0;
2580 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002583 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 if (cmp > 0)
2585 count++;
2586 else if (cmp < 0)
2587 return NULL;
2588 }
2589 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002590}
2591
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002592/*[clinic input]
2593list.remove
2594
2595 value: object
2596 /
2597
2598Remove first occurrence of value.
2599
2600Raises ValueError if the value is not present.
2601[clinic start generated code]*/
2602
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002603static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002604list_remove(PyListObject *self, PyObject *value)
2605/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002610 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 if (cmp > 0) {
2612 if (list_ass_slice(self, i, i+1,
2613 (PyObject *)NULL) == 0)
2614 Py_RETURN_NONE;
2615 return NULL;
2616 }
2617 else if (cmp < 0)
2618 return NULL;
2619 }
2620 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2621 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002622}
2623
Jeremy Hylton8caad492000-06-23 14:18:11 +00002624static int
2625list_traverse(PyListObject *o, visitproc visit, void *arg)
2626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 for (i = Py_SIZE(o); --i >= 0; )
2630 Py_VISIT(o->ob_item[i]);
2631 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002632}
2633
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002634static PyObject *
2635list_richcompare(PyObject *v, PyObject *w, int op)
2636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 PyListObject *vl, *wl;
2638 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002639
Brian Curtindfc80e32011-08-10 20:28:54 -05002640 if (!PyList_Check(v) || !PyList_Check(w))
2641 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 vl = (PyListObject *)v;
2644 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2647 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002649 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 else
stratakise8b19652017-11-02 11:32:54 +01002651 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 /* Search for the first index where items are different */
2655 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2656 int k = PyObject_RichCompareBool(vl->ob_item[i],
2657 wl->ob_item[i], Py_EQ);
2658 if (k < 0)
2659 return NULL;
2660 if (!k)
2661 break;
2662 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2665 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002666 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 /* We have an item that differs -- shortcuts for EQ/NE */
2670 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002671 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 }
2673 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002674 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 /* Compare the final item again using the proper operator */
2678 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002679}
2680
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002681/*[clinic input]
2682list.__init__
2683
2684 iterable: object(c_default="NULL") = ()
2685 /
2686
2687Built-in mutable sequence.
2688
2689If no argument is given, the constructor creates a new empty list.
2690The argument must be an iterable if specified.
2691[clinic start generated code]*/
2692
Tim Peters6d6c1a32001-08-02 04:15:00 +00002693static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002694list___init___impl(PyListObject *self, PyObject *iterable)
2695/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 /* Verify list invariants established by PyType_GenericAlloc() */
2698 assert(0 <= Py_SIZE(self));
2699 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2700 assert(self->ob_item != NULL ||
2701 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 /* Empty previous contents */
2704 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002705 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002707 if (iterable != NULL) {
Pablo Galindo372d7052018-10-28 20:16:26 +00002708 if (_PyObject_HasLen(iterable)) {
2709 Py_ssize_t iter_len = PyObject_Size(iterable);
2710 if (iter_len == -1) {
2711 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2712 return -1;
2713 }
2714 PyErr_Clear();
2715 }
2716 if (iter_len > 0 && self->ob_item == NULL
2717 && list_preallocate_exact(self, iter_len)) {
2718 return -1;
2719 }
2720 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002721 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 if (rv == NULL)
2723 return -1;
2724 Py_DECREF(rv);
2725 }
2726 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002727}
2728
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002729/*[clinic input]
2730list.__sizeof__
2731
2732Return the size of the list in memory, in bytes.
2733[clinic start generated code]*/
2734
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002735static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002736list___sizeof___impl(PyListObject *self)
2737/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002740
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002741 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002743}
2744
Raymond Hettinger1021c442003-11-07 15:38:09 +00002745static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002746static PyObject *list_subscript(PyListObject*, PyObject*);
2747
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002748static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002749 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2750 LIST___REVERSED___METHODDEF
2751 LIST___SIZEOF___METHODDEF
2752 LIST_CLEAR_METHODDEF
2753 LIST_COPY_METHODDEF
2754 LIST_APPEND_METHODDEF
2755 LIST_INSERT_METHODDEF
2756 LIST_EXTEND_METHODDEF
2757 LIST_POP_METHODDEF
2758 LIST_REMOVE_METHODDEF
2759 LIST_INDEX_METHODDEF
2760 LIST_COUNT_METHODDEF
2761 LIST_REVERSE_METHODDEF
2762 LIST_SORT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002764};
2765
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002766static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 (lenfunc)list_length, /* sq_length */
2768 (binaryfunc)list_concat, /* sq_concat */
2769 (ssizeargfunc)list_repeat, /* sq_repeat */
2770 (ssizeargfunc)list_item, /* sq_item */
2771 0, /* sq_slice */
2772 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2773 0, /* sq_ass_slice */
2774 (objobjproc)list_contains, /* sq_contains */
2775 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2776 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002777};
2778
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002779static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002780list_subscript(PyListObject* self, PyObject* item)
2781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 if (PyIndex_Check(item)) {
2783 Py_ssize_t i;
2784 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2785 if (i == -1 && PyErr_Occurred())
2786 return NULL;
2787 if (i < 0)
2788 i += PyList_GET_SIZE(self);
2789 return list_item(self, i);
2790 }
2791 else if (PySlice_Check(item)) {
HongWeipeng3c87a662019-09-08 18:15:56 +08002792 Py_ssize_t start, stop, step, slicelength, i;
2793 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 PyObject* result;
2795 PyObject* it;
2796 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002797
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002798 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 return NULL;
2800 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002801 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2802 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 if (slicelength <= 0) {
2805 return PyList_New(0);
2806 }
2807 else if (step == 1) {
2808 return list_slice(self, start, stop);
2809 }
2810 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002811 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 src = self->ob_item;
2815 dest = ((PyListObject *)result)->ob_item;
2816 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002817 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 it = src[cur];
2819 Py_INCREF(it);
2820 dest[i] = it;
2821 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002822 Py_SIZE(result) = slicelength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 return result;
2824 }
2825 }
2826 else {
2827 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002828 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 item->ob_type->tp_name);
2830 return NULL;
2831 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002832}
2833
Tim Peters3b01a122002-07-19 02:35:45 +00002834static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002835list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 if (PyIndex_Check(item)) {
2838 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2839 if (i == -1 && PyErr_Occurred())
2840 return -1;
2841 if (i < 0)
2842 i += PyList_GET_SIZE(self);
2843 return list_ass_item(self, i, value);
2844 }
2845 else if (PySlice_Check(item)) {
2846 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002847
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002848 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 return -1;
2850 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002851 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2852 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 if (step == 1)
2855 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 /* Make sure s[5:2] = [..] inserts at the right place:
2858 before 5, not before 2. */
2859 if ((step < 0 && start < stop) ||
2860 (step > 0 && start > stop))
2861 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 if (value == NULL) {
2864 /* delete slice */
2865 PyObject **garbage;
2866 size_t cur;
2867 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002868 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 if (slicelength <= 0)
2871 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 if (step < 0) {
2874 stop = start + 1;
2875 start = stop + step*(slicelength - 1) - 1;
2876 step = -step;
2877 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 garbage = (PyObject**)
2880 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2881 if (!garbage) {
2882 PyErr_NoMemory();
2883 return -1;
2884 }
Tim Peters3b01a122002-07-19 02:35:45 +00002885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 /* drawing pictures might help understand these for
2887 loops. Basically, we memmove the parts of the
2888 list that are *not* part of the slice: step-1
2889 items for each item that is part of the slice,
2890 and then tail end of the list that was not
2891 covered by the slice */
2892 for (cur = start, i = 0;
2893 cur < (size_t)stop;
2894 cur += step, i++) {
2895 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 if (cur + step >= (size_t)Py_SIZE(self)) {
2900 lim = Py_SIZE(self) - cur - 1;
2901 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 memmove(self->ob_item + cur - i,
2904 self->ob_item + cur + 1,
2905 lim * sizeof(PyObject *));
2906 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002907 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 if (cur < (size_t)Py_SIZE(self)) {
2909 memmove(self->ob_item + cur - slicelength,
2910 self->ob_item + cur,
2911 (Py_SIZE(self) - cur) *
2912 sizeof(PyObject *));
2913 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002916 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 for (i = 0; i < slicelength; i++) {
2919 Py_DECREF(garbage[i]);
2920 }
2921 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002922
Victor Stinner35f28032013-11-21 12:16:35 +01002923 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 }
2925 else {
2926 /* assign slice */
2927 PyObject *ins, *seq;
2928 PyObject **garbage, **seqitems, **selfitems;
HongWeipeng3c87a662019-09-08 18:15:56 +08002929 Py_ssize_t i;
2930 size_t cur;
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}