blob: b210c005da13cf2bd9ca7cf56c453a574d322014 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* List object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01004#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01005#include "pycore_pystate.h"
Sergey Fedoseev234531b2019-02-25 21:59:12 +05006#include "pycore_tupleobject.h"
Victor Stinnere281f7d2018-11-01 02:30:36 +01007#include "pycore_accu.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00008
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00009#ifdef STDC_HEADERS
10#include <stddef.h>
11#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000012#include <sys/types.h> /* For size_t */
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000013#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014
Serhiy Storchakafdd42c42017-03-11 09:19:20 +020015/*[clinic input]
16class list "PyListObject *" "&PyList_Type"
17[clinic start generated code]*/
18/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f9b222678f9f71e0]*/
19
20#include "clinic/listobject.c.h"
21
Tim Peters8d9eb102004-07-31 02:24:20 +000022/* Ensure ob_item has room for at least newsize elements, and set
23 * ob_size to newsize. If newsize > ob_size on entry, the content
24 * of the new slots at exit is undefined heap trash; it's the caller's
Ezio Melotti13925002011-03-16 11:05:33 +020025 * responsibility to overwrite them with sane values.
Tim Peters8d9eb102004-07-31 02:24:20 +000026 * The number of allocated elements may grow, shrink, or stay the same.
27 * Failure is impossible if newsize <= self.allocated on entry, although
28 * that partly relies on an assumption that the system realloc() never
29 * fails when passed a number of bytes <= the number of bytes last
30 * allocated (the C standard doesn't guarantee this, but it's hard to
31 * imagine a realloc implementation where it wouldn't be true).
32 * Note that self->ob_item may change, and even if newsize is less
33 * than ob_size on entry.
34 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000035static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000036list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 PyObject **items;
Xiang Zhang4cee0492017-02-22 12:32:30 +080039 size_t new_allocated, num_allocated_bytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 /* Bypass realloc() when a previous overallocation is large enough
43 to accommodate the newsize. If the newsize falls lower than half
44 the allocated size, then proceed with the realloc() to shrink the list.
45 */
46 if (allocated >= newsize && newsize >= (allocated >> 1)) {
47 assert(self->ob_item != NULL || newsize == 0);
48 Py_SIZE(self) = newsize;
49 return 0;
50 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 /* This over-allocates proportional to the list size, making room
53 * for additional growth. The over-allocation is mild, but is
54 * enough to give linear-time amortized behavior over a long
55 * sequence of appends() in the presence of a poorly-performing
56 * system realloc().
57 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
Xiang Zhang4cee0492017-02-22 12:32:30 +080058 * Note: new_allocated won't overflow because the largest possible value
59 * is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 */
Xiang Zhang4cee0492017-02-22 12:32:30 +080061 new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
62 if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 PyErr_NoMemory();
64 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 if (newsize == 0)
68 new_allocated = 0;
Xiang Zhang4cee0492017-02-22 12:32:30 +080069 num_allocated_bytes = new_allocated * sizeof(PyObject *);
70 items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (items == NULL) {
72 PyErr_NoMemory();
73 return -1;
74 }
75 self->ob_item = items;
76 Py_SIZE(self) = newsize;
77 self->allocated = new_allocated;
78 return 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000079}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000080
Pablo Galindo372d7052018-10-28 20:16:26 +000081static int
82list_preallocate_exact(PyListObject *self, Py_ssize_t size)
83{
84 assert(self->ob_item == NULL);
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050085 assert(size > 0);
Pablo Galindo372d7052018-10-28 20:16:26 +000086
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050087 PyObject **items = PyMem_New(PyObject*, size);
Pablo Galindo372d7052018-10-28 20:16:26 +000088 if (items == NULL) {
89 PyErr_NoMemory();
90 return -1;
91 }
92 self->ob_item = items;
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050093 self->allocated = size;
Pablo Galindo372d7052018-10-28 20:16:26 +000094 return 0;
95}
96
Christian Heimes77c02eb2008-02-09 02:18:51 +000097/* Debug statistic to compare allocations with reuse through the free list */
98#undef SHOW_ALLOC_COUNT
99#ifdef SHOW_ALLOC_COUNT
100static size_t count_alloc = 0;
101static size_t count_reuse = 0;
102
103static void
104show_alloc(void)
105{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200106 PyInterpreterState *interp = _PyInterpreterState_Get();
Victor Stinner331a6a52019-05-27 16:39:22 +0200107 if (!interp->config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300108 return;
Victor Stinner25420fe2017-11-20 18:12:22 -0800109 }
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
112 count_alloc);
113 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
114 "d\n", count_reuse);
115 fprintf(stderr, "%.2f%% reuse rate\n\n",
116 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +0000117}
118#endif
119
Raymond Hettinger0468e412004-05-05 05:37:53 +0000120/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000121#ifndef PyList_MAXFREELIST
122#define PyList_MAXFREELIST 80
123#endif
124static PyListObject *free_list[PyList_MAXFREELIST];
125static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000126
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100127int
128PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100131 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 while (numfree) {
133 op = free_list[--numfree];
134 assert(PyList_CheckExact(op));
135 PyObject_GC_Del(op);
136 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100137 return ret;
138}
139
140void
141PyList_Fini(void)
142{
143 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000144}
145
David Malcolm49526f42012-06-22 14:55:41 -0400146/* Print summary info about the state of the optimized allocator */
147void
148_PyList_DebugMallocStats(FILE *out)
149{
150 _PyDebugAllocatorStats(out,
151 "free PyListObject",
152 numfree, sizeof(PyListObject));
153}
154
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000155PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000156PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 PyListObject *op;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000159#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 static int initialized = 0;
161 if (!initialized) {
162 Py_AtExit(show_alloc);
163 initialized = 1;
164 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000165#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 if (size < 0) {
168 PyErr_BadInternalCall();
169 return NULL;
170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (numfree) {
172 numfree--;
173 op = free_list[numfree];
174 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000175#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000177#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 } else {
179 op = PyObject_GC_New(PyListObject, &PyList_Type);
180 if (op == NULL)
181 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000182#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000184#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 }
186 if (size <= 0)
187 op->ob_item = NULL;
188 else {
Mark Dickinson5d132382016-08-21 08:55:15 +0100189 op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 if (op->ob_item == NULL) {
191 Py_DECREF(op);
192 return PyErr_NoMemory();
193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 }
195 Py_SIZE(op) = size;
196 op->allocated = size;
197 _PyObject_GC_TRACK(op);
198 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199}
200
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500201static PyObject *
202list_new_prealloc(Py_ssize_t size)
203{
204 PyListObject *op = (PyListObject *) PyList_New(0);
205 if (size == 0 || op == NULL) {
206 return (PyObject *) op;
207 }
208 assert(op->ob_item == NULL);
209 op->ob_item = PyMem_New(PyObject *, size);
210 if (op->ob_item == NULL) {
211 Py_DECREF(op);
212 return PyErr_NoMemory();
213 }
214 op->allocated = size;
215 return (PyObject *) op;
216}
217
Martin v. Löwis18e16552006-02-15 17:27:45 +0000218Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000219PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 if (!PyList_Check(op)) {
222 PyErr_BadInternalCall();
223 return -1;
224 }
225 else
226 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227}
228
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700229static inline int
230valid_index(Py_ssize_t i, Py_ssize_t limit)
231{
232 /* The cast to size_t lets us use just a single comparison
233 to check whether i is in the range: 0 <= i < limit.
234
235 See: Section 14.2 "Bounds Checking" in the Agner Fog
236 optimization manual found at:
237 https://www.agner.org/optimize/optimizing_cpp.pdf
238 */
239 return (size_t) i < (size_t) limit;
240}
241
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000242static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000243
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000245PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (!PyList_Check(op)) {
248 PyErr_BadInternalCall();
249 return NULL;
250 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700251 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 if (indexerr == NULL) {
253 indexerr = PyUnicode_FromString(
254 "list index out of range");
255 if (indexerr == NULL)
256 return NULL;
257 }
258 PyErr_SetObject(PyExc_IndexError, indexerr);
259 return NULL;
260 }
261 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262}
263
264int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200265PyList_SetItem(PyObject *op, Py_ssize_t i,
266 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200268 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 if (!PyList_Check(op)) {
270 Py_XDECREF(newitem);
271 PyErr_BadInternalCall();
272 return -1;
273 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700274 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 Py_XDECREF(newitem);
276 PyErr_SetString(PyExc_IndexError,
277 "list assignment index out of range");
278 return -1;
279 }
280 p = ((PyListObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300281 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283}
284
285static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000286ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_ssize_t i, n = Py_SIZE(self);
289 PyObject **items;
290 if (v == NULL) {
291 PyErr_BadInternalCall();
292 return -1;
293 }
294 if (n == PY_SSIZE_T_MAX) {
295 PyErr_SetString(PyExc_OverflowError,
296 "cannot add more objects to list");
297 return -1;
298 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000299
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800300 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (where < 0) {
304 where += n;
305 if (where < 0)
306 where = 0;
307 }
308 if (where > n)
309 where = n;
310 items = self->ob_item;
311 for (i = n; --i >= where; )
312 items[i+1] = items[i];
313 Py_INCREF(v);
314 items[where] = v;
315 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316}
317
318int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000319PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 if (!PyList_Check(op)) {
322 PyErr_BadInternalCall();
323 return -1;
324 }
325 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326}
327
Raymond Hettinger40a03822004-04-12 13:05:09 +0000328static int
329app1(PyListObject *self, PyObject *v)
330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 assert (v != NULL);
334 if (n == PY_SSIZE_T_MAX) {
335 PyErr_SetString(PyExc_OverflowError,
336 "cannot add more objects to list");
337 return -1;
338 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000339
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800340 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 Py_INCREF(v);
344 PyList_SET_ITEM(self, n, v);
345 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000346}
347
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348int
Fred Drakea2f55112000-07-09 15:16:51 +0000349PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 if (PyList_Check(op) && (newitem != NULL))
352 return app1((PyListObject *)op, newitem);
353 PyErr_BadInternalCall();
354 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355}
356
357/* Methods */
358
359static void
Fred Drakea2f55112000-07-09 15:16:51 +0000360list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 Py_ssize_t i;
363 PyObject_GC_UnTrack(op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200364 Py_TRASHCAN_BEGIN(op, list_dealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 if (op->ob_item != NULL) {
366 /* Do it backwards, for Christian Tismer.
367 There's a simple test case where somehow this reduces
368 thrashing when a *very* large list is created and
369 immediately deleted. */
370 i = Py_SIZE(op);
371 while (--i >= 0) {
372 Py_XDECREF(op->ob_item[i]);
373 }
374 PyMem_FREE(op->ob_item);
375 }
376 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
377 free_list[numfree++] = op;
378 else
379 Py_TYPE(op)->tp_free((PyObject *)op);
Jeroen Demeyer351c6742019-05-10 19:21:11 +0200380 Py_TRASHCAN_END
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381}
382
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000384list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 Py_ssize_t i;
Victor Stinner5c733472013-11-18 21:11:57 +0100387 PyObject *s;
Victor Stinner5c733472013-11-18 21:11:57 +0100388 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200389
390 if (Py_SIZE(v) == 0) {
391 return PyUnicode_FromString("[]");
392 }
393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 i = Py_ReprEnter((PyObject*)v);
395 if (i != 0) {
396 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
397 }
Tim Petersa7259592001-06-16 05:11:17 +0000398
Victor Stinner5c733472013-11-18 21:11:57 +0100399 _PyUnicodeWriter_Init(&writer);
400 writer.overallocate = 1;
Victor Stinnerb8fb1972013-11-18 22:15:44 +0100401 /* "[" + "1" + ", 2" * (len - 1) + "]" */
402 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +0000403
Victor Stinner5c733472013-11-18 21:11:57 +0100404 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200405 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 /* Do repr() on each element. Note that this may mutate the list,
408 so must refetch the list size on each iteration. */
409 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100410 if (i > 0) {
Victor Stinner4a587072013-11-19 12:54:53 +0100411 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
Victor Stinner5c733472013-11-18 21:11:57 +0100412 goto error;
413 }
414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner5c733472013-11-18 21:11:57 +0100416 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200417 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100418
419 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
420 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200421 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100422 }
423 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 }
Victor Stinner5c733472013-11-18 21:11:57 +0100425
Victor Stinner4d3f1092013-11-19 12:09:00 +0100426 writer.overallocate = 0;
Victor Stinner5c733472013-11-18 21:11:57 +0100427 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200428 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100431 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200432
433error:
Victor Stinner5c733472013-11-18 21:11:57 +0100434 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200435 Py_ReprLeave((PyObject *)v);
436 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437}
438
Martin v. Löwis18e16552006-02-15 17:27:45 +0000439static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000440list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443}
444
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000445static int
Fred Drakea2f55112000-07-09 15:16:51 +0000446list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 Py_ssize_t i;
449 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
452 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
453 Py_EQ);
454 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000455}
456
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000458list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700460 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (indexerr == NULL) {
462 indexerr = PyUnicode_FromString(
463 "list index out of range");
464 if (indexerr == NULL)
465 return NULL;
466 }
467 PyErr_SetObject(PyExc_IndexError, indexerr);
468 return NULL;
469 }
470 Py_INCREF(a->ob_item[i]);
471 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472}
473
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000474static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000475list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 PyListObject *np;
478 PyObject **src, **dest;
479 Py_ssize_t i, len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500481 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (np == NULL)
483 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 src = a->ob_item + ilow;
486 dest = np->ob_item;
487 for (i = 0; i < len; i++) {
488 PyObject *v = src[i];
489 Py_INCREF(v);
490 dest[i] = v;
491 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500492 Py_SIZE(np) = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494}
495
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000497PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (!PyList_Check(a)) {
500 PyErr_BadInternalCall();
501 return NULL;
502 }
Sergey Fedoseevef1b88b2019-02-21 11:51:52 +0500503 if (ilow < 0) {
504 ilow = 0;
505 }
506 else if (ilow > Py_SIZE(a)) {
507 ilow = Py_SIZE(a);
508 }
509 if (ihigh < ilow) {
510 ihigh = ilow;
511 }
512 else if (ihigh > Py_SIZE(a)) {
513 ihigh = Py_SIZE(a);
514 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000516}
517
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000518static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000519list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 Py_ssize_t size;
522 Py_ssize_t i;
523 PyObject **src, **dest;
524 PyListObject *np;
525 if (!PyList_Check(bb)) {
526 PyErr_Format(PyExc_TypeError,
527 "can only concatenate list (not \"%.200s\") to list",
528 bb->ob_type->tp_name);
529 return NULL;
530 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000531#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000532 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000534 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500535 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 if (np == NULL) {
537 return NULL;
538 }
539 src = a->ob_item;
540 dest = np->ob_item;
541 for (i = 0; i < Py_SIZE(a); i++) {
542 PyObject *v = src[i];
543 Py_INCREF(v);
544 dest[i] = v;
545 }
546 src = b->ob_item;
547 dest = np->ob_item + Py_SIZE(a);
548 for (i = 0; i < Py_SIZE(b); i++) {
549 PyObject *v = src[i];
550 Py_INCREF(v);
551 dest[i] = v;
552 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500553 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555#undef b
556}
557
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000559list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 Py_ssize_t i, j;
562 Py_ssize_t size;
563 PyListObject *np;
564 PyObject **p, **items;
565 PyObject *elem;
566 if (n < 0)
567 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100568 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100570 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 if (size == 0)
572 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500573 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 if (np == NULL)
575 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500578 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 elem = a->ob_item[0];
580 for (i = 0; i < n; i++) {
581 items[i] = elem;
582 Py_INCREF(elem);
583 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500585 else {
586 p = np->ob_item;
587 items = a->ob_item;
588 for (i = 0; i < n; i++) {
589 for (j = 0; j < Py_SIZE(a); j++) {
590 *p = items[j];
591 Py_INCREF(*p);
592 p++;
593 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 }
595 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500596 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000598}
599
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000600static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200601_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 Py_ssize_t i;
604 PyObject **item = a->ob_item;
605 if (item != NULL) {
606 /* Because XDECREF can recursively invoke operations on
607 this list, we make it empty first. */
608 i = Py_SIZE(a);
609 Py_SIZE(a) = 0;
610 a->ob_item = NULL;
611 a->allocated = 0;
612 while (--i >= 0) {
613 Py_XDECREF(item[i]);
614 }
615 PyMem_FREE(item);
616 }
617 /* Never fails; the return value can be ignored.
618 Note that there is no guarantee that the list is actually empty
619 at this point, because XDECREF may have populated it again! */
620 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000621}
622
Tim Peters8fc4a912004-07-31 21:53:19 +0000623/* a[ilow:ihigh] = v if v != NULL.
624 * del a[ilow:ihigh] if v == NULL.
625 *
626 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
627 * guaranteed the call cannot fail.
628 */
Armin Rigo93677f02004-07-29 12:40:23 +0000629static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000630list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 /* Because [X]DECREF can recursively invoke list operations on
633 this list, we must postpone all [X]DECREF activity until
634 after the list is back in its canonical shape. Therefore
635 we must allocate an additional array, 'recycle', into which
636 we temporarily copy the items that are deleted from the
637 list. :-( */
638 PyObject *recycle_on_stack[8];
639 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
640 PyObject **item;
641 PyObject **vitem = NULL;
642 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
643 Py_ssize_t n; /* # of elements in replacement list */
644 Py_ssize_t norig; /* # of elements in list getting replaced */
645 Py_ssize_t d; /* Change in size */
646 Py_ssize_t k;
647 size_t s;
648 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 if (v == NULL)
651 n = 0;
652 else {
653 if (a == b) {
654 /* Special case "a[i:j] = a" -- copy b first */
655 v = list_slice(b, 0, Py_SIZE(b));
656 if (v == NULL)
657 return result;
658 result = list_ass_slice(a, ilow, ihigh, v);
659 Py_DECREF(v);
660 return result;
661 }
662 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
663 if(v_as_SF == NULL)
664 goto Error;
665 n = PySequence_Fast_GET_SIZE(v_as_SF);
666 vitem = PySequence_Fast_ITEMS(v_as_SF);
667 }
668 if (ilow < 0)
669 ilow = 0;
670 else if (ilow > Py_SIZE(a))
671 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 if (ihigh < ilow)
674 ihigh = ilow;
675 else if (ihigh > Py_SIZE(a))
676 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 norig = ihigh - ilow;
679 assert(norig >= 0);
680 d = n - norig;
681 if (Py_SIZE(a) + d == 0) {
682 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200683 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 }
685 item = a->ob_item;
686 /* recycle the items that we are about to remove */
687 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700688 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
689 if (s) {
690 if (s > sizeof(recycle_on_stack)) {
691 recycle = (PyObject **)PyMem_MALLOC(s);
692 if (recycle == NULL) {
693 PyErr_NoMemory();
694 goto Error;
695 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700697 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200701 Py_ssize_t tail;
702 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
703 memmove(&item[ihigh+d], &item[ihigh], tail);
704 if (list_resize(a, Py_SIZE(a) + d) < 0) {
705 memmove(&item[ihigh], &item[ihigh+d], tail);
706 memcpy(&item[ilow], recycle, s);
707 goto Error;
708 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 item = a->ob_item;
710 }
711 else if (d > 0) { /* Insert d items */
712 k = Py_SIZE(a);
713 if (list_resize(a, k+d) < 0)
714 goto Error;
715 item = a->ob_item;
716 memmove(&item[ihigh+d], &item[ihigh],
717 (k - ihigh)*sizeof(PyObject *));
718 }
719 for (k = 0; k < n; k++, ilow++) {
720 PyObject *w = vitem[k];
721 Py_XINCREF(w);
722 item[ilow] = w;
723 }
724 for (k = norig - 1; k >= 0; --k)
725 Py_XDECREF(recycle[k]);
726 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000727 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 if (recycle != recycle_on_stack)
729 PyMem_FREE(recycle);
730 Py_XDECREF(v_as_SF);
731 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000732#undef b
733}
734
Guido van Rossum234f9421993-06-17 12:35:49 +0000735int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000736PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 if (!PyList_Check(a)) {
739 PyErr_BadInternalCall();
740 return -1;
741 }
742 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000743}
744
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000745static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000746list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 PyObject **items;
749 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000750
751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 size = PyList_GET_SIZE(self);
753 if (size == 0 || n == 1) {
754 Py_INCREF(self);
755 return (PyObject *)self;
756 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 if (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200759 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 Py_INCREF(self);
761 return (PyObject *)self;
762 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 if (size > PY_SSIZE_T_MAX / n) {
765 return PyErr_NoMemory();
766 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000767
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800768 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 p = size;
772 items = self->ob_item;
773 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
774 for (j = 0; j < size; j++) {
775 PyObject *o = items[j];
776 Py_INCREF(o);
777 items[p++] = o;
778 }
779 }
780 Py_INCREF(self);
781 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000782}
783
Guido van Rossum4a450d01991-04-03 19:05:18 +0000784static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000785list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000786{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700787 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyErr_SetString(PyExc_IndexError,
789 "list assignment index out of range");
790 return -1;
791 }
792 if (v == NULL)
793 return list_ass_slice(a, i, i+1, v);
794 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300795 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000797}
798
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200799/*[clinic input]
800list.insert
801
802 index: Py_ssize_t
803 object: object
804 /
805
806Insert object before index.
807[clinic start generated code]*/
808
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000809static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200810list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
811/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000812{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200813 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 Py_RETURN_NONE;
815 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816}
817
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200818/*[clinic input]
819list.clear
820
821Remove all items from list.
822[clinic start generated code]*/
823
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000824static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200825list_clear_impl(PyListObject *self)
826/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000827{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200828 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000829 Py_RETURN_NONE;
830}
831
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200832/*[clinic input]
833list.copy
834
835Return a shallow copy of the list.
836[clinic start generated code]*/
837
Eli Benderskycbbaa962011-02-25 05:47:53 +0000838static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200839list_copy_impl(PyListObject *self)
840/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000841{
842 return list_slice(self, 0, Py_SIZE(self));
843}
844
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200845/*[clinic input]
846list.append
847
848 object: object
849 /
850
851Append object to the end of the list.
852[clinic start generated code]*/
853
Eli Benderskycbbaa962011-02-25 05:47:53 +0000854static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200855list_append(PyListObject *self, PyObject *object)
856/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000857{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200858 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 Py_RETURN_NONE;
860 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000861}
862
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200863/*[clinic input]
864list.extend
865
866 iterable: object
867 /
868
869Extend list by appending elements from the iterable.
870[clinic start generated code]*/
871
Barry Warsawdedf6d61998-10-09 16:37:25 +0000872static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200873list_extend(PyListObject *self, PyObject *iterable)
874/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 PyObject *it; /* iter(v) */
877 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200878 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 Py_ssize_t mn; /* m + n */
880 Py_ssize_t i;
881 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 /* Special cases:
884 1) lists and tuples which can use PySequence_Fast ops
885 2) extending self to self requires making a copy first
886 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200887 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
888 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200890 iterable = PySequence_Fast(iterable, "argument must be iterable");
891 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200893 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200895 /* short circuit when iterable is empty */
896 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 Py_RETURN_NONE;
898 }
899 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000900 /* It should not be possible to allocate a list large enough to cause
901 an overflow on any relevant platform */
902 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800903 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200904 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 return NULL;
906 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200907 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 * situation a.extend(a), but the following code works
909 * in that case too. Just make sure to resize self
910 * before calling PySequence_Fast_ITEMS.
911 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200912 /* populate the end of self with iterable's items */
913 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 dest = self->ob_item + m;
915 for (i = 0; i < n; i++) {
916 PyObject *o = src[i];
917 Py_INCREF(o);
918 dest[i] = o;
919 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200920 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 Py_RETURN_NONE;
922 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000923
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200924 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if (it == NULL)
926 return NULL;
927 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200930 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800931 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 Py_DECREF(it);
933 return NULL;
934 }
935 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000936 if (m > PY_SSIZE_T_MAX - n) {
937 /* m + n overflowed; on the chance that n lied, and there really
938 * is enough room, ignore it. If n was telling the truth, we'll
939 * eventually run out of memory during the loop.
940 */
941 }
942 else {
943 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800945 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 goto error;
947 /* Make the list sane again. */
948 Py_SIZE(self) = m;
949 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 /* Run iterator to exhaustion. */
952 for (;;) {
953 PyObject *item = iternext(it);
954 if (item == NULL) {
955 if (PyErr_Occurred()) {
956 if (PyErr_ExceptionMatches(PyExc_StopIteration))
957 PyErr_Clear();
958 else
959 goto error;
960 }
961 break;
962 }
963 if (Py_SIZE(self) < self->allocated) {
964 /* steals ref */
965 PyList_SET_ITEM(self, Py_SIZE(self), item);
966 ++Py_SIZE(self);
967 }
968 else {
969 int status = app1(self, item);
970 Py_DECREF(item); /* append creates a new ref */
971 if (status < 0)
972 goto error;
973 }
974 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200977 if (Py_SIZE(self) < self->allocated) {
978 if (list_resize(self, Py_SIZE(self)) < 0)
979 goto error;
980 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 Py_DECREF(it);
983 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000984
985 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 Py_DECREF(it);
987 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000988}
989
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000990PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200991_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000992{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200993 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000994}
995
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000996static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000997list_inplace_concat(PyListObject *self, PyObject *other)
998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001000
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001001 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 if (result == NULL)
1003 return result;
1004 Py_DECREF(result);
1005 Py_INCREF(self);
1006 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001007}
1008
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001009/*[clinic input]
1010list.pop
1011
1012 index: Py_ssize_t = -1
1013 /
1014
1015Remove and return item at index (default last).
1016
1017Raises IndexError if list is empty or index is out of range.
1018[clinic start generated code]*/
1019
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001020static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001021list_pop_impl(PyListObject *self, Py_ssize_t index)
1022/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 PyObject *v;
1025 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 if (Py_SIZE(self) == 0) {
1028 /* Special-case most common failure cause */
1029 PyErr_SetString(PyExc_IndexError, "pop from empty list");
1030 return NULL;
1031 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001032 if (index < 0)
1033 index += Py_SIZE(self);
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -07001034 if (!valid_index(index, Py_SIZE(self))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1036 return NULL;
1037 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001038 v = self->ob_item[index];
1039 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001041 if (status >= 0)
1042 return v; /* and v now owns the reference the list had */
1043 else
1044 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 }
1046 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001047 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001048 if (status < 0) {
1049 Py_DECREF(v);
1050 return NULL;
1051 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001053}
1054
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001055/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1056static void
1057reverse_slice(PyObject **lo, PyObject **hi)
1058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 --hi;
1062 while (lo < hi) {
1063 PyObject *t = *lo;
1064 *lo = *hi;
1065 *hi = t;
1066 ++lo;
1067 --hi;
1068 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001069}
1070
Tim Petersa64dc242002-08-01 02:13:36 +00001071/* Lots of code for an adaptive, stable, natural mergesort. There are many
1072 * pieces to this algorithm; read listsort.txt for overviews and details.
1073 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001074
Daniel Stutzbach98338222010-12-02 21:55:33 +00001075/* A sortslice contains a pointer to an array of keys and a pointer to
1076 * an array of corresponding values. In other words, keys[i]
1077 * corresponds with values[i]. If values == NULL, then the keys are
1078 * also the values.
1079 *
1080 * Several convenience routines are provided here, so that keys and
1081 * values are always moved in sync.
1082 */
1083
1084typedef struct {
1085 PyObject **keys;
1086 PyObject **values;
1087} sortslice;
1088
1089Py_LOCAL_INLINE(void)
1090sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1091{
1092 s1->keys[i] = s2->keys[j];
1093 if (s1->values != NULL)
1094 s1->values[i] = s2->values[j];
1095}
1096
1097Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001098sortslice_copy_incr(sortslice *dst, sortslice *src)
1099{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001100 *dst->keys++ = *src->keys++;
1101 if (dst->values != NULL)
1102 *dst->values++ = *src->values++;
1103}
1104
1105Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001106sortslice_copy_decr(sortslice *dst, sortslice *src)
1107{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001108 *dst->keys-- = *src->keys--;
1109 if (dst->values != NULL)
1110 *dst->values-- = *src->values--;
1111}
1112
1113
1114Py_LOCAL_INLINE(void)
1115sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001116 Py_ssize_t n)
1117{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001118 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1119 if (s1->values != NULL)
1120 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1121}
1122
1123Py_LOCAL_INLINE(void)
1124sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001125 Py_ssize_t n)
1126{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001127 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1128 if (s1->values != NULL)
1129 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1130}
1131
1132Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001133sortslice_advance(sortslice *slice, Py_ssize_t n)
1134{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001135 slice->keys += n;
1136 if (slice->values != NULL)
1137 slice->values += n;
1138}
1139
embg1e34da42018-01-28 20:03:23 -07001140/* Comparison function: ms->key_compare, which is set at run-time in
1141 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001142 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1143 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001144
embg1e34da42018-01-28 20:03:23 -07001145#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001146
1147/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001148 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1149 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1150*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001151#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001153
embg1e34da42018-01-28 20:03:23 -07001154/* The maximum number of entries in a MergeState's pending-runs stack.
1155 * This is enough to sort arrays of size up to about
1156 * 32 * phi ** MAX_MERGE_PENDING
1157 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1158 * with 2**64 elements.
1159 */
1160#define MAX_MERGE_PENDING 85
1161
1162/* When we get into galloping mode, we stay there until both runs win less
1163 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1164 */
1165#define MIN_GALLOP 7
1166
1167/* Avoid malloc for small temp arrays. */
1168#define MERGESTATE_TEMP_SIZE 256
1169
1170/* One MergeState exists on the stack per invocation of mergesort. It's just
1171 * a convenient way to pass state around among the helper functions.
1172 */
1173struct s_slice {
1174 sortslice base;
1175 Py_ssize_t len;
1176};
1177
1178typedef struct s_MergeState MergeState;
1179struct s_MergeState {
1180 /* This controls when we get *into* galloping mode. It's initialized
1181 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1182 * random data, and lower for highly structured data.
1183 */
1184 Py_ssize_t min_gallop;
1185
1186 /* 'a' is temp storage to help with merges. It contains room for
1187 * alloced entries.
1188 */
1189 sortslice a; /* may point to temparray below */
1190 Py_ssize_t alloced;
1191
1192 /* A stack of n pending runs yet to be merged. Run #i starts at
1193 * address base[i] and extends for len[i] elements. It's always
1194 * true (so long as the indices are in bounds) that
1195 *
1196 * pending[i].base + pending[i].len == pending[i+1].base
1197 *
1198 * so we could cut the storage for this, but it's a minor amount,
1199 * and keeping all the info explicit simplifies the code.
1200 */
1201 int n;
1202 struct s_slice pending[MAX_MERGE_PENDING];
1203
1204 /* 'a' points to this when possible, rather than muck with malloc. */
1205 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1206
1207 /* This is the function we will use to compare two keys,
1208 * even when none of our special cases apply and we have to use
1209 * safe_object_compare. */
1210 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1211
1212 /* This function is used by unsafe_object_compare to optimize comparisons
1213 * when we know our list is type-homogeneous but we can't assume anything else.
1214 * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1215 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1216
1217 /* This function is used by unsafe_tuple_compare to compare the first elements
1218 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1219 * we can assume more, and use one of the special-case compares. */
1220 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1221};
1222
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001223/* binarysort is the best method for sorting small arrays: it does
1224 few compares, but can do data movement quadratic in the number of
1225 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001226 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001227 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001228 On entry, must have lo <= start <= hi, and that [lo, start) is already
1229 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001230 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001231 Even in case of error, the output slice will be some permutation of
1232 the input (nothing is lost or duplicated).
1233*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001234static int
embg1e34da42018-01-28 20:03:23 -07001235binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001236{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001237 Py_ssize_t k;
1238 PyObject **l, **p, **r;
1239 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001240
Daniel Stutzbach98338222010-12-02 21:55:33 +00001241 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001243 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 ++start;
1245 for (; start < hi; ++start) {
1246 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001247 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 r = start;
1249 pivot = *r;
1250 /* Invariants:
1251 * pivot >= all in [lo, l).
1252 * pivot < all in [r, start).
1253 * The second is vacuously true at the start.
1254 */
1255 assert(l < r);
1256 do {
1257 p = l + ((r - l) >> 1);
1258 IFLT(pivot, *p)
1259 r = p;
1260 else
1261 l = p+1;
1262 } while (l < r);
1263 assert(l == r);
1264 /* The invariants still hold, so pivot >= all in [lo, l) and
1265 pivot < all in [l, start), so pivot belongs at l. Note
1266 that if there are elements equal to pivot, l points to the
1267 first slot after them -- that's why this sort is stable.
1268 Slide over to make room.
1269 Caution: using memmove is much slower under MSVC 5;
1270 we're not usually moving many slots. */
1271 for (p = start; p > l; --p)
1272 *p = *(p-1);
1273 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001274 if (lo.values != NULL) {
1275 Py_ssize_t offset = lo.values - lo.keys;
1276 p = start + offset;
1277 pivot = *p;
1278 l += offset;
1279 for (p = start + offset; p > l; --p)
1280 *p = *(p-1);
1281 *l = pivot;
1282 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 }
1284 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001285
1286 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001288}
1289
Tim Petersa64dc242002-08-01 02:13:36 +00001290/*
1291Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1292is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001293
Tim Petersa64dc242002-08-01 02:13:36 +00001294 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001295
Tim Petersa64dc242002-08-01 02:13:36 +00001296or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001297
Tim Petersa64dc242002-08-01 02:13:36 +00001298 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001299
Tim Petersa64dc242002-08-01 02:13:36 +00001300Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1301For its intended use in a stable mergesort, the strictness of the defn of
1302"descending" is needed so that the caller can safely reverse a descending
1303sequence without violating stability (strict > ensures there are no equal
1304elements to get out of order).
1305
1306Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001307*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001308static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001309count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 Py_ssize_t k;
1312 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 assert(lo < hi);
1315 *descending = 0;
1316 ++lo;
1317 if (lo == hi)
1318 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 n = 2;
1321 IFLT(*lo, *(lo-1)) {
1322 *descending = 1;
1323 for (lo = lo+1; lo < hi; ++lo, ++n) {
1324 IFLT(*lo, *(lo-1))
1325 ;
1326 else
1327 break;
1328 }
1329 }
1330 else {
1331 for (lo = lo+1; lo < hi; ++lo, ++n) {
1332 IFLT(*lo, *(lo-1))
1333 break;
1334 }
1335 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001338fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001340}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001341
Tim Petersa64dc242002-08-01 02:13:36 +00001342/*
1343Locate the proper position of key in a sorted vector; if the vector contains
1344an element equal to key, return the position immediately to the left of
1345the leftmost equal element. [gallop_right() does the same except returns
1346the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001347
Tim Petersa64dc242002-08-01 02:13:36 +00001348"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001349
Tim Petersa64dc242002-08-01 02:13:36 +00001350"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1351hint is to the final result, the faster this runs.
1352
1353The return value is the int k in 0..n such that
1354
1355 a[k-1] < key <= a[k]
1356
1357pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1358key belongs at index k; or, IOW, the first k elements of a should precede
1359key, and the last n-k should follow key.
1360
1361Returns -1 on error. See listsort.txt for info on the method.
1362*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001363static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001364gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 Py_ssize_t ofs;
1367 Py_ssize_t lastofs;
1368 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 a += hint;
1373 lastofs = 0;
1374 ofs = 1;
1375 IFLT(*a, key) {
1376 /* a[hint] < key -- gallop right, until
1377 * a[hint + lastofs] < key <= a[hint + ofs]
1378 */
1379 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1380 while (ofs < maxofs) {
1381 IFLT(a[ofs], key) {
1382 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001383 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 }
1386 else /* key <= a[hint + ofs] */
1387 break;
1388 }
1389 if (ofs > maxofs)
1390 ofs = maxofs;
1391 /* Translate back to offsets relative to &a[0]. */
1392 lastofs += hint;
1393 ofs += hint;
1394 }
1395 else {
1396 /* key <= a[hint] -- gallop left, until
1397 * a[hint - ofs] < key <= a[hint - lastofs]
1398 */
1399 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1400 while (ofs < maxofs) {
1401 IFLT(*(a-ofs), key)
1402 break;
1403 /* key <= a[hint - ofs] */
1404 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001405 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 }
1408 if (ofs > maxofs)
1409 ofs = maxofs;
1410 /* Translate back to positive offsets relative to &a[0]. */
1411 k = lastofs;
1412 lastofs = hint - ofs;
1413 ofs = hint - k;
1414 }
1415 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1418 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1419 * right of lastofs but no farther right than ofs. Do a binary
1420 * search, with invariant a[lastofs-1] < key <= a[ofs].
1421 */
1422 ++lastofs;
1423 while (lastofs < ofs) {
1424 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 IFLT(a[m], key)
1427 lastofs = m+1; /* a[m] < key */
1428 else
1429 ofs = m; /* key <= a[m] */
1430 }
1431 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1432 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001433
1434fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001436}
1437
1438/*
1439Exactly like gallop_left(), except that if key already exists in a[0:n],
1440finds the position immediately to the right of the rightmost equal value.
1441
1442The return value is the int k in 0..n such that
1443
1444 a[k-1] <= key < a[k]
1445
1446or -1 if error.
1447
1448The code duplication is massive, but this is enough different given that
1449we're sticking to "<" comparisons that it's much harder to follow if
1450written as one routine with yet another "left or right?" flag.
1451*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001452static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001453gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 Py_ssize_t ofs;
1456 Py_ssize_t lastofs;
1457 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 a += hint;
1462 lastofs = 0;
1463 ofs = 1;
1464 IFLT(key, *a) {
1465 /* key < a[hint] -- gallop left, until
1466 * a[hint - ofs] <= key < a[hint - lastofs]
1467 */
1468 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1469 while (ofs < maxofs) {
1470 IFLT(key, *(a-ofs)) {
1471 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001472 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 }
1475 else /* a[hint - ofs] <= key */
1476 break;
1477 }
1478 if (ofs > maxofs)
1479 ofs = maxofs;
1480 /* Translate back to positive offsets relative to &a[0]. */
1481 k = lastofs;
1482 lastofs = hint - ofs;
1483 ofs = hint - k;
1484 }
1485 else {
1486 /* a[hint] <= key -- gallop right, until
1487 * a[hint + lastofs] <= key < a[hint + ofs]
1488 */
1489 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1490 while (ofs < maxofs) {
1491 IFLT(key, a[ofs])
1492 break;
1493 /* a[hint + ofs] <= key */
1494 lastofs = ofs;
Alexey Izbyshev6bc59172019-05-23 03:01:08 +03001495 assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 ofs = (ofs << 1) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 }
1498 if (ofs > maxofs)
1499 ofs = maxofs;
1500 /* Translate back to offsets relative to &a[0]. */
1501 lastofs += hint;
1502 ofs += hint;
1503 }
1504 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1507 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1508 * right of lastofs but no farther right than ofs. Do a binary
1509 * search, with invariant a[lastofs-1] <= key < a[ofs].
1510 */
1511 ++lastofs;
1512 while (lastofs < ofs) {
1513 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 IFLT(key, a[m])
1516 ofs = m; /* key < a[m] */
1517 else
1518 lastofs = m+1; /* a[m] <= key */
1519 }
1520 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1521 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001522
1523fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001525}
1526
Tim Petersa64dc242002-08-01 02:13:36 +00001527/* Conceptually a MergeState's constructor. */
1528static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001529merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001532 if (has_keyfunc) {
1533 /* The temporary space for merging will need at most half the list
1534 * size rounded up. Use the minimum possible space so we can use the
1535 * rest of temparray for other things. In particular, if there is
1536 * enough extra space, listsort() will use it to store the keys.
1537 */
1538 ms->alloced = (list_size + 1) / 2;
1539
1540 /* ms->alloced describes how many keys will be stored at
1541 ms->temparray, but we also need to store the values. Hence,
1542 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1543 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1544 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1545 ms->a.values = &ms->temparray[ms->alloced];
1546 }
1547 else {
1548 ms->alloced = MERGESTATE_TEMP_SIZE;
1549 ms->a.values = NULL;
1550 }
1551 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 ms->n = 0;
1553 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001554}
1555
1556/* Free all the temp memory owned by the MergeState. This must be called
1557 * when you're done with a MergeState, and may be called before then if
1558 * you want to free the temp memory early.
1559 */
1560static void
1561merge_freemem(MergeState *ms)
1562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001564 if (ms->a.keys != ms->temparray)
1565 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001566}
1567
1568/* Ensure enough temp memory for 'need' array slots is available.
1569 * Returns 0 on success and -1 if the memory can't be gotten.
1570 */
1571static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001572merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001573{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001574 int multiplier;
1575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 assert(ms != NULL);
1577 if (need <= ms->alloced)
1578 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001579
1580 multiplier = ms->a.values != NULL ? 2 : 1;
1581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 /* Don't realloc! That can cost cycles to copy the old data, but
1583 * we don't care what's in the block.
1584 */
1585 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001586 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 PyErr_NoMemory();
1588 return -1;
1589 }
embg1e34da42018-01-28 20:03:23 -07001590 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001591 * sizeof(PyObject *));
1592 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001594 if (ms->a.values != NULL)
1595 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 return 0;
1597 }
1598 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001600}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1602 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001603
Daniel Stutzbach98338222010-12-02 21:55:33 +00001604/* Merge the na elements starting at ssa with the nb elements starting at
1605 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1606 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1607 * should have na <= nb. See listsort.txt for more info. Return 0 if
1608 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001609 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001610static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001611merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1612 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001615 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 int result = -1; /* guilty until proved innocent */
1617 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001618
Daniel Stutzbach98338222010-12-02 21:55:33 +00001619 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1620 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 if (MERGE_GETMEM(ms, na) < 0)
1622 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001623 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1624 dest = ssa;
1625 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001626
Daniel Stutzbach98338222010-12-02 21:55:33 +00001627 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 --nb;
1629 if (nb == 0)
1630 goto Succeed;
1631 if (na == 1)
1632 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 min_gallop = ms->min_gallop;
1635 for (;;) {
1636 Py_ssize_t acount = 0; /* # of times A won in a row */
1637 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 /* Do the straightforward thing until (if ever) one run
1640 * appears to win consistently.
1641 */
1642 for (;;) {
1643 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001644 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 if (k) {
1646 if (k < 0)
1647 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001648 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 ++bcount;
1650 acount = 0;
1651 --nb;
1652 if (nb == 0)
1653 goto Succeed;
1654 if (bcount >= min_gallop)
1655 break;
1656 }
1657 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001658 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 ++acount;
1660 bcount = 0;
1661 --na;
1662 if (na == 1)
1663 goto CopyB;
1664 if (acount >= min_gallop)
1665 break;
1666 }
1667 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 /* One run is winning so consistently that galloping may
1670 * be a huge win. So try that, and continue galloping until
1671 * (if ever) neither run appears to be winning consistently
1672 * anymore.
1673 */
1674 ++min_gallop;
1675 do {
1676 assert(na > 1 && nb > 0);
1677 min_gallop -= min_gallop > 1;
1678 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001679 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 acount = k;
1681 if (k) {
1682 if (k < 0)
1683 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001684 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1685 sortslice_advance(&dest, k);
1686 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 na -= k;
1688 if (na == 1)
1689 goto CopyB;
1690 /* na==0 is impossible now if the comparison
1691 * function is consistent, but we can't assume
1692 * that it is.
1693 */
1694 if (na == 0)
1695 goto Succeed;
1696 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001697 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 --nb;
1699 if (nb == 0)
1700 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001701
embg1e34da42018-01-28 20:03:23 -07001702 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 bcount = k;
1704 if (k) {
1705 if (k < 0)
1706 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001707 sortslice_memmove(&dest, 0, &ssb, 0, k);
1708 sortslice_advance(&dest, k);
1709 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 nb -= k;
1711 if (nb == 0)
1712 goto Succeed;
1713 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001714 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 --na;
1716 if (na == 1)
1717 goto CopyB;
1718 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1719 ++min_gallop; /* penalize it for leaving galloping mode */
1720 ms->min_gallop = min_gallop;
1721 }
Tim Petersa64dc242002-08-01 02:13:36 +00001722Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001724Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001726 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001728CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001730 /* The last element of ssa belongs at the end of the merge. */
1731 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1732 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001734}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001735
Daniel Stutzbach98338222010-12-02 21:55:33 +00001736/* Merge the na elements starting at pa with the nb elements starting at
1737 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1738 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1739 * should have na >= nb. See listsort.txt for more info. Return 0 if
1740 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001741 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001742static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001743merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1744 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001747 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001750
Daniel Stutzbach98338222010-12-02 21:55:33 +00001751 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1752 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if (MERGE_GETMEM(ms, nb) < 0)
1754 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001755 dest = ssb;
1756 sortslice_advance(&dest, nb-1);
1757 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1758 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001760 ssb.keys = ms->a.keys + nb - 1;
1761 if (ssb.values != NULL)
1762 ssb.values = ms->a.values + nb - 1;
1763 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001764
Daniel Stutzbach98338222010-12-02 21:55:33 +00001765 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 --na;
1767 if (na == 0)
1768 goto Succeed;
1769 if (nb == 1)
1770 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 min_gallop = ms->min_gallop;
1773 for (;;) {
1774 Py_ssize_t acount = 0; /* # of times A won in a row */
1775 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 /* Do the straightforward thing until (if ever) one run
1778 * appears to win consistently.
1779 */
1780 for (;;) {
1781 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001782 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 if (k) {
1784 if (k < 0)
1785 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001786 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 ++acount;
1788 bcount = 0;
1789 --na;
1790 if (na == 0)
1791 goto Succeed;
1792 if (acount >= min_gallop)
1793 break;
1794 }
1795 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001796 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 ++bcount;
1798 acount = 0;
1799 --nb;
1800 if (nb == 1)
1801 goto CopyA;
1802 if (bcount >= min_gallop)
1803 break;
1804 }
1805 }
Tim Petersa64dc242002-08-01 02:13:36 +00001806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 /* One run is winning so consistently that galloping may
1808 * be a huge win. So try that, and continue galloping until
1809 * (if ever) neither run appears to be winning consistently
1810 * anymore.
1811 */
1812 ++min_gallop;
1813 do {
1814 assert(na > 0 && nb > 1);
1815 min_gallop -= min_gallop > 1;
1816 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001817 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 if (k < 0)
1819 goto Fail;
1820 k = na - k;
1821 acount = k;
1822 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001823 sortslice_advance(&dest, -k);
1824 sortslice_advance(&ssa, -k);
1825 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 na -= k;
1827 if (na == 0)
1828 goto Succeed;
1829 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001830 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 --nb;
1832 if (nb == 1)
1833 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001834
embg1e34da42018-01-28 20:03:23 -07001835 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 if (k < 0)
1837 goto Fail;
1838 k = nb - k;
1839 bcount = k;
1840 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001841 sortslice_advance(&dest, -k);
1842 sortslice_advance(&ssb, -k);
1843 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 nb -= k;
1845 if (nb == 1)
1846 goto CopyA;
1847 /* nb==0 is impossible now if the comparison
1848 * function is consistent, but we can't assume
1849 * that it is.
1850 */
1851 if (nb == 0)
1852 goto Succeed;
1853 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001854 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 --na;
1856 if (na == 0)
1857 goto Succeed;
1858 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1859 ++min_gallop; /* penalize it for leaving galloping mode */
1860 ms->min_gallop = min_gallop;
1861 }
Tim Petersa64dc242002-08-01 02:13:36 +00001862Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001864Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001866 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001868CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001870 /* The first element of ssb belongs at the front of the merge. */
1871 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1872 sortslice_advance(&dest, -na);
1873 sortslice_advance(&ssa, -na);
1874 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001876}
1877
1878/* Merge the two runs at stack indices i and i+1.
1879 * Returns 0 on success, -1 on error.
1880 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001881static Py_ssize_t
1882merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001883{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001884 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 Py_ssize_t na, nb;
1886 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 assert(ms != NULL);
1889 assert(ms->n >= 2);
1890 assert(i >= 0);
1891 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001892
Daniel Stutzbach98338222010-12-02 21:55:33 +00001893 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001895 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 nb = ms->pending[i+1].len;
1897 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001898 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 /* Record the length of the combined runs; if i is the 3rd-last
1901 * run now, also slide over the last run (which isn't involved
1902 * in this merge). The current run i+1 goes away in any case.
1903 */
1904 ms->pending[i].len = na + nb;
1905 if (i == ms->n - 3)
1906 ms->pending[i+1] = ms->pending[i+2];
1907 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 /* Where does b start in a? Elements in a before that can be
1910 * ignored (already in place).
1911 */
embg1e34da42018-01-28 20:03:23 -07001912 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 if (k < 0)
1914 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001915 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 na -= k;
1917 if (na == 0)
1918 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 /* Where does a end in b? Elements in b after that can be
1921 * ignored (already in place).
1922 */
embg1e34da42018-01-28 20:03:23 -07001923 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 if (nb <= 0)
1925 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 /* Merge what remains of the runs, using a temp array with
1928 * min(na, nb) elements.
1929 */
1930 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001931 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001933 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001934}
1935
1936/* Examine the stack of runs waiting to be merged, merging adjacent runs
1937 * until the stack invariants are re-established:
1938 *
1939 * 1. len[-3] > len[-2] + len[-1]
1940 * 2. len[-2] > len[-1]
1941 *
1942 * See listsort.txt for more info.
1943 *
1944 * Returns 0 on success, -1 on error.
1945 */
1946static int
1947merge_collapse(MergeState *ms)
1948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 assert(ms);
1952 while (ms->n > 1) {
1953 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001954 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1955 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 if (p[n-1].len < p[n+1].len)
1957 --n;
1958 if (merge_at(ms, n) < 0)
1959 return -1;
1960 }
1961 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001962 if (merge_at(ms, n) < 0)
1963 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 }
1965 else
1966 break;
1967 }
1968 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001969}
1970
1971/* Regardless of invariants, merge all runs on the stack until only one
1972 * remains. This is used at the end of the mergesort.
1973 *
1974 * Returns 0 on success, -1 on error.
1975 */
1976static int
1977merge_force_collapse(MergeState *ms)
1978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 assert(ms);
1982 while (ms->n > 1) {
1983 Py_ssize_t n = ms->n - 2;
1984 if (n > 0 && p[n-1].len < p[n+1].len)
1985 --n;
1986 if (merge_at(ms, n) < 0)
1987 return -1;
1988 }
1989 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001990}
1991
1992/* Compute a good value for the minimum run length; natural runs shorter
1993 * than this are boosted artificially via binary insertion.
1994 *
1995 * If n < 64, return n (it's too small to bother with fancy stuff).
1996 * Else if n is an exact power of 2, return 32.
1997 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1998 * strictly less than, an exact power of 2.
1999 *
2000 * See listsort.txt for more info.
2001 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002002static Py_ssize_t
2003merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00002004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00002006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 assert(n >= 0);
2008 while (n >= 64) {
2009 r |= n & 1;
2010 n >>= 1;
2011 }
2012 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002013}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00002014
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002015static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00002016reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002017{
Daniel Stutzbach98338222010-12-02 21:55:33 +00002018 reverse_slice(s->keys, &s->keys[n]);
2019 if (s->values != NULL)
2020 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002021}
2022
embg1e34da42018-01-28 20:03:23 -07002023/* Here we define custom comparison functions to optimize for the cases one commonly
2024 * encounters in practice: homogeneous lists, often of one of the basic types. */
2025
2026/* This struct holds the comparison function and helper functions
2027 * selected in the pre-sort check. */
2028
2029/* These are the special case compare functions.
2030 * ms->key_compare will always point to one of these: */
2031
2032/* Heterogeneous compare: default, always safe to fall back on. */
2033static int
2034safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2035{
2036 /* No assumptions necessary! */
2037 return PyObject_RichCompareBool(v, w, Py_LT);
2038}
2039
2040/* Homogeneous compare: safe for any two compareable objects of the same type.
2041 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2042 * pre-sort check.)
2043 */
2044static int
2045unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2046{
2047 PyObject *res_obj; int res;
2048
2049 /* No assumptions, because we check first: */
2050 if (v->ob_type->tp_richcompare != ms->key_richcompare)
2051 return PyObject_RichCompareBool(v, w, Py_LT);
2052
2053 assert(ms->key_richcompare != NULL);
2054 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2055
2056 if (res_obj == Py_NotImplemented) {
2057 Py_DECREF(res_obj);
2058 return PyObject_RichCompareBool(v, w, Py_LT);
2059 }
2060 if (res_obj == NULL)
2061 return -1;
2062
2063 if (PyBool_Check(res_obj)) {
2064 res = (res_obj == Py_True);
2065 }
2066 else {
2067 res = PyObject_IsTrue(res_obj);
2068 }
2069 Py_DECREF(res_obj);
2070
2071 /* Note that we can't assert
2072 * res == PyObject_RichCompareBool(v, w, Py_LT);
2073 * because of evil compare functions like this:
2074 * lambda a, b: int(random.random() * 3) - 1)
2075 * (which is actually in test_sort.py) */
2076 return res;
2077}
2078
2079/* Latin string compare: safe for any two latin (one byte per char) strings. */
2080static int
2081unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2082{
Victor Stinner8017b802018-01-29 13:47:06 +01002083 Py_ssize_t len;
2084 int res;
embg1e34da42018-01-28 20:03:23 -07002085
2086 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2087 assert(v->ob_type == w->ob_type);
2088 assert(v->ob_type == &PyUnicode_Type);
2089 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2090 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2091
2092 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2093 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2094
2095 res = (res != 0 ?
2096 res < 0 :
2097 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2098
2099 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2100 return res;
2101}
2102
2103/* Bounded int compare: compare any two longs that fit in a single machine word. */
2104static int
2105unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2106{
2107 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2108
2109 /* Modified from Objects/longobject.c:long_compare, assuming: */
2110 assert(v->ob_type == w->ob_type);
2111 assert(v->ob_type == &PyLong_Type);
2112 assert(Py_ABS(Py_SIZE(v)) <= 1);
2113 assert(Py_ABS(Py_SIZE(w)) <= 1);
2114
2115 vl = (PyLongObject*)v;
2116 wl = (PyLongObject*)w;
2117
2118 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2119 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2120
2121 if (Py_SIZE(vl) < 0)
2122 v0 = -v0;
2123 if (Py_SIZE(wl) < 0)
2124 w0 = -w0;
2125
2126 res = v0 < w0;
2127 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2128 return res;
2129}
2130
2131/* Float compare: compare any two floats. */
2132static int
2133unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2134{
2135 int res;
2136
2137 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2138 assert(v->ob_type == w->ob_type);
2139 assert(v->ob_type == &PyFloat_Type);
2140
2141 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2142 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2143 return res;
2144}
2145
2146/* Tuple compare: compare *any* two tuples, using
2147 * ms->tuple_elem_compare to compare the first elements, which is set
2148 * using the same pre-sort check as we use for ms->key_compare,
2149 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2150 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2151 * that most tuple compares don't involve x[1:]. */
2152static int
2153unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2154{
2155 PyTupleObject *vt, *wt;
2156 Py_ssize_t i, vlen, wlen;
2157 int k;
2158
2159 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2160 assert(v->ob_type == w->ob_type);
2161 assert(v->ob_type == &PyTuple_Type);
2162 assert(Py_SIZE(v) > 0);
2163 assert(Py_SIZE(w) > 0);
2164
2165 vt = (PyTupleObject *)v;
2166 wt = (PyTupleObject *)w;
2167
2168 vlen = Py_SIZE(vt);
2169 wlen = Py_SIZE(wt);
2170
2171 for (i = 0; i < vlen && i < wlen; i++) {
2172 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2173 if (k < 0)
2174 return -1;
2175 if (!k)
2176 break;
2177 }
2178
2179 if (i >= vlen || i >= wlen)
2180 return vlen < wlen;
2181
2182 if (i == 0)
2183 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2184 else
2185 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2186}
2187
Tim Petersa64dc242002-08-01 02:13:36 +00002188/* An adaptive, stable, natural mergesort. See listsort.txt.
2189 * Returns Py_None on success, NULL on error. Even in case of error, the
2190 * list will be some permutation of its input state (nothing is lost or
2191 * duplicated).
2192 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002193/*[clinic input]
2194list.sort
2195
2196 *
2197 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002198 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002199
2200Stable sort *IN PLACE*.
2201[clinic start generated code]*/
2202
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002203static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002204list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002205/*[clinic end generated code: output=57b9f9c5e23fbe42 input=b0fcf743982c5b90]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 Py_ssize_t nremaining;
2209 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002210 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 Py_ssize_t saved_ob_size, saved_allocated;
2212 PyObject **saved_ob_item;
2213 PyObject **final_ob_item;
2214 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002216 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002219 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 if (keyfunc == Py_None)
2221 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 /* The list is temporarily made empty, so that mutations performed
2224 * by comparison functions can't affect the slice of memory we're
2225 * sorting (allowing mutations during sorting is a core-dump
2226 * factory, since ob_item may change).
2227 */
2228 saved_ob_size = Py_SIZE(self);
2229 saved_ob_item = self->ob_item;
2230 saved_allocated = self->allocated;
2231 Py_SIZE(self) = 0;
2232 self->ob_item = NULL;
2233 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002234
Daniel Stutzbach98338222010-12-02 21:55:33 +00002235 if (keyfunc == NULL) {
2236 keys = NULL;
2237 lo.keys = saved_ob_item;
2238 lo.values = NULL;
2239 }
2240 else {
2241 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2242 /* Leverage stack space we allocated but won't otherwise use */
2243 keys = &ms.temparray[saved_ob_size+1];
2244 else {
2245 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002246 if (keys == NULL) {
2247 PyErr_NoMemory();
2248 goto keyfunc_fail;
2249 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002251
2252 for (i = 0; i < saved_ob_size ; i++) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002253 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
2254 NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002255 if (keys[i] == NULL) {
2256 for (i=i-1 ; i>=0 ; i--)
2257 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002258 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002259 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002260 goto keyfunc_fail;
2261 }
2262 }
2263
2264 lo.keys = keys;
2265 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002267
embg1e34da42018-01-28 20:03:23 -07002268
2269 /* The pre-sort check: here's where we decide which compare function to use.
2270 * How much optimization is safe? We test for homogeneity with respect to
2271 * several properties that are expensive to check at compare-time, and
2272 * set ms appropriately. */
2273 if (saved_ob_size > 1) {
2274 /* Assume the first element is representative of the whole list. */
2275 int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2276 Py_SIZE(lo.keys[0]) > 0);
2277
2278 PyTypeObject* key_type = (keys_are_in_tuples ?
2279 PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2280 lo.keys[0]->ob_type);
2281
2282 int keys_are_all_same_type = 1;
2283 int strings_are_latin = 1;
2284 int ints_are_bounded = 1;
2285
2286 /* Prove that assumption by checking every key. */
embg1e34da42018-01-28 20:03:23 -07002287 for (i=0; i < saved_ob_size; i++) {
2288
2289 if (keys_are_in_tuples &&
2290 !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2291 keys_are_in_tuples = 0;
2292 keys_are_all_same_type = 0;
2293 break;
2294 }
2295
2296 /* Note: for lists of tuples, key is the first element of the tuple
2297 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2298 * for lists of tuples in the if-statement directly above. */
2299 PyObject *key = (keys_are_in_tuples ?
2300 PyTuple_GET_ITEM(lo.keys[i], 0) :
2301 lo.keys[i]);
2302
2303 if (key->ob_type != key_type) {
2304 keys_are_all_same_type = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002305 /* If keys are in tuple we must loop over the whole list to make
2306 sure all items are tuples */
2307 if (!keys_are_in_tuples) {
2308 break;
2309 }
embg1e34da42018-01-28 20:03:23 -07002310 }
2311
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002312 if (keys_are_all_same_type) {
2313 if (key_type == &PyLong_Type &&
2314 ints_are_bounded &&
2315 Py_ABS(Py_SIZE(key)) > 1) {
2316
embg1e34da42018-01-28 20:03:23 -07002317 ints_are_bounded = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002318 }
2319 else if (key_type == &PyUnicode_Type &&
2320 strings_are_latin &&
2321 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {
2322
2323 strings_are_latin = 0;
2324 }
2325 }
embg1e34da42018-01-28 20:03:23 -07002326 }
embg1e34da42018-01-28 20:03:23 -07002327
2328 /* Choose the best compare, given what we now know about the keys. */
2329 if (keys_are_all_same_type) {
2330
2331 if (key_type == &PyUnicode_Type && strings_are_latin) {
2332 ms.key_compare = unsafe_latin_compare;
2333 }
2334 else if (key_type == &PyLong_Type && ints_are_bounded) {
2335 ms.key_compare = unsafe_long_compare;
2336 }
2337 else if (key_type == &PyFloat_Type) {
2338 ms.key_compare = unsafe_float_compare;
2339 }
2340 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2341 ms.key_compare = unsafe_object_compare;
2342 }
Zackery Spytzebc793d2019-02-21 00:47:14 -07002343 else {
2344 ms.key_compare = safe_object_compare;
2345 }
embg1e34da42018-01-28 20:03:23 -07002346 }
2347 else {
2348 ms.key_compare = safe_object_compare;
2349 }
2350
2351 if (keys_are_in_tuples) {
2352 /* Make sure we're not dealing with tuples of tuples
2353 * (remember: here, key_type refers list [key[0] for key in keys]) */
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002354 if (key_type == &PyTuple_Type) {
embg1e34da42018-01-28 20:03:23 -07002355 ms.tuple_elem_compare = safe_object_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002356 }
2357 else {
embg1e34da42018-01-28 20:03:23 -07002358 ms.tuple_elem_compare = ms.key_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002359 }
embg1e34da42018-01-28 20:03:23 -07002360
2361 ms.key_compare = unsafe_tuple_compare;
2362 }
2363 }
2364 /* End of pre-sort check: ms is now set properly! */
2365
Daniel Stutzbach98338222010-12-02 21:55:33 +00002366 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 nremaining = saved_ob_size;
2369 if (nremaining < 2)
2370 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002371
Benjamin Peterson05380642010-08-23 19:35:39 +00002372 /* Reverse sort stability achieved by initially reversing the list,
2373 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002374 if (reverse) {
2375 if (keys != NULL)
2376 reverse_slice(&keys[0], &keys[saved_ob_size]);
2377 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2378 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 /* March over the array once, left to right, finding natural runs,
2381 * and extending short natural runs to minrun elements.
2382 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 minrun = merge_compute_minrun(nremaining);
2384 do {
2385 int descending;
2386 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002389 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 if (n < 0)
2391 goto fail;
2392 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002393 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 /* If short, extend to min(minrun, nremaining). */
2395 if (n < minrun) {
2396 const Py_ssize_t force = nremaining <= minrun ?
2397 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002398 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 goto fail;
2400 n = force;
2401 }
2402 /* Push run onto pending-runs stack, and maybe merge. */
2403 assert(ms.n < MAX_MERGE_PENDING);
2404 ms.pending[ms.n].base = lo;
2405 ms.pending[ms.n].len = n;
2406 ++ms.n;
2407 if (merge_collapse(&ms) < 0)
2408 goto fail;
2409 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002410 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 nremaining -= n;
2412 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 if (merge_force_collapse(&ms) < 0)
2415 goto fail;
2416 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002417 assert(keys == NULL
2418 ? ms.pending[0].base.keys == saved_ob_item
2419 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002421 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002422
2423succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002425fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002426 if (keys != NULL) {
2427 for (i = 0; i < saved_ob_size; i++)
2428 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002429 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002430 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 if (self->allocated != -1 && result != NULL) {
2434 /* The user mucked with the list during the sort,
2435 * and we don't already have another error to report.
2436 */
2437 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2438 result = NULL;
2439 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 if (reverse && saved_ob_size > 1)
2442 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002445
Daniel Stutzbach98338222010-12-02 21:55:33 +00002446keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 final_ob_item = self->ob_item;
2448 i = Py_SIZE(self);
2449 Py_SIZE(self) = saved_ob_size;
2450 self->ob_item = saved_ob_item;
2451 self->allocated = saved_allocated;
2452 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002453 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 guarantee that the list is really empty when it returns */
2455 while (--i >= 0) {
2456 Py_XDECREF(final_ob_item[i]);
2457 }
2458 PyMem_FREE(final_ob_item);
2459 }
2460 Py_XINCREF(result);
2461 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002462}
Tim Peters330f9e92002-07-19 07:05:44 +00002463#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002464#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002465
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002466int
Fred Drakea2f55112000-07-09 15:16:51 +00002467PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 if (v == NULL || !PyList_Check(v)) {
2470 PyErr_BadInternalCall();
2471 return -1;
2472 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002473 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 if (v == NULL)
2475 return -1;
2476 Py_DECREF(v);
2477 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002478}
2479
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002480/*[clinic input]
2481list.reverse
2482
2483Reverse *IN PLACE*.
2484[clinic start generated code]*/
2485
Guido van Rossumb86c5492001-02-12 22:06:02 +00002486static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002487list_reverse_impl(PyListObject *self)
2488/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 if (Py_SIZE(self) > 1)
2491 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2492 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002493}
2494
Guido van Rossum84c76f51990-10-30 13:32:20 +00002495int
Fred Drakea2f55112000-07-09 15:16:51 +00002496PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 if (v == NULL || !PyList_Check(v)) {
2501 PyErr_BadInternalCall();
2502 return -1;
2503 }
2504 if (Py_SIZE(self) > 1)
2505 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2506 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002507}
2508
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002509PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002510PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 if (v == NULL || !PyList_Check(v)) {
2513 PyErr_BadInternalCall();
2514 return NULL;
2515 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +05002516 return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002517}
2518
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002519/*[clinic input]
2520list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002521
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002522 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002523 start: slice_index(accept={int}) = 0
2524 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002525 /
2526
2527Return first index of value.
2528
2529Raises ValueError if the value is not present.
2530[clinic start generated code]*/
2531
2532static PyObject *
2533list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2534 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002535/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002536{
2537 Py_ssize_t i;
2538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 if (start < 0) {
2540 start += Py_SIZE(self);
2541 if (start < 0)
2542 start = 0;
2543 }
2544 if (stop < 0) {
2545 stop += Py_SIZE(self);
2546 if (stop < 0)
2547 stop = 0;
2548 }
2549 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002550 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 if (cmp > 0)
2552 return PyLong_FromSsize_t(i);
2553 else if (cmp < 0)
2554 return NULL;
2555 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002556 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002558}
2559
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002560/*[clinic input]
2561list.count
2562
2563 value: object
2564 /
2565
2566Return number of occurrences of value.
2567[clinic start generated code]*/
2568
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002569static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002570list_count(PyListObject *self, PyObject *value)
2571/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 Py_ssize_t count = 0;
2574 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002577 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 if (cmp > 0)
2579 count++;
2580 else if (cmp < 0)
2581 return NULL;
2582 }
2583 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002584}
2585
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002586/*[clinic input]
2587list.remove
2588
2589 value: object
2590 /
2591
2592Remove first occurrence of value.
2593
2594Raises ValueError if the value is not present.
2595[clinic start generated code]*/
2596
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002597static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002598list_remove(PyListObject *self, PyObject *value)
2599/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002601 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002604 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 if (cmp > 0) {
2606 if (list_ass_slice(self, i, i+1,
2607 (PyObject *)NULL) == 0)
2608 Py_RETURN_NONE;
2609 return NULL;
2610 }
2611 else if (cmp < 0)
2612 return NULL;
2613 }
2614 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2615 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002616}
2617
Jeremy Hylton8caad492000-06-23 14:18:11 +00002618static int
2619list_traverse(PyListObject *o, visitproc visit, void *arg)
2620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 for (i = Py_SIZE(o); --i >= 0; )
2624 Py_VISIT(o->ob_item[i]);
2625 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002626}
2627
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002628static PyObject *
2629list_richcompare(PyObject *v, PyObject *w, int op)
2630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 PyListObject *vl, *wl;
2632 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002633
Brian Curtindfc80e32011-08-10 20:28:54 -05002634 if (!PyList_Check(v) || !PyList_Check(w))
2635 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 vl = (PyListObject *)v;
2638 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2641 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002643 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 else
stratakise8b19652017-11-02 11:32:54 +01002645 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 /* Search for the first index where items are different */
2649 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2650 int k = PyObject_RichCompareBool(vl->ob_item[i],
2651 wl->ob_item[i], Py_EQ);
2652 if (k < 0)
2653 return NULL;
2654 if (!k)
2655 break;
2656 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2659 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002660 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 /* We have an item that differs -- shortcuts for EQ/NE */
2664 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002665 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 }
2667 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002668 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 /* Compare the final item again using the proper operator */
2672 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002673}
2674
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002675/*[clinic input]
2676list.__init__
2677
2678 iterable: object(c_default="NULL") = ()
2679 /
2680
2681Built-in mutable sequence.
2682
2683If no argument is given, the constructor creates a new empty list.
2684The argument must be an iterable if specified.
2685[clinic start generated code]*/
2686
Tim Peters6d6c1a32001-08-02 04:15:00 +00002687static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002688list___init___impl(PyListObject *self, PyObject *iterable)
2689/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 /* Verify list invariants established by PyType_GenericAlloc() */
2692 assert(0 <= Py_SIZE(self));
2693 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2694 assert(self->ob_item != NULL ||
2695 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 /* Empty previous contents */
2698 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002699 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002701 if (iterable != NULL) {
Pablo Galindo372d7052018-10-28 20:16:26 +00002702 if (_PyObject_HasLen(iterable)) {
2703 Py_ssize_t iter_len = PyObject_Size(iterable);
2704 if (iter_len == -1) {
2705 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2706 return -1;
2707 }
2708 PyErr_Clear();
2709 }
2710 if (iter_len > 0 && self->ob_item == NULL
2711 && list_preallocate_exact(self, iter_len)) {
2712 return -1;
2713 }
2714 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002715 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 if (rv == NULL)
2717 return -1;
2718 Py_DECREF(rv);
2719 }
2720 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002721}
2722
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002723/*[clinic input]
2724list.__sizeof__
2725
2726Return the size of the list in memory, in bytes.
2727[clinic start generated code]*/
2728
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002729static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002730list___sizeof___impl(PyListObject *self)
2731/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002734
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002735 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002737}
2738
Raymond Hettinger1021c442003-11-07 15:38:09 +00002739static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002740static PyObject *list_subscript(PyListObject*, PyObject*);
2741
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002742static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002743 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2744 LIST___REVERSED___METHODDEF
2745 LIST___SIZEOF___METHODDEF
2746 LIST_CLEAR_METHODDEF
2747 LIST_COPY_METHODDEF
2748 LIST_APPEND_METHODDEF
2749 LIST_INSERT_METHODDEF
2750 LIST_EXTEND_METHODDEF
2751 LIST_POP_METHODDEF
2752 LIST_REMOVE_METHODDEF
2753 LIST_INDEX_METHODDEF
2754 LIST_COUNT_METHODDEF
2755 LIST_REVERSE_METHODDEF
2756 LIST_SORT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002758};
2759
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002760static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 (lenfunc)list_length, /* sq_length */
2762 (binaryfunc)list_concat, /* sq_concat */
2763 (ssizeargfunc)list_repeat, /* sq_repeat */
2764 (ssizeargfunc)list_item, /* sq_item */
2765 0, /* sq_slice */
2766 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2767 0, /* sq_ass_slice */
2768 (objobjproc)list_contains, /* sq_contains */
2769 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2770 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002771};
2772
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002773static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002774list_subscript(PyListObject* self, PyObject* item)
2775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 if (PyIndex_Check(item)) {
2777 Py_ssize_t i;
2778 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2779 if (i == -1 && PyErr_Occurred())
2780 return NULL;
2781 if (i < 0)
2782 i += PyList_GET_SIZE(self);
2783 return list_item(self, i);
2784 }
2785 else if (PySlice_Check(item)) {
2786 Py_ssize_t start, stop, step, slicelength, cur, i;
2787 PyObject* result;
2788 PyObject* it;
2789 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002790
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002791 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 return NULL;
2793 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002794 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2795 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 if (slicelength <= 0) {
2798 return PyList_New(0);
2799 }
2800 else if (step == 1) {
2801 return list_slice(self, start, stop);
2802 }
2803 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002804 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 src = self->ob_item;
2808 dest = ((PyListObject *)result)->ob_item;
2809 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002810 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 it = src[cur];
2812 Py_INCREF(it);
2813 dest[i] = it;
2814 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002815 Py_SIZE(result) = slicelength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 return result;
2817 }
2818 }
2819 else {
2820 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002821 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 item->ob_type->tp_name);
2823 return NULL;
2824 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002825}
2826
Tim Peters3b01a122002-07-19 02:35:45 +00002827static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002828list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 if (PyIndex_Check(item)) {
2831 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2832 if (i == -1 && PyErr_Occurred())
2833 return -1;
2834 if (i < 0)
2835 i += PyList_GET_SIZE(self);
2836 return list_ass_item(self, i, value);
2837 }
2838 else if (PySlice_Check(item)) {
2839 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002840
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002841 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 return -1;
2843 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002844 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2845 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 if (step == 1)
2848 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 /* Make sure s[5:2] = [..] inserts at the right place:
2851 before 5, not before 2. */
2852 if ((step < 0 && start < stop) ||
2853 (step > 0 && start > stop))
2854 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 if (value == NULL) {
2857 /* delete slice */
2858 PyObject **garbage;
2859 size_t cur;
2860 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002861 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 if (slicelength <= 0)
2864 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 if (step < 0) {
2867 stop = start + 1;
2868 start = stop + step*(slicelength - 1) - 1;
2869 step = -step;
2870 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 garbage = (PyObject**)
2873 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2874 if (!garbage) {
2875 PyErr_NoMemory();
2876 return -1;
2877 }
Tim Peters3b01a122002-07-19 02:35:45 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 /* drawing pictures might help understand these for
2880 loops. Basically, we memmove the parts of the
2881 list that are *not* part of the slice: step-1
2882 items for each item that is part of the slice,
2883 and then tail end of the list that was not
2884 covered by the slice */
2885 for (cur = start, i = 0;
2886 cur < (size_t)stop;
2887 cur += step, i++) {
2888 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 if (cur + step >= (size_t)Py_SIZE(self)) {
2893 lim = Py_SIZE(self) - cur - 1;
2894 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 memmove(self->ob_item + cur - i,
2897 self->ob_item + cur + 1,
2898 lim * sizeof(PyObject *));
2899 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002900 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 if (cur < (size_t)Py_SIZE(self)) {
2902 memmove(self->ob_item + cur - slicelength,
2903 self->ob_item + cur,
2904 (Py_SIZE(self) - cur) *
2905 sizeof(PyObject *));
2906 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002909 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 for (i = 0; i < slicelength; i++) {
2912 Py_DECREF(garbage[i]);
2913 }
2914 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002915
Victor Stinner35f28032013-11-21 12:16:35 +01002916 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 }
2918 else {
2919 /* assign slice */
2920 PyObject *ins, *seq;
2921 PyObject **garbage, **seqitems, **selfitems;
2922 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 /* protect against a[::-1] = a */
2925 if (self == (PyListObject*)value) {
2926 seq = list_slice((PyListObject*)value, 0,
2927 PyList_GET_SIZE(value));
2928 }
2929 else {
2930 seq = PySequence_Fast(value,
2931 "must assign iterable "
2932 "to extended slice");
2933 }
2934 if (!seq)
2935 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2938 PyErr_Format(PyExc_ValueError,
2939 "attempt to assign sequence of "
2940 "size %zd to extended slice of "
2941 "size %zd",
2942 PySequence_Fast_GET_SIZE(seq),
2943 slicelength);
2944 Py_DECREF(seq);
2945 return -1;
2946 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 if (!slicelength) {
2949 Py_DECREF(seq);
2950 return 0;
2951 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 garbage = (PyObject**)
2954 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2955 if (!garbage) {
2956 Py_DECREF(seq);
2957 PyErr_NoMemory();
2958 return -1;
2959 }
Tim Peters3b01a122002-07-19 02:35:45 +00002960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 selfitems = self->ob_item;
2962 seqitems = PySequence_Fast_ITEMS(seq);
2963 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002964 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002965 garbage[i] = selfitems[cur];
2966 ins = seqitems[i];
2967 Py_INCREF(ins);
2968 selfitems[cur] = ins;
2969 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 for (i = 0; i < slicelength; i++) {
2972 Py_DECREF(garbage[i]);
2973 }
Tim Peters3b01a122002-07-19 02:35:45 +00002974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 PyMem_FREE(garbage);
2976 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 return 0;
2979 }
2980 }
2981 else {
2982 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002983 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 item->ob_type->tp_name);
2985 return -1;
2986 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002987}
2988
2989static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 (lenfunc)list_length,
2991 (binaryfunc)list_subscript,
2992 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002993};
2994
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002995PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2997 "list",
2998 sizeof(PyListObject),
2999 0,
3000 (destructor)list_dealloc, /* tp_dealloc */
3001 0, /* tp_print */
3002 0, /* tp_getattr */
3003 0, /* tp_setattr */
3004 0, /* tp_reserved */
3005 (reprfunc)list_repr, /* tp_repr */
3006 0, /* tp_as_number */
3007 &list_as_sequence, /* tp_as_sequence */
3008 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003009 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 0, /* tp_call */
3011 0, /* tp_str */
3012 PyObject_GenericGetAttr, /* tp_getattro */
3013 0, /* tp_setattro */
3014 0, /* tp_as_buffer */
3015 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003016 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
3017 list___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 (traverseproc)list_traverse, /* tp_traverse */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003019 (inquiry)_list_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 list_richcompare, /* tp_richcompare */
3021 0, /* tp_weaklistoffset */
3022 list_iter, /* tp_iter */
3023 0, /* tp_iternext */
3024 list_methods, /* tp_methods */
3025 0, /* tp_members */
3026 0, /* tp_getset */
3027 0, /* tp_base */
3028 0, /* tp_dict */
3029 0, /* tp_descr_get */
3030 0, /* tp_descr_set */
3031 0, /* tp_dictoffset */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003032 (initproc)list___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 PyType_GenericAlloc, /* tp_alloc */
3034 PyType_GenericNew, /* tp_new */
3035 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003036};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00003037
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003038/*********************** List Iterator **************************/
3039
3040typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02003042 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003044} listiterobject;
3045
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003046static void listiter_dealloc(listiterobject *);
3047static int listiter_traverse(listiterobject *, visitproc, void *);
3048static PyObject *listiter_next(listiterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303049static PyObject *listiter_len(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003050static PyObject *listiter_reduce_general(void *_it, int forward);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303051static PyObject *listiter_reduce(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003052static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00003053
Armin Rigof5b3e362006-02-11 21:32:43 +00003054PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003055PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3056PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003057
3058static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003060 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3061 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00003063};
3064
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003065PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3067 "list_iterator", /* tp_name */
3068 sizeof(listiterobject), /* tp_basicsize */
3069 0, /* tp_itemsize */
3070 /* methods */
3071 (destructor)listiter_dealloc, /* tp_dealloc */
3072 0, /* tp_print */
3073 0, /* tp_getattr */
3074 0, /* tp_setattr */
3075 0, /* tp_reserved */
3076 0, /* tp_repr */
3077 0, /* tp_as_number */
3078 0, /* tp_as_sequence */
3079 0, /* tp_as_mapping */
3080 0, /* tp_hash */
3081 0, /* tp_call */
3082 0, /* tp_str */
3083 PyObject_GenericGetAttr, /* tp_getattro */
3084 0, /* tp_setattro */
3085 0, /* tp_as_buffer */
3086 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3087 0, /* tp_doc */
3088 (traverseproc)listiter_traverse, /* tp_traverse */
3089 0, /* tp_clear */
3090 0, /* tp_richcompare */
3091 0, /* tp_weaklistoffset */
3092 PyObject_SelfIter, /* tp_iter */
3093 (iternextfunc)listiter_next, /* tp_iternext */
3094 listiter_methods, /* tp_methods */
3095 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003096};
Raymond Hettinger1021c442003-11-07 15:38:09 +00003097
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003098
3099static PyObject *
3100list_iter(PyObject *seq)
3101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 if (!PyList_Check(seq)) {
3105 PyErr_BadInternalCall();
3106 return NULL;
3107 }
3108 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3109 if (it == NULL)
3110 return NULL;
3111 it->it_index = 0;
3112 Py_INCREF(seq);
3113 it->it_seq = (PyListObject *)seq;
3114 _PyObject_GC_TRACK(it);
3115 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003116}
3117
3118static void
3119listiter_dealloc(listiterobject *it)
3120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 _PyObject_GC_UNTRACK(it);
3122 Py_XDECREF(it->it_seq);
3123 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003124}
3125
3126static int
3127listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 Py_VISIT(it->it_seq);
3130 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003131}
3132
3133static PyObject *
3134listiter_next(listiterobject *it)
3135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 PyListObject *seq;
3137 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 assert(it != NULL);
3140 seq = it->it_seq;
3141 if (seq == NULL)
3142 return NULL;
3143 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 if (it->it_index < PyList_GET_SIZE(seq)) {
3146 item = PyList_GET_ITEM(seq, it->it_index);
3147 ++it->it_index;
3148 Py_INCREF(item);
3149 return item;
3150 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003153 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003155}
3156
3157static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303158listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 Py_ssize_t len;
3161 if (it->it_seq) {
3162 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3163 if (len >= 0)
3164 return PyLong_FromSsize_t(len);
3165 }
3166 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003167}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003168
3169static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303170listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003171{
3172 return listiter_reduce_general(it, 1);
3173}
3174
3175static PyObject *
3176listiter_setstate(listiterobject *it, PyObject *state)
3177{
Victor Stinner7660b882013-06-24 23:59:24 +02003178 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003179 if (index == -1 && PyErr_Occurred())
3180 return NULL;
3181 if (it->it_seq != NULL) {
3182 if (index < 0)
3183 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003184 else if (index > PyList_GET_SIZE(it->it_seq))
3185 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003186 it->it_index = index;
3187 }
3188 Py_RETURN_NONE;
3189}
3190
Raymond Hettinger1021c442003-11-07 15:38:09 +00003191/*********************** List Reverse Iterator **************************/
3192
3193typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 PyObject_HEAD
3195 Py_ssize_t it_index;
3196 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00003197} listreviterobject;
3198
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003199static void listreviter_dealloc(listreviterobject *);
3200static int listreviter_traverse(listreviterobject *, visitproc, void *);
3201static PyObject *listreviter_next(listreviterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303202static PyObject *listreviter_len(listreviterobject *, PyObject *);
3203static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003204static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003205
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003206static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003208 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3209 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003211};
3212
Raymond Hettinger1021c442003-11-07 15:38:09 +00003213PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3215 "list_reverseiterator", /* tp_name */
3216 sizeof(listreviterobject), /* tp_basicsize */
3217 0, /* tp_itemsize */
3218 /* methods */
3219 (destructor)listreviter_dealloc, /* tp_dealloc */
3220 0, /* tp_print */
3221 0, /* tp_getattr */
3222 0, /* tp_setattr */
3223 0, /* tp_reserved */
3224 0, /* tp_repr */
3225 0, /* tp_as_number */
3226 0, /* tp_as_sequence */
3227 0, /* tp_as_mapping */
3228 0, /* tp_hash */
3229 0, /* tp_call */
3230 0, /* tp_str */
3231 PyObject_GenericGetAttr, /* tp_getattro */
3232 0, /* tp_setattro */
3233 0, /* tp_as_buffer */
3234 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3235 0, /* tp_doc */
3236 (traverseproc)listreviter_traverse, /* tp_traverse */
3237 0, /* tp_clear */
3238 0, /* tp_richcompare */
3239 0, /* tp_weaklistoffset */
3240 PyObject_SelfIter, /* tp_iter */
3241 (iternextfunc)listreviter_next, /* tp_iternext */
3242 listreviter_methods, /* tp_methods */
3243 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00003244};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003245
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003246/*[clinic input]
3247list.__reversed__
3248
3249Return a reverse iterator over the list.
3250[clinic start generated code]*/
3251
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003252static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003253list___reversed___impl(PyListObject *self)
3254/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3259 if (it == NULL)
3260 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003261 assert(PyList_Check(self));
3262 it->it_index = PyList_GET_SIZE(self) - 1;
3263 Py_INCREF(self);
3264 it->it_seq = self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 PyObject_GC_Track(it);
3266 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003267}
3268
3269static void
3270listreviter_dealloc(listreviterobject *it)
3271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 PyObject_GC_UnTrack(it);
3273 Py_XDECREF(it->it_seq);
3274 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003275}
3276
3277static int
3278listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 Py_VISIT(it->it_seq);
3281 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003282}
3283
3284static PyObject *
3285listreviter_next(listreviterobject *it)
3286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003288 Py_ssize_t index;
3289 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003290
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003291 assert(it != NULL);
3292 seq = it->it_seq;
3293 if (seq == NULL) {
3294 return NULL;
3295 }
3296 assert(PyList_Check(seq));
3297
3298 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 if (index>=0 && index < PyList_GET_SIZE(seq)) {
3300 item = PyList_GET_ITEM(seq, index);
3301 it->it_index--;
3302 Py_INCREF(item);
3303 return item;
3304 }
3305 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003306 it->it_seq = NULL;
3307 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003309}
3310
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003311static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303312listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 Py_ssize_t len = it->it_index + 1;
3315 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3316 len = 0;
3317 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003318}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003319
3320static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303321listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003322{
3323 return listiter_reduce_general(it, 0);
3324}
3325
3326static PyObject *
3327listreviter_setstate(listreviterobject *it, PyObject *state)
3328{
3329 Py_ssize_t index = PyLong_AsSsize_t(state);
3330 if (index == -1 && PyErr_Occurred())
3331 return NULL;
3332 if (it->it_seq != NULL) {
3333 if (index < -1)
3334 index = -1;
3335 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3336 index = PyList_GET_SIZE(it->it_seq) - 1;
3337 it->it_index = index;
3338 }
3339 Py_RETURN_NONE;
3340}
3341
3342/* common pickling support */
3343
3344static PyObject *
3345listiter_reduce_general(void *_it, int forward)
3346{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003347 _Py_IDENTIFIER(iter);
3348 _Py_IDENTIFIER(reversed);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003349 PyObject *list;
3350
3351 /* the objects are not the same, index is of different types! */
3352 if (forward) {
3353 listiterobject *it = (listiterobject *)_it;
3354 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003355 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003356 it->it_seq, it->it_index);
3357 } else {
3358 listreviterobject *it = (listreviterobject *)_it;
3359 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003360 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_reversed),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003361 it->it_seq, it->it_index);
3362 }
3363 /* empty iterator, create an empty list */
3364 list = PyList_New(0);
3365 if (list == NULL)
3366 return NULL;
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003367 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003368}