blob: 6f1edc55d879854799c736257f2e60efc1b969bb [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"
4
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00005#ifdef STDC_HEADERS
6#include <stddef.h>
7#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00008#include <sys/types.h> /* For size_t */
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00009#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010
Tim Peters8d9eb102004-07-31 02:24:20 +000011/* Ensure ob_item has room for at least newsize elements, and set
12 * ob_size to newsize. If newsize > ob_size on entry, the content
13 * of the new slots at exit is undefined heap trash; it's the caller's
Ezio Melotti13925002011-03-16 11:05:33 +020014 * responsibility to overwrite them with sane values.
Tim Peters8d9eb102004-07-31 02:24:20 +000015 * The number of allocated elements may grow, shrink, or stay the same.
16 * Failure is impossible if newsize <= self.allocated on entry, although
17 * that partly relies on an assumption that the system realloc() never
18 * fails when passed a number of bytes <= the number of bytes last
19 * allocated (the C standard doesn't guarantee this, but it's hard to
20 * imagine a realloc implementation where it wouldn't be true).
21 * Note that self->ob_item may change, and even if newsize is less
22 * than ob_size on entry.
23 */
Guido van Rossuma46d51d1995-01-26 22:59:43 +000024static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000025list_resize(PyListObject *self, Py_ssize_t newsize)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 PyObject **items;
28 size_t new_allocated;
29 Py_ssize_t allocated = self->allocated;
Tim Peters65b8b842001-05-26 05:28:40 +000030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 /* Bypass realloc() when a previous overallocation is large enough
32 to accommodate the newsize. If the newsize falls lower than half
33 the allocated size, then proceed with the realloc() to shrink the list.
34 */
35 if (allocated >= newsize && newsize >= (allocated >> 1)) {
36 assert(self->ob_item != NULL || newsize == 0);
37 Py_SIZE(self) = newsize;
38 return 0;
39 }
Raymond Hettinger4bb95402004-02-13 11:36:39 +000040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 /* This over-allocates proportional to the list size, making room
42 * for additional growth. The over-allocation is mild, but is
43 * enough to give linear-time amortized behavior over a long
44 * sequence of appends() in the presence of a poorly-performing
45 * system realloc().
46 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
47 */
48 new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 /* check for integer overflow */
51 if (new_allocated > PY_SIZE_MAX - newsize) {
52 PyErr_NoMemory();
53 return -1;
54 } else {
55 new_allocated += newsize;
56 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 if (newsize == 0)
59 new_allocated = 0;
60 items = self->ob_item;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +010061 if (new_allocated <= (PY_SIZE_MAX / sizeof(PyObject *)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 PyMem_RESIZE(items, PyObject *, new_allocated);
63 else
64 items = NULL;
65 if (items == NULL) {
66 PyErr_NoMemory();
67 return -1;
68 }
69 self->ob_item = items;
70 Py_SIZE(self) = newsize;
71 self->allocated = new_allocated;
72 return 0;
Raymond Hettinger4bb95402004-02-13 11:36:39 +000073}
Guido van Rossuma46d51d1995-01-26 22:59:43 +000074
Christian Heimes77c02eb2008-02-09 02:18:51 +000075/* Debug statistic to compare allocations with reuse through the free list */
76#undef SHOW_ALLOC_COUNT
77#ifdef SHOW_ALLOC_COUNT
78static size_t count_alloc = 0;
79static size_t count_reuse = 0;
80
81static void
82show_alloc(void)
83{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
85 count_alloc);
86 fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
87 "d\n", count_reuse);
88 fprintf(stderr, "%.2f%% reuse rate\n\n",
89 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +000090}
91#endif
92
Raymond Hettinger0468e412004-05-05 05:37:53 +000093/* Empty list reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +000094#ifndef PyList_MAXFREELIST
95#define PyList_MAXFREELIST 80
96#endif
97static PyListObject *free_list[PyList_MAXFREELIST];
98static int numfree = 0;
Raymond Hettinger0468e412004-05-05 05:37:53 +000099
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100100int
101PyList_ClearFreeList(void)
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 PyListObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100104 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 while (numfree) {
106 op = free_list[--numfree];
107 assert(PyList_CheckExact(op));
108 PyObject_GC_Del(op);
109 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100110 return ret;
111}
112
113void
114PyList_Fini(void)
115{
116 PyList_ClearFreeList();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000117}
118
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000119PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000120PyList_New(Py_ssize_t size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 PyListObject *op;
123 size_t nbytes;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000124#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 static int initialized = 0;
126 if (!initialized) {
127 Py_AtExit(show_alloc);
128 initialized = 1;
129 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000130#endif
Tim Peters3986d4e2004-07-29 02:28:42 +0000131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 if (size < 0) {
133 PyErr_BadInternalCall();
134 return NULL;
135 }
136 /* Check for overflow without an actual overflow,
137 * which can cause compiler to optimise out */
138 if ((size_t)size > PY_SIZE_MAX / sizeof(PyObject *))
139 return PyErr_NoMemory();
140 nbytes = size * sizeof(PyObject *);
141 if (numfree) {
142 numfree--;
143 op = free_list[numfree];
144 _Py_NewReference((PyObject *)op);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000145#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000147#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 } else {
149 op = PyObject_GC_New(PyListObject, &PyList_Type);
150 if (op == NULL)
151 return NULL;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000152#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000154#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 }
156 if (size <= 0)
157 op->ob_item = NULL;
158 else {
159 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
160 if (op->ob_item == NULL) {
161 Py_DECREF(op);
162 return PyErr_NoMemory();
163 }
164 memset(op->ob_item, 0, nbytes);
165 }
166 Py_SIZE(op) = size;
167 op->allocated = size;
168 _PyObject_GC_TRACK(op);
169 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
Martin v. Löwis18e16552006-02-15 17:27:45 +0000172Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000173PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 if (!PyList_Check(op)) {
176 PyErr_BadInternalCall();
177 return -1;
178 }
179 else
180 return Py_SIZE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181}
182
Raymond Hettingerfdfe6182004-05-05 06:28:16 +0000183static PyObject *indexerr = NULL;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000184
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000185PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000186PyList_GetItem(PyObject *op, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 if (!PyList_Check(op)) {
189 PyErr_BadInternalCall();
190 return NULL;
191 }
192 if (i < 0 || i >= Py_SIZE(op)) {
193 if (indexerr == NULL) {
194 indexerr = PyUnicode_FromString(
195 "list index out of range");
196 if (indexerr == NULL)
197 return NULL;
198 }
199 PyErr_SetObject(PyExc_IndexError, indexerr);
200 return NULL;
201 }
202 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203}
204
205int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000206PyList_SetItem(register PyObject *op, register Py_ssize_t i,
Fred Drakea2f55112000-07-09 15:16:51 +0000207 register PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 register PyObject *olditem;
210 register PyObject **p;
211 if (!PyList_Check(op)) {
212 Py_XDECREF(newitem);
213 PyErr_BadInternalCall();
214 return -1;
215 }
216 if (i < 0 || i >= Py_SIZE(op)) {
217 Py_XDECREF(newitem);
218 PyErr_SetString(PyExc_IndexError,
219 "list assignment index out of range");
220 return -1;
221 }
222 p = ((PyListObject *)op) -> ob_item + i;
223 olditem = *p;
224 *p = newitem;
225 Py_XDECREF(olditem);
226 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227}
228
229static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000230ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 Py_ssize_t i, n = Py_SIZE(self);
233 PyObject **items;
234 if (v == NULL) {
235 PyErr_BadInternalCall();
236 return -1;
237 }
238 if (n == PY_SSIZE_T_MAX) {
239 PyErr_SetString(PyExc_OverflowError,
240 "cannot add more objects to list");
241 return -1;
242 }
Tim Petersb38e2b62004-07-29 02:29:26 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 if (list_resize(self, n+1) == -1)
245 return -1;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (where < 0) {
248 where += n;
249 if (where < 0)
250 where = 0;
251 }
252 if (where > n)
253 where = n;
254 items = self->ob_item;
255 for (i = n; --i >= where; )
256 items[i+1] = items[i];
257 Py_INCREF(v);
258 items[where] = v;
259 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260}
261
262int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000263PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 if (!PyList_Check(op)) {
266 PyErr_BadInternalCall();
267 return -1;
268 }
269 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270}
271
Raymond Hettinger40a03822004-04-12 13:05:09 +0000272static int
273app1(PyListObject *self, PyObject *v)
274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 Py_ssize_t n = PyList_GET_SIZE(self);
Raymond Hettinger40a03822004-04-12 13:05:09 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 assert (v != NULL);
278 if (n == PY_SSIZE_T_MAX) {
279 PyErr_SetString(PyExc_OverflowError,
280 "cannot add more objects to list");
281 return -1;
282 }
Raymond Hettinger40a03822004-04-12 13:05:09 +0000283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 if (list_resize(self, n+1) == -1)
285 return -1;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 Py_INCREF(v);
288 PyList_SET_ITEM(self, n, v);
289 return 0;
Raymond Hettinger40a03822004-04-12 13:05:09 +0000290}
291
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292int
Fred Drakea2f55112000-07-09 15:16:51 +0000293PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 if (PyList_Check(op) && (newitem != NULL))
296 return app1((PyListObject *)op, newitem);
297 PyErr_BadInternalCall();
298 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299}
300
301/* Methods */
302
303static void
Fred Drakea2f55112000-07-09 15:16:51 +0000304list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 Py_ssize_t i;
307 PyObject_GC_UnTrack(op);
308 Py_TRASHCAN_SAFE_BEGIN(op)
309 if (op->ob_item != NULL) {
310 /* Do it backwards, for Christian Tismer.
311 There's a simple test case where somehow this reduces
312 thrashing when a *very* large list is created and
313 immediately deleted. */
314 i = Py_SIZE(op);
315 while (--i >= 0) {
316 Py_XDECREF(op->ob_item[i]);
317 }
318 PyMem_FREE(op->ob_item);
319 }
320 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
321 free_list[numfree++] = op;
322 else
323 Py_TYPE(op)->tp_free((PyObject *)op);
324 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325}
326
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000327static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000328list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 Py_ssize_t i;
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200331 PyObject *s = NULL;
332 _PyAccu acc;
333 static PyObject *sep = NULL;
334
335 if (Py_SIZE(v) == 0) {
336 return PyUnicode_FromString("[]");
337 }
338
339 if (sep == NULL) {
340 sep = PyUnicode_FromString(", ");
341 if (sep == NULL)
342 return NULL;
343 }
Guido van Rossumfb376de1998-04-10 22:47:27 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 i = Py_ReprEnter((PyObject*)v);
346 if (i != 0) {
347 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
348 }
Tim Petersa7259592001-06-16 05:11:17 +0000349
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200350 if (_PyAccu_Init(&acc))
351 goto error;
Tim Petersa7259592001-06-16 05:11:17 +0000352
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200353 s = PyUnicode_FromString("[");
354 if (s == NULL || _PyAccu_Accumulate(&acc, s))
355 goto error;
356 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 /* Do repr() on each element. Note that this may mutate the list,
359 so must refetch the list size on each iteration. */
360 for (i = 0; i < Py_SIZE(v); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 if (Py_EnterRecursiveCall(" while getting the repr of a list"))
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200362 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 s = PyObject_Repr(v->ob_item[i]);
364 Py_LeaveRecursiveCall();
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200365 if (i > 0 && _PyAccu_Accumulate(&acc, sep))
366 goto error;
367 if (s == NULL || _PyAccu_Accumulate(&acc, s))
368 goto error;
369 Py_CLEAR(s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 s = PyUnicode_FromString("]");
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200372 if (s == NULL || _PyAccu_Accumulate(&acc, s))
373 goto error;
374 Py_CLEAR(s);
Tim Petersa7259592001-06-16 05:11:17 +0000375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 Py_ReprLeave((PyObject *)v);
Antoine Pitroueeb7eea2011-10-06 18:57:27 +0200377 return _PyAccu_Finish(&acc);
378
379error:
380 _PyAccu_Destroy(&acc);
381 Py_XDECREF(s);
382 Py_ReprLeave((PyObject *)v);
383 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384}
385
Martin v. Löwis18e16552006-02-15 17:27:45 +0000386static Py_ssize_t
Fred Drakea2f55112000-07-09 15:16:51 +0000387list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 return Py_SIZE(a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390}
391
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000392static int
Fred Drakea2f55112000-07-09 15:16:51 +0000393list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 Py_ssize_t i;
396 int cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
399 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
400 Py_EQ);
401 return cmp;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000402}
403
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000404static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000405list_item(PyListObject *a, Py_ssize_t i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 if (i < 0 || i >= Py_SIZE(a)) {
408 if (indexerr == NULL) {
409 indexerr = PyUnicode_FromString(
410 "list index out of range");
411 if (indexerr == NULL)
412 return NULL;
413 }
414 PyErr_SetObject(PyExc_IndexError, indexerr);
415 return NULL;
416 }
417 Py_INCREF(a->ob_item[i]);
418 return a->ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419}
420
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000421static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000422list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 PyListObject *np;
425 PyObject **src, **dest;
426 Py_ssize_t i, len;
427 if (ilow < 0)
428 ilow = 0;
429 else if (ilow > Py_SIZE(a))
430 ilow = Py_SIZE(a);
431 if (ihigh < ilow)
432 ihigh = ilow;
433 else if (ihigh > Py_SIZE(a))
434 ihigh = Py_SIZE(a);
435 len = ihigh - ilow;
436 np = (PyListObject *) PyList_New(len);
437 if (np == NULL)
438 return NULL;
Raymond Hettinger99842b62004-03-08 05:56:15 +0000439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 src = a->ob_item + ilow;
441 dest = np->ob_item;
442 for (i = 0; i < len; i++) {
443 PyObject *v = src[i];
444 Py_INCREF(v);
445 dest[i] = v;
446 }
447 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448}
449
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000450PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000451PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (!PyList_Check(a)) {
454 PyErr_BadInternalCall();
455 return NULL;
456 }
457 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000458}
459
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000460static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000461list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 Py_ssize_t size;
464 Py_ssize_t i;
465 PyObject **src, **dest;
466 PyListObject *np;
467 if (!PyList_Check(bb)) {
468 PyErr_Format(PyExc_TypeError,
469 "can only concatenate list (not \"%.200s\") to list",
470 bb->ob_type->tp_name);
471 return NULL;
472 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473#define b ((PyListObject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 size = Py_SIZE(a) + Py_SIZE(b);
475 if (size < 0)
476 return PyErr_NoMemory();
477 np = (PyListObject *) PyList_New(size);
478 if (np == NULL) {
479 return NULL;
480 }
481 src = a->ob_item;
482 dest = np->ob_item;
483 for (i = 0; i < Py_SIZE(a); i++) {
484 PyObject *v = src[i];
485 Py_INCREF(v);
486 dest[i] = v;
487 }
488 src = b->ob_item;
489 dest = np->ob_item + Py_SIZE(a);
490 for (i = 0; i < Py_SIZE(b); i++) {
491 PyObject *v = src[i];
492 Py_INCREF(v);
493 dest[i] = v;
494 }
495 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496#undef b
497}
498
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000499static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000500list_repeat(PyListObject *a, Py_ssize_t n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 Py_ssize_t i, j;
503 Py_ssize_t size;
504 PyListObject *np;
505 PyObject **p, **items;
506 PyObject *elem;
507 if (n < 0)
508 n = 0;
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100509 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 return PyErr_NoMemory();
Mark Dickinsonc0420fd2011-09-19 19:18:37 +0100511 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if (size == 0)
513 return PyList_New(0);
514 np = (PyListObject *) PyList_New(size);
515 if (np == NULL)
516 return NULL;
Raymond Hettinger6624e682003-05-21 05:58:46 +0000517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 items = np->ob_item;
519 if (Py_SIZE(a) == 1) {
520 elem = a->ob_item[0];
521 for (i = 0; i < n; i++) {
522 items[i] = elem;
523 Py_INCREF(elem);
524 }
525 return (PyObject *) np;
526 }
527 p = np->ob_item;
528 items = a->ob_item;
529 for (i = 0; i < n; i++) {
530 for (j = 0; j < Py_SIZE(a); j++) {
531 *p = items[j];
532 Py_INCREF(*p);
533 p++;
534 }
535 }
536 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000537}
538
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000539static int
Armin Rigo93677f02004-07-29 12:40:23 +0000540list_clear(PyListObject *a)
541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 Py_ssize_t i;
543 PyObject **item = a->ob_item;
544 if (item != NULL) {
545 /* Because XDECREF can recursively invoke operations on
546 this list, we make it empty first. */
547 i = Py_SIZE(a);
548 Py_SIZE(a) = 0;
549 a->ob_item = NULL;
550 a->allocated = 0;
551 while (--i >= 0) {
552 Py_XDECREF(item[i]);
553 }
554 PyMem_FREE(item);
555 }
556 /* Never fails; the return value can be ignored.
557 Note that there is no guarantee that the list is actually empty
558 at this point, because XDECREF may have populated it again! */
559 return 0;
Armin Rigo93677f02004-07-29 12:40:23 +0000560}
561
Tim Peters8fc4a912004-07-31 21:53:19 +0000562/* a[ilow:ihigh] = v if v != NULL.
563 * del a[ilow:ihigh] if v == NULL.
564 *
565 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
566 * guaranteed the call cannot fail.
567 */
Armin Rigo93677f02004-07-29 12:40:23 +0000568static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000569list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 /* Because [X]DECREF can recursively invoke list operations on
572 this list, we must postpone all [X]DECREF activity until
573 after the list is back in its canonical shape. Therefore
574 we must allocate an additional array, 'recycle', into which
575 we temporarily copy the items that are deleted from the
576 list. :-( */
577 PyObject *recycle_on_stack[8];
578 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
579 PyObject **item;
580 PyObject **vitem = NULL;
581 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
582 Py_ssize_t n; /* # of elements in replacement list */
583 Py_ssize_t norig; /* # of elements in list getting replaced */
584 Py_ssize_t d; /* Change in size */
585 Py_ssize_t k;
586 size_t s;
587 int result = -1; /* guilty until proved innocent */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000588#define b ((PyListObject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 if (v == NULL)
590 n = 0;
591 else {
592 if (a == b) {
593 /* Special case "a[i:j] = a" -- copy b first */
594 v = list_slice(b, 0, Py_SIZE(b));
595 if (v == NULL)
596 return result;
597 result = list_ass_slice(a, ilow, ihigh, v);
598 Py_DECREF(v);
599 return result;
600 }
601 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
602 if(v_as_SF == NULL)
603 goto Error;
604 n = PySequence_Fast_GET_SIZE(v_as_SF);
605 vitem = PySequence_Fast_ITEMS(v_as_SF);
606 }
607 if (ilow < 0)
608 ilow = 0;
609 else if (ilow > Py_SIZE(a))
610 ilow = Py_SIZE(a);
Tim Peters8d9eb102004-07-31 02:24:20 +0000611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 if (ihigh < ilow)
613 ihigh = ilow;
614 else if (ihigh > Py_SIZE(a))
615 ihigh = Py_SIZE(a);
Armin Rigo93677f02004-07-29 12:40:23 +0000616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 norig = ihigh - ilow;
618 assert(norig >= 0);
619 d = n - norig;
620 if (Py_SIZE(a) + d == 0) {
621 Py_XDECREF(v_as_SF);
622 return list_clear(a);
623 }
624 item = a->ob_item;
625 /* recycle the items that we are about to remove */
626 s = norig * sizeof(PyObject *);
627 if (s > sizeof(recycle_on_stack)) {
628 recycle = (PyObject **)PyMem_MALLOC(s);
629 if (recycle == NULL) {
630 PyErr_NoMemory();
631 goto Error;
632 }
633 }
634 memcpy(recycle, &item[ilow], s);
Tim Peters8d9eb102004-07-31 02:24:20 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 if (d < 0) { /* Delete -d items */
637 memmove(&item[ihigh+d], &item[ihigh],
638 (Py_SIZE(a) - ihigh)*sizeof(PyObject *));
639 list_resize(a, Py_SIZE(a) + d);
640 item = a->ob_item;
641 }
642 else if (d > 0) { /* Insert d items */
643 k = Py_SIZE(a);
644 if (list_resize(a, k+d) < 0)
645 goto Error;
646 item = a->ob_item;
647 memmove(&item[ihigh+d], &item[ihigh],
648 (k - ihigh)*sizeof(PyObject *));
649 }
650 for (k = 0; k < n; k++, ilow++) {
651 PyObject *w = vitem[k];
652 Py_XINCREF(w);
653 item[ilow] = w;
654 }
655 for (k = norig - 1; k >= 0; --k)
656 Py_XDECREF(recycle[k]);
657 result = 0;
Tim Peters8d9eb102004-07-31 02:24:20 +0000658 Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 if (recycle != recycle_on_stack)
660 PyMem_FREE(recycle);
661 Py_XDECREF(v_as_SF);
662 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000663#undef b
664}
665
Guido van Rossum234f9421993-06-17 12:35:49 +0000666int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000667PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 if (!PyList_Check(a)) {
670 PyErr_BadInternalCall();
671 return -1;
672 }
673 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000674}
675
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000676static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000677list_inplace_repeat(PyListObject *self, Py_ssize_t n)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 PyObject **items;
680 Py_ssize_t size, i, j, p;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000681
682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 size = PyList_GET_SIZE(self);
684 if (size == 0 || n == 1) {
685 Py_INCREF(self);
686 return (PyObject *)self;
687 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 if (n < 1) {
690 (void)list_clear(self);
691 Py_INCREF(self);
692 return (PyObject *)self;
693 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 if (size > PY_SSIZE_T_MAX / n) {
696 return PyErr_NoMemory();
697 }
Christian Heimesaf98da12008-01-27 15:18:18 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 if (list_resize(self, size*n) == -1)
700 return NULL;
Raymond Hettinger4bb95402004-02-13 11:36:39 +0000701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 p = size;
703 items = self->ob_item;
704 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
705 for (j = 0; j < size; j++) {
706 PyObject *o = items[j];
707 Py_INCREF(o);
708 items[p++] = o;
709 }
710 }
711 Py_INCREF(self);
712 return (PyObject *)self;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000713}
714
Guido van Rossum4a450d01991-04-03 19:05:18 +0000715static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000716list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 PyObject *old_value;
719 if (i < 0 || i >= Py_SIZE(a)) {
720 PyErr_SetString(PyExc_IndexError,
721 "list assignment index out of range");
722 return -1;
723 }
724 if (v == NULL)
725 return list_ass_slice(a, i, i+1, v);
726 Py_INCREF(v);
727 old_value = a->ob_item[i];
728 a->ob_item[i] = v;
729 Py_DECREF(old_value);
730 return 0;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000731}
732
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000734listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 Py_ssize_t i;
737 PyObject *v;
738 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
739 return NULL;
740 if (ins1(self, i, v) == 0)
741 Py_RETURN_NONE;
742 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000743}
744
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000745static PyObject *
Eli Benderskycbbaa962011-02-25 05:47:53 +0000746listclear(PyListObject *self)
747{
748 list_clear(self);
749 Py_RETURN_NONE;
750}
751
752static PyObject *
753listcopy(PyListObject *self)
754{
755 return list_slice(self, 0, Py_SIZE(self));
756}
757
758static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000759listappend(PyListObject *self, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 if (app1(self, v) == 0)
762 Py_RETURN_NONE;
763 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000764}
765
Barry Warsawdedf6d61998-10-09 16:37:25 +0000766static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000767listextend(PyListObject *self, PyObject *b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyObject *it; /* iter(v) */
770 Py_ssize_t m; /* size of self */
771 Py_ssize_t n; /* guess for size of b */
772 Py_ssize_t mn; /* m + n */
773 Py_ssize_t i;
774 PyObject *(*iternext)(PyObject *);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 /* Special cases:
777 1) lists and tuples which can use PySequence_Fast ops
778 2) extending self to self requires making a copy first
779 */
780 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
781 PyObject **src, **dest;
782 b = PySequence_Fast(b, "argument must be iterable");
783 if (!b)
784 return NULL;
785 n = PySequence_Fast_GET_SIZE(b);
786 if (n == 0) {
787 /* short circuit when b is empty */
788 Py_DECREF(b);
789 Py_RETURN_NONE;
790 }
791 m = Py_SIZE(self);
792 if (list_resize(self, m + n) == -1) {
793 Py_DECREF(b);
794 return NULL;
795 }
796 /* note that we may still have self == b here for the
797 * situation a.extend(a), but the following code works
798 * in that case too. Just make sure to resize self
799 * before calling PySequence_Fast_ITEMS.
800 */
801 /* populate the end of self with b's items */
802 src = PySequence_Fast_ITEMS(b);
803 dest = self->ob_item + m;
804 for (i = 0; i < n; i++) {
805 PyObject *o = src[i];
806 Py_INCREF(o);
807 dest[i] = o;
808 }
809 Py_DECREF(b);
810 Py_RETURN_NONE;
811 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 it = PyObject_GetIter(b);
814 if (it == NULL)
815 return NULL;
816 iternext = *it->ob_type->tp_iternext;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 /* Guess a result list size. */
819 n = _PyObject_LengthHint(b, 8);
820 if (n == -1) {
821 Py_DECREF(it);
822 return NULL;
823 }
824 m = Py_SIZE(self);
825 mn = m + n;
826 if (mn >= m) {
827 /* Make room. */
828 if (list_resize(self, mn) == -1)
829 goto error;
830 /* Make the list sane again. */
831 Py_SIZE(self) = m;
832 }
833 /* Else m + n overflowed; on the chance that n lied, and there really
834 * is enough room, ignore it. If n was telling the truth, we'll
835 * eventually run out of memory during the loop.
836 */
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 /* Run iterator to exhaustion. */
839 for (;;) {
840 PyObject *item = iternext(it);
841 if (item == NULL) {
842 if (PyErr_Occurred()) {
843 if (PyErr_ExceptionMatches(PyExc_StopIteration))
844 PyErr_Clear();
845 else
846 goto error;
847 }
848 break;
849 }
850 if (Py_SIZE(self) < self->allocated) {
851 /* steals ref */
852 PyList_SET_ITEM(self, Py_SIZE(self), item);
853 ++Py_SIZE(self);
854 }
855 else {
856 int status = app1(self, item);
857 Py_DECREF(item); /* append creates a new ref */
858 if (status < 0)
859 goto error;
860 }
861 }
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 /* Cut back result list if initial guess was too large. */
864 if (Py_SIZE(self) < self->allocated)
865 list_resize(self, Py_SIZE(self)); /* shrinking can't fail */
Raymond Hettingeraa241e02004-09-26 19:24:20 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 Py_DECREF(it);
868 Py_RETURN_NONE;
Raymond Hettinger90a39bf2004-02-15 03:57:00 +0000869
870 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 Py_DECREF(it);
872 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000873}
874
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000875PyObject *
876_PyList_Extend(PyListObject *self, PyObject *b)
877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 return listextend(self, b);
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +0000879}
880
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000881static PyObject *
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000882list_inplace_concat(PyListObject *self, PyObject *other)
883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 PyObject *result;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 result = listextend(self, other);
887 if (result == NULL)
888 return result;
889 Py_DECREF(result);
890 Py_INCREF(self);
891 return (PyObject *)self;
Raymond Hettinger97bc6182004-03-11 07:34:19 +0000892}
893
894static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000895listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 Py_ssize_t i = -1;
898 PyObject *v;
899 int status;
Raymond Hettinger9eb86b32004-02-17 11:36:16 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 if (!PyArg_ParseTuple(args, "|n:pop", &i))
902 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 if (Py_SIZE(self) == 0) {
905 /* Special-case most common failure cause */
906 PyErr_SetString(PyExc_IndexError, "pop from empty list");
907 return NULL;
908 }
909 if (i < 0)
910 i += Py_SIZE(self);
911 if (i < 0 || i >= Py_SIZE(self)) {
912 PyErr_SetString(PyExc_IndexError, "pop index out of range");
913 return NULL;
914 }
915 v = self->ob_item[i];
916 if (i == Py_SIZE(self) - 1) {
917 status = list_resize(self, Py_SIZE(self) - 1);
918 assert(status >= 0);
919 return v; /* and v now owns the reference the list had */
920 }
921 Py_INCREF(v);
922 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
923 assert(status >= 0);
924 /* Use status, so that in a release build compilers don't
925 * complain about the unused name.
926 */
927 (void) status;
Brett Cannon651dd522004-08-08 21:21:18 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 return v;
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000930}
931
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000932/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
933static void
934reverse_slice(PyObject **lo, PyObject **hi)
935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 assert(lo && hi);
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 --hi;
939 while (lo < hi) {
940 PyObject *t = *lo;
941 *lo = *hi;
942 *hi = t;
943 ++lo;
944 --hi;
945 }
Tim Peters8e2e7ca2002-07-19 02:33:08 +0000946}
947
Tim Petersa64dc242002-08-01 02:13:36 +0000948/* Lots of code for an adaptive, stable, natural mergesort. There are many
949 * pieces to this algorithm; read listsort.txt for overviews and details.
950 */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000951
Daniel Stutzbach98338222010-12-02 21:55:33 +0000952/* A sortslice contains a pointer to an array of keys and a pointer to
953 * an array of corresponding values. In other words, keys[i]
954 * corresponds with values[i]. If values == NULL, then the keys are
955 * also the values.
956 *
957 * Several convenience routines are provided here, so that keys and
958 * values are always moved in sync.
959 */
960
961typedef struct {
962 PyObject **keys;
963 PyObject **values;
964} sortslice;
965
966Py_LOCAL_INLINE(void)
967sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
968{
969 s1->keys[i] = s2->keys[j];
970 if (s1->values != NULL)
971 s1->values[i] = s2->values[j];
972}
973
974Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000975sortslice_copy_incr(sortslice *dst, sortslice *src)
976{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000977 *dst->keys++ = *src->keys++;
978 if (dst->values != NULL)
979 *dst->values++ = *src->values++;
980}
981
982Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000983sortslice_copy_decr(sortslice *dst, sortslice *src)
984{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000985 *dst->keys-- = *src->keys--;
986 if (dst->values != NULL)
987 *dst->values-- = *src->values--;
988}
989
990
991Py_LOCAL_INLINE(void)
992sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +0000993 Py_ssize_t n)
994{
Daniel Stutzbach98338222010-12-02 21:55:33 +0000995 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
996 if (s1->values != NULL)
997 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
998}
999
1000Py_LOCAL_INLINE(void)
1001sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001002 Py_ssize_t n)
1003{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001004 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
1005 if (s1->values != NULL)
1006 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
1007}
1008
1009Py_LOCAL_INLINE(void)
Benjamin Peterson9efdcca2010-12-03 01:44:10 +00001010sortslice_advance(sortslice *slice, Py_ssize_t n)
1011{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001012 slice->keys += n;
1013 if (slice->values != NULL)
1014 slice->values += n;
1015}
1016
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001017/* Comparison function: PyObject_RichCompareBool with Py_LT.
Tim Petersa64dc242002-08-01 02:13:36 +00001018 * Returns -1 on error, 1 if x < y, 0 if x >= y.
1019 */
Guido van Rossum3f236de1996-12-10 23:55:39 +00001020
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001021#define ISLT(X, Y) (PyObject_RichCompareBool(X, Y, Py_LT))
Tim Peters66860f62002-08-04 17:47:26 +00001022
1023/* Compare X to Y via "<". Goto "fail" if the comparison raises an
Tim Petersa8c974c2002-07-19 03:30:57 +00001024 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
1025 started. It makes more sense in context <wink>. X and Y are PyObject*s.
1026*/
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001027#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 if (k)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001029
1030/* binarysort is the best method for sorting small arrays: it does
1031 few compares, but can do data movement quadratic in the number of
1032 elements.
Guido van Rossum42812581998-06-17 14:15:44 +00001033 [lo, hi) is a contiguous slice of a list, and is sorted via
Tim Petersa8c974c2002-07-19 03:30:57 +00001034 binary insertion. This sort is stable.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001035 On entry, must have lo <= start <= hi, and that [lo, start) is already
1036 sorted (pass start == lo if you don't know!).
Tim Petersa8c974c2002-07-19 03:30:57 +00001037 If islt() complains return -1, else 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001038 Even in case of error, the output slice will be some permutation of
1039 the input (nothing is lost or duplicated).
1040*/
Guido van Rossum3f236de1996-12-10 23:55:39 +00001041static int
Daniel Stutzbach98338222010-12-02 21:55:33 +00001042binarysort(sortslice lo, PyObject **hi, PyObject **start)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 register Py_ssize_t k;
1045 register PyObject **l, **p, **r;
1046 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001047
Daniel Stutzbach98338222010-12-02 21:55:33 +00001048 assert(lo.keys <= start && start <= hi);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 /* assert [lo, start) is sorted */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001050 if (lo.keys == start)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 ++start;
1052 for (; start < hi; ++start) {
1053 /* set l to where *start belongs */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001054 l = lo.keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 r = start;
1056 pivot = *r;
1057 /* Invariants:
1058 * pivot >= all in [lo, l).
1059 * pivot < all in [r, start).
1060 * The second is vacuously true at the start.
1061 */
1062 assert(l < r);
1063 do {
1064 p = l + ((r - l) >> 1);
1065 IFLT(pivot, *p)
1066 r = p;
1067 else
1068 l = p+1;
1069 } while (l < r);
1070 assert(l == r);
1071 /* The invariants still hold, so pivot >= all in [lo, l) and
1072 pivot < all in [l, start), so pivot belongs at l. Note
1073 that if there are elements equal to pivot, l points to the
1074 first slot after them -- that's why this sort is stable.
1075 Slide over to make room.
1076 Caution: using memmove is much slower under MSVC 5;
1077 we're not usually moving many slots. */
1078 for (p = start; p > l; --p)
1079 *p = *(p-1);
1080 *l = pivot;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001081 if (lo.values != NULL) {
1082 Py_ssize_t offset = lo.values - lo.keys;
1083 p = start + offset;
1084 pivot = *p;
1085 l += offset;
1086 for (p = start + offset; p > l; --p)
1087 *p = *(p-1);
1088 *l = pivot;
1089 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 }
1091 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001092
1093 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001095}
1096
Tim Petersa64dc242002-08-01 02:13:36 +00001097/*
1098Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
1099is required on entry. "A run" is the longest ascending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001100
Tim Petersa64dc242002-08-01 02:13:36 +00001101 lo[0] <= lo[1] <= lo[2] <= ...
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001102
Tim Petersa64dc242002-08-01 02:13:36 +00001103or the longest descending sequence, with
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001104
Tim Petersa64dc242002-08-01 02:13:36 +00001105 lo[0] > lo[1] > lo[2] > ...
Tim Peters3b01a122002-07-19 02:35:45 +00001106
Tim Petersa64dc242002-08-01 02:13:36 +00001107Boolean *descending is set to 0 in the former case, or to 1 in the latter.
1108For its intended use in a stable mergesort, the strictness of the defn of
1109"descending" is needed so that the caller can safely reverse a descending
1110sequence without violating stability (strict > ensures there are no equal
1111elements to get out of order).
1112
1113Returns -1 in case of error.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001114*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001115static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001116count_run(PyObject **lo, PyObject **hi, int *descending)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 Py_ssize_t k;
1119 Py_ssize_t n;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 assert(lo < hi);
1122 *descending = 0;
1123 ++lo;
1124 if (lo == hi)
1125 return 1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 n = 2;
1128 IFLT(*lo, *(lo-1)) {
1129 *descending = 1;
1130 for (lo = lo+1; lo < hi; ++lo, ++n) {
1131 IFLT(*lo, *(lo-1))
1132 ;
1133 else
1134 break;
1135 }
1136 }
1137 else {
1138 for (lo = lo+1; lo < hi; ++lo, ++n) {
1139 IFLT(*lo, *(lo-1))
1140 break;
1141 }
1142 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 return n;
Tim Petersa64dc242002-08-01 02:13:36 +00001145fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001147}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001148
Tim Petersa64dc242002-08-01 02:13:36 +00001149/*
1150Locate the proper position of key in a sorted vector; if the vector contains
1151an element equal to key, return the position immediately to the left of
1152the leftmost equal element. [gallop_right() does the same except returns
1153the position to the right of the rightmost equal element (if any).]
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001154
Tim Petersa64dc242002-08-01 02:13:36 +00001155"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001156
Tim Petersa64dc242002-08-01 02:13:36 +00001157"hint" is an index at which to begin the search, 0 <= hint < n. The closer
1158hint is to the final result, the faster this runs.
1159
1160The return value is the int k in 0..n such that
1161
1162 a[k-1] < key <= a[k]
1163
1164pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
1165key belongs at index k; or, IOW, the first k elements of a should precede
1166key, and the last n-k should follow key.
1167
1168Returns -1 on error. See listsort.txt for info on the method.
1169*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001170static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001171gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 Py_ssize_t ofs;
1174 Py_ssize_t lastofs;
1175 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 a += hint;
1180 lastofs = 0;
1181 ofs = 1;
1182 IFLT(*a, key) {
1183 /* a[hint] < key -- gallop right, until
1184 * a[hint + lastofs] < key <= a[hint + ofs]
1185 */
1186 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1187 while (ofs < maxofs) {
1188 IFLT(a[ofs], key) {
1189 lastofs = ofs;
1190 ofs = (ofs << 1) + 1;
1191 if (ofs <= 0) /* int overflow */
1192 ofs = maxofs;
1193 }
1194 else /* key <= a[hint + ofs] */
1195 break;
1196 }
1197 if (ofs > maxofs)
1198 ofs = maxofs;
1199 /* Translate back to offsets relative to &a[0]. */
1200 lastofs += hint;
1201 ofs += hint;
1202 }
1203 else {
1204 /* key <= a[hint] -- gallop left, until
1205 * a[hint - ofs] < key <= a[hint - lastofs]
1206 */
1207 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1208 while (ofs < maxofs) {
1209 IFLT(*(a-ofs), key)
1210 break;
1211 /* key <= a[hint - ofs] */
1212 lastofs = ofs;
1213 ofs = (ofs << 1) + 1;
1214 if (ofs <= 0) /* int overflow */
1215 ofs = maxofs;
1216 }
1217 if (ofs > maxofs)
1218 ofs = maxofs;
1219 /* Translate back to positive offsets relative to &a[0]. */
1220 k = lastofs;
1221 lastofs = hint - ofs;
1222 ofs = hint - k;
1223 }
1224 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1227 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
1228 * right of lastofs but no farther right than ofs. Do a binary
1229 * search, with invariant a[lastofs-1] < key <= a[ofs].
1230 */
1231 ++lastofs;
1232 while (lastofs < ofs) {
1233 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 IFLT(a[m], key)
1236 lastofs = m+1; /* a[m] < key */
1237 else
1238 ofs = m; /* key <= a[m] */
1239 }
1240 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
1241 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001242
1243fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001245}
1246
1247/*
1248Exactly like gallop_left(), except that if key already exists in a[0:n],
1249finds the position immediately to the right of the rightmost equal value.
1250
1251The return value is the int k in 0..n such that
1252
1253 a[k-1] <= key < a[k]
1254
1255or -1 if error.
1256
1257The code duplication is massive, but this is enough different given that
1258we're sticking to "<" comparisons that it's much harder to follow if
1259written as one routine with yet another "left or right?" flag.
1260*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001261static Py_ssize_t
Raymond Hettinger70b64fc2008-01-30 20:15:17 +00001262gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
Tim Petersa64dc242002-08-01 02:13:36 +00001263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 Py_ssize_t ofs;
1265 Py_ssize_t lastofs;
1266 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 assert(key && a && n > 0 && hint >= 0 && hint < n);
Tim Petersa64dc242002-08-01 02:13:36 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 a += hint;
1271 lastofs = 0;
1272 ofs = 1;
1273 IFLT(key, *a) {
1274 /* key < a[hint] -- gallop left, until
1275 * a[hint - ofs] <= key < a[hint - lastofs]
1276 */
1277 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
1278 while (ofs < maxofs) {
1279 IFLT(key, *(a-ofs)) {
1280 lastofs = ofs;
1281 ofs = (ofs << 1) + 1;
1282 if (ofs <= 0) /* int overflow */
1283 ofs = maxofs;
1284 }
1285 else /* a[hint - ofs] <= key */
1286 break;
1287 }
1288 if (ofs > maxofs)
1289 ofs = maxofs;
1290 /* Translate back to positive offsets relative to &a[0]. */
1291 k = lastofs;
1292 lastofs = hint - ofs;
1293 ofs = hint - k;
1294 }
1295 else {
1296 /* a[hint] <= key -- gallop right, until
1297 * a[hint + lastofs] <= key < a[hint + ofs]
1298 */
1299 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
1300 while (ofs < maxofs) {
1301 IFLT(key, a[ofs])
1302 break;
1303 /* a[hint + ofs] <= key */
1304 lastofs = ofs;
1305 ofs = (ofs << 1) + 1;
1306 if (ofs <= 0) /* int overflow */
1307 ofs = maxofs;
1308 }
1309 if (ofs > maxofs)
1310 ofs = maxofs;
1311 /* Translate back to offsets relative to &a[0]. */
1312 lastofs += hint;
1313 ofs += hint;
1314 }
1315 a -= hint;
Tim Petersa64dc242002-08-01 02:13:36 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
1318 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
1319 * right of lastofs but no farther right than ofs. Do a binary
1320 * search, with invariant a[lastofs-1] <= key < a[ofs].
1321 */
1322 ++lastofs;
1323 while (lastofs < ofs) {
1324 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 IFLT(key, a[m])
1327 ofs = m; /* key < a[m] */
1328 else
1329 lastofs = m+1; /* a[m] <= key */
1330 }
1331 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
1332 return ofs;
Tim Petersa64dc242002-08-01 02:13:36 +00001333
1334fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001336}
1337
1338/* The maximum number of entries in a MergeState's pending-runs stack.
1339 * This is enough to sort arrays of size up to about
1340 * 32 * phi ** MAX_MERGE_PENDING
1341 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
1342 * with 2**64 elements.
1343 */
1344#define MAX_MERGE_PENDING 85
1345
Tim Peterse05f65a2002-08-10 05:21:15 +00001346/* When we get into galloping mode, we stay there until both runs win less
1347 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
Tim Petersa64dc242002-08-01 02:13:36 +00001348 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001349#define MIN_GALLOP 7
Tim Petersa64dc242002-08-01 02:13:36 +00001350
1351/* Avoid malloc for small temp arrays. */
1352#define MERGESTATE_TEMP_SIZE 256
1353
1354/* One MergeState exists on the stack per invocation of mergesort. It's just
1355 * a convenient way to pass state around among the helper functions.
1356 */
Tim Peterse05f65a2002-08-10 05:21:15 +00001357struct s_slice {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001358 sortslice base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 Py_ssize_t len;
Tim Peterse05f65a2002-08-10 05:21:15 +00001360};
1361
Tim Petersa64dc242002-08-01 02:13:36 +00001362typedef struct s_MergeState {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 /* This controls when we get *into* galloping mode. It's initialized
1364 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
1365 * random data, and lower for highly structured data.
1366 */
1367 Py_ssize_t min_gallop;
Tim Peterse05f65a2002-08-10 05:21:15 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 /* 'a' is temp storage to help with merges. It contains room for
1370 * alloced entries.
1371 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001372 sortslice a; /* may point to temparray below */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 Py_ssize_t alloced;
Tim Petersa64dc242002-08-01 02:13:36 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 /* A stack of n pending runs yet to be merged. Run #i starts at
1376 * address base[i] and extends for len[i] elements. It's always
1377 * true (so long as the indices are in bounds) that
1378 *
1379 * pending[i].base + pending[i].len == pending[i+1].base
1380 *
1381 * so we could cut the storage for this, but it's a minor amount,
1382 * and keeping all the info explicit simplifies the code.
1383 */
1384 int n;
1385 struct s_slice pending[MAX_MERGE_PENDING];
Tim Petersa64dc242002-08-01 02:13:36 +00001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 /* 'a' points to this when possible, rather than muck with malloc. */
1388 PyObject *temparray[MERGESTATE_TEMP_SIZE];
Tim Petersa64dc242002-08-01 02:13:36 +00001389} MergeState;
1390
1391/* Conceptually a MergeState's constructor. */
1392static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001393merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
Tim Petersa64dc242002-08-01 02:13:36 +00001394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001396 if (has_keyfunc) {
1397 /* The temporary space for merging will need at most half the list
1398 * size rounded up. Use the minimum possible space so we can use the
1399 * rest of temparray for other things. In particular, if there is
1400 * enough extra space, listsort() will use it to store the keys.
1401 */
1402 ms->alloced = (list_size + 1) / 2;
1403
1404 /* ms->alloced describes how many keys will be stored at
1405 ms->temparray, but we also need to store the values. Hence,
1406 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
1407 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
1408 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
1409 ms->a.values = &ms->temparray[ms->alloced];
1410 }
1411 else {
1412 ms->alloced = MERGESTATE_TEMP_SIZE;
1413 ms->a.values = NULL;
1414 }
1415 ms->a.keys = ms->temparray;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 ms->n = 0;
1417 ms->min_gallop = MIN_GALLOP;
Tim Petersa64dc242002-08-01 02:13:36 +00001418}
1419
1420/* Free all the temp memory owned by the MergeState. This must be called
1421 * when you're done with a MergeState, and may be called before then if
1422 * you want to free the temp memory early.
1423 */
1424static void
1425merge_freemem(MergeState *ms)
1426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 assert(ms != NULL);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001428 if (ms->a.keys != ms->temparray)
1429 PyMem_Free(ms->a.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001430}
1431
1432/* Ensure enough temp memory for 'need' array slots is available.
1433 * Returns 0 on success and -1 if the memory can't be gotten.
1434 */
1435static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001436merge_getmem(MergeState *ms, Py_ssize_t need)
Tim Petersa64dc242002-08-01 02:13:36 +00001437{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001438 int multiplier;
1439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 assert(ms != NULL);
1441 if (need <= ms->alloced)
1442 return 0;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001443
1444 multiplier = ms->a.values != NULL ? 2 : 1;
1445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 /* Don't realloc! That can cost cycles to copy the old data, but
1447 * we don't care what's in the block.
1448 */
1449 merge_freemem(ms);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001450 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*) / multiplier) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 PyErr_NoMemory();
1452 return -1;
1453 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001454 ms->a.keys = (PyObject**)PyMem_Malloc(multiplier * need
1455 * sizeof(PyObject *));
1456 if (ms->a.keys != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 ms->alloced = need;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001458 if (ms->a.values != NULL)
1459 ms->a.values = &ms->a.keys[need];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 return 0;
1461 }
1462 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 return -1;
Tim Petersa64dc242002-08-01 02:13:36 +00001464}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
1466 merge_getmem(MS, NEED))
Tim Petersa64dc242002-08-01 02:13:36 +00001467
Daniel Stutzbach98338222010-12-02 21:55:33 +00001468/* Merge the na elements starting at ssa with the nb elements starting at
1469 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1470 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1471 * should have na <= nb. See listsort.txt for more info. Return 0 if
1472 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001473 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001474static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001475merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
1476 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001479 sortslice dest;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 int result = -1; /* guilty until proved innocent */
1481 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001482
Daniel Stutzbach98338222010-12-02 21:55:33 +00001483 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1484 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 if (MERGE_GETMEM(ms, na) < 0)
1486 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001487 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
1488 dest = ssa;
1489 ssa = ms->a;
Tim Petersa64dc242002-08-01 02:13:36 +00001490
Daniel Stutzbach98338222010-12-02 21:55:33 +00001491 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 --nb;
1493 if (nb == 0)
1494 goto Succeed;
1495 if (na == 1)
1496 goto CopyB;
Tim Petersa64dc242002-08-01 02:13:36 +00001497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 min_gallop = ms->min_gallop;
1499 for (;;) {
1500 Py_ssize_t acount = 0; /* # of times A won in a row */
1501 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 /* Do the straightforward thing until (if ever) one run
1504 * appears to win consistently.
1505 */
1506 for (;;) {
1507 assert(na > 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001508 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 if (k) {
1510 if (k < 0)
1511 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001512 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 ++bcount;
1514 acount = 0;
1515 --nb;
1516 if (nb == 0)
1517 goto Succeed;
1518 if (bcount >= min_gallop)
1519 break;
1520 }
1521 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001522 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 ++acount;
1524 bcount = 0;
1525 --na;
1526 if (na == 1)
1527 goto CopyB;
1528 if (acount >= min_gallop)
1529 break;
1530 }
1531 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 /* One run is winning so consistently that galloping may
1534 * be a huge win. So try that, and continue galloping until
1535 * (if ever) neither run appears to be winning consistently
1536 * anymore.
1537 */
1538 ++min_gallop;
1539 do {
1540 assert(na > 1 && nb > 0);
1541 min_gallop -= min_gallop > 1;
1542 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001543 k = gallop_right(ssb.keys[0], ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 acount = k;
1545 if (k) {
1546 if (k < 0)
1547 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001548 sortslice_memcpy(&dest, 0, &ssa, 0, k);
1549 sortslice_advance(&dest, k);
1550 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 na -= k;
1552 if (na == 1)
1553 goto CopyB;
1554 /* na==0 is impossible now if the comparison
1555 * function is consistent, but we can't assume
1556 * that it is.
1557 */
1558 if (na == 0)
1559 goto Succeed;
1560 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001561 sortslice_copy_incr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 --nb;
1563 if (nb == 0)
1564 goto Succeed;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001565
Daniel Stutzbach98338222010-12-02 21:55:33 +00001566 k = gallop_left(ssa.keys[0], ssb.keys, nb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 bcount = k;
1568 if (k) {
1569 if (k < 0)
1570 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001571 sortslice_memmove(&dest, 0, &ssb, 0, k);
1572 sortslice_advance(&dest, k);
1573 sortslice_advance(&ssb, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 nb -= k;
1575 if (nb == 0)
1576 goto Succeed;
1577 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001578 sortslice_copy_incr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 --na;
1580 if (na == 1)
1581 goto CopyB;
1582 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1583 ++min_gallop; /* penalize it for leaving galloping mode */
1584 ms->min_gallop = min_gallop;
1585 }
Tim Petersa64dc242002-08-01 02:13:36 +00001586Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001588Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 if (na)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001590 sortslice_memcpy(&dest, 0, &ssa, 0, na);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001592CopyB:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 assert(na == 1 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001594 /* The last element of ssa belongs at the end of the merge. */
1595 sortslice_memmove(&dest, 0, &ssb, 0, nb);
1596 sortslice_copy(&dest, nb, &ssa, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001598}
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001599
Daniel Stutzbach98338222010-12-02 21:55:33 +00001600/* Merge the na elements starting at pa with the nb elements starting at
1601 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
1602 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
1603 * should have na >= nb. See listsort.txt for more info. Return 0 if
1604 * successful, -1 if error.
Tim Petersa64dc242002-08-01 02:13:36 +00001605 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001606static Py_ssize_t
Daniel Stutzbach98338222010-12-02 21:55:33 +00001607merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
1608 sortslice ssb, Py_ssize_t nb)
Tim Petersa64dc242002-08-01 02:13:36 +00001609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 Py_ssize_t k;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001611 sortslice dest, basea, baseb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 int result = -1; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 Py_ssize_t min_gallop;
Tim Petersa64dc242002-08-01 02:13:36 +00001614
Daniel Stutzbach98338222010-12-02 21:55:33 +00001615 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
1616 assert(ssa.keys + na == ssb.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 if (MERGE_GETMEM(ms, nb) < 0)
1618 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001619 dest = ssb;
1620 sortslice_advance(&dest, nb-1);
1621 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
1622 basea = ssa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 baseb = ms->a;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001624 ssb.keys = ms->a.keys + nb - 1;
1625 if (ssb.values != NULL)
1626 ssb.values = ms->a.values + nb - 1;
1627 sortslice_advance(&ssa, na - 1);
Tim Petersa64dc242002-08-01 02:13:36 +00001628
Daniel Stutzbach98338222010-12-02 21:55:33 +00001629 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 --na;
1631 if (na == 0)
1632 goto Succeed;
1633 if (nb == 1)
1634 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 min_gallop = ms->min_gallop;
1637 for (;;) {
1638 Py_ssize_t acount = 0; /* # of times A won in a row */
1639 Py_ssize_t bcount = 0; /* # of times B won in a row */
Tim Petersa64dc242002-08-01 02:13:36 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 /* Do the straightforward thing until (if ever) one run
1642 * appears to win consistently.
1643 */
1644 for (;;) {
1645 assert(na > 0 && nb > 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001646 k = ISLT(ssb.keys[0], ssa.keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 if (k) {
1648 if (k < 0)
1649 goto Fail;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001650 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 ++acount;
1652 bcount = 0;
1653 --na;
1654 if (na == 0)
1655 goto Succeed;
1656 if (acount >= min_gallop)
1657 break;
1658 }
1659 else {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001660 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 ++bcount;
1662 acount = 0;
1663 --nb;
1664 if (nb == 1)
1665 goto CopyA;
1666 if (bcount >= min_gallop)
1667 break;
1668 }
1669 }
Tim Petersa64dc242002-08-01 02:13:36 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 /* One run is winning so consistently that galloping may
1672 * be a huge win. So try that, and continue galloping until
1673 * (if ever) neither run appears to be winning consistently
1674 * anymore.
1675 */
1676 ++min_gallop;
1677 do {
1678 assert(na > 0 && nb > 1);
1679 min_gallop -= min_gallop > 1;
1680 ms->min_gallop = min_gallop;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001681 k = gallop_right(ssb.keys[0], basea.keys, na, na-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 if (k < 0)
1683 goto Fail;
1684 k = na - k;
1685 acount = k;
1686 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001687 sortslice_advance(&dest, -k);
1688 sortslice_advance(&ssa, -k);
1689 sortslice_memmove(&dest, 1, &ssa, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 na -= k;
1691 if (na == 0)
1692 goto Succeed;
1693 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001694 sortslice_copy_decr(&dest, &ssb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 --nb;
1696 if (nb == 1)
1697 goto CopyA;
Tim Petersa64dc242002-08-01 02:13:36 +00001698
Daniel Stutzbach98338222010-12-02 21:55:33 +00001699 k = gallop_left(ssa.keys[0], baseb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 if (k < 0)
1701 goto Fail;
1702 k = nb - k;
1703 bcount = k;
1704 if (k) {
Daniel Stutzbach98338222010-12-02 21:55:33 +00001705 sortslice_advance(&dest, -k);
1706 sortslice_advance(&ssb, -k);
1707 sortslice_memcpy(&dest, 1, &ssb, 1, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 nb -= k;
1709 if (nb == 1)
1710 goto CopyA;
1711 /* nb==0 is impossible now if the comparison
1712 * function is consistent, but we can't assume
1713 * that it is.
1714 */
1715 if (nb == 0)
1716 goto Succeed;
1717 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001718 sortslice_copy_decr(&dest, &ssa);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 --na;
1720 if (na == 0)
1721 goto Succeed;
1722 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
1723 ++min_gallop; /* penalize it for leaving galloping mode */
1724 ms->min_gallop = min_gallop;
1725 }
Tim Petersa64dc242002-08-01 02:13:36 +00001726Succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 result = 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001728Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 if (nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001730 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return result;
Tim Petersa64dc242002-08-01 02:13:36 +00001732CopyA:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 assert(nb == 1 && na > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001734 /* The first element of ssb belongs at the front of the merge. */
1735 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
1736 sortslice_advance(&dest, -na);
1737 sortslice_advance(&ssa, -na);
1738 sortslice_copy(&dest, 0, &ssb, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001740}
1741
1742/* Merge the two runs at stack indices i and i+1.
1743 * Returns 0 on success, -1 on error.
1744 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001745static Py_ssize_t
1746merge_at(MergeState *ms, Py_ssize_t i)
Tim Petersa64dc242002-08-01 02:13:36 +00001747{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001748 sortslice ssa, ssb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 Py_ssize_t na, nb;
1750 Py_ssize_t k;
Tim Petersa64dc242002-08-01 02:13:36 +00001751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 assert(ms != NULL);
1753 assert(ms->n >= 2);
1754 assert(i >= 0);
1755 assert(i == ms->n - 2 || i == ms->n - 3);
Tim Petersa64dc242002-08-01 02:13:36 +00001756
Daniel Stutzbach98338222010-12-02 21:55:33 +00001757 ssa = ms->pending[i].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 na = ms->pending[i].len;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001759 ssb = ms->pending[i+1].base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 nb = ms->pending[i+1].len;
1761 assert(na > 0 && nb > 0);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001762 assert(ssa.keys + na == ssb.keys);
Tim Petersa64dc242002-08-01 02:13:36 +00001763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 /* Record the length of the combined runs; if i is the 3rd-last
1765 * run now, also slide over the last run (which isn't involved
1766 * in this merge). The current run i+1 goes away in any case.
1767 */
1768 ms->pending[i].len = na + nb;
1769 if (i == ms->n - 3)
1770 ms->pending[i+1] = ms->pending[i+2];
1771 --ms->n;
Tim Petersa64dc242002-08-01 02:13:36 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 /* Where does b start in a? Elements in a before that can be
1774 * ignored (already in place).
1775 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001776 k = gallop_right(*ssb.keys, ssa.keys, na, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 if (k < 0)
1778 return -1;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001779 sortslice_advance(&ssa, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 na -= k;
1781 if (na == 0)
1782 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 /* Where does a end in b? Elements in b after that can be
1785 * ignored (already in place).
1786 */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001787 nb = gallop_left(ssa.keys[na-1], ssb.keys, nb, nb-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 if (nb <= 0)
1789 return nb;
Tim Petersa64dc242002-08-01 02:13:36 +00001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 /* Merge what remains of the runs, using a temp array with
1792 * min(na, nb) elements.
1793 */
1794 if (na <= nb)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001795 return merge_lo(ms, ssa, na, ssb, nb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 else
Daniel Stutzbach98338222010-12-02 21:55:33 +00001797 return merge_hi(ms, ssa, na, ssb, nb);
Tim Petersa64dc242002-08-01 02:13:36 +00001798}
1799
1800/* Examine the stack of runs waiting to be merged, merging adjacent runs
1801 * until the stack invariants are re-established:
1802 *
1803 * 1. len[-3] > len[-2] + len[-1]
1804 * 2. len[-2] > len[-1]
1805 *
1806 * See listsort.txt for more info.
1807 *
1808 * Returns 0 on success, -1 on error.
1809 */
1810static int
1811merge_collapse(MergeState *ms)
1812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 assert(ms);
1816 while (ms->n > 1) {
1817 Py_ssize_t n = ms->n - 2;
1818 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
1819 if (p[n-1].len < p[n+1].len)
1820 --n;
1821 if (merge_at(ms, n) < 0)
1822 return -1;
1823 }
1824 else if (p[n].len <= p[n+1].len) {
1825 if (merge_at(ms, n) < 0)
1826 return -1;
1827 }
1828 else
1829 break;
1830 }
1831 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001832}
1833
1834/* Regardless of invariants, merge all runs on the stack until only one
1835 * remains. This is used at the end of the mergesort.
1836 *
1837 * Returns 0 on success, -1 on error.
1838 */
1839static int
1840merge_force_collapse(MergeState *ms)
1841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 struct s_slice *p = ms->pending;
Tim Petersa64dc242002-08-01 02:13:36 +00001843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 assert(ms);
1845 while (ms->n > 1) {
1846 Py_ssize_t n = ms->n - 2;
1847 if (n > 0 && p[n-1].len < p[n+1].len)
1848 --n;
1849 if (merge_at(ms, n) < 0)
1850 return -1;
1851 }
1852 return 0;
Tim Petersa64dc242002-08-01 02:13:36 +00001853}
1854
1855/* Compute a good value for the minimum run length; natural runs shorter
1856 * than this are boosted artificially via binary insertion.
1857 *
1858 * If n < 64, return n (it's too small to bother with fancy stuff).
1859 * Else if n is an exact power of 2, return 32.
1860 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
1861 * strictly less than, an exact power of 2.
1862 *
1863 * See listsort.txt for more info.
1864 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001865static Py_ssize_t
1866merge_compute_minrun(Py_ssize_t n)
Tim Petersa64dc242002-08-01 02:13:36 +00001867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
Tim Petersa64dc242002-08-01 02:13:36 +00001869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 assert(n >= 0);
1871 while (n >= 64) {
1872 r |= n & 1;
1873 n >>= 1;
1874 }
1875 return n + r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001876}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001877
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001878static void
Daniel Stutzbach98338222010-12-02 21:55:33 +00001879reverse_sortslice(sortslice *s, Py_ssize_t n)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001880{
Daniel Stutzbach98338222010-12-02 21:55:33 +00001881 reverse_slice(s->keys, &s->keys[n]);
1882 if (s->values != NULL)
1883 reverse_slice(s->values, &s->values[n]);
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001884}
1885
Tim Petersa64dc242002-08-01 02:13:36 +00001886/* An adaptive, stable, natural mergesort. See listsort.txt.
1887 * Returns Py_None on success, NULL on error. Even in case of error, the
1888 * list will be some permutation of its input state (nothing is lost or
1889 * duplicated).
1890 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001891static PyObject *
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001892listsort(PyListObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 MergeState ms;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 Py_ssize_t nremaining;
1896 Py_ssize_t minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001897 sortslice lo;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 Py_ssize_t saved_ob_size, saved_allocated;
1899 PyObject **saved_ob_item;
1900 PyObject **final_ob_item;
1901 PyObject *result = NULL; /* guilty until proved innocent */
1902 int reverse = 0;
1903 PyObject *keyfunc = NULL;
1904 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 static char *kwlist[] = {"key", "reverse", 0};
Daniel Stutzbach98338222010-12-02 21:55:33 +00001906 PyObject **keys;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 assert(self != NULL);
1909 assert (PyList_Check(self));
1910 if (args != NULL) {
1911 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort",
1912 kwlist, &keyfunc, &reverse))
1913 return NULL;
1914 if (Py_SIZE(args) > 0) {
1915 PyErr_SetString(PyExc_TypeError,
1916 "must use keyword argument for key function");
1917 return NULL;
1918 }
1919 }
1920 if (keyfunc == Py_None)
1921 keyfunc = NULL;
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 /* The list is temporarily made empty, so that mutations performed
1924 * by comparison functions can't affect the slice of memory we're
1925 * sorting (allowing mutations during sorting is a core-dump
1926 * factory, since ob_item may change).
1927 */
1928 saved_ob_size = Py_SIZE(self);
1929 saved_ob_item = self->ob_item;
1930 saved_allocated = self->allocated;
1931 Py_SIZE(self) = 0;
1932 self->ob_item = NULL;
1933 self->allocated = -1; /* any operation will reset it to >= 0 */
Tim Peters330f9e92002-07-19 07:05:44 +00001934
Daniel Stutzbach98338222010-12-02 21:55:33 +00001935 if (keyfunc == NULL) {
1936 keys = NULL;
1937 lo.keys = saved_ob_item;
1938 lo.values = NULL;
1939 }
1940 else {
1941 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
1942 /* Leverage stack space we allocated but won't otherwise use */
1943 keys = &ms.temparray[saved_ob_size+1];
1944 else {
1945 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
1946 if (keys == NULL)
1947 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 }
Daniel Stutzbach98338222010-12-02 21:55:33 +00001949
1950 for (i = 0; i < saved_ob_size ; i++) {
1951 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
1952 NULL);
1953 if (keys[i] == NULL) {
1954 for (i=i-1 ; i>=0 ; i--)
1955 Py_DECREF(keys[i]);
Daniel Stutzbach8eda5f72011-03-02 23:37:50 +00001956 if (keys != &ms.temparray[saved_ob_size+1])
1957 PyMem_FREE(keys);
Daniel Stutzbach98338222010-12-02 21:55:33 +00001958 goto keyfunc_fail;
1959 }
1960 }
1961
1962 lo.keys = keys;
1963 lo.values = saved_ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001965
Daniel Stutzbach98338222010-12-02 21:55:33 +00001966 merge_init(&ms, saved_ob_size, keys != NULL);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00001967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 nremaining = saved_ob_size;
1969 if (nremaining < 2)
1970 goto succeed;
Tim Peters330f9e92002-07-19 07:05:44 +00001971
Benjamin Peterson05380642010-08-23 19:35:39 +00001972 /* Reverse sort stability achieved by initially reversing the list,
1973 applying a stable forward sort, then reversing the final result. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001974 if (reverse) {
1975 if (keys != NULL)
1976 reverse_slice(&keys[0], &keys[saved_ob_size]);
1977 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
1978 }
Benjamin Peterson05380642010-08-23 19:35:39 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 /* March over the array once, left to right, finding natural runs,
1981 * and extending short natural runs to minrun elements.
1982 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 minrun = merge_compute_minrun(nremaining);
1984 do {
1985 int descending;
1986 Py_ssize_t n;
Tim Peters330f9e92002-07-19 07:05:44 +00001987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 /* Identify next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00001989 n = count_run(lo.keys, lo.keys + nremaining, &descending);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 if (n < 0)
1991 goto fail;
1992 if (descending)
Daniel Stutzbach98338222010-12-02 21:55:33 +00001993 reverse_sortslice(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 /* If short, extend to min(minrun, nremaining). */
1995 if (n < minrun) {
1996 const Py_ssize_t force = nremaining <= minrun ?
1997 nremaining : minrun;
Daniel Stutzbach98338222010-12-02 21:55:33 +00001998 if (binarysort(lo, lo.keys + force, lo.keys + n) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 goto fail;
2000 n = force;
2001 }
2002 /* Push run onto pending-runs stack, and maybe merge. */
2003 assert(ms.n < MAX_MERGE_PENDING);
2004 ms.pending[ms.n].base = lo;
2005 ms.pending[ms.n].len = n;
2006 ++ms.n;
2007 if (merge_collapse(&ms) < 0)
2008 goto fail;
2009 /* Advance to find next run. */
Daniel Stutzbach98338222010-12-02 21:55:33 +00002010 sortslice_advance(&lo, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 nremaining -= n;
2012 } while (nremaining);
Tim Peters330f9e92002-07-19 07:05:44 +00002013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 if (merge_force_collapse(&ms) < 0)
2015 goto fail;
2016 assert(ms.n == 1);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002017 assert(keys == NULL
2018 ? ms.pending[0].base.keys == saved_ob_item
2019 : ms.pending[0].base.keys == &keys[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 assert(ms.pending[0].len == saved_ob_size);
Daniel Stutzbach98338222010-12-02 21:55:33 +00002021 lo = ms.pending[0].base;
Tim Petersa64dc242002-08-01 02:13:36 +00002022
2023succeed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 result = Py_None;
Tim Peters330f9e92002-07-19 07:05:44 +00002025fail:
Daniel Stutzbach98338222010-12-02 21:55:33 +00002026 if (keys != NULL) {
2027 for (i = 0; i < saved_ob_size; i++)
2028 Py_DECREF(keys[i]);
2029 if (keys != &ms.temparray[saved_ob_size+1])
2030 PyMem_FREE(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 if (self->allocated != -1 && result != NULL) {
2034 /* The user mucked with the list during the sort,
2035 * and we don't already have another error to report.
2036 */
2037 PyErr_SetString(PyExc_ValueError, "list modified during sort");
2038 result = NULL;
2039 }
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 if (reverse && saved_ob_size > 1)
2042 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 merge_freemem(&ms);
Michael W. Hudson1df0f652003-12-04 11:25:46 +00002045
Daniel Stutzbach98338222010-12-02 21:55:33 +00002046keyfunc_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 final_ob_item = self->ob_item;
2048 i = Py_SIZE(self);
2049 Py_SIZE(self) = saved_ob_size;
2050 self->ob_item = saved_ob_item;
2051 self->allocated = saved_allocated;
2052 if (final_ob_item != NULL) {
2053 /* we cannot use list_clear() for this because it does not
2054 guarantee that the list is really empty when it returns */
2055 while (--i >= 0) {
2056 Py_XDECREF(final_ob_item[i]);
2057 }
2058 PyMem_FREE(final_ob_item);
2059 }
2060 Py_XINCREF(result);
2061 return result;
Guido van Rossum3f236de1996-12-10 23:55:39 +00002062}
Tim Peters330f9e92002-07-19 07:05:44 +00002063#undef IFLT
Tim Peters66860f62002-08-04 17:47:26 +00002064#undef ISLT
Tim Peters330f9e92002-07-19 07:05:44 +00002065
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002066int
Fred Drakea2f55112000-07-09 15:16:51 +00002067PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 if (v == NULL || !PyList_Check(v)) {
2070 PyErr_BadInternalCall();
2071 return -1;
2072 }
2073 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
2074 if (v == NULL)
2075 return -1;
2076 Py_DECREF(v);
2077 return 0;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002078}
2079
Guido van Rossumb86c5492001-02-12 22:06:02 +00002080static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002081listreverse(PyListObject *self)
Guido van Rossumb86c5492001-02-12 22:06:02 +00002082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 if (Py_SIZE(self) > 1)
2084 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2085 Py_RETURN_NONE;
Guido van Rossumed98d481991-03-06 13:07:53 +00002086}
2087
Guido van Rossum84c76f51990-10-30 13:32:20 +00002088int
Fred Drakea2f55112000-07-09 15:16:51 +00002089PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 PyListObject *self = (PyListObject *)v;
Tim Peters6063e262002-08-08 01:06:39 +00002092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 if (v == NULL || !PyList_Check(v)) {
2094 PyErr_BadInternalCall();
2095 return -1;
2096 }
2097 if (Py_SIZE(self) > 1)
2098 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
2099 return 0;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00002100}
2101
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002102PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00002103PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 PyObject *w;
2106 PyObject **p, **q;
2107 Py_ssize_t n;
2108 if (v == NULL || !PyList_Check(v)) {
2109 PyErr_BadInternalCall();
2110 return NULL;
2111 }
2112 n = Py_SIZE(v);
2113 w = PyTuple_New(n);
2114 if (w == NULL)
2115 return NULL;
2116 p = ((PyTupleObject *)w)->ob_item;
2117 q = ((PyListObject *)v)->ob_item;
2118 while (--n >= 0) {
2119 Py_INCREF(*q);
2120 *p = *q;
2121 p++;
2122 q++;
2123 }
2124 return w;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00002125}
2126
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002127static PyObject *
Raymond Hettingerd05abde2003-06-17 05:05:49 +00002128listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00002129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 Py_ssize_t i, start=0, stop=Py_SIZE(self);
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002131 PyObject *v;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002132
Petri Lehtinenebfaabd2011-11-06 21:02:39 +02002133 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2134 _PyEval_SliceIndex, &start,
2135 _PyEval_SliceIndex, &stop))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 return NULL;
2137 if (start < 0) {
2138 start += Py_SIZE(self);
2139 if (start < 0)
2140 start = 0;
2141 }
2142 if (stop < 0) {
2143 stop += Py_SIZE(self);
2144 if (stop < 0)
2145 stop = 0;
2146 }
2147 for (i = start; i < stop && i < Py_SIZE(self); i++) {
2148 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2149 if (cmp > 0)
2150 return PyLong_FromSsize_t(i);
2151 else if (cmp < 0)
2152 return NULL;
2153 }
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +01002154 PyErr_Format(PyExc_ValueError, "%R is not in list", v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002156}
2157
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002158static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002159listcount(PyListObject *self, PyObject *v)
Guido van Rossume6f7d181991-10-20 20:20:40 +00002160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 Py_ssize_t count = 0;
2162 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 for (i = 0; i < Py_SIZE(self); i++) {
2165 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2166 if (cmp > 0)
2167 count++;
2168 else if (cmp < 0)
2169 return NULL;
2170 }
2171 return PyLong_FromSsize_t(count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00002172}
2173
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002174static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002175listremove(PyListObject *self, PyObject *v)
Guido van Rossumed98d481991-03-06 13:07:53 +00002176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 Py_ssize_t i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00002178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 for (i = 0; i < Py_SIZE(self); i++) {
2180 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
2181 if (cmp > 0) {
2182 if (list_ass_slice(self, i, i+1,
2183 (PyObject *)NULL) == 0)
2184 Py_RETURN_NONE;
2185 return NULL;
2186 }
2187 else if (cmp < 0)
2188 return NULL;
2189 }
2190 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2191 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00002192}
2193
Jeremy Hylton8caad492000-06-23 14:18:11 +00002194static int
2195list_traverse(PyListObject *o, visitproc visit, void *arg)
2196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 Py_ssize_t i;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 for (i = Py_SIZE(o); --i >= 0; )
2200 Py_VISIT(o->ob_item[i]);
2201 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002202}
2203
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002204static PyObject *
2205list_richcompare(PyObject *v, PyObject *w, int op)
2206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 PyListObject *vl, *wl;
2208 Py_ssize_t i;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002209
Brian Curtindfc80e32011-08-10 20:28:54 -05002210 if (!PyList_Check(v) || !PyList_Check(w))
2211 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 vl = (PyListObject *)v;
2214 wl = (PyListObject *)w;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
2217 /* Shortcut: if the lengths differ, the lists differ */
2218 PyObject *res;
2219 if (op == Py_EQ)
2220 res = Py_False;
2221 else
2222 res = Py_True;
2223 Py_INCREF(res);
2224 return res;
2225 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 /* Search for the first index where items are different */
2228 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
2229 int k = PyObject_RichCompareBool(vl->ob_item[i],
2230 wl->ob_item[i], Py_EQ);
2231 if (k < 0)
2232 return NULL;
2233 if (!k)
2234 break;
2235 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
2238 /* No more items to compare -- compare sizes */
2239 Py_ssize_t vs = Py_SIZE(vl);
2240 Py_ssize_t ws = Py_SIZE(wl);
2241 int cmp;
2242 PyObject *res;
2243 switch (op) {
2244 case Py_LT: cmp = vs < ws; break;
2245 case Py_LE: cmp = vs <= ws; break;
2246 case Py_EQ: cmp = vs == ws; break;
2247 case Py_NE: cmp = vs != ws; break;
2248 case Py_GT: cmp = vs > ws; break;
2249 case Py_GE: cmp = vs >= ws; break;
2250 default: return NULL; /* cannot happen */
2251 }
2252 if (cmp)
2253 res = Py_True;
2254 else
2255 res = Py_False;
2256 Py_INCREF(res);
2257 return res;
2258 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 /* We have an item that differs -- shortcuts for EQ/NE */
2261 if (op == Py_EQ) {
2262 Py_INCREF(Py_False);
2263 return Py_False;
2264 }
2265 if (op == Py_NE) {
2266 Py_INCREF(Py_True);
2267 return Py_True;
2268 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 /* Compare the final item again using the proper operator */
2271 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00002272}
2273
Tim Peters6d6c1a32001-08-02 04:15:00 +00002274static int
2275list_init(PyListObject *self, PyObject *args, PyObject *kw)
2276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 PyObject *arg = NULL;
2278 static char *kwlist[] = {"sequence", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2281 return -1;
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 /* Verify list invariants established by PyType_GenericAlloc() */
2284 assert(0 <= Py_SIZE(self));
2285 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
2286 assert(self->ob_item != NULL ||
2287 self->allocated == 0 || self->allocated == -1);
Raymond Hettingerc0aaa2d2004-07-29 23:31:29 +00002288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 /* Empty previous contents */
2290 if (self->ob_item != NULL) {
2291 (void)list_clear(self);
2292 }
2293 if (arg != NULL) {
2294 PyObject *rv = listextend(self, arg);
2295 if (rv == NULL)
2296 return -1;
2297 Py_DECREF(rv);
2298 }
2299 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002300}
2301
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002302static PyObject *
2303list_sizeof(PyListObject *self)
2304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 res = sizeof(PyListObject) + self->allocated * sizeof(void*);
2308 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002309}
2310
Raymond Hettinger1021c442003-11-07 15:38:09 +00002311static PyObject *list_iter(PyObject *seq);
2312static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
2313
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002314PyDoc_STRVAR(getitem_doc,
2315"x.__getitem__(y) <==> x[y]");
Raymond Hettinger1021c442003-11-07 15:38:09 +00002316PyDoc_STRVAR(reversed_doc,
2317"L.__reversed__() -- return a reverse iterator over the list");
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002318PyDoc_STRVAR(sizeof_doc,
2319"L.__sizeof__() -- size of L in memory, in bytes");
Eli Benderskycbbaa962011-02-25 05:47:53 +00002320PyDoc_STRVAR(clear_doc,
2321"L.clear() -> None -- remove all items from L");
2322PyDoc_STRVAR(copy_doc,
2323"L.copy() -> list -- a shallow copy of L");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002324PyDoc_STRVAR(append_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002325"L.append(object) -> None -- append object to end");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002326PyDoc_STRVAR(extend_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002327"L.extend(iterable) -> None -- extend list by appending elements from the iterable");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002328PyDoc_STRVAR(insert_doc,
2329"L.insert(index, object) -- insert object before index");
2330PyDoc_STRVAR(pop_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002331"L.pop([index]) -> item -- remove and return item at index (default last).\n"
2332"Raises IndexError if list is empty or index is out of range.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002333PyDoc_STRVAR(remove_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002334"L.remove(value) -> None -- remove first occurrence of value.\n"
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002335"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002336PyDoc_STRVAR(index_doc,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002337"L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2338"Raises ValueError if the value is not present.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002339PyDoc_STRVAR(count_doc,
2340"L.count(value) -> integer -- return number of occurrences of value");
2341PyDoc_STRVAR(reverse_doc,
2342"L.reverse() -- reverse *IN PLACE*");
2343PyDoc_STRVAR(sort_doc,
Georg Brandl388349a2011-10-08 18:32:40 +02002344"L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*");
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00002345
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002346static PyObject *list_subscript(PyListObject*, PyObject*);
2347
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002348static PyMethodDef list_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
2350 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
2351 {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002352 {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc},
2353 {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 {"append", (PyCFunction)listappend, METH_O, append_doc},
2355 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
Eli Benderskycbbaa962011-02-25 05:47:53 +00002356 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
2358 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
2359 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
2360 {"count", (PyCFunction)listcount, METH_O, count_doc},
2361 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
2362 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
2363 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002364};
2365
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002366static PySequenceMethods list_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 (lenfunc)list_length, /* sq_length */
2368 (binaryfunc)list_concat, /* sq_concat */
2369 (ssizeargfunc)list_repeat, /* sq_repeat */
2370 (ssizeargfunc)list_item, /* sq_item */
2371 0, /* sq_slice */
2372 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
2373 0, /* sq_ass_slice */
2374 (objobjproc)list_contains, /* sq_contains */
2375 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
2376 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002377};
2378
Neal Norwitz2c2e8272002-06-14 02:04:18 +00002379PyDoc_STRVAR(list_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002380"list() -> new empty list\n"
2381"list(iterable) -> new list initialized from iterable's items");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002382
Jeremy Hyltona4b4c3b2002-07-13 03:51:17 +00002383static PyObject *
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002384list_subscript(PyListObject* self, PyObject* item)
2385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 if (PyIndex_Check(item)) {
2387 Py_ssize_t i;
2388 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2389 if (i == -1 && PyErr_Occurred())
2390 return NULL;
2391 if (i < 0)
2392 i += PyList_GET_SIZE(self);
2393 return list_item(self, i);
2394 }
2395 else if (PySlice_Check(item)) {
2396 Py_ssize_t start, stop, step, slicelength, cur, i;
2397 PyObject* result;
2398 PyObject* it;
2399 PyObject **src, **dest;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002400
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002401 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 &start, &stop, &step, &slicelength) < 0) {
2403 return NULL;
2404 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 if (slicelength <= 0) {
2407 return PyList_New(0);
2408 }
2409 else if (step == 1) {
2410 return list_slice(self, start, stop);
2411 }
2412 else {
2413 result = PyList_New(slicelength);
2414 if (!result) return NULL;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 src = self->ob_item;
2417 dest = ((PyListObject *)result)->ob_item;
2418 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002419 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 it = src[cur];
2421 Py_INCREF(it);
2422 dest[i] = it;
2423 }
Tim Peters3b01a122002-07-19 02:35:45 +00002424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 return result;
2426 }
2427 }
2428 else {
2429 PyErr_Format(PyExc_TypeError,
2430 "list indices must be integers, not %.200s",
2431 item->ob_type->tp_name);
2432 return NULL;
2433 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002434}
2435
Tim Peters3b01a122002-07-19 02:35:45 +00002436static int
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002437list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
2438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 if (PyIndex_Check(item)) {
2440 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2441 if (i == -1 && PyErr_Occurred())
2442 return -1;
2443 if (i < 0)
2444 i += PyList_GET_SIZE(self);
2445 return list_ass_item(self, i, value);
2446 }
2447 else if (PySlice_Check(item)) {
2448 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002449
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002450 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 &start, &stop, &step, &slicelength) < 0) {
2452 return -1;
2453 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 if (step == 1)
2456 return list_ass_slice(self, start, stop, value);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 /* Make sure s[5:2] = [..] inserts at the right place:
2459 before 5, not before 2. */
2460 if ((step < 0 && start < stop) ||
2461 (step > 0 && start > stop))
2462 stop = start;
Thomas Woutersed03b412007-08-28 21:37:11 +00002463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 if (value == NULL) {
2465 /* delete slice */
2466 PyObject **garbage;
2467 size_t cur;
2468 Py_ssize_t i;
Tim Peters3b01a122002-07-19 02:35:45 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 if (slicelength <= 0)
2471 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 if (step < 0) {
2474 stop = start + 1;
2475 start = stop + step*(slicelength - 1) - 1;
2476 step = -step;
2477 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 assert((size_t)slicelength <=
2480 PY_SIZE_MAX / sizeof(PyObject*));
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 garbage = (PyObject**)
2483 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2484 if (!garbage) {
2485 PyErr_NoMemory();
2486 return -1;
2487 }
Tim Peters3b01a122002-07-19 02:35:45 +00002488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 /* drawing pictures might help understand these for
2490 loops. Basically, we memmove the parts of the
2491 list that are *not* part of the slice: step-1
2492 items for each item that is part of the slice,
2493 and then tail end of the list that was not
2494 covered by the slice */
2495 for (cur = start, i = 0;
2496 cur < (size_t)stop;
2497 cur += step, i++) {
2498 Py_ssize_t lim = step - 1;
Michael W. Hudson56796f62002-07-29 14:35:04 +00002499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 garbage[i] = PyList_GET_ITEM(self, cur);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 if (cur + step >= (size_t)Py_SIZE(self)) {
2503 lim = Py_SIZE(self) - cur - 1;
2504 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00002505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 memmove(self->ob_item + cur - i,
2507 self->ob_item + cur + 1,
2508 lim * sizeof(PyObject *));
2509 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002510 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 if (cur < (size_t)Py_SIZE(self)) {
2512 memmove(self->ob_item + cur - slicelength,
2513 self->ob_item + cur,
2514 (Py_SIZE(self) - cur) *
2515 sizeof(PyObject *));
2516 }
Raymond Hettingera6366fe2004-03-09 13:05:22 +00002517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 Py_SIZE(self) -= slicelength;
2519 list_resize(self, Py_SIZE(self));
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 for (i = 0; i < slicelength; i++) {
2522 Py_DECREF(garbage[i]);
2523 }
2524 PyMem_FREE(garbage);
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 return 0;
2527 }
2528 else {
2529 /* assign slice */
2530 PyObject *ins, *seq;
2531 PyObject **garbage, **seqitems, **selfitems;
2532 Py_ssize_t cur, i;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 /* protect against a[::-1] = a */
2535 if (self == (PyListObject*)value) {
2536 seq = list_slice((PyListObject*)value, 0,
2537 PyList_GET_SIZE(value));
2538 }
2539 else {
2540 seq = PySequence_Fast(value,
2541 "must assign iterable "
2542 "to extended slice");
2543 }
2544 if (!seq)
2545 return -1;
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
2548 PyErr_Format(PyExc_ValueError,
2549 "attempt to assign sequence of "
2550 "size %zd to extended slice of "
2551 "size %zd",
2552 PySequence_Fast_GET_SIZE(seq),
2553 slicelength);
2554 Py_DECREF(seq);
2555 return -1;
2556 }
Michael W. Hudsona69c0302002-12-05 21:32:32 +00002557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 if (!slicelength) {
2559 Py_DECREF(seq);
2560 return 0;
2561 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 garbage = (PyObject**)
2564 PyMem_MALLOC(slicelength*sizeof(PyObject*));
2565 if (!garbage) {
2566 Py_DECREF(seq);
2567 PyErr_NoMemory();
2568 return -1;
2569 }
Tim Peters3b01a122002-07-19 02:35:45 +00002570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 selfitems = self->ob_item;
2572 seqitems = PySequence_Fast_ITEMS(seq);
2573 for (cur = start, i = 0; i < slicelength;
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002574 cur += (size_t)step, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 garbage[i] = selfitems[cur];
2576 ins = seqitems[i];
2577 Py_INCREF(ins);
2578 selfitems[cur] = ins;
2579 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002581 for (i = 0; i < slicelength; i++) {
2582 Py_DECREF(garbage[i]);
2583 }
Tim Peters3b01a122002-07-19 02:35:45 +00002584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 PyMem_FREE(garbage);
2586 Py_DECREF(seq);
Tim Peters3b01a122002-07-19 02:35:45 +00002587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 return 0;
2589 }
2590 }
2591 else {
2592 PyErr_Format(PyExc_TypeError,
2593 "list indices must be integers, not %.200s",
2594 item->ob_type->tp_name);
2595 return -1;
2596 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002597}
2598
2599static PyMappingMethods list_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 (lenfunc)list_length,
2601 (binaryfunc)list_subscript,
2602 (objobjargproc)list_ass_subscript
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002603};
2604
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002605PyTypeObject PyList_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2607 "list",
2608 sizeof(PyListObject),
2609 0,
2610 (destructor)list_dealloc, /* tp_dealloc */
2611 0, /* tp_print */
2612 0, /* tp_getattr */
2613 0, /* tp_setattr */
2614 0, /* tp_reserved */
2615 (reprfunc)list_repr, /* tp_repr */
2616 0, /* tp_as_number */
2617 &list_as_sequence, /* tp_as_sequence */
2618 &list_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002619 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002620 0, /* tp_call */
2621 0, /* tp_str */
2622 PyObject_GenericGetAttr, /* tp_getattro */
2623 0, /* tp_setattro */
2624 0, /* tp_as_buffer */
2625 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2626 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
2627 list_doc, /* tp_doc */
2628 (traverseproc)list_traverse, /* tp_traverse */
2629 (inquiry)list_clear, /* tp_clear */
2630 list_richcompare, /* tp_richcompare */
2631 0, /* tp_weaklistoffset */
2632 list_iter, /* tp_iter */
2633 0, /* tp_iternext */
2634 list_methods, /* tp_methods */
2635 0, /* tp_members */
2636 0, /* tp_getset */
2637 0, /* tp_base */
2638 0, /* tp_dict */
2639 0, /* tp_descr_get */
2640 0, /* tp_descr_set */
2641 0, /* tp_dictoffset */
2642 (initproc)list_init, /* tp_init */
2643 PyType_GenericAlloc, /* tp_alloc */
2644 PyType_GenericNew, /* tp_new */
2645 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002646};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00002647
2648
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002649/*********************** List Iterator **************************/
2650
2651typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 PyObject_HEAD
2653 long it_index;
2654 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002655} listiterobject;
2656
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002657static PyObject *list_iter(PyObject *);
2658static void listiter_dealloc(listiterobject *);
2659static int listiter_traverse(listiterobject *, visitproc, void *);
2660static PyObject *listiter_next(listiterobject *);
2661static PyObject *listiter_len(listiterobject *);
Raymond Hettinger435bf582004-03-18 22:43:10 +00002662
Armin Rigof5b3e362006-02-11 21:32:43 +00002663PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002664
2665static PyMethodDef listiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
2667 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +00002668};
2669
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002670PyTypeObject PyListIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2672 "list_iterator", /* tp_name */
2673 sizeof(listiterobject), /* tp_basicsize */
2674 0, /* tp_itemsize */
2675 /* methods */
2676 (destructor)listiter_dealloc, /* tp_dealloc */
2677 0, /* tp_print */
2678 0, /* tp_getattr */
2679 0, /* tp_setattr */
2680 0, /* tp_reserved */
2681 0, /* tp_repr */
2682 0, /* tp_as_number */
2683 0, /* tp_as_sequence */
2684 0, /* tp_as_mapping */
2685 0, /* tp_hash */
2686 0, /* tp_call */
2687 0, /* tp_str */
2688 PyObject_GenericGetAttr, /* tp_getattro */
2689 0, /* tp_setattro */
2690 0, /* tp_as_buffer */
2691 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2692 0, /* tp_doc */
2693 (traverseproc)listiter_traverse, /* tp_traverse */
2694 0, /* tp_clear */
2695 0, /* tp_richcompare */
2696 0, /* tp_weaklistoffset */
2697 PyObject_SelfIter, /* tp_iter */
2698 (iternextfunc)listiter_next, /* tp_iternext */
2699 listiter_methods, /* tp_methods */
2700 0, /* tp_members */
Raymond Hettinger14bd6de2002-05-31 21:40:38 +00002701};
Raymond Hettinger1021c442003-11-07 15:38:09 +00002702
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002703
2704static PyObject *
2705list_iter(PyObject *seq)
2706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 listiterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 if (!PyList_Check(seq)) {
2710 PyErr_BadInternalCall();
2711 return NULL;
2712 }
2713 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
2714 if (it == NULL)
2715 return NULL;
2716 it->it_index = 0;
2717 Py_INCREF(seq);
2718 it->it_seq = (PyListObject *)seq;
2719 _PyObject_GC_TRACK(it);
2720 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002721}
2722
2723static void
2724listiter_dealloc(listiterobject *it)
2725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 _PyObject_GC_UNTRACK(it);
2727 Py_XDECREF(it->it_seq);
2728 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002729}
2730
2731static int
2732listiter_traverse(listiterobject *it, visitproc visit, void *arg)
2733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 Py_VISIT(it->it_seq);
2735 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002736}
2737
2738static PyObject *
2739listiter_next(listiterobject *it)
2740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 PyListObject *seq;
2742 PyObject *item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 assert(it != NULL);
2745 seq = it->it_seq;
2746 if (seq == NULL)
2747 return NULL;
2748 assert(PyList_Check(seq));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 if (it->it_index < PyList_GET_SIZE(seq)) {
2751 item = PyList_GET_ITEM(seq, it->it_index);
2752 ++it->it_index;
2753 Py_INCREF(item);
2754 return item;
2755 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 Py_DECREF(seq);
2758 it->it_seq = NULL;
2759 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002760}
2761
2762static PyObject *
2763listiter_len(listiterobject *it)
2764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 Py_ssize_t len;
2766 if (it->it_seq) {
2767 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
2768 if (len >= 0)
2769 return PyLong_FromSsize_t(len);
2770 }
2771 return PyLong_FromLong(0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002772}
Raymond Hettinger1021c442003-11-07 15:38:09 +00002773/*********************** List Reverse Iterator **************************/
2774
2775typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 PyObject_HEAD
2777 Py_ssize_t it_index;
2778 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
Raymond Hettinger1021c442003-11-07 15:38:09 +00002779} listreviterobject;
2780
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002781static PyObject *list_reversed(PyListObject *, PyObject *);
2782static void listreviter_dealloc(listreviterobject *);
2783static int listreviter_traverse(listreviterobject *, visitproc, void *);
2784static PyObject *listreviter_next(listreviterobject *);
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002785static PyObject *listreviter_len(listreviterobject *);
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002786
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002787static PyMethodDef listreviter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
2789 {NULL, NULL} /* sentinel */
Raymond Hettingeref9bf402004-03-10 10:10:42 +00002790};
2791
Raymond Hettinger1021c442003-11-07 15:38:09 +00002792PyTypeObject PyListRevIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2794 "list_reverseiterator", /* tp_name */
2795 sizeof(listreviterobject), /* tp_basicsize */
2796 0, /* tp_itemsize */
2797 /* methods */
2798 (destructor)listreviter_dealloc, /* tp_dealloc */
2799 0, /* tp_print */
2800 0, /* tp_getattr */
2801 0, /* tp_setattr */
2802 0, /* tp_reserved */
2803 0, /* tp_repr */
2804 0, /* tp_as_number */
2805 0, /* tp_as_sequence */
2806 0, /* tp_as_mapping */
2807 0, /* tp_hash */
2808 0, /* tp_call */
2809 0, /* tp_str */
2810 PyObject_GenericGetAttr, /* tp_getattro */
2811 0, /* tp_setattro */
2812 0, /* tp_as_buffer */
2813 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2814 0, /* tp_doc */
2815 (traverseproc)listreviter_traverse, /* tp_traverse */
2816 0, /* tp_clear */
2817 0, /* tp_richcompare */
2818 0, /* tp_weaklistoffset */
2819 PyObject_SelfIter, /* tp_iter */
2820 (iternextfunc)listreviter_next, /* tp_iternext */
2821 listreviter_methods, /* tp_methods */
2822 0,
Raymond Hettinger1021c442003-11-07 15:38:09 +00002823};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002824
2825static PyObject *
2826list_reversed(PyListObject *seq, PyObject *unused)
2827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 listreviterobject *it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
2831 if (it == NULL)
2832 return NULL;
2833 assert(PyList_Check(seq));
2834 it->it_index = PyList_GET_SIZE(seq) - 1;
2835 Py_INCREF(seq);
2836 it->it_seq = seq;
2837 PyObject_GC_Track(it);
2838 return (PyObject *)it;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002839}
2840
2841static void
2842listreviter_dealloc(listreviterobject *it)
2843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 PyObject_GC_UnTrack(it);
2845 Py_XDECREF(it->it_seq);
2846 PyObject_GC_Del(it);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002847}
2848
2849static int
2850listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
2851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 Py_VISIT(it->it_seq);
2853 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002854}
2855
2856static PyObject *
2857listreviter_next(listreviterobject *it)
2858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 PyObject *item;
2860 Py_ssize_t index = it->it_index;
2861 PyListObject *seq = it->it_seq;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 if (index>=0 && index < PyList_GET_SIZE(seq)) {
2864 item = PyList_GET_ITEM(seq, index);
2865 it->it_index--;
2866 Py_INCREF(item);
2867 return item;
2868 }
2869 it->it_index = -1;
2870 if (seq != NULL) {
2871 it->it_seq = NULL;
2872 Py_DECREF(seq);
2873 }
2874 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002875}
2876
Raymond Hettingerf5b64112008-12-02 21:33:45 +00002877static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002878listreviter_len(listreviterobject *it)
2879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 Py_ssize_t len = it->it_index + 1;
2881 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2882 len = 0;
2883 return PyLong_FromSsize_t(len);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002884}