blob: c29954283c488ea777f4a8abe1c6a2b46a6c370d [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* List object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01004#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01005#include "pycore_pystate.h"
Sergey Fedoseev234531b2019-02-25 21:59:12 +05006#include "pycore_tupleobject.h"
Victor Stinnere281f7d2018-11-01 02:30:36 +01007#include "pycore_accu.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00008
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00009#ifdef STDC_HEADERS
10#include <stddef.h>
11#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000012#include <sys/types.h> /* For size_t */
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000013#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014
Serhiy Storchakafdd42c42017-03-11 09:19:20 +020015/*[clinic input]
16class list "PyListObject *" "&PyList_Type"
17[clinic start generated code]*/
18/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f9b222678f9f71e0]*/
19
20#include "clinic/listobject.c.h"
21
Tim Peters8d9eb102004-07-31 02:24:20 +000022/* Ensure ob_item has room for at least newsize elements, and set
23 * ob_size to newsize. If newsize > ob_size on entry, the content
24 * of the new slots at exit is undefined heap trash; it's the caller's
Ezio Melotti13925002011-03-16 11:05:33 +020025 * responsibility to overwrite them with sane values.
Tim Peters8d9eb102004-07-31 02:24:20 +000026 * The number of allocated elements may grow, shrink, or stay the same.
27 * Failure is impossible if newsize <= self.allocated on entry, although
28 * that partly relies on an assumption that the system realloc() never
29 * fails when passed a number of bytes <= the number of bytes last
30 * allocated (the C standard doesn't guarantee this, but it's hard to
31 * imagine a realloc implementation where it wouldn't be true).
32 * Note that self->ob_item may change, and even if newsize is less
33 * than ob_size on entry.
34 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000035static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000036list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 PyObject **items;
Xiang Zhang4cee0492017-02-22 12:32:30 +080039 size_t new_allocated, num_allocated_bytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 /* Bypass realloc() when a previous overallocation is large enough
43 to accommodate the newsize. If the newsize falls lower than half
44 the allocated size, then proceed with the realloc() to shrink the list.
45 */
46 if (allocated >= newsize && newsize >= (allocated >> 1)) {
47 assert(self->ob_item != NULL || newsize == 0);
48 Py_SIZE(self) = newsize;
49 return 0;
50 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 /* This over-allocates proportional to the list size, making room
53 * for additional growth. The over-allocation is mild, but is
54 * enough to give linear-time amortized behavior over a long
55 * sequence of appends() in the presence of a poorly-performing
56 * system realloc().
57 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
Xiang Zhang4cee0492017-02-22 12:32:30 +080058 * Note: new_allocated won't overflow because the largest possible value
59 * is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 */
Xiang Zhang4cee0492017-02-22 12:32:30 +080061 new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
62 if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 PyErr_NoMemory();
64 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 if (newsize == 0)
68 new_allocated = 0;
Xiang Zhang4cee0492017-02-22 12:32:30 +080069 num_allocated_bytes = new_allocated * sizeof(PyObject *);
70 items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (items == NULL) {
72 PyErr_NoMemory();
73 return -1;
74 }
75 self->ob_item = items;
76 Py_SIZE(self) = newsize;
77 self->allocated = new_allocated;
78 return 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000079}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000080
Pablo Galindo372d7052018-10-28 20:16:26 +000081static int
82list_preallocate_exact(PyListObject *self, Py_ssize_t size)
83{
84 assert(self->ob_item == NULL);
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050085 assert(size > 0);
Pablo Galindo372d7052018-10-28 20:16:26 +000086
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050087 PyObject **items = PyMem_New(PyObject*, size);
Pablo Galindo372d7052018-10-28 20:16:26 +000088 if (items == NULL) {
89 PyErr_NoMemory();
90 return -1;
91 }
92 self->ob_item = items;
Sergey Fedoseev0e5f7712018-12-30 03:31:36 +050093 self->allocated = size;
Pablo Galindo372d7052018-10-28 20:16:26 +000094 return 0;
95}
96
Christian Heimes77c02eb2008-02-09 02:18:51 +000097/* Debug statistic to compare allocations with reuse through the free list */
98#undef SHOW_ALLOC_COUNT
99#ifdef SHOW_ALLOC_COUNT
100static size_t count_alloc = 0;
101static size_t count_reuse = 0;
102
103static void
104show_alloc(void)
105{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200106 PyInterpreterState *interp = _PyInterpreterState_Get();
Eddie Elizondo745dc652018-02-21 20:55:18 -0800107 if (!interp->core_config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300108 return;
Victor Stinner25420fe2017-11-20 18:12:22 -0800109 }
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
112 count_alloc);
113 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
114 "d\n", count_reuse);
115 fprintf(stderr, "%.2f%% reuse rate\n\n",
116 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +0000117}
118#endif
119
Raymond Hettinger0468e412004-05-05 05:37:53 +0000120/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000121#ifndef PyList_MAXFREELIST
122#define PyList_MAXFREELIST 80
123#endif
124static PyListObject *free_list[PyList_MAXFREELIST];
125static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000126
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100127int
128PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100131 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 while (numfree) {
133 op = free_list[--numfree];
134 assert(PyList_CheckExact(op));
135 PyObject_GC_Del(op);
136 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100137 return ret;
138}
139
140void
141PyList_Fini(void)
142{
143 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000144}
145
David Malcolm49526f42012-06-22 14:55:41 -0400146/* Print summary info about the state of the optimized allocator */
147void
148_PyList_DebugMallocStats(FILE *out)
149{
150 _PyDebugAllocatorStats(out,
151 "free PyListObject",
152 numfree, sizeof(PyListObject));
153}
154
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000155PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000156PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 PyListObject *op;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000159#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 static int initialized = 0;
161 if (!initialized) {
162 Py_AtExit(show_alloc);
163 initialized = 1;
164 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000165#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 if (size < 0) {
168 PyErr_BadInternalCall();
169 return NULL;
170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (numfree) {
172 numfree--;
173 op = free_list[numfree];
174 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000175#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000177#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 } else {
179 op = PyObject_GC_New(PyListObject, &PyList_Type);
180 if (op == NULL)
181 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000182#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000184#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 }
186 if (size <= 0)
187 op->ob_item = NULL;
188 else {
Mark Dickinson5d132382016-08-21 08:55:15 +0100189 op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 if (op->ob_item == NULL) {
191 Py_DECREF(op);
192 return PyErr_NoMemory();
193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 }
195 Py_SIZE(op) = size;
196 op->allocated = size;
197 _PyObject_GC_TRACK(op);
198 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199}
200
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500201static PyObject *
202list_new_prealloc(Py_ssize_t size)
203{
204 PyListObject *op = (PyListObject *) PyList_New(0);
205 if (size == 0 || op == NULL) {
206 return (PyObject *) op;
207 }
208 assert(op->ob_item == NULL);
209 op->ob_item = PyMem_New(PyObject *, size);
210 if (op->ob_item == NULL) {
211 Py_DECREF(op);
212 return PyErr_NoMemory();
213 }
214 op->allocated = size;
215 return (PyObject *) op;
216}
217
Martin v. Löwis18e16552006-02-15 17:27:45 +0000218Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000219PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 if (!PyList_Check(op)) {
222 PyErr_BadInternalCall();
223 return -1;
224 }
225 else
226 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227}
228
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700229static inline int
230valid_index(Py_ssize_t i, Py_ssize_t limit)
231{
232 /* The cast to size_t lets us use just a single comparison
233 to check whether i is in the range: 0 <= i < limit.
234
235 See: Section 14.2 "Bounds Checking" in the Agner Fog
236 optimization manual found at:
237 https://www.agner.org/optimize/optimizing_cpp.pdf
238 */
239 return (size_t) i < (size_t) limit;
240}
241
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000242static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000243
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000245PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (!PyList_Check(op)) {
248 PyErr_BadInternalCall();
249 return NULL;
250 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700251 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 if (indexerr == NULL) {
253 indexerr = PyUnicode_FromString(
254 "list index out of range");
255 if (indexerr == NULL)
256 return NULL;
257 }
258 PyErr_SetObject(PyExc_IndexError, indexerr);
259 return NULL;
260 }
261 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262}
263
264int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200265PyList_SetItem(PyObject *op, Py_ssize_t i,
266 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200268 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 if (!PyList_Check(op)) {
270 Py_XDECREF(newitem);
271 PyErr_BadInternalCall();
272 return -1;
273 }
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700274 if (!valid_index(i, Py_SIZE(op))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 Py_XDECREF(newitem);
276 PyErr_SetString(PyExc_IndexError,
277 "list assignment index out of range");
278 return -1;
279 }
280 p = ((PyListObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300281 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283}
284
285static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000286ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_ssize_t i, n = Py_SIZE(self);
289 PyObject **items;
290 if (v == NULL) {
291 PyErr_BadInternalCall();
292 return -1;
293 }
294 if (n == PY_SSIZE_T_MAX) {
295 PyErr_SetString(PyExc_OverflowError,
296 "cannot add more objects to list");
297 return -1;
298 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000299
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800300 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (where < 0) {
304 where += n;
305 if (where < 0)
306 where = 0;
307 }
308 if (where > n)
309 where = n;
310 items = self->ob_item;
311 for (i = n; --i >= where; )
312 items[i+1] = items[i];
313 Py_INCREF(v);
314 items[where] = v;
315 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316}
317
318int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000319PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 if (!PyList_Check(op)) {
322 PyErr_BadInternalCall();
323 return -1;
324 }
325 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326}
327
Raymond Hettinger40a03822004-04-12 13:05:09 +0000328static int
329app1(PyListObject *self, PyObject *v)
330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 assert (v != NULL);
334 if (n == PY_SSIZE_T_MAX) {
335 PyErr_SetString(PyExc_OverflowError,
336 "cannot add more objects to list");
337 return -1;
338 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000339
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800340 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 Py_INCREF(v);
344 PyList_SET_ITEM(self, n, v);
345 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000346}
347
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348int
Fred Drakea2f55112000-07-09 15:16:51 +0000349PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 if (PyList_Check(op) && (newitem != NULL))
352 return app1((PyListObject *)op, newitem);
353 PyErr_BadInternalCall();
354 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355}
356
357/* Methods */
358
359static void
Fred Drakea2f55112000-07-09 15:16:51 +0000360list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 Py_ssize_t i;
363 PyObject_GC_UnTrack(op);
364 Py_TRASHCAN_SAFE_BEGIN(op)
365 if (op->ob_item != NULL) {
366 /* Do it backwards, for Christian Tismer.
367 There's a simple test case where somehow this reduces
368 thrashing when a *very* large list is created and
369 immediately deleted. */
370 i = Py_SIZE(op);
371 while (--i >= 0) {
372 Py_XDECREF(op->ob_item[i]);
373 }
374 PyMem_FREE(op->ob_item);
375 }
376 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
377 free_list[numfree++] = op;
378 else
379 Py_TYPE(op)->tp_free((PyObject *)op);
380 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381}
382
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000384list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 Py_ssize_t i;
Victor Stinner5c733472013-11-18 21:11:57 +0100387 PyObject *s;
Victor Stinner5c733472013-11-18 21:11:57 +0100388 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200389
390 if (Py_SIZE(v) == 0) {
391 return PyUnicode_FromString("[]");
392 }
393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 i = Py_ReprEnter((PyObject*)v);
395 if (i != 0) {
396 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
397 }
Tim Petersa7259592001-06-16 05:11:17 +0000398
Victor Stinner5c733472013-11-18 21:11:57 +0100399 _PyUnicodeWriter_Init(&writer);
400 writer.overallocate = 1;
Victor Stinnerb8fb1972013-11-18 22:15:44 +0100401 /* "[" + "1" + ", 2" * (len - 1) + "]" */
402 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +0000403
Victor Stinner5c733472013-11-18 21:11:57 +0100404 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200405 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 /* Do repr() on each element. Note that this may mutate the list,
408 so must refetch the list size on each iteration. */
409 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100410 if (i > 0) {
Victor Stinner4a587072013-11-19 12:54:53 +0100411 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
Victor Stinner5c733472013-11-18 21:11:57 +0100412 goto error;
413 }
414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 s = PyObject_Repr(v->ob_item[i]);
Victor Stinner5c733472013-11-18 21:11:57 +0100416 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200417 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100418
419 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
420 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200421 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100422 }
423 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 }
Victor Stinner5c733472013-11-18 21:11:57 +0100425
Victor Stinner4d3f1092013-11-19 12:09:00 +0100426 writer.overallocate = 0;
Victor Stinner5c733472013-11-18 21:11:57 +0100427 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200428 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100431 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200432
433error:
Victor Stinner5c733472013-11-18 21:11:57 +0100434 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200435 Py_ReprLeave((PyObject *)v);
436 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437}
438
Martin v. Löwis18e16552006-02-15 17:27:45 +0000439static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000440list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443}
444
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000445static int
Fred Drakea2f55112000-07-09 15:16:51 +0000446list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 Py_ssize_t i;
449 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
452 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
453 Py_EQ);
454 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000455}
456
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000458list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700460 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (indexerr == NULL) {
462 indexerr = PyUnicode_FromString(
463 "list index out of range");
464 if (indexerr == NULL)
465 return NULL;
466 }
467 PyErr_SetObject(PyExc_IndexError, indexerr);
468 return NULL;
469 }
470 Py_INCREF(a->ob_item[i]);
471 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472}
473
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000474static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000475list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 PyListObject *np;
478 PyObject **src, **dest;
479 Py_ssize_t i, len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 len = ihigh - ilow;
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500481 np = (PyListObject *) list_new_prealloc(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (np == NULL)
483 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 src = a->ob_item + ilow;
486 dest = np->ob_item;
487 for (i = 0; i < len; i++) {
488 PyObject *v = src[i];
489 Py_INCREF(v);
490 dest[i] = v;
491 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500492 Py_SIZE(np) = len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494}
495
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000497PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (!PyList_Check(a)) {
500 PyErr_BadInternalCall();
501 return NULL;
502 }
Sergey Fedoseevef1b88b2019-02-21 11:51:52 +0500503 if (ilow < 0) {
504 ilow = 0;
505 }
506 else if (ilow > Py_SIZE(a)) {
507 ilow = Py_SIZE(a);
508 }
509 if (ihigh < ilow) {
510 ihigh = ilow;
511 }
512 else if (ihigh > Py_SIZE(a)) {
513 ihigh = Py_SIZE(a);
514 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000516}
517
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000518static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000519list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 Py_ssize_t size;
522 Py_ssize_t i;
523 PyObject **src, **dest;
524 PyListObject *np;
525 if (!PyList_Check(bb)) {
526 PyErr_Format(PyExc_TypeError,
527 "can only concatenate list (not \"%.200s\") to list",
528 bb->ob_type->tp_name);
529 return NULL;
530 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000531#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000532 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000534 size = Py_SIZE(a) + Py_SIZE(b);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500535 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 if (np == NULL) {
537 return NULL;
538 }
539 src = a->ob_item;
540 dest = np->ob_item;
541 for (i = 0; i < Py_SIZE(a); i++) {
542 PyObject *v = src[i];
543 Py_INCREF(v);
544 dest[i] = v;
545 }
546 src = b->ob_item;
547 dest = np->ob_item + Py_SIZE(a);
548 for (i = 0; i < Py_SIZE(b); i++) {
549 PyObject *v = src[i];
550 Py_INCREF(v);
551 dest[i] = v;
552 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500553 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555#undef b
556}
557
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000559list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 Py_ssize_t i, j;
562 Py_ssize_t size;
563 PyListObject *np;
564 PyObject **p, **items;
565 PyObject *elem;
566 if (n < 0)
567 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100568 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100570 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 if (size == 0)
572 return PyList_New(0);
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500573 np = (PyListObject *) list_new_prealloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 if (np == NULL)
575 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 if (Py_SIZE(a) == 1) {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500578 items = np->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 elem = a->ob_item[0];
580 for (i = 0; i < n; i++) {
581 items[i] = elem;
582 Py_INCREF(elem);
583 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500585 else {
586 p = np->ob_item;
587 items = a->ob_item;
588 for (i = 0; i < n; i++) {
589 for (j = 0; j < Py_SIZE(a); j++) {
590 *p = items[j];
591 Py_INCREF(*p);
592 p++;
593 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 }
595 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +0500596 Py_SIZE(np) = size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000598}
599
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000600static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200601_list_clear(PyListObject *a)
Armin Rigo93677f02004-07-29 12:40:23 +0000602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 Py_ssize_t i;
604 PyObject **item = a->ob_item;
605 if (item != NULL) {
606 /* Because XDECREF can recursively invoke operations on
607 this list, we make it empty first. */
608 i = Py_SIZE(a);
609 Py_SIZE(a) = 0;
610 a->ob_item = NULL;
611 a->allocated = 0;
612 while (--i >= 0) {
613 Py_XDECREF(item[i]);
614 }
615 PyMem_FREE(item);
616 }
617 /* Never fails; the return value can be ignored.
618 Note that there is no guarantee that the list is actually empty
619 at this point, because XDECREF may have populated it again! */
620 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000621}
622
Tim Peters8fc4a912004-07-31 21:53:19 +0000623/* a[ilow:ihigh] = v if v != NULL.
624 * del a[ilow:ihigh] if v == NULL.
625 *
626 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
627 * guaranteed the call cannot fail.
628 */
Armin Rigo93677f02004-07-29 12:40:23 +0000629static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000630list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 /* Because [X]DECREF can recursively invoke list operations on
633 this list, we must postpone all [X]DECREF activity until
634 after the list is back in its canonical shape. Therefore
635 we must allocate an additional array, 'recycle', into which
636 we temporarily copy the items that are deleted from the
637 list. :-( */
638 PyObject *recycle_on_stack[8];
639 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
640 PyObject **item;
641 PyObject **vitem = NULL;
642 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
643 Py_ssize_t n; /* # of elements in replacement list */
644 Py_ssize_t norig; /* # of elements in list getting replaced */
645 Py_ssize_t d; /* Change in size */
646 Py_ssize_t k;
647 size_t s;
648 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 if (v == NULL)
651 n = 0;
652 else {
653 if (a == b) {
654 /* Special case "a[i:j] = a" -- copy b first */
655 v = list_slice(b, 0, Py_SIZE(b));
656 if (v == NULL)
657 return result;
658 result = list_ass_slice(a, ilow, ihigh, v);
659 Py_DECREF(v);
660 return result;
661 }
662 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
663 if(v_as_SF == NULL)
664 goto Error;
665 n = PySequence_Fast_GET_SIZE(v_as_SF);
666 vitem = PySequence_Fast_ITEMS(v_as_SF);
667 }
668 if (ilow < 0)
669 ilow = 0;
670 else if (ilow > Py_SIZE(a))
671 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 if (ihigh < ilow)
674 ihigh = ilow;
675 else if (ihigh > Py_SIZE(a))
676 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 norig = ihigh - ilow;
679 assert(norig >= 0);
680 d = n - norig;
681 if (Py_SIZE(a) + d == 0) {
682 Py_XDECREF(v_as_SF);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200683 return _list_clear(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 }
685 item = a->ob_item;
686 /* recycle the items that we are about to remove */
687 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700688 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
689 if (s) {
690 if (s > sizeof(recycle_on_stack)) {
691 recycle = (PyObject **)PyMem_MALLOC(s);
692 if (recycle == NULL) {
693 PyErr_NoMemory();
694 goto Error;
695 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700697 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200701 Py_ssize_t tail;
702 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
703 memmove(&item[ihigh+d], &item[ihigh], tail);
704 if (list_resize(a, Py_SIZE(a) + d) < 0) {
705 memmove(&item[ihigh], &item[ihigh+d], tail);
706 memcpy(&item[ilow], recycle, s);
707 goto Error;
708 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 item = a->ob_item;
710 }
711 else if (d > 0) { /* Insert d items */
712 k = Py_SIZE(a);
713 if (list_resize(a, k+d) < 0)
714 goto Error;
715 item = a->ob_item;
716 memmove(&item[ihigh+d], &item[ihigh],
717 (k - ihigh)*sizeof(PyObject *));
718 }
719 for (k = 0; k < n; k++, ilow++) {
720 PyObject *w = vitem[k];
721 Py_XINCREF(w);
722 item[ilow] = w;
723 }
724 for (k = norig - 1; k >= 0; --k)
725 Py_XDECREF(recycle[k]);
726 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000727 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 if (recycle != recycle_on_stack)
729 PyMem_FREE(recycle);
730 Py_XDECREF(v_as_SF);
731 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000732#undef b
733}
734
Guido van Rossum234f9421993-06-17 12:35:49 +0000735int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000736PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 if (!PyList_Check(a)) {
739 PyErr_BadInternalCall();
740 return -1;
741 }
742 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000743}
744
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000745static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000746list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 PyObject **items;
749 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000750
751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 size = PyList_GET_SIZE(self);
753 if (size == 0 || n == 1) {
754 Py_INCREF(self);
755 return (PyObject *)self;
756 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 if (n < 1) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200759 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 Py_INCREF(self);
761 return (PyObject *)self;
762 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 if (size > PY_SSIZE_T_MAX / n) {
765 return PyErr_NoMemory();
766 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000767
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800768 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 p = size;
772 items = self->ob_item;
773 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
774 for (j = 0; j < size; j++) {
775 PyObject *o = items[j];
776 Py_INCREF(o);
777 items[p++] = o;
778 }
779 }
780 Py_INCREF(self);
781 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000782}
783
Guido van Rossum4a450d01991-04-03 19:05:18 +0000784static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000785list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000786{
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -0700787 if (!valid_index(i, Py_SIZE(a))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyErr_SetString(PyExc_IndexError,
789 "list assignment index out of range");
790 return -1;
791 }
792 if (v == NULL)
793 return list_ass_slice(a, i, i+1, v);
794 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300795 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000797}
798
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200799/*[clinic input]
800list.insert
801
802 index: Py_ssize_t
803 object: object
804 /
805
806Insert object before index.
807[clinic start generated code]*/
808
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000809static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200810list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
811/*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000812{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200813 if (ins1(self, index, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 Py_RETURN_NONE;
815 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816}
817
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200818/*[clinic input]
819list.clear
820
821Remove all items from list.
822[clinic start generated code]*/
823
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000824static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200825list_clear_impl(PyListObject *self)
826/*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000827{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200828 _list_clear(self);
Eli Benderskycbbaa962011-02-25 05:47:53 +0000829 Py_RETURN_NONE;
830}
831
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200832/*[clinic input]
833list.copy
834
835Return a shallow copy of the list.
836[clinic start generated code]*/
837
Eli Benderskycbbaa962011-02-25 05:47:53 +0000838static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200839list_copy_impl(PyListObject *self)
840/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
Eli Benderskycbbaa962011-02-25 05:47:53 +0000841{
842 return list_slice(self, 0, Py_SIZE(self));
843}
844
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200845/*[clinic input]
846list.append
847
848 object: object
849 /
850
851Append object to the end of the list.
852[clinic start generated code]*/
853
Eli Benderskycbbaa962011-02-25 05:47:53 +0000854static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200855list_append(PyListObject *self, PyObject *object)
856/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000857{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200858 if (app1(self, object) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 Py_RETURN_NONE;
860 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000861}
862
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200863/*[clinic input]
864list.extend
865
866 iterable: object
867 /
868
869Extend list by appending elements from the iterable.
870[clinic start generated code]*/
871
Barry Warsawdedf6d61998-10-09 16:37:25 +0000872static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200873list_extend(PyListObject *self, PyObject *iterable)
874/*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 PyObject *it; /* iter(v) */
877 Py_ssize_t m; /* size of self */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200878 Py_ssize_t n; /* guess for size of iterable */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 Py_ssize_t mn; /* m + n */
880 Py_ssize_t i;
881 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 /* Special cases:
884 1) lists and tuples which can use PySequence_Fast ops
885 2) extending self to self requires making a copy first
886 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200887 if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
888 (PyObject *)self == iterable) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 PyObject **src, **dest;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200890 iterable = PySequence_Fast(iterable, "argument must be iterable");
891 if (!iterable)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200893 n = PySequence_Fast_GET_SIZE(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 if (n == 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200895 /* short circuit when iterable is empty */
896 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 Py_RETURN_NONE;
898 }
899 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000900 /* It should not be possible to allocate a list large enough to cause
901 an overflow on any relevant platform */
902 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800903 if (list_resize(self, m + n) < 0) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200904 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 return NULL;
906 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200907 /* note that we may still have self == iterable here for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 * situation a.extend(a), but the following code works
909 * in that case too. Just make sure to resize self
910 * before calling PySequence_Fast_ITEMS.
911 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200912 /* populate the end of self with iterable's items */
913 src = PySequence_Fast_ITEMS(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 dest = self->ob_item + m;
915 for (i = 0; i < n; i++) {
916 PyObject *o = src[i];
917 Py_INCREF(o);
918 dest[i] = o;
919 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200920 Py_DECREF(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 Py_RETURN_NONE;
922 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000923
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200924 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if (it == NULL)
926 return NULL;
927 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 /* Guess a result list size. */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200930 n = PyObject_LengthHint(iterable, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800931 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 Py_DECREF(it);
933 return NULL;
934 }
935 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000936 if (m > PY_SSIZE_T_MAX - n) {
937 /* m + n overflowed; on the chance that n lied, and there really
938 * is enough room, ignore it. If n was telling the truth, we'll
939 * eventually run out of memory during the loop.
940 */
941 }
942 else {
943 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800945 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 goto error;
947 /* Make the list sane again. */
948 Py_SIZE(self) = m;
949 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 /* Run iterator to exhaustion. */
952 for (;;) {
953 PyObject *item = iternext(it);
954 if (item == NULL) {
955 if (PyErr_Occurred()) {
956 if (PyErr_ExceptionMatches(PyExc_StopIteration))
957 PyErr_Clear();
958 else
959 goto error;
960 }
961 break;
962 }
963 if (Py_SIZE(self) < self->allocated) {
964 /* steals ref */
965 PyList_SET_ITEM(self, Py_SIZE(self), item);
966 ++Py_SIZE(self);
967 }
968 else {
969 int status = app1(self, item);
970 Py_DECREF(item); /* append creates a new ref */
971 if (status < 0)
972 goto error;
973 }
974 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200977 if (Py_SIZE(self) < self->allocated) {
978 if (list_resize(self, Py_SIZE(self)) < 0)
979 goto error;
980 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 Py_DECREF(it);
983 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000984
985 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 Py_DECREF(it);
987 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000988}
989
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000990PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200991_PyList_Extend(PyListObject *self, PyObject *iterable)
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000992{
Serhiy Storchakafdd42c42017-03-11 09:19:20 +0200993 return list_extend(self, iterable);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000994}
995
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000996static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000997list_inplace_concat(PyListObject *self, PyObject *other)
998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001000
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001001 result = list_extend(self, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 if (result == NULL)
1003 return result;
1004 Py_DECREF(result);
1005 Py_INCREF(self);
1006 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001007}
1008
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001009/*[clinic input]
1010list.pop
1011
1012 index: Py_ssize_t = -1
1013 /
1014
1015Remove and return item at index (default last).
1016
1017Raises IndexError if list is empty or index is out of range.
1018[clinic start generated code]*/
1019
Raymond Hettinger97bc6182004-03-11 07:34:19 +00001020static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001021list_pop_impl(PyListObject *self, Py_ssize_t index)
1022/*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 PyObject *v;
1025 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 if (Py_SIZE(self) == 0) {
1028 /* Special-case most common failure cause */
1029 PyErr_SetString(PyExc_IndexError, "pop from empty list");
1030 return NULL;
1031 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001032 if (index < 0)
1033 index += Py_SIZE(self);
Raymond Hettingerf1aa8ae2018-10-10 20:37:28 -07001034 if (!valid_index(index, Py_SIZE(self))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1036 return NULL;
1037 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001038 v = self->ob_item[index];
1039 if (index == Py_SIZE(self) - 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +02001041 if (status >= 0)
1042 return v; /* and v now owns the reference the list had */
1043 else
1044 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 }
1046 Py_INCREF(v);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02001047 status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +02001048 if (status < 0) {
1049 Py_DECREF(v);
1050 return NULL;
1051 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001053}
1054
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001055/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
1056static void
1057reverse_slice(PyObject **lo, PyObject **hi)
1058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 --hi;
1062 while (lo < hi) {
1063 PyObject *t = *lo;
1064 *lo = *hi;
1065 *hi = t;
1066 ++lo;
1067 --hi;
1068 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +00001069}
1070
Tim Petersa64dc242002-08-01 02:13:36 +00001071/* Lots of code for an adaptive, stable, natural mergesort. There are many
1072 * pieces to this algorithm; read listsort.txt for overviews and details.
1073 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001074
Daniel Stutzbach98338222010-12-02 21:55:33 +00001075/* A sortslice contains a pointer to an array of keys and a pointer to
1076 * an array of corresponding values. In other words, keys[i]
1077 * corresponds with values[i]. If values == NULL, then the keys are
1078 * also the values.
1079 *
1080 * Several convenience routines are provided here, so that keys and
1081 * values are always moved in sync.
1082 */
1083
1084typedef struct {
1085 PyObject **keys;
1086 PyObject **values;
1087} sortslice;
1088
1089Py_LOCAL_INLINE(void)
1090sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
1091{
1092 s1->keys[i] = s2->keys[j];
1093 if (s1->values != NULL)
1094 s1->values[i] = s2->values[j];
1095}
1096
1097Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001098sortslice_copy_incr(sortslice *dst, sortslice *src)
1099{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001100 *dst->keys++ = *src->keys++;
1101 if (dst->values != NULL)
1102 *dst->values++ = *src->values++;
1103}
1104
1105Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001106sortslice_copy_decr(sortslice *dst, sortslice *src)
1107{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001108 *dst->keys-- = *src->keys--;
1109 if (dst->values != NULL)
1110 *dst->values-- = *src->values--;
1111}
1112
1113
1114Py_LOCAL_INLINE(void)
1115sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001116 Py_ssize_t n)
1117{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001118 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1119 if (s1->values != NULL)
1120 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1121}
1122
1123Py_LOCAL_INLINE(void)
1124sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001125 Py_ssize_t n)
1126{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001127 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1128 if (s1->values != NULL)
1129 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1130}
1131
1132Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001133sortslice_advance(sortslice *slice, Py_ssize_t n)
1134{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001135 slice->keys += n;
1136 if (slice->values != NULL)
1137 slice->values += n;
1138}
1139
embg1e34da42018-01-28 20:03:23 -07001140/* Comparison function: ms->key_compare, which is set at run-time in
1141 * listsort_impl to optimize for various special cases.
Tim Petersa64dc242002-08-01 02:13:36 +00001142 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1143 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001144
embg1e34da42018-01-28 20:03:23 -07001145#define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
Tim Peters66860f62002-08-04 17:47:26 +00001146
1147/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001148 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1149 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1150*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001151#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001153
embg1e34da42018-01-28 20:03:23 -07001154/* The maximum number of entries in a MergeState's pending-runs stack.
1155 * This is enough to sort arrays of size up to about
1156 * 32 * phi ** MAX_MERGE_PENDING
1157 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1158 * with 2**64 elements.
1159 */
1160#define MAX_MERGE_PENDING 85
1161
1162/* When we get into galloping mode, we stay there until both runs win less
1163 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
1164 */
1165#define MIN_GALLOP 7
1166
1167/* Avoid malloc for small temp arrays. */
1168#define MERGESTATE_TEMP_SIZE 256
1169
1170/* One MergeState exists on the stack per invocation of mergesort. It's just
1171 * a convenient way to pass state around among the helper functions.
1172 */
1173struct s_slice {
1174 sortslice base;
1175 Py_ssize_t len;
1176};
1177
1178typedef struct s_MergeState MergeState;
1179struct s_MergeState {
1180 /* This controls when we get *into* galloping mode. It's initialized
1181 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1182 * random data, and lower for highly structured data.
1183 */
1184 Py_ssize_t min_gallop;
1185
1186 /* 'a' is temp storage to help with merges. It contains room for
1187 * alloced entries.
1188 */
1189 sortslice a; /* may point to temparray below */
1190 Py_ssize_t alloced;
1191
1192 /* A stack of n pending runs yet to be merged. Run #i starts at
1193 * address base[i] and extends for len[i] elements. It's always
1194 * true (so long as the indices are in bounds) that
1195 *
1196 * pending[i].base + pending[i].len == pending[i+1].base
1197 *
1198 * so we could cut the storage for this, but it's a minor amount,
1199 * and keeping all the info explicit simplifies the code.
1200 */
1201 int n;
1202 struct s_slice pending[MAX_MERGE_PENDING];
1203
1204 /* 'a' points to this when possible, rather than muck with malloc. */
1205 PyObject *temparray[MERGESTATE_TEMP_SIZE];
1206
1207 /* This is the function we will use to compare two keys,
1208 * even when none of our special cases apply and we have to use
1209 * safe_object_compare. */
1210 int (*key_compare)(PyObject *, PyObject *, MergeState *);
1211
1212 /* This function is used by unsafe_object_compare to optimize comparisons
1213 * when we know our list is type-homogeneous but we can't assume anything else.
1214 * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
1215 PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
1216
1217 /* This function is used by unsafe_tuple_compare to compare the first elements
1218 * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
1219 * we can assume more, and use one of the special-case compares. */
1220 int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
1221};
1222
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001223/* binarysort is the best method for sorting small arrays: it does
1224 few compares, but can do data movement quadratic in the number of
1225 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001226 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001227 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001228 On entry, must have lo <= start <= hi, and that [lo, start) is already
1229 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001230 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001231 Even in case of error, the output slice will be some permutation of
1232 the input (nothing is lost or duplicated).
1233*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001234static int
embg1e34da42018-01-28 20:03:23 -07001235binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001236{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001237 Py_ssize_t k;
1238 PyObject **l, **p, **r;
1239 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001240
Daniel Stutzbach98338222010-12-02 21:55:33 +00001241 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001243 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 ++start;
1245 for (; start < hi; ++start) {
1246 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001247 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 r = start;
1249 pivot = *r;
1250 /* Invariants:
1251 * pivot >= all in [lo, l).
1252 * pivot < all in [r, start).
1253 * The second is vacuously true at the start.
1254 */
1255 assert(l < r);
1256 do {
1257 p = l + ((r - l) >> 1);
1258 IFLT(pivot, *p)
1259 r = p;
1260 else
1261 l = p+1;
1262 } while (l < r);
1263 assert(l == r);
1264 /* The invariants still hold, so pivot >= all in [lo, l) and
1265 pivot < all in [l, start), so pivot belongs at l. Note
1266 that if there are elements equal to pivot, l points to the
1267 first slot after them -- that's why this sort is stable.
1268 Slide over to make room.
1269 Caution: using memmove is much slower under MSVC 5;
1270 we're not usually moving many slots. */
1271 for (p = start; p > l; --p)
1272 *p = *(p-1);
1273 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001274 if (lo.values != NULL) {
1275 Py_ssize_t offset = lo.values - lo.keys;
1276 p = start + offset;
1277 pivot = *p;
1278 l += offset;
1279 for (p = start + offset; p > l; --p)
1280 *p = *(p-1);
1281 *l = pivot;
1282 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 }
1284 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001285
1286 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001288}
1289
Tim Petersa64dc242002-08-01 02:13:36 +00001290/*
1291Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1292is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001293
Tim Petersa64dc242002-08-01 02:13:36 +00001294 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001295
Tim Petersa64dc242002-08-01 02:13:36 +00001296or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001297
Tim Petersa64dc242002-08-01 02:13:36 +00001298 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001299
Tim Petersa64dc242002-08-01 02:13:36 +00001300Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1301For its intended use in a stable mergesort, the strictness of the defn of
1302"descending" is needed so that the caller can safely reverse a descending
1303sequence without violating stability (strict > ensures there are no equal
1304elements to get out of order).
1305
1306Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001307*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001308static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001309count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 Py_ssize_t k;
1312 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 assert(lo < hi);
1315 *descending = 0;
1316 ++lo;
1317 if (lo == hi)
1318 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 n = 2;
1321 IFLT(*lo, *(lo-1)) {
1322 *descending = 1;
1323 for (lo = lo+1; lo < hi; ++lo, ++n) {
1324 IFLT(*lo, *(lo-1))
1325 ;
1326 else
1327 break;
1328 }
1329 }
1330 else {
1331 for (lo = lo+1; lo < hi; ++lo, ++n) {
1332 IFLT(*lo, *(lo-1))
1333 break;
1334 }
1335 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001338fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001340}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001341
Tim Petersa64dc242002-08-01 02:13:36 +00001342/*
1343Locate the proper position of key in a sorted vector; if the vector contains
1344an element equal to key, return the position immediately to the left of
1345the leftmost equal element. [gallop_right() does the same except returns
1346the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001347
Tim Petersa64dc242002-08-01 02:13:36 +00001348"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001349
Tim Petersa64dc242002-08-01 02:13:36 +00001350"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1351hint is to the final result, the faster this runs.
1352
1353The return value is the int k in 0..n such that
1354
1355 a[k-1] < key <= a[k]
1356
1357pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1358key belongs at index k; or, IOW, the first k elements of a should precede
1359key, and the last n-k should follow key.
1360
1361Returns -1 on error. See listsort.txt for info on the method.
1362*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001363static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001364gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 Py_ssize_t ofs;
1367 Py_ssize_t lastofs;
1368 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 a += hint;
1373 lastofs = 0;
1374 ofs = 1;
1375 IFLT(*a, key) {
1376 /* a[hint] < key -- gallop right, until
1377 * a[hint + lastofs] < key <= a[hint + ofs]
1378 */
1379 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1380 while (ofs < maxofs) {
1381 IFLT(a[ofs], key) {
1382 lastofs = ofs;
1383 ofs = (ofs << 1) + 1;
1384 if (ofs <= 0) /* int overflow */
1385 ofs = maxofs;
1386 }
1387 else /* key <= a[hint + ofs] */
1388 break;
1389 }
1390 if (ofs > maxofs)
1391 ofs = maxofs;
1392 /* Translate back to offsets relative to &a[0]. */
1393 lastofs += hint;
1394 ofs += hint;
1395 }
1396 else {
1397 /* key <= a[hint] -- gallop left, until
1398 * a[hint - ofs] < key <= a[hint - lastofs]
1399 */
1400 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1401 while (ofs < maxofs) {
1402 IFLT(*(a-ofs), key)
1403 break;
1404 /* key <= a[hint - ofs] */
1405 lastofs = ofs;
1406 ofs = (ofs << 1) + 1;
1407 if (ofs <= 0) /* int overflow */
1408 ofs = maxofs;
1409 }
1410 if (ofs > maxofs)
1411 ofs = maxofs;
1412 /* Translate back to positive offsets relative to &a[0]. */
1413 k = lastofs;
1414 lastofs = hint - ofs;
1415 ofs = hint - k;
1416 }
1417 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1420 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1421 * right of lastofs but no farther right than ofs. Do a binary
1422 * search, with invariant a[lastofs-1] < key <= a[ofs].
1423 */
1424 ++lastofs;
1425 while (lastofs < ofs) {
1426 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 IFLT(a[m], key)
1429 lastofs = m+1; /* a[m] < key */
1430 else
1431 ofs = m; /* key <= a[m] */
1432 }
1433 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1434 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001435
1436fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001438}
1439
1440/*
1441Exactly like gallop_left(), except that if key already exists in a[0:n],
1442finds the position immediately to the right of the rightmost equal value.
1443
1444The return value is the int k in 0..n such that
1445
1446 a[k-1] <= key < a[k]
1447
1448or -1 if error.
1449
1450The code duplication is massive, but this is enough different given that
1451we're sticking to "<" comparisons that it's much harder to follow if
1452written as one routine with yet another "left or right?" flag.
1453*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001454static Py_ssize_t
embg1e34da42018-01-28 20:03:23 -07001455gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 Py_ssize_t ofs;
1458 Py_ssize_t lastofs;
1459 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 a += hint;
1464 lastofs = 0;
1465 ofs = 1;
1466 IFLT(key, *a) {
1467 /* key < a[hint] -- gallop left, until
1468 * a[hint - ofs] <= key < a[hint - lastofs]
1469 */
1470 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1471 while (ofs < maxofs) {
1472 IFLT(key, *(a-ofs)) {
1473 lastofs = ofs;
1474 ofs = (ofs << 1) + 1;
1475 if (ofs <= 0) /* int overflow */
1476 ofs = maxofs;
1477 }
1478 else /* a[hint - ofs] <= key */
1479 break;
1480 }
1481 if (ofs > maxofs)
1482 ofs = maxofs;
1483 /* Translate back to positive offsets relative to &a[0]. */
1484 k = lastofs;
1485 lastofs = hint - ofs;
1486 ofs = hint - k;
1487 }
1488 else {
1489 /* a[hint] <= key -- gallop right, until
1490 * a[hint + lastofs] <= key < a[hint + ofs]
1491 */
1492 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1493 while (ofs < maxofs) {
1494 IFLT(key, a[ofs])
1495 break;
1496 /* a[hint + ofs] <= key */
1497 lastofs = ofs;
1498 ofs = (ofs << 1) + 1;
1499 if (ofs <= 0) /* int overflow */
1500 ofs = maxofs;
1501 }
1502 if (ofs > maxofs)
1503 ofs = maxofs;
1504 /* Translate back to offsets relative to &a[0]. */
1505 lastofs += hint;
1506 ofs += hint;
1507 }
1508 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1511 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1512 * right of lastofs but no farther right than ofs. Do a binary
1513 * search, with invariant a[lastofs-1] <= key < a[ofs].
1514 */
1515 ++lastofs;
1516 while (lastofs < ofs) {
1517 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 IFLT(key, a[m])
1520 ofs = m; /* key < a[m] */
1521 else
1522 lastofs = m+1; /* a[m] <= key */
1523 }
1524 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1525 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001526
1527fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001529}
1530
Tim Petersa64dc242002-08-01 02:13:36 +00001531/* Conceptually a MergeState's constructor. */
1532static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001533merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001536 if (has_keyfunc) {
1537 /* The temporary space for merging will need at most half the list
1538 * size rounded up. Use the minimum possible space so we can use the
1539 * rest of temparray for other things. In particular, if there is
1540 * enough extra space, listsort() will use it to store the keys.
1541 */
1542 ms->alloced = (list_size + 1) / 2;
1543
1544 /* ms->alloced describes how many keys will be stored at
1545 ms->temparray, but we also need to store the values. Hence,
1546 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1547 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1548 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1549 ms->a.values = &ms->temparray[ms->alloced];
1550 }
1551 else {
1552 ms->alloced = MERGESTATE_TEMP_SIZE;
1553 ms->a.values = NULL;
1554 }
1555 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 ms->n = 0;
1557 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001558}
1559
1560/* Free all the temp memory owned by the MergeState. This must be called
1561 * when you're done with a MergeState, and may be called before then if
1562 * you want to free the temp memory early.
1563 */
1564static void
1565merge_freemem(MergeState *ms)
1566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001568 if (ms->a.keys != ms->temparray)
1569 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001570}
1571
1572/* Ensure enough temp memory for 'need' array slots is available.
1573 * Returns 0 on success and -1 if the memory can't be gotten.
1574 */
1575static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001576merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001577{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001578 int multiplier;
1579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 assert(ms != NULL);
1581 if (need <= ms->alloced)
1582 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001583
1584 multiplier = ms->a.values != NULL ? 2 : 1;
1585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 /* Don't realloc! That can cost cycles to copy the old data, but
1587 * we don't care what's in the block.
1588 */
1589 merge_freemem(ms);
embg1e34da42018-01-28 20:03:23 -07001590 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 PyErr_NoMemory();
1592 return -1;
1593 }
embg1e34da42018-01-28 20:03:23 -07001594 ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
Daniel Stutzbach98338222010-12-02 21:55:33 +00001595 * sizeof(PyObject *));
1596 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001598 if (ms->a.values != NULL)
1599 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 return 0;
1601 }
1602 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001604}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1606 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001607
Daniel Stutzbach98338222010-12-02 21:55:33 +00001608/* Merge the na elements starting at ssa with the nb elements starting at
1609 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1610 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1611 * should have na <= nb. See listsort.txt for more info. Return 0 if
1612 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001613 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001614static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001615merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1616 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001619 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 int result = -1; /* guilty until proved innocent */
1621 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001622
Daniel Stutzbach98338222010-12-02 21:55:33 +00001623 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1624 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 if (MERGE_GETMEM(ms, na) < 0)
1626 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001627 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1628 dest = ssa;
1629 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001630
Daniel Stutzbach98338222010-12-02 21:55:33 +00001631 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 --nb;
1633 if (nb == 0)
1634 goto Succeed;
1635 if (na == 1)
1636 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 min_gallop = ms->min_gallop;
1639 for (;;) {
1640 Py_ssize_t acount = 0; /* # of times A won in a row */
1641 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 /* Do the straightforward thing until (if ever) one run
1644 * appears to win consistently.
1645 */
1646 for (;;) {
1647 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001648 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if (k) {
1650 if (k < 0)
1651 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001652 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 ++bcount;
1654 acount = 0;
1655 --nb;
1656 if (nb == 0)
1657 goto Succeed;
1658 if (bcount >= min_gallop)
1659 break;
1660 }
1661 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001662 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 ++acount;
1664 bcount = 0;
1665 --na;
1666 if (na == 1)
1667 goto CopyB;
1668 if (acount >= min_gallop)
1669 break;
1670 }
1671 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 /* One run is winning so consistently that galloping may
1674 * be a huge win. So try that, and continue galloping until
1675 * (if ever) neither run appears to be winning consistently
1676 * anymore.
1677 */
1678 ++min_gallop;
1679 do {
1680 assert(na > 1 && nb > 0);
1681 min_gallop -= min_gallop > 1;
1682 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001683 k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 acount = k;
1685 if (k) {
1686 if (k < 0)
1687 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001688 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1689 sortslice_advance(&dest, k);
1690 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 na -= k;
1692 if (na == 1)
1693 goto CopyB;
1694 /* na==0 is impossible now if the comparison
1695 * function is consistent, but we can't assume
1696 * that it is.
1697 */
1698 if (na == 0)
1699 goto Succeed;
1700 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001701 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 --nb;
1703 if (nb == 0)
1704 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001705
embg1e34da42018-01-28 20:03:23 -07001706 k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 bcount = k;
1708 if (k) {
1709 if (k < 0)
1710 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001711 sortslice_memmove(&dest, 0, &ssb, 0, k);
1712 sortslice_advance(&dest, k);
1713 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 nb -= k;
1715 if (nb == 0)
1716 goto Succeed;
1717 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001718 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 --na;
1720 if (na == 1)
1721 goto CopyB;
1722 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1723 ++min_gallop; /* penalize it for leaving galloping mode */
1724 ms->min_gallop = min_gallop;
1725 }
Tim Petersa64dc242002-08-01 02:13:36 +00001726Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001728Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001730 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001732CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001734 /* The last element of ssa belongs at the end of the merge. */
1735 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1736 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001738}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001739
Daniel Stutzbach98338222010-12-02 21:55:33 +00001740/* Merge the na elements starting at pa with the nb elements starting at
1741 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1742 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1743 * should have na >= nb. See listsort.txt for more info. Return 0 if
1744 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001745 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001746static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001747merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1748 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001751 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001754
Daniel Stutzbach98338222010-12-02 21:55:33 +00001755 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1756 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 if (MERGE_GETMEM(ms, nb) < 0)
1758 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001759 dest = ssb;
1760 sortslice_advance(&dest, nb-1);
1761 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1762 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001764 ssb.keys = ms->a.keys + nb - 1;
1765 if (ssb.values != NULL)
1766 ssb.values = ms->a.values + nb - 1;
1767 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001768
Daniel Stutzbach98338222010-12-02 21:55:33 +00001769 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 --na;
1771 if (na == 0)
1772 goto Succeed;
1773 if (nb == 1)
1774 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 min_gallop = ms->min_gallop;
1777 for (;;) {
1778 Py_ssize_t acount = 0; /* # of times A won in a row */
1779 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 /* Do the straightforward thing until (if ever) one run
1782 * appears to win consistently.
1783 */
1784 for (;;) {
1785 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001786 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (k) {
1788 if (k < 0)
1789 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001790 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 ++acount;
1792 bcount = 0;
1793 --na;
1794 if (na == 0)
1795 goto Succeed;
1796 if (acount >= min_gallop)
1797 break;
1798 }
1799 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001800 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 ++bcount;
1802 acount = 0;
1803 --nb;
1804 if (nb == 1)
1805 goto CopyA;
1806 if (bcount >= min_gallop)
1807 break;
1808 }
1809 }
Tim Petersa64dc242002-08-01 02:13:36 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 /* One run is winning so consistently that galloping may
1812 * be a huge win. So try that, and continue galloping until
1813 * (if ever) neither run appears to be winning consistently
1814 * anymore.
1815 */
1816 ++min_gallop;
1817 do {
1818 assert(na > 0 && nb > 1);
1819 min_gallop -= min_gallop > 1;
1820 ms->min_gallop = min_gallop;
embg1e34da42018-01-28 20:03:23 -07001821 k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 if (k < 0)
1823 goto Fail;
1824 k = na - k;
1825 acount = k;
1826 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001827 sortslice_advance(&dest, -k);
1828 sortslice_advance(&ssa, -k);
1829 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 na -= k;
1831 if (na == 0)
1832 goto Succeed;
1833 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001834 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 --nb;
1836 if (nb == 1)
1837 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001838
embg1e34da42018-01-28 20:03:23 -07001839 k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 if (k < 0)
1841 goto Fail;
1842 k = nb - k;
1843 bcount = k;
1844 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001845 sortslice_advance(&dest, -k);
1846 sortslice_advance(&ssb, -k);
1847 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 nb -= k;
1849 if (nb == 1)
1850 goto CopyA;
1851 /* nb==0 is impossible now if the comparison
1852 * function is consistent, but we can't assume
1853 * that it is.
1854 */
1855 if (nb == 0)
1856 goto Succeed;
1857 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001858 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 --na;
1860 if (na == 0)
1861 goto Succeed;
1862 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1863 ++min_gallop; /* penalize it for leaving galloping mode */
1864 ms->min_gallop = min_gallop;
1865 }
Tim Petersa64dc242002-08-01 02:13:36 +00001866Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001868Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001870 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001872CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001874 /* The first element of ssb belongs at the front of the merge. */
1875 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1876 sortslice_advance(&dest, -na);
1877 sortslice_advance(&ssa, -na);
1878 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001880}
1881
1882/* Merge the two runs at stack indices i and i+1.
1883 * Returns 0 on success, -1 on error.
1884 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001885static Py_ssize_t
1886merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001887{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001888 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 Py_ssize_t na, nb;
1890 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 assert(ms != NULL);
1893 assert(ms->n >= 2);
1894 assert(i >= 0);
1895 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001896
Daniel Stutzbach98338222010-12-02 21:55:33 +00001897 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001899 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 nb = ms->pending[i+1].len;
1901 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001902 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 /* Record the length of the combined runs; if i is the 3rd-last
1905 * run now, also slide over the last run (which isn't involved
1906 * in this merge). The current run i+1 goes away in any case.
1907 */
1908 ms->pending[i].len = na + nb;
1909 if (i == ms->n - 3)
1910 ms->pending[i+1] = ms->pending[i+2];
1911 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 /* Where does b start in a? Elements in a before that can be
1914 * ignored (already in place).
1915 */
embg1e34da42018-01-28 20:03:23 -07001916 k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 if (k < 0)
1918 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001919 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 na -= k;
1921 if (na == 0)
1922 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 /* Where does a end in b? Elements in b after that can be
1925 * ignored (already in place).
1926 */
embg1e34da42018-01-28 20:03:23 -07001927 nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 if (nb <= 0)
1929 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 /* Merge what remains of the runs, using a temp array with
1932 * min(na, nb) elements.
1933 */
1934 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001935 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001937 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001938}
1939
1940/* Examine the stack of runs waiting to be merged, merging adjacent runs
1941 * until the stack invariants are re-established:
1942 *
1943 * 1. len[-3] > len[-2] + len[-1]
1944 * 2. len[-2] > len[-1]
1945 *
1946 * See listsort.txt for more info.
1947 *
1948 * Returns 0 on success, -1 on error.
1949 */
1950static int
1951merge_collapse(MergeState *ms)
1952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 assert(ms);
1956 while (ms->n > 1) {
1957 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001958 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1959 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 if (p[n-1].len < p[n+1].len)
1961 --n;
1962 if (merge_at(ms, n) < 0)
1963 return -1;
1964 }
1965 else if (p[n].len <= p[n+1].len) {
embg1e34da42018-01-28 20:03:23 -07001966 if (merge_at(ms, n) < 0)
1967 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 }
1969 else
1970 break;
1971 }
1972 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001973}
1974
1975/* Regardless of invariants, merge all runs on the stack until only one
1976 * remains. This is used at the end of the mergesort.
1977 *
1978 * Returns 0 on success, -1 on error.
1979 */
1980static int
1981merge_force_collapse(MergeState *ms)
1982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 assert(ms);
1986 while (ms->n > 1) {
1987 Py_ssize_t n = ms->n - 2;
1988 if (n > 0 && p[n-1].len < p[n+1].len)
1989 --n;
1990 if (merge_at(ms, n) < 0)
1991 return -1;
1992 }
1993 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001994}
1995
1996/* Compute a good value for the minimum run length; natural runs shorter
1997 * than this are boosted artificially via binary insertion.
1998 *
1999 * If n < 64, return n (it's too small to bother with fancy stuff).
2000 * Else if n is an exact power of 2, return 32.
2001 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
2002 * strictly less than, an exact power of 2.
2003 *
2004 * See listsort.txt for more info.
2005 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002006static Py_ssize_t
2007merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00002008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00002010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 assert(n >= 0);
2012 while (n >= 64) {
2013 r |= n & 1;
2014 n >>= 1;
2015 }
2016 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002017}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00002018
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002019static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00002020reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002021{
Daniel Stutzbach98338222010-12-02 21:55:33 +00002022 reverse_slice(s->keys, &s->keys[n]);
2023 if (s->values != NULL)
2024 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002025}
2026
embg1e34da42018-01-28 20:03:23 -07002027/* Here we define custom comparison functions to optimize for the cases one commonly
2028 * encounters in practice: homogeneous lists, often of one of the basic types. */
2029
2030/* This struct holds the comparison function and helper functions
2031 * selected in the pre-sort check. */
2032
2033/* These are the special case compare functions.
2034 * ms->key_compare will always point to one of these: */
2035
2036/* Heterogeneous compare: default, always safe to fall back on. */
2037static int
2038safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2039{
2040 /* No assumptions necessary! */
2041 return PyObject_RichCompareBool(v, w, Py_LT);
2042}
2043
2044/* Homogeneous compare: safe for any two compareable objects of the same type.
2045 * (ms->key_richcompare is set to ob_type->tp_richcompare in the
2046 * pre-sort check.)
2047 */
2048static int
2049unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
2050{
2051 PyObject *res_obj; int res;
2052
2053 /* No assumptions, because we check first: */
2054 if (v->ob_type->tp_richcompare != ms->key_richcompare)
2055 return PyObject_RichCompareBool(v, w, Py_LT);
2056
2057 assert(ms->key_richcompare != NULL);
2058 res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
2059
2060 if (res_obj == Py_NotImplemented) {
2061 Py_DECREF(res_obj);
2062 return PyObject_RichCompareBool(v, w, Py_LT);
2063 }
2064 if (res_obj == NULL)
2065 return -1;
2066
2067 if (PyBool_Check(res_obj)) {
2068 res = (res_obj == Py_True);
2069 }
2070 else {
2071 res = PyObject_IsTrue(res_obj);
2072 }
2073 Py_DECREF(res_obj);
2074
2075 /* Note that we can't assert
2076 * res == PyObject_RichCompareBool(v, w, Py_LT);
2077 * because of evil compare functions like this:
2078 * lambda a, b: int(random.random() * 3) - 1)
2079 * (which is actually in test_sort.py) */
2080 return res;
2081}
2082
2083/* Latin string compare: safe for any two latin (one byte per char) strings. */
2084static int
2085unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
2086{
Victor Stinner8017b802018-01-29 13:47:06 +01002087 Py_ssize_t len;
2088 int res;
embg1e34da42018-01-28 20:03:23 -07002089
2090 /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
2091 assert(v->ob_type == w->ob_type);
2092 assert(v->ob_type == &PyUnicode_Type);
2093 assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
2094 assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
2095
2096 len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
2097 res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
2098
2099 res = (res != 0 ?
2100 res < 0 :
2101 PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
2102
2103 assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
2104 return res;
2105}
2106
2107/* Bounded int compare: compare any two longs that fit in a single machine word. */
2108static int
2109unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
2110{
2111 PyLongObject *vl, *wl; sdigit v0, w0; int res;
2112
2113 /* Modified from Objects/longobject.c:long_compare, assuming: */
2114 assert(v->ob_type == w->ob_type);
2115 assert(v->ob_type == &PyLong_Type);
2116 assert(Py_ABS(Py_SIZE(v)) <= 1);
2117 assert(Py_ABS(Py_SIZE(w)) <= 1);
2118
2119 vl = (PyLongObject*)v;
2120 wl = (PyLongObject*)w;
2121
2122 v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
2123 w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
2124
2125 if (Py_SIZE(vl) < 0)
2126 v0 = -v0;
2127 if (Py_SIZE(wl) < 0)
2128 w0 = -w0;
2129
2130 res = v0 < w0;
2131 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2132 return res;
2133}
2134
2135/* Float compare: compare any two floats. */
2136static int
2137unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
2138{
2139 int res;
2140
2141 /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
2142 assert(v->ob_type == w->ob_type);
2143 assert(v->ob_type == &PyFloat_Type);
2144
2145 res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
2146 assert(res == PyObject_RichCompareBool(v, w, Py_LT));
2147 return res;
2148}
2149
2150/* Tuple compare: compare *any* two tuples, using
2151 * ms->tuple_elem_compare to compare the first elements, which is set
2152 * using the same pre-sort check as we use for ms->key_compare,
2153 * but run on the list [x[0] for x in L]. This allows us to optimize compares
2154 * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
2155 * that most tuple compares don't involve x[1:]. */
2156static int
2157unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
2158{
2159 PyTupleObject *vt, *wt;
2160 Py_ssize_t i, vlen, wlen;
2161 int k;
2162
2163 /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
2164 assert(v->ob_type == w->ob_type);
2165 assert(v->ob_type == &PyTuple_Type);
2166 assert(Py_SIZE(v) > 0);
2167 assert(Py_SIZE(w) > 0);
2168
2169 vt = (PyTupleObject *)v;
2170 wt = (PyTupleObject *)w;
2171
2172 vlen = Py_SIZE(vt);
2173 wlen = Py_SIZE(wt);
2174
2175 for (i = 0; i < vlen && i < wlen; i++) {
2176 k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
2177 if (k < 0)
2178 return -1;
2179 if (!k)
2180 break;
2181 }
2182
2183 if (i >= vlen || i >= wlen)
2184 return vlen < wlen;
2185
2186 if (i == 0)
2187 return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
2188 else
2189 return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
2190}
2191
Tim Petersa64dc242002-08-01 02:13:36 +00002192/* An adaptive, stable, natural mergesort. See listsort.txt.
2193 * Returns Py_None on success, NULL on error. Even in case of error, the
2194 * list will be some permutation of its input state (nothing is lost or
2195 * duplicated).
2196 */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002197/*[clinic input]
2198list.sort
2199
2200 *
2201 key as keyfunc: object = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002202 reverse: bool(accept={int}) = False
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002203
2204Stable sort *IN PLACE*.
2205[clinic start generated code]*/
2206
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002207static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002208list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002209/*[clinic end generated code: output=57b9f9c5e23fbe42 input=b0fcf743982c5b90]*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00002210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 Py_ssize_t nremaining;
2213 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002214 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 Py_ssize_t saved_ob_size, saved_allocated;
2216 PyObject **saved_ob_item;
2217 PyObject **final_ob_item;
2218 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002220 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 assert(self != NULL);
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002223 assert(PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 if (keyfunc == Py_None)
2225 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00002226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 /* The list is temporarily made empty, so that mutations performed
2228 * by comparison functions can't affect the slice of memory we're
2229 * sorting (allowing mutations during sorting is a core-dump
2230 * factory, since ob_item may change).
2231 */
2232 saved_ob_size = Py_SIZE(self);
2233 saved_ob_item = self->ob_item;
2234 saved_allocated = self->allocated;
2235 Py_SIZE(self) = 0;
2236 self->ob_item = NULL;
2237 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00002238
Daniel Stutzbach98338222010-12-02 21:55:33 +00002239 if (keyfunc == NULL) {
2240 keys = NULL;
2241 lo.keys = saved_ob_item;
2242 lo.values = NULL;
2243 }
2244 else {
2245 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
2246 /* Leverage stack space we allocated but won't otherwise use */
2247 keys = &ms.temparray[saved_ob_size+1];
2248 else {
2249 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04002250 if (keys == NULL) {
2251 PyErr_NoMemory();
2252 goto keyfunc_fail;
2253 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00002255
2256 for (i = 0; i < saved_ob_size ; i++) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002257 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
2258 NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002259 if (keys[i] == NULL) {
2260 for (i=i-1 ; i>=0 ; i--)
2261 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05002262 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00002263 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002264 goto keyfunc_fail;
2265 }
2266 }
2267
2268 lo.keys = keys;
2269 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002271
embg1e34da42018-01-28 20:03:23 -07002272
2273 /* The pre-sort check: here's where we decide which compare function to use.
2274 * How much optimization is safe? We test for homogeneity with respect to
2275 * several properties that are expensive to check at compare-time, and
2276 * set ms appropriately. */
2277 if (saved_ob_size > 1) {
2278 /* Assume the first element is representative of the whole list. */
2279 int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
2280 Py_SIZE(lo.keys[0]) > 0);
2281
2282 PyTypeObject* key_type = (keys_are_in_tuples ?
2283 PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
2284 lo.keys[0]->ob_type);
2285
2286 int keys_are_all_same_type = 1;
2287 int strings_are_latin = 1;
2288 int ints_are_bounded = 1;
2289
2290 /* Prove that assumption by checking every key. */
embg1e34da42018-01-28 20:03:23 -07002291 for (i=0; i < saved_ob_size; i++) {
2292
2293 if (keys_are_in_tuples &&
2294 !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
2295 keys_are_in_tuples = 0;
2296 keys_are_all_same_type = 0;
2297 break;
2298 }
2299
2300 /* Note: for lists of tuples, key is the first element of the tuple
2301 * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
2302 * for lists of tuples in the if-statement directly above. */
2303 PyObject *key = (keys_are_in_tuples ?
2304 PyTuple_GET_ITEM(lo.keys[i], 0) :
2305 lo.keys[i]);
2306
2307 if (key->ob_type != key_type) {
2308 keys_are_all_same_type = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002309 /* If keys are in tuple we must loop over the whole list to make
2310 sure all items are tuples */
2311 if (!keys_are_in_tuples) {
2312 break;
2313 }
embg1e34da42018-01-28 20:03:23 -07002314 }
2315
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002316 if (keys_are_all_same_type) {
2317 if (key_type == &PyLong_Type &&
2318 ints_are_bounded &&
2319 Py_ABS(Py_SIZE(key)) > 1) {
2320
embg1e34da42018-01-28 20:03:23 -07002321 ints_are_bounded = 0;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002322 }
2323 else if (key_type == &PyUnicode_Type &&
2324 strings_are_latin &&
2325 PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {
2326
2327 strings_are_latin = 0;
2328 }
2329 }
embg1e34da42018-01-28 20:03:23 -07002330 }
embg1e34da42018-01-28 20:03:23 -07002331
2332 /* Choose the best compare, given what we now know about the keys. */
2333 if (keys_are_all_same_type) {
2334
2335 if (key_type == &PyUnicode_Type && strings_are_latin) {
2336 ms.key_compare = unsafe_latin_compare;
2337 }
2338 else if (key_type == &PyLong_Type && ints_are_bounded) {
2339 ms.key_compare = unsafe_long_compare;
2340 }
2341 else if (key_type == &PyFloat_Type) {
2342 ms.key_compare = unsafe_float_compare;
2343 }
2344 else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
2345 ms.key_compare = unsafe_object_compare;
2346 }
Zackery Spytzebc793d2019-02-21 00:47:14 -07002347 else {
2348 ms.key_compare = safe_object_compare;
2349 }
embg1e34da42018-01-28 20:03:23 -07002350 }
2351 else {
2352 ms.key_compare = safe_object_compare;
2353 }
2354
2355 if (keys_are_in_tuples) {
2356 /* Make sure we're not dealing with tuples of tuples
2357 * (remember: here, key_type refers list [key[0] for key in keys]) */
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002358 if (key_type == &PyTuple_Type) {
embg1e34da42018-01-28 20:03:23 -07002359 ms.tuple_elem_compare = safe_object_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002360 }
2361 else {
embg1e34da42018-01-28 20:03:23 -07002362 ms.tuple_elem_compare = ms.key_compare;
Rémi Lapeyredd5417a2019-03-25 08:25:37 +01002363 }
embg1e34da42018-01-28 20:03:23 -07002364
2365 ms.key_compare = unsafe_tuple_compare;
2366 }
2367 }
2368 /* End of pre-sort check: ms is now set properly! */
2369
Daniel Stutzbach98338222010-12-02 21:55:33 +00002370 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 nremaining = saved_ob_size;
2373 if (nremaining < 2)
2374 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00002375
Benjamin Peterson05380642010-08-23 19:35:39 +00002376 /* Reverse sort stability achieved by initially reversing the list,
2377 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002378 if (reverse) {
2379 if (keys != NULL)
2380 reverse_slice(&keys[0], &keys[saved_ob_size]);
2381 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
2382 }
Benjamin Peterson05380642010-08-23 19:35:39 +00002383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 /* March over the array once, left to right, finding natural runs,
2385 * and extending short natural runs to minrun elements.
2386 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 minrun = merge_compute_minrun(nremaining);
2388 do {
2389 int descending;
2390 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 /* Identify next run. */
embg1e34da42018-01-28 20:03:23 -07002393 n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 if (n < 0)
2395 goto fail;
2396 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002397 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 /* If short, extend to min(minrun, nremaining). */
2399 if (n < minrun) {
2400 const Py_ssize_t force = nremaining <= minrun ?
2401 nremaining : minrun;
embg1e34da42018-01-28 20:03:23 -07002402 if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 goto fail;
2404 n = force;
2405 }
2406 /* Push run onto pending-runs stack, and maybe merge. */
2407 assert(ms.n < MAX_MERGE_PENDING);
2408 ms.pending[ms.n].base = lo;
2409 ms.pending[ms.n].len = n;
2410 ++ms.n;
2411 if (merge_collapse(&ms) < 0)
2412 goto fail;
2413 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002414 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 nremaining -= n;
2416 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 if (merge_force_collapse(&ms) < 0)
2419 goto fail;
2420 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002421 assert(keys == NULL
2422 ? ms.pending[0].base.keys == saved_ob_item
2423 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002425 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002426
2427succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002429fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002430 if (keys != NULL) {
2431 for (i = 0; i < saved_ob_size; i++)
2432 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002433 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002434 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 if (self->allocated != -1 && result != NULL) {
2438 /* The user mucked with the list during the sort,
2439 * and we don't already have another error to report.
2440 */
2441 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2442 result = NULL;
2443 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 if (reverse && saved_ob_size > 1)
2446 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002449
Daniel Stutzbach98338222010-12-02 21:55:33 +00002450keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 final_ob_item = self->ob_item;
2452 i = Py_SIZE(self);
2453 Py_SIZE(self) = saved_ob_size;
2454 self->ob_item = saved_ob_item;
2455 self->allocated = saved_allocated;
2456 if (final_ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002457 /* we cannot use _list_clear() for this because it does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 guarantee that the list is really empty when it returns */
2459 while (--i >= 0) {
2460 Py_XDECREF(final_ob_item[i]);
2461 }
2462 PyMem_FREE(final_ob_item);
2463 }
2464 Py_XINCREF(result);
2465 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002466}
Tim Peters330f9e92002-07-19 07:05:44 +00002467#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002468#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002469
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002470int
Fred Drakea2f55112000-07-09 15:16:51 +00002471PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 if (v == NULL || !PyList_Check(v)) {
2474 PyErr_BadInternalCall();
2475 return -1;
2476 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002477 v = list_sort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 if (v == NULL)
2479 return -1;
2480 Py_DECREF(v);
2481 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002482}
2483
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002484/*[clinic input]
2485list.reverse
2486
2487Reverse *IN PLACE*.
2488[clinic start generated code]*/
2489
Guido van Rossumb86c5492001-02-12 22:06:02 +00002490static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002491list_reverse_impl(PyListObject *self)
2492/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
Guido van Rossumb86c5492001-02-12 22:06:02 +00002493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 if (Py_SIZE(self) > 1)
2495 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2496 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002497}
2498
Guido van Rossum84c76f51990-10-30 13:32:20 +00002499int
Fred Drakea2f55112000-07-09 15:16:51 +00002500PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 if (v == NULL || !PyList_Check(v)) {
2505 PyErr_BadInternalCall();
2506 return -1;
2507 }
2508 if (Py_SIZE(self) > 1)
2509 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2510 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002511}
2512
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002513PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002514PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 if (v == NULL || !PyList_Check(v)) {
2517 PyErr_BadInternalCall();
2518 return NULL;
2519 }
Sergey Fedoseev234531b2019-02-25 21:59:12 +05002520 return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002521}
2522
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002523/*[clinic input]
2524list.index
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002525
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002526 value: object
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002527 start: slice_index(accept={int}) = 0
2528 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002529 /
2530
2531Return first index of value.
2532
2533Raises ValueError if the value is not present.
2534[clinic start generated code]*/
2535
2536static PyObject *
2537list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
2538 Py_ssize_t stop)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02002539/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002540{
2541 Py_ssize_t i;
2542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 if (start < 0) {
2544 start += Py_SIZE(self);
2545 if (start < 0)
2546 start = 0;
2547 }
2548 if (stop < 0) {
2549 stop += Py_SIZE(self);
2550 if (stop < 0)
2551 stop = 0;
2552 }
2553 for (i = start; i < stop && i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002554 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 if (cmp > 0)
2556 return PyLong_FromSsize_t(i);
2557 else if (cmp < 0)
2558 return NULL;
2559 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002560 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002562}
2563
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002564/*[clinic input]
2565list.count
2566
2567 value: object
2568 /
2569
2570Return number of occurrences of value.
2571[clinic start generated code]*/
2572
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002573static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002574list_count(PyListObject *self, PyObject *value)
2575/*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
Guido van Rossume6f7d181991-10-20 20:20:40 +00002576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 Py_ssize_t count = 0;
2578 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002581 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 if (cmp > 0)
2583 count++;
2584 else if (cmp < 0)
2585 return NULL;
2586 }
2587 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002588}
2589
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002590/*[clinic input]
2591list.remove
2592
2593 value: object
2594 /
2595
2596Remove first occurrence of value.
2597
2598Raises ValueError if the value is not present.
2599[clinic start generated code]*/
2600
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002601static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002602list_remove(PyListObject *self, PyObject *value)
2603/*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
Guido van Rossumed98d481991-03-06 13:07:53 +00002604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 for (i = 0; i < Py_SIZE(self); i++) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002608 int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 if (cmp > 0) {
2610 if (list_ass_slice(self, i, i+1,
2611 (PyObject *)NULL) == 0)
2612 Py_RETURN_NONE;
2613 return NULL;
2614 }
2615 else if (cmp < 0)
2616 return NULL;
2617 }
2618 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2619 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002620}
2621
Jeremy Hylton8caad492000-06-23 14:18:11 +00002622static int
2623list_traverse(PyListObject *o, visitproc visit, void *arg)
2624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 for (i = Py_SIZE(o); --i >= 0; )
2628 Py_VISIT(o->ob_item[i]);
2629 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002630}
2631
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002632static PyObject *
2633list_richcompare(PyObject *v, PyObject *w, int op)
2634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 PyListObject *vl, *wl;
2636 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002637
Brian Curtindfc80e32011-08-10 20:28:54 -05002638 if (!PyList_Check(v) || !PyList_Check(w))
2639 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 vl = (PyListObject *)v;
2642 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2645 /* Shortcut: if the lengths differ, the lists differ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 if (op == Py_EQ)
stratakise8b19652017-11-02 11:32:54 +01002647 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 else
stratakise8b19652017-11-02 11:32:54 +01002649 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 /* Search for the first index where items are different */
2653 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2654 int k = PyObject_RichCompareBool(vl->ob_item[i],
2655 wl->ob_item[i], Py_EQ);
2656 if (k < 0)
2657 return NULL;
2658 if (!k)
2659 break;
2660 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2663 /* No more items to compare -- compare sizes */
stratakise8b19652017-11-02 11:32:54 +01002664 Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 /* We have an item that differs -- shortcuts for EQ/NE */
2668 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002669 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 }
2671 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002672 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 /* Compare the final item again using the proper operator */
2676 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002677}
2678
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002679/*[clinic input]
2680list.__init__
2681
2682 iterable: object(c_default="NULL") = ()
2683 /
2684
2685Built-in mutable sequence.
2686
2687If no argument is given, the constructor creates a new empty list.
2688The argument must be an iterable if specified.
2689[clinic start generated code]*/
2690
Tim Peters6d6c1a32001-08-02 04:15:00 +00002691static int
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002692list___init___impl(PyListObject *self, PyObject *iterable)
2693/*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00002694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 /* Verify list invariants established by PyType_GenericAlloc() */
2696 assert(0 <= Py_SIZE(self));
2697 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2698 assert(self->ob_item != NULL ||
2699 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 /* Empty previous contents */
2702 if (self->ob_item != NULL) {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002703 (void)_list_clear(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002705 if (iterable != NULL) {
Pablo Galindo372d7052018-10-28 20:16:26 +00002706 if (_PyObject_HasLen(iterable)) {
2707 Py_ssize_t iter_len = PyObject_Size(iterable);
2708 if (iter_len == -1) {
2709 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2710 return -1;
2711 }
2712 PyErr_Clear();
2713 }
2714 if (iter_len > 0 && self->ob_item == NULL
2715 && list_preallocate_exact(self, iter_len)) {
2716 return -1;
2717 }
2718 }
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002719 PyObject *rv = list_extend(self, iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 if (rv == NULL)
2721 return -1;
2722 Py_DECREF(rv);
2723 }
2724 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002725}
2726
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002727/*[clinic input]
2728list.__sizeof__
2729
2730Return the size of the list in memory, in bytes.
2731[clinic start generated code]*/
2732
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002733static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002734list___sizeof___impl(PyListObject *self)
2735/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002738
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002739 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002741}
2742
Raymond Hettinger1021c442003-11-07 15:38:09 +00002743static PyObject *list_iter(PyObject *seq);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002744static PyObject *list_subscript(PyListObject*, PyObject*);
2745
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002746static PyMethodDef list_methods[] = {
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02002747 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
2748 LIST___REVERSED___METHODDEF
2749 LIST___SIZEOF___METHODDEF
2750 LIST_CLEAR_METHODDEF
2751 LIST_COPY_METHODDEF
2752 LIST_APPEND_METHODDEF
2753 LIST_INSERT_METHODDEF
2754 LIST_EXTEND_METHODDEF
2755 LIST_POP_METHODDEF
2756 LIST_REMOVE_METHODDEF
2757 LIST_INDEX_METHODDEF
2758 LIST_COUNT_METHODDEF
2759 LIST_REVERSE_METHODDEF
2760 LIST_SORT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002762};
2763
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002764static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 (lenfunc)list_length, /* sq_length */
2766 (binaryfunc)list_concat, /* sq_concat */
2767 (ssizeargfunc)list_repeat, /* sq_repeat */
2768 (ssizeargfunc)list_item, /* sq_item */
2769 0, /* sq_slice */
2770 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2771 0, /* sq_ass_slice */
2772 (objobjproc)list_contains, /* sq_contains */
2773 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2774 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002775};
2776
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002777static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002778list_subscript(PyListObject* self, PyObject* item)
2779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 if (PyIndex_Check(item)) {
2781 Py_ssize_t i;
2782 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2783 if (i == -1 && PyErr_Occurred())
2784 return NULL;
2785 if (i < 0)
2786 i += PyList_GET_SIZE(self);
2787 return list_item(self, i);
2788 }
2789 else if (PySlice_Check(item)) {
2790 Py_ssize_t start, stop, step, slicelength, cur, i;
2791 PyObject* result;
2792 PyObject* it;
2793 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002794
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002795 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 return NULL;
2797 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002798 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2799 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 if (slicelength <= 0) {
2802 return PyList_New(0);
2803 }
2804 else if (step == 1) {
2805 return list_slice(self, start, stop);
2806 }
2807 else {
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002808 result = list_new_prealloc(slicelength);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 src = self->ob_item;
2812 dest = ((PyListObject *)result)->ob_item;
2813 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002814 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 it = src[cur];
2816 Py_INCREF(it);
2817 dest[i] = it;
2818 }
Sergey Fedoseev2fc46972018-08-11 18:12:07 +05002819 Py_SIZE(result) = slicelength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 return result;
2821 }
2822 }
2823 else {
2824 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002825 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 item->ob_type->tp_name);
2827 return NULL;
2828 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002829}
2830
Tim Peters3b01a122002-07-19 02:35:45 +00002831static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002832list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 if (PyIndex_Check(item)) {
2835 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2836 if (i == -1 && PyErr_Occurred())
2837 return -1;
2838 if (i < 0)
2839 i += PyList_GET_SIZE(self);
2840 return list_ass_item(self, i, value);
2841 }
2842 else if (PySlice_Check(item)) {
2843 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002844
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002845 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 return -1;
2847 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002848 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2849 step);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 if (step == 1)
2852 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 /* Make sure s[5:2] = [..] inserts at the right place:
2855 before 5, not before 2. */
2856 if ((step < 0 && start < stop) ||
2857 (step > 0 && start > stop))
2858 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 if (value == NULL) {
2861 /* delete slice */
2862 PyObject **garbage;
2863 size_t cur;
2864 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002865 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 if (slicelength <= 0)
2868 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 if (step < 0) {
2871 stop = start + 1;
2872 start = stop + step*(slicelength - 1) - 1;
2873 step = -step;
2874 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 garbage = (PyObject**)
2877 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2878 if (!garbage) {
2879 PyErr_NoMemory();
2880 return -1;
2881 }
Tim Peters3b01a122002-07-19 02:35:45 +00002882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 /* drawing pictures might help understand these for
2884 loops. Basically, we memmove the parts of the
2885 list that are *not* part of the slice: step-1
2886 items for each item that is part of the slice,
2887 and then tail end of the list that was not
2888 covered by the slice */
2889 for (cur = start, i = 0;
2890 cur < (size_t)stop;
2891 cur += step, i++) {
2892 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 if (cur + step >= (size_t)Py_SIZE(self)) {
2897 lim = Py_SIZE(self) - cur - 1;
2898 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 memmove(self->ob_item + cur - i,
2901 self->ob_item + cur + 1,
2902 lim * sizeof(PyObject *));
2903 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002904 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 if (cur < (size_t)Py_SIZE(self)) {
2906 memmove(self->ob_item + cur - slicelength,
2907 self->ob_item + cur,
2908 (Py_SIZE(self) - cur) *
2909 sizeof(PyObject *));
2910 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002913 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 for (i = 0; i < slicelength; i++) {
2916 Py_DECREF(garbage[i]);
2917 }
2918 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002919
Victor Stinner35f28032013-11-21 12:16:35 +01002920 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 }
2922 else {
2923 /* assign slice */
2924 PyObject *ins, *seq;
2925 PyObject **garbage, **seqitems, **selfitems;
2926 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 /* protect against a[::-1] = a */
2929 if (self == (PyListObject*)value) {
2930 seq = list_slice((PyListObject*)value, 0,
2931 PyList_GET_SIZE(value));
2932 }
2933 else {
2934 seq = PySequence_Fast(value,
2935 "must assign iterable "
2936 "to extended slice");
2937 }
2938 if (!seq)
2939 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2942 PyErr_Format(PyExc_ValueError,
2943 "attempt to assign sequence of "
2944 "size %zd to extended slice of "
2945 "size %zd",
2946 PySequence_Fast_GET_SIZE(seq),
2947 slicelength);
2948 Py_DECREF(seq);
2949 return -1;
2950 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 if (!slicelength) {
2953 Py_DECREF(seq);
2954 return 0;
2955 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 garbage = (PyObject**)
2958 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2959 if (!garbage) {
2960 Py_DECREF(seq);
2961 PyErr_NoMemory();
2962 return -1;
2963 }
Tim Peters3b01a122002-07-19 02:35:45 +00002964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002965 selfitems = self->ob_item;
2966 seqitems = PySequence_Fast_ITEMS(seq);
2967 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002968 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 garbage[i] = selfitems[cur];
2970 ins = seqitems[i];
2971 Py_INCREF(ins);
2972 selfitems[cur] = ins;
2973 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 for (i = 0; i < slicelength; i++) {
2976 Py_DECREF(garbage[i]);
2977 }
Tim Peters3b01a122002-07-19 02:35:45 +00002978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 PyMem_FREE(garbage);
2980 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 return 0;
2983 }
2984 }
2985 else {
2986 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002987 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 item->ob_type->tp_name);
2989 return -1;
2990 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002991}
2992
2993static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 (lenfunc)list_length,
2995 (binaryfunc)list_subscript,
2996 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002997};
2998
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002999PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3001 "list",
3002 sizeof(PyListObject),
3003 0,
3004 (destructor)list_dealloc, /* tp_dealloc */
3005 0, /* tp_print */
3006 0, /* tp_getattr */
3007 0, /* tp_setattr */
3008 0, /* tp_reserved */
3009 (reprfunc)list_repr, /* tp_repr */
3010 0, /* tp_as_number */
3011 &list_as_sequence, /* tp_as_sequence */
3012 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003013 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 0, /* tp_call */
3015 0, /* tp_str */
3016 PyObject_GenericGetAttr, /* tp_getattro */
3017 0, /* tp_setattro */
3018 0, /* tp_as_buffer */
3019 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003020 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
3021 list___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 (traverseproc)list_traverse, /* tp_traverse */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003023 (inquiry)_list_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 list_richcompare, /* tp_richcompare */
3025 0, /* tp_weaklistoffset */
3026 list_iter, /* tp_iter */
3027 0, /* tp_iternext */
3028 list_methods, /* tp_methods */
3029 0, /* tp_members */
3030 0, /* tp_getset */
3031 0, /* tp_base */
3032 0, /* tp_dict */
3033 0, /* tp_descr_get */
3034 0, /* tp_descr_set */
3035 0, /* tp_dictoffset */
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003036 (initproc)list___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 PyType_GenericAlloc, /* tp_alloc */
3038 PyType_GenericNew, /* tp_new */
3039 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003040};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00003041
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003042/*********************** List Iterator **************************/
3043
3044typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02003046 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003048} listiterobject;
3049
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003050static void listiter_dealloc(listiterobject *);
3051static int listiter_traverse(listiterobject *, visitproc, void *);
3052static PyObject *listiter_next(listiterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303053static PyObject *listiter_len(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003054static PyObject *listiter_reduce_general(void *_it, int forward);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303055static PyObject *listiter_reduce(listiterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003056static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00003057
Armin Rigof5b3e362006-02-11 21:32:43 +00003058PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003059PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3060PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003061
3062static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003064 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
3065 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00003067};
3068
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003069PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3071 "list_iterator", /* tp_name */
3072 sizeof(listiterobject), /* tp_basicsize */
3073 0, /* tp_itemsize */
3074 /* methods */
3075 (destructor)listiter_dealloc, /* tp_dealloc */
3076 0, /* tp_print */
3077 0, /* tp_getattr */
3078 0, /* tp_setattr */
3079 0, /* tp_reserved */
3080 0, /* tp_repr */
3081 0, /* tp_as_number */
3082 0, /* tp_as_sequence */
3083 0, /* tp_as_mapping */
3084 0, /* tp_hash */
3085 0, /* tp_call */
3086 0, /* tp_str */
3087 PyObject_GenericGetAttr, /* tp_getattro */
3088 0, /* tp_setattro */
3089 0, /* tp_as_buffer */
3090 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3091 0, /* tp_doc */
3092 (traverseproc)listiter_traverse, /* tp_traverse */
3093 0, /* tp_clear */
3094 0, /* tp_richcompare */
3095 0, /* tp_weaklistoffset */
3096 PyObject_SelfIter, /* tp_iter */
3097 (iternextfunc)listiter_next, /* tp_iternext */
3098 listiter_methods, /* tp_methods */
3099 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00003100};
Raymond Hettinger1021c442003-11-07 15:38:09 +00003101
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003102
3103static PyObject *
3104list_iter(PyObject *seq)
3105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 if (!PyList_Check(seq)) {
3109 PyErr_BadInternalCall();
3110 return NULL;
3111 }
3112 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
3113 if (it == NULL)
3114 return NULL;
3115 it->it_index = 0;
3116 Py_INCREF(seq);
3117 it->it_seq = (PyListObject *)seq;
3118 _PyObject_GC_TRACK(it);
3119 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003120}
3121
3122static void
3123listiter_dealloc(listiterobject *it)
3124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 _PyObject_GC_UNTRACK(it);
3126 Py_XDECREF(it->it_seq);
3127 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003128}
3129
3130static int
3131listiter_traverse(listiterobject *it, visitproc visit, void *arg)
3132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 Py_VISIT(it->it_seq);
3134 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003135}
3136
3137static PyObject *
3138listiter_next(listiterobject *it)
3139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 PyListObject *seq;
3141 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 assert(it != NULL);
3144 seq = it->it_seq;
3145 if (seq == NULL)
3146 return NULL;
3147 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 if (it->it_index < PyList_GET_SIZE(seq)) {
3150 item = PyList_GET_ITEM(seq, it->it_index);
3151 ++it->it_index;
3152 Py_INCREF(item);
3153 return item;
3154 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003157 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003159}
3160
3161static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303162listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 Py_ssize_t len;
3165 if (it->it_seq) {
3166 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
3167 if (len >= 0)
3168 return PyLong_FromSsize_t(len);
3169 }
3170 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003171}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003172
3173static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303174listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003175{
3176 return listiter_reduce_general(it, 1);
3177}
3178
3179static PyObject *
3180listiter_setstate(listiterobject *it, PyObject *state)
3181{
Victor Stinner7660b882013-06-24 23:59:24 +02003182 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003183 if (index == -1 && PyErr_Occurred())
3184 return NULL;
3185 if (it->it_seq != NULL) {
3186 if (index < 0)
3187 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003188 else if (index > PyList_GET_SIZE(it->it_seq))
3189 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003190 it->it_index = index;
3191 }
3192 Py_RETURN_NONE;
3193}
3194
Raymond Hettinger1021c442003-11-07 15:38:09 +00003195/*********************** List Reverse Iterator **************************/
3196
3197typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 PyObject_HEAD
3199 Py_ssize_t it_index;
3200 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00003201} listreviterobject;
3202
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003203static void listreviter_dealloc(listreviterobject *);
3204static int listreviter_traverse(listreviterobject *, visitproc, void *);
3205static PyObject *listreviter_next(listreviterobject *);
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303206static PyObject *listreviter_len(listreviterobject *, PyObject *);
3207static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003208static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003209
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003210static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003212 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
3213 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00003215};
3216
Raymond Hettinger1021c442003-11-07 15:38:09 +00003217PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3219 "list_reverseiterator", /* tp_name */
3220 sizeof(listreviterobject), /* tp_basicsize */
3221 0, /* tp_itemsize */
3222 /* methods */
3223 (destructor)listreviter_dealloc, /* tp_dealloc */
3224 0, /* tp_print */
3225 0, /* tp_getattr */
3226 0, /* tp_setattr */
3227 0, /* tp_reserved */
3228 0, /* tp_repr */
3229 0, /* tp_as_number */
3230 0, /* tp_as_sequence */
3231 0, /* tp_as_mapping */
3232 0, /* tp_hash */
3233 0, /* tp_call */
3234 0, /* tp_str */
3235 PyObject_GenericGetAttr, /* tp_getattro */
3236 0, /* tp_setattro */
3237 0, /* tp_as_buffer */
3238 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3239 0, /* tp_doc */
3240 (traverseproc)listreviter_traverse, /* tp_traverse */
3241 0, /* tp_clear */
3242 0, /* tp_richcompare */
3243 0, /* tp_weaklistoffset */
3244 PyObject_SelfIter, /* tp_iter */
3245 (iternextfunc)listreviter_next, /* tp_iternext */
3246 listreviter_methods, /* tp_methods */
3247 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00003248};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003249
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003250/*[clinic input]
3251list.__reversed__
3252
3253Return a reverse iterator over the list.
3254[clinic start generated code]*/
3255
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003256static PyObject *
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003257list___reversed___impl(PyListObject *self)
3258/*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003262 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
3263 if (it == NULL)
3264 return NULL;
Serhiy Storchakafdd42c42017-03-11 09:19:20 +02003265 assert(PyList_Check(self));
3266 it->it_index = PyList_GET_SIZE(self) - 1;
3267 Py_INCREF(self);
3268 it->it_seq = self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 PyObject_GC_Track(it);
3270 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003271}
3272
3273static void
3274listreviter_dealloc(listreviterobject *it)
3275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 PyObject_GC_UnTrack(it);
3277 Py_XDECREF(it->it_seq);
3278 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003279}
3280
3281static int
3282listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
3283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 Py_VISIT(it->it_seq);
3285 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003286}
3287
3288static PyObject *
3289listreviter_next(listreviterobject *it)
3290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003292 Py_ssize_t index;
3293 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003294
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003295 assert(it != NULL);
3296 seq = it->it_seq;
3297 if (seq == NULL) {
3298 return NULL;
3299 }
3300 assert(PyList_Check(seq));
3301
3302 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 if (index>=0 && index < PyList_GET_SIZE(seq)) {
3304 item = PyList_GET_ITEM(seq, index);
3305 it->it_index--;
3306 Py_INCREF(item);
3307 return item;
3308 }
3309 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003310 it->it_seq = NULL;
3311 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003313}
3314
Raymond Hettingerf5b64112008-12-02 21:33:45 +00003315static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303316listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 Py_ssize_t len = it->it_index + 1;
3319 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
3320 len = 0;
3321 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003322}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003323
3324static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303325listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003326{
3327 return listiter_reduce_general(it, 0);
3328}
3329
3330static PyObject *
3331listreviter_setstate(listreviterobject *it, PyObject *state)
3332{
3333 Py_ssize_t index = PyLong_AsSsize_t(state);
3334 if (index == -1 && PyErr_Occurred())
3335 return NULL;
3336 if (it->it_seq != NULL) {
3337 if (index < -1)
3338 index = -1;
3339 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
3340 index = PyList_GET_SIZE(it->it_seq) - 1;
3341 it->it_index = index;
3342 }
3343 Py_RETURN_NONE;
3344}
3345
3346/* common pickling support */
3347
3348static PyObject *
3349listiter_reduce_general(void *_it, int forward)
3350{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003351 _Py_IDENTIFIER(iter);
3352 _Py_IDENTIFIER(reversed);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003353 PyObject *list;
3354
3355 /* the objects are not the same, index is of different types! */
3356 if (forward) {
3357 listiterobject *it = (listiterobject *)_it;
3358 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003359 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003360 it->it_seq, it->it_index);
3361 } else {
3362 listreviterobject *it = (listreviterobject *)_it;
3363 if (it->it_seq)
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003364 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_reversed),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003365 it->it_seq, it->it_index);
3366 }
3367 /* empty iterator, create an empty list */
3368 list = PyList_New(0);
3369 if (list == NULL)
3370 return NULL;
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003371 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003372}