blob: cbd6e81ea472e6381f930b57c40f9dfc4e584d8b [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;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500480 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 if (np == NULL)
482 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 src = a->ob_item + ilow;
485 dest = np->ob_item;
486 for (i = 0; i < len; i++) {
487 PyObject *v = src[i];
488 Py_INCREF(v);
489 dest[i] = v;
490 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500491 Py_SIZE(np) = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493}
494
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000495PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000496PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 if (!PyList_Check(a)) {
499 PyErr_BadInternalCall();
500 return NULL;
501 }
Sergey Fedoseevef1b88b2019-02-21 11:51:52 +0500502 if (ilow < 0) {
503 ilow = 0;
504 }
505 else if (ilow > Py_SIZE(a)) {
506 ilow = Py_SIZE(a);
507 }
508 if (ihigh < ilow) {
509 ihigh = ilow;
510 }
511 else if (ihigh > Py_SIZE(a)) {
512 ihigh = Py_SIZE(a);
513 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000515}
516
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000517static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000518list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 Py_ssize_t size;
521 Py_ssize_t i;
522 PyObject **src, **dest;
523 PyListObject *np;
524 if (!PyList_Check(bb)) {
525 PyErr_Format(PyExc_TypeError,
526 "can only concatenate list (not \"%.200s\") to list",
527 bb->ob_type->tp_name);
528 return NULL;
529 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000531 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000533 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500534 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 if (np == NULL) {
536 return NULL;
537 }
538 src = a->ob_item;
539 dest = np->ob_item;
540 for (i = 0; i < Py_SIZE(a); i++) {
541 PyObject *v = src[i];
542 Py_INCREF(v);
543 dest[i] = v;
544 }
545 src = b->ob_item;
546 dest = np->ob_item + Py_SIZE(a);
547 for (i = 0; i < Py_SIZE(b); i++) {
548 PyObject *v = src[i];
549 Py_INCREF(v);
550 dest[i] = v;
551 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500552 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554#undef b
555}
556
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000557static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000558list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 Py_ssize_t i, j;
561 Py_ssize_t size;
562 PyListObject *np;
563 PyObject **p, **items;
564 PyObject *elem;
565 if (n < 0)
566 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100567 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100569 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 if (size == 0)
571 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500572 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 if (np == NULL)
574 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500577 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 elem = a->ob_item[0];
579 for (i = 0; i < n; i++) {
580 items[i] = elem;
581 Py_INCREF(elem);
582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500584 else {
585 p = np->ob_item;
586 items = a->ob_item;
587 for (i = 0; i < n; i++) {
588 for (j = 0; j < Py_SIZE(a); j++) {
589 *p = items[j];
590 Py_INCREF(*p);
591 p++;
592 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 }
594 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500595 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000597}
598
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000599static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200600_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 Py_ssize_t i;
603 PyObject **item = a->ob_item;
604 if (item != NULL) {
605 /* Because XDECREF can recursively invoke operations on
606 this list, we make it empty first. */
607 i = Py_SIZE(a);
608 Py_SIZE(a) = 0;
609 a->ob_item = NULL;
610 a->allocated = 0;
611 while (--i >= 0) {
612 Py_XDECREF(item[i]);
613 }
614 PyMem_FREE(item);
615 }
616 /* Never fails; the return value can be ignored.
617 Note that there is no guarantee that the list is actually empty
618 at this point, because XDECREF may have populated it again! */
619 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000620}
621
Tim Peters8fc4a912004-07-31 21:53:19 +0000622/* a[ilow:ihigh] = v if v != NULL.
623 * del a[ilow:ihigh] if v == NULL.
624 *
625 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
626 * guaranteed the call cannot fail.
627 */
Armin Rigo93677f02004-07-29 12:40:23 +0000628static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000629list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 /* Because [X]DECREF can recursively invoke list operations on
632 this list, we must postpone all [X]DECREF activity until
633 after the list is back in its canonical shape. Therefore
634 we must allocate an additional array, 'recycle', into which
635 we temporarily copy the items that are deleted from the
636 list. :-( */
637 PyObject *recycle_on_stack[8];
638 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
639 PyObject **item;
640 PyObject **vitem = NULL;
641 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
642 Py_ssize_t n; /* # of elements in replacement list */
643 Py_ssize_t norig; /* # of elements in list getting replaced */
644 Py_ssize_t d; /* Change in size */
645 Py_ssize_t k;
646 size_t s;
647 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000648#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 if (v == NULL)
650 n = 0;
651 else {
652 if (a == b) {
653 /* Special case "a[i:j] = a" -- copy b first */
654 v = list_slice(b, 0, Py_SIZE(b));
655 if (v == NULL)
656 return result;
657 result = list_ass_slice(a, ilow, ihigh, v);
658 Py_DECREF(v);
659 return result;
660 }
661 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
662 if(v_as_SF == NULL)
663 goto Error;
664 n = PySequence_Fast_GET_SIZE(v_as_SF);
665 vitem = PySequence_Fast_ITEMS(v_as_SF);
666 }
667 if (ilow < 0)
668 ilow = 0;
669 else if (ilow > Py_SIZE(a))
670 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 if (ihigh < ilow)
673 ihigh = ilow;
674 else if (ihigh > Py_SIZE(a))
675 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 norig = ihigh - ilow;
678 assert(norig >= 0);
679 d = n - norig;
680 if (Py_SIZE(a) + d == 0) {
681 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200682 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 }
684 item = a->ob_item;
685 /* recycle the items that we are about to remove */
686 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700687 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
688 if (s) {
689 if (s > sizeof(recycle_on_stack)) {
690 recycle = (PyObject **)PyMem_MALLOC(s);
691 if (recycle == NULL) {
692 PyErr_NoMemory();
693 goto Error;
694 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700696 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200700 Py_ssize_t tail;
701 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
702 memmove(&item[ihigh+d], &item[ihigh], tail);
703 if (list_resize(a, Py_SIZE(a) + d) < 0) {
704 memmove(&item[ihigh], &item[ihigh+d], tail);
705 memcpy(&item[ilow], recycle, s);
706 goto Error;
707 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 item = a->ob_item;
709 }
710 else if (d > 0) { /* Insert d items */
711 k = Py_SIZE(a);
712 if (list_resize(a, k+d) < 0)
713 goto Error;
714 item = a->ob_item;
715 memmove(&item[ihigh+d], &item[ihigh],
716 (k - ihigh)*sizeof(PyObject *));
717 }
718 for (k = 0; k < n; k++, ilow++) {
719 PyObject *w = vitem[k];
720 Py_XINCREF(w);
721 item[ilow] = w;
722 }
723 for (k = norig - 1; k >= 0; --k)
724 Py_XDECREF(recycle[k]);
725 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000726 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 if (recycle != recycle_on_stack)
728 PyMem_FREE(recycle);
729 Py_XDECREF(v_as_SF);
730 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000731#undef b
732}
733
Guido van Rossum234f9421993-06-17 12:35:49 +0000734int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000735PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 if (!PyList_Check(a)) {
738 PyErr_BadInternalCall();
739 return -1;
740 }
741 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000742}
743
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000744static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000745list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 PyObject **items;
748 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000749
750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 size = PyList_GET_SIZE(self);
752 if (size == 0 || n == 1) {
753 Py_INCREF(self);
754 return (PyObject *)self;
755 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 if (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200758 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 Py_INCREF(self);
760 return (PyObject *)self;
761 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 if (size > PY_SSIZE_T_MAX / n) {
764 return PyErr_NoMemory();
765 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000766
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800767 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 p = size;
771 items = self->ob_item;
772 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
773 for (j = 0; j < size; j++) {
774 PyObject *o = items[j];
775 Py_INCREF(o);
776 items[p++] = o;
777 }
778 }
779 Py_INCREF(self);
780 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000781}
782
Guido van Rossum4a450d01991-04-03 19:05:18 +0000783static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000784list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000785{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700786 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 PyErr_SetString(PyExc_IndexError,
788 "list assignment index out of range");
789 return -1;
790 }
791 if (v == NULL)
792 return list_ass_slice(a, i, i+1, v);
793 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300794 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000796}
797
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200798/*[clinic input]
799list.insert
800
801 index: Py_ssize_t
802 object: object
803 /
804
805Insert object before index.
806[clinic start generated code]*/
807
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000808static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200809list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
810/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000811{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200812 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 Py_RETURN_NONE;
814 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000815}
816
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200817/*[clinic input]
818list.clear
819
820Remove all items from list.
821[clinic start generated code]*/
822
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000823static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200824list_clear_impl(PyListObject *self)
825/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000826{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200827 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000828 Py_RETURN_NONE;
829}
830
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200831/*[clinic input]
832list.copy
833
834Return a shallow copy of the list.
835[clinic start generated code]*/
836
Eli Benderskycbbaa962011-02-25 05:47:53 +0000837static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200838list_copy_impl(PyListObject *self)
839/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000840{
841 return list_slice(self, 0, Py_SIZE(self));
842}
843
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200844/*[clinic input]
845list.append
846
847 object: object
848 /
849
850Append object to the end of the list.
851[clinic start generated code]*/
852
Eli Benderskycbbaa962011-02-25 05:47:53 +0000853static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200854list_append(PyListObject *self, PyObject *object)
855/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000856{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200857 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 Py_RETURN_NONE;
859 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000860}
861
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200862/*[clinic input]
863list.extend
864
865 iterable: object
866 /
867
868Extend list by appending elements from the iterable.
869[clinic start generated code]*/
870
Barry Warsawdedf6d61998-10-09 16:37:25 +0000871static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200872list_extend(PyListObject *self, PyObject *iterable)
873/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000874{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 PyObject *it; /* iter(v) */
876 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200877 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 Py_ssize_t mn; /* m + n */
879 Py_ssize_t i;
880 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 /* Special cases:
883 1) lists and tuples which can use PySequence_Fast ops
884 2) extending self to self requires making a copy first
885 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200886 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
887 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200889 iterable = PySequence_Fast(iterable, "argument must be iterable");
890 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200892 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200894 /* short circuit when iterable is empty */
895 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 Py_RETURN_NONE;
897 }
898 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000899 /* It should not be possible to allocate a list large enough to cause
900 an overflow on any relevant platform */
901 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800902 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200903 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 return NULL;
905 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200906 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 * situation a.extend(a), but the following code works
908 * in that case too. Just make sure to resize self
909 * before calling PySequence_Fast_ITEMS.
910 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200911 /* populate the end of self with iterable's items */
912 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 dest = self->ob_item + m;
914 for (i = 0; i < n; i++) {
915 PyObject *o = src[i];
916 Py_INCREF(o);
917 dest[i] = o;
918 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200919 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 Py_RETURN_NONE;
921 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000922
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200923 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 if (it == NULL)
925 return NULL;
926 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200929 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800930 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 Py_DECREF(it);
932 return NULL;
933 }
934 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000935 if (m > PY_SSIZE_T_MAX - n) {
936 /* m + n overflowed; on the chance that n lied, and there really
937 * is enough room, ignore it. If n was telling the truth, we'll
938 * eventually run out of memory during the loop.
939 */
940 }
941 else {
942 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800944 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 goto error;
946 /* Make the list sane again. */
947 Py_SIZE(self) = m;
948 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 /* Run iterator to exhaustion. */
951 for (;;) {
952 PyObject *item = iternext(it);
953 if (item == NULL) {
954 if (PyErr_Occurred()) {
955 if (PyErr_ExceptionMatches(PyExc_StopIteration))
956 PyErr_Clear();
957 else
958 goto error;
959 }
960 break;
961 }
962 if (Py_SIZE(self) < self->allocated) {
963 /* steals ref */
964 PyList_SET_ITEM(self, Py_SIZE(self), item);
965 ++Py_SIZE(self);
966 }
967 else {
968 int status = app1(self, item);
969 Py_DECREF(item); /* append creates a new ref */
970 if (status < 0)
971 goto error;
972 }
973 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200976 if (Py_SIZE(self) < self->allocated) {
977 if (list_resize(self, Py_SIZE(self)) < 0)
978 goto error;
979 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 Py_DECREF(it);
982 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000983
984 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 Py_DECREF(it);
986 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000987}
988
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000989PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200990_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000991{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200992 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000993}
994
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000995static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000996list_inplace_concat(PyListObject *self, PyObject *other)
997{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000999
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001000 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 if (result == NULL)
1002 return result;
1003 Py_DECREF(result);
1004 Py_INCREF(self);
1005 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001006}
1007
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001008/*[clinic input]
1009list.pop
1010
1011 index: Py_ssize_t = -1
1012 /
1013
1014Remove and return item at index (default last).
1015
1016Raises IndexError if list is empty or index is out of range.
1017[clinic start generated code]*/
1018
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001019static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001020list_pop_impl(PyListObject *self, Py_ssize_t index)
1021/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 PyObject *v;
1024 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (Py_SIZE(self) == 0) {
1027 /* Special-case most common failure cause */
1028 PyErr_SetString(PyExc_IndexError, "pop from empty list");
1029 return NULL;
1030 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001031 if (index < 0)
1032 index += Py_SIZE(self);
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -07001033 if (!valid_index(index, Py_SIZE(self))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1035 return NULL;
1036 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001037 v = self->ob_item[index];
1038 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001040 if (status >= 0)
1041 return v; /* and v now owns the reference the list had */
1042 else
1043 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 }
1045 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001046 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001047 if (status < 0) {
1048 Py_DECREF(v);
1049 return NULL;
1050 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001052}
1053
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001054/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1055static void
1056reverse_slice(PyObject **lo, PyObject **hi)
1057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 --hi;
1061 while (lo < hi) {
1062 PyObject *t = *lo;
1063 *lo = *hi;
1064 *hi = t;
1065 ++lo;
1066 --hi;
1067 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001068}
1069
Tim Petersa64dc242002-08-01 02:13:36 +00001070/* Lots of code for an adaptive, stable, natural mergesort. There are many
1071 * pieces to this algorithm; read listsort.txt for overviews and details.
1072 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001073
Daniel Stutzbach98338222010-12-02 21:55:33 +00001074/* A sortslice contains a pointer to an array of keys and a pointer to
1075 * an array of corresponding values. In other words, keys[i]
1076 * corresponds with values[i]. If values == NULL, then the keys are
1077 * also the values.
1078 *
1079 * Several convenience routines are provided here, so that keys and
1080 * values are always moved in sync.
1081 */
1082
1083typedef struct {
1084 PyObject **keys;
1085 PyObject **values;
1086} sortslice;
1087
1088Py_LOCAL_INLINE(void)
1089sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1090{
1091 s1->keys[i] = s2->keys[j];
1092 if (s1->values != NULL)
1093 s1->values[i] = s2->values[j];
1094}
1095
1096Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001097sortslice_copy_incr(sortslice *dst, sortslice *src)
1098{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001099 *dst->keys++ = *src->keys++;
1100 if (dst->values != NULL)
1101 *dst->values++ = *src->values++;
1102}
1103
1104Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001105sortslice_copy_decr(sortslice *dst, sortslice *src)
1106{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001107 *dst->keys-- = *src->keys--;
1108 if (dst->values != NULL)
1109 *dst->values-- = *src->values--;
1110}
1111
1112
1113Py_LOCAL_INLINE(void)
1114sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001115 Py_ssize_t n)
1116{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001117 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1118 if (s1->values != NULL)
1119 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1120}
1121
1122Py_LOCAL_INLINE(void)
1123sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001124 Py_ssize_t n)
1125{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001126 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1127 if (s1->values != NULL)
1128 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1129}
1130
1131Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001132sortslice_advance(sortslice *slice, Py_ssize_t n)
1133{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001134 slice->keys += n;
1135 if (slice->values != NULL)
1136 slice->values += n;
1137}
1138
embg1e34da42018-01-28 20:03:23 -07001139/* Comparison function: ms->key_compare, which is set at run-time in
1140 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001141 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1142 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001143
embg1e34da42018-01-28 20:03:23 -07001144#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001145
1146/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001147 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1148 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1149*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001150#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001152
embg1e34da42018-01-28 20:03:23 -07001153/* The maximum number of entries in a MergeState's pending-runs stack.
1154 * This is enough to sort arrays of size up to about
1155 * 32 * phi ** MAX_MERGE_PENDING
1156 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1157 * with 2**64 elements.
1158 */
1159#define MAX_MERGE_PENDING 85
1160
1161/* When we get into galloping mode, we stay there until both runs win less
1162 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1163 */
1164#define MIN_GALLOP 7
1165
1166/* Avoid malloc for small temp arrays. */
1167#define MERGESTATE_TEMP_SIZE 256
1168
1169/* One MergeState exists on the stack per invocation of mergesort. It's just
1170 * a convenient way to pass state around among the helper functions.
1171 */
1172struct s_slice {
1173 sortslice base;
1174 Py_ssize_t len;
1175};
1176
1177typedef struct s_MergeState MergeState;
1178struct s_MergeState {
1179 /* This controls when we get *into* galloping mode. It's initialized
1180 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1181 * random data, and lower for highly structured data.
1182 */
1183 Py_ssize_t min_gallop;
1184
1185 /* 'a' is temp storage to help with merges. It contains room for
1186 * alloced entries.
1187 */
1188 sortslice a; /* may point to temparray below */
1189 Py_ssize_t alloced;
1190
1191 /* A stack of n pending runs yet to be merged. Run #i starts at
1192 * address base[i] and extends for len[i] elements. It's always
1193 * true (so long as the indices are in bounds) that
1194 *
1195 * pending[i].base + pending[i].len == pending[i+1].base
1196 *
1197 * so we could cut the storage for this, but it's a minor amount,
1198 * and keeping all the info explicit simplifies the code.
1199 */
1200 int n;
1201 struct s_slice pending[MAX_MERGE_PENDING];
1202
1203 /* 'a' points to this when possible, rather than muck with malloc. */
1204 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1205
1206 /* This is the function we will use to compare two keys,
1207 * even when none of our special cases apply and we have to use
1208 * safe_object_compare. */
1209 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1210
1211 /* This function is used by unsafe_object_compare to optimize comparisons
1212 * when we know our list is type-homogeneous but we can't assume anything else.
1213 * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1214 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1215
1216 /* This function is used by unsafe_tuple_compare to compare the first elements
1217 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1218 * we can assume more, and use one of the special-case compares. */
1219 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1220};
1221
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001222/* binarysort is the best method for sorting small arrays: it does
1223 few compares, but can do data movement quadratic in the number of
1224 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001225 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001226 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001227 On entry, must have lo <= start <= hi, and that [lo, start) is already
1228 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001229 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001230 Even in case of error, the output slice will be some permutation of
1231 the input (nothing is lost or duplicated).
1232*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001233static int
embg1e34da42018-01-28 20:03:23 -07001234binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001235{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001236 Py_ssize_t k;
1237 PyObject **l, **p, **r;
1238 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001239
Daniel Stutzbach98338222010-12-02 21:55:33 +00001240 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001242 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 ++start;
1244 for (; start < hi; ++start) {
1245 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001246 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 r = start;
1248 pivot = *r;
1249 /* Invariants:
1250 * pivot >= all in [lo, l).
1251 * pivot < all in [r, start).
1252 * The second is vacuously true at the start.
1253 */
1254 assert(l < r);
1255 do {
1256 p = l + ((r - l) >> 1);
1257 IFLT(pivot, *p)
1258 r = p;
1259 else
1260 l = p+1;
1261 } while (l < r);
1262 assert(l == r);
1263 /* The invariants still hold, so pivot >= all in [lo, l) and
1264 pivot < all in [l, start), so pivot belongs at l. Note
1265 that if there are elements equal to pivot, l points to the
1266 first slot after them -- that's why this sort is stable.
1267 Slide over to make room.
1268 Caution: using memmove is much slower under MSVC 5;
1269 we're not usually moving many slots. */
1270 for (p = start; p > l; --p)
1271 *p = *(p-1);
1272 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001273 if (lo.values != NULL) {
1274 Py_ssize_t offset = lo.values - lo.keys;
1275 p = start + offset;
1276 pivot = *p;
1277 l += offset;
1278 for (p = start + offset; p > l; --p)
1279 *p = *(p-1);
1280 *l = pivot;
1281 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 }
1283 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001284
1285 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001287}
1288
Tim Petersa64dc242002-08-01 02:13:36 +00001289/*
1290Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1291is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001292
Tim Petersa64dc242002-08-01 02:13:36 +00001293 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001294
Tim Petersa64dc242002-08-01 02:13:36 +00001295or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001296
Tim Petersa64dc242002-08-01 02:13:36 +00001297 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001298
Tim Petersa64dc242002-08-01 02:13:36 +00001299Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1300For its intended use in a stable mergesort, the strictness of the defn of
1301"descending" is needed so that the caller can safely reverse a descending
1302sequence without violating stability (strict > ensures there are no equal
1303elements to get out of order).
1304
1305Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001306*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001307static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001308count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 Py_ssize_t k;
1311 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 assert(lo < hi);
1314 *descending = 0;
1315 ++lo;
1316 if (lo == hi)
1317 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 n = 2;
1320 IFLT(*lo, *(lo-1)) {
1321 *descending = 1;
1322 for (lo = lo+1; lo < hi; ++lo, ++n) {
1323 IFLT(*lo, *(lo-1))
1324 ;
1325 else
1326 break;
1327 }
1328 }
1329 else {
1330 for (lo = lo+1; lo < hi; ++lo, ++n) {
1331 IFLT(*lo, *(lo-1))
1332 break;
1333 }
1334 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001337fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001339}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001340
Tim Petersa64dc242002-08-01 02:13:36 +00001341/*
1342Locate the proper position of key in a sorted vector; if the vector contains
1343an element equal to key, return the position immediately to the left of
1344the leftmost equal element. [gallop_right() does the same except returns
1345the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001346
Tim Petersa64dc242002-08-01 02:13:36 +00001347"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001348
Tim Petersa64dc242002-08-01 02:13:36 +00001349"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1350hint is to the final result, the faster this runs.
1351
1352The return value is the int k in 0..n such that
1353
1354 a[k-1] < key <= a[k]
1355
1356pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1357key belongs at index k; or, IOW, the first k elements of a should precede
1358key, and the last n-k should follow key.
1359
1360Returns -1 on error. See listsort.txt for info on the method.
1361*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001362static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001363gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 Py_ssize_t ofs;
1366 Py_ssize_t lastofs;
1367 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 a += hint;
1372 lastofs = 0;
1373 ofs = 1;
1374 IFLT(*a, key) {
1375 /* a[hint] < key -- gallop right, until
1376 * a[hint + lastofs] < key <= a[hint + ofs]
1377 */
1378 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1379 while (ofs < maxofs) {
1380 IFLT(a[ofs], key) {
1381 lastofs = ofs;
1382 ofs = (ofs << 1) + 1;
1383 if (ofs <= 0) /* int overflow */
1384 ofs = maxofs;
1385 }
1386 else /* key <= a[hint + ofs] */
1387 break;
1388 }
1389 if (ofs > maxofs)
1390 ofs = maxofs;
1391 /* Translate back to offsets relative to &a[0]. */
1392 lastofs += hint;
1393 ofs += hint;
1394 }
1395 else {
1396 /* key <= a[hint] -- gallop left, until
1397 * a[hint - ofs] < key <= a[hint - lastofs]
1398 */
1399 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1400 while (ofs < maxofs) {
1401 IFLT(*(a-ofs), key)
1402 break;
1403 /* key <= a[hint - ofs] */
1404 lastofs = ofs;
1405 ofs = (ofs << 1) + 1;
1406 if (ofs <= 0) /* int overflow */
1407 ofs = maxofs;
1408 }
1409 if (ofs > maxofs)
1410 ofs = maxofs;
1411 /* Translate back to positive offsets relative to &a[0]. */
1412 k = lastofs;
1413 lastofs = hint - ofs;
1414 ofs = hint - k;
1415 }
1416 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1419 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1420 * right of lastofs but no farther right than ofs. Do a binary
1421 * search, with invariant a[lastofs-1] < key <= a[ofs].
1422 */
1423 ++lastofs;
1424 while (lastofs < ofs) {
1425 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 IFLT(a[m], key)
1428 lastofs = m+1; /* a[m] < key */
1429 else
1430 ofs = m; /* key <= a[m] */
1431 }
1432 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1433 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001434
1435fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001437}
1438
1439/*
1440Exactly like gallop_left(), except that if key already exists in a[0:n],
1441finds the position immediately to the right of the rightmost equal value.
1442
1443The return value is the int k in 0..n such that
1444
1445 a[k-1] <= key < a[k]
1446
1447or -1 if error.
1448
1449The code duplication is massive, but this is enough different given that
1450we're sticking to "<" comparisons that it's much harder to follow if
1451written as one routine with yet another "left or right?" flag.
1452*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001453static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001454gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 Py_ssize_t ofs;
1457 Py_ssize_t lastofs;
1458 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 a += hint;
1463 lastofs = 0;
1464 ofs = 1;
1465 IFLT(key, *a) {
1466 /* key < a[hint] -- gallop left, until
1467 * a[hint - ofs] <= key < a[hint - lastofs]
1468 */
1469 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1470 while (ofs < maxofs) {
1471 IFLT(key, *(a-ofs)) {
1472 lastofs = ofs;
1473 ofs = (ofs << 1) + 1;
1474 if (ofs <= 0) /* int overflow */
1475 ofs = maxofs;
1476 }
1477 else /* a[hint - ofs] <= key */
1478 break;
1479 }
1480 if (ofs > maxofs)
1481 ofs = maxofs;
1482 /* Translate back to positive offsets relative to &a[0]. */
1483 k = lastofs;
1484 lastofs = hint - ofs;
1485 ofs = hint - k;
1486 }
1487 else {
1488 /* a[hint] <= key -- gallop right, until
1489 * a[hint + lastofs] <= key < a[hint + ofs]
1490 */
1491 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1492 while (ofs < maxofs) {
1493 IFLT(key, a[ofs])
1494 break;
1495 /* a[hint + ofs] <= key */
1496 lastofs = ofs;
1497 ofs = (ofs << 1) + 1;
1498 if (ofs <= 0) /* int overflow */
1499 ofs = maxofs;
1500 }
1501 if (ofs > maxofs)
1502 ofs = maxofs;
1503 /* Translate back to offsets relative to &a[0]. */
1504 lastofs += hint;
1505 ofs += hint;
1506 }
1507 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1510 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1511 * right of lastofs but no farther right than ofs. Do a binary
1512 * search, with invariant a[lastofs-1] <= key < a[ofs].
1513 */
1514 ++lastofs;
1515 while (lastofs < ofs) {
1516 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 IFLT(key, a[m])
1519 ofs = m; /* key < a[m] */
1520 else
1521 lastofs = m+1; /* a[m] <= key */
1522 }
1523 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1524 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001525
1526fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001528}
1529
Tim Petersa64dc242002-08-01 02:13:36 +00001530/* Conceptually a MergeState's constructor. */
1531static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001532merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001535 if (has_keyfunc) {
1536 /* The temporary space for merging will need at most half the list
1537 * size rounded up. Use the minimum possible space so we can use the
1538 * rest of temparray for other things. In particular, if there is
1539 * enough extra space, listsort() will use it to store the keys.
1540 */
1541 ms->alloced = (list_size + 1) / 2;
1542
1543 /* ms->alloced describes how many keys will be stored at
1544 ms->temparray, but we also need to store the values. Hence,
1545 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1546 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1547 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1548 ms->a.values = &ms->temparray[ms->alloced];
1549 }
1550 else {
1551 ms->alloced = MERGESTATE_TEMP_SIZE;
1552 ms->a.values = NULL;
1553 }
1554 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 ms->n = 0;
1556 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001557}
1558
1559/* Free all the temp memory owned by the MergeState. This must be called
1560 * when you're done with a MergeState, and may be called before then if
1561 * you want to free the temp memory early.
1562 */
1563static void
1564merge_freemem(MergeState *ms)
1565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001567 if (ms->a.keys != ms->temparray)
1568 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001569}
1570
1571/* Ensure enough temp memory for 'need' array slots is available.
1572 * Returns 0 on success and -1 if the memory can't be gotten.
1573 */
1574static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001575merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001576{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001577 int multiplier;
1578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 assert(ms != NULL);
1580 if (need <= ms->alloced)
1581 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001582
1583 multiplier = ms->a.values != NULL ? 2 : 1;
1584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 /* Don't realloc! That can cost cycles to copy the old data, but
1586 * we don't care what's in the block.
1587 */
1588 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001589 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 PyErr_NoMemory();
1591 return -1;
1592 }
embg1e34da42018-01-28 20:03:23 -07001593 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001594 * sizeof(PyObject *));
1595 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001597 if (ms->a.values != NULL)
1598 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 return 0;
1600 }
1601 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001603}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1605 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001606
Daniel Stutzbach98338222010-12-02 21:55:33 +00001607/* Merge the na elements starting at ssa with the nb elements starting at
1608 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1609 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1610 * should have na <= nb. See listsort.txt for more info. Return 0 if
1611 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001612 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001613static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001614merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1615 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001618 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 int result = -1; /* guilty until proved innocent */
1620 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001621
Daniel Stutzbach98338222010-12-02 21:55:33 +00001622 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1623 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 if (MERGE_GETMEM(ms, na) < 0)
1625 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001626 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1627 dest = ssa;
1628 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001629
Daniel Stutzbach98338222010-12-02 21:55:33 +00001630 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 --nb;
1632 if (nb == 0)
1633 goto Succeed;
1634 if (na == 1)
1635 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 min_gallop = ms->min_gallop;
1638 for (;;) {
1639 Py_ssize_t acount = 0; /* # of times A won in a row */
1640 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 /* Do the straightforward thing until (if ever) one run
1643 * appears to win consistently.
1644 */
1645 for (;;) {
1646 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001647 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (k) {
1649 if (k < 0)
1650 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001651 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 ++bcount;
1653 acount = 0;
1654 --nb;
1655 if (nb == 0)
1656 goto Succeed;
1657 if (bcount >= min_gallop)
1658 break;
1659 }
1660 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001661 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 ++acount;
1663 bcount = 0;
1664 --na;
1665 if (na == 1)
1666 goto CopyB;
1667 if (acount >= min_gallop)
1668 break;
1669 }
1670 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 /* One run is winning so consistently that galloping may
1673 * be a huge win. So try that, and continue galloping until
1674 * (if ever) neither run appears to be winning consistently
1675 * anymore.
1676 */
1677 ++min_gallop;
1678 do {
1679 assert(na > 1 && nb > 0);
1680 min_gallop -= min_gallop > 1;
1681 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001682 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 acount = k;
1684 if (k) {
1685 if (k < 0)
1686 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001687 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1688 sortslice_advance(&dest, k);
1689 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 na -= k;
1691 if (na == 1)
1692 goto CopyB;
1693 /* na==0 is impossible now if the comparison
1694 * function is consistent, but we can't assume
1695 * that it is.
1696 */
1697 if (na == 0)
1698 goto Succeed;
1699 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001700 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 --nb;
1702 if (nb == 0)
1703 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001704
embg1e34da42018-01-28 20:03:23 -07001705 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 bcount = k;
1707 if (k) {
1708 if (k < 0)
1709 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001710 sortslice_memmove(&dest, 0, &ssb, 0, k);
1711 sortslice_advance(&dest, k);
1712 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 nb -= k;
1714 if (nb == 0)
1715 goto Succeed;
1716 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001717 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 --na;
1719 if (na == 1)
1720 goto CopyB;
1721 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1722 ++min_gallop; /* penalize it for leaving galloping mode */
1723 ms->min_gallop = min_gallop;
1724 }
Tim Petersa64dc242002-08-01 02:13:36 +00001725Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001727Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001729 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001731CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001733 /* The last element of ssa belongs at the end of the merge. */
1734 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1735 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001737}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001738
Daniel Stutzbach98338222010-12-02 21:55:33 +00001739/* Merge the na elements starting at pa with the nb elements starting at
1740 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1741 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1742 * should have na >= nb. See listsort.txt for more info. Return 0 if
1743 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001744 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001745static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001746merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1747 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001750 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001753
Daniel Stutzbach98338222010-12-02 21:55:33 +00001754 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1755 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 if (MERGE_GETMEM(ms, nb) < 0)
1757 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001758 dest = ssb;
1759 sortslice_advance(&dest, nb-1);
1760 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1761 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001763 ssb.keys = ms->a.keys + nb - 1;
1764 if (ssb.values != NULL)
1765 ssb.values = ms->a.values + nb - 1;
1766 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001767
Daniel Stutzbach98338222010-12-02 21:55:33 +00001768 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 --na;
1770 if (na == 0)
1771 goto Succeed;
1772 if (nb == 1)
1773 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 min_gallop = ms->min_gallop;
1776 for (;;) {
1777 Py_ssize_t acount = 0; /* # of times A won in a row */
1778 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 /* Do the straightforward thing until (if ever) one run
1781 * appears to win consistently.
1782 */
1783 for (;;) {
1784 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001785 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 if (k) {
1787 if (k < 0)
1788 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001789 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 ++acount;
1791 bcount = 0;
1792 --na;
1793 if (na == 0)
1794 goto Succeed;
1795 if (acount >= min_gallop)
1796 break;
1797 }
1798 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001799 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 ++bcount;
1801 acount = 0;
1802 --nb;
1803 if (nb == 1)
1804 goto CopyA;
1805 if (bcount >= min_gallop)
1806 break;
1807 }
1808 }
Tim Petersa64dc242002-08-01 02:13:36 +00001809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 /* One run is winning so consistently that galloping may
1811 * be a huge win. So try that, and continue galloping until
1812 * (if ever) neither run appears to be winning consistently
1813 * anymore.
1814 */
1815 ++min_gallop;
1816 do {
1817 assert(na > 0 && nb > 1);
1818 min_gallop -= min_gallop > 1;
1819 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001820 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 if (k < 0)
1822 goto Fail;
1823 k = na - k;
1824 acount = k;
1825 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001826 sortslice_advance(&dest, -k);
1827 sortslice_advance(&ssa, -k);
1828 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 na -= k;
1830 if (na == 0)
1831 goto Succeed;
1832 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001833 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 --nb;
1835 if (nb == 1)
1836 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001837
embg1e34da42018-01-28 20:03:23 -07001838 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 if (k < 0)
1840 goto Fail;
1841 k = nb - k;
1842 bcount = k;
1843 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001844 sortslice_advance(&dest, -k);
1845 sortslice_advance(&ssb, -k);
1846 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 nb -= k;
1848 if (nb == 1)
1849 goto CopyA;
1850 /* nb==0 is impossible now if the comparison
1851 * function is consistent, but we can't assume
1852 * that it is.
1853 */
1854 if (nb == 0)
1855 goto Succeed;
1856 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001857 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 --na;
1859 if (na == 0)
1860 goto Succeed;
1861 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1862 ++min_gallop; /* penalize it for leaving galloping mode */
1863 ms->min_gallop = min_gallop;
1864 }
Tim Petersa64dc242002-08-01 02:13:36 +00001865Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001867Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001869 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001871CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001873 /* The first element of ssb belongs at the front of the merge. */
1874 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1875 sortslice_advance(&dest, -na);
1876 sortslice_advance(&ssa, -na);
1877 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001879}
1880
1881/* Merge the two runs at stack indices i and i+1.
1882 * Returns 0 on success, -1 on error.
1883 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001884static Py_ssize_t
1885merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001886{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001887 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 Py_ssize_t na, nb;
1889 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 assert(ms != NULL);
1892 assert(ms->n >= 2);
1893 assert(i >= 0);
1894 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001895
Daniel Stutzbach98338222010-12-02 21:55:33 +00001896 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001898 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 nb = ms->pending[i+1].len;
1900 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001901 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 /* Record the length of the combined runs; if i is the 3rd-last
1904 * run now, also slide over the last run (which isn't involved
1905 * in this merge). The current run i+1 goes away in any case.
1906 */
1907 ms->pending[i].len = na + nb;
1908 if (i == ms->n - 3)
1909 ms->pending[i+1] = ms->pending[i+2];
1910 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 /* Where does b start in a? Elements in a before that can be
1913 * ignored (already in place).
1914 */
embg1e34da42018-01-28 20:03:23 -07001915 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 if (k < 0)
1917 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001918 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 na -= k;
1920 if (na == 0)
1921 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 /* Where does a end in b? Elements in b after that can be
1924 * ignored (already in place).
1925 */
embg1e34da42018-01-28 20:03:23 -07001926 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 if (nb <= 0)
1928 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 /* Merge what remains of the runs, using a temp array with
1931 * min(na, nb) elements.
1932 */
1933 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001934 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001936 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001937}
1938
1939/* Examine the stack of runs waiting to be merged, merging adjacent runs
1940 * until the stack invariants are re-established:
1941 *
1942 * 1. len[-3] > len[-2] + len[-1]
1943 * 2. len[-2] > len[-1]
1944 *
1945 * See listsort.txt for more info.
1946 *
1947 * Returns 0 on success, -1 on error.
1948 */
1949static int
1950merge_collapse(MergeState *ms)
1951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 assert(ms);
1955 while (ms->n > 1) {
1956 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001957 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1958 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 if (p[n-1].len < p[n+1].len)
1960 --n;
1961 if (merge_at(ms, n) < 0)
1962 return -1;
1963 }
1964 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001965 if (merge_at(ms, n) < 0)
1966 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 }
1968 else
1969 break;
1970 }
1971 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001972}
1973
1974/* Regardless of invariants, merge all runs on the stack until only one
1975 * remains. This is used at the end of the mergesort.
1976 *
1977 * Returns 0 on success, -1 on error.
1978 */
1979static int
1980merge_force_collapse(MergeState *ms)
1981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 assert(ms);
1985 while (ms->n > 1) {
1986 Py_ssize_t n = ms->n - 2;
1987 if (n > 0 && p[n-1].len < p[n+1].len)
1988 --n;
1989 if (merge_at(ms, n) < 0)
1990 return -1;
1991 }
1992 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001993}
1994
1995/* Compute a good value for the minimum run length; natural runs shorter
1996 * than this are boosted artificially via binary insertion.
1997 *
1998 * If n < 64, return n (it's too small to bother with fancy stuff).
1999 * Else if n is an exact power of 2, return 32.
2000 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
2001 * strictly less than, an exact power of 2.
2002 *
2003 * See listsort.txt for more info.
2004 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002005static Py_ssize_t
2006merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00002007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00002009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 assert(n >= 0);
2011 while (n >= 64) {
2012 r |= n & 1;
2013 n >>= 1;
2014 }
2015 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002016}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00002017
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002018static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00002019reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002020{
Daniel Stutzbach98338222010-12-02 21:55:33 +00002021 reverse_slice(s->keys, &s->keys[n]);
2022 if (s->values != NULL)
2023 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002024}
2025
embg1e34da42018-01-28 20:03:23 -07002026/* Here we define custom comparison functions to optimize for the cases one commonly
2027 * encounters in practice: homogeneous lists, often of one of the basic types. */
2028
2029/* This struct holds the comparison function and helper functions
2030 * selected in the pre-sort check. */
2031
2032/* These are the special case compare functions.
2033 * ms->key_compare will always point to one of these: */
2034
2035/* Heterogeneous compare: default, always safe to fall back on. */
2036static int
2037safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2038{
2039 /* No assumptions necessary! */
2040 return PyObject_RichCompareBool(v, w, Py_LT);
2041}
2042
2043/* Homogeneous compare: safe for any two compareable objects of the same type.
2044 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2045 * pre-sort check.)
2046 */
2047static int
2048unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2049{
2050 PyObject *res_obj; int res;
2051
2052 /* No assumptions, because we check first: */
2053 if (v->ob_type->tp_richcompare != ms->key_richcompare)
2054 return PyObject_RichCompareBool(v, w, Py_LT);
2055
2056 assert(ms->key_richcompare != NULL);
2057 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2058
2059 if (res_obj == Py_NotImplemented) {
2060 Py_DECREF(res_obj);
2061 return PyObject_RichCompareBool(v, w, Py_LT);
2062 }
2063 if (res_obj == NULL)
2064 return -1;
2065
2066 if (PyBool_Check(res_obj)) {
2067 res = (res_obj == Py_True);
2068 }
2069 else {
2070 res = PyObject_IsTrue(res_obj);
2071 }
2072 Py_DECREF(res_obj);
2073
2074 /* Note that we can't assert
2075 * res == PyObject_RichCompareBool(v, w, Py_LT);
2076 * because of evil compare functions like this:
2077 * lambda a, b: int(random.random() * 3) - 1)
2078 * (which is actually in test_sort.py) */
2079 return res;
2080}
2081
2082/* Latin string compare: safe for any two latin (one byte per char) strings. */
2083static int
2084unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2085{
Victor Stinner8017b802018-01-29 13:47:06 +01002086 Py_ssize_t len;
2087 int res;
embg1e34da42018-01-28 20:03:23 -07002088
2089 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2090 assert(v->ob_type == w->ob_type);
2091 assert(v->ob_type == &PyUnicode_Type);
2092 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2093 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2094
2095 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2096 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2097
2098 res = (res != 0 ?
2099 res < 0 :
2100 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2101
2102 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2103 return res;
2104}
2105
2106/* Bounded int compare: compare any two longs that fit in a single machine word. */
2107static int
2108unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2109{
2110 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2111
2112 /* Modified from Objects/longobject.c:long_compare, assuming: */
2113 assert(v->ob_type == w->ob_type);
2114 assert(v->ob_type == &PyLong_Type);
2115 assert(Py_ABS(Py_SIZE(v)) <= 1);
2116 assert(Py_ABS(Py_SIZE(w)) <= 1);
2117
2118 vl = (PyLongObject*)v;
2119 wl = (PyLongObject*)w;
2120
2121 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2122 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2123
2124 if (Py_SIZE(vl) < 0)
2125 v0 = -v0;
2126 if (Py_SIZE(wl) < 0)
2127 w0 = -w0;
2128
2129 res = v0 < w0;
2130 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2131 return res;
2132}
2133
2134/* Float compare: compare any two floats. */
2135static int
2136unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2137{
2138 int res;
2139
2140 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2141 assert(v->ob_type == w->ob_type);
2142 assert(v->ob_type == &PyFloat_Type);
2143
2144 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2145 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2146 return res;
2147}
2148
2149/* Tuple compare: compare *any* two tuples, using
2150 * ms->tuple_elem_compare to compare the first elements, which is set
2151 * using the same pre-sort check as we use for ms->key_compare,
2152 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2153 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2154 * that most tuple compares don't involve x[1:]. */
2155static int
2156unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2157{
2158 PyTupleObject *vt, *wt;
2159 Py_ssize_t i, vlen, wlen;
2160 int k;
2161
2162 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2163 assert(v->ob_type == w->ob_type);
2164 assert(v->ob_type == &PyTuple_Type);
2165 assert(Py_SIZE(v) > 0);
2166 assert(Py_SIZE(w) > 0);
2167
2168 vt = (PyTupleObject *)v;
2169 wt = (PyTupleObject *)w;
2170
2171 vlen = Py_SIZE(vt);
2172 wlen = Py_SIZE(wt);
2173
2174 for (i = 0; i < vlen && i < wlen; i++) {
2175 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2176 if (k < 0)
2177 return -1;
2178 if (!k)
2179 break;
2180 }
2181
2182 if (i >= vlen || i >= wlen)
2183 return vlen < wlen;
2184
2185 if (i == 0)
2186 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2187 else
2188 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2189}
2190
Tim Petersa64dc242002-08-01 02:13:36 +00002191/* An adaptive, stable, natural mergesort. See listsort.txt.
2192 * Returns Py_None on success, NULL on error. Even in case of error, the
2193 * list will be some permutation of its input state (nothing is lost or
2194 * duplicated).
2195 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002196/*[clinic input]
2197list.sort
2198
2199 *
2200 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002201 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002202
2203Stable sort *IN PLACE*.
2204[clinic start generated code]*/
2205
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002206static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002207list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002208/*[clinic end generated code: output=57b9f9c5e23fbe42 input=b0fcf743982c5b90]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 Py_ssize_t nremaining;
2212 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002213 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 Py_ssize_t saved_ob_size, saved_allocated;
2215 PyObject **saved_ob_item;
2216 PyObject **final_ob_item;
2217 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002219 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002222 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 if (keyfunc == Py_None)
2224 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 /* The list is temporarily made empty, so that mutations performed
2227 * by comparison functions can't affect the slice of memory we're
2228 * sorting (allowing mutations during sorting is a core-dump
2229 * factory, since ob_item may change).
2230 */
2231 saved_ob_size = Py_SIZE(self);
2232 saved_ob_item = self->ob_item;
2233 saved_allocated = self->allocated;
2234 Py_SIZE(self) = 0;
2235 self->ob_item = NULL;
2236 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002237
Daniel Stutzbach98338222010-12-02 21:55:33 +00002238 if (keyfunc == NULL) {
2239 keys = NULL;
2240 lo.keys = saved_ob_item;
2241 lo.values = NULL;
2242 }
2243 else {
2244 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2245 /* Leverage stack space we allocated but won't otherwise use */
2246 keys = &ms.temparray[saved_ob_size+1];
2247 else {
2248 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002249 if (keys == NULL) {
2250 PyErr_NoMemory();
2251 goto keyfunc_fail;
2252 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002254
2255 for (i = 0; i < saved_ob_size ; i++) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002256 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
2257 NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002258 if (keys[i] == NULL) {
2259 for (i=i-1 ; i>=0 ; i--)
2260 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002261 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002262 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002263 goto keyfunc_fail;
2264 }
2265 }
2266
2267 lo.keys = keys;
2268 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002270
embg1e34da42018-01-28 20:03:23 -07002271
2272 /* The pre-sort check: here's where we decide which compare function to use.
2273 * How much optimization is safe? We test for homogeneity with respect to
2274 * several properties that are expensive to check at compare-time, and
2275 * set ms appropriately. */
2276 if (saved_ob_size > 1) {
2277 /* Assume the first element is representative of the whole list. */
2278 int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2279 Py_SIZE(lo.keys[0]) > 0);
2280
2281 PyTypeObject* key_type = (keys_are_in_tuples ?
2282 PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2283 lo.keys[0]->ob_type);
2284
2285 int keys_are_all_same_type = 1;
2286 int strings_are_latin = 1;
2287 int ints_are_bounded = 1;
2288
2289 /* Prove that assumption by checking every key. */
embg1e34da42018-01-28 20:03:23 -07002290 for (i=0; i < saved_ob_size; i++) {
2291
2292 if (keys_are_in_tuples &&
2293 !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2294 keys_are_in_tuples = 0;
2295 keys_are_all_same_type = 0;
2296 break;
2297 }
2298
2299 /* Note: for lists of tuples, key is the first element of the tuple
2300 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2301 * for lists of tuples in the if-statement directly above. */
2302 PyObject *key = (keys_are_in_tuples ?
2303 PyTuple_GET_ITEM(lo.keys[i], 0) :
2304 lo.keys[i]);
2305
2306 if (key->ob_type != key_type) {
2307 keys_are_all_same_type = 0;
2308 break;
2309 }
2310
2311 if (key_type == &PyLong_Type) {
2312 if (ints_are_bounded && Py_ABS(Py_SIZE(key)) > 1)
2313 ints_are_bounded = 0;
2314 }
2315 else if (key_type == &PyUnicode_Type){
2316 if (strings_are_latin &&
2317 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND)
2318 strings_are_latin = 0;
2319 }
2320 }
2321
2322 /* Choose the best compare, given what we now know about the keys. */
2323 if (keys_are_all_same_type) {
2324
2325 if (key_type == &PyUnicode_Type && strings_are_latin) {
2326 ms.key_compare = unsafe_latin_compare;
2327 }
2328 else if (key_type == &PyLong_Type && ints_are_bounded) {
2329 ms.key_compare = unsafe_long_compare;
2330 }
2331 else if (key_type == &PyFloat_Type) {
2332 ms.key_compare = unsafe_float_compare;
2333 }
2334 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2335 ms.key_compare = unsafe_object_compare;
2336 }
Zackery Spytzebc793d2019-02-21 00:47:14 -07002337 else {
2338 ms.key_compare = safe_object_compare;
2339 }
embg1e34da42018-01-28 20:03:23 -07002340 }
2341 else {
2342 ms.key_compare = safe_object_compare;
2343 }
2344
2345 if (keys_are_in_tuples) {
2346 /* Make sure we're not dealing with tuples of tuples
2347 * (remember: here, key_type refers list [key[0] for key in keys]) */
2348 if (key_type == &PyTuple_Type)
2349 ms.tuple_elem_compare = safe_object_compare;
2350 else
2351 ms.tuple_elem_compare = ms.key_compare;
2352
2353 ms.key_compare = unsafe_tuple_compare;
2354 }
2355 }
2356 /* End of pre-sort check: ms is now set properly! */
2357
Daniel Stutzbach98338222010-12-02 21:55:33 +00002358 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 nremaining = saved_ob_size;
2361 if (nremaining < 2)
2362 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002363
Benjamin Peterson05380642010-08-23 19:35:39 +00002364 /* Reverse sort stability achieved by initially reversing the list,
2365 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002366 if (reverse) {
2367 if (keys != NULL)
2368 reverse_slice(&keys[0], &keys[saved_ob_size]);
2369 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2370 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 /* March over the array once, left to right, finding natural runs,
2373 * and extending short natural runs to minrun elements.
2374 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 minrun = merge_compute_minrun(nremaining);
2376 do {
2377 int descending;
2378 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002381 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 if (n < 0)
2383 goto fail;
2384 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002385 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 /* If short, extend to min(minrun, nremaining). */
2387 if (n < minrun) {
2388 const Py_ssize_t force = nremaining <= minrun ?
2389 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002390 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 goto fail;
2392 n = force;
2393 }
2394 /* Push run onto pending-runs stack, and maybe merge. */
2395 assert(ms.n < MAX_MERGE_PENDING);
2396 ms.pending[ms.n].base = lo;
2397 ms.pending[ms.n].len = n;
2398 ++ms.n;
2399 if (merge_collapse(&ms) < 0)
2400 goto fail;
2401 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002402 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 nremaining -= n;
2404 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 if (merge_force_collapse(&ms) < 0)
2407 goto fail;
2408 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002409 assert(keys == NULL
2410 ? ms.pending[0].base.keys == saved_ob_item
2411 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002413 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002414
2415succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002417fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002418 if (keys != NULL) {
2419 for (i = 0; i < saved_ob_size; i++)
2420 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002421 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002422 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 if (self->allocated != -1 && result != NULL) {
2426 /* The user mucked with the list during the sort,
2427 * and we don't already have another error to report.
2428 */
2429 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2430 result = NULL;
2431 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 if (reverse && saved_ob_size > 1)
2434 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002437
Daniel Stutzbach98338222010-12-02 21:55:33 +00002438keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 final_ob_item = self->ob_item;
2440 i = Py_SIZE(self);
2441 Py_SIZE(self) = saved_ob_size;
2442 self->ob_item = saved_ob_item;
2443 self->allocated = saved_allocated;
2444 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002445 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 guarantee that the list is really empty when it returns */
2447 while (--i >= 0) {
2448 Py_XDECREF(final_ob_item[i]);
2449 }
2450 PyMem_FREE(final_ob_item);
2451 }
2452 Py_XINCREF(result);
2453 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002454}
Tim Peters330f9e92002-07-19 07:05:44 +00002455#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002456#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002457
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002458int
Fred Drakea2f55112000-07-09 15:16:51 +00002459PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 if (v == NULL || !PyList_Check(v)) {
2462 PyErr_BadInternalCall();
2463 return -1;
2464 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002465 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 if (v == NULL)
2467 return -1;
2468 Py_DECREF(v);
2469 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002470}
2471
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002472/*[clinic input]
2473list.reverse
2474
2475Reverse *IN PLACE*.
2476[clinic start generated code]*/
2477
Guido van Rossumb86c5492001-02-12 22:06:02 +00002478static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002479list_reverse_impl(PyListObject *self)
2480/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 if (Py_SIZE(self) > 1)
2483 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2484 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002485}
2486
Guido van Rossum84c76f51990-10-30 13:32:20 +00002487int
Fred Drakea2f55112000-07-09 15:16:51 +00002488PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 if (v == NULL || !PyList_Check(v)) {
2493 PyErr_BadInternalCall();
2494 return -1;
2495 }
2496 if (Py_SIZE(self) > 1)
2497 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2498 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002499}
2500
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002501PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002502PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 PyObject *w;
2505 PyObject **p, **q;
2506 Py_ssize_t n;
2507 if (v == NULL || !PyList_Check(v)) {
2508 PyErr_BadInternalCall();
2509 return NULL;
2510 }
2511 n = Py_SIZE(v);
2512 w = PyTuple_New(n);
2513 if (w == NULL)
2514 return NULL;
2515 p = ((PyTupleObject *)w)->ob_item;
2516 q = ((PyListObject *)v)->ob_item;
2517 while (--n >= 0) {
2518 Py_INCREF(*q);
2519 *p = *q;
2520 p++;
2521 q++;
2522 }
2523 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002524}
2525
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002526/*[clinic input]
2527list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002528
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002529 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002530 start: slice_index(accept={int}) = 0
2531 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002532 /
2533
2534Return first index of value.
2535
2536Raises ValueError if the value is not present.
2537[clinic start generated code]*/
2538
2539static PyObject *
2540list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2541 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002542/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002543{
2544 Py_ssize_t i;
2545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 if (start < 0) {
2547 start += Py_SIZE(self);
2548 if (start < 0)
2549 start = 0;
2550 }
2551 if (stop < 0) {
2552 stop += Py_SIZE(self);
2553 if (stop < 0)
2554 stop = 0;
2555 }
2556 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002557 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 if (cmp > 0)
2559 return PyLong_FromSsize_t(i);
2560 else if (cmp < 0)
2561 return NULL;
2562 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002563 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002565}
2566
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002567/*[clinic input]
2568list.count
2569
2570 value: object
2571 /
2572
2573Return number of occurrences of value.
2574[clinic start generated code]*/
2575
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002576static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002577list_count(PyListObject *self, PyObject *value)
2578/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 Py_ssize_t count = 0;
2581 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002584 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 if (cmp > 0)
2586 count++;
2587 else if (cmp < 0)
2588 return NULL;
2589 }
2590 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002591}
2592
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002593/*[clinic input]
2594list.remove
2595
2596 value: object
2597 /
2598
2599Remove first occurrence of value.
2600
2601Raises ValueError if the value is not present.
2602[clinic start generated code]*/
2603
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002604static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002605list_remove(PyListObject *self, PyObject *value)
2606/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002611 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 if (cmp > 0) {
2613 if (list_ass_slice(self, i, i+1,
2614 (PyObject *)NULL) == 0)
2615 Py_RETURN_NONE;
2616 return NULL;
2617 }
2618 else if (cmp < 0)
2619 return NULL;
2620 }
2621 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2622 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002623}
2624
Jeremy Hylton8caad492000-06-23 14:18:11 +00002625static int
2626list_traverse(PyListObject *o, visitproc visit, void *arg)
2627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 for (i = Py_SIZE(o); --i >= 0; )
2631 Py_VISIT(o->ob_item[i]);
2632 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002633}
2634
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002635static PyObject *
2636list_richcompare(PyObject *v, PyObject *w, int op)
2637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 PyListObject *vl, *wl;
2639 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002640
Brian Curtindfc80e32011-08-10 20:28:54 -05002641 if (!PyList_Check(v) || !PyList_Check(w))
2642 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 vl = (PyListObject *)v;
2645 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2648 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002650 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 else
stratakise8b19652017-11-02 11:32:54 +01002652 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 /* Search for the first index where items are different */
2656 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2657 int k = PyObject_RichCompareBool(vl->ob_item[i],
2658 wl->ob_item[i], Py_EQ);
2659 if (k < 0)
2660 return NULL;
2661 if (!k)
2662 break;
2663 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2666 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002667 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 /* We have an item that differs -- shortcuts for EQ/NE */
2671 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002672 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 }
2674 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002675 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 /* Compare the final item again using the proper operator */
2679 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002680}
2681
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002682/*[clinic input]
2683list.__init__
2684
2685 iterable: object(c_default="NULL") = ()
2686 /
2687
2688Built-in mutable sequence.
2689
2690If no argument is given, the constructor creates a new empty list.
2691The argument must be an iterable if specified.
2692[clinic start generated code]*/
2693
Tim Peters6d6c1a32001-08-02 04:15:00 +00002694static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002695list___init___impl(PyListObject *self, PyObject *iterable)
2696/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 /* Verify list invariants established by PyType_GenericAlloc() */
2699 assert(0 <= Py_SIZE(self));
2700 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2701 assert(self->ob_item != NULL ||
2702 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 /* Empty previous contents */
2705 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002706 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002708 if (iterable != NULL) {
Pablo Galindo372d7052018-10-28 20:16:26 +00002709 if (_PyObject_HasLen(iterable)) {
2710 Py_ssize_t iter_len = PyObject_Size(iterable);
2711 if (iter_len == -1) {
2712 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2713 return -1;
2714 }
2715 PyErr_Clear();
2716 }
2717 if (iter_len > 0 && self->ob_item == NULL
2718 && list_preallocate_exact(self, iter_len)) {
2719 return -1;
2720 }
2721 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002722 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 if (rv == NULL)
2724 return -1;
2725 Py_DECREF(rv);
2726 }
2727 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002728}
2729
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002730/*[clinic input]
2731list.__sizeof__
2732
2733Return the size of the list in memory, in bytes.
2734[clinic start generated code]*/
2735
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002736static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002737list___sizeof___impl(PyListObject *self)
2738/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002741
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002742 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002744}
2745
Raymond Hettinger1021c442003-11-07 15:38:09 +00002746static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002747static PyObject *list_subscript(PyListObject*, PyObject*);
2748
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002749static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002750 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2751 LIST___REVERSED___METHODDEF
2752 LIST___SIZEOF___METHODDEF
2753 LIST_CLEAR_METHODDEF
2754 LIST_COPY_METHODDEF
2755 LIST_APPEND_METHODDEF
2756 LIST_INSERT_METHODDEF
2757 LIST_EXTEND_METHODDEF
2758 LIST_POP_METHODDEF
2759 LIST_REMOVE_METHODDEF
2760 LIST_INDEX_METHODDEF
2761 LIST_COUNT_METHODDEF
2762 LIST_REVERSE_METHODDEF
2763 LIST_SORT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002765};
2766
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002767static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 (lenfunc)list_length, /* sq_length */
2769 (binaryfunc)list_concat, /* sq_concat */
2770 (ssizeargfunc)list_repeat, /* sq_repeat */
2771 (ssizeargfunc)list_item, /* sq_item */
2772 0, /* sq_slice */
2773 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2774 0, /* sq_ass_slice */
2775 (objobjproc)list_contains, /* sq_contains */
2776 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2777 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002778};
2779
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002780static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002781list_subscript(PyListObject* self, PyObject* item)
2782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 if (PyIndex_Check(item)) {
2784 Py_ssize_t i;
2785 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2786 if (i == -1 && PyErr_Occurred())
2787 return NULL;
2788 if (i < 0)
2789 i += PyList_GET_SIZE(self);
2790 return list_item(self, i);
2791 }
2792 else if (PySlice_Check(item)) {
2793 Py_ssize_t start, stop, step, slicelength, cur, i;
2794 PyObject* result;
2795 PyObject* it;
2796 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002797
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002798 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 return NULL;
2800 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002801 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2802 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 if (slicelength <= 0) {
2805 return PyList_New(0);
2806 }
2807 else if (step == 1) {
2808 return list_slice(self, start, stop);
2809 }
2810 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002811 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 src = self->ob_item;
2815 dest = ((PyListObject *)result)->ob_item;
2816 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002817 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 it = src[cur];
2819 Py_INCREF(it);
2820 dest[i] = it;
2821 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002822 Py_SIZE(result) = slicelength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 return result;
2824 }
2825 }
2826 else {
2827 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002828 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 item->ob_type->tp_name);
2830 return NULL;
2831 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002832}
2833
Tim Peters3b01a122002-07-19 02:35:45 +00002834static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002835list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 if (PyIndex_Check(item)) {
2838 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2839 if (i == -1 && PyErr_Occurred())
2840 return -1;
2841 if (i < 0)
2842 i += PyList_GET_SIZE(self);
2843 return list_ass_item(self, i, value);
2844 }
2845 else if (PySlice_Check(item)) {
2846 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002847
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002848 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 return -1;
2850 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002851 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2852 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 if (step == 1)
2855 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 /* Make sure s[5:2] = [..] inserts at the right place:
2858 before 5, not before 2. */
2859 if ((step < 0 && start < stop) ||
2860 (step > 0 && start > stop))
2861 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 if (value == NULL) {
2864 /* delete slice */
2865 PyObject **garbage;
2866 size_t cur;
2867 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002868 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 if (slicelength <= 0)
2871 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 if (step < 0) {
2874 stop = start + 1;
2875 start = stop + step*(slicelength - 1) - 1;
2876 step = -step;
2877 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 garbage = (PyObject**)
2880 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2881 if (!garbage) {
2882 PyErr_NoMemory();
2883 return -1;
2884 }
Tim Peters3b01a122002-07-19 02:35:45 +00002885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 /* drawing pictures might help understand these for
2887 loops. Basically, we memmove the parts of the
2888 list that are *not* part of the slice: step-1
2889 items for each item that is part of the slice,
2890 and then tail end of the list that was not
2891 covered by the slice */
2892 for (cur = start, i = 0;
2893 cur < (size_t)stop;
2894 cur += step, i++) {
2895 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 if (cur + step >= (size_t)Py_SIZE(self)) {
2900 lim = Py_SIZE(self) - cur - 1;
2901 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 memmove(self->ob_item + cur - i,
2904 self->ob_item + cur + 1,
2905 lim * sizeof(PyObject *));
2906 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002907 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 if (cur < (size_t)Py_SIZE(self)) {
2909 memmove(self->ob_item + cur - slicelength,
2910 self->ob_item + cur,
2911 (Py_SIZE(self) - cur) *
2912 sizeof(PyObject *));
2913 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002916 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 for (i = 0; i < slicelength; i++) {
2919 Py_DECREF(garbage[i]);
2920 }
2921 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002922
Victor Stinner35f28032013-11-21 12:16:35 +01002923 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 }
2925 else {
2926 /* assign slice */
2927 PyObject *ins, *seq;
2928 PyObject **garbage, **seqitems, **selfitems;
2929 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 /* protect against a[::-1] = a */
2932 if (self == (PyListObject*)value) {
2933 seq = list_slice((PyListObject*)value, 0,
2934 PyList_GET_SIZE(value));
2935 }
2936 else {
2937 seq = PySequence_Fast(value,
2938 "must assign iterable "
2939 "to extended slice");
2940 }
2941 if (!seq)
2942 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2945 PyErr_Format(PyExc_ValueError,
2946 "attempt to assign sequence of "
2947 "size %zd to extended slice of "
2948 "size %zd",
2949 PySequence_Fast_GET_SIZE(seq),
2950 slicelength);
2951 Py_DECREF(seq);
2952 return -1;
2953 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 if (!slicelength) {
2956 Py_DECREF(seq);
2957 return 0;
2958 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 garbage = (PyObject**)
2961 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2962 if (!garbage) {
2963 Py_DECREF(seq);
2964 PyErr_NoMemory();
2965 return -1;
2966 }
Tim Peters3b01a122002-07-19 02:35:45 +00002967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 selfitems = self->ob_item;
2969 seqitems = PySequence_Fast_ITEMS(seq);
2970 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002971 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 garbage[i] = selfitems[cur];
2973 ins = seqitems[i];
2974 Py_INCREF(ins);
2975 selfitems[cur] = ins;
2976 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 for (i = 0; i < slicelength; i++) {
2979 Py_DECREF(garbage[i]);
2980 }
Tim Peters3b01a122002-07-19 02:35:45 +00002981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 PyMem_FREE(garbage);
2983 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 return 0;
2986 }
2987 }
2988 else {
2989 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002990 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 item->ob_type->tp_name);
2992 return -1;
2993 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002994}
2995
2996static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 (lenfunc)list_length,
2998 (binaryfunc)list_subscript,
2999 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00003000};
3001
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003002PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3004 "list",
3005 sizeof(PyListObject),
3006 0,
3007 (destructor)list_dealloc, /* tp_dealloc */
3008 0, /* tp_print */
3009 0, /* tp_getattr */
3010 0, /* tp_setattr */
3011 0, /* tp_reserved */
3012 (reprfunc)list_repr, /* tp_repr */
3013 0, /* tp_as_number */
3014 &list_as_sequence, /* tp_as_sequence */
3015 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003016 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 0, /* tp_call */
3018 0, /* tp_str */
3019 PyObject_GenericGetAttr, /* tp_getattro */
3020 0, /* tp_setattro */
3021 0, /* tp_as_buffer */
3022 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003023 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
3024 list___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 (traverseproc)list_traverse, /* tp_traverse */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003026 (inquiry)_list_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 list_richcompare, /* tp_richcompare */
3028 0, /* tp_weaklistoffset */
3029 list_iter, /* tp_iter */
3030 0, /* tp_iternext */
3031 list_methods, /* tp_methods */
3032 0, /* tp_members */
3033 0, /* tp_getset */
3034 0, /* tp_base */
3035 0, /* tp_dict */
3036 0, /* tp_descr_get */
3037 0, /* tp_descr_set */
3038 0, /* tp_dictoffset */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003039 (initproc)list___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 PyType_GenericAlloc, /* tp_alloc */
3041 PyType_GenericNew, /* tp_new */
3042 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003043};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00003044
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003045/*********************** List Iterator **************************/
3046
3047typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02003049 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003051} listiterobject;
3052
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003053static void listiter_dealloc(listiterobject *);
3054static int listiter_traverse(listiterobject *, visitproc, void *);
3055static PyObject *listiter_next(listiterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303056static PyObject *listiter_len(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003057static PyObject *listiter_reduce_general(void *_it, int forward);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303058static PyObject *listiter_reduce(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003059static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00003060
Armin Rigof5b3e362006-02-11 21:32:43 +00003061PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003062PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3063PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003064
3065static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003067 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3068 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00003070};
3071
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003072PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3074 "list_iterator", /* tp_name */
3075 sizeof(listiterobject), /* tp_basicsize */
3076 0, /* tp_itemsize */
3077 /* methods */
3078 (destructor)listiter_dealloc, /* tp_dealloc */
3079 0, /* tp_print */
3080 0, /* tp_getattr */
3081 0, /* tp_setattr */
3082 0, /* tp_reserved */
3083 0, /* tp_repr */
3084 0, /* tp_as_number */
3085 0, /* tp_as_sequence */
3086 0, /* tp_as_mapping */
3087 0, /* tp_hash */
3088 0, /* tp_call */
3089 0, /* tp_str */
3090 PyObject_GenericGetAttr, /* tp_getattro */
3091 0, /* tp_setattro */
3092 0, /* tp_as_buffer */
3093 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3094 0, /* tp_doc */
3095 (traverseproc)listiter_traverse, /* tp_traverse */
3096 0, /* tp_clear */
3097 0, /* tp_richcompare */
3098 0, /* tp_weaklistoffset */
3099 PyObject_SelfIter, /* tp_iter */
3100 (iternextfunc)listiter_next, /* tp_iternext */
3101 listiter_methods, /* tp_methods */
3102 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003103};
Raymond Hettinger1021c442003-11-07 15:38:09 +00003104
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003105
3106static PyObject *
3107list_iter(PyObject *seq)
3108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 if (!PyList_Check(seq)) {
3112 PyErr_BadInternalCall();
3113 return NULL;
3114 }
3115 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3116 if (it == NULL)
3117 return NULL;
3118 it->it_index = 0;
3119 Py_INCREF(seq);
3120 it->it_seq = (PyListObject *)seq;
3121 _PyObject_GC_TRACK(it);
3122 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003123}
3124
3125static void
3126listiter_dealloc(listiterobject *it)
3127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 _PyObject_GC_UNTRACK(it);
3129 Py_XDECREF(it->it_seq);
3130 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003131}
3132
3133static int
3134listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 Py_VISIT(it->it_seq);
3137 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003138}
3139
3140static PyObject *
3141listiter_next(listiterobject *it)
3142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 PyListObject *seq;
3144 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 assert(it != NULL);
3147 seq = it->it_seq;
3148 if (seq == NULL)
3149 return NULL;
3150 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 if (it->it_index < PyList_GET_SIZE(seq)) {
3153 item = PyList_GET_ITEM(seq, it->it_index);
3154 ++it->it_index;
3155 Py_INCREF(item);
3156 return item;
3157 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003160 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003162}
3163
3164static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303165listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 Py_ssize_t len;
3168 if (it->it_seq) {
3169 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3170 if (len >= 0)
3171 return PyLong_FromSsize_t(len);
3172 }
3173 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003174}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003175
3176static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303177listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003178{
3179 return listiter_reduce_general(it, 1);
3180}
3181
3182static PyObject *
3183listiter_setstate(listiterobject *it, PyObject *state)
3184{
Victor Stinner7660b882013-06-24 23:59:24 +02003185 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003186 if (index == -1 && PyErr_Occurred())
3187 return NULL;
3188 if (it->it_seq != NULL) {
3189 if (index < 0)
3190 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003191 else if (index > PyList_GET_SIZE(it->it_seq))
3192 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003193 it->it_index = index;
3194 }
3195 Py_RETURN_NONE;
3196}
3197
Raymond Hettinger1021c442003-11-07 15:38:09 +00003198/*********************** List Reverse Iterator **************************/
3199
3200typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 PyObject_HEAD
3202 Py_ssize_t it_index;
3203 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00003204} listreviterobject;
3205
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003206static void listreviter_dealloc(listreviterobject *);
3207static int listreviter_traverse(listreviterobject *, visitproc, void *);
3208static PyObject *listreviter_next(listreviterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303209static PyObject *listreviter_len(listreviterobject *, PyObject *);
3210static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003211static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003212
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003213static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003215 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3216 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003218};
3219
Raymond Hettinger1021c442003-11-07 15:38:09 +00003220PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3222 "list_reverseiterator", /* tp_name */
3223 sizeof(listreviterobject), /* tp_basicsize */
3224 0, /* tp_itemsize */
3225 /* methods */
3226 (destructor)listreviter_dealloc, /* tp_dealloc */
3227 0, /* tp_print */
3228 0, /* tp_getattr */
3229 0, /* tp_setattr */
3230 0, /* tp_reserved */
3231 0, /* tp_repr */
3232 0, /* tp_as_number */
3233 0, /* tp_as_sequence */
3234 0, /* tp_as_mapping */
3235 0, /* tp_hash */
3236 0, /* tp_call */
3237 0, /* tp_str */
3238 PyObject_GenericGetAttr, /* tp_getattro */
3239 0, /* tp_setattro */
3240 0, /* tp_as_buffer */
3241 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3242 0, /* tp_doc */
3243 (traverseproc)listreviter_traverse, /* tp_traverse */
3244 0, /* tp_clear */
3245 0, /* tp_richcompare */
3246 0, /* tp_weaklistoffset */
3247 PyObject_SelfIter, /* tp_iter */
3248 (iternextfunc)listreviter_next, /* tp_iternext */
3249 listreviter_methods, /* tp_methods */
3250 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00003251};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003252
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003253/*[clinic input]
3254list.__reversed__
3255
3256Return a reverse iterator over the list.
3257[clinic start generated code]*/
3258
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003259static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003260list___reversed___impl(PyListObject *self)
3261/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3266 if (it == NULL)
3267 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003268 assert(PyList_Check(self));
3269 it->it_index = PyList_GET_SIZE(self) - 1;
3270 Py_INCREF(self);
3271 it->it_seq = self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 PyObject_GC_Track(it);
3273 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003274}
3275
3276static void
3277listreviter_dealloc(listreviterobject *it)
3278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 PyObject_GC_UnTrack(it);
3280 Py_XDECREF(it->it_seq);
3281 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003282}
3283
3284static int
3285listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 Py_VISIT(it->it_seq);
3288 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003289}
3290
3291static PyObject *
3292listreviter_next(listreviterobject *it)
3293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003295 Py_ssize_t index;
3296 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003297
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003298 assert(it != NULL);
3299 seq = it->it_seq;
3300 if (seq == NULL) {
3301 return NULL;
3302 }
3303 assert(PyList_Check(seq));
3304
3305 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 if (index>=0 && index < PyList_GET_SIZE(seq)) {
3307 item = PyList_GET_ITEM(seq, index);
3308 it->it_index--;
3309 Py_INCREF(item);
3310 return item;
3311 }
3312 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003313 it->it_seq = NULL;
3314 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003316}
3317
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003318static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303319listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 Py_ssize_t len = it->it_index + 1;
3322 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3323 len = 0;
3324 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003325}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003326
3327static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303328listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003329{
3330 return listiter_reduce_general(it, 0);
3331}
3332
3333static PyObject *
3334listreviter_setstate(listreviterobject *it, PyObject *state)
3335{
3336 Py_ssize_t index = PyLong_AsSsize_t(state);
3337 if (index == -1 && PyErr_Occurred())
3338 return NULL;
3339 if (it->it_seq != NULL) {
3340 if (index < -1)
3341 index = -1;
3342 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3343 index = PyList_GET_SIZE(it->it_seq) - 1;
3344 it->it_index = index;
3345 }
3346 Py_RETURN_NONE;
3347}
3348
3349/* common pickling support */
3350
3351static PyObject *
3352listiter_reduce_general(void *_it, int forward)
3353{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003354 _Py_IDENTIFIER(iter);
3355 _Py_IDENTIFIER(reversed);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003356 PyObject *list;
3357
3358 /* the objects are not the same, index is of different types! */
3359 if (forward) {
3360 listiterobject *it = (listiterobject *)_it;
3361 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003362 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003363 it->it_seq, it->it_index);
3364 } else {
3365 listreviterobject *it = (listreviterobject *)_it;
3366 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003367 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_reversed),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003368 it->it_seq, it->it_index);
3369 }
3370 /* empty iterator, create an empty list */
3371 list = PyList_New(0);
3372 if (list == NULL)
3373 return NULL;
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003374 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003375}