blob: e11a3fdd1358de94123d98672a4527115391b3fb [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);
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050084 assert(size > 0);
Pablo Galindo372d7052018-10-28 20:16:26 +000085
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050086 PyObject **items = PyMem_New(PyObject*, size);
Pablo Galindo372d7052018-10-28 20:16:26 +000087 if (items == NULL) {
88 PyErr_NoMemory();
89 return -1;
90 }
91 self->ob_item = items;
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050092 self->allocated = size;
Pablo Galindo372d7052018-10-28 20:16:26 +000093 return 0;
94}
95
Christian Heimes77c02eb2008-02-09 02:18:51 +000096/* Debug statistic to compare allocations with reuse through the free list */
97#undef SHOW_ALLOC_COUNT
98#ifdef SHOW_ALLOC_COUNT
99static size_t count_alloc = 0;
100static size_t count_reuse = 0;
101
102static void
103show_alloc(void)
104{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200105 PyInterpreterState *interp = _PyInterpreterState_Get();
Eddie Elizondo745dc652018-02-21 20:55:18 -0800106 if (!interp->core_config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300107 return;
Victor Stinner25420fe2017-11-20 18:12:22 -0800108 }
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
111 count_alloc);
112 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
113 "d\n", count_reuse);
114 fprintf(stderr, "%.2f%% reuse rate\n\n",
115 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +0000116}
117#endif
118
Raymond Hettinger0468e412004-05-05 05:37:53 +0000119/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000120#ifndef PyList_MAXFREELIST
121#define PyList_MAXFREELIST 80
122#endif
123static PyListObject *free_list[PyList_MAXFREELIST];
124static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000125
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100126int
127PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100130 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 while (numfree) {
132 op = free_list[--numfree];
133 assert(PyList_CheckExact(op));
134 PyObject_GC_Del(op);
135 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100136 return ret;
137}
138
139void
140PyList_Fini(void)
141{
142 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000143}
144
David Malcolm49526f42012-06-22 14:55:41 -0400145/* Print summary info about the state of the optimized allocator */
146void
147_PyList_DebugMallocStats(FILE *out)
148{
149 _PyDebugAllocatorStats(out,
150 "free PyListObject",
151 numfree, sizeof(PyListObject));
152}
153
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000154PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000155PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 PyListObject *op;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000158#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 static int initialized = 0;
160 if (!initialized) {
161 Py_AtExit(show_alloc);
162 initialized = 1;
163 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000164#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 if (size < 0) {
167 PyErr_BadInternalCall();
168 return NULL;
169 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 if (numfree) {
171 numfree--;
172 op = free_list[numfree];
173 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000174#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000176#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 } else {
178 op = PyObject_GC_New(PyListObject, &PyList_Type);
179 if (op == NULL)
180 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000181#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000183#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 }
185 if (size <= 0)
186 op->ob_item = NULL;
187 else {
Mark Dickinson5d132382016-08-21 08:55:15 +0100188 op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 if (op->ob_item == NULL) {
190 Py_DECREF(op);
191 return PyErr_NoMemory();
192 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 }
194 Py_SIZE(op) = size;
195 op->allocated = size;
196 _PyObject_GC_TRACK(op);
197 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198}
199
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500200static PyObject *
201list_new_prealloc(Py_ssize_t size)
202{
203 PyListObject *op = (PyListObject *) PyList_New(0);
204 if (size == 0 || op == NULL) {
205 return (PyObject *) op;
206 }
207 assert(op->ob_item == NULL);
208 op->ob_item = PyMem_New(PyObject *, size);
209 if (op->ob_item == NULL) {
210 Py_DECREF(op);
211 return PyErr_NoMemory();
212 }
213 op->allocated = size;
214 return (PyObject *) op;
215}
216
Martin v. Löwis18e16552006-02-15 17:27:45 +0000217Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000218PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 if (!PyList_Check(op)) {
221 PyErr_BadInternalCall();
222 return -1;
223 }
224 else
225 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226}
227
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700228static inline int
229valid_index(Py_ssize_t i, Py_ssize_t limit)
230{
231 /* The cast to size_t lets us use just a single comparison
232 to check whether i is in the range: 0 <= i < limit.
233
234 See: Section 14.2 "Bounds Checking" in the Agner Fog
235 optimization manual found at:
236 https://www.agner.org/optimize/optimizing_cpp.pdf
237 */
238 return (size_t) i < (size_t) limit;
239}
240
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000241static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000242
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000243PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000244PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (!PyList_Check(op)) {
247 PyErr_BadInternalCall();
248 return NULL;
249 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700250 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 if (indexerr == NULL) {
252 indexerr = PyUnicode_FromString(
253 "list index out of range");
254 if (indexerr == NULL)
255 return NULL;
256 }
257 PyErr_SetObject(PyExc_IndexError, indexerr);
258 return NULL;
259 }
260 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261}
262
263int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200264PyList_SetItem(PyObject *op, Py_ssize_t i,
265 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200267 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 if (!PyList_Check(op)) {
269 Py_XDECREF(newitem);
270 PyErr_BadInternalCall();
271 return -1;
272 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700273 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 Py_XDECREF(newitem);
275 PyErr_SetString(PyExc_IndexError,
276 "list assignment index out of range");
277 return -1;
278 }
279 p = ((PyListObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300280 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282}
283
284static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000285ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 Py_ssize_t i, n = Py_SIZE(self);
288 PyObject **items;
289 if (v == NULL) {
290 PyErr_BadInternalCall();
291 return -1;
292 }
293 if (n == PY_SSIZE_T_MAX) {
294 PyErr_SetString(PyExc_OverflowError,
295 "cannot add more objects to list");
296 return -1;
297 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000298
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800299 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 if (where < 0) {
303 where += n;
304 if (where < 0)
305 where = 0;
306 }
307 if (where > n)
308 where = n;
309 items = self->ob_item;
310 for (i = n; --i >= where; )
311 items[i+1] = items[i];
312 Py_INCREF(v);
313 items[where] = v;
314 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315}
316
317int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000318PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 if (!PyList_Check(op)) {
321 PyErr_BadInternalCall();
322 return -1;
323 }
324 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325}
326
Raymond Hettinger40a03822004-04-12 13:05:09 +0000327static int
328app1(PyListObject *self, PyObject *v)
329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 assert (v != NULL);
333 if (n == PY_SSIZE_T_MAX) {
334 PyErr_SetString(PyExc_OverflowError,
335 "cannot add more objects to list");
336 return -1;
337 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000338
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800339 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 Py_INCREF(v);
343 PyList_SET_ITEM(self, n, v);
344 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000345}
346
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347int
Fred Drakea2f55112000-07-09 15:16:51 +0000348PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 if (PyList_Check(op) && (newitem != NULL))
351 return app1((PyListObject *)op, newitem);
352 PyErr_BadInternalCall();
353 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354}
355
356/* Methods */
357
358static void
Fred Drakea2f55112000-07-09 15:16:51 +0000359list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 Py_ssize_t i;
362 PyObject_GC_UnTrack(op);
363 Py_TRASHCAN_SAFE_BEGIN(op)
364 if (op->ob_item != NULL) {
365 /* Do it backwards, for Christian Tismer.
366 There's a simple test case where somehow this reduces
367 thrashing when a *very* large list is created and
368 immediately deleted. */
369 i = Py_SIZE(op);
370 while (--i >= 0) {
371 Py_XDECREF(op->ob_item[i]);
372 }
373 PyMem_FREE(op->ob_item);
374 }
375 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
376 free_list[numfree++] = op;
377 else
378 Py_TYPE(op)->tp_free((PyObject *)op);
379 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000380}
381
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000383list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 Py_ssize_t i;
Victor Stinner5c733472013-11-18 21:11:57 +0100386 PyObject *s;
Victor Stinner5c733472013-11-18 21:11:57 +0100387 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200388
389 if (Py_SIZE(v) == 0) {
390 return PyUnicode_FromString("[]");
391 }
392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 i = Py_ReprEnter((PyObject*)v);
394 if (i != 0) {
395 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
396 }
Tim Petersa7259592001-06-16 05:11:17 +0000397
Victor Stinner5c733472013-11-18 21:11:57 +0100398 _PyUnicodeWriter_Init(&writer);
399 writer.overallocate = 1;
Victor Stinnerb8fb1972013-11-18 22:15:44 +0100400 /* "[" + "1" + ", 2" * (len - 1) + "]" */
401 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +0000402
Victor Stinner5c733472013-11-18 21:11:57 +0100403 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200404 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 /* Do repr() on each element. Note that this may mutate the list,
407 so must refetch the list size on each iteration. */
408 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100409 if (i > 0) {
Victor Stinner4a587072013-11-19 12:54:53 +0100410 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
Victor Stinner5c733472013-11-18 21:11:57 +0100411 goto error;
412 }
413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner5c733472013-11-18 21:11:57 +0100415 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200416 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100417
418 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
419 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200420 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100421 }
422 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 }
Victor Stinner5c733472013-11-18 21:11:57 +0100424
Victor Stinner4d3f1092013-11-19 12:09:00 +0100425 writer.overallocate = 0;
Victor Stinner5c733472013-11-18 21:11:57 +0100426 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200427 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100430 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200431
432error:
Victor Stinner5c733472013-11-18 21:11:57 +0100433 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200434 Py_ReprLeave((PyObject *)v);
435 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436}
437
Martin v. Löwis18e16552006-02-15 17:27:45 +0000438static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000439list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442}
443
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000444static int
Fred Drakea2f55112000-07-09 15:16:51 +0000445list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 Py_ssize_t i;
448 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
451 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
452 Py_EQ);
453 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000454}
455
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000457list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700459 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 if (indexerr == NULL) {
461 indexerr = PyUnicode_FromString(
462 "list index out of range");
463 if (indexerr == NULL)
464 return NULL;
465 }
466 PyErr_SetObject(PyExc_IndexError, indexerr);
467 return NULL;
468 }
469 Py_INCREF(a->ob_item[i]);
470 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471}
472
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000474list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 PyListObject *np;
477 PyObject **src, **dest;
478 Py_ssize_t i, len;
479 if (ilow < 0)
480 ilow = 0;
481 else if (ilow > Py_SIZE(a))
482 ilow = Py_SIZE(a);
483 if (ihigh < ilow)
484 ihigh = ilow;
485 else if (ihigh > Py_SIZE(a))
486 ihigh = Py_SIZE(a);
487 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500488 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 if (np == NULL)
490 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 src = a->ob_item + ilow;
493 dest = np->ob_item;
494 for (i = 0; i < len; i++) {
495 PyObject *v = src[i];
496 Py_INCREF(v);
497 dest[i] = v;
498 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500499 Py_SIZE(np) = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000501}
502
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000503PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000504PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 if (!PyList_Check(a)) {
507 PyErr_BadInternalCall();
508 return NULL;
509 }
510 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000511}
512
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000514list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 Py_ssize_t size;
517 Py_ssize_t i;
518 PyObject **src, **dest;
519 PyListObject *np;
520 if (!PyList_Check(bb)) {
521 PyErr_Format(PyExc_TypeError,
522 "can only concatenate list (not \"%.200s\") to list",
523 bb->ob_type->tp_name);
524 return NULL;
525 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000527 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000529 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500530 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 if (np == NULL) {
532 return NULL;
533 }
534 src = a->ob_item;
535 dest = np->ob_item;
536 for (i = 0; i < Py_SIZE(a); i++) {
537 PyObject *v = src[i];
538 Py_INCREF(v);
539 dest[i] = v;
540 }
541 src = b->ob_item;
542 dest = np->ob_item + Py_SIZE(a);
543 for (i = 0; i < Py_SIZE(b); i++) {
544 PyObject *v = src[i];
545 Py_INCREF(v);
546 dest[i] = v;
547 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500548 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550#undef b
551}
552
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000553static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000554list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 Py_ssize_t i, j;
557 Py_ssize_t size;
558 PyListObject *np;
559 PyObject **p, **items;
560 PyObject *elem;
561 if (n < 0)
562 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100563 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100565 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 if (size == 0)
567 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500568 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 if (np == NULL)
570 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500573 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 elem = a->ob_item[0];
575 for (i = 0; i < n; i++) {
576 items[i] = elem;
577 Py_INCREF(elem);
578 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500580 else {
581 p = np->ob_item;
582 items = a->ob_item;
583 for (i = 0; i < n; i++) {
584 for (j = 0; j < Py_SIZE(a); j++) {
585 *p = items[j];
586 Py_INCREF(*p);
587 p++;
588 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 }
590 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500591 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000593}
594
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000595static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200596_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 Py_ssize_t i;
599 PyObject **item = a->ob_item;
600 if (item != NULL) {
601 /* Because XDECREF can recursively invoke operations on
602 this list, we make it empty first. */
603 i = Py_SIZE(a);
604 Py_SIZE(a) = 0;
605 a->ob_item = NULL;
606 a->allocated = 0;
607 while (--i >= 0) {
608 Py_XDECREF(item[i]);
609 }
610 PyMem_FREE(item);
611 }
612 /* Never fails; the return value can be ignored.
613 Note that there is no guarantee that the list is actually empty
614 at this point, because XDECREF may have populated it again! */
615 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000616}
617
Tim Peters8fc4a912004-07-31 21:53:19 +0000618/* a[ilow:ihigh] = v if v != NULL.
619 * del a[ilow:ihigh] if v == NULL.
620 *
621 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
622 * guaranteed the call cannot fail.
623 */
Armin Rigo93677f02004-07-29 12:40:23 +0000624static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000625list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 /* Because [X]DECREF can recursively invoke list operations on
628 this list, we must postpone all [X]DECREF activity until
629 after the list is back in its canonical shape. Therefore
630 we must allocate an additional array, 'recycle', into which
631 we temporarily copy the items that are deleted from the
632 list. :-( */
633 PyObject *recycle_on_stack[8];
634 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
635 PyObject **item;
636 PyObject **vitem = NULL;
637 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
638 Py_ssize_t n; /* # of elements in replacement list */
639 Py_ssize_t norig; /* # of elements in list getting replaced */
640 Py_ssize_t d; /* Change in size */
641 Py_ssize_t k;
642 size_t s;
643 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000644#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 if (v == NULL)
646 n = 0;
647 else {
648 if (a == b) {
649 /* Special case "a[i:j] = a" -- copy b first */
650 v = list_slice(b, 0, Py_SIZE(b));
651 if (v == NULL)
652 return result;
653 result = list_ass_slice(a, ilow, ihigh, v);
654 Py_DECREF(v);
655 return result;
656 }
657 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
658 if(v_as_SF == NULL)
659 goto Error;
660 n = PySequence_Fast_GET_SIZE(v_as_SF);
661 vitem = PySequence_Fast_ITEMS(v_as_SF);
662 }
663 if (ilow < 0)
664 ilow = 0;
665 else if (ilow > Py_SIZE(a))
666 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 if (ihigh < ilow)
669 ihigh = ilow;
670 else if (ihigh > Py_SIZE(a))
671 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 norig = ihigh - ilow;
674 assert(norig >= 0);
675 d = n - norig;
676 if (Py_SIZE(a) + d == 0) {
677 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200678 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 }
680 item = a->ob_item;
681 /* recycle the items that we are about to remove */
682 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700683 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
684 if (s) {
685 if (s > sizeof(recycle_on_stack)) {
686 recycle = (PyObject **)PyMem_MALLOC(s);
687 if (recycle == NULL) {
688 PyErr_NoMemory();
689 goto Error;
690 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700692 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200696 Py_ssize_t tail;
697 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
698 memmove(&item[ihigh+d], &item[ihigh], tail);
699 if (list_resize(a, Py_SIZE(a) + d) < 0) {
700 memmove(&item[ihigh], &item[ihigh+d], tail);
701 memcpy(&item[ilow], recycle, s);
702 goto Error;
703 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 item = a->ob_item;
705 }
706 else if (d > 0) { /* Insert d items */
707 k = Py_SIZE(a);
708 if (list_resize(a, k+d) < 0)
709 goto Error;
710 item = a->ob_item;
711 memmove(&item[ihigh+d], &item[ihigh],
712 (k - ihigh)*sizeof(PyObject *));
713 }
714 for (k = 0; k < n; k++, ilow++) {
715 PyObject *w = vitem[k];
716 Py_XINCREF(w);
717 item[ilow] = w;
718 }
719 for (k = norig - 1; k >= 0; --k)
720 Py_XDECREF(recycle[k]);
721 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000722 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 if (recycle != recycle_on_stack)
724 PyMem_FREE(recycle);
725 Py_XDECREF(v_as_SF);
726 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727#undef b
728}
729
Guido van Rossum234f9421993-06-17 12:35:49 +0000730int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000731PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 if (!PyList_Check(a)) {
734 PyErr_BadInternalCall();
735 return -1;
736 }
737 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000738}
739
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000740static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000741list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 PyObject **items;
744 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000745
746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 size = PyList_GET_SIZE(self);
748 if (size == 0 || n == 1) {
749 Py_INCREF(self);
750 return (PyObject *)self;
751 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 if (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200754 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 Py_INCREF(self);
756 return (PyObject *)self;
757 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 if (size > PY_SSIZE_T_MAX / n) {
760 return PyErr_NoMemory();
761 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000762
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800763 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 p = size;
767 items = self->ob_item;
768 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
769 for (j = 0; j < size; j++) {
770 PyObject *o = items[j];
771 Py_INCREF(o);
772 items[p++] = o;
773 }
774 }
775 Py_INCREF(self);
776 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000777}
778
Guido van Rossum4a450d01991-04-03 19:05:18 +0000779static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000780list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000781{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700782 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 PyErr_SetString(PyExc_IndexError,
784 "list assignment index out of range");
785 return -1;
786 }
787 if (v == NULL)
788 return list_ass_slice(a, i, i+1, v);
789 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300790 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000792}
793
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200794/*[clinic input]
795list.insert
796
797 index: Py_ssize_t
798 object: object
799 /
800
801Insert object before index.
802[clinic start generated code]*/
803
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000804static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200805list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
806/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000807{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200808 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 Py_RETURN_NONE;
810 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000811}
812
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200813/*[clinic input]
814list.clear
815
816Remove all items from list.
817[clinic start generated code]*/
818
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000819static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200820list_clear_impl(PyListObject *self)
821/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000822{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200823 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000824 Py_RETURN_NONE;
825}
826
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200827/*[clinic input]
828list.copy
829
830Return a shallow copy of the list.
831[clinic start generated code]*/
832
Eli Benderskycbbaa962011-02-25 05:47:53 +0000833static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200834list_copy_impl(PyListObject *self)
835/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000836{
837 return list_slice(self, 0, Py_SIZE(self));
838}
839
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200840/*[clinic input]
841list.append
842
843 object: object
844 /
845
846Append object to the end of the list.
847[clinic start generated code]*/
848
Eli Benderskycbbaa962011-02-25 05:47:53 +0000849static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200850list_append(PyListObject *self, PyObject *object)
851/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000852{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200853 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 Py_RETURN_NONE;
855 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000856}
857
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200858/*[clinic input]
859list.extend
860
861 iterable: object
862 /
863
864Extend list by appending elements from the iterable.
865[clinic start generated code]*/
866
Barry Warsawdedf6d61998-10-09 16:37:25 +0000867static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200868list_extend(PyListObject *self, PyObject *iterable)
869/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 PyObject *it; /* iter(v) */
872 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200873 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 Py_ssize_t mn; /* m + n */
875 Py_ssize_t i;
876 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 /* Special cases:
879 1) lists and tuples which can use PySequence_Fast ops
880 2) extending self to self requires making a copy first
881 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200882 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
883 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200885 iterable = PySequence_Fast(iterable, "argument must be iterable");
886 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200888 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200890 /* short circuit when iterable is empty */
891 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 Py_RETURN_NONE;
893 }
894 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000895 /* It should not be possible to allocate a list large enough to cause
896 an overflow on any relevant platform */
897 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800898 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200899 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 return NULL;
901 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200902 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 * situation a.extend(a), but the following code works
904 * in that case too. Just make sure to resize self
905 * before calling PySequence_Fast_ITEMS.
906 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200907 /* populate the end of self with iterable's items */
908 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 dest = self->ob_item + m;
910 for (i = 0; i < n; i++) {
911 PyObject *o = src[i];
912 Py_INCREF(o);
913 dest[i] = o;
914 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200915 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 Py_RETURN_NONE;
917 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000918
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200919 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 if (it == NULL)
921 return NULL;
922 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200925 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800926 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 Py_DECREF(it);
928 return NULL;
929 }
930 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000931 if (m > PY_SSIZE_T_MAX - n) {
932 /* m + n overflowed; on the chance that n lied, and there really
933 * is enough room, ignore it. If n was telling the truth, we'll
934 * eventually run out of memory during the loop.
935 */
936 }
937 else {
938 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800940 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 goto error;
942 /* Make the list sane again. */
943 Py_SIZE(self) = m;
944 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 /* Run iterator to exhaustion. */
947 for (;;) {
948 PyObject *item = iternext(it);
949 if (item == NULL) {
950 if (PyErr_Occurred()) {
951 if (PyErr_ExceptionMatches(PyExc_StopIteration))
952 PyErr_Clear();
953 else
954 goto error;
955 }
956 break;
957 }
958 if (Py_SIZE(self) < self->allocated) {
959 /* steals ref */
960 PyList_SET_ITEM(self, Py_SIZE(self), item);
961 ++Py_SIZE(self);
962 }
963 else {
964 int status = app1(self, item);
965 Py_DECREF(item); /* append creates a new ref */
966 if (status < 0)
967 goto error;
968 }
969 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200972 if (Py_SIZE(self) < self->allocated) {
973 if (list_resize(self, Py_SIZE(self)) < 0)
974 goto error;
975 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 Py_DECREF(it);
978 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000979
980 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 Py_DECREF(it);
982 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000983}
984
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000985PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200986_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000987{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200988 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000989}
990
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000991static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000992list_inplace_concat(PyListObject *self, PyObject *other)
993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000995
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200996 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 if (result == NULL)
998 return result;
999 Py_DECREF(result);
1000 Py_INCREF(self);
1001 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001002}
1003
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001004/*[clinic input]
1005list.pop
1006
1007 index: Py_ssize_t = -1
1008 /
1009
1010Remove and return item at index (default last).
1011
1012Raises IndexError if list is empty or index is out of range.
1013[clinic start generated code]*/
1014
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001015static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001016list_pop_impl(PyListObject *self, Py_ssize_t index)
1017/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 PyObject *v;
1020 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 if (Py_SIZE(self) == 0) {
1023 /* Special-case most common failure cause */
1024 PyErr_SetString(PyExc_IndexError, "pop from empty list");
1025 return NULL;
1026 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001027 if (index < 0)
1028 index += Py_SIZE(self);
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -07001029 if (!valid_index(index, Py_SIZE(self))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1031 return NULL;
1032 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001033 v = self->ob_item[index];
1034 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001036 if (status >= 0)
1037 return v; /* and v now owns the reference the list had */
1038 else
1039 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 }
1041 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001042 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001043 if (status < 0) {
1044 Py_DECREF(v);
1045 return NULL;
1046 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001048}
1049
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001050/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1051static void
1052reverse_slice(PyObject **lo, PyObject **hi)
1053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 --hi;
1057 while (lo < hi) {
1058 PyObject *t = *lo;
1059 *lo = *hi;
1060 *hi = t;
1061 ++lo;
1062 --hi;
1063 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001064}
1065
Tim Petersa64dc242002-08-01 02:13:36 +00001066/* Lots of code for an adaptive, stable, natural mergesort. There are many
1067 * pieces to this algorithm; read listsort.txt for overviews and details.
1068 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001069
Daniel Stutzbach98338222010-12-02 21:55:33 +00001070/* A sortslice contains a pointer to an array of keys and a pointer to
1071 * an array of corresponding values. In other words, keys[i]
1072 * corresponds with values[i]. If values == NULL, then the keys are
1073 * also the values.
1074 *
1075 * Several convenience routines are provided here, so that keys and
1076 * values are always moved in sync.
1077 */
1078
1079typedef struct {
1080 PyObject **keys;
1081 PyObject **values;
1082} sortslice;
1083
1084Py_LOCAL_INLINE(void)
1085sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1086{
1087 s1->keys[i] = s2->keys[j];
1088 if (s1->values != NULL)
1089 s1->values[i] = s2->values[j];
1090}
1091
1092Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001093sortslice_copy_incr(sortslice *dst, sortslice *src)
1094{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001095 *dst->keys++ = *src->keys++;
1096 if (dst->values != NULL)
1097 *dst->values++ = *src->values++;
1098}
1099
1100Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001101sortslice_copy_decr(sortslice *dst, sortslice *src)
1102{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001103 *dst->keys-- = *src->keys--;
1104 if (dst->values != NULL)
1105 *dst->values-- = *src->values--;
1106}
1107
1108
1109Py_LOCAL_INLINE(void)
1110sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001111 Py_ssize_t n)
1112{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001113 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1114 if (s1->values != NULL)
1115 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1116}
1117
1118Py_LOCAL_INLINE(void)
1119sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001120 Py_ssize_t n)
1121{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001122 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1123 if (s1->values != NULL)
1124 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1125}
1126
1127Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001128sortslice_advance(sortslice *slice, Py_ssize_t n)
1129{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001130 slice->keys += n;
1131 if (slice->values != NULL)
1132 slice->values += n;
1133}
1134
embg1e34da42018-01-28 20:03:23 -07001135/* Comparison function: ms->key_compare, which is set at run-time in
1136 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001137 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1138 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001139
embg1e34da42018-01-28 20:03:23 -07001140#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001141
1142/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001143 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1144 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1145*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001146#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001148
embg1e34da42018-01-28 20:03:23 -07001149/* The maximum number of entries in a MergeState's pending-runs stack.
1150 * This is enough to sort arrays of size up to about
1151 * 32 * phi ** MAX_MERGE_PENDING
1152 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1153 * with 2**64 elements.
1154 */
1155#define MAX_MERGE_PENDING 85
1156
1157/* When we get into galloping mode, we stay there until both runs win less
1158 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1159 */
1160#define MIN_GALLOP 7
1161
1162/* Avoid malloc for small temp arrays. */
1163#define MERGESTATE_TEMP_SIZE 256
1164
1165/* One MergeState exists on the stack per invocation of mergesort. It's just
1166 * a convenient way to pass state around among the helper functions.
1167 */
1168struct s_slice {
1169 sortslice base;
1170 Py_ssize_t len;
1171};
1172
1173typedef struct s_MergeState MergeState;
1174struct s_MergeState {
1175 /* This controls when we get *into* galloping mode. It's initialized
1176 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1177 * random data, and lower for highly structured data.
1178 */
1179 Py_ssize_t min_gallop;
1180
1181 /* 'a' is temp storage to help with merges. It contains room for
1182 * alloced entries.
1183 */
1184 sortslice a; /* may point to temparray below */
1185 Py_ssize_t alloced;
1186
1187 /* A stack of n pending runs yet to be merged. Run #i starts at
1188 * address base[i] and extends for len[i] elements. It's always
1189 * true (so long as the indices are in bounds) that
1190 *
1191 * pending[i].base + pending[i].len == pending[i+1].base
1192 *
1193 * so we could cut the storage for this, but it's a minor amount,
1194 * and keeping all the info explicit simplifies the code.
1195 */
1196 int n;
1197 struct s_slice pending[MAX_MERGE_PENDING];
1198
1199 /* 'a' points to this when possible, rather than muck with malloc. */
1200 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1201
1202 /* This is the function we will use to compare two keys,
1203 * even when none of our special cases apply and we have to use
1204 * safe_object_compare. */
1205 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1206
1207 /* This function is used by unsafe_object_compare to optimize comparisons
1208 * when we know our list is type-homogeneous but we can't assume anything else.
1209 * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1210 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1211
1212 /* This function is used by unsafe_tuple_compare to compare the first elements
1213 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1214 * we can assume more, and use one of the special-case compares. */
1215 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1216};
1217
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001218/* binarysort is the best method for sorting small arrays: it does
1219 few compares, but can do data movement quadratic in the number of
1220 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001221 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001222 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001223 On entry, must have lo <= start <= hi, and that [lo, start) is already
1224 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001225 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001226 Even in case of error, the output slice will be some permutation of
1227 the input (nothing is lost or duplicated).
1228*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001229static int
embg1e34da42018-01-28 20:03:23 -07001230binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001231{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001232 Py_ssize_t k;
1233 PyObject **l, **p, **r;
1234 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001235
Daniel Stutzbach98338222010-12-02 21:55:33 +00001236 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001238 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 ++start;
1240 for (; start < hi; ++start) {
1241 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001242 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 r = start;
1244 pivot = *r;
1245 /* Invariants:
1246 * pivot >= all in [lo, l).
1247 * pivot < all in [r, start).
1248 * The second is vacuously true at the start.
1249 */
1250 assert(l < r);
1251 do {
1252 p = l + ((r - l) >> 1);
1253 IFLT(pivot, *p)
1254 r = p;
1255 else
1256 l = p+1;
1257 } while (l < r);
1258 assert(l == r);
1259 /* The invariants still hold, so pivot >= all in [lo, l) and
1260 pivot < all in [l, start), so pivot belongs at l. Note
1261 that if there are elements equal to pivot, l points to the
1262 first slot after them -- that's why this sort is stable.
1263 Slide over to make room.
1264 Caution: using memmove is much slower under MSVC 5;
1265 we're not usually moving many slots. */
1266 for (p = start; p > l; --p)
1267 *p = *(p-1);
1268 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001269 if (lo.values != NULL) {
1270 Py_ssize_t offset = lo.values - lo.keys;
1271 p = start + offset;
1272 pivot = *p;
1273 l += offset;
1274 for (p = start + offset; p > l; --p)
1275 *p = *(p-1);
1276 *l = pivot;
1277 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 }
1279 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001280
1281 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001283}
1284
Tim Petersa64dc242002-08-01 02:13:36 +00001285/*
1286Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1287is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001288
Tim Petersa64dc242002-08-01 02:13:36 +00001289 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001290
Tim Petersa64dc242002-08-01 02:13:36 +00001291or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001292
Tim Petersa64dc242002-08-01 02:13:36 +00001293 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001294
Tim Petersa64dc242002-08-01 02:13:36 +00001295Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1296For its intended use in a stable mergesort, the strictness of the defn of
1297"descending" is needed so that the caller can safely reverse a descending
1298sequence without violating stability (strict > ensures there are no equal
1299elements to get out of order).
1300
1301Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001302*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001303static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001304count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 Py_ssize_t k;
1307 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 assert(lo < hi);
1310 *descending = 0;
1311 ++lo;
1312 if (lo == hi)
1313 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 n = 2;
1316 IFLT(*lo, *(lo-1)) {
1317 *descending = 1;
1318 for (lo = lo+1; lo < hi; ++lo, ++n) {
1319 IFLT(*lo, *(lo-1))
1320 ;
1321 else
1322 break;
1323 }
1324 }
1325 else {
1326 for (lo = lo+1; lo < hi; ++lo, ++n) {
1327 IFLT(*lo, *(lo-1))
1328 break;
1329 }
1330 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001333fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001335}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001336
Tim Petersa64dc242002-08-01 02:13:36 +00001337/*
1338Locate the proper position of key in a sorted vector; if the vector contains
1339an element equal to key, return the position immediately to the left of
1340the leftmost equal element. [gallop_right() does the same except returns
1341the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001342
Tim Petersa64dc242002-08-01 02:13:36 +00001343"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001344
Tim Petersa64dc242002-08-01 02:13:36 +00001345"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1346hint is to the final result, the faster this runs.
1347
1348The return value is the int k in 0..n such that
1349
1350 a[k-1] < key <= a[k]
1351
1352pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1353key belongs at index k; or, IOW, the first k elements of a should precede
1354key, and the last n-k should follow key.
1355
1356Returns -1 on error. See listsort.txt for info on the method.
1357*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001358static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001359gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 Py_ssize_t ofs;
1362 Py_ssize_t lastofs;
1363 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 a += hint;
1368 lastofs = 0;
1369 ofs = 1;
1370 IFLT(*a, key) {
1371 /* a[hint] < key -- gallop right, until
1372 * a[hint + lastofs] < key <= a[hint + ofs]
1373 */
1374 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1375 while (ofs < maxofs) {
1376 IFLT(a[ofs], key) {
1377 lastofs = ofs;
1378 ofs = (ofs << 1) + 1;
1379 if (ofs <= 0) /* int overflow */
1380 ofs = maxofs;
1381 }
1382 else /* key <= a[hint + ofs] */
1383 break;
1384 }
1385 if (ofs > maxofs)
1386 ofs = maxofs;
1387 /* Translate back to offsets relative to &a[0]. */
1388 lastofs += hint;
1389 ofs += hint;
1390 }
1391 else {
1392 /* key <= a[hint] -- gallop left, until
1393 * a[hint - ofs] < key <= a[hint - lastofs]
1394 */
1395 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1396 while (ofs < maxofs) {
1397 IFLT(*(a-ofs), key)
1398 break;
1399 /* key <= a[hint - ofs] */
1400 lastofs = ofs;
1401 ofs = (ofs << 1) + 1;
1402 if (ofs <= 0) /* int overflow */
1403 ofs = maxofs;
1404 }
1405 if (ofs > maxofs)
1406 ofs = maxofs;
1407 /* Translate back to positive offsets relative to &a[0]. */
1408 k = lastofs;
1409 lastofs = hint - ofs;
1410 ofs = hint - k;
1411 }
1412 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1415 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1416 * right of lastofs but no farther right than ofs. Do a binary
1417 * search, with invariant a[lastofs-1] < key <= a[ofs].
1418 */
1419 ++lastofs;
1420 while (lastofs < ofs) {
1421 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 IFLT(a[m], key)
1424 lastofs = m+1; /* a[m] < key */
1425 else
1426 ofs = m; /* key <= a[m] */
1427 }
1428 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1429 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001430
1431fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001433}
1434
1435/*
1436Exactly like gallop_left(), except that if key already exists in a[0:n],
1437finds the position immediately to the right of the rightmost equal value.
1438
1439The return value is the int k in 0..n such that
1440
1441 a[k-1] <= key < a[k]
1442
1443or -1 if error.
1444
1445The code duplication is massive, but this is enough different given that
1446we're sticking to "<" comparisons that it's much harder to follow if
1447written as one routine with yet another "left or right?" flag.
1448*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001449static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001450gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 Py_ssize_t ofs;
1453 Py_ssize_t lastofs;
1454 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 a += hint;
1459 lastofs = 0;
1460 ofs = 1;
1461 IFLT(key, *a) {
1462 /* key < a[hint] -- gallop left, until
1463 * a[hint - ofs] <= key < a[hint - lastofs]
1464 */
1465 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1466 while (ofs < maxofs) {
1467 IFLT(key, *(a-ofs)) {
1468 lastofs = ofs;
1469 ofs = (ofs << 1) + 1;
1470 if (ofs <= 0) /* int overflow */
1471 ofs = maxofs;
1472 }
1473 else /* a[hint - ofs] <= key */
1474 break;
1475 }
1476 if (ofs > maxofs)
1477 ofs = maxofs;
1478 /* Translate back to positive offsets relative to &a[0]. */
1479 k = lastofs;
1480 lastofs = hint - ofs;
1481 ofs = hint - k;
1482 }
1483 else {
1484 /* a[hint] <= key -- gallop right, until
1485 * a[hint + lastofs] <= key < a[hint + ofs]
1486 */
1487 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1488 while (ofs < maxofs) {
1489 IFLT(key, a[ofs])
1490 break;
1491 /* a[hint + ofs] <= key */
1492 lastofs = ofs;
1493 ofs = (ofs << 1) + 1;
1494 if (ofs <= 0) /* int overflow */
1495 ofs = maxofs;
1496 }
1497 if (ofs > maxofs)
1498 ofs = maxofs;
1499 /* Translate back to offsets relative to &a[0]. */
1500 lastofs += hint;
1501 ofs += hint;
1502 }
1503 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1506 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1507 * right of lastofs but no farther right than ofs. Do a binary
1508 * search, with invariant a[lastofs-1] <= key < a[ofs].
1509 */
1510 ++lastofs;
1511 while (lastofs < ofs) {
1512 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 IFLT(key, a[m])
1515 ofs = m; /* key < a[m] */
1516 else
1517 lastofs = m+1; /* a[m] <= key */
1518 }
1519 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1520 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001521
1522fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001524}
1525
Tim Petersa64dc242002-08-01 02:13:36 +00001526/* Conceptually a MergeState's constructor. */
1527static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001528merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001531 if (has_keyfunc) {
1532 /* The temporary space for merging will need at most half the list
1533 * size rounded up. Use the minimum possible space so we can use the
1534 * rest of temparray for other things. In particular, if there is
1535 * enough extra space, listsort() will use it to store the keys.
1536 */
1537 ms->alloced = (list_size + 1) / 2;
1538
1539 /* ms->alloced describes how many keys will be stored at
1540 ms->temparray, but we also need to store the values. Hence,
1541 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1542 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1543 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1544 ms->a.values = &ms->temparray[ms->alloced];
1545 }
1546 else {
1547 ms->alloced = MERGESTATE_TEMP_SIZE;
1548 ms->a.values = NULL;
1549 }
1550 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 ms->n = 0;
1552 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001553}
1554
1555/* Free all the temp memory owned by the MergeState. This must be called
1556 * when you're done with a MergeState, and may be called before then if
1557 * you want to free the temp memory early.
1558 */
1559static void
1560merge_freemem(MergeState *ms)
1561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001563 if (ms->a.keys != ms->temparray)
1564 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001565}
1566
1567/* Ensure enough temp memory for 'need' array slots is available.
1568 * Returns 0 on success and -1 if the memory can't be gotten.
1569 */
1570static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001571merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001572{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001573 int multiplier;
1574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 assert(ms != NULL);
1576 if (need <= ms->alloced)
1577 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001578
1579 multiplier = ms->a.values != NULL ? 2 : 1;
1580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 /* Don't realloc! That can cost cycles to copy the old data, but
1582 * we don't care what's in the block.
1583 */
1584 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001585 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 PyErr_NoMemory();
1587 return -1;
1588 }
embg1e34da42018-01-28 20:03:23 -07001589 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001590 * sizeof(PyObject *));
1591 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001593 if (ms->a.values != NULL)
1594 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 return 0;
1596 }
1597 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001599}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1601 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001602
Daniel Stutzbach98338222010-12-02 21:55:33 +00001603/* Merge the na elements starting at ssa with the nb elements starting at
1604 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1605 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1606 * should have na <= nb. See listsort.txt for more info. Return 0 if
1607 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001608 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001609static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001610merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1611 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001614 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 int result = -1; /* guilty until proved innocent */
1616 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001617
Daniel Stutzbach98338222010-12-02 21:55:33 +00001618 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1619 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 if (MERGE_GETMEM(ms, na) < 0)
1621 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001622 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1623 dest = ssa;
1624 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001625
Daniel Stutzbach98338222010-12-02 21:55:33 +00001626 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 --nb;
1628 if (nb == 0)
1629 goto Succeed;
1630 if (na == 1)
1631 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 min_gallop = ms->min_gallop;
1634 for (;;) {
1635 Py_ssize_t acount = 0; /* # of times A won in a row */
1636 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 /* Do the straightforward thing until (if ever) one run
1639 * appears to win consistently.
1640 */
1641 for (;;) {
1642 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001643 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 if (k) {
1645 if (k < 0)
1646 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001647 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 ++bcount;
1649 acount = 0;
1650 --nb;
1651 if (nb == 0)
1652 goto Succeed;
1653 if (bcount >= min_gallop)
1654 break;
1655 }
1656 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001657 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 ++acount;
1659 bcount = 0;
1660 --na;
1661 if (na == 1)
1662 goto CopyB;
1663 if (acount >= min_gallop)
1664 break;
1665 }
1666 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 /* One run is winning so consistently that galloping may
1669 * be a huge win. So try that, and continue galloping until
1670 * (if ever) neither run appears to be winning consistently
1671 * anymore.
1672 */
1673 ++min_gallop;
1674 do {
1675 assert(na > 1 && nb > 0);
1676 min_gallop -= min_gallop > 1;
1677 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001678 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 acount = k;
1680 if (k) {
1681 if (k < 0)
1682 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001683 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1684 sortslice_advance(&dest, k);
1685 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 na -= k;
1687 if (na == 1)
1688 goto CopyB;
1689 /* na==0 is impossible now if the comparison
1690 * function is consistent, but we can't assume
1691 * that it is.
1692 */
1693 if (na == 0)
1694 goto Succeed;
1695 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001696 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 --nb;
1698 if (nb == 0)
1699 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001700
embg1e34da42018-01-28 20:03:23 -07001701 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 bcount = k;
1703 if (k) {
1704 if (k < 0)
1705 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001706 sortslice_memmove(&dest, 0, &ssb, 0, k);
1707 sortslice_advance(&dest, k);
1708 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 nb -= k;
1710 if (nb == 0)
1711 goto Succeed;
1712 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001713 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 --na;
1715 if (na == 1)
1716 goto CopyB;
1717 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1718 ++min_gallop; /* penalize it for leaving galloping mode */
1719 ms->min_gallop = min_gallop;
1720 }
Tim Petersa64dc242002-08-01 02:13:36 +00001721Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001723Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001725 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001727CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001729 /* The last element of ssa belongs at the end of the merge. */
1730 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1731 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001733}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001734
Daniel Stutzbach98338222010-12-02 21:55:33 +00001735/* Merge the na elements starting at pa with the nb elements starting at
1736 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1737 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1738 * should have na >= nb. See listsort.txt for more info. Return 0 if
1739 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001740 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001741static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001742merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1743 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001746 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001749
Daniel Stutzbach98338222010-12-02 21:55:33 +00001750 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1751 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 if (MERGE_GETMEM(ms, nb) < 0)
1753 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001754 dest = ssb;
1755 sortslice_advance(&dest, nb-1);
1756 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1757 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001759 ssb.keys = ms->a.keys + nb - 1;
1760 if (ssb.values != NULL)
1761 ssb.values = ms->a.values + nb - 1;
1762 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001763
Daniel Stutzbach98338222010-12-02 21:55:33 +00001764 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 --na;
1766 if (na == 0)
1767 goto Succeed;
1768 if (nb == 1)
1769 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 min_gallop = ms->min_gallop;
1772 for (;;) {
1773 Py_ssize_t acount = 0; /* # of times A won in a row */
1774 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 /* Do the straightforward thing until (if ever) one run
1777 * appears to win consistently.
1778 */
1779 for (;;) {
1780 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001781 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 if (k) {
1783 if (k < 0)
1784 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001785 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 ++acount;
1787 bcount = 0;
1788 --na;
1789 if (na == 0)
1790 goto Succeed;
1791 if (acount >= min_gallop)
1792 break;
1793 }
1794 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001795 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 ++bcount;
1797 acount = 0;
1798 --nb;
1799 if (nb == 1)
1800 goto CopyA;
1801 if (bcount >= min_gallop)
1802 break;
1803 }
1804 }
Tim Petersa64dc242002-08-01 02:13:36 +00001805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 /* One run is winning so consistently that galloping may
1807 * be a huge win. So try that, and continue galloping until
1808 * (if ever) neither run appears to be winning consistently
1809 * anymore.
1810 */
1811 ++min_gallop;
1812 do {
1813 assert(na > 0 && nb > 1);
1814 min_gallop -= min_gallop > 1;
1815 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001816 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 if (k < 0)
1818 goto Fail;
1819 k = na - k;
1820 acount = k;
1821 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001822 sortslice_advance(&dest, -k);
1823 sortslice_advance(&ssa, -k);
1824 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 na -= k;
1826 if (na == 0)
1827 goto Succeed;
1828 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001829 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 --nb;
1831 if (nb == 1)
1832 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001833
embg1e34da42018-01-28 20:03:23 -07001834 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 if (k < 0)
1836 goto Fail;
1837 k = nb - k;
1838 bcount = k;
1839 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001840 sortslice_advance(&dest, -k);
1841 sortslice_advance(&ssb, -k);
1842 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 nb -= k;
1844 if (nb == 1)
1845 goto CopyA;
1846 /* nb==0 is impossible now if the comparison
1847 * function is consistent, but we can't assume
1848 * that it is.
1849 */
1850 if (nb == 0)
1851 goto Succeed;
1852 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001853 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 --na;
1855 if (na == 0)
1856 goto Succeed;
1857 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1858 ++min_gallop; /* penalize it for leaving galloping mode */
1859 ms->min_gallop = min_gallop;
1860 }
Tim Petersa64dc242002-08-01 02:13:36 +00001861Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001863Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001865 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001867CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001869 /* The first element of ssb belongs at the front of the merge. */
1870 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1871 sortslice_advance(&dest, -na);
1872 sortslice_advance(&ssa, -na);
1873 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001875}
1876
1877/* Merge the two runs at stack indices i and i+1.
1878 * Returns 0 on success, -1 on error.
1879 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001880static Py_ssize_t
1881merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001882{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001883 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 Py_ssize_t na, nb;
1885 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 assert(ms != NULL);
1888 assert(ms->n >= 2);
1889 assert(i >= 0);
1890 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001891
Daniel Stutzbach98338222010-12-02 21:55:33 +00001892 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001894 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 nb = ms->pending[i+1].len;
1896 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001897 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 /* Record the length of the combined runs; if i is the 3rd-last
1900 * run now, also slide over the last run (which isn't involved
1901 * in this merge). The current run i+1 goes away in any case.
1902 */
1903 ms->pending[i].len = na + nb;
1904 if (i == ms->n - 3)
1905 ms->pending[i+1] = ms->pending[i+2];
1906 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 /* Where does b start in a? Elements in a before that can be
1909 * ignored (already in place).
1910 */
embg1e34da42018-01-28 20:03:23 -07001911 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 if (k < 0)
1913 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001914 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 na -= k;
1916 if (na == 0)
1917 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 /* Where does a end in b? Elements in b after that can be
1920 * ignored (already in place).
1921 */
embg1e34da42018-01-28 20:03:23 -07001922 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 if (nb <= 0)
1924 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 /* Merge what remains of the runs, using a temp array with
1927 * min(na, nb) elements.
1928 */
1929 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001930 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001932 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001933}
1934
1935/* Examine the stack of runs waiting to be merged, merging adjacent runs
1936 * until the stack invariants are re-established:
1937 *
1938 * 1. len[-3] > len[-2] + len[-1]
1939 * 2. len[-2] > len[-1]
1940 *
1941 * See listsort.txt for more info.
1942 *
1943 * Returns 0 on success, -1 on error.
1944 */
1945static int
1946merge_collapse(MergeState *ms)
1947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 assert(ms);
1951 while (ms->n > 1) {
1952 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001953 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1954 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 if (p[n-1].len < p[n+1].len)
1956 --n;
1957 if (merge_at(ms, n) < 0)
1958 return -1;
1959 }
1960 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001961 if (merge_at(ms, n) < 0)
1962 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 }
1964 else
1965 break;
1966 }
1967 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001968}
1969
1970/* Regardless of invariants, merge all runs on the stack until only one
1971 * remains. This is used at the end of the mergesort.
1972 *
1973 * Returns 0 on success, -1 on error.
1974 */
1975static int
1976merge_force_collapse(MergeState *ms)
1977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 assert(ms);
1981 while (ms->n > 1) {
1982 Py_ssize_t n = ms->n - 2;
1983 if (n > 0 && p[n-1].len < p[n+1].len)
1984 --n;
1985 if (merge_at(ms, n) < 0)
1986 return -1;
1987 }
1988 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001989}
1990
1991/* Compute a good value for the minimum run length; natural runs shorter
1992 * than this are boosted artificially via binary insertion.
1993 *
1994 * If n < 64, return n (it's too small to bother with fancy stuff).
1995 * Else if n is an exact power of 2, return 32.
1996 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1997 * strictly less than, an exact power of 2.
1998 *
1999 * See listsort.txt for more info.
2000 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002001static Py_ssize_t
2002merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00002003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00002005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 assert(n >= 0);
2007 while (n >= 64) {
2008 r |= n & 1;
2009 n >>= 1;
2010 }
2011 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002012}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00002013
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002014static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00002015reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002016{
Daniel Stutzbach98338222010-12-02 21:55:33 +00002017 reverse_slice(s->keys, &s->keys[n]);
2018 if (s->values != NULL)
2019 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002020}
2021
embg1e34da42018-01-28 20:03:23 -07002022/* Here we define custom comparison functions to optimize for the cases one commonly
2023 * encounters in practice: homogeneous lists, often of one of the basic types. */
2024
2025/* This struct holds the comparison function and helper functions
2026 * selected in the pre-sort check. */
2027
2028/* These are the special case compare functions.
2029 * ms->key_compare will always point to one of these: */
2030
2031/* Heterogeneous compare: default, always safe to fall back on. */
2032static int
2033safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2034{
2035 /* No assumptions necessary! */
2036 return PyObject_RichCompareBool(v, w, Py_LT);
2037}
2038
2039/* Homogeneous compare: safe for any two compareable objects of the same type.
2040 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2041 * pre-sort check.)
2042 */
2043static int
2044unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2045{
2046 PyObject *res_obj; int res;
2047
2048 /* No assumptions, because we check first: */
2049 if (v->ob_type->tp_richcompare != ms->key_richcompare)
2050 return PyObject_RichCompareBool(v, w, Py_LT);
2051
2052 assert(ms->key_richcompare != NULL);
2053 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2054
2055 if (res_obj == Py_NotImplemented) {
2056 Py_DECREF(res_obj);
2057 return PyObject_RichCompareBool(v, w, Py_LT);
2058 }
2059 if (res_obj == NULL)
2060 return -1;
2061
2062 if (PyBool_Check(res_obj)) {
2063 res = (res_obj == Py_True);
2064 }
2065 else {
2066 res = PyObject_IsTrue(res_obj);
2067 }
2068 Py_DECREF(res_obj);
2069
2070 /* Note that we can't assert
2071 * res == PyObject_RichCompareBool(v, w, Py_LT);
2072 * because of evil compare functions like this:
2073 * lambda a, b: int(random.random() * 3) - 1)
2074 * (which is actually in test_sort.py) */
2075 return res;
2076}
2077
2078/* Latin string compare: safe for any two latin (one byte per char) strings. */
2079static int
2080unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2081{
Victor Stinner8017b802018-01-29 13:47:06 +01002082 Py_ssize_t len;
2083 int res;
embg1e34da42018-01-28 20:03:23 -07002084
2085 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2086 assert(v->ob_type == w->ob_type);
2087 assert(v->ob_type == &PyUnicode_Type);
2088 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2089 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2090
2091 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2092 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2093
2094 res = (res != 0 ?
2095 res < 0 :
2096 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2097
2098 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2099 return res;
2100}
2101
2102/* Bounded int compare: compare any two longs that fit in a single machine word. */
2103static int
2104unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2105{
2106 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2107
2108 /* Modified from Objects/longobject.c:long_compare, assuming: */
2109 assert(v->ob_type == w->ob_type);
2110 assert(v->ob_type == &PyLong_Type);
2111 assert(Py_ABS(Py_SIZE(v)) <= 1);
2112 assert(Py_ABS(Py_SIZE(w)) <= 1);
2113
2114 vl = (PyLongObject*)v;
2115 wl = (PyLongObject*)w;
2116
2117 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2118 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2119
2120 if (Py_SIZE(vl) < 0)
2121 v0 = -v0;
2122 if (Py_SIZE(wl) < 0)
2123 w0 = -w0;
2124
2125 res = v0 < w0;
2126 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2127 return res;
2128}
2129
2130/* Float compare: compare any two floats. */
2131static int
2132unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2133{
2134 int res;
2135
2136 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2137 assert(v->ob_type == w->ob_type);
2138 assert(v->ob_type == &PyFloat_Type);
2139
2140 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2141 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2142 return res;
2143}
2144
2145/* Tuple compare: compare *any* two tuples, using
2146 * ms->tuple_elem_compare to compare the first elements, which is set
2147 * using the same pre-sort check as we use for ms->key_compare,
2148 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2149 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2150 * that most tuple compares don't involve x[1:]. */
2151static int
2152unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2153{
2154 PyTupleObject *vt, *wt;
2155 Py_ssize_t i, vlen, wlen;
2156 int k;
2157
2158 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2159 assert(v->ob_type == w->ob_type);
2160 assert(v->ob_type == &PyTuple_Type);
2161 assert(Py_SIZE(v) > 0);
2162 assert(Py_SIZE(w) > 0);
2163
2164 vt = (PyTupleObject *)v;
2165 wt = (PyTupleObject *)w;
2166
2167 vlen = Py_SIZE(vt);
2168 wlen = Py_SIZE(wt);
2169
2170 for (i = 0; i < vlen && i < wlen; i++) {
2171 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2172 if (k < 0)
2173 return -1;
2174 if (!k)
2175 break;
2176 }
2177
2178 if (i >= vlen || i >= wlen)
2179 return vlen < wlen;
2180
2181 if (i == 0)
2182 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2183 else
2184 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2185}
2186
Tim Petersa64dc242002-08-01 02:13:36 +00002187/* An adaptive, stable, natural mergesort. See listsort.txt.
2188 * Returns Py_None on success, NULL on error. Even in case of error, the
2189 * list will be some permutation of its input state (nothing is lost or
2190 * duplicated).
2191 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002192/*[clinic input]
2193list.sort
2194
2195 *
2196 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002197 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002198
2199Stable sort *IN PLACE*.
2200[clinic start generated code]*/
2201
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002202static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002203list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002204/*[clinic end generated code: output=57b9f9c5e23fbe42 input=b0fcf743982c5b90]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 Py_ssize_t nremaining;
2208 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002209 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 Py_ssize_t saved_ob_size, saved_allocated;
2211 PyObject **saved_ob_item;
2212 PyObject **final_ob_item;
2213 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002215 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002218 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 if (keyfunc == Py_None)
2220 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 /* The list is temporarily made empty, so that mutations performed
2223 * by comparison functions can't affect the slice of memory we're
2224 * sorting (allowing mutations during sorting is a core-dump
2225 * factory, since ob_item may change).
2226 */
2227 saved_ob_size = Py_SIZE(self);
2228 saved_ob_item = self->ob_item;
2229 saved_allocated = self->allocated;
2230 Py_SIZE(self) = 0;
2231 self->ob_item = NULL;
2232 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002233
Daniel Stutzbach98338222010-12-02 21:55:33 +00002234 if (keyfunc == NULL) {
2235 keys = NULL;
2236 lo.keys = saved_ob_item;
2237 lo.values = NULL;
2238 }
2239 else {
2240 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2241 /* Leverage stack space we allocated but won't otherwise use */
2242 keys = &ms.temparray[saved_ob_size+1];
2243 else {
2244 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002245 if (keys == NULL) {
2246 PyErr_NoMemory();
2247 goto keyfunc_fail;
2248 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002250
2251 for (i = 0; i < saved_ob_size ; i++) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002252 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
2253 NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002254 if (keys[i] == NULL) {
2255 for (i=i-1 ; i>=0 ; i--)
2256 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002257 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002258 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002259 goto keyfunc_fail;
2260 }
2261 }
2262
2263 lo.keys = keys;
2264 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002266
embg1e34da42018-01-28 20:03:23 -07002267
2268 /* The pre-sort check: here's where we decide which compare function to use.
2269 * How much optimization is safe? We test for homogeneity with respect to
2270 * several properties that are expensive to check at compare-time, and
2271 * set ms appropriately. */
2272 if (saved_ob_size > 1) {
2273 /* Assume the first element is representative of the whole list. */
2274 int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2275 Py_SIZE(lo.keys[0]) > 0);
2276
2277 PyTypeObject* key_type = (keys_are_in_tuples ?
2278 PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2279 lo.keys[0]->ob_type);
2280
2281 int keys_are_all_same_type = 1;
2282 int strings_are_latin = 1;
2283 int ints_are_bounded = 1;
2284
2285 /* Prove that assumption by checking every key. */
embg1e34da42018-01-28 20:03:23 -07002286 for (i=0; i < saved_ob_size; i++) {
2287
2288 if (keys_are_in_tuples &&
2289 !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2290 keys_are_in_tuples = 0;
2291 keys_are_all_same_type = 0;
2292 break;
2293 }
2294
2295 /* Note: for lists of tuples, key is the first element of the tuple
2296 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2297 * for lists of tuples in the if-statement directly above. */
2298 PyObject *key = (keys_are_in_tuples ?
2299 PyTuple_GET_ITEM(lo.keys[i], 0) :
2300 lo.keys[i]);
2301
2302 if (key->ob_type != key_type) {
2303 keys_are_all_same_type = 0;
2304 break;
2305 }
2306
2307 if (key_type == &PyLong_Type) {
2308 if (ints_are_bounded && Py_ABS(Py_SIZE(key)) > 1)
2309 ints_are_bounded = 0;
2310 }
2311 else if (key_type == &PyUnicode_Type){
2312 if (strings_are_latin &&
2313 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND)
2314 strings_are_latin = 0;
2315 }
2316 }
2317
2318 /* Choose the best compare, given what we now know about the keys. */
2319 if (keys_are_all_same_type) {
2320
2321 if (key_type == &PyUnicode_Type && strings_are_latin) {
2322 ms.key_compare = unsafe_latin_compare;
2323 }
2324 else if (key_type == &PyLong_Type && ints_are_bounded) {
2325 ms.key_compare = unsafe_long_compare;
2326 }
2327 else if (key_type == &PyFloat_Type) {
2328 ms.key_compare = unsafe_float_compare;
2329 }
2330 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2331 ms.key_compare = unsafe_object_compare;
2332 }
2333 }
2334 else {
2335 ms.key_compare = safe_object_compare;
2336 }
2337
2338 if (keys_are_in_tuples) {
2339 /* Make sure we're not dealing with tuples of tuples
2340 * (remember: here, key_type refers list [key[0] for key in keys]) */
2341 if (key_type == &PyTuple_Type)
2342 ms.tuple_elem_compare = safe_object_compare;
2343 else
2344 ms.tuple_elem_compare = ms.key_compare;
2345
2346 ms.key_compare = unsafe_tuple_compare;
2347 }
2348 }
2349 /* End of pre-sort check: ms is now set properly! */
2350
Daniel Stutzbach98338222010-12-02 21:55:33 +00002351 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 nremaining = saved_ob_size;
2354 if (nremaining < 2)
2355 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002356
Benjamin Peterson05380642010-08-23 19:35:39 +00002357 /* Reverse sort stability achieved by initially reversing the list,
2358 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002359 if (reverse) {
2360 if (keys != NULL)
2361 reverse_slice(&keys[0], &keys[saved_ob_size]);
2362 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2363 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 /* March over the array once, left to right, finding natural runs,
2366 * and extending short natural runs to minrun elements.
2367 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 minrun = merge_compute_minrun(nremaining);
2369 do {
2370 int descending;
2371 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002374 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 if (n < 0)
2376 goto fail;
2377 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002378 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 /* If short, extend to min(minrun, nremaining). */
2380 if (n < minrun) {
2381 const Py_ssize_t force = nremaining <= minrun ?
2382 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002383 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 goto fail;
2385 n = force;
2386 }
2387 /* Push run onto pending-runs stack, and maybe merge. */
2388 assert(ms.n < MAX_MERGE_PENDING);
2389 ms.pending[ms.n].base = lo;
2390 ms.pending[ms.n].len = n;
2391 ++ms.n;
2392 if (merge_collapse(&ms) < 0)
2393 goto fail;
2394 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002395 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 nremaining -= n;
2397 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 if (merge_force_collapse(&ms) < 0)
2400 goto fail;
2401 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002402 assert(keys == NULL
2403 ? ms.pending[0].base.keys == saved_ob_item
2404 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002406 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002407
2408succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002410fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002411 if (keys != NULL) {
2412 for (i = 0; i < saved_ob_size; i++)
2413 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002414 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002415 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 if (self->allocated != -1 && result != NULL) {
2419 /* The user mucked with the list during the sort,
2420 * and we don't already have another error to report.
2421 */
2422 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2423 result = NULL;
2424 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 if (reverse && saved_ob_size > 1)
2427 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002430
Daniel Stutzbach98338222010-12-02 21:55:33 +00002431keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 final_ob_item = self->ob_item;
2433 i = Py_SIZE(self);
2434 Py_SIZE(self) = saved_ob_size;
2435 self->ob_item = saved_ob_item;
2436 self->allocated = saved_allocated;
2437 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002438 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 guarantee that the list is really empty when it returns */
2440 while (--i >= 0) {
2441 Py_XDECREF(final_ob_item[i]);
2442 }
2443 PyMem_FREE(final_ob_item);
2444 }
2445 Py_XINCREF(result);
2446 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002447}
Tim Peters330f9e92002-07-19 07:05:44 +00002448#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002449#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002450
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002451int
Fred Drakea2f55112000-07-09 15:16:51 +00002452PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 if (v == NULL || !PyList_Check(v)) {
2455 PyErr_BadInternalCall();
2456 return -1;
2457 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002458 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 if (v == NULL)
2460 return -1;
2461 Py_DECREF(v);
2462 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002463}
2464
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002465/*[clinic input]
2466list.reverse
2467
2468Reverse *IN PLACE*.
2469[clinic start generated code]*/
2470
Guido van Rossumb86c5492001-02-12 22:06:02 +00002471static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002472list_reverse_impl(PyListObject *self)
2473/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 if (Py_SIZE(self) > 1)
2476 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2477 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002478}
2479
Guido van Rossum84c76f51990-10-30 13:32:20 +00002480int
Fred Drakea2f55112000-07-09 15:16:51 +00002481PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 if (v == NULL || !PyList_Check(v)) {
2486 PyErr_BadInternalCall();
2487 return -1;
2488 }
2489 if (Py_SIZE(self) > 1)
2490 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2491 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002492}
2493
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002494PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002495PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 PyObject *w;
2498 PyObject **p, **q;
2499 Py_ssize_t n;
2500 if (v == NULL || !PyList_Check(v)) {
2501 PyErr_BadInternalCall();
2502 return NULL;
2503 }
2504 n = Py_SIZE(v);
2505 w = PyTuple_New(n);
2506 if (w == NULL)
2507 return NULL;
2508 p = ((PyTupleObject *)w)->ob_item;
2509 q = ((PyListObject *)v)->ob_item;
2510 while (--n >= 0) {
2511 Py_INCREF(*q);
2512 *p = *q;
2513 p++;
2514 q++;
2515 }
2516 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002517}
2518
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002519/*[clinic input]
2520list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002521
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002522 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002523 start: slice_index(accept={int}) = 0
2524 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002525 /
2526
2527Return first index of value.
2528
2529Raises ValueError if the value is not present.
2530[clinic start generated code]*/
2531
2532static PyObject *
2533list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2534 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002535/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002536{
2537 Py_ssize_t i;
2538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 if (start < 0) {
2540 start += Py_SIZE(self);
2541 if (start < 0)
2542 start = 0;
2543 }
2544 if (stop < 0) {
2545 stop += Py_SIZE(self);
2546 if (stop < 0)
2547 stop = 0;
2548 }
2549 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002550 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 if (cmp > 0)
2552 return PyLong_FromSsize_t(i);
2553 else if (cmp < 0)
2554 return NULL;
2555 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002556 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002558}
2559
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002560/*[clinic input]
2561list.count
2562
2563 value: object
2564 /
2565
2566Return number of occurrences of value.
2567[clinic start generated code]*/
2568
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002569static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002570list_count(PyListObject *self, PyObject *value)
2571/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 Py_ssize_t count = 0;
2574 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002577 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 if (cmp > 0)
2579 count++;
2580 else if (cmp < 0)
2581 return NULL;
2582 }
2583 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002584}
2585
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002586/*[clinic input]
2587list.remove
2588
2589 value: object
2590 /
2591
2592Remove first occurrence of value.
2593
2594Raises ValueError if the value is not present.
2595[clinic start generated code]*/
2596
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002597static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002598list_remove(PyListObject *self, PyObject *value)
2599/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002601 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002604 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 if (cmp > 0) {
2606 if (list_ass_slice(self, i, i+1,
2607 (PyObject *)NULL) == 0)
2608 Py_RETURN_NONE;
2609 return NULL;
2610 }
2611 else if (cmp < 0)
2612 return NULL;
2613 }
2614 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2615 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002616}
2617
Jeremy Hylton8caad492000-06-23 14:18:11 +00002618static int
2619list_traverse(PyListObject *o, visitproc visit, void *arg)
2620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 for (i = Py_SIZE(o); --i >= 0; )
2624 Py_VISIT(o->ob_item[i]);
2625 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002626}
2627
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002628static PyObject *
2629list_richcompare(PyObject *v, PyObject *w, int op)
2630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 PyListObject *vl, *wl;
2632 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002633
Brian Curtindfc80e32011-08-10 20:28:54 -05002634 if (!PyList_Check(v) || !PyList_Check(w))
2635 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 vl = (PyListObject *)v;
2638 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2641 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002643 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 else
stratakise8b19652017-11-02 11:32:54 +01002645 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 /* Search for the first index where items are different */
2649 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2650 int k = PyObject_RichCompareBool(vl->ob_item[i],
2651 wl->ob_item[i], Py_EQ);
2652 if (k < 0)
2653 return NULL;
2654 if (!k)
2655 break;
2656 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2659 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002660 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 /* We have an item that differs -- shortcuts for EQ/NE */
2664 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002665 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 }
2667 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002668 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 /* Compare the final item again using the proper operator */
2672 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002673}
2674
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002675/*[clinic input]
2676list.__init__
2677
2678 iterable: object(c_default="NULL") = ()
2679 /
2680
2681Built-in mutable sequence.
2682
2683If no argument is given, the constructor creates a new empty list.
2684The argument must be an iterable if specified.
2685[clinic start generated code]*/
2686
Tim Peters6d6c1a32001-08-02 04:15:00 +00002687static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002688list___init___impl(PyListObject *self, PyObject *iterable)
2689/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 /* Verify list invariants established by PyType_GenericAlloc() */
2692 assert(0 <= Py_SIZE(self));
2693 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2694 assert(self->ob_item != NULL ||
2695 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 /* Empty previous contents */
2698 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002699 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002701 if (iterable != NULL) {
Pablo Galindo372d7052018-10-28 20:16:26 +00002702 if (_PyObject_HasLen(iterable)) {
2703 Py_ssize_t iter_len = PyObject_Size(iterable);
2704 if (iter_len == -1) {
2705 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2706 return -1;
2707 }
2708 PyErr_Clear();
2709 }
2710 if (iter_len > 0 && self->ob_item == NULL
2711 && list_preallocate_exact(self, iter_len)) {
2712 return -1;
2713 }
2714 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002715 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 if (rv == NULL)
2717 return -1;
2718 Py_DECREF(rv);
2719 }
2720 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002721}
2722
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002723/*[clinic input]
2724list.__sizeof__
2725
2726Return the size of the list in memory, in bytes.
2727[clinic start generated code]*/
2728
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002729static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002730list___sizeof___impl(PyListObject *self)
2731/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002734
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002735 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002737}
2738
Raymond Hettinger1021c442003-11-07 15:38:09 +00002739static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002740static PyObject *list_subscript(PyListObject*, PyObject*);
2741
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002742static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002743 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2744 LIST___REVERSED___METHODDEF
2745 LIST___SIZEOF___METHODDEF
2746 LIST_CLEAR_METHODDEF
2747 LIST_COPY_METHODDEF
2748 LIST_APPEND_METHODDEF
2749 LIST_INSERT_METHODDEF
2750 LIST_EXTEND_METHODDEF
2751 LIST_POP_METHODDEF
2752 LIST_REMOVE_METHODDEF
2753 LIST_INDEX_METHODDEF
2754 LIST_COUNT_METHODDEF
2755 LIST_REVERSE_METHODDEF
2756 LIST_SORT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002758};
2759
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002760static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 (lenfunc)list_length, /* sq_length */
2762 (binaryfunc)list_concat, /* sq_concat */
2763 (ssizeargfunc)list_repeat, /* sq_repeat */
2764 (ssizeargfunc)list_item, /* sq_item */
2765 0, /* sq_slice */
2766 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2767 0, /* sq_ass_slice */
2768 (objobjproc)list_contains, /* sq_contains */
2769 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2770 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002771};
2772
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002773static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002774list_subscript(PyListObject* self, PyObject* item)
2775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 if (PyIndex_Check(item)) {
2777 Py_ssize_t i;
2778 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2779 if (i == -1 && PyErr_Occurred())
2780 return NULL;
2781 if (i < 0)
2782 i += PyList_GET_SIZE(self);
2783 return list_item(self, i);
2784 }
2785 else if (PySlice_Check(item)) {
2786 Py_ssize_t start, stop, step, slicelength, cur, i;
2787 PyObject* result;
2788 PyObject* it;
2789 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002790
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002791 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 return NULL;
2793 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002794 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2795 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 if (slicelength <= 0) {
2798 return PyList_New(0);
2799 }
2800 else if (step == 1) {
2801 return list_slice(self, start, stop);
2802 }
2803 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002804 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 src = self->ob_item;
2808 dest = ((PyListObject *)result)->ob_item;
2809 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002810 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 it = src[cur];
2812 Py_INCREF(it);
2813 dest[i] = it;
2814 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002815 Py_SIZE(result) = slicelength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 return result;
2817 }
2818 }
2819 else {
2820 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002821 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 item->ob_type->tp_name);
2823 return NULL;
2824 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002825}
2826
Tim Peters3b01a122002-07-19 02:35:45 +00002827static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002828list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 if (PyIndex_Check(item)) {
2831 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2832 if (i == -1 && PyErr_Occurred())
2833 return -1;
2834 if (i < 0)
2835 i += PyList_GET_SIZE(self);
2836 return list_ass_item(self, i, value);
2837 }
2838 else if (PySlice_Check(item)) {
2839 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002840
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002841 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 return -1;
2843 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002844 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2845 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 if (step == 1)
2848 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 /* Make sure s[5:2] = [..] inserts at the right place:
2851 before 5, not before 2. */
2852 if ((step < 0 && start < stop) ||
2853 (step > 0 && start > stop))
2854 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 if (value == NULL) {
2857 /* delete slice */
2858 PyObject **garbage;
2859 size_t cur;
2860 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002861 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 if (slicelength <= 0)
2864 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 if (step < 0) {
2867 stop = start + 1;
2868 start = stop + step*(slicelength - 1) - 1;
2869 step = -step;
2870 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 garbage = (PyObject**)
2873 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2874 if (!garbage) {
2875 PyErr_NoMemory();
2876 return -1;
2877 }
Tim Peters3b01a122002-07-19 02:35:45 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 /* drawing pictures might help understand these for
2880 loops. Basically, we memmove the parts of the
2881 list that are *not* part of the slice: step-1
2882 items for each item that is part of the slice,
2883 and then tail end of the list that was not
2884 covered by the slice */
2885 for (cur = start, i = 0;
2886 cur < (size_t)stop;
2887 cur += step, i++) {
2888 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 if (cur + step >= (size_t)Py_SIZE(self)) {
2893 lim = Py_SIZE(self) - cur - 1;
2894 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 memmove(self->ob_item + cur - i,
2897 self->ob_item + cur + 1,
2898 lim * sizeof(PyObject *));
2899 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002900 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 if (cur < (size_t)Py_SIZE(self)) {
2902 memmove(self->ob_item + cur - slicelength,
2903 self->ob_item + cur,
2904 (Py_SIZE(self) - cur) *
2905 sizeof(PyObject *));
2906 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002909 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 for (i = 0; i < slicelength; i++) {
2912 Py_DECREF(garbage[i]);
2913 }
2914 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002915
Victor Stinner35f28032013-11-21 12:16:35 +01002916 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 }
2918 else {
2919 /* assign slice */
2920 PyObject *ins, *seq;
2921 PyObject **garbage, **seqitems, **selfitems;
2922 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 /* protect against a[::-1] = a */
2925 if (self == (PyListObject*)value) {
2926 seq = list_slice((PyListObject*)value, 0,
2927 PyList_GET_SIZE(value));
2928 }
2929 else {
2930 seq = PySequence_Fast(value,
2931 "must assign iterable "
2932 "to extended slice");
2933 }
2934 if (!seq)
2935 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2938 PyErr_Format(PyExc_ValueError,
2939 "attempt to assign sequence of "
2940 "size %zd to extended slice of "
2941 "size %zd",
2942 PySequence_Fast_GET_SIZE(seq),
2943 slicelength);
2944 Py_DECREF(seq);
2945 return -1;
2946 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 if (!slicelength) {
2949 Py_DECREF(seq);
2950 return 0;
2951 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 garbage = (PyObject**)
2954 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2955 if (!garbage) {
2956 Py_DECREF(seq);
2957 PyErr_NoMemory();
2958 return -1;
2959 }
Tim Peters3b01a122002-07-19 02:35:45 +00002960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 selfitems = self->ob_item;
2962 seqitems = PySequence_Fast_ITEMS(seq);
2963 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002964 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002965 garbage[i] = selfitems[cur];
2966 ins = seqitems[i];
2967 Py_INCREF(ins);
2968 selfitems[cur] = ins;
2969 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 for (i = 0; i < slicelength; i++) {
2972 Py_DECREF(garbage[i]);
2973 }
Tim Peters3b01a122002-07-19 02:35:45 +00002974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 PyMem_FREE(garbage);
2976 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 return 0;
2979 }
2980 }
2981 else {
2982 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002983 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 item->ob_type->tp_name);
2985 return -1;
2986 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002987}
2988
2989static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 (lenfunc)list_length,
2991 (binaryfunc)list_subscript,
2992 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002993};
2994
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002995PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2997 "list",
2998 sizeof(PyListObject),
2999 0,
3000 (destructor)list_dealloc, /* tp_dealloc */
3001 0, /* tp_print */
3002 0, /* tp_getattr */
3003 0, /* tp_setattr */
3004 0, /* tp_reserved */
3005 (reprfunc)list_repr, /* tp_repr */
3006 0, /* tp_as_number */
3007 &list_as_sequence, /* tp_as_sequence */
3008 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003009 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 0, /* tp_call */
3011 0, /* tp_str */
3012 PyObject_GenericGetAttr, /* tp_getattro */
3013 0, /* tp_setattro */
3014 0, /* tp_as_buffer */
3015 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003016 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
3017 list___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 (traverseproc)list_traverse, /* tp_traverse */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003019 (inquiry)_list_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 list_richcompare, /* tp_richcompare */
3021 0, /* tp_weaklistoffset */
3022 list_iter, /* tp_iter */
3023 0, /* tp_iternext */
3024 list_methods, /* tp_methods */
3025 0, /* tp_members */
3026 0, /* tp_getset */
3027 0, /* tp_base */
3028 0, /* tp_dict */
3029 0, /* tp_descr_get */
3030 0, /* tp_descr_set */
3031 0, /* tp_dictoffset */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003032 (initproc)list___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 PyType_GenericAlloc, /* tp_alloc */
3034 PyType_GenericNew, /* tp_new */
3035 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003036};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00003037
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003038/*********************** List Iterator **************************/
3039
3040typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02003042 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003044} listiterobject;
3045
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003046static void listiter_dealloc(listiterobject *);
3047static int listiter_traverse(listiterobject *, visitproc, void *);
3048static PyObject *listiter_next(listiterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303049static PyObject *listiter_len(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003050static PyObject *listiter_reduce_general(void *_it, int forward);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303051static PyObject *listiter_reduce(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003052static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00003053
Armin Rigof5b3e362006-02-11 21:32:43 +00003054PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003055PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3056PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003057
3058static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003060 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3061 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00003063};
3064
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003065PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3067 "list_iterator", /* tp_name */
3068 sizeof(listiterobject), /* tp_basicsize */
3069 0, /* tp_itemsize */
3070 /* methods */
3071 (destructor)listiter_dealloc, /* tp_dealloc */
3072 0, /* tp_print */
3073 0, /* tp_getattr */
3074 0, /* tp_setattr */
3075 0, /* tp_reserved */
3076 0, /* tp_repr */
3077 0, /* tp_as_number */
3078 0, /* tp_as_sequence */
3079 0, /* tp_as_mapping */
3080 0, /* tp_hash */
3081 0, /* tp_call */
3082 0, /* tp_str */
3083 PyObject_GenericGetAttr, /* tp_getattro */
3084 0, /* tp_setattro */
3085 0, /* tp_as_buffer */
3086 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3087 0, /* tp_doc */
3088 (traverseproc)listiter_traverse, /* tp_traverse */
3089 0, /* tp_clear */
3090 0, /* tp_richcompare */
3091 0, /* tp_weaklistoffset */
3092 PyObject_SelfIter, /* tp_iter */
3093 (iternextfunc)listiter_next, /* tp_iternext */
3094 listiter_methods, /* tp_methods */
3095 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003096};
Raymond Hettinger1021c442003-11-07 15:38:09 +00003097
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003098
3099static PyObject *
3100list_iter(PyObject *seq)
3101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 if (!PyList_Check(seq)) {
3105 PyErr_BadInternalCall();
3106 return NULL;
3107 }
3108 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3109 if (it == NULL)
3110 return NULL;
3111 it->it_index = 0;
3112 Py_INCREF(seq);
3113 it->it_seq = (PyListObject *)seq;
3114 _PyObject_GC_TRACK(it);
3115 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003116}
3117
3118static void
3119listiter_dealloc(listiterobject *it)
3120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 _PyObject_GC_UNTRACK(it);
3122 Py_XDECREF(it->it_seq);
3123 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003124}
3125
3126static int
3127listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 Py_VISIT(it->it_seq);
3130 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003131}
3132
3133static PyObject *
3134listiter_next(listiterobject *it)
3135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 PyListObject *seq;
3137 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 assert(it != NULL);
3140 seq = it->it_seq;
3141 if (seq == NULL)
3142 return NULL;
3143 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 if (it->it_index < PyList_GET_SIZE(seq)) {
3146 item = PyList_GET_ITEM(seq, it->it_index);
3147 ++it->it_index;
3148 Py_INCREF(item);
3149 return item;
3150 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003153 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003155}
3156
3157static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303158listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 Py_ssize_t len;
3161 if (it->it_seq) {
3162 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3163 if (len >= 0)
3164 return PyLong_FromSsize_t(len);
3165 }
3166 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003167}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003168
3169static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303170listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003171{
3172 return listiter_reduce_general(it, 1);
3173}
3174
3175static PyObject *
3176listiter_setstate(listiterobject *it, PyObject *state)
3177{
Victor Stinner7660b882013-06-24 23:59:24 +02003178 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003179 if (index == -1 && PyErr_Occurred())
3180 return NULL;
3181 if (it->it_seq != NULL) {
3182 if (index < 0)
3183 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003184 else if (index > PyList_GET_SIZE(it->it_seq))
3185 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003186 it->it_index = index;
3187 }
3188 Py_RETURN_NONE;
3189}
3190
Raymond Hettinger1021c442003-11-07 15:38:09 +00003191/*********************** List Reverse Iterator **************************/
3192
3193typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 PyObject_HEAD
3195 Py_ssize_t it_index;
3196 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00003197} listreviterobject;
3198
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003199static void listreviter_dealloc(listreviterobject *);
3200static int listreviter_traverse(listreviterobject *, visitproc, void *);
3201static PyObject *listreviter_next(listreviterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303202static PyObject *listreviter_len(listreviterobject *, PyObject *);
3203static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003204static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003205
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003206static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003208 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3209 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003211};
3212
Raymond Hettinger1021c442003-11-07 15:38:09 +00003213PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3215 "list_reverseiterator", /* tp_name */
3216 sizeof(listreviterobject), /* tp_basicsize */
3217 0, /* tp_itemsize */
3218 /* methods */
3219 (destructor)listreviter_dealloc, /* tp_dealloc */
3220 0, /* tp_print */
3221 0, /* tp_getattr */
3222 0, /* tp_setattr */
3223 0, /* tp_reserved */
3224 0, /* tp_repr */
3225 0, /* tp_as_number */
3226 0, /* tp_as_sequence */
3227 0, /* tp_as_mapping */
3228 0, /* tp_hash */
3229 0, /* tp_call */
3230 0, /* tp_str */
3231 PyObject_GenericGetAttr, /* tp_getattro */
3232 0, /* tp_setattro */
3233 0, /* tp_as_buffer */
3234 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3235 0, /* tp_doc */
3236 (traverseproc)listreviter_traverse, /* tp_traverse */
3237 0, /* tp_clear */
3238 0, /* tp_richcompare */
3239 0, /* tp_weaklistoffset */
3240 PyObject_SelfIter, /* tp_iter */
3241 (iternextfunc)listreviter_next, /* tp_iternext */
3242 listreviter_methods, /* tp_methods */
3243 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00003244};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003245
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003246/*[clinic input]
3247list.__reversed__
3248
3249Return a reverse iterator over the list.
3250[clinic start generated code]*/
3251
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003252static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003253list___reversed___impl(PyListObject *self)
3254/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3259 if (it == NULL)
3260 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003261 assert(PyList_Check(self));
3262 it->it_index = PyList_GET_SIZE(self) - 1;
3263 Py_INCREF(self);
3264 it->it_seq = self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 PyObject_GC_Track(it);
3266 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003267}
3268
3269static void
3270listreviter_dealloc(listreviterobject *it)
3271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 PyObject_GC_UnTrack(it);
3273 Py_XDECREF(it->it_seq);
3274 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003275}
3276
3277static int
3278listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 Py_VISIT(it->it_seq);
3281 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003282}
3283
3284static PyObject *
3285listreviter_next(listreviterobject *it)
3286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003288 Py_ssize_t index;
3289 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003290
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003291 assert(it != NULL);
3292 seq = it->it_seq;
3293 if (seq == NULL) {
3294 return NULL;
3295 }
3296 assert(PyList_Check(seq));
3297
3298 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 if (index>=0 && index < PyList_GET_SIZE(seq)) {
3300 item = PyList_GET_ITEM(seq, index);
3301 it->it_index--;
3302 Py_INCREF(item);
3303 return item;
3304 }
3305 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003306 it->it_seq = NULL;
3307 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003309}
3310
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003311static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303312listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 Py_ssize_t len = it->it_index + 1;
3315 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3316 len = 0;
3317 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003318}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003319
3320static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303321listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003322{
3323 return listiter_reduce_general(it, 0);
3324}
3325
3326static PyObject *
3327listreviter_setstate(listreviterobject *it, PyObject *state)
3328{
3329 Py_ssize_t index = PyLong_AsSsize_t(state);
3330 if (index == -1 && PyErr_Occurred())
3331 return NULL;
3332 if (it->it_seq != NULL) {
3333 if (index < -1)
3334 index = -1;
3335 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3336 index = PyList_GET_SIZE(it->it_seq) - 1;
3337 it->it_index = index;
3338 }
3339 Py_RETURN_NONE;
3340}
3341
3342/* common pickling support */
3343
3344static PyObject *
3345listiter_reduce_general(void *_it, int forward)
3346{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003347 _Py_IDENTIFIER(iter);
3348 _Py_IDENTIFIER(reversed);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003349 PyObject *list;
3350
3351 /* the objects are not the same, index is of different types! */
3352 if (forward) {
3353 listiterobject *it = (listiterobject *)_it;
3354 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003355 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003356 it->it_seq, it->it_index);
3357 } else {
3358 listreviterobject *it = (listreviterobject *)_it;
3359 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003360 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_reversed),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003361 it->it_seq, it->it_index);
3362 }
3363 /* empty iterator, create an empty list */
3364 list = PyList_New(0);
3365 if (list == NULL)
3366 return NULL;
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003367 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003368}