blob: 59f68819d4905316c6a58ce9bc0b0e0141ed5614 [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"
Antoine Pitrou0197ff92012-03-22 14:38:16 +01004#include "accu.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00005
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00006#ifdef STDC_HEADERS
7#include <stddef.h>
8#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00009#include <sys/types.h> /* For size_t */
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000010#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011
Tim Peters8d9eb102004-07-31 02:24:20 +000012/* Ensure ob_item has room for at least newsize elements, and set
13 * ob_size to newsize. If newsize > ob_size on entry, the content
14 * of the new slots at exit is undefined heap trash; it's the caller's
Ezio Melotti13925002011-03-16 11:05:33 +020015 * responsibility to overwrite them with sane values.
Tim Peters8d9eb102004-07-31 02:24:20 +000016 * The number of allocated elements may grow, shrink, or stay the same.
17 * Failure is impossible if newsize <= self.allocated on entry, although
18 * that partly relies on an assumption that the system realloc() never
19 * fails when passed a number of bytes <= the number of bytes last
20 * allocated (the C standard doesn't guarantee this, but it's hard to
21 * imagine a realloc implementation where it wouldn't be true).
22 * Note that self->ob_item may change, and even if newsize is less
23 * than ob_size on entry.
24 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000025static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000026list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 PyObject **items;
Xiang Zhang4cee0492017-02-22 12:32:30 +080029 size_t new_allocated, num_allocated_bytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 /* Bypass realloc() when a previous overallocation is large enough
33 to accommodate the newsize. If the newsize falls lower than half
34 the allocated size, then proceed with the realloc() to shrink the list.
35 */
36 if (allocated >= newsize && newsize >= (allocated >> 1)) {
37 assert(self->ob_item != NULL || newsize == 0);
38 Py_SIZE(self) = newsize;
39 return 0;
40 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 /* This over-allocates proportional to the list size, making room
43 * for additional growth. The over-allocation is mild, but is
44 * enough to give linear-time amortized behavior over a long
45 * sequence of appends() in the presence of a poorly-performing
46 * system realloc().
47 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
Xiang Zhang4cee0492017-02-22 12:32:30 +080048 * Note: new_allocated won't overflow because the largest possible value
49 * is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 */
Xiang Zhang4cee0492017-02-22 12:32:30 +080051 new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
52 if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 PyErr_NoMemory();
54 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 if (newsize == 0)
58 new_allocated = 0;
Xiang Zhang4cee0492017-02-22 12:32:30 +080059 num_allocated_bytes = new_allocated * sizeof(PyObject *);
60 items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 if (items == NULL) {
62 PyErr_NoMemory();
63 return -1;
64 }
65 self->ob_item = items;
66 Py_SIZE(self) = newsize;
67 self->allocated = new_allocated;
68 return 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000069}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000070
Christian Heimes77c02eb2008-02-09 02:18:51 +000071/* Debug statistic to compare allocations with reuse through the free list */
72#undef SHOW_ALLOC_COUNT
73#ifdef SHOW_ALLOC_COUNT
74static size_t count_alloc = 0;
75static size_t count_reuse = 0;
76
77static void
78show_alloc(void)
79{
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +030080 PyObject *xoptions, *value;
81 _Py_IDENTIFIER(showalloccount);
82
83 xoptions = PySys_GetXOptions();
84 if (xoptions == NULL)
85 return;
86 value = _PyDict_GetItemId(xoptions, &PyId_showalloccount);
87 if (value != Py_True)
88 return;
89
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
91 count_alloc);
92 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
93 "d\n", count_reuse);
94 fprintf(stderr, "%.2f%% reuse rate\n\n",
95 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +000096}
97#endif
98
Raymond Hettinger0468e412004-05-05 05:37:53 +000099/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000100#ifndef PyList_MAXFREELIST
101#define PyList_MAXFREELIST 80
102#endif
103static PyListObject *free_list[PyList_MAXFREELIST];
104static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +0000105
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100106int
107PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100110 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 while (numfree) {
112 op = free_list[--numfree];
113 assert(PyList_CheckExact(op));
114 PyObject_GC_Del(op);
115 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100116 return ret;
117}
118
119void
120PyList_Fini(void)
121{
122 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000123}
124
David Malcolm49526f42012-06-22 14:55:41 -0400125/* Print summary info about the state of the optimized allocator */
126void
127_PyList_DebugMallocStats(FILE *out)
128{
129 _PyDebugAllocatorStats(out,
130 "free PyListObject",
131 numfree, sizeof(PyListObject));
132}
133
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000135PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 PyListObject *op;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000138#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 static int initialized = 0;
140 if (!initialized) {
141 Py_AtExit(show_alloc);
142 initialized = 1;
143 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000144#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 if (size < 0) {
147 PyErr_BadInternalCall();
148 return NULL;
149 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 if (numfree) {
151 numfree--;
152 op = free_list[numfree];
153 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000154#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000156#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 } else {
158 op = PyObject_GC_New(PyListObject, &PyList_Type);
159 if (op == NULL)
160 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000161#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000163#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 }
165 if (size <= 0)
166 op->ob_item = NULL;
167 else {
Mark Dickinson5d132382016-08-21 08:55:15 +0100168 op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 if (op->ob_item == NULL) {
170 Py_DECREF(op);
171 return PyErr_NoMemory();
172 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 }
174 Py_SIZE(op) = size;
175 op->allocated = size;
176 _PyObject_GC_TRACK(op);
177 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000178}
179
Martin v. Löwis18e16552006-02-15 17:27:45 +0000180Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000181PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 if (!PyList_Check(op)) {
184 PyErr_BadInternalCall();
185 return -1;
186 }
187 else
188 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189}
190
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000191static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000192
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000193PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000194PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 if (!PyList_Check(op)) {
197 PyErr_BadInternalCall();
198 return NULL;
199 }
200 if (i < 0 || i >= Py_SIZE(op)) {
201 if (indexerr == NULL) {
202 indexerr = PyUnicode_FromString(
203 "list index out of range");
204 if (indexerr == NULL)
205 return NULL;
206 }
207 PyErr_SetObject(PyExc_IndexError, indexerr);
208 return NULL;
209 }
210 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211}
212
213int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200214PyList_SetItem(PyObject *op, Py_ssize_t i,
215 PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200217 PyObject **p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if (!PyList_Check(op)) {
219 Py_XDECREF(newitem);
220 PyErr_BadInternalCall();
221 return -1;
222 }
223 if (i < 0 || i >= Py_SIZE(op)) {
224 Py_XDECREF(newitem);
225 PyErr_SetString(PyExc_IndexError,
226 "list assignment index out of range");
227 return -1;
228 }
229 p = ((PyListObject *)op) -> ob_item + i;
Serhiy Storchakaec397562016-04-06 09:50:03 +0300230 Py_XSETREF(*p, newitem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232}
233
234static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000235ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 Py_ssize_t i, n = Py_SIZE(self);
238 PyObject **items;
239 if (v == NULL) {
240 PyErr_BadInternalCall();
241 return -1;
242 }
243 if (n == PY_SSIZE_T_MAX) {
244 PyErr_SetString(PyExc_OverflowError,
245 "cannot add more objects to list");
246 return -1;
247 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000248
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800249 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 if (where < 0) {
253 where += n;
254 if (where < 0)
255 where = 0;
256 }
257 if (where > n)
258 where = n;
259 items = self->ob_item;
260 for (i = n; --i >= where; )
261 items[i+1] = items[i];
262 Py_INCREF(v);
263 items[where] = v;
264 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265}
266
267int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000268PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (!PyList_Check(op)) {
271 PyErr_BadInternalCall();
272 return -1;
273 }
274 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275}
276
Raymond Hettinger40a03822004-04-12 13:05:09 +0000277static int
278app1(PyListObject *self, PyObject *v)
279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 assert (v != NULL);
283 if (n == PY_SSIZE_T_MAX) {
284 PyErr_SetString(PyExc_OverflowError,
285 "cannot add more objects to list");
286 return -1;
287 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000288
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800289 if (list_resize(self, n+1) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 Py_INCREF(v);
293 PyList_SET_ITEM(self, n, v);
294 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000295}
296
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297int
Fred Drakea2f55112000-07-09 15:16:51 +0000298PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 if (PyList_Check(op) && (newitem != NULL))
301 return app1((PyListObject *)op, newitem);
302 PyErr_BadInternalCall();
303 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304}
305
306/* Methods */
307
308static void
Fred Drakea2f55112000-07-09 15:16:51 +0000309list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 Py_ssize_t i;
312 PyObject_GC_UnTrack(op);
313 Py_TRASHCAN_SAFE_BEGIN(op)
314 if (op->ob_item != NULL) {
315 /* Do it backwards, for Christian Tismer.
316 There's a simple test case where somehow this reduces
317 thrashing when a *very* large list is created and
318 immediately deleted. */
319 i = Py_SIZE(op);
320 while (--i >= 0) {
321 Py_XDECREF(op->ob_item[i]);
322 }
323 PyMem_FREE(op->ob_item);
324 }
325 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
326 free_list[numfree++] = op;
327 else
328 Py_TYPE(op)->tp_free((PyObject *)op);
329 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330}
331
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000332static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000333list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 Py_ssize_t i;
Victor Stinner5c733472013-11-18 21:11:57 +0100336 PyObject *s;
Victor Stinner5c733472013-11-18 21:11:57 +0100337 _PyUnicodeWriter writer;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200338
339 if (Py_SIZE(v) == 0) {
340 return PyUnicode_FromString("[]");
341 }
342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 i = Py_ReprEnter((PyObject*)v);
344 if (i != 0) {
345 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
346 }
Tim Petersa7259592001-06-16 05:11:17 +0000347
Victor Stinner5c733472013-11-18 21:11:57 +0100348 _PyUnicodeWriter_Init(&writer);
349 writer.overallocate = 1;
Victor Stinnerb8fb1972013-11-18 22:15:44 +0100350 /* "[" + "1" + ", 2" * (len - 1) + "]" */
351 writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +0000352
Victor Stinner5c733472013-11-18 21:11:57 +0100353 if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200354 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 /* Do repr() on each element. Note that this may mutate the list,
357 so must refetch the list size on each iteration. */
358 for (i = 0; i < Py_SIZE(v); ++i) {
Victor Stinner5c733472013-11-18 21:11:57 +0100359 if (i > 0) {
Victor Stinner4a587072013-11-19 12:54:53 +0100360 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
Victor Stinner5c733472013-11-18 21:11:57 +0100361 goto error;
362 }
363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 if (Py_EnterRecursiveCall(" while getting the repr of a list"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200365 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 s = PyObject_Repr(v->ob_item[i]);
367 Py_LeaveRecursiveCall();
Victor Stinner5c733472013-11-18 21:11:57 +0100368 if (s == NULL)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200369 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100370
371 if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
372 Py_DECREF(s);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200373 goto error;
Victor Stinner5c733472013-11-18 21:11:57 +0100374 }
375 Py_DECREF(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 }
Victor Stinner5c733472013-11-18 21:11:57 +0100377
Victor Stinner4d3f1092013-11-19 12:09:00 +0100378 writer.overallocate = 0;
Victor Stinner5c733472013-11-18 21:11:57 +0100379 if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200380 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 Py_ReprLeave((PyObject *)v);
Victor Stinner5c733472013-11-18 21:11:57 +0100383 return _PyUnicodeWriter_Finish(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200384
385error:
Victor Stinner5c733472013-11-18 21:11:57 +0100386 _PyUnicodeWriter_Dealloc(&writer);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200387 Py_ReprLeave((PyObject *)v);
388 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389}
390
Martin v. Löwis18e16552006-02-15 17:27:45 +0000391static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000392list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395}
396
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000397static int
Fred Drakea2f55112000-07-09 15:16:51 +0000398list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 Py_ssize_t i;
401 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
404 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
405 Py_EQ);
406 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000407}
408
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000409static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000410list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 if (i < 0 || i >= Py_SIZE(a)) {
413 if (indexerr == NULL) {
414 indexerr = PyUnicode_FromString(
415 "list index out of range");
416 if (indexerr == NULL)
417 return NULL;
418 }
419 PyErr_SetObject(PyExc_IndexError, indexerr);
420 return NULL;
421 }
422 Py_INCREF(a->ob_item[i]);
423 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424}
425
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000426static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000427list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 PyListObject *np;
430 PyObject **src, **dest;
431 Py_ssize_t i, len;
432 if (ilow < 0)
433 ilow = 0;
434 else if (ilow > Py_SIZE(a))
435 ilow = Py_SIZE(a);
436 if (ihigh < ilow)
437 ihigh = ilow;
438 else if (ihigh > Py_SIZE(a))
439 ihigh = Py_SIZE(a);
440 len = ihigh - ilow;
441 np = (PyListObject *) PyList_New(len);
442 if (np == NULL)
443 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 src = a->ob_item + ilow;
446 dest = np->ob_item;
447 for (i = 0; i < len; i++) {
448 PyObject *v = src[i];
449 Py_INCREF(v);
450 dest[i] = v;
451 }
452 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453}
454
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000456PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 if (!PyList_Check(a)) {
459 PyErr_BadInternalCall();
460 return NULL;
461 }
462 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000463}
464
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000466list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 Py_ssize_t size;
469 Py_ssize_t i;
470 PyObject **src, **dest;
471 PyListObject *np;
472 if (!PyList_Check(bb)) {
473 PyErr_Format(PyExc_TypeError,
474 "can only concatenate list (not \"%.200s\") to list",
475 bb->ob_type->tp_name);
476 return NULL;
477 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000478#define b ((PyListObject *)bb)
Martin Panterb93d8632016-07-25 02:39:20 +0000479 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 return PyErr_NoMemory();
Martin Panterb93d8632016-07-25 02:39:20 +0000481 size = Py_SIZE(a) + Py_SIZE(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 np = (PyListObject *) PyList_New(size);
483 if (np == NULL) {
484 return NULL;
485 }
486 src = a->ob_item;
487 dest = np->ob_item;
488 for (i = 0; i < Py_SIZE(a); i++) {
489 PyObject *v = src[i];
490 Py_INCREF(v);
491 dest[i] = v;
492 }
493 src = b->ob_item;
494 dest = np->ob_item + Py_SIZE(a);
495 for (i = 0; i < Py_SIZE(b); i++) {
496 PyObject *v = src[i];
497 Py_INCREF(v);
498 dest[i] = v;
499 }
500 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000501#undef b
502}
503
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000504static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000505list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 Py_ssize_t i, j;
508 Py_ssize_t size;
509 PyListObject *np;
510 PyObject **p, **items;
511 PyObject *elem;
512 if (n < 0)
513 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100514 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100516 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 if (size == 0)
518 return PyList_New(0);
519 np = (PyListObject *) PyList_New(size);
520 if (np == NULL)
521 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 items = np->ob_item;
524 if (Py_SIZE(a) == 1) {
525 elem = a->ob_item[0];
526 for (i = 0; i < n; i++) {
527 items[i] = elem;
528 Py_INCREF(elem);
529 }
530 return (PyObject *) np;
531 }
532 p = np->ob_item;
533 items = a->ob_item;
534 for (i = 0; i < n; i++) {
535 for (j = 0; j < Py_SIZE(a); j++) {
536 *p = items[j];
537 Py_INCREF(*p);
538 p++;
539 }
540 }
541 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000542}
543
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544static int
Armin Rigo93677f02004-07-29 12:40:23 +0000545list_clear(PyListObject *a)
546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 Py_ssize_t i;
548 PyObject **item = a->ob_item;
549 if (item != NULL) {
550 /* Because XDECREF can recursively invoke operations on
551 this list, we make it empty first. */
552 i = Py_SIZE(a);
553 Py_SIZE(a) = 0;
554 a->ob_item = NULL;
555 a->allocated = 0;
556 while (--i >= 0) {
557 Py_XDECREF(item[i]);
558 }
559 PyMem_FREE(item);
560 }
561 /* Never fails; the return value can be ignored.
562 Note that there is no guarantee that the list is actually empty
563 at this point, because XDECREF may have populated it again! */
564 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000565}
566
Tim Peters8fc4a912004-07-31 21:53:19 +0000567/* a[ilow:ihigh] = v if v != NULL.
568 * del a[ilow:ihigh] if v == NULL.
569 *
570 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
571 * guaranteed the call cannot fail.
572 */
Armin Rigo93677f02004-07-29 12:40:23 +0000573static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000574list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 /* Because [X]DECREF can recursively invoke list operations on
577 this list, we must postpone all [X]DECREF activity until
578 after the list is back in its canonical shape. Therefore
579 we must allocate an additional array, 'recycle', into which
580 we temporarily copy the items that are deleted from the
581 list. :-( */
582 PyObject *recycle_on_stack[8];
583 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
584 PyObject **item;
585 PyObject **vitem = NULL;
586 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
587 Py_ssize_t n; /* # of elements in replacement list */
588 Py_ssize_t norig; /* # of elements in list getting replaced */
589 Py_ssize_t d; /* Change in size */
590 Py_ssize_t k;
591 size_t s;
592 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 if (v == NULL)
595 n = 0;
596 else {
597 if (a == b) {
598 /* Special case "a[i:j] = a" -- copy b first */
599 v = list_slice(b, 0, Py_SIZE(b));
600 if (v == NULL)
601 return result;
602 result = list_ass_slice(a, ilow, ihigh, v);
603 Py_DECREF(v);
604 return result;
605 }
606 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
607 if(v_as_SF == NULL)
608 goto Error;
609 n = PySequence_Fast_GET_SIZE(v_as_SF);
610 vitem = PySequence_Fast_ITEMS(v_as_SF);
611 }
612 if (ilow < 0)
613 ilow = 0;
614 else if (ilow > Py_SIZE(a))
615 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 if (ihigh < ilow)
618 ihigh = ilow;
619 else if (ihigh > Py_SIZE(a))
620 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 norig = ihigh - ilow;
623 assert(norig >= 0);
624 d = n - norig;
625 if (Py_SIZE(a) + d == 0) {
626 Py_XDECREF(v_as_SF);
627 return list_clear(a);
628 }
629 item = a->ob_item;
630 /* recycle the items that we are about to remove */
631 s = norig * sizeof(PyObject *);
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700632 /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
633 if (s) {
634 if (s > sizeof(recycle_on_stack)) {
635 recycle = (PyObject **)PyMem_MALLOC(s);
636 if (recycle == NULL) {
637 PyErr_NoMemory();
638 goto Error;
639 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 }
Benjamin Peterson5a7d9232016-09-06 17:58:25 -0700641 memcpy(recycle, &item[ilow], s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 }
Tim Peters8d9eb102004-07-31 02:24:20 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 if (d < 0) { /* Delete -d items */
Victor Stinner2c40f642013-07-19 23:06:21 +0200645 Py_ssize_t tail;
646 tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
647 memmove(&item[ihigh+d], &item[ihigh], tail);
648 if (list_resize(a, Py_SIZE(a) + d) < 0) {
649 memmove(&item[ihigh], &item[ihigh+d], tail);
650 memcpy(&item[ilow], recycle, s);
651 goto Error;
652 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 item = a->ob_item;
654 }
655 else if (d > 0) { /* Insert d items */
656 k = Py_SIZE(a);
657 if (list_resize(a, k+d) < 0)
658 goto Error;
659 item = a->ob_item;
660 memmove(&item[ihigh+d], &item[ihigh],
661 (k - ihigh)*sizeof(PyObject *));
662 }
663 for (k = 0; k < n; k++, ilow++) {
664 PyObject *w = vitem[k];
665 Py_XINCREF(w);
666 item[ilow] = w;
667 }
668 for (k = norig - 1; k >= 0; --k)
669 Py_XDECREF(recycle[k]);
670 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000671 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 if (recycle != recycle_on_stack)
673 PyMem_FREE(recycle);
674 Py_XDECREF(v_as_SF);
675 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000676#undef b
677}
678
Guido van Rossum234f9421993-06-17 12:35:49 +0000679int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000680PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 if (!PyList_Check(a)) {
683 PyErr_BadInternalCall();
684 return -1;
685 }
686 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000687}
688
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000689static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000690list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 PyObject **items;
693 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000694
695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 size = PyList_GET_SIZE(self);
697 if (size == 0 || n == 1) {
698 Py_INCREF(self);
699 return (PyObject *)self;
700 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 if (n < 1) {
703 (void)list_clear(self);
704 Py_INCREF(self);
705 return (PyObject *)self;
706 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 if (size > PY_SSIZE_T_MAX / n) {
709 return PyErr_NoMemory();
710 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000711
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800712 if (list_resize(self, size*n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 p = size;
716 items = self->ob_item;
717 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
718 for (j = 0; j < size; j++) {
719 PyObject *o = items[j];
720 Py_INCREF(o);
721 items[p++] = o;
722 }
723 }
724 Py_INCREF(self);
725 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000726}
727
Guido van Rossum4a450d01991-04-03 19:05:18 +0000728static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000729list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 if (i < 0 || i >= Py_SIZE(a)) {
732 PyErr_SetString(PyExc_IndexError,
733 "list assignment index out of range");
734 return -1;
735 }
736 if (v == NULL)
737 return list_ass_slice(a, i, i+1, v);
738 Py_INCREF(v);
Serhiy Storchakaf01e4082016-04-10 18:12:01 +0300739 Py_SETREF(a->ob_item[i], v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000741}
742
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000743static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000744listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 Py_ssize_t i;
747 PyObject *v;
748 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
749 return NULL;
750 if (ins1(self, i, v) == 0)
751 Py_RETURN_NONE;
752 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000753}
754
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000755static PyObject *
Eli Benderskycbbaa962011-02-25 05:47:53 +0000756listclear(PyListObject *self)
757{
758 list_clear(self);
759 Py_RETURN_NONE;
760}
761
762static PyObject *
763listcopy(PyListObject *self)
764{
765 return list_slice(self, 0, Py_SIZE(self));
766}
767
768static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000769listappend(PyListObject *self, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 if (app1(self, v) == 0)
772 Py_RETURN_NONE;
773 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000774}
775
Barry Warsawdedf6d61998-10-09 16:37:25 +0000776static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000777listextend(PyListObject *self, PyObject *b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 PyObject *it; /* iter(v) */
780 Py_ssize_t m; /* size of self */
781 Py_ssize_t n; /* guess for size of b */
782 Py_ssize_t mn; /* m + n */
783 Py_ssize_t i;
784 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 /* Special cases:
787 1) lists and tuples which can use PySequence_Fast ops
788 2) extending self to self requires making a copy first
789 */
790 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
791 PyObject **src, **dest;
792 b = PySequence_Fast(b, "argument must be iterable");
793 if (!b)
794 return NULL;
795 n = PySequence_Fast_GET_SIZE(b);
796 if (n == 0) {
797 /* short circuit when b is empty */
798 Py_DECREF(b);
799 Py_RETURN_NONE;
800 }
801 m = Py_SIZE(self);
Martin Panter94b39ce2017-01-14 06:30:37 +0000802 /* It should not be possible to allocate a list large enough to cause
803 an overflow on any relevant platform */
804 assert(m < PY_SSIZE_T_MAX - n);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800805 if (list_resize(self, m + n) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 Py_DECREF(b);
807 return NULL;
808 }
809 /* note that we may still have self == b here for the
810 * situation a.extend(a), but the following code works
811 * in that case too. Just make sure to resize self
812 * before calling PySequence_Fast_ITEMS.
813 */
814 /* populate the end of self with b's items */
815 src = PySequence_Fast_ITEMS(b);
816 dest = self->ob_item + m;
817 for (i = 0; i < n; i++) {
818 PyObject *o = src[i];
819 Py_INCREF(o);
820 dest[i] = o;
821 }
822 Py_DECREF(b);
823 Py_RETURN_NONE;
824 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 it = PyObject_GetIter(b);
827 if (it == NULL)
828 return NULL;
829 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 /* Guess a result list size. */
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200832 n = PyObject_LengthHint(b, 8);
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800833 if (n < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 Py_DECREF(it);
835 return NULL;
836 }
837 m = Py_SIZE(self);
Martin Panterb93d8632016-07-25 02:39:20 +0000838 if (m > PY_SSIZE_T_MAX - n) {
839 /* m + n overflowed; on the chance that n lied, and there really
840 * is enough room, ignore it. If n was telling the truth, we'll
841 * eventually run out of memory during the loop.
842 */
843 }
844 else {
845 mn = m + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 /* Make room. */
Raymond Hettinger0dceb912016-01-25 10:33:30 -0800847 if (list_resize(self, mn) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 goto error;
849 /* Make the list sane again. */
850 Py_SIZE(self) = m;
851 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 /* Run iterator to exhaustion. */
854 for (;;) {
855 PyObject *item = iternext(it);
856 if (item == NULL) {
857 if (PyErr_Occurred()) {
858 if (PyErr_ExceptionMatches(PyExc_StopIteration))
859 PyErr_Clear();
860 else
861 goto error;
862 }
863 break;
864 }
865 if (Py_SIZE(self) < self->allocated) {
866 /* steals ref */
867 PyList_SET_ITEM(self, Py_SIZE(self), item);
868 ++Py_SIZE(self);
869 }
870 else {
871 int status = app1(self, item);
872 Py_DECREF(item); /* append creates a new ref */
873 if (status < 0)
874 goto error;
875 }
876 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 /* Cut back result list if initial guess was too large. */
Victor Stinner32fd6ea2013-07-16 21:45:58 +0200879 if (Py_SIZE(self) < self->allocated) {
880 if (list_resize(self, Py_SIZE(self)) < 0)
881 goto error;
882 }
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 Py_DECREF(it);
885 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000886
887 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 Py_DECREF(it);
889 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000890}
891
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000892PyObject *
893_PyList_Extend(PyListObject *self, PyObject *b)
894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 return listextend(self, b);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000896}
897
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000898static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000899list_inplace_concat(PyListObject *self, PyObject *other)
900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 result = listextend(self, other);
904 if (result == NULL)
905 return result;
906 Py_DECREF(result);
907 Py_INCREF(self);
908 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000909}
910
911static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000912listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 Py_ssize_t i = -1;
915 PyObject *v;
916 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 if (!PyArg_ParseTuple(args, "|n:pop", &i))
919 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 if (Py_SIZE(self) == 0) {
922 /* Special-case most common failure cause */
923 PyErr_SetString(PyExc_IndexError, "pop from empty list");
924 return NULL;
925 }
926 if (i < 0)
927 i += Py_SIZE(self);
928 if (i < 0 || i >= Py_SIZE(self)) {
929 PyErr_SetString(PyExc_IndexError, "pop index out of range");
930 return NULL;
931 }
932 v = self->ob_item[i];
933 if (i == Py_SIZE(self) - 1) {
934 status = list_resize(self, Py_SIZE(self) - 1);
Victor Stinnerb27cd3e2013-07-08 22:20:44 +0200935 if (status >= 0)
936 return v; /* and v now owns the reference the list had */
937 else
938 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 }
940 Py_INCREF(v);
941 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
Victor Stinner095d99f2013-07-17 21:58:01 +0200942 if (status < 0) {
943 Py_DECREF(v);
944 return NULL;
945 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000947}
948
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000949/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
950static void
951reverse_slice(PyObject **lo, PyObject **hi)
952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 --hi;
956 while (lo < hi) {
957 PyObject *t = *lo;
958 *lo = *hi;
959 *hi = t;
960 ++lo;
961 --hi;
962 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000963}
964
Tim Petersa64dc242002-08-01 02:13:36 +0000965/* Lots of code for an adaptive, stable, natural mergesort. There are many
966 * pieces to this algorithm; read listsort.txt for overviews and details.
967 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000968
Daniel Stutzbach98338222010-12-02 21:55:33 +0000969/* A sortslice contains a pointer to an array of keys and a pointer to
970 * an array of corresponding values. In other words, keys[i]
971 * corresponds with values[i]. If values == NULL, then the keys are
972 * also the values.
973 *
974 * Several convenience routines are provided here, so that keys and
975 * values are always moved in sync.
976 */
977
978typedef struct {
979 PyObject **keys;
980 PyObject **values;
981} sortslice;
982
983Py_LOCAL_INLINE(void)
984sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
985{
986 s1->keys[i] = s2->keys[j];
987 if (s1->values != NULL)
988 s1->values[i] = s2->values[j];
989}
990
991Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000992sortslice_copy_incr(sortslice *dst, sortslice *src)
993{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000994 *dst->keys++ = *src->keys++;
995 if (dst->values != NULL)
996 *dst->values++ = *src->values++;
997}
998
999Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001000sortslice_copy_decr(sortslice *dst, sortslice *src)
1001{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001002 *dst->keys-- = *src->keys--;
1003 if (dst->values != NULL)
1004 *dst->values-- = *src->values--;
1005}
1006
1007
1008Py_LOCAL_INLINE(void)
1009sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001010 Py_ssize_t n)
1011{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001012 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1013 if (s1->values != NULL)
1014 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1015}
1016
1017Py_LOCAL_INLINE(void)
1018sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001019 Py_ssize_t n)
1020{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001021 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1022 if (s1->values != NULL)
1023 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1024}
1025
1026Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001027sortslice_advance(sortslice *slice, Py_ssize_t n)
1028{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001029 slice->keys += n;
1030 if (slice->values != NULL)
1031 slice->values += n;
1032}
1033
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001034/* Comparison function: PyObject_RichCompareBool with Py_LT.
Tim Petersa64dc242002-08-01 02:13:36 +00001035 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1036 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001037
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001038#define ISLT(X, Y) (PyObject_RichCompareBool(X, Y, Py_LT))
Tim Peters66860f62002-08-04 17:47:26 +00001039
1040/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001041 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1042 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1043*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001044#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001046
1047/* binarysort is the best method for sorting small arrays: it does
1048 few compares, but can do data movement quadratic in the number of
1049 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001050 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001051 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001052 On entry, must have lo <= start <= hi, and that [lo, start) is already
1053 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001054 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001055 Even in case of error, the output slice will be some permutation of
1056 the input (nothing is lost or duplicated).
1057*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001058static int
Daniel Stutzbach98338222010-12-02 21:55:33 +00001059binarysort(sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001060{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001061 Py_ssize_t k;
1062 PyObject **l, **p, **r;
1063 PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001064
Daniel Stutzbach98338222010-12-02 21:55:33 +00001065 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001067 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 ++start;
1069 for (; start < hi; ++start) {
1070 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001071 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 r = start;
1073 pivot = *r;
1074 /* Invariants:
1075 * pivot >= all in [lo, l).
1076 * pivot < all in [r, start).
1077 * The second is vacuously true at the start.
1078 */
1079 assert(l < r);
1080 do {
1081 p = l + ((r - l) >> 1);
1082 IFLT(pivot, *p)
1083 r = p;
1084 else
1085 l = p+1;
1086 } while (l < r);
1087 assert(l == r);
1088 /* The invariants still hold, so pivot >= all in [lo, l) and
1089 pivot < all in [l, start), so pivot belongs at l. Note
1090 that if there are elements equal to pivot, l points to the
1091 first slot after them -- that's why this sort is stable.
1092 Slide over to make room.
1093 Caution: using memmove is much slower under MSVC 5;
1094 we're not usually moving many slots. */
1095 for (p = start; p > l; --p)
1096 *p = *(p-1);
1097 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001098 if (lo.values != NULL) {
1099 Py_ssize_t offset = lo.values - lo.keys;
1100 p = start + offset;
1101 pivot = *p;
1102 l += offset;
1103 for (p = start + offset; p > l; --p)
1104 *p = *(p-1);
1105 *l = pivot;
1106 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 }
1108 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001109
1110 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001112}
1113
Tim Petersa64dc242002-08-01 02:13:36 +00001114/*
1115Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1116is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001117
Tim Petersa64dc242002-08-01 02:13:36 +00001118 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001119
Tim Petersa64dc242002-08-01 02:13:36 +00001120or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001121
Tim Petersa64dc242002-08-01 02:13:36 +00001122 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001123
Tim Petersa64dc242002-08-01 02:13:36 +00001124Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1125For its intended use in a stable mergesort, the strictness of the defn of
1126"descending" is needed so that the caller can safely reverse a descending
1127sequence without violating stability (strict > ensures there are no equal
1128elements to get out of order).
1129
1130Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001131*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001132static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001133count_run(PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 Py_ssize_t k;
1136 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 assert(lo < hi);
1139 *descending = 0;
1140 ++lo;
1141 if (lo == hi)
1142 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 n = 2;
1145 IFLT(*lo, *(lo-1)) {
1146 *descending = 1;
1147 for (lo = lo+1; lo < hi; ++lo, ++n) {
1148 IFLT(*lo, *(lo-1))
1149 ;
1150 else
1151 break;
1152 }
1153 }
1154 else {
1155 for (lo = lo+1; lo < hi; ++lo, ++n) {
1156 IFLT(*lo, *(lo-1))
1157 break;
1158 }
1159 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001162fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001164}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001165
Tim Petersa64dc242002-08-01 02:13:36 +00001166/*
1167Locate the proper position of key in a sorted vector; if the vector contains
1168an element equal to key, return the position immediately to the left of
1169the leftmost equal element. [gallop_right() does the same except returns
1170the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001171
Tim Petersa64dc242002-08-01 02:13:36 +00001172"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001173
Tim Petersa64dc242002-08-01 02:13:36 +00001174"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1175hint is to the final result, the faster this runs.
1176
1177The return value is the int k in 0..n such that
1178
1179 a[k-1] < key <= a[k]
1180
1181pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1182key belongs at index k; or, IOW, the first k elements of a should precede
1183key, and the last n-k should follow key.
1184
1185Returns -1 on error. See listsort.txt for info on the method.
1186*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001187static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001188gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 Py_ssize_t ofs;
1191 Py_ssize_t lastofs;
1192 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 a += hint;
1197 lastofs = 0;
1198 ofs = 1;
1199 IFLT(*a, key) {
1200 /* a[hint] < key -- gallop right, until
1201 * a[hint + lastofs] < key <= a[hint + ofs]
1202 */
1203 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1204 while (ofs < maxofs) {
1205 IFLT(a[ofs], key) {
1206 lastofs = ofs;
1207 ofs = (ofs << 1) + 1;
1208 if (ofs <= 0) /* int overflow */
1209 ofs = maxofs;
1210 }
1211 else /* key <= a[hint + ofs] */
1212 break;
1213 }
1214 if (ofs > maxofs)
1215 ofs = maxofs;
1216 /* Translate back to offsets relative to &a[0]. */
1217 lastofs += hint;
1218 ofs += hint;
1219 }
1220 else {
1221 /* key <= a[hint] -- gallop left, until
1222 * a[hint - ofs] < key <= a[hint - lastofs]
1223 */
1224 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1225 while (ofs < maxofs) {
1226 IFLT(*(a-ofs), key)
1227 break;
1228 /* key <= a[hint - ofs] */
1229 lastofs = ofs;
1230 ofs = (ofs << 1) + 1;
1231 if (ofs <= 0) /* int overflow */
1232 ofs = maxofs;
1233 }
1234 if (ofs > maxofs)
1235 ofs = maxofs;
1236 /* Translate back to positive offsets relative to &a[0]. */
1237 k = lastofs;
1238 lastofs = hint - ofs;
1239 ofs = hint - k;
1240 }
1241 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1244 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1245 * right of lastofs but no farther right than ofs. Do a binary
1246 * search, with invariant a[lastofs-1] < key <= a[ofs].
1247 */
1248 ++lastofs;
1249 while (lastofs < ofs) {
1250 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 IFLT(a[m], key)
1253 lastofs = m+1; /* a[m] < key */
1254 else
1255 ofs = m; /* key <= a[m] */
1256 }
1257 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1258 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001259
1260fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001262}
1263
1264/*
1265Exactly like gallop_left(), except that if key already exists in a[0:n],
1266finds the position immediately to the right of the rightmost equal value.
1267
1268The return value is the int k in 0..n such that
1269
1270 a[k-1] <= key < a[k]
1271
1272or -1 if error.
1273
1274The code duplication is massive, but this is enough different given that
1275we're sticking to "<" comparisons that it's much harder to follow if
1276written as one routine with yet another "left or right?" flag.
1277*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001278static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001279gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 Py_ssize_t ofs;
1282 Py_ssize_t lastofs;
1283 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 a += hint;
1288 lastofs = 0;
1289 ofs = 1;
1290 IFLT(key, *a) {
1291 /* key < a[hint] -- gallop left, until
1292 * a[hint - ofs] <= key < a[hint - lastofs]
1293 */
1294 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1295 while (ofs < maxofs) {
1296 IFLT(key, *(a-ofs)) {
1297 lastofs = ofs;
1298 ofs = (ofs << 1) + 1;
1299 if (ofs <= 0) /* int overflow */
1300 ofs = maxofs;
1301 }
1302 else /* a[hint - ofs] <= key */
1303 break;
1304 }
1305 if (ofs > maxofs)
1306 ofs = maxofs;
1307 /* Translate back to positive offsets relative to &a[0]. */
1308 k = lastofs;
1309 lastofs = hint - ofs;
1310 ofs = hint - k;
1311 }
1312 else {
1313 /* a[hint] <= key -- gallop right, until
1314 * a[hint + lastofs] <= key < a[hint + ofs]
1315 */
1316 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1317 while (ofs < maxofs) {
1318 IFLT(key, a[ofs])
1319 break;
1320 /* a[hint + ofs] <= key */
1321 lastofs = ofs;
1322 ofs = (ofs << 1) + 1;
1323 if (ofs <= 0) /* int overflow */
1324 ofs = maxofs;
1325 }
1326 if (ofs > maxofs)
1327 ofs = maxofs;
1328 /* Translate back to offsets relative to &a[0]. */
1329 lastofs += hint;
1330 ofs += hint;
1331 }
1332 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1335 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1336 * right of lastofs but no farther right than ofs. Do a binary
1337 * search, with invariant a[lastofs-1] <= key < a[ofs].
1338 */
1339 ++lastofs;
1340 while (lastofs < ofs) {
1341 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 IFLT(key, a[m])
1344 ofs = m; /* key < a[m] */
1345 else
1346 lastofs = m+1; /* a[m] <= key */
1347 }
1348 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1349 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001350
1351fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001353}
1354
1355/* The maximum number of entries in a MergeState's pending-runs stack.
1356 * This is enough to sort arrays of size up to about
1357 * 32 * phi ** MAX_MERGE_PENDING
1358 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1359 * with 2**64 elements.
1360 */
1361#define MAX_MERGE_PENDING 85
1362
Tim Peterse05f65a2002-08-10 05:21:15 +00001363/* When we get into galloping mode, we stay there until both runs win less
1364 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
Tim Petersa64dc242002-08-01 02:13:36 +00001365 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001366#define MIN_GALLOP 7
Tim Petersa64dc242002-08-01 02:13:36 +00001367
1368/* Avoid malloc for small temp arrays. */
1369#define MERGESTATE_TEMP_SIZE 256
1370
1371/* One MergeState exists on the stack per invocation of mergesort. It's just
1372 * a convenient way to pass state around among the helper functions.
1373 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001374struct s_slice {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001375 sortslice base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 Py_ssize_t len;
Tim Peterse05f65a2002-08-10 05:21:15 +00001377};
1378
Tim Petersa64dc242002-08-01 02:13:36 +00001379typedef struct s_MergeState {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 /* This controls when we get *into* galloping mode. It's initialized
1381 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1382 * random data, and lower for highly structured data.
1383 */
1384 Py_ssize_t min_gallop;
Tim Peterse05f65a2002-08-10 05:21:15 +00001385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 /* 'a' is temp storage to help with merges. It contains room for
1387 * alloced entries.
1388 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001389 sortslice a; /* may point to temparray below */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 Py_ssize_t alloced;
Tim Petersa64dc242002-08-01 02:13:36 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 /* A stack of n pending runs yet to be merged. Run #i starts at
1393 * address base[i] and extends for len[i] elements. It's always
1394 * true (so long as the indices are in bounds) that
1395 *
1396 * pending[i].base + pending[i].len == pending[i+1].base
1397 *
1398 * so we could cut the storage for this, but it's a minor amount,
1399 * and keeping all the info explicit simplifies the code.
1400 */
1401 int n;
1402 struct s_slice pending[MAX_MERGE_PENDING];
Tim Petersa64dc242002-08-01 02:13:36 +00001403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 /* 'a' points to this when possible, rather than muck with malloc. */
1405 PyObject *temparray[MERGESTATE_TEMP_SIZE];
Tim Petersa64dc242002-08-01 02:13:36 +00001406} MergeState;
1407
1408/* Conceptually a MergeState's constructor. */
1409static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001410merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001413 if (has_keyfunc) {
1414 /* The temporary space for merging will need at most half the list
1415 * size rounded up. Use the minimum possible space so we can use the
1416 * rest of temparray for other things. In particular, if there is
1417 * enough extra space, listsort() will use it to store the keys.
1418 */
1419 ms->alloced = (list_size + 1) / 2;
1420
1421 /* ms->alloced describes how many keys will be stored at
1422 ms->temparray, but we also need to store the values. Hence,
1423 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1424 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1425 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1426 ms->a.values = &ms->temparray[ms->alloced];
1427 }
1428 else {
1429 ms->alloced = MERGESTATE_TEMP_SIZE;
1430 ms->a.values = NULL;
1431 }
1432 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 ms->n = 0;
1434 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001435}
1436
1437/* Free all the temp memory owned by the MergeState. This must be called
1438 * when you're done with a MergeState, and may be called before then if
1439 * you want to free the temp memory early.
1440 */
1441static void
1442merge_freemem(MergeState *ms)
1443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001445 if (ms->a.keys != ms->temparray)
1446 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001447}
1448
1449/* Ensure enough temp memory for 'need' array slots is available.
1450 * Returns 0 on success and -1 if the memory can't be gotten.
1451 */
1452static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001453merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001454{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001455 int multiplier;
1456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 assert(ms != NULL);
1458 if (need <= ms->alloced)
1459 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001460
1461 multiplier = ms->a.values != NULL ? 2 : 1;
1462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 /* Don't realloc! That can cost cycles to copy the old data, but
1464 * we don't care what's in the block.
1465 */
1466 merge_freemem(ms);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001467 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 PyErr_NoMemory();
1469 return -1;
1470 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001471 ms->a.keys = (PyObject**)PyMem_Malloc(multiplier * need
1472 * sizeof(PyObject *));
1473 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001475 if (ms->a.values != NULL)
1476 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 return 0;
1478 }
1479 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001481}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1483 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001484
Daniel Stutzbach98338222010-12-02 21:55:33 +00001485/* Merge the na elements starting at ssa with the nb elements starting at
1486 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1487 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1488 * should have na <= nb. See listsort.txt for more info. Return 0 if
1489 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001490 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001491static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001492merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1493 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001496 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 int result = -1; /* guilty until proved innocent */
1498 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001499
Daniel Stutzbach98338222010-12-02 21:55:33 +00001500 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1501 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 if (MERGE_GETMEM(ms, na) < 0)
1503 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001504 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1505 dest = ssa;
1506 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001507
Daniel Stutzbach98338222010-12-02 21:55:33 +00001508 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 --nb;
1510 if (nb == 0)
1511 goto Succeed;
1512 if (na == 1)
1513 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 min_gallop = ms->min_gallop;
1516 for (;;) {
1517 Py_ssize_t acount = 0; /* # of times A won in a row */
1518 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 /* Do the straightforward thing until (if ever) one run
1521 * appears to win consistently.
1522 */
1523 for (;;) {
1524 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001525 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 if (k) {
1527 if (k < 0)
1528 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001529 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 ++bcount;
1531 acount = 0;
1532 --nb;
1533 if (nb == 0)
1534 goto Succeed;
1535 if (bcount >= min_gallop)
1536 break;
1537 }
1538 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001539 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 ++acount;
1541 bcount = 0;
1542 --na;
1543 if (na == 1)
1544 goto CopyB;
1545 if (acount >= min_gallop)
1546 break;
1547 }
1548 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 /* One run is winning so consistently that galloping may
1551 * be a huge win. So try that, and continue galloping until
1552 * (if ever) neither run appears to be winning consistently
1553 * anymore.
1554 */
1555 ++min_gallop;
1556 do {
1557 assert(na > 1 && nb > 0);
1558 min_gallop -= min_gallop > 1;
1559 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001560 k = gallop_right(ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 acount = k;
1562 if (k) {
1563 if (k < 0)
1564 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001565 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1566 sortslice_advance(&dest, k);
1567 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 na -= k;
1569 if (na == 1)
1570 goto CopyB;
1571 /* na==0 is impossible now if the comparison
1572 * function is consistent, but we can't assume
1573 * that it is.
1574 */
1575 if (na == 0)
1576 goto Succeed;
1577 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001578 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 --nb;
1580 if (nb == 0)
1581 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001582
Daniel Stutzbach98338222010-12-02 21:55:33 +00001583 k = gallop_left(ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 bcount = k;
1585 if (k) {
1586 if (k < 0)
1587 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001588 sortslice_memmove(&dest, 0, &ssb, 0, k);
1589 sortslice_advance(&dest, k);
1590 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 nb -= k;
1592 if (nb == 0)
1593 goto Succeed;
1594 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001595 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 --na;
1597 if (na == 1)
1598 goto CopyB;
1599 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1600 ++min_gallop; /* penalize it for leaving galloping mode */
1601 ms->min_gallop = min_gallop;
1602 }
Tim Petersa64dc242002-08-01 02:13:36 +00001603Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001605Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001607 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001609CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001611 /* The last element of ssa belongs at the end of the merge. */
1612 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1613 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001615}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001616
Daniel Stutzbach98338222010-12-02 21:55:33 +00001617/* Merge the na elements starting at pa with the nb elements starting at
1618 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1619 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1620 * should have na >= nb. See listsort.txt for more info. Return 0 if
1621 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001622 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001623static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001624merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1625 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001628 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001631
Daniel Stutzbach98338222010-12-02 21:55:33 +00001632 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1633 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 if (MERGE_GETMEM(ms, nb) < 0)
1635 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001636 dest = ssb;
1637 sortslice_advance(&dest, nb-1);
1638 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1639 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001641 ssb.keys = ms->a.keys + nb - 1;
1642 if (ssb.values != NULL)
1643 ssb.values = ms->a.values + nb - 1;
1644 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001645
Daniel Stutzbach98338222010-12-02 21:55:33 +00001646 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 --na;
1648 if (na == 0)
1649 goto Succeed;
1650 if (nb == 1)
1651 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 min_gallop = ms->min_gallop;
1654 for (;;) {
1655 Py_ssize_t acount = 0; /* # of times A won in a row */
1656 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 /* Do the straightforward thing until (if ever) one run
1659 * appears to win consistently.
1660 */
1661 for (;;) {
1662 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001663 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 if (k) {
1665 if (k < 0)
1666 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001667 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 ++acount;
1669 bcount = 0;
1670 --na;
1671 if (na == 0)
1672 goto Succeed;
1673 if (acount >= min_gallop)
1674 break;
1675 }
1676 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001677 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 ++bcount;
1679 acount = 0;
1680 --nb;
1681 if (nb == 1)
1682 goto CopyA;
1683 if (bcount >= min_gallop)
1684 break;
1685 }
1686 }
Tim Petersa64dc242002-08-01 02:13:36 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 /* One run is winning so consistently that galloping may
1689 * be a huge win. So try that, and continue galloping until
1690 * (if ever) neither run appears to be winning consistently
1691 * anymore.
1692 */
1693 ++min_gallop;
1694 do {
1695 assert(na > 0 && nb > 1);
1696 min_gallop -= min_gallop > 1;
1697 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001698 k = gallop_right(ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 if (k < 0)
1700 goto Fail;
1701 k = na - k;
1702 acount = k;
1703 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001704 sortslice_advance(&dest, -k);
1705 sortslice_advance(&ssa, -k);
1706 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 na -= k;
1708 if (na == 0)
1709 goto Succeed;
1710 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001711 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 --nb;
1713 if (nb == 1)
1714 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001715
Daniel Stutzbach98338222010-12-02 21:55:33 +00001716 k = gallop_left(ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 if (k < 0)
1718 goto Fail;
1719 k = nb - k;
1720 bcount = k;
1721 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001722 sortslice_advance(&dest, -k);
1723 sortslice_advance(&ssb, -k);
1724 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 nb -= k;
1726 if (nb == 1)
1727 goto CopyA;
1728 /* nb==0 is impossible now if the comparison
1729 * function is consistent, but we can't assume
1730 * that it is.
1731 */
1732 if (nb == 0)
1733 goto Succeed;
1734 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001735 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 --na;
1737 if (na == 0)
1738 goto Succeed;
1739 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1740 ++min_gallop; /* penalize it for leaving galloping mode */
1741 ms->min_gallop = min_gallop;
1742 }
Tim Petersa64dc242002-08-01 02:13:36 +00001743Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001745Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001747 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001749CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001751 /* The first element of ssb belongs at the front of the merge. */
1752 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1753 sortslice_advance(&dest, -na);
1754 sortslice_advance(&ssa, -na);
1755 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001757}
1758
1759/* Merge the two runs at stack indices i and i+1.
1760 * Returns 0 on success, -1 on error.
1761 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001762static Py_ssize_t
1763merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001764{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001765 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 Py_ssize_t na, nb;
1767 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 assert(ms != NULL);
1770 assert(ms->n >= 2);
1771 assert(i >= 0);
1772 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001773
Daniel Stutzbach98338222010-12-02 21:55:33 +00001774 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001776 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 nb = ms->pending[i+1].len;
1778 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001779 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 /* Record the length of the combined runs; if i is the 3rd-last
1782 * run now, also slide over the last run (which isn't involved
1783 * in this merge). The current run i+1 goes away in any case.
1784 */
1785 ms->pending[i].len = na + nb;
1786 if (i == ms->n - 3)
1787 ms->pending[i+1] = ms->pending[i+2];
1788 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 /* Where does b start in a? Elements in a before that can be
1791 * ignored (already in place).
1792 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001793 k = gallop_right(*ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 if (k < 0)
1795 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001796 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 na -= k;
1798 if (na == 0)
1799 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 /* Where does a end in b? Elements in b after that can be
1802 * ignored (already in place).
1803 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001804 nb = gallop_left(ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 if (nb <= 0)
1806 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 /* Merge what remains of the runs, using a temp array with
1809 * min(na, nb) elements.
1810 */
1811 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001812 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001814 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001815}
1816
1817/* Examine the stack of runs waiting to be merged, merging adjacent runs
1818 * until the stack invariants are re-established:
1819 *
1820 * 1. len[-3] > len[-2] + len[-1]
1821 * 2. len[-2] > len[-1]
1822 *
1823 * See listsort.txt for more info.
1824 *
1825 * Returns 0 on success, -1 on error.
1826 */
1827static int
1828merge_collapse(MergeState *ms)
1829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 assert(ms);
1833 while (ms->n > 1) {
1834 Py_ssize_t n = ms->n - 2;
Benjamin Petersonb808d592015-02-25 10:12:26 -05001835 if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
1836 (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 if (p[n-1].len < p[n+1].len)
1838 --n;
1839 if (merge_at(ms, n) < 0)
1840 return -1;
1841 }
1842 else if (p[n].len <= p[n+1].len) {
1843 if (merge_at(ms, n) < 0)
1844 return -1;
1845 }
1846 else
1847 break;
1848 }
1849 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001850}
1851
1852/* Regardless of invariants, merge all runs on the stack until only one
1853 * remains. This is used at the end of the mergesort.
1854 *
1855 * Returns 0 on success, -1 on error.
1856 */
1857static int
1858merge_force_collapse(MergeState *ms)
1859{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 assert(ms);
1863 while (ms->n > 1) {
1864 Py_ssize_t n = ms->n - 2;
1865 if (n > 0 && p[n-1].len < p[n+1].len)
1866 --n;
1867 if (merge_at(ms, n) < 0)
1868 return -1;
1869 }
1870 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001871}
1872
1873/* Compute a good value for the minimum run length; natural runs shorter
1874 * than this are boosted artificially via binary insertion.
1875 *
1876 * If n < 64, return n (it's too small to bother with fancy stuff).
1877 * Else if n is an exact power of 2, return 32.
1878 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1879 * strictly less than, an exact power of 2.
1880 *
1881 * See listsort.txt for more info.
1882 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001883static Py_ssize_t
1884merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 assert(n >= 0);
1889 while (n >= 64) {
1890 r |= n & 1;
1891 n >>= 1;
1892 }
1893 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001894}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001895
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001896static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001897reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001898{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001899 reverse_slice(s->keys, &s->keys[n]);
1900 if (s->values != NULL)
1901 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001902}
1903
Tim Petersa64dc242002-08-01 02:13:36 +00001904/* An adaptive, stable, natural mergesort. See listsort.txt.
1905 * Returns Py_None on success, NULL on error. Even in case of error, the
1906 * list will be some permutation of its input state (nothing is lost or
1907 * duplicated).
1908 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001909static PyObject *
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02001910listsort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001911{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 Py_ssize_t nremaining;
1914 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001915 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 Py_ssize_t saved_ob_size, saved_allocated;
1917 PyObject **saved_ob_item;
1918 PyObject **final_ob_item;
1919 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 Py_ssize_t i;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001921 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 assert(self != NULL);
1924 assert (PyList_Check(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 if (keyfunc == Py_None)
1926 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 /* The list is temporarily made empty, so that mutations performed
1929 * by comparison functions can't affect the slice of memory we're
1930 * sorting (allowing mutations during sorting is a core-dump
1931 * factory, since ob_item may change).
1932 */
1933 saved_ob_size = Py_SIZE(self);
1934 saved_ob_item = self->ob_item;
1935 saved_allocated = self->allocated;
1936 Py_SIZE(self) = 0;
1937 self->ob_item = NULL;
1938 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00001939
Daniel Stutzbach98338222010-12-02 21:55:33 +00001940 if (keyfunc == NULL) {
1941 keys = NULL;
1942 lo.keys = saved_ob_item;
1943 lo.values = NULL;
1944 }
1945 else {
1946 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
1947 /* Leverage stack space we allocated but won't otherwise use */
1948 keys = &ms.temparray[saved_ob_size+1];
1949 else {
1950 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
Benjamin Peterson0823ffb2015-04-23 17:04:36 -04001951 if (keys == NULL) {
1952 PyErr_NoMemory();
1953 goto keyfunc_fail;
1954 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001956
1957 for (i = 0; i < saved_ob_size ; i++) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001958 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
1959 NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001960 if (keys[i] == NULL) {
1961 for (i=i-1 ; i>=0 ; i--)
1962 Py_DECREF(keys[i]);
Benjamin Peterson4a42cd42014-03-15 12:21:28 -05001963 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00001964 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001965 goto keyfunc_fail;
1966 }
1967 }
1968
1969 lo.keys = keys;
1970 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001972
Daniel Stutzbach98338222010-12-02 21:55:33 +00001973 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 nremaining = saved_ob_size;
1976 if (nremaining < 2)
1977 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00001978
Benjamin Peterson05380642010-08-23 19:35:39 +00001979 /* Reverse sort stability achieved by initially reversing the list,
1980 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001981 if (reverse) {
1982 if (keys != NULL)
1983 reverse_slice(&keys[0], &keys[saved_ob_size]);
1984 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
1985 }
Benjamin Peterson05380642010-08-23 19:35:39 +00001986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 /* March over the array once, left to right, finding natural runs,
1988 * and extending short natural runs to minrun elements.
1989 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 minrun = merge_compute_minrun(nremaining);
1991 do {
1992 int descending;
1993 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00001994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 /* Identify next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001996 n = count_run(lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 if (n < 0)
1998 goto fail;
1999 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002000 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 /* If short, extend to min(minrun, nremaining). */
2002 if (n < minrun) {
2003 const Py_ssize_t force = nremaining <= minrun ?
2004 nremaining : minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00002005 if (binarysort(lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 goto fail;
2007 n = force;
2008 }
2009 /* Push run onto pending-runs stack, and maybe merge. */
2010 assert(ms.n < MAX_MERGE_PENDING);
2011 ms.pending[ms.n].base = lo;
2012 ms.pending[ms.n].len = n;
2013 ++ms.n;
2014 if (merge_collapse(&ms) < 0)
2015 goto fail;
2016 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002017 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 nremaining -= n;
2019 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 if (merge_force_collapse(&ms) < 0)
2022 goto fail;
2023 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002024 assert(keys == NULL
2025 ? ms.pending[0].base.keys == saved_ob_item
2026 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002028 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002029
2030succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002032fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002033 if (keys != NULL) {
2034 for (i = 0; i < saved_ob_size; i++)
2035 Py_DECREF(keys[i]);
Benjamin Petersonef87f8c2014-03-14 21:54:31 -05002036 if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
Daniel Stutzbach98338222010-12-02 21:55:33 +00002037 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 if (self->allocated != -1 && result != NULL) {
2041 /* The user mucked with the list during the sort,
2042 * and we don't already have another error to report.
2043 */
2044 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2045 result = NULL;
2046 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 if (reverse && saved_ob_size > 1)
2049 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002052
Daniel Stutzbach98338222010-12-02 21:55:33 +00002053keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 final_ob_item = self->ob_item;
2055 i = Py_SIZE(self);
2056 Py_SIZE(self) = saved_ob_size;
2057 self->ob_item = saved_ob_item;
2058 self->allocated = saved_allocated;
2059 if (final_ob_item != NULL) {
2060 /* we cannot use list_clear() for this because it does not
2061 guarantee that the list is really empty when it returns */
2062 while (--i >= 0) {
2063 Py_XDECREF(final_ob_item[i]);
2064 }
2065 PyMem_FREE(final_ob_item);
2066 }
2067 Py_XINCREF(result);
2068 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002069}
Tim Peters330f9e92002-07-19 07:05:44 +00002070#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002071#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002072
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002073static PyObject *
2074listsort(PyListObject *self, PyObject *args, PyObject *kwds)
2075{
2076 static char *kwlist[] = {"key", "reverse", 0};
2077 PyObject *keyfunc = NULL;
2078 int reverse = 0;
2079
2080 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|$Oi:sort",
2081 kwlist, &keyfunc, &reverse))
2082 return NULL;
2083 return listsort_impl(self, keyfunc, reverse);
2084}
2085
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002086int
Fred Drakea2f55112000-07-09 15:16:51 +00002087PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 if (v == NULL || !PyList_Check(v)) {
2090 PyErr_BadInternalCall();
2091 return -1;
2092 }
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002093 v = listsort_impl((PyListObject *)v, NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 if (v == NULL)
2095 return -1;
2096 Py_DECREF(v);
2097 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002098}
2099
Guido van Rossumb86c5492001-02-12 22:06:02 +00002100static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002101listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 if (Py_SIZE(self) > 1)
2104 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2105 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002106}
2107
Guido van Rossum84c76f51990-10-30 13:32:20 +00002108int
Fred Drakea2f55112000-07-09 15:16:51 +00002109PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 if (v == NULL || !PyList_Check(v)) {
2114 PyErr_BadInternalCall();
2115 return -1;
2116 }
2117 if (Py_SIZE(self) > 1)
2118 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2119 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002120}
2121
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002122PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002123PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 PyObject *w;
2126 PyObject **p, **q;
2127 Py_ssize_t n;
2128 if (v == NULL || !PyList_Check(v)) {
2129 PyErr_BadInternalCall();
2130 return NULL;
2131 }
2132 n = Py_SIZE(v);
2133 w = PyTuple_New(n);
2134 if (w == NULL)
2135 return NULL;
2136 p = ((PyTupleObject *)w)->ob_item;
2137 q = ((PyListObject *)v)->ob_item;
2138 while (--n >= 0) {
2139 Py_INCREF(*q);
2140 *p = *q;
2141 p++;
2142 q++;
2143 }
2144 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002145}
2146
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002147static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002148listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002151 PyObject *v;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002152
Petri Lehtinenebfaabd2011-11-06 21:02:39 +02002153 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2154 _PyEval_SliceIndex, &start,
2155 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 return NULL;
2157 if (start < 0) {
2158 start += Py_SIZE(self);
2159 if (start < 0)
2160 start = 0;
2161 }
2162 if (stop < 0) {
2163 stop += Py_SIZE(self);
2164 if (stop < 0)
2165 stop = 0;
2166 }
2167 for (i = start; i < stop && i < Py_SIZE(self); i++) {
2168 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2169 if (cmp > 0)
2170 return PyLong_FromSsize_t(i);
2171 else if (cmp < 0)
2172 return NULL;
2173 }
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002174 PyErr_Format(PyExc_ValueError, "%R is not in list", v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002176}
2177
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002178static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002179listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 Py_ssize_t count = 0;
2182 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 for (i = 0; i < Py_SIZE(self); i++) {
2185 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2186 if (cmp > 0)
2187 count++;
2188 else if (cmp < 0)
2189 return NULL;
2190 }
2191 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002192}
2193
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002194static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002195listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 for (i = 0; i < Py_SIZE(self); i++) {
2200 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2201 if (cmp > 0) {
2202 if (list_ass_slice(self, i, i+1,
2203 (PyObject *)NULL) == 0)
2204 Py_RETURN_NONE;
2205 return NULL;
2206 }
2207 else if (cmp < 0)
2208 return NULL;
2209 }
2210 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2211 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002212}
2213
Jeremy Hylton8caad492000-06-23 14:18:11 +00002214static int
2215list_traverse(PyListObject *o, visitproc visit, void *arg)
2216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 for (i = Py_SIZE(o); --i >= 0; )
2220 Py_VISIT(o->ob_item[i]);
2221 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002222}
2223
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002224static PyObject *
2225list_richcompare(PyObject *v, PyObject *w, int op)
2226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 PyListObject *vl, *wl;
2228 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002229
Brian Curtindfc80e32011-08-10 20:28:54 -05002230 if (!PyList_Check(v) || !PyList_Check(w))
2231 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 vl = (PyListObject *)v;
2234 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2237 /* Shortcut: if the lengths differ, the lists differ */
2238 PyObject *res;
2239 if (op == Py_EQ)
2240 res = Py_False;
2241 else
2242 res = Py_True;
2243 Py_INCREF(res);
2244 return res;
2245 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 /* Search for the first index where items are different */
2248 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2249 int k = PyObject_RichCompareBool(vl->ob_item[i],
2250 wl->ob_item[i], Py_EQ);
2251 if (k < 0)
2252 return NULL;
2253 if (!k)
2254 break;
2255 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2258 /* No more items to compare -- compare sizes */
2259 Py_ssize_t vs = Py_SIZE(vl);
2260 Py_ssize_t ws = Py_SIZE(wl);
2261 int cmp;
2262 PyObject *res;
2263 switch (op) {
2264 case Py_LT: cmp = vs < ws; break;
2265 case Py_LE: cmp = vs <= ws; break;
2266 case Py_EQ: cmp = vs == ws; break;
2267 case Py_NE: cmp = vs != ws; break;
2268 case Py_GT: cmp = vs > ws; break;
2269 case Py_GE: cmp = vs >= ws; break;
2270 default: return NULL; /* cannot happen */
2271 }
2272 if (cmp)
2273 res = Py_True;
2274 else
2275 res = Py_False;
2276 Py_INCREF(res);
2277 return res;
2278 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 /* We have an item that differs -- shortcuts for EQ/NE */
2281 if (op == Py_EQ) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002282 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 }
2284 if (op == Py_NE) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002285 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 /* Compare the final item again using the proper operator */
2289 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002290}
2291
Tim Peters6d6c1a32001-08-02 04:15:00 +00002292static int
2293list_init(PyListObject *self, PyObject *args, PyObject *kw)
2294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 PyObject *arg = NULL;
2296 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2299 return -1;
Serhiy Storchaka58d23e62017-03-06 00:53:39 +02002300 if (arg != NULL && PyTuple_GET_SIZE(args) == 0) {
2301 if (PyErr_Warn(PyExc_DeprecationWarning,
2302 "Using 'sequence' as a keyword argument is deprecated; "
2303 "specify the value as a positional argument instead") < 0)
2304 return -1;
2305 }
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 /* Verify list invariants established by PyType_GenericAlloc() */
2308 assert(0 <= Py_SIZE(self));
2309 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2310 assert(self->ob_item != NULL ||
2311 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 /* Empty previous contents */
2314 if (self->ob_item != NULL) {
2315 (void)list_clear(self);
2316 }
2317 if (arg != NULL) {
2318 PyObject *rv = listextend(self, arg);
2319 if (rv == NULL)
2320 return -1;
2321 Py_DECREF(rv);
2322 }
2323 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002324}
2325
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002326static PyObject *
2327list_sizeof(PyListObject *self)
2328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002330
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002331 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002333}
2334
Raymond Hettinger1021c442003-11-07 15:38:09 +00002335static PyObject *list_iter(PyObject *seq);
2336static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2337
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002338PyDoc_STRVAR(getitem_doc,
2339"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002340PyDoc_STRVAR(reversed_doc,
2341"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002342PyDoc_STRVAR(sizeof_doc,
2343"L.__sizeof__() -- size of L in memory, in bytes");
Eli Benderskycbbaa962011-02-25 05:47:53 +00002344PyDoc_STRVAR(clear_doc,
2345"L.clear() -> None -- remove all items from L");
2346PyDoc_STRVAR(copy_doc,
2347"L.copy() -> list -- a shallow copy of L");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002348PyDoc_STRVAR(append_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002349"L.append(object) -> None -- append object to end");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002350PyDoc_STRVAR(extend_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002351"L.extend(iterable) -> None -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002352PyDoc_STRVAR(insert_doc,
2353"L.insert(index, object) -- insert object before index");
2354PyDoc_STRVAR(pop_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002355"L.pop([index]) -> item -- remove and return item at index (default last).\n"
2356"Raises IndexError if list is empty or index is out of range.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002357PyDoc_STRVAR(remove_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002358"L.remove(value) -> None -- remove first occurrence of value.\n"
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002359"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002360PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002361"L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2362"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002363PyDoc_STRVAR(count_doc,
2364"L.count(value) -> integer -- return number of occurrences of value");
2365PyDoc_STRVAR(reverse_doc,
2366"L.reverse() -- reverse *IN PLACE*");
2367PyDoc_STRVAR(sort_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002368"L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002369
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002370static PyObject *list_subscript(PyListObject*, PyObject*);
2371
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002372static PyMethodDef list_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
2374 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
2375 {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002376 {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc},
2377 {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 {"append", (PyCFunction)listappend, METH_O, append_doc},
2379 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002380 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
2382 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
2383 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
2384 {"count", (PyCFunction)listcount, METH_O, count_doc},
2385 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
2386 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
2387 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002388};
2389
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002390static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 (lenfunc)list_length, /* sq_length */
2392 (binaryfunc)list_concat, /* sq_concat */
2393 (ssizeargfunc)list_repeat, /* sq_repeat */
2394 (ssizeargfunc)list_item, /* sq_item */
2395 0, /* sq_slice */
2396 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2397 0, /* sq_ass_slice */
2398 (objobjproc)list_contains, /* sq_contains */
2399 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2400 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002401};
2402
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002403PyDoc_STRVAR(list_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002404"list() -> new empty list\n"
2405"list(iterable) -> new list initialized from iterable's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002406
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002407static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002408list_subscript(PyListObject* self, PyObject* item)
2409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 if (PyIndex_Check(item)) {
2411 Py_ssize_t i;
2412 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2413 if (i == -1 && PyErr_Occurred())
2414 return NULL;
2415 if (i < 0)
2416 i += PyList_GET_SIZE(self);
2417 return list_item(self, i);
2418 }
2419 else if (PySlice_Check(item)) {
2420 Py_ssize_t start, stop, step, slicelength, cur, i;
2421 PyObject* result;
2422 PyObject* it;
2423 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002424
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002425 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 &start, &stop, &step, &slicelength) < 0) {
2427 return NULL;
2428 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 if (slicelength <= 0) {
2431 return PyList_New(0);
2432 }
2433 else if (step == 1) {
2434 return list_slice(self, start, stop);
2435 }
2436 else {
2437 result = PyList_New(slicelength);
2438 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 src = self->ob_item;
2441 dest = ((PyListObject *)result)->ob_item;
2442 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002443 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 it = src[cur];
2445 Py_INCREF(it);
2446 dest[i] = it;
2447 }
Tim Peters3b01a122002-07-19 02:35:45 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 return result;
2450 }
2451 }
2452 else {
2453 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002454 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 item->ob_type->tp_name);
2456 return NULL;
2457 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002458}
2459
Tim Peters3b01a122002-07-19 02:35:45 +00002460static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002461list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 if (PyIndex_Check(item)) {
2464 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2465 if (i == -1 && PyErr_Occurred())
2466 return -1;
2467 if (i < 0)
2468 i += PyList_GET_SIZE(self);
2469 return list_ass_item(self, i, value);
2470 }
2471 else if (PySlice_Check(item)) {
2472 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002473
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002474 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 &start, &stop, &step, &slicelength) < 0) {
2476 return -1;
2477 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 if (step == 1)
2480 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 /* Make sure s[5:2] = [..] inserts at the right place:
2483 before 5, not before 2. */
2484 if ((step < 0 && start < stop) ||
2485 (step > 0 && start > stop))
2486 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 if (value == NULL) {
2489 /* delete slice */
2490 PyObject **garbage;
2491 size_t cur;
2492 Py_ssize_t i;
Victor Stinner35f28032013-11-21 12:16:35 +01002493 int res;
Tim Peters3b01a122002-07-19 02:35:45 +00002494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 if (slicelength <= 0)
2496 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 if (step < 0) {
2499 stop = start + 1;
2500 start = stop + step*(slicelength - 1) - 1;
2501 step = -step;
2502 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 garbage = (PyObject**)
2505 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2506 if (!garbage) {
2507 PyErr_NoMemory();
2508 return -1;
2509 }
Tim Peters3b01a122002-07-19 02:35:45 +00002510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 /* drawing pictures might help understand these for
2512 loops. Basically, we memmove the parts of the
2513 list that are *not* part of the slice: step-1
2514 items for each item that is part of the slice,
2515 and then tail end of the list that was not
2516 covered by the slice */
2517 for (cur = start, i = 0;
2518 cur < (size_t)stop;
2519 cur += step, i++) {
2520 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 if (cur + step >= (size_t)Py_SIZE(self)) {
2525 lim = Py_SIZE(self) - cur - 1;
2526 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 memmove(self->ob_item + cur - i,
2529 self->ob_item + cur + 1,
2530 lim * sizeof(PyObject *));
2531 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002532 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 if (cur < (size_t)Py_SIZE(self)) {
2534 memmove(self->ob_item + cur - slicelength,
2535 self->ob_item + cur,
2536 (Py_SIZE(self) - cur) *
2537 sizeof(PyObject *));
2538 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 Py_SIZE(self) -= slicelength;
Victor Stinner35f28032013-11-21 12:16:35 +01002541 res = list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 for (i = 0; i < slicelength; i++) {
2544 Py_DECREF(garbage[i]);
2545 }
2546 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002547
Victor Stinner35f28032013-11-21 12:16:35 +01002548 return res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 }
2550 else {
2551 /* assign slice */
2552 PyObject *ins, *seq;
2553 PyObject **garbage, **seqitems, **selfitems;
2554 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 /* protect against a[::-1] = a */
2557 if (self == (PyListObject*)value) {
2558 seq = list_slice((PyListObject*)value, 0,
2559 PyList_GET_SIZE(value));
2560 }
2561 else {
2562 seq = PySequence_Fast(value,
2563 "must assign iterable "
2564 "to extended slice");
2565 }
2566 if (!seq)
2567 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2570 PyErr_Format(PyExc_ValueError,
2571 "attempt to assign sequence of "
2572 "size %zd to extended slice of "
2573 "size %zd",
2574 PySequence_Fast_GET_SIZE(seq),
2575 slicelength);
2576 Py_DECREF(seq);
2577 return -1;
2578 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 if (!slicelength) {
2581 Py_DECREF(seq);
2582 return 0;
2583 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 garbage = (PyObject**)
2586 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2587 if (!garbage) {
2588 Py_DECREF(seq);
2589 PyErr_NoMemory();
2590 return -1;
2591 }
Tim Peters3b01a122002-07-19 02:35:45 +00002592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 selfitems = self->ob_item;
2594 seqitems = PySequence_Fast_ITEMS(seq);
2595 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002596 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 garbage[i] = selfitems[cur];
2598 ins = seqitems[i];
2599 Py_INCREF(ins);
2600 selfitems[cur] = ins;
2601 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 for (i = 0; i < slicelength; i++) {
2604 Py_DECREF(garbage[i]);
2605 }
Tim Peters3b01a122002-07-19 02:35:45 +00002606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 PyMem_FREE(garbage);
2608 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610 return 0;
2611 }
2612 }
2613 else {
2614 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04002615 "list indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 item->ob_type->tp_name);
2617 return -1;
2618 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002619}
2620
2621static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 (lenfunc)list_length,
2623 (binaryfunc)list_subscript,
2624 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002625};
2626
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002627PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2629 "list",
2630 sizeof(PyListObject),
2631 0,
2632 (destructor)list_dealloc, /* tp_dealloc */
2633 0, /* tp_print */
2634 0, /* tp_getattr */
2635 0, /* tp_setattr */
2636 0, /* tp_reserved */
2637 (reprfunc)list_repr, /* tp_repr */
2638 0, /* tp_as_number */
2639 &list_as_sequence, /* tp_as_sequence */
2640 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002641 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 0, /* tp_call */
2643 0, /* tp_str */
2644 PyObject_GenericGetAttr, /* tp_getattro */
2645 0, /* tp_setattro */
2646 0, /* tp_as_buffer */
2647 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2648 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2649 list_doc, /* tp_doc */
2650 (traverseproc)list_traverse, /* tp_traverse */
2651 (inquiry)list_clear, /* tp_clear */
2652 list_richcompare, /* tp_richcompare */
2653 0, /* tp_weaklistoffset */
2654 list_iter, /* tp_iter */
2655 0, /* tp_iternext */
2656 list_methods, /* tp_methods */
2657 0, /* tp_members */
2658 0, /* tp_getset */
2659 0, /* tp_base */
2660 0, /* tp_dict */
2661 0, /* tp_descr_get */
2662 0, /* tp_descr_set */
2663 0, /* tp_dictoffset */
2664 (initproc)list_init, /* tp_init */
2665 PyType_GenericAlloc, /* tp_alloc */
2666 PyType_GenericNew, /* tp_new */
2667 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002668};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002669
2670
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002671/*********************** List Iterator **************************/
2672
2673typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 PyObject_HEAD
Victor Stinner7660b882013-06-24 23:59:24 +02002675 Py_ssize_t it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002677} listiterobject;
2678
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002679static PyObject *list_iter(PyObject *);
2680static void listiter_dealloc(listiterobject *);
2681static int listiter_traverse(listiterobject *, visitproc, void *);
2682static PyObject *listiter_next(listiterobject *);
2683static PyObject *listiter_len(listiterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002684static PyObject *listiter_reduce_general(void *_it, int forward);
2685static PyObject *listiter_reduce(listiterobject *);
2686static PyObject *listiter_setstate(listiterobject *, PyObject *state);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002687
Armin Rigof5b3e362006-02-11 21:32:43 +00002688PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002689PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2690PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002691
2692static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002694 {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
2695 {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002697};
2698
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002699PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2701 "list_iterator", /* tp_name */
2702 sizeof(listiterobject), /* tp_basicsize */
2703 0, /* tp_itemsize */
2704 /* methods */
2705 (destructor)listiter_dealloc, /* tp_dealloc */
2706 0, /* tp_print */
2707 0, /* tp_getattr */
2708 0, /* tp_setattr */
2709 0, /* tp_reserved */
2710 0, /* tp_repr */
2711 0, /* tp_as_number */
2712 0, /* tp_as_sequence */
2713 0, /* tp_as_mapping */
2714 0, /* tp_hash */
2715 0, /* tp_call */
2716 0, /* tp_str */
2717 PyObject_GenericGetAttr, /* tp_getattro */
2718 0, /* tp_setattro */
2719 0, /* tp_as_buffer */
2720 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2721 0, /* tp_doc */
2722 (traverseproc)listiter_traverse, /* tp_traverse */
2723 0, /* tp_clear */
2724 0, /* tp_richcompare */
2725 0, /* tp_weaklistoffset */
2726 PyObject_SelfIter, /* tp_iter */
2727 (iternextfunc)listiter_next, /* tp_iternext */
2728 listiter_methods, /* tp_methods */
2729 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002730};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002731
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002732
2733static PyObject *
2734list_iter(PyObject *seq)
2735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 if (!PyList_Check(seq)) {
2739 PyErr_BadInternalCall();
2740 return NULL;
2741 }
2742 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2743 if (it == NULL)
2744 return NULL;
2745 it->it_index = 0;
2746 Py_INCREF(seq);
2747 it->it_seq = (PyListObject *)seq;
2748 _PyObject_GC_TRACK(it);
2749 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002750}
2751
2752static void
2753listiter_dealloc(listiterobject *it)
2754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 _PyObject_GC_UNTRACK(it);
2756 Py_XDECREF(it->it_seq);
2757 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002758}
2759
2760static int
2761listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 Py_VISIT(it->it_seq);
2764 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002765}
2766
2767static PyObject *
2768listiter_next(listiterobject *it)
2769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 PyListObject *seq;
2771 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 assert(it != NULL);
2774 seq = it->it_seq;
2775 if (seq == NULL)
2776 return NULL;
2777 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 if (it->it_index < PyList_GET_SIZE(seq)) {
2780 item = PyList_GET_ITEM(seq, it->it_index);
2781 ++it->it_index;
2782 Py_INCREF(item);
2783 return item;
2784 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002787 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002789}
2790
2791static PyObject *
2792listiter_len(listiterobject *it)
2793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 Py_ssize_t len;
2795 if (it->it_seq) {
2796 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2797 if (len >= 0)
2798 return PyLong_FromSsize_t(len);
2799 }
2800 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002801}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002802
2803static PyObject *
2804listiter_reduce(listiterobject *it)
2805{
2806 return listiter_reduce_general(it, 1);
2807}
2808
2809static PyObject *
2810listiter_setstate(listiterobject *it, PyObject *state)
2811{
Victor Stinner7660b882013-06-24 23:59:24 +02002812 Py_ssize_t index = PyLong_AsSsize_t(state);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002813 if (index == -1 && PyErr_Occurred())
2814 return NULL;
2815 if (it->it_seq != NULL) {
2816 if (index < 0)
2817 index = 0;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002818 else if (index > PyList_GET_SIZE(it->it_seq))
2819 index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002820 it->it_index = index;
2821 }
2822 Py_RETURN_NONE;
2823}
2824
Raymond Hettinger1021c442003-11-07 15:38:09 +00002825/*********************** List Reverse Iterator **************************/
2826
2827typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 PyObject_HEAD
2829 Py_ssize_t it_index;
2830 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002831} listreviterobject;
2832
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002833static PyObject *list_reversed(PyListObject *, PyObject *);
2834static void listreviter_dealloc(listreviterobject *);
2835static int listreviter_traverse(listreviterobject *, visitproc, void *);
2836static PyObject *listreviter_next(listreviterobject *);
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002837static PyObject *listreviter_len(listreviterobject *);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002838static PyObject *listreviter_reduce(listreviterobject *);
2839static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002840
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002841static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002843 {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
2844 {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002846};
2847
Raymond Hettinger1021c442003-11-07 15:38:09 +00002848PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2850 "list_reverseiterator", /* tp_name */
2851 sizeof(listreviterobject), /* tp_basicsize */
2852 0, /* tp_itemsize */
2853 /* methods */
2854 (destructor)listreviter_dealloc, /* tp_dealloc */
2855 0, /* tp_print */
2856 0, /* tp_getattr */
2857 0, /* tp_setattr */
2858 0, /* tp_reserved */
2859 0, /* tp_repr */
2860 0, /* tp_as_number */
2861 0, /* tp_as_sequence */
2862 0, /* tp_as_mapping */
2863 0, /* tp_hash */
2864 0, /* tp_call */
2865 0, /* tp_str */
2866 PyObject_GenericGetAttr, /* tp_getattro */
2867 0, /* tp_setattro */
2868 0, /* tp_as_buffer */
2869 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2870 0, /* tp_doc */
2871 (traverseproc)listreviter_traverse, /* tp_traverse */
2872 0, /* tp_clear */
2873 0, /* tp_richcompare */
2874 0, /* tp_weaklistoffset */
2875 PyObject_SelfIter, /* tp_iter */
2876 (iternextfunc)listreviter_next, /* tp_iternext */
2877 listreviter_methods, /* tp_methods */
2878 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00002879};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002880
2881static PyObject *
2882list_reversed(PyListObject *seq, PyObject *unused)
2883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2887 if (it == NULL)
2888 return NULL;
2889 assert(PyList_Check(seq));
2890 it->it_index = PyList_GET_SIZE(seq) - 1;
2891 Py_INCREF(seq);
2892 it->it_seq = seq;
2893 PyObject_GC_Track(it);
2894 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002895}
2896
2897static void
2898listreviter_dealloc(listreviterobject *it)
2899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 PyObject_GC_UnTrack(it);
2901 Py_XDECREF(it->it_seq);
2902 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002903}
2904
2905static int
2906listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 Py_VISIT(it->it_seq);
2909 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002910}
2911
2912static PyObject *
2913listreviter_next(listreviterobject *it)
2914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 PyObject *item;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002916 Py_ssize_t index;
2917 PyListObject *seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002918
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002919 assert(it != NULL);
2920 seq = it->it_seq;
2921 if (seq == NULL) {
2922 return NULL;
2923 }
2924 assert(PyList_Check(seq));
2925
2926 index = it->it_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2928 item = PyList_GET_ITEM(seq, index);
2929 it->it_index--;
2930 Py_INCREF(item);
2931 return item;
2932 }
2933 it->it_index = -1;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002934 it->it_seq = NULL;
2935 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002937}
2938
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002939static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002940listreviter_len(listreviterobject *it)
2941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 Py_ssize_t len = it->it_index + 1;
2943 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2944 len = 0;
2945 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002946}
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002947
2948static PyObject *
2949listreviter_reduce(listreviterobject *it)
2950{
2951 return listiter_reduce_general(it, 0);
2952}
2953
2954static PyObject *
2955listreviter_setstate(listreviterobject *it, PyObject *state)
2956{
2957 Py_ssize_t index = PyLong_AsSsize_t(state);
2958 if (index == -1 && PyErr_Occurred())
2959 return NULL;
2960 if (it->it_seq != NULL) {
2961 if (index < -1)
2962 index = -1;
2963 else if (index > PyList_GET_SIZE(it->it_seq) - 1)
2964 index = PyList_GET_SIZE(it->it_seq) - 1;
2965 it->it_index = index;
2966 }
2967 Py_RETURN_NONE;
2968}
2969
2970/* common pickling support */
2971
2972static PyObject *
2973listiter_reduce_general(void *_it, int forward)
2974{
2975 PyObject *list;
2976
2977 /* the objects are not the same, index is of different types! */
2978 if (forward) {
2979 listiterobject *it = (listiterobject *)_it;
2980 if (it->it_seq)
Victor Stinner7660b882013-06-24 23:59:24 +02002981 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002982 it->it_seq, it->it_index);
2983 } else {
2984 listreviterobject *it = (listreviterobject *)_it;
2985 if (it->it_seq)
Antoine Pitroua7013882012-04-05 00:04:20 +02002986 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("reversed"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002987 it->it_seq, it->it_index);
2988 }
2989 /* empty iterator, create an empty list */
2990 list = PyList_New(0);
2991 if (list == NULL)
2992 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02002993 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002994}