blob: 6da8391fc27544034c1e02a4f78c61252983ae84 [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"
Victor Stinnere281f7d2018-11-01 02:30:36 +01006#include "pycore_accu.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00008#ifdef STDC_HEADERS
9#include <stddef.h>
10#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011#include <sys/types.h> /* For size_t */
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000012#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013
Serhiy Storchakafdd42c42017-03-11 09:19:20 +020014/*[clinic input]
15class list "PyListObject *" "&PyList_Type"
16[clinic start generated code]*/
17/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f9b222678f9f71e0]*/
18
19#include "clinic/listobject.c.h"
20
Tim Peters8d9eb102004-07-31 02:24:20 +000021/* Ensure ob_item has room for at least newsize elements, and set
22 * ob_size to newsize. If newsize > ob_size on entry, the content
23 * of the new slots at exit is undefined heap trash; it's the caller's
Ezio Melotti13925002011-03-16 11:05:33 +020024 * responsibility to overwrite them with sane values.
Tim Peters8d9eb102004-07-31 02:24:20 +000025 * The number of allocated elements may grow, shrink, or stay the same.
26 * Failure is impossible if newsize <= self.allocated on entry, although
27 * that partly relies on an assumption that the system realloc() never
28 * fails when passed a number of bytes <= the number of bytes last
29 * allocated (the C standard doesn't guarantee this, but it's hard to
30 * imagine a realloc implementation where it wouldn't be true).
31 * Note that self->ob_item may change, and even if newsize is less
32 * than ob_size on entry.
33 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000034static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000035list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 PyObject **items;
Xiang Zhang4cee0492017-02-22 12:32:30 +080038 size_t new_allocated, num_allocated_bytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 /* Bypass realloc() when a previous overallocation is large enough
42 to accommodate the newsize. If the newsize falls lower than half
43 the allocated size, then proceed with the realloc() to shrink the list.
44 */
45 if (allocated >= newsize && newsize >= (allocated >> 1)) {
46 assert(self->ob_item != NULL || newsize == 0);
47 Py_SIZE(self) = newsize;
48 return 0;
49 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +000050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 /* This over-allocates proportional to the list size, making room
52 * for additional growth. The over-allocation is mild, but is
53 * enough to give linear-time amortized behavior over a long
54 * sequence of appends() in the presence of a poorly-performing
55 * system realloc().
56 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
Xiang Zhang4cee0492017-02-22 12:32:30 +080057 * Note: new_allocated won't overflow because the largest possible value
58 * is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 */
Xiang Zhang4cee0492017-02-22 12:32:30 +080060 new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
61 if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 PyErr_NoMemory();
63 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 if (newsize == 0)
67 new_allocated = 0;
Xiang Zhang4cee0492017-02-22 12:32:30 +080068 num_allocated_bytes = new_allocated * sizeof(PyObject *);
69 items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 if (items == NULL) {
71 PyErr_NoMemory();
72 return -1;
73 }
74 self->ob_item = items;
75 Py_SIZE(self) = newsize;
76 self->allocated = new_allocated;
77 return 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000078}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000079
Pablo Galindo372d7052018-10-28 20:16:26 +000080static int
81list_preallocate_exact(PyListObject *self, Py_ssize_t size)
82{
83 assert(self->ob_item == NULL);
84
85 PyObject **items;
86 size_t allocated;
87
88 allocated = (size_t)size;
89 if (allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
90 PyErr_NoMemory();
91 return -1;
92 }
93
94 if (size == 0) {
95 allocated = 0;
96 }
97 items = (PyObject **)PyMem_New(PyObject*, allocated);
98 if (items == NULL) {
99 PyErr_NoMemory();
100 return -1;
101 }
102 self->ob_item = items;
103 self->allocated = allocated;
104 return 0;
105}
106
Christian Heimes77c02eb2008-02-09 02:18:51 +0000107/* Debug statistic to compare allocations with reuse through the free list */
108#undef SHOW_ALLOC_COUNT
109#ifdef SHOW_ALLOC_COUNT
110static size_t count_alloc = 0;
111static size_t count_reuse = 0;
112
113static void
114show_alloc(void)
115{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200116 PyInterpreterState *interp = _PyInterpreterState_Get();
Eddie Elizondo745dc652018-02-21 20:55:18 -0800117 if (!interp->core_config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300118 return;
Victor Stinner25420fe2017-11-20 18:12:22 -0800119 }
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
122 count_alloc);
123 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
124 "d\n", count_reuse);
125 fprintf(stderr, "%.2f%% reuse rate\n\n",
126 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +0000127}
128#endif
129
Raymond Hettinger0468e412004-05-05 05:37:53 +0000130/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000131#ifndef PyList_MAXFREELIST
132#define PyList_MAXFREELIST 80
133#endif
134static PyListObject *free_list[PyList_MAXFREELIST];
135static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000136
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100137int
138PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100141 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 while (numfree) {
143 op = free_list[--numfree];
144 assert(PyList_CheckExact(op));
145 PyObject_GC_Del(op);
146 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100147 return ret;
148}
149
150void
151PyList_Fini(void)
152{
153 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000154}
155
David Malcolm49526f42012-06-22 14:55:41 -0400156/* Print summary info about the state of the optimized allocator */
157void
158_PyList_DebugMallocStats(FILE *out)
159{
160 _PyDebugAllocatorStats(out,
161 "free PyListObject",
162 numfree, sizeof(PyListObject));
163}
164
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000166PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 PyListObject *op;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000169#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 static int initialized = 0;
171 if (!initialized) {
172 Py_AtExit(show_alloc);
173 initialized = 1;
174 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000175#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 if (size < 0) {
178 PyErr_BadInternalCall();
179 return NULL;
180 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 if (numfree) {
182 numfree--;
183 op = free_list[numfree];
184 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000185#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000187#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 } else {
189 op = PyObject_GC_New(PyListObject, &PyList_Type);
190 if (op == NULL)
191 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000192#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000194#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 }
196 if (size <= 0)
197 op->ob_item = NULL;
198 else {
Mark Dickinson5d132382016-08-21 08:55:15 +0100199 op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 if (op->ob_item == NULL) {
201 Py_DECREF(op);
202 return PyErr_NoMemory();
203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 }
205 Py_SIZE(op) = size;
206 op->allocated = size;
207 _PyObject_GC_TRACK(op);
208 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209}
210
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500211static PyObject *
212list_new_prealloc(Py_ssize_t size)
213{
214 PyListObject *op = (PyListObject *) PyList_New(0);
215 if (size == 0 || op == NULL) {
216 return (PyObject *) op;
217 }
218 assert(op->ob_item == NULL);
219 op->ob_item = PyMem_New(PyObject *, size);
220 if (op->ob_item == NULL) {
221 Py_DECREF(op);
222 return PyErr_NoMemory();
223 }
224 op->allocated = size;
225 return (PyObject *) op;
226}
227
Martin v. Löwis18e16552006-02-15 17:27:45 +0000228Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000229PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 if (!PyList_Check(op)) {
232 PyErr_BadInternalCall();
233 return -1;
234 }
235 else
236 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237}
238
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700239static inline int
240valid_index(Py_ssize_t i, Py_ssize_t limit)
241{
242 /* The cast to size_t lets us use just a single comparison
243 to check whether i is in the range: 0 <= i < limit.
244
245 See: Section 14.2 "Bounds Checking" in the Agner Fog
246 optimization manual found at:
247 https://www.agner.org/optimize/optimizing_cpp.pdf
248 */
249 return (size_t) i < (size_t) limit;
250}
251
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000252static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000253
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000254PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000255PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 if (!PyList_Check(op)) {
258 PyErr_BadInternalCall();
259 return NULL;
260 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700261 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 if (indexerr == NULL) {
263 indexerr = PyUnicode_FromString(
264 "list index out of range");
265 if (indexerr == NULL)
266 return NULL;
267 }
268 PyErr_SetObject(PyExc_IndexError, indexerr);
269 return NULL;
270 }
271 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272}
273
274int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200275PyList_SetItem(PyObject *op, Py_ssize_t i,
276 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200278 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 if (!PyList_Check(op)) {
280 Py_XDECREF(newitem);
281 PyErr_BadInternalCall();
282 return -1;
283 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700284 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 Py_XDECREF(newitem);
286 PyErr_SetString(PyExc_IndexError,
287 "list assignment index out of range");
288 return -1;
289 }
290 p = ((PyListObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300291 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000293}
294
295static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000296ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 Py_ssize_t i, n = Py_SIZE(self);
299 PyObject **items;
300 if (v == NULL) {
301 PyErr_BadInternalCall();
302 return -1;
303 }
304 if (n == PY_SSIZE_T_MAX) {
305 PyErr_SetString(PyExc_OverflowError,
306 "cannot add more objects to list");
307 return -1;
308 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000309
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800310 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 if (where < 0) {
314 where += n;
315 if (where < 0)
316 where = 0;
317 }
318 if (where > n)
319 where = n;
320 items = self->ob_item;
321 for (i = n; --i >= where; )
322 items[i+1] = items[i];
323 Py_INCREF(v);
324 items[where] = v;
325 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326}
327
328int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000329PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 if (!PyList_Check(op)) {
332 PyErr_BadInternalCall();
333 return -1;
334 }
335 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336}
337
Raymond Hettinger40a03822004-04-12 13:05:09 +0000338static int
339app1(PyListObject *self, PyObject *v)
340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 assert (v != NULL);
344 if (n == PY_SSIZE_T_MAX) {
345 PyErr_SetString(PyExc_OverflowError,
346 "cannot add more objects to list");
347 return -1;
348 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000349
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800350 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 Py_INCREF(v);
354 PyList_SET_ITEM(self, n, v);
355 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000356}
357
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358int
Fred Drakea2f55112000-07-09 15:16:51 +0000359PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 if (PyList_Check(op) && (newitem != NULL))
362 return app1((PyListObject *)op, newitem);
363 PyErr_BadInternalCall();
364 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365}
366
367/* Methods */
368
369static void
Fred Drakea2f55112000-07-09 15:16:51 +0000370list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 Py_ssize_t i;
373 PyObject_GC_UnTrack(op);
374 Py_TRASHCAN_SAFE_BEGIN(op)
375 if (op->ob_item != NULL) {
376 /* Do it backwards, for Christian Tismer.
377 There's a simple test case where somehow this reduces
378 thrashing when a *very* large list is created and
379 immediately deleted. */
380 i = Py_SIZE(op);
381 while (--i >= 0) {
382 Py_XDECREF(op->ob_item[i]);
383 }
384 PyMem_FREE(op->ob_item);
385 }
386 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
387 free_list[numfree++] = op;
388 else
389 Py_TYPE(op)->tp_free((PyObject *)op);
390 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391}
392
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000394list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 Py_ssize_t i;
Victor Stinner5c733472013-11-18 21:11:57 +0100397 PyObject *s;
Victor Stinner5c733472013-11-18 21:11:57 +0100398 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200399
400 if (Py_SIZE(v) == 0) {
401 return PyUnicode_FromString("[]");
402 }
403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 i = Py_ReprEnter((PyObject*)v);
405 if (i != 0) {
406 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
407 }
Tim Petersa7259592001-06-16 05:11:17 +0000408
Victor Stinner5c733472013-11-18 21:11:57 +0100409 _PyUnicodeWriter_Init(&writer);
410 writer.overallocate = 1;
Victor Stinnerb8fb1972013-11-18 22:15:44 +0100411 /* "[" + "1" + ", 2" * (len - 1) + "]" */
412 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +0000413
Victor Stinner5c733472013-11-18 21:11:57 +0100414 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200415 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 /* Do repr() on each element. Note that this may mutate the list,
418 so must refetch the list size on each iteration. */
419 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100420 if (i > 0) {
Victor Stinner4a587072013-11-19 12:54:53 +0100421 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
Victor Stinner5c733472013-11-18 21:11:57 +0100422 goto error;
423 }
424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner5c733472013-11-18 21:11:57 +0100426 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200427 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100428
429 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
430 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200431 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100432 }
433 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 }
Victor Stinner5c733472013-11-18 21:11:57 +0100435
Victor Stinner4d3f1092013-11-19 12:09:00 +0100436 writer.overallocate = 0;
Victor Stinner5c733472013-11-18 21:11:57 +0100437 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200438 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100441 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200442
443error:
Victor Stinner5c733472013-11-18 21:11:57 +0100444 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200445 Py_ReprLeave((PyObject *)v);
446 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447}
448
Martin v. Löwis18e16552006-02-15 17:27:45 +0000449static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000450list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453}
454
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000455static int
Fred Drakea2f55112000-07-09 15:16:51 +0000456list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 Py_ssize_t i;
459 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
462 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
463 Py_EQ);
464 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000465}
466
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000467static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000468list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700470 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 if (indexerr == NULL) {
472 indexerr = PyUnicode_FromString(
473 "list index out of range");
474 if (indexerr == NULL)
475 return NULL;
476 }
477 PyErr_SetObject(PyExc_IndexError, indexerr);
478 return NULL;
479 }
480 Py_INCREF(a->ob_item[i]);
481 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000482}
483
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000484static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000485list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 PyListObject *np;
488 PyObject **src, **dest;
489 Py_ssize_t i, len;
490 if (ilow < 0)
491 ilow = 0;
492 else if (ilow > Py_SIZE(a))
493 ilow = Py_SIZE(a);
494 if (ihigh < ilow)
495 ihigh = ilow;
496 else if (ihigh > Py_SIZE(a))
497 ihigh = Py_SIZE(a);
498 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500499 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (np == NULL)
501 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 src = a->ob_item + ilow;
504 dest = np->ob_item;
505 for (i = 0; i < len; i++) {
506 PyObject *v = src[i];
507 Py_INCREF(v);
508 dest[i] = v;
509 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500510 Py_SIZE(np) = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512}
513
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000514PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000515PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 if (!PyList_Check(a)) {
518 PyErr_BadInternalCall();
519 return NULL;
520 }
521 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000522}
523
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000525list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 Py_ssize_t size;
528 Py_ssize_t i;
529 PyObject **src, **dest;
530 PyListObject *np;
531 if (!PyList_Check(bb)) {
532 PyErr_Format(PyExc_TypeError,
533 "can only concatenate list (not \"%.200s\") to list",
534 bb->ob_type->tp_name);
535 return NULL;
536 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000538 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000540 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500541 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (np == NULL) {
543 return NULL;
544 }
545 src = a->ob_item;
546 dest = np->ob_item;
547 for (i = 0; i < Py_SIZE(a); i++) {
548 PyObject *v = src[i];
549 Py_INCREF(v);
550 dest[i] = v;
551 }
552 src = b->ob_item;
553 dest = np->ob_item + Py_SIZE(a);
554 for (i = 0; i < Py_SIZE(b); i++) {
555 PyObject *v = src[i];
556 Py_INCREF(v);
557 dest[i] = v;
558 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500559 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000561#undef b
562}
563
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000565list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 Py_ssize_t i, j;
568 Py_ssize_t size;
569 PyListObject *np;
570 PyObject **p, **items;
571 PyObject *elem;
572 if (n < 0)
573 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100574 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100576 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 if (size == 0)
578 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500579 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (np == NULL)
581 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500584 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 elem = a->ob_item[0];
586 for (i = 0; i < n; i++) {
587 items[i] = elem;
588 Py_INCREF(elem);
589 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500591 else {
592 p = np->ob_item;
593 items = a->ob_item;
594 for (i = 0; i < n; i++) {
595 for (j = 0; j < Py_SIZE(a); j++) {
596 *p = items[j];
597 Py_INCREF(*p);
598 p++;
599 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 }
601 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500602 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000604}
605
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000606static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200607_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 Py_ssize_t i;
610 PyObject **item = a->ob_item;
611 if (item != NULL) {
612 /* Because XDECREF can recursively invoke operations on
613 this list, we make it empty first. */
614 i = Py_SIZE(a);
615 Py_SIZE(a) = 0;
616 a->ob_item = NULL;
617 a->allocated = 0;
618 while (--i >= 0) {
619 Py_XDECREF(item[i]);
620 }
621 PyMem_FREE(item);
622 }
623 /* Never fails; the return value can be ignored.
624 Note that there is no guarantee that the list is actually empty
625 at this point, because XDECREF may have populated it again! */
626 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000627}
628
Tim Peters8fc4a912004-07-31 21:53:19 +0000629/* a[ilow:ihigh] = v if v != NULL.
630 * del a[ilow:ihigh] if v == NULL.
631 *
632 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
633 * guaranteed the call cannot fail.
634 */
Armin Rigo93677f02004-07-29 12:40:23 +0000635static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000636list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 /* Because [X]DECREF can recursively invoke list operations on
639 this list, we must postpone all [X]DECREF activity until
640 after the list is back in its canonical shape. Therefore
641 we must allocate an additional array, 'recycle', into which
642 we temporarily copy the items that are deleted from the
643 list. :-( */
644 PyObject *recycle_on_stack[8];
645 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
646 PyObject **item;
647 PyObject **vitem = NULL;
648 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
649 Py_ssize_t n; /* # of elements in replacement list */
650 Py_ssize_t norig; /* # of elements in list getting replaced */
651 Py_ssize_t d; /* Change in size */
652 Py_ssize_t k;
653 size_t s;
654 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 if (v == NULL)
657 n = 0;
658 else {
659 if (a == b) {
660 /* Special case "a[i:j] = a" -- copy b first */
661 v = list_slice(b, 0, Py_SIZE(b));
662 if (v == NULL)
663 return result;
664 result = list_ass_slice(a, ilow, ihigh, v);
665 Py_DECREF(v);
666 return result;
667 }
668 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
669 if(v_as_SF == NULL)
670 goto Error;
671 n = PySequence_Fast_GET_SIZE(v_as_SF);
672 vitem = PySequence_Fast_ITEMS(v_as_SF);
673 }
674 if (ilow < 0)
675 ilow = 0;
676 else if (ilow > Py_SIZE(a))
677 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 if (ihigh < ilow)
680 ihigh = ilow;
681 else if (ihigh > Py_SIZE(a))
682 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 norig = ihigh - ilow;
685 assert(norig >= 0);
686 d = n - norig;
687 if (Py_SIZE(a) + d == 0) {
688 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200689 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 }
691 item = a->ob_item;
692 /* recycle the items that we are about to remove */
693 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700694 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
695 if (s) {
696 if (s > sizeof(recycle_on_stack)) {
697 recycle = (PyObject **)PyMem_MALLOC(s);
698 if (recycle == NULL) {
699 PyErr_NoMemory();
700 goto Error;
701 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700703 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200707 Py_ssize_t tail;
708 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
709 memmove(&item[ihigh+d], &item[ihigh], tail);
710 if (list_resize(a, Py_SIZE(a) + d) < 0) {
711 memmove(&item[ihigh], &item[ihigh+d], tail);
712 memcpy(&item[ilow], recycle, s);
713 goto Error;
714 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 item = a->ob_item;
716 }
717 else if (d > 0) { /* Insert d items */
718 k = Py_SIZE(a);
719 if (list_resize(a, k+d) < 0)
720 goto Error;
721 item = a->ob_item;
722 memmove(&item[ihigh+d], &item[ihigh],
723 (k - ihigh)*sizeof(PyObject *));
724 }
725 for (k = 0; k < n; k++, ilow++) {
726 PyObject *w = vitem[k];
727 Py_XINCREF(w);
728 item[ilow] = w;
729 }
730 for (k = norig - 1; k >= 0; --k)
731 Py_XDECREF(recycle[k]);
732 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000733 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 if (recycle != recycle_on_stack)
735 PyMem_FREE(recycle);
736 Py_XDECREF(v_as_SF);
737 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000738#undef b
739}
740
Guido van Rossum234f9421993-06-17 12:35:49 +0000741int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000742PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 if (!PyList_Check(a)) {
745 PyErr_BadInternalCall();
746 return -1;
747 }
748 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000749}
750
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000751static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000752list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 PyObject **items;
755 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000756
757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 size = PyList_GET_SIZE(self);
759 if (size == 0 || n == 1) {
760 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 (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200765 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 Py_INCREF(self);
767 return (PyObject *)self;
768 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 if (size > PY_SSIZE_T_MAX / n) {
771 return PyErr_NoMemory();
772 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000773
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800774 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 p = size;
778 items = self->ob_item;
779 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
780 for (j = 0; j < size; j++) {
781 PyObject *o = items[j];
782 Py_INCREF(o);
783 items[p++] = o;
784 }
785 }
786 Py_INCREF(self);
787 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000788}
789
Guido van Rossum4a450d01991-04-03 19:05:18 +0000790static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000791list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000792{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700793 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 PyErr_SetString(PyExc_IndexError,
795 "list assignment index out of range");
796 return -1;
797 }
798 if (v == NULL)
799 return list_ass_slice(a, i, i+1, v);
800 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300801 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000803}
804
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200805/*[clinic input]
806list.insert
807
808 index: Py_ssize_t
809 object: object
810 /
811
812Insert object before index.
813[clinic start generated code]*/
814
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000815static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200816list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
817/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000818{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200819 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 Py_RETURN_NONE;
821 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822}
823
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200824/*[clinic input]
825list.clear
826
827Remove all items from list.
828[clinic start generated code]*/
829
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000830static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200831list_clear_impl(PyListObject *self)
832/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000833{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200834 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000835 Py_RETURN_NONE;
836}
837
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200838/*[clinic input]
839list.copy
840
841Return a shallow copy of the list.
842[clinic start generated code]*/
843
Eli Benderskycbbaa962011-02-25 05:47:53 +0000844static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200845list_copy_impl(PyListObject *self)
846/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000847{
848 return list_slice(self, 0, Py_SIZE(self));
849}
850
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200851/*[clinic input]
852list.append
853
854 object: object
855 /
856
857Append object to the end of the list.
858[clinic start generated code]*/
859
Eli Benderskycbbaa962011-02-25 05:47:53 +0000860static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200861list_append(PyListObject *self, PyObject *object)
862/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000863{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200864 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 Py_RETURN_NONE;
866 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000867}
868
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200869/*[clinic input]
870list.extend
871
872 iterable: object
873 /
874
875Extend list by appending elements from the iterable.
876[clinic start generated code]*/
877
Barry Warsawdedf6d61998-10-09 16:37:25 +0000878static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200879list_extend(PyListObject *self, PyObject *iterable)
880/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 PyObject *it; /* iter(v) */
883 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200884 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 Py_ssize_t mn; /* m + n */
886 Py_ssize_t i;
887 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 /* Special cases:
890 1) lists and tuples which can use PySequence_Fast ops
891 2) extending self to self requires making a copy first
892 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200893 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
894 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200896 iterable = PySequence_Fast(iterable, "argument must be iterable");
897 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200899 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200901 /* short circuit when iterable is empty */
902 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 Py_RETURN_NONE;
904 }
905 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000906 /* It should not be possible to allocate a list large enough to cause
907 an overflow on any relevant platform */
908 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800909 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200910 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 return NULL;
912 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200913 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 * situation a.extend(a), but the following code works
915 * in that case too. Just make sure to resize self
916 * before calling PySequence_Fast_ITEMS.
917 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200918 /* populate the end of self with iterable's items */
919 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 dest = self->ob_item + m;
921 for (i = 0; i < n; i++) {
922 PyObject *o = src[i];
923 Py_INCREF(o);
924 dest[i] = o;
925 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200926 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 Py_RETURN_NONE;
928 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000929
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200930 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 if (it == NULL)
932 return NULL;
933 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200936 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800937 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 Py_DECREF(it);
939 return NULL;
940 }
941 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000942 if (m > PY_SSIZE_T_MAX - n) {
943 /* m + n overflowed; on the chance that n lied, and there really
944 * is enough room, ignore it. If n was telling the truth, we'll
945 * eventually run out of memory during the loop.
946 */
947 }
948 else {
949 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800951 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 goto error;
953 /* Make the list sane again. */
954 Py_SIZE(self) = m;
955 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 /* Run iterator to exhaustion. */
958 for (;;) {
959 PyObject *item = iternext(it);
960 if (item == NULL) {
961 if (PyErr_Occurred()) {
962 if (PyErr_ExceptionMatches(PyExc_StopIteration))
963 PyErr_Clear();
964 else
965 goto error;
966 }
967 break;
968 }
969 if (Py_SIZE(self) < self->allocated) {
970 /* steals ref */
971 PyList_SET_ITEM(self, Py_SIZE(self), item);
972 ++Py_SIZE(self);
973 }
974 else {
975 int status = app1(self, item);
976 Py_DECREF(item); /* append creates a new ref */
977 if (status < 0)
978 goto error;
979 }
980 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200983 if (Py_SIZE(self) < self->allocated) {
984 if (list_resize(self, Py_SIZE(self)) < 0)
985 goto error;
986 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 Py_DECREF(it);
989 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000990
991 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 Py_DECREF(it);
993 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000994}
995
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000996PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200997_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000998{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200999 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +00001000}
1001
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001002static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001003list_inplace_concat(PyListObject *self, PyObject *other)
1004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001006
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001007 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 if (result == NULL)
1009 return result;
1010 Py_DECREF(result);
1011 Py_INCREF(self);
1012 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001013}
1014
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001015/*[clinic input]
1016list.pop
1017
1018 index: Py_ssize_t = -1
1019 /
1020
1021Remove and return item at index (default last).
1022
1023Raises IndexError if list is empty or index is out of range.
1024[clinic start generated code]*/
1025
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001026static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001027list_pop_impl(PyListObject *self, Py_ssize_t index)
1028/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 PyObject *v;
1031 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +00001032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 if (Py_SIZE(self) == 0) {
1034 /* Special-case most common failure cause */
1035 PyErr_SetString(PyExc_IndexError, "pop from empty list");
1036 return NULL;
1037 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001038 if (index < 0)
1039 index += Py_SIZE(self);
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -07001040 if (!valid_index(index, Py_SIZE(self))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1042 return NULL;
1043 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001044 v = self->ob_item[index];
1045 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001047 if (status >= 0)
1048 return v; /* and v now owns the reference the list had */
1049 else
1050 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 }
1052 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001053 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001054 if (status < 0) {
1055 Py_DECREF(v);
1056 return NULL;
1057 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001059}
1060
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001061/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1062static void
1063reverse_slice(PyObject **lo, PyObject **hi)
1064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 --hi;
1068 while (lo < hi) {
1069 PyObject *t = *lo;
1070 *lo = *hi;
1071 *hi = t;
1072 ++lo;
1073 --hi;
1074 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001075}
1076
Tim Petersa64dc242002-08-01 02:13:36 +00001077/* Lots of code for an adaptive, stable, natural mergesort. There are many
1078 * pieces to this algorithm; read listsort.txt for overviews and details.
1079 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001080
Daniel Stutzbach98338222010-12-02 21:55:33 +00001081/* A sortslice contains a pointer to an array of keys and a pointer to
1082 * an array of corresponding values. In other words, keys[i]
1083 * corresponds with values[i]. If values == NULL, then the keys are
1084 * also the values.
1085 *
1086 * Several convenience routines are provided here, so that keys and
1087 * values are always moved in sync.
1088 */
1089
1090typedef struct {
1091 PyObject **keys;
1092 PyObject **values;
1093} sortslice;
1094
1095Py_LOCAL_INLINE(void)
1096sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1097{
1098 s1->keys[i] = s2->keys[j];
1099 if (s1->values != NULL)
1100 s1->values[i] = s2->values[j];
1101}
1102
1103Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001104sortslice_copy_incr(sortslice *dst, sortslice *src)
1105{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001106 *dst->keys++ = *src->keys++;
1107 if (dst->values != NULL)
1108 *dst->values++ = *src->values++;
1109}
1110
1111Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001112sortslice_copy_decr(sortslice *dst, sortslice *src)
1113{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001114 *dst->keys-- = *src->keys--;
1115 if (dst->values != NULL)
1116 *dst->values-- = *src->values--;
1117}
1118
1119
1120Py_LOCAL_INLINE(void)
1121sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001122 Py_ssize_t n)
1123{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001124 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1125 if (s1->values != NULL)
1126 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1127}
1128
1129Py_LOCAL_INLINE(void)
1130sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001131 Py_ssize_t n)
1132{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001133 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1134 if (s1->values != NULL)
1135 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1136}
1137
1138Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001139sortslice_advance(sortslice *slice, Py_ssize_t n)
1140{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001141 slice->keys += n;
1142 if (slice->values != NULL)
1143 slice->values += n;
1144}
1145
embg1e34da42018-01-28 20:03:23 -07001146/* Comparison function: ms->key_compare, which is set at run-time in
1147 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001148 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1149 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001150
embg1e34da42018-01-28 20:03:23 -07001151#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001152
1153/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001154 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1155 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1156*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001157#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001159
embg1e34da42018-01-28 20:03:23 -07001160/* The maximum number of entries in a MergeState's pending-runs stack.
1161 * This is enough to sort arrays of size up to about
1162 * 32 * phi ** MAX_MERGE_PENDING
1163 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1164 * with 2**64 elements.
1165 */
1166#define MAX_MERGE_PENDING 85
1167
1168/* When we get into galloping mode, we stay there until both runs win less
1169 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1170 */
1171#define MIN_GALLOP 7
1172
1173/* Avoid malloc for small temp arrays. */
1174#define MERGESTATE_TEMP_SIZE 256
1175
1176/* One MergeState exists on the stack per invocation of mergesort. It's just
1177 * a convenient way to pass state around among the helper functions.
1178 */
1179struct s_slice {
1180 sortslice base;
1181 Py_ssize_t len;
1182};
1183
1184typedef struct s_MergeState MergeState;
1185struct s_MergeState {
1186 /* This controls when we get *into* galloping mode. It's initialized
1187 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1188 * random data, and lower for highly structured data.
1189 */
1190 Py_ssize_t min_gallop;
1191
1192 /* 'a' is temp storage to help with merges. It contains room for
1193 * alloced entries.
1194 */
1195 sortslice a; /* may point to temparray below */
1196 Py_ssize_t alloced;
1197
1198 /* A stack of n pending runs yet to be merged. Run #i starts at
1199 * address base[i] and extends for len[i] elements. It's always
1200 * true (so long as the indices are in bounds) that
1201 *
1202 * pending[i].base + pending[i].len == pending[i+1].base
1203 *
1204 * so we could cut the storage for this, but it's a minor amount,
1205 * and keeping all the info explicit simplifies the code.
1206 */
1207 int n;
1208 struct s_slice pending[MAX_MERGE_PENDING];
1209
1210 /* 'a' points to this when possible, rather than muck with malloc. */
1211 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1212
1213 /* This is the function we will use to compare two keys,
1214 * even when none of our special cases apply and we have to use
1215 * safe_object_compare. */
1216 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1217
1218 /* This function is used by unsafe_object_compare to optimize comparisons
1219 * when we know our list is type-homogeneous but we can't assume anything else.
1220 * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1221 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1222
1223 /* This function is used by unsafe_tuple_compare to compare the first elements
1224 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1225 * we can assume more, and use one of the special-case compares. */
1226 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1227};
1228
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001229/* binarysort is the best method for sorting small arrays: it does
1230 few compares, but can do data movement quadratic in the number of
1231 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001232 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001233 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001234 On entry, must have lo <= start <= hi, and that [lo, start) is already
1235 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001236 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001237 Even in case of error, the output slice will be some permutation of
1238 the input (nothing is lost or duplicated).
1239*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001240static int
embg1e34da42018-01-28 20:03:23 -07001241binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001242{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001243 Py_ssize_t k;
1244 PyObject **l, **p, **r;
1245 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001246
Daniel Stutzbach98338222010-12-02 21:55:33 +00001247 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001249 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 ++start;
1251 for (; start < hi; ++start) {
1252 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001253 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 r = start;
1255 pivot = *r;
1256 /* Invariants:
1257 * pivot >= all in [lo, l).
1258 * pivot < all in [r, start).
1259 * The second is vacuously true at the start.
1260 */
1261 assert(l < r);
1262 do {
1263 p = l + ((r - l) >> 1);
1264 IFLT(pivot, *p)
1265 r = p;
1266 else
1267 l = p+1;
1268 } while (l < r);
1269 assert(l == r);
1270 /* The invariants still hold, so pivot >= all in [lo, l) and
1271 pivot < all in [l, start), so pivot belongs at l. Note
1272 that if there are elements equal to pivot, l points to the
1273 first slot after them -- that's why this sort is stable.
1274 Slide over to make room.
1275 Caution: using memmove is much slower under MSVC 5;
1276 we're not usually moving many slots. */
1277 for (p = start; p > l; --p)
1278 *p = *(p-1);
1279 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001280 if (lo.values != NULL) {
1281 Py_ssize_t offset = lo.values - lo.keys;
1282 p = start + offset;
1283 pivot = *p;
1284 l += offset;
1285 for (p = start + offset; p > l; --p)
1286 *p = *(p-1);
1287 *l = pivot;
1288 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 }
1290 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001291
1292 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001294}
1295
Tim Petersa64dc242002-08-01 02:13:36 +00001296/*
1297Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1298is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001299
Tim Petersa64dc242002-08-01 02:13:36 +00001300 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001301
Tim Petersa64dc242002-08-01 02:13:36 +00001302or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001303
Tim Petersa64dc242002-08-01 02:13:36 +00001304 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001305
Tim Petersa64dc242002-08-01 02:13:36 +00001306Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1307For its intended use in a stable mergesort, the strictness of the defn of
1308"descending" is needed so that the caller can safely reverse a descending
1309sequence without violating stability (strict > ensures there are no equal
1310elements to get out of order).
1311
1312Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001313*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001314static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001315count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 Py_ssize_t k;
1318 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 assert(lo < hi);
1321 *descending = 0;
1322 ++lo;
1323 if (lo == hi)
1324 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 n = 2;
1327 IFLT(*lo, *(lo-1)) {
1328 *descending = 1;
1329 for (lo = lo+1; lo < hi; ++lo, ++n) {
1330 IFLT(*lo, *(lo-1))
1331 ;
1332 else
1333 break;
1334 }
1335 }
1336 else {
1337 for (lo = lo+1; lo < hi; ++lo, ++n) {
1338 IFLT(*lo, *(lo-1))
1339 break;
1340 }
1341 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001344fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001346}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001347
Tim Petersa64dc242002-08-01 02:13:36 +00001348/*
1349Locate the proper position of key in a sorted vector; if the vector contains
1350an element equal to key, return the position immediately to the left of
1351the leftmost equal element. [gallop_right() does the same except returns
1352the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001353
Tim Petersa64dc242002-08-01 02:13:36 +00001354"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001355
Tim Petersa64dc242002-08-01 02:13:36 +00001356"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1357hint is to the final result, the faster this runs.
1358
1359The return value is the int k in 0..n such that
1360
1361 a[k-1] < key <= a[k]
1362
1363pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1364key belongs at index k; or, IOW, the first k elements of a should precede
1365key, and the last n-k should follow key.
1366
1367Returns -1 on error. See listsort.txt for info on the method.
1368*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001369static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001370gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 Py_ssize_t ofs;
1373 Py_ssize_t lastofs;
1374 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 a += hint;
1379 lastofs = 0;
1380 ofs = 1;
1381 IFLT(*a, key) {
1382 /* a[hint] < key -- gallop right, until
1383 * a[hint + lastofs] < key <= a[hint + ofs]
1384 */
1385 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1386 while (ofs < maxofs) {
1387 IFLT(a[ofs], key) {
1388 lastofs = ofs;
1389 ofs = (ofs << 1) + 1;
1390 if (ofs <= 0) /* int overflow */
1391 ofs = maxofs;
1392 }
1393 else /* key <= a[hint + ofs] */
1394 break;
1395 }
1396 if (ofs > maxofs)
1397 ofs = maxofs;
1398 /* Translate back to offsets relative to &a[0]. */
1399 lastofs += hint;
1400 ofs += hint;
1401 }
1402 else {
1403 /* key <= a[hint] -- gallop left, until
1404 * a[hint - ofs] < key <= a[hint - lastofs]
1405 */
1406 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1407 while (ofs < maxofs) {
1408 IFLT(*(a-ofs), key)
1409 break;
1410 /* key <= a[hint - ofs] */
1411 lastofs = ofs;
1412 ofs = (ofs << 1) + 1;
1413 if (ofs <= 0) /* int overflow */
1414 ofs = maxofs;
1415 }
1416 if (ofs > maxofs)
1417 ofs = maxofs;
1418 /* Translate back to positive offsets relative to &a[0]. */
1419 k = lastofs;
1420 lastofs = hint - ofs;
1421 ofs = hint - k;
1422 }
1423 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1426 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1427 * right of lastofs but no farther right than ofs. Do a binary
1428 * search, with invariant a[lastofs-1] < key <= a[ofs].
1429 */
1430 ++lastofs;
1431 while (lastofs < ofs) {
1432 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 IFLT(a[m], key)
1435 lastofs = m+1; /* a[m] < key */
1436 else
1437 ofs = m; /* key <= a[m] */
1438 }
1439 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1440 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001441
1442fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001444}
1445
1446/*
1447Exactly like gallop_left(), except that if key already exists in a[0:n],
1448finds the position immediately to the right of the rightmost equal value.
1449
1450The return value is the int k in 0..n such that
1451
1452 a[k-1] <= key < a[k]
1453
1454or -1 if error.
1455
1456The code duplication is massive, but this is enough different given that
1457we're sticking to "<" comparisons that it's much harder to follow if
1458written as one routine with yet another "left or right?" flag.
1459*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001460static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001461gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 Py_ssize_t ofs;
1464 Py_ssize_t lastofs;
1465 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 a += hint;
1470 lastofs = 0;
1471 ofs = 1;
1472 IFLT(key, *a) {
1473 /* key < a[hint] -- gallop left, until
1474 * a[hint - ofs] <= key < a[hint - lastofs]
1475 */
1476 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1477 while (ofs < maxofs) {
1478 IFLT(key, *(a-ofs)) {
1479 lastofs = ofs;
1480 ofs = (ofs << 1) + 1;
1481 if (ofs <= 0) /* int overflow */
1482 ofs = maxofs;
1483 }
1484 else /* a[hint - ofs] <= key */
1485 break;
1486 }
1487 if (ofs > maxofs)
1488 ofs = maxofs;
1489 /* Translate back to positive offsets relative to &a[0]. */
1490 k = lastofs;
1491 lastofs = hint - ofs;
1492 ofs = hint - k;
1493 }
1494 else {
1495 /* a[hint] <= key -- gallop right, until
1496 * a[hint + lastofs] <= key < a[hint + ofs]
1497 */
1498 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1499 while (ofs < maxofs) {
1500 IFLT(key, a[ofs])
1501 break;
1502 /* a[hint + ofs] <= key */
1503 lastofs = ofs;
1504 ofs = (ofs << 1) + 1;
1505 if (ofs <= 0) /* int overflow */
1506 ofs = maxofs;
1507 }
1508 if (ofs > maxofs)
1509 ofs = maxofs;
1510 /* Translate back to offsets relative to &a[0]. */
1511 lastofs += hint;
1512 ofs += hint;
1513 }
1514 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1517 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1518 * right of lastofs but no farther right than ofs. Do a binary
1519 * search, with invariant a[lastofs-1] <= key < a[ofs].
1520 */
1521 ++lastofs;
1522 while (lastofs < ofs) {
1523 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 IFLT(key, a[m])
1526 ofs = m; /* key < a[m] */
1527 else
1528 lastofs = m+1; /* a[m] <= key */
1529 }
1530 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1531 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001532
1533fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001535}
1536
Tim Petersa64dc242002-08-01 02:13:36 +00001537/* Conceptually a MergeState's constructor. */
1538static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001539merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001542 if (has_keyfunc) {
1543 /* The temporary space for merging will need at most half the list
1544 * size rounded up. Use the minimum possible space so we can use the
1545 * rest of temparray for other things. In particular, if there is
1546 * enough extra space, listsort() will use it to store the keys.
1547 */
1548 ms->alloced = (list_size + 1) / 2;
1549
1550 /* ms->alloced describes how many keys will be stored at
1551 ms->temparray, but we also need to store the values. Hence,
1552 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1553 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1554 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1555 ms->a.values = &ms->temparray[ms->alloced];
1556 }
1557 else {
1558 ms->alloced = MERGESTATE_TEMP_SIZE;
1559 ms->a.values = NULL;
1560 }
1561 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 ms->n = 0;
1563 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001564}
1565
1566/* Free all the temp memory owned by the MergeState. This must be called
1567 * when you're done with a MergeState, and may be called before then if
1568 * you want to free the temp memory early.
1569 */
1570static void
1571merge_freemem(MergeState *ms)
1572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001574 if (ms->a.keys != ms->temparray)
1575 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001576}
1577
1578/* Ensure enough temp memory for 'need' array slots is available.
1579 * Returns 0 on success and -1 if the memory can't be gotten.
1580 */
1581static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001582merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001583{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001584 int multiplier;
1585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 assert(ms != NULL);
1587 if (need <= ms->alloced)
1588 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001589
1590 multiplier = ms->a.values != NULL ? 2 : 1;
1591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 /* Don't realloc! That can cost cycles to copy the old data, but
1593 * we don't care what's in the block.
1594 */
1595 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001596 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 PyErr_NoMemory();
1598 return -1;
1599 }
embg1e34da42018-01-28 20:03:23 -07001600 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001601 * sizeof(PyObject *));
1602 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001604 if (ms->a.values != NULL)
1605 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 return 0;
1607 }
1608 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001610}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1612 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001613
Daniel Stutzbach98338222010-12-02 21:55:33 +00001614/* Merge the na elements starting at ssa with the nb elements starting at
1615 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1616 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1617 * should have na <= nb. See listsort.txt for more info. Return 0 if
1618 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001619 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001620static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001621merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1622 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001625 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 int result = -1; /* guilty until proved innocent */
1627 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001628
Daniel Stutzbach98338222010-12-02 21:55:33 +00001629 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1630 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 if (MERGE_GETMEM(ms, na) < 0)
1632 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001633 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1634 dest = ssa;
1635 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001636
Daniel Stutzbach98338222010-12-02 21:55:33 +00001637 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 --nb;
1639 if (nb == 0)
1640 goto Succeed;
1641 if (na == 1)
1642 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 min_gallop = ms->min_gallop;
1645 for (;;) {
1646 Py_ssize_t acount = 0; /* # of times A won in a row */
1647 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 /* Do the straightforward thing until (if ever) one run
1650 * appears to win consistently.
1651 */
1652 for (;;) {
1653 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001654 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 if (k) {
1656 if (k < 0)
1657 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001658 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 ++bcount;
1660 acount = 0;
1661 --nb;
1662 if (nb == 0)
1663 goto Succeed;
1664 if (bcount >= min_gallop)
1665 break;
1666 }
1667 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001668 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 ++acount;
1670 bcount = 0;
1671 --na;
1672 if (na == 1)
1673 goto CopyB;
1674 if (acount >= min_gallop)
1675 break;
1676 }
1677 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 /* One run is winning so consistently that galloping may
1680 * be a huge win. So try that, and continue galloping until
1681 * (if ever) neither run appears to be winning consistently
1682 * anymore.
1683 */
1684 ++min_gallop;
1685 do {
1686 assert(na > 1 && nb > 0);
1687 min_gallop -= min_gallop > 1;
1688 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001689 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 acount = k;
1691 if (k) {
1692 if (k < 0)
1693 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001694 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1695 sortslice_advance(&dest, k);
1696 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 na -= k;
1698 if (na == 1)
1699 goto CopyB;
1700 /* na==0 is impossible now if the comparison
1701 * function is consistent, but we can't assume
1702 * that it is.
1703 */
1704 if (na == 0)
1705 goto Succeed;
1706 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001707 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 --nb;
1709 if (nb == 0)
1710 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001711
embg1e34da42018-01-28 20:03:23 -07001712 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 bcount = k;
1714 if (k) {
1715 if (k < 0)
1716 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001717 sortslice_memmove(&dest, 0, &ssb, 0, k);
1718 sortslice_advance(&dest, k);
1719 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 nb -= k;
1721 if (nb == 0)
1722 goto Succeed;
1723 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001724 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 --na;
1726 if (na == 1)
1727 goto CopyB;
1728 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1729 ++min_gallop; /* penalize it for leaving galloping mode */
1730 ms->min_gallop = min_gallop;
1731 }
Tim Petersa64dc242002-08-01 02:13:36 +00001732Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001734Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001736 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001738CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001740 /* The last element of ssa belongs at the end of the merge. */
1741 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1742 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001744}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001745
Daniel Stutzbach98338222010-12-02 21:55:33 +00001746/* Merge the na elements starting at pa with the nb elements starting at
1747 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1748 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1749 * should have na >= nb. See listsort.txt for more info. Return 0 if
1750 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001751 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001752static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001753merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1754 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001757 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001760
Daniel Stutzbach98338222010-12-02 21:55:33 +00001761 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1762 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 if (MERGE_GETMEM(ms, nb) < 0)
1764 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001765 dest = ssb;
1766 sortslice_advance(&dest, nb-1);
1767 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1768 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001770 ssb.keys = ms->a.keys + nb - 1;
1771 if (ssb.values != NULL)
1772 ssb.values = ms->a.values + nb - 1;
1773 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001774
Daniel Stutzbach98338222010-12-02 21:55:33 +00001775 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 --na;
1777 if (na == 0)
1778 goto Succeed;
1779 if (nb == 1)
1780 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 min_gallop = ms->min_gallop;
1783 for (;;) {
1784 Py_ssize_t acount = 0; /* # of times A won in a row */
1785 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 /* Do the straightforward thing until (if ever) one run
1788 * appears to win consistently.
1789 */
1790 for (;;) {
1791 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001792 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 if (k) {
1794 if (k < 0)
1795 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001796 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 ++acount;
1798 bcount = 0;
1799 --na;
1800 if (na == 0)
1801 goto Succeed;
1802 if (acount >= min_gallop)
1803 break;
1804 }
1805 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001806 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 ++bcount;
1808 acount = 0;
1809 --nb;
1810 if (nb == 1)
1811 goto CopyA;
1812 if (bcount >= min_gallop)
1813 break;
1814 }
1815 }
Tim Petersa64dc242002-08-01 02:13:36 +00001816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 /* One run is winning so consistently that galloping may
1818 * be a huge win. So try that, and continue galloping until
1819 * (if ever) neither run appears to be winning consistently
1820 * anymore.
1821 */
1822 ++min_gallop;
1823 do {
1824 assert(na > 0 && nb > 1);
1825 min_gallop -= min_gallop > 1;
1826 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001827 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 if (k < 0)
1829 goto Fail;
1830 k = na - k;
1831 acount = k;
1832 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001833 sortslice_advance(&dest, -k);
1834 sortslice_advance(&ssa, -k);
1835 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 na -= k;
1837 if (na == 0)
1838 goto Succeed;
1839 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001840 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 --nb;
1842 if (nb == 1)
1843 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001844
embg1e34da42018-01-28 20:03:23 -07001845 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 if (k < 0)
1847 goto Fail;
1848 k = nb - k;
1849 bcount = k;
1850 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001851 sortslice_advance(&dest, -k);
1852 sortslice_advance(&ssb, -k);
1853 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 nb -= k;
1855 if (nb == 1)
1856 goto CopyA;
1857 /* nb==0 is impossible now if the comparison
1858 * function is consistent, but we can't assume
1859 * that it is.
1860 */
1861 if (nb == 0)
1862 goto Succeed;
1863 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001864 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 --na;
1866 if (na == 0)
1867 goto Succeed;
1868 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1869 ++min_gallop; /* penalize it for leaving galloping mode */
1870 ms->min_gallop = min_gallop;
1871 }
Tim Petersa64dc242002-08-01 02:13:36 +00001872Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001874Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001876 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001878CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001880 /* The first element of ssb belongs at the front of the merge. */
1881 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1882 sortslice_advance(&dest, -na);
1883 sortslice_advance(&ssa, -na);
1884 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001886}
1887
1888/* Merge the two runs at stack indices i and i+1.
1889 * Returns 0 on success, -1 on error.
1890 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001891static Py_ssize_t
1892merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001893{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001894 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 Py_ssize_t na, nb;
1896 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 assert(ms != NULL);
1899 assert(ms->n >= 2);
1900 assert(i >= 0);
1901 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001902
Daniel Stutzbach98338222010-12-02 21:55:33 +00001903 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001905 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 nb = ms->pending[i+1].len;
1907 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001908 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 /* Record the length of the combined runs; if i is the 3rd-last
1911 * run now, also slide over the last run (which isn't involved
1912 * in this merge). The current run i+1 goes away in any case.
1913 */
1914 ms->pending[i].len = na + nb;
1915 if (i == ms->n - 3)
1916 ms->pending[i+1] = ms->pending[i+2];
1917 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 /* Where does b start in a? Elements in a before that can be
1920 * ignored (already in place).
1921 */
embg1e34da42018-01-28 20:03:23 -07001922 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 if (k < 0)
1924 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001925 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 na -= k;
1927 if (na == 0)
1928 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 /* Where does a end in b? Elements in b after that can be
1931 * ignored (already in place).
1932 */
embg1e34da42018-01-28 20:03:23 -07001933 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 if (nb <= 0)
1935 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 /* Merge what remains of the runs, using a temp array with
1938 * min(na, nb) elements.
1939 */
1940 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001941 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001943 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001944}
1945
1946/* Examine the stack of runs waiting to be merged, merging adjacent runs
1947 * until the stack invariants are re-established:
1948 *
1949 * 1. len[-3] > len[-2] + len[-1]
1950 * 2. len[-2] > len[-1]
1951 *
1952 * See listsort.txt for more info.
1953 *
1954 * Returns 0 on success, -1 on error.
1955 */
1956static int
1957merge_collapse(MergeState *ms)
1958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 assert(ms);
1962 while (ms->n > 1) {
1963 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001964 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1965 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 if (p[n-1].len < p[n+1].len)
1967 --n;
1968 if (merge_at(ms, n) < 0)
1969 return -1;
1970 }
1971 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001972 if (merge_at(ms, n) < 0)
1973 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 }
1975 else
1976 break;
1977 }
1978 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001979}
1980
1981/* Regardless of invariants, merge all runs on the stack until only one
1982 * remains. This is used at the end of the mergesort.
1983 *
1984 * Returns 0 on success, -1 on error.
1985 */
1986static int
1987merge_force_collapse(MergeState *ms)
1988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 assert(ms);
1992 while (ms->n > 1) {
1993 Py_ssize_t n = ms->n - 2;
1994 if (n > 0 && p[n-1].len < p[n+1].len)
1995 --n;
1996 if (merge_at(ms, n) < 0)
1997 return -1;
1998 }
1999 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00002000}
2001
2002/* Compute a good value for the minimum run length; natural runs shorter
2003 * than this are boosted artificially via binary insertion.
2004 *
2005 * If n < 64, return n (it's too small to bother with fancy stuff).
2006 * Else if n is an exact power of 2, return 32.
2007 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
2008 * strictly less than, an exact power of 2.
2009 *
2010 * See listsort.txt for more info.
2011 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002012static Py_ssize_t
2013merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00002014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00002016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 assert(n >= 0);
2018 while (n >= 64) {
2019 r |= n & 1;
2020 n >>= 1;
2021 }
2022 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002023}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00002024
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002025static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00002026reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002027{
Daniel Stutzbach98338222010-12-02 21:55:33 +00002028 reverse_slice(s->keys, &s->keys[n]);
2029 if (s->values != NULL)
2030 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002031}
2032
embg1e34da42018-01-28 20:03:23 -07002033/* Here we define custom comparison functions to optimize for the cases one commonly
2034 * encounters in practice: homogeneous lists, often of one of the basic types. */
2035
2036/* This struct holds the comparison function and helper functions
2037 * selected in the pre-sort check. */
2038
2039/* These are the special case compare functions.
2040 * ms->key_compare will always point to one of these: */
2041
2042/* Heterogeneous compare: default, always safe to fall back on. */
2043static int
2044safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2045{
2046 /* No assumptions necessary! */
2047 return PyObject_RichCompareBool(v, w, Py_LT);
2048}
2049
2050/* Homogeneous compare: safe for any two compareable objects of the same type.
2051 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2052 * pre-sort check.)
2053 */
2054static int
2055unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2056{
2057 PyObject *res_obj; int res;
2058
2059 /* No assumptions, because we check first: */
2060 if (v->ob_type->tp_richcompare != ms->key_richcompare)
2061 return PyObject_RichCompareBool(v, w, Py_LT);
2062
2063 assert(ms->key_richcompare != NULL);
2064 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2065
2066 if (res_obj == Py_NotImplemented) {
2067 Py_DECREF(res_obj);
2068 return PyObject_RichCompareBool(v, w, Py_LT);
2069 }
2070 if (res_obj == NULL)
2071 return -1;
2072
2073 if (PyBool_Check(res_obj)) {
2074 res = (res_obj == Py_True);
2075 }
2076 else {
2077 res = PyObject_IsTrue(res_obj);
2078 }
2079 Py_DECREF(res_obj);
2080
2081 /* Note that we can't assert
2082 * res == PyObject_RichCompareBool(v, w, Py_LT);
2083 * because of evil compare functions like this:
2084 * lambda a, b: int(random.random() * 3) - 1)
2085 * (which is actually in test_sort.py) */
2086 return res;
2087}
2088
2089/* Latin string compare: safe for any two latin (one byte per char) strings. */
2090static int
2091unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2092{
Victor Stinner8017b802018-01-29 13:47:06 +01002093 Py_ssize_t len;
2094 int res;
embg1e34da42018-01-28 20:03:23 -07002095
2096 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2097 assert(v->ob_type == w->ob_type);
2098 assert(v->ob_type == &PyUnicode_Type);
2099 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2100 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2101
2102 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2103 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2104
2105 res = (res != 0 ?
2106 res < 0 :
2107 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2108
2109 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2110 return res;
2111}
2112
2113/* Bounded int compare: compare any two longs that fit in a single machine word. */
2114static int
2115unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2116{
2117 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2118
2119 /* Modified from Objects/longobject.c:long_compare, assuming: */
2120 assert(v->ob_type == w->ob_type);
2121 assert(v->ob_type == &PyLong_Type);
2122 assert(Py_ABS(Py_SIZE(v)) <= 1);
2123 assert(Py_ABS(Py_SIZE(w)) <= 1);
2124
2125 vl = (PyLongObject*)v;
2126 wl = (PyLongObject*)w;
2127
2128 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2129 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2130
2131 if (Py_SIZE(vl) < 0)
2132 v0 = -v0;
2133 if (Py_SIZE(wl) < 0)
2134 w0 = -w0;
2135
2136 res = v0 < w0;
2137 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2138 return res;
2139}
2140
2141/* Float compare: compare any two floats. */
2142static int
2143unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2144{
2145 int res;
2146
2147 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2148 assert(v->ob_type == w->ob_type);
2149 assert(v->ob_type == &PyFloat_Type);
2150
2151 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2152 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2153 return res;
2154}
2155
2156/* Tuple compare: compare *any* two tuples, using
2157 * ms->tuple_elem_compare to compare the first elements, which is set
2158 * using the same pre-sort check as we use for ms->key_compare,
2159 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2160 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2161 * that most tuple compares don't involve x[1:]. */
2162static int
2163unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2164{
2165 PyTupleObject *vt, *wt;
2166 Py_ssize_t i, vlen, wlen;
2167 int k;
2168
2169 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2170 assert(v->ob_type == w->ob_type);
2171 assert(v->ob_type == &PyTuple_Type);
2172 assert(Py_SIZE(v) > 0);
2173 assert(Py_SIZE(w) > 0);
2174
2175 vt = (PyTupleObject *)v;
2176 wt = (PyTupleObject *)w;
2177
2178 vlen = Py_SIZE(vt);
2179 wlen = Py_SIZE(wt);
2180
2181 for (i = 0; i < vlen && i < wlen; i++) {
2182 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2183 if (k < 0)
2184 return -1;
2185 if (!k)
2186 break;
2187 }
2188
2189 if (i >= vlen || i >= wlen)
2190 return vlen < wlen;
2191
2192 if (i == 0)
2193 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2194 else
2195 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2196}
2197
Tim Petersa64dc242002-08-01 02:13:36 +00002198/* An adaptive, stable, natural mergesort. See listsort.txt.
2199 * Returns Py_None on success, NULL on error. Even in case of error, the
2200 * list will be some permutation of its input state (nothing is lost or
2201 * duplicated).
2202 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002203/*[clinic input]
2204list.sort
2205
2206 *
2207 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002208 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002209
2210Stable sort *IN PLACE*.
2211[clinic start generated code]*/
2212
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002213static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002214list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002215/*[clinic end generated code: output=57b9f9c5e23fbe42 input=b0fcf743982c5b90]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 Py_ssize_t nremaining;
2219 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002220 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 Py_ssize_t saved_ob_size, saved_allocated;
2222 PyObject **saved_ob_item;
2223 PyObject **final_ob_item;
2224 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002226 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002229 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 if (keyfunc == Py_None)
2231 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 /* The list is temporarily made empty, so that mutations performed
2234 * by comparison functions can't affect the slice of memory we're
2235 * sorting (allowing mutations during sorting is a core-dump
2236 * factory, since ob_item may change).
2237 */
2238 saved_ob_size = Py_SIZE(self);
2239 saved_ob_item = self->ob_item;
2240 saved_allocated = self->allocated;
2241 Py_SIZE(self) = 0;
2242 self->ob_item = NULL;
2243 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002244
Daniel Stutzbach98338222010-12-02 21:55:33 +00002245 if (keyfunc == NULL) {
2246 keys = NULL;
2247 lo.keys = saved_ob_item;
2248 lo.values = NULL;
2249 }
2250 else {
2251 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2252 /* Leverage stack space we allocated but won't otherwise use */
2253 keys = &ms.temparray[saved_ob_size+1];
2254 else {
2255 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002256 if (keys == NULL) {
2257 PyErr_NoMemory();
2258 goto keyfunc_fail;
2259 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002261
2262 for (i = 0; i < saved_ob_size ; i++) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002263 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
2264 NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002265 if (keys[i] == NULL) {
2266 for (i=i-1 ; i>=0 ; i--)
2267 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002268 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002269 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002270 goto keyfunc_fail;
2271 }
2272 }
2273
2274 lo.keys = keys;
2275 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002277
embg1e34da42018-01-28 20:03:23 -07002278
2279 /* The pre-sort check: here's where we decide which compare function to use.
2280 * How much optimization is safe? We test for homogeneity with respect to
2281 * several properties that are expensive to check at compare-time, and
2282 * set ms appropriately. */
2283 if (saved_ob_size > 1) {
2284 /* Assume the first element is representative of the whole list. */
2285 int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2286 Py_SIZE(lo.keys[0]) > 0);
2287
2288 PyTypeObject* key_type = (keys_are_in_tuples ?
2289 PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2290 lo.keys[0]->ob_type);
2291
2292 int keys_are_all_same_type = 1;
2293 int strings_are_latin = 1;
2294 int ints_are_bounded = 1;
2295
2296 /* Prove that assumption by checking every key. */
2297 int i;
2298 for (i=0; i < saved_ob_size; i++) {
2299
2300 if (keys_are_in_tuples &&
2301 !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2302 keys_are_in_tuples = 0;
2303 keys_are_all_same_type = 0;
2304 break;
2305 }
2306
2307 /* Note: for lists of tuples, key is the first element of the tuple
2308 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2309 * for lists of tuples in the if-statement directly above. */
2310 PyObject *key = (keys_are_in_tuples ?
2311 PyTuple_GET_ITEM(lo.keys[i], 0) :
2312 lo.keys[i]);
2313
2314 if (key->ob_type != key_type) {
2315 keys_are_all_same_type = 0;
2316 break;
2317 }
2318
2319 if (key_type == &PyLong_Type) {
2320 if (ints_are_bounded && Py_ABS(Py_SIZE(key)) > 1)
2321 ints_are_bounded = 0;
2322 }
2323 else if (key_type == &PyUnicode_Type){
2324 if (strings_are_latin &&
2325 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND)
2326 strings_are_latin = 0;
2327 }
2328 }
2329
2330 /* Choose the best compare, given what we now know about the keys. */
2331 if (keys_are_all_same_type) {
2332
2333 if (key_type == &PyUnicode_Type && strings_are_latin) {
2334 ms.key_compare = unsafe_latin_compare;
2335 }
2336 else if (key_type == &PyLong_Type && ints_are_bounded) {
2337 ms.key_compare = unsafe_long_compare;
2338 }
2339 else if (key_type == &PyFloat_Type) {
2340 ms.key_compare = unsafe_float_compare;
2341 }
2342 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2343 ms.key_compare = unsafe_object_compare;
2344 }
2345 }
2346 else {
2347 ms.key_compare = safe_object_compare;
2348 }
2349
2350 if (keys_are_in_tuples) {
2351 /* Make sure we're not dealing with tuples of tuples
2352 * (remember: here, key_type refers list [key[0] for key in keys]) */
2353 if (key_type == &PyTuple_Type)
2354 ms.tuple_elem_compare = safe_object_compare;
2355 else
2356 ms.tuple_elem_compare = ms.key_compare;
2357
2358 ms.key_compare = unsafe_tuple_compare;
2359 }
2360 }
2361 /* End of pre-sort check: ms is now set properly! */
2362
Daniel Stutzbach98338222010-12-02 21:55:33 +00002363 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 nremaining = saved_ob_size;
2366 if (nremaining < 2)
2367 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002368
Benjamin Peterson05380642010-08-23 19:35:39 +00002369 /* Reverse sort stability achieved by initially reversing the list,
2370 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002371 if (reverse) {
2372 if (keys != NULL)
2373 reverse_slice(&keys[0], &keys[saved_ob_size]);
2374 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2375 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 /* March over the array once, left to right, finding natural runs,
2378 * and extending short natural runs to minrun elements.
2379 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 minrun = merge_compute_minrun(nremaining);
2381 do {
2382 int descending;
2383 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002386 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 if (n < 0)
2388 goto fail;
2389 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002390 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 /* If short, extend to min(minrun, nremaining). */
2392 if (n < minrun) {
2393 const Py_ssize_t force = nremaining <= minrun ?
2394 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002395 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 goto fail;
2397 n = force;
2398 }
2399 /* Push run onto pending-runs stack, and maybe merge. */
2400 assert(ms.n < MAX_MERGE_PENDING);
2401 ms.pending[ms.n].base = lo;
2402 ms.pending[ms.n].len = n;
2403 ++ms.n;
2404 if (merge_collapse(&ms) < 0)
2405 goto fail;
2406 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002407 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 nremaining -= n;
2409 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 if (merge_force_collapse(&ms) < 0)
2412 goto fail;
2413 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002414 assert(keys == NULL
2415 ? ms.pending[0].base.keys == saved_ob_item
2416 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002418 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002419
2420succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002422fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002423 if (keys != NULL) {
2424 for (i = 0; i < saved_ob_size; i++)
2425 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002426 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002427 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 if (self->allocated != -1 && result != NULL) {
2431 /* The user mucked with the list during the sort,
2432 * and we don't already have another error to report.
2433 */
2434 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2435 result = NULL;
2436 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 if (reverse && saved_ob_size > 1)
2439 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002442
Daniel Stutzbach98338222010-12-02 21:55:33 +00002443keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 final_ob_item = self->ob_item;
2445 i = Py_SIZE(self);
2446 Py_SIZE(self) = saved_ob_size;
2447 self->ob_item = saved_ob_item;
2448 self->allocated = saved_allocated;
2449 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002450 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 guarantee that the list is really empty when it returns */
2452 while (--i >= 0) {
2453 Py_XDECREF(final_ob_item[i]);
2454 }
2455 PyMem_FREE(final_ob_item);
2456 }
2457 Py_XINCREF(result);
2458 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002459}
Tim Peters330f9e92002-07-19 07:05:44 +00002460#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002461#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002462
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002463int
Fred Drakea2f55112000-07-09 15:16:51 +00002464PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 if (v == NULL || !PyList_Check(v)) {
2467 PyErr_BadInternalCall();
2468 return -1;
2469 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002470 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 if (v == NULL)
2472 return -1;
2473 Py_DECREF(v);
2474 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002475}
2476
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002477/*[clinic input]
2478list.reverse
2479
2480Reverse *IN PLACE*.
2481[clinic start generated code]*/
2482
Guido van Rossumb86c5492001-02-12 22:06:02 +00002483static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002484list_reverse_impl(PyListObject *self)
2485/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 if (Py_SIZE(self) > 1)
2488 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2489 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002490}
2491
Guido van Rossum84c76f51990-10-30 13:32:20 +00002492int
Fred Drakea2f55112000-07-09 15:16:51 +00002493PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 if (v == NULL || !PyList_Check(v)) {
2498 PyErr_BadInternalCall();
2499 return -1;
2500 }
2501 if (Py_SIZE(self) > 1)
2502 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2503 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002504}
2505
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002506PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002507PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 PyObject *w;
2510 PyObject **p, **q;
2511 Py_ssize_t n;
2512 if (v == NULL || !PyList_Check(v)) {
2513 PyErr_BadInternalCall();
2514 return NULL;
2515 }
2516 n = Py_SIZE(v);
2517 w = PyTuple_New(n);
2518 if (w == NULL)
2519 return NULL;
2520 p = ((PyTupleObject *)w)->ob_item;
2521 q = ((PyListObject *)v)->ob_item;
2522 while (--n >= 0) {
2523 Py_INCREF(*q);
2524 *p = *q;
2525 p++;
2526 q++;
2527 }
2528 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002529}
2530
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002531/*[clinic input]
2532list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002533
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002534 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002535 start: slice_index(accept={int}) = 0
2536 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002537 /
2538
2539Return first index of value.
2540
2541Raises ValueError if the value is not present.
2542[clinic start generated code]*/
2543
2544static PyObject *
2545list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2546 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002547/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002548{
2549 Py_ssize_t i;
2550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 if (start < 0) {
2552 start += Py_SIZE(self);
2553 if (start < 0)
2554 start = 0;
2555 }
2556 if (stop < 0) {
2557 stop += Py_SIZE(self);
2558 if (stop < 0)
2559 stop = 0;
2560 }
2561 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002562 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 if (cmp > 0)
2564 return PyLong_FromSsize_t(i);
2565 else if (cmp < 0)
2566 return NULL;
2567 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002568 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002570}
2571
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002572/*[clinic input]
2573list.count
2574
2575 value: object
2576 /
2577
2578Return number of occurrences of value.
2579[clinic start generated code]*/
2580
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002581static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002582list_count(PyListObject *self, PyObject *value)
2583/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 Py_ssize_t count = 0;
2586 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002589 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 if (cmp > 0)
2591 count++;
2592 else if (cmp < 0)
2593 return NULL;
2594 }
2595 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002596}
2597
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002598/*[clinic input]
2599list.remove
2600
2601 value: object
2602 /
2603
2604Remove first occurrence of value.
2605
2606Raises ValueError if the value is not present.
2607[clinic start generated code]*/
2608
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002609static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002610list_remove(PyListObject *self, PyObject *value)
2611/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002616 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 if (cmp > 0) {
2618 if (list_ass_slice(self, i, i+1,
2619 (PyObject *)NULL) == 0)
2620 Py_RETURN_NONE;
2621 return NULL;
2622 }
2623 else if (cmp < 0)
2624 return NULL;
2625 }
2626 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2627 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002628}
2629
Jeremy Hylton8caad492000-06-23 14:18:11 +00002630static int
2631list_traverse(PyListObject *o, visitproc visit, void *arg)
2632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 for (i = Py_SIZE(o); --i >= 0; )
2636 Py_VISIT(o->ob_item[i]);
2637 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002638}
2639
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002640static PyObject *
2641list_richcompare(PyObject *v, PyObject *w, int op)
2642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 PyListObject *vl, *wl;
2644 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002645
Brian Curtindfc80e32011-08-10 20:28:54 -05002646 if (!PyList_Check(v) || !PyList_Check(w))
2647 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 vl = (PyListObject *)v;
2650 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2653 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002655 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 else
stratakise8b19652017-11-02 11:32:54 +01002657 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 /* Search for the first index where items are different */
2661 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2662 int k = PyObject_RichCompareBool(vl->ob_item[i],
2663 wl->ob_item[i], Py_EQ);
2664 if (k < 0)
2665 return NULL;
2666 if (!k)
2667 break;
2668 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2671 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002672 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 /* We have an item that differs -- shortcuts for EQ/NE */
2676 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002677 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 }
2679 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002680 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 /* Compare the final item again using the proper operator */
2684 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002685}
2686
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002687/*[clinic input]
2688list.__init__
2689
2690 iterable: object(c_default="NULL") = ()
2691 /
2692
2693Built-in mutable sequence.
2694
2695If no argument is given, the constructor creates a new empty list.
2696The argument must be an iterable if specified.
2697[clinic start generated code]*/
2698
Tim Peters6d6c1a32001-08-02 04:15:00 +00002699static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002700list___init___impl(PyListObject *self, PyObject *iterable)
2701/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 /* Verify list invariants established by PyType_GenericAlloc() */
2704 assert(0 <= Py_SIZE(self));
2705 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2706 assert(self->ob_item != NULL ||
2707 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 /* Empty previous contents */
2710 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002711 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002713 if (iterable != NULL) {
Pablo Galindo372d7052018-10-28 20:16:26 +00002714 if (_PyObject_HasLen(iterable)) {
2715 Py_ssize_t iter_len = PyObject_Size(iterable);
2716 if (iter_len == -1) {
2717 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2718 return -1;
2719 }
2720 PyErr_Clear();
2721 }
2722 if (iter_len > 0 && self->ob_item == NULL
2723 && list_preallocate_exact(self, iter_len)) {
2724 return -1;
2725 }
2726 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002727 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 if (rv == NULL)
2729 return -1;
2730 Py_DECREF(rv);
2731 }
2732 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002733}
2734
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002735/*[clinic input]
2736list.__sizeof__
2737
2738Return the size of the list in memory, in bytes.
2739[clinic start generated code]*/
2740
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002741static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002742list___sizeof___impl(PyListObject *self)
2743/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002746
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002747 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002749}
2750
Raymond Hettinger1021c442003-11-07 15:38:09 +00002751static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002752static PyObject *list_subscript(PyListObject*, PyObject*);
2753
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002754static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002755 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2756 LIST___REVERSED___METHODDEF
2757 LIST___SIZEOF___METHODDEF
2758 LIST_CLEAR_METHODDEF
2759 LIST_COPY_METHODDEF
2760 LIST_APPEND_METHODDEF
2761 LIST_INSERT_METHODDEF
2762 LIST_EXTEND_METHODDEF
2763 LIST_POP_METHODDEF
2764 LIST_REMOVE_METHODDEF
2765 LIST_INDEX_METHODDEF
2766 LIST_COUNT_METHODDEF
2767 LIST_REVERSE_METHODDEF
2768 LIST_SORT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002770};
2771
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002772static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 (lenfunc)list_length, /* sq_length */
2774 (binaryfunc)list_concat, /* sq_concat */
2775 (ssizeargfunc)list_repeat, /* sq_repeat */
2776 (ssizeargfunc)list_item, /* sq_item */
2777 0, /* sq_slice */
2778 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2779 0, /* sq_ass_slice */
2780 (objobjproc)list_contains, /* sq_contains */
2781 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2782 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002783};
2784
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002785static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002786list_subscript(PyListObject* self, PyObject* item)
2787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 if (PyIndex_Check(item)) {
2789 Py_ssize_t i;
2790 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2791 if (i == -1 && PyErr_Occurred())
2792 return NULL;
2793 if (i < 0)
2794 i += PyList_GET_SIZE(self);
2795 return list_item(self, i);
2796 }
2797 else if (PySlice_Check(item)) {
2798 Py_ssize_t start, stop, step, slicelength, cur, i;
2799 PyObject* result;
2800 PyObject* it;
2801 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002802
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002803 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 return NULL;
2805 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002806 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2807 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 if (slicelength <= 0) {
2810 return PyList_New(0);
2811 }
2812 else if (step == 1) {
2813 return list_slice(self, start, stop);
2814 }
2815 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002816 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 src = self->ob_item;
2820 dest = ((PyListObject *)result)->ob_item;
2821 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002822 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 it = src[cur];
2824 Py_INCREF(it);
2825 dest[i] = it;
2826 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002827 Py_SIZE(result) = slicelength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 return result;
2829 }
2830 }
2831 else {
2832 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002833 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 item->ob_type->tp_name);
2835 return NULL;
2836 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002837}
2838
Tim Peters3b01a122002-07-19 02:35:45 +00002839static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002840list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 if (PyIndex_Check(item)) {
2843 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2844 if (i == -1 && PyErr_Occurred())
2845 return -1;
2846 if (i < 0)
2847 i += PyList_GET_SIZE(self);
2848 return list_ass_item(self, i, value);
2849 }
2850 else if (PySlice_Check(item)) {
2851 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002852
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002853 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 return -1;
2855 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002856 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2857 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 if (step == 1)
2860 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 /* Make sure s[5:2] = [..] inserts at the right place:
2863 before 5, not before 2. */
2864 if ((step < 0 && start < stop) ||
2865 (step > 0 && start > stop))
2866 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 if (value == NULL) {
2869 /* delete slice */
2870 PyObject **garbage;
2871 size_t cur;
2872 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002873 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 if (slicelength <= 0)
2876 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 if (step < 0) {
2879 stop = start + 1;
2880 start = stop + step*(slicelength - 1) - 1;
2881 step = -step;
2882 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 garbage = (PyObject**)
2885 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2886 if (!garbage) {
2887 PyErr_NoMemory();
2888 return -1;
2889 }
Tim Peters3b01a122002-07-19 02:35:45 +00002890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 /* drawing pictures might help understand these for
2892 loops. Basically, we memmove the parts of the
2893 list that are *not* part of the slice: step-1
2894 items for each item that is part of the slice,
2895 and then tail end of the list that was not
2896 covered by the slice */
2897 for (cur = start, i = 0;
2898 cur < (size_t)stop;
2899 cur += step, i++) {
2900 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 if (cur + step >= (size_t)Py_SIZE(self)) {
2905 lim = Py_SIZE(self) - cur - 1;
2906 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 memmove(self->ob_item + cur - i,
2909 self->ob_item + cur + 1,
2910 lim * sizeof(PyObject *));
2911 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002912 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 if (cur < (size_t)Py_SIZE(self)) {
2914 memmove(self->ob_item + cur - slicelength,
2915 self->ob_item + cur,
2916 (Py_SIZE(self) - cur) *
2917 sizeof(PyObject *));
2918 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002921 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 for (i = 0; i < slicelength; i++) {
2924 Py_DECREF(garbage[i]);
2925 }
2926 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002927
Victor Stinner35f28032013-11-21 12:16:35 +01002928 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 }
2930 else {
2931 /* assign slice */
2932 PyObject *ins, *seq;
2933 PyObject **garbage, **seqitems, **selfitems;
2934 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 /* protect against a[::-1] = a */
2937 if (self == (PyListObject*)value) {
2938 seq = list_slice((PyListObject*)value, 0,
2939 PyList_GET_SIZE(value));
2940 }
2941 else {
2942 seq = PySequence_Fast(value,
2943 "must assign iterable "
2944 "to extended slice");
2945 }
2946 if (!seq)
2947 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2950 PyErr_Format(PyExc_ValueError,
2951 "attempt to assign sequence of "
2952 "size %zd to extended slice of "
2953 "size %zd",
2954 PySequence_Fast_GET_SIZE(seq),
2955 slicelength);
2956 Py_DECREF(seq);
2957 return -1;
2958 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 if (!slicelength) {
2961 Py_DECREF(seq);
2962 return 0;
2963 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002965 garbage = (PyObject**)
2966 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2967 if (!garbage) {
2968 Py_DECREF(seq);
2969 PyErr_NoMemory();
2970 return -1;
2971 }
Tim Peters3b01a122002-07-19 02:35:45 +00002972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 selfitems = self->ob_item;
2974 seqitems = PySequence_Fast_ITEMS(seq);
2975 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002976 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 garbage[i] = selfitems[cur];
2978 ins = seqitems[i];
2979 Py_INCREF(ins);
2980 selfitems[cur] = ins;
2981 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 for (i = 0; i < slicelength; i++) {
2984 Py_DECREF(garbage[i]);
2985 }
Tim Peters3b01a122002-07-19 02:35:45 +00002986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 PyMem_FREE(garbage);
2988 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 return 0;
2991 }
2992 }
2993 else {
2994 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002995 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 item->ob_type->tp_name);
2997 return -1;
2998 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002999}
3000
3001static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 (lenfunc)list_length,
3003 (binaryfunc)list_subscript,
3004 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003005};
3006
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003007PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3009 "list",
3010 sizeof(PyListObject),
3011 0,
3012 (destructor)list_dealloc, /* tp_dealloc */
3013 0, /* tp_print */
3014 0, /* tp_getattr */
3015 0, /* tp_setattr */
3016 0, /* tp_reserved */
3017 (reprfunc)list_repr, /* tp_repr */
3018 0, /* tp_as_number */
3019 &list_as_sequence, /* tp_as_sequence */
3020 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003021 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 0, /* tp_call */
3023 0, /* tp_str */
3024 PyObject_GenericGetAttr, /* tp_getattro */
3025 0, /* tp_setattro */
3026 0, /* tp_as_buffer */
3027 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003028 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
3029 list___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 (traverseproc)list_traverse, /* tp_traverse */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003031 (inquiry)_list_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 list_richcompare, /* tp_richcompare */
3033 0, /* tp_weaklistoffset */
3034 list_iter, /* tp_iter */
3035 0, /* tp_iternext */
3036 list_methods, /* tp_methods */
3037 0, /* tp_members */
3038 0, /* tp_getset */
3039 0, /* tp_base */
3040 0, /* tp_dict */
3041 0, /* tp_descr_get */
3042 0, /* tp_descr_set */
3043 0, /* tp_dictoffset */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003044 (initproc)list___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 PyType_GenericAlloc, /* tp_alloc */
3046 PyType_GenericNew, /* tp_new */
3047 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003048};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00003049
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003050/*********************** List Iterator **************************/
3051
3052typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02003054 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003056} listiterobject;
3057
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003058static void listiter_dealloc(listiterobject *);
3059static int listiter_traverse(listiterobject *, visitproc, void *);
3060static PyObject *listiter_next(listiterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303061static PyObject *listiter_len(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003062static PyObject *listiter_reduce_general(void *_it, int forward);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303063static PyObject *listiter_reduce(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003064static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00003065
Armin Rigof5b3e362006-02-11 21:32:43 +00003066PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003067PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3068PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003069
3070static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003072 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3073 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00003075};
3076
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003077PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3079 "list_iterator", /* tp_name */
3080 sizeof(listiterobject), /* tp_basicsize */
3081 0, /* tp_itemsize */
3082 /* methods */
3083 (destructor)listiter_dealloc, /* tp_dealloc */
3084 0, /* tp_print */
3085 0, /* tp_getattr */
3086 0, /* tp_setattr */
3087 0, /* tp_reserved */
3088 0, /* tp_repr */
3089 0, /* tp_as_number */
3090 0, /* tp_as_sequence */
3091 0, /* tp_as_mapping */
3092 0, /* tp_hash */
3093 0, /* tp_call */
3094 0, /* tp_str */
3095 PyObject_GenericGetAttr, /* tp_getattro */
3096 0, /* tp_setattro */
3097 0, /* tp_as_buffer */
3098 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3099 0, /* tp_doc */
3100 (traverseproc)listiter_traverse, /* tp_traverse */
3101 0, /* tp_clear */
3102 0, /* tp_richcompare */
3103 0, /* tp_weaklistoffset */
3104 PyObject_SelfIter, /* tp_iter */
3105 (iternextfunc)listiter_next, /* tp_iternext */
3106 listiter_methods, /* tp_methods */
3107 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003108};
Raymond Hettinger1021c442003-11-07 15:38:09 +00003109
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003110
3111static PyObject *
3112list_iter(PyObject *seq)
3113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 if (!PyList_Check(seq)) {
3117 PyErr_BadInternalCall();
3118 return NULL;
3119 }
3120 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3121 if (it == NULL)
3122 return NULL;
3123 it->it_index = 0;
3124 Py_INCREF(seq);
3125 it->it_seq = (PyListObject *)seq;
3126 _PyObject_GC_TRACK(it);
3127 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003128}
3129
3130static void
3131listiter_dealloc(listiterobject *it)
3132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 _PyObject_GC_UNTRACK(it);
3134 Py_XDECREF(it->it_seq);
3135 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003136}
3137
3138static int
3139listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 Py_VISIT(it->it_seq);
3142 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003143}
3144
3145static PyObject *
3146listiter_next(listiterobject *it)
3147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 PyListObject *seq;
3149 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 assert(it != NULL);
3152 seq = it->it_seq;
3153 if (seq == NULL)
3154 return NULL;
3155 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 if (it->it_index < PyList_GET_SIZE(seq)) {
3158 item = PyList_GET_ITEM(seq, it->it_index);
3159 ++it->it_index;
3160 Py_INCREF(item);
3161 return item;
3162 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003165 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003167}
3168
3169static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303170listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 Py_ssize_t len;
3173 if (it->it_seq) {
3174 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3175 if (len >= 0)
3176 return PyLong_FromSsize_t(len);
3177 }
3178 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003179}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003180
3181static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303182listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003183{
3184 return listiter_reduce_general(it, 1);
3185}
3186
3187static PyObject *
3188listiter_setstate(listiterobject *it, PyObject *state)
3189{
Victor Stinner7660b882013-06-24 23:59:24 +02003190 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003191 if (index == -1 && PyErr_Occurred())
3192 return NULL;
3193 if (it->it_seq != NULL) {
3194 if (index < 0)
3195 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003196 else if (index > PyList_GET_SIZE(it->it_seq))
3197 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003198 it->it_index = index;
3199 }
3200 Py_RETURN_NONE;
3201}
3202
Raymond Hettinger1021c442003-11-07 15:38:09 +00003203/*********************** List Reverse Iterator **************************/
3204
3205typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 PyObject_HEAD
3207 Py_ssize_t it_index;
3208 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00003209} listreviterobject;
3210
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003211static void listreviter_dealloc(listreviterobject *);
3212static int listreviter_traverse(listreviterobject *, visitproc, void *);
3213static PyObject *listreviter_next(listreviterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303214static PyObject *listreviter_len(listreviterobject *, PyObject *);
3215static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003216static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003217
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003218static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003220 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3221 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003223};
3224
Raymond Hettinger1021c442003-11-07 15:38:09 +00003225PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3227 "list_reverseiterator", /* tp_name */
3228 sizeof(listreviterobject), /* tp_basicsize */
3229 0, /* tp_itemsize */
3230 /* methods */
3231 (destructor)listreviter_dealloc, /* tp_dealloc */
3232 0, /* tp_print */
3233 0, /* tp_getattr */
3234 0, /* tp_setattr */
3235 0, /* tp_reserved */
3236 0, /* tp_repr */
3237 0, /* tp_as_number */
3238 0, /* tp_as_sequence */
3239 0, /* tp_as_mapping */
3240 0, /* tp_hash */
3241 0, /* tp_call */
3242 0, /* tp_str */
3243 PyObject_GenericGetAttr, /* tp_getattro */
3244 0, /* tp_setattro */
3245 0, /* tp_as_buffer */
3246 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3247 0, /* tp_doc */
3248 (traverseproc)listreviter_traverse, /* tp_traverse */
3249 0, /* tp_clear */
3250 0, /* tp_richcompare */
3251 0, /* tp_weaklistoffset */
3252 PyObject_SelfIter, /* tp_iter */
3253 (iternextfunc)listreviter_next, /* tp_iternext */
3254 listreviter_methods, /* tp_methods */
3255 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00003256};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003257
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003258/*[clinic input]
3259list.__reversed__
3260
3261Return a reverse iterator over the list.
3262[clinic start generated code]*/
3263
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003264static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003265list___reversed___impl(PyListObject *self)
3266/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3271 if (it == NULL)
3272 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003273 assert(PyList_Check(self));
3274 it->it_index = PyList_GET_SIZE(self) - 1;
3275 Py_INCREF(self);
3276 it->it_seq = self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 PyObject_GC_Track(it);
3278 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003279}
3280
3281static void
3282listreviter_dealloc(listreviterobject *it)
3283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 PyObject_GC_UnTrack(it);
3285 Py_XDECREF(it->it_seq);
3286 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003287}
3288
3289static int
3290listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 Py_VISIT(it->it_seq);
3293 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003294}
3295
3296static PyObject *
3297listreviter_next(listreviterobject *it)
3298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003300 Py_ssize_t index;
3301 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003302
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003303 assert(it != NULL);
3304 seq = it->it_seq;
3305 if (seq == NULL) {
3306 return NULL;
3307 }
3308 assert(PyList_Check(seq));
3309
3310 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 if (index>=0 && index < PyList_GET_SIZE(seq)) {
3312 item = PyList_GET_ITEM(seq, index);
3313 it->it_index--;
3314 Py_INCREF(item);
3315 return item;
3316 }
3317 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003318 it->it_seq = NULL;
3319 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003320 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003321}
3322
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003323static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303324listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 Py_ssize_t len = it->it_index + 1;
3327 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3328 len = 0;
3329 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003330}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003331
3332static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303333listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003334{
3335 return listiter_reduce_general(it, 0);
3336}
3337
3338static PyObject *
3339listreviter_setstate(listreviterobject *it, PyObject *state)
3340{
3341 Py_ssize_t index = PyLong_AsSsize_t(state);
3342 if (index == -1 && PyErr_Occurred())
3343 return NULL;
3344 if (it->it_seq != NULL) {
3345 if (index < -1)
3346 index = -1;
3347 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3348 index = PyList_GET_SIZE(it->it_seq) - 1;
3349 it->it_index = index;
3350 }
3351 Py_RETURN_NONE;
3352}
3353
3354/* common pickling support */
3355
3356static PyObject *
3357listiter_reduce_general(void *_it, int forward)
3358{
3359 PyObject *list;
3360
3361 /* the objects are not the same, index is of different types! */
3362 if (forward) {
3363 listiterobject *it = (listiterobject *)_it;
3364 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02003365 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003366 it->it_seq, it->it_index);
3367 } else {
3368 listreviterobject *it = (listreviterobject *)_it;
3369 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +02003370 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("reversed"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003371 it->it_seq, it->it_index);
3372 }
3373 /* empty iterator, create an empty list */
3374 list = PyList_New(0);
3375 if (list == NULL)
3376 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02003377 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003378}