blob: b77cc0af0270b4238d82c0c95523aa036b4fc07f [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* List object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
5
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00006#ifdef STDC_HEADERS
7#include <stddef.h>
8#else
9#include <sys/types.h> /* For size_t */
10#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011
Guido van Rossuma46d51d1995-01-26 22:59:43 +000012static int
Fred Drakea2f55112000-07-09 15:16:51 +000013roundupsize(int n)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000014{
Tim Peters65b8b842001-05-26 05:28:40 +000015 unsigned int nbits = 0;
16 unsigned int n2 = (unsigned int)n >> 5;
17
18 /* Round up:
19 * If n < 256, to a multiple of 8.
20 * If n < 2048, to a multiple of 64.
21 * If n < 16384, to a multiple of 512.
22 * If n < 131072, to a multiple of 4096.
23 * If n < 1048576, to a multiple of 32768.
24 * If n < 8388608, to a multiple of 262144.
25 * If n < 67108864, to a multiple of 2097152.
26 * If n < 536870912, to a multiple of 16777216.
27 * ...
28 * If n < 2**(5+3*i), to a multiple of 2**(3*i).
29 *
30 * This over-allocates proportional to the list size, making room
31 * for additional growth. The over-allocation is mild, but is
32 * enough to give linear-time amortized behavior over a long
33 * sequence of appends() in the presence of a poorly-performing
34 * system realloc() (which is a reality, e.g., across all flavors
35 * of Windows, with Win9x behavior being particularly bad -- and
36 * we've still got address space fragmentation problems on Win9x
37 * even with this scheme, although it requires much longer lists to
38 * provoke them than it used to).
39 */
40 do {
41 n2 >>= 3;
42 nbits += 3;
43 } while (n2);
44 return ((n >> nbits) + 1) << nbits;
45 }
Guido van Rossuma46d51d1995-01-26 22:59:43 +000046
Guido van Rossuma27d1121997-08-25 18:36:23 +000047#define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems))
Guido van Rossuma46d51d1995-01-26 22:59:43 +000048
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000050PyList_New(int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051{
52 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000053 PyListObject *op;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000054 size_t nbytes;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055 if (size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000056 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057 return NULL;
58 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000059 nbytes = size * sizeof(PyObject *);
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000060 /* Check for overflow */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000061 if (nbytes / sizeof(PyObject *) != (size_t)size) {
62 return PyErr_NoMemory();
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000063 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000064 /* PyObject_NewVar is inlined */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +000065 op = (PyListObject *) PyObject_MALLOC(sizeof(PyListObject)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000066 + PyGC_HEAD_SIZE);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000068 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000070 op = (PyListObject *) PyObject_FROM_GC(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071 if (size <= 0) {
72 op->ob_item = NULL;
73 }
74 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +000075 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 if (op->ob_item == NULL) {
Vladimir Marangozov467a67e2000-07-15 03:31:31 +000077 PyObject_FREE(PyObject_AS_GC(op));
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 }
80 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000081 PyObject_INIT_VAR(op, &PyList_Type, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082 for (i = 0; i < size; i++)
83 op->ob_item[i] = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000084 PyObject_GC_Init(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086}
87
88int
Fred Drakea2f55112000-07-09 15:16:51 +000089PyList_Size(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091 if (!PyList_Check(op)) {
92 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 return -1;
94 }
95 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000096 return ((PyListObject *)op) -> ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097}
98
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099static PyObject *indexerr;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000100
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000101PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000102PyList_GetItem(PyObject *op, int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000104 if (!PyList_Check(op)) {
105 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106 return NULL;
107 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000108 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000109 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110 indexerr = PyString_FromString(
111 "list index out of range");
112 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113 return NULL;
114 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116}
117
118int
Fred Drakea2f55112000-07-09 15:16:51 +0000119PyList_SetItem(register PyObject *op, register int i,
120 register PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000122 register PyObject *olditem;
123 register PyObject **p;
124 if (!PyList_Check(op)) {
125 Py_XDECREF(newitem);
126 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000127 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
130 Py_XDECREF(newitem);
131 PyErr_SetString(PyExc_IndexError,
132 "list assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000133 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000135 p = ((PyListObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000136 olditem = *p;
137 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000138 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139 return 0;
140}
141
142static int
Fred Drakea2f55112000-07-09 15:16:51 +0000143ins1(PyListObject *self, int where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144{
145 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000146 PyObject **items;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000147 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000149 return -1;
150 }
Trent Micka5846642000-08-13 22:47:45 +0000151 if (self->ob_size == INT_MAX) {
152 PyErr_SetString(PyExc_OverflowError,
153 "cannot add more objects to list");
154 return -1;
155 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156 items = self->ob_item;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157 NRESIZE(items, PyObject *, self->ob_size+1);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000158 if (items == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000160 return -1;
161 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162 if (where < 0)
163 where = 0;
164 if (where > self->ob_size)
165 where = self->ob_size;
166 for (i = self->ob_size; --i >= where; )
167 items[i+1] = items[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000168 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169 items[where] = v;
170 self->ob_item = items;
171 self->ob_size++;
172 return 0;
173}
174
175int
Fred Drakea2f55112000-07-09 15:16:51 +0000176PyList_Insert(PyObject *op, int where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178 if (!PyList_Check(op)) {
179 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000180 return -1;
181 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000183}
184
185int
Fred Drakea2f55112000-07-09 15:16:51 +0000186PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000188 if (!PyList_Check(op)) {
189 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000190 return -1;
191 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000192 return ins1((PyListObject *)op,
193 (int) ((PyListObject *)op)->ob_size, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194}
195
196/* Methods */
197
198static void
Fred Drakea2f55112000-07-09 15:16:51 +0000199list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200{
201 int i;
Guido van Rossumd724b232000-03-13 16:01:29 +0000202 Py_TRASHCAN_SAFE_BEGIN(op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000203 PyObject_GC_Fini(op);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000204 if (op->ob_item != NULL) {
Guido van Rossumfa717011999-06-09 15:19:34 +0000205 /* Do it backwards, for Christian Tismer.
206 There's a simple test case where somehow this reduces
207 thrashing when a *very* large list is created and
208 immediately deleted. */
209 i = op->ob_size;
210 while (--i >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211 Py_XDECREF(op->ob_item[i]);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000212 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000213 PyMem_FREE(op->ob_item);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000214 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000215 op = (PyListObject *) PyObject_AS_GC(op);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000216 PyObject_DEL(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000217 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218}
219
Guido van Rossum90933611991-06-07 16:10:43 +0000220static int
Fred Drakea2f55112000-07-09 15:16:51 +0000221list_print(PyListObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222{
223 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000224
225 i = Py_ReprEnter((PyObject*)op);
226 if (i != 0) {
227 if (i < 0)
228 return i;
229 fprintf(fp, "[...]");
230 return 0;
231 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232 fprintf(fp, "[");
Guido van Rossum90933611991-06-07 16:10:43 +0000233 for (i = 0; i < op->ob_size; i++) {
234 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235 fprintf(fp, ", ");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000236 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
237 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000238 return -1;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000239 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240 }
241 fprintf(fp, "]");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000242 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000243 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244}
245
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000246static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000247list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249 int i;
Tim Petersa7259592001-06-16 05:11:17 +0000250 PyObject *s, *temp;
251 PyObject *pieces = NULL, *result = NULL;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000252
253 i = Py_ReprEnter((PyObject*)v);
254 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000255 return i > 0 ? PyString_FromString("[...]") : NULL;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000256 }
Tim Petersa7259592001-06-16 05:11:17 +0000257
258 if (v->ob_size == 0) {
259 result = PyString_FromString("[]");
260 goto Done;
261 }
262
263 pieces = PyList_New(0);
264 if (pieces == NULL)
265 goto Done;
266
267 /* Do repr() on each element. Note that this may mutate the list,
268 so must refetch the list size on each iteration. */
269 for (i = 0; i < v->ob_size; ++i) {
270 int status;
271 s = PyObject_Repr(v->ob_item[i]);
272 if (s == NULL)
273 goto Done;
274 status = PyList_Append(pieces, s);
275 Py_DECREF(s); /* append created a new ref */
276 if (status < 0)
277 goto Done;
278 }
279
280 /* Add "[]" decorations to the first and last items. */
281 assert(PyList_GET_SIZE(pieces) > 0);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282 s = PyString_FromString("[");
Tim Petersa7259592001-06-16 05:11:17 +0000283 if (s == NULL)
284 goto Done;
285 temp = PyList_GET_ITEM(pieces, 0);
286 PyString_ConcatAndDel(&s, temp);
287 PyList_SET_ITEM(pieces, 0, s);
288 if (s == NULL)
289 goto Done;
290
291 s = PyString_FromString("]");
292 if (s == NULL)
293 goto Done;
294 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
295 PyString_ConcatAndDel(&temp, s);
296 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
297 if (temp == NULL)
298 goto Done;
299
300 /* Paste them all together with ", " between. */
301 s = PyString_FromString(", ");
302 if (s == NULL)
303 goto Done;
304 result = _PyString_Join(s, pieces);
305 Py_DECREF(s);
306
307Done:
308 Py_XDECREF(pieces);
Guido van Rossumfb376de1998-04-10 22:47:27 +0000309 Py_ReprLeave((PyObject *)v);
Tim Petersa7259592001-06-16 05:11:17 +0000310 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311}
312
313static int
Fred Drakea2f55112000-07-09 15:16:51 +0000314list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315{
316 return a->ob_size;
317}
318
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000319
320
321static int
Fred Drakea2f55112000-07-09 15:16:51 +0000322list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000323{
Guido van Rossum65e1cea2001-01-17 22:11:59 +0000324 int i;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000325
326 for (i = 0; i < a->ob_size; ++i) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +0000327 int cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
328 Py_EQ);
329 if (cmp > 0)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000330 return 1;
Guido van Rossum65e1cea2001-01-17 22:11:59 +0000331 else if (cmp < 0)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000332 return -1;
333 }
334 return 0;
335}
336
337
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000338static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000339list_item(PyListObject *a, int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340{
341 if (i < 0 || i >= a->ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000342 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000343 indexerr = PyString_FromString(
344 "list index out of range");
345 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346 return NULL;
347 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000348 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349 return a->ob_item[i];
350}
351
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000352static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000353list_slice(PyListObject *a, int ilow, int ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000355 PyListObject *np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356 int i;
357 if (ilow < 0)
358 ilow = 0;
359 else if (ilow > a->ob_size)
360 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361 if (ihigh < ilow)
362 ihigh = ilow;
363 else if (ihigh > a->ob_size)
364 ihigh = a->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000365 np = (PyListObject *) PyList_New(ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366 if (np == NULL)
367 return NULL;
368 for (i = ilow; i < ihigh; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369 PyObject *v = a->ob_item[i];
370 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371 np->ob_item[i - ilow] = v;
372 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000373 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374}
375
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000376PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000377PyList_GetSlice(PyObject *a, int ilow, int ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000378{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000379 if (!PyList_Check(a)) {
380 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000381 return NULL;
382 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000384}
385
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000387list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388{
389 int size;
390 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000391 PyListObject *np;
392 if (!PyList_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000393 PyErr_Format(PyExc_TypeError,
Fred Drake914a2ed2000-06-01 14:31:03 +0000394 "can only concatenate list (not \"%.200s\") to list",
395 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396 return NULL;
397 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000398#define b ((PyListObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399 size = a->ob_size + b->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000402 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403 }
404 for (i = 0; i < a->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405 PyObject *v = a->ob_item[i];
406 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407 np->ob_item[i] = v;
408 }
409 for (i = 0; i < b->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410 PyObject *v = b->ob_item[i];
411 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412 np->ob_item[i + a->ob_size] = v;
413 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000414 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415#undef b
416}
417
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000418static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000419list_repeat(PyListObject *a, int n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000420{
421 int i, j;
422 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000423 PyListObject *np;
424 PyObject **p;
Guido van Rossumed98d481991-03-06 13:07:53 +0000425 if (n < 0)
426 n = 0;
427 size = a->ob_size * n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000428 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000429 if (np == NULL)
430 return NULL;
431 p = np->ob_item;
432 for (i = 0; i < n; i++) {
433 for (j = 0; j < a->ob_size; j++) {
434 *p = a->ob_item[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000435 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000436 p++;
437 }
438 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000440}
441
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442static int
Fred Drakea2f55112000-07-09 15:16:51 +0000443list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000445 /* Because [X]DECREF can recursively invoke list operations on
446 this list, we must postpone all [X]DECREF activity until
447 after the list is back in its canonical shape. Therefore
448 we must allocate an additional array, 'recycle', into which
449 we temporarily copy the items that are deleted from the
450 list. :-( */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451 PyObject **recycle, **p;
452 PyObject **item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453 int n; /* Size of replacement list */
454 int d; /* Change in size */
455 int k; /* Loop index */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457 if (v == NULL)
458 n = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000459 else if (PyList_Check(v)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460 n = b->ob_size;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000461 if (a == b) {
462 /* Special case "a[i:j] = a" -- copy b first */
463 int ret;
464 v = list_slice(b, 0, n);
465 ret = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466 Py_DECREF(v);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000467 return ret;
468 }
469 }
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000470 else {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000471 PyErr_Format(PyExc_TypeError,
472 "must assign list (not \"%.200s\") to slice",
473 v->ob_type->tp_name);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000474 return -1;
475 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476 if (ilow < 0)
477 ilow = 0;
478 else if (ilow > a->ob_size)
479 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480 if (ihigh < ilow)
481 ihigh = ilow;
482 else if (ihigh > a->ob_size)
483 ihigh = a->ob_size;
484 item = a->ob_item;
485 d = n - (ihigh-ilow);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000486 if (ihigh > ilow)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000487 p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000488 else
489 p = recycle = NULL;
490 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000491 for (k = ilow; k < ihigh; k++)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000492 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493 if (d < 0) {
494 for (/*k = ihigh*/; k < a->ob_size; k++)
495 item[k+d] = item[k];
496 a->ob_size += d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000497 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498 a->ob_item = item;
499 }
500 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000501 else { /* Insert d items; recycle ihigh-ilow items */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000502 NRESIZE(item, PyObject *, a->ob_size + d);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000503 if (item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000504 if (recycle != NULL)
505 PyMem_DEL(recycle);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000506 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000507 return -1;
508 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000509 for (k = a->ob_size; --k >= ihigh; )
510 item[k+d] = item[k];
511 for (/*k = ihigh-1*/; k >= ilow; --k)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000512 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000513 a->ob_item = item;
514 a->ob_size += d;
515 }
516 for (k = 0; k < n; k++, ilow++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000517 PyObject *w = b->ob_item[k];
518 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000519 item[ilow] = w;
520 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000521 if (recycle) {
522 while (--p >= recycle)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523 Py_XDECREF(*p);
524 PyMem_DEL(recycle);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000525 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000526 if (a->ob_size == 0 && a->ob_item != NULL) {
527 PyMem_FREE(a->ob_item);
528 a->ob_item = NULL;
529 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000530 return 0;
531#undef b
532}
533
Guido van Rossum234f9421993-06-17 12:35:49 +0000534int
Fred Drakea2f55112000-07-09 15:16:51 +0000535PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000536{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537 if (!PyList_Check(a)) {
538 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000539 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000540 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000541 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000542}
543
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000544static PyObject *
545list_inplace_repeat(PyListObject *self, int n)
546{
547 PyObject **items;
548 int size, i, j;
549
550
551 size = PyList_GET_SIZE(self);
552 if (size == 0) {
553 Py_INCREF(self);
554 return (PyObject *)self;
555 }
556
557 items = self->ob_item;
558
559 if (n < 1) {
560 self->ob_item = NULL;
561 self->ob_size = 0;
562 for (i = 0; i < size; i++)
563 Py_XDECREF(items[i]);
564 PyMem_DEL(items);
565 Py_INCREF(self);
566 return (PyObject *)self;
567 }
568
569 NRESIZE(items, PyObject*, size*n);
570 if (items == NULL) {
571 PyErr_NoMemory();
572 goto finally;
573 }
574 self->ob_item = items;
575 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
576 for (j = 0; j < size; j++) {
577 PyObject *o = PyList_GET_ITEM(self, j);
578 Py_INCREF(o);
579 PyList_SET_ITEM(self, self->ob_size++, o);
580 }
581 }
582 Py_INCREF(self);
583 return (PyObject *)self;
584 finally:
585 return NULL;
586}
587
Guido van Rossum4a450d01991-04-03 19:05:18 +0000588static int
Fred Drakea2f55112000-07-09 15:16:51 +0000589list_ass_item(PyListObject *a, int i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000590{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000591 PyObject *old_value;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000592 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593 PyErr_SetString(PyExc_IndexError,
594 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000595 return -1;
596 }
597 if (v == NULL)
598 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000600 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000601 a->ob_item[i] = v;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000603 return 0;
604}
605
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000607ins(PyListObject *self, int where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000608{
609 if (ins1(self, where, v) != 0)
610 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000611 Py_INCREF(Py_None);
612 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000613}
614
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000615static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000616listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617{
618 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000619 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000620 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000621 return NULL;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000622 return ins(self, i, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000623}
624
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000625static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000626listappend(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000627{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628 PyObject *v;
Tim Peters442914d2001-05-26 05:50:03 +0000629 if (!PyArg_ParseTuple(args, "O:append", &v))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000630 return NULL;
631 return ins(self, (int) self->ob_size, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000632}
633
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000634static int
635listextend_internal(PyListObject *self, PyObject *b)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000636{
Barry Warsawdedf6d61998-10-09 16:37:25 +0000637 PyObject **items;
638 int selflen = PyList_GET_SIZE(self);
639 int blen;
640 register int i;
641
Jeremy Hyltondb60bb52001-01-03 22:32:16 +0000642 if (PyObject_Size(b) == 0) {
Barry Warsawdedf6d61998-10-09 16:37:25 +0000643 /* short circuit when b is empty */
Jeremy Hyltondb60bb52001-01-03 22:32:16 +0000644 Py_DECREF(b);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000645 return 0;
Jeremy Hyltondb60bb52001-01-03 22:32:16 +0000646 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000647
Barry Warsawdedf6d61998-10-09 16:37:25 +0000648 if (self == (PyListObject*)b) {
649 /* as in list_ass_slice() we must special case the
650 * situation: a.extend(a)
651 *
652 * XXX: I think this way ought to be faster than using
653 * list_slice() the way list_ass_slice() does.
654 */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000655 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000656 b = PyList_New(selflen);
657 if (!b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000658 return -1;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000659 for (i = 0; i < selflen; i++) {
660 PyObject *o = PyList_GET_ITEM(self, i);
661 Py_INCREF(o);
662 PyList_SET_ITEM(b, i, o);
663 }
664 }
Barry Warsawdedf6d61998-10-09 16:37:25 +0000665
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000666 blen = PyObject_Size(b);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000667
Barry Warsawdedf6d61998-10-09 16:37:25 +0000668 /* resize a using idiom */
669 items = self->ob_item;
670 NRESIZE(items, PyObject*, selflen + blen);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000671 if (items == NULL) {
Barry Warsawdedf6d61998-10-09 16:37:25 +0000672 PyErr_NoMemory();
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000673 Py_DECREF(b);
674 return -1;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000675 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000676
Barry Warsawdedf6d61998-10-09 16:37:25 +0000677 self->ob_item = items;
678
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000679 /* populate the end of self with b's items */
Barry Warsawdedf6d61998-10-09 16:37:25 +0000680 for (i = 0; i < blen; i++) {
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000681 PyObject *o = PySequence_Fast_GET_ITEM(b, i);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000682 Py_INCREF(o);
683 PyList_SET_ITEM(self, self->ob_size++, o);
684 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000685 Py_DECREF(b);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000686 return 0;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000687}
688
689
690static PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000691list_inplace_concat(PyListObject *self, PyObject *other)
692{
Tim Peters1af03e92001-05-26 19:37:54 +0000693 other = PySequence_Fast(other, "argument to += must be iterable");
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000694 if (!other)
695 return NULL;
696
697 if (listextend_internal(self, other) < 0)
698 return NULL;
699
700 Py_INCREF(self);
701 return (PyObject *)self;
702}
703
704static PyObject *
705listextend(PyListObject *self, PyObject *args)
706{
707
708 PyObject *b;
709
710 if (!PyArg_ParseTuple(args, "O:extend", &b))
711 return NULL;
712
Tim Peters1af03e92001-05-26 19:37:54 +0000713 b = PySequence_Fast(b, "list.extend() argument must be iterable");
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000714 if (!b)
715 return NULL;
716
717 if (listextend_internal(self, b) < 0)
718 return NULL;
719
720 Py_INCREF(Py_None);
721 return Py_None;
722}
723
724static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000725listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000726{
727 int i = -1;
728 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000729 if (!PyArg_ParseTuple(args, "|i:pop", &i))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000730 return NULL;
731 if (self->ob_size == 0) {
732 /* Special-case most common failure cause */
733 PyErr_SetString(PyExc_IndexError, "pop from empty list");
734 return NULL;
735 }
736 if (i < 0)
737 i += self->ob_size;
738 if (i < 0 || i >= self->ob_size) {
739 PyErr_SetString(PyExc_IndexError, "pop index out of range");
740 return NULL;
741 }
742 v = self->ob_item[i];
743 Py_INCREF(v);
744 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
745 Py_DECREF(v);
746 return NULL;
747 }
748 return v;
749}
750
Guido van Rossum3f236de1996-12-10 23:55:39 +0000751/* New quicksort implementation for arrays of object pointers.
752 Thanks to discussions with Tim Peters. */
753
754/* CMPERROR is returned by our comparison function when an error
755 occurred. This is the largest negative integer (0x80000000 on a
756 32-bit system). */
Guido van Rossum19700b61997-03-05 00:45:43 +0000757#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
Guido van Rossum3f236de1996-12-10 23:55:39 +0000758
759/* Comparison function. Takes care of calling a user-supplied
760 comparison function (any callable Python object). Calls the
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000761 standard comparison function, PyObject_Compare(), if the user-
762 supplied function is NULL. */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000763
764static int
Fred Drakea2f55112000-07-09 15:16:51 +0000765docompare(PyObject *x, PyObject *y, PyObject *compare)
Guido van Rossum3f236de1996-12-10 23:55:39 +0000766{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000767 PyObject *args, *res;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000768 int i;
769
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000770 if (compare == NULL) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +0000771 /* NOTE: we rely on the fact here that the sorting algorithm
772 only ever checks whether k<0, i.e., whether x<y. So we
773 invoke the rich comparison function with Py_LT ('<'), and
774 return -1 when it returns true and 0 when it returns
775 false. */
776 i = PyObject_RichCompareBool(x, y, Py_LT);
777 if (i < 0)
778 return CMPERROR;
779 else
780 return -i;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000781 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000782
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000783 args = Py_BuildValue("(OO)", x, y);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000784 if (args == NULL)
785 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000786 res = PyEval_CallObject(compare, args);
787 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000788 if (res == NULL)
789 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000790 if (!PyInt_Check(res)) {
791 Py_DECREF(res);
792 PyErr_SetString(PyExc_TypeError,
Guido van Rossum9bcd1d71999-04-19 17:44:39 +0000793 "comparison function must return int");
Guido van Rossum3f236de1996-12-10 23:55:39 +0000794 return CMPERROR;
795 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000796 i = PyInt_AsLong(res);
797 Py_DECREF(res);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000798 if (i < 0)
799 return -1;
800 if (i > 0)
801 return 1;
802 return 0;
803}
804
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000805/* MINSIZE is the smallest array that will get a full-blown samplesort
806 treatment; smaller arrays are sorted using binary insertion. It must
807 be at least 7 for the samplesort implementation to work. Binary
808 insertion does fewer compares, but can suffer O(N**2) data movement.
809 The more expensive compares, the larger MINSIZE should be. */
810#define MINSIZE 100
811
812/* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
813 partition; smaller slices are passed to binarysort. It must be at
814 least 2, and no larger than MINSIZE. Setting it higher reduces the #
815 of compares slowly, but increases the amount of data movement quickly.
816 The value here was chosen assuming a compare costs ~25x more than
817 swapping a pair of memory-resident pointers -- but under that assumption,
818 changing the value by a few dozen more or less has aggregate effect
819 under 1%. So the value is crucial, but not touchy <wink>. */
820#define MINPARTITIONSIZE 40
821
822/* MAXMERGE is the largest number of elements we'll always merge into
823 a known-to-be sorted chunk via binary insertion, regardless of the
824 size of that chunk. Given a chunk of N sorted elements, and a group
825 of K unknowns, the largest K for which it's better to do insertion
826 (than a full-blown sort) is a complicated function of N and K mostly
827 involving the expected number of compares and data moves under each
828 approach, and the relative cost of those operations on a specific
829 architecure. The fixed value here is conservative, and should be a
830 clear win regardless of architecture or N. */
831#define MAXMERGE 15
Guido van Rossum3f236de1996-12-10 23:55:39 +0000832
Guido van Rossum3f236de1996-12-10 23:55:39 +0000833/* STACKSIZE is the size of our work stack. A rough estimate is that
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000834 this allows us to sort arrays of size N where
835 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
836 for arrays of size 2**64. Because we push the biggest partition
837 first, the worst case occurs when all subarrays are always partitioned
838 exactly in two. */
839#define STACKSIZE 60
Guido van Rossum3f236de1996-12-10 23:55:39 +0000840
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000841
842#define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
843
844/* binarysort is the best method for sorting small arrays: it does
845 few compares, but can do data movement quadratic in the number of
846 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000847 [lo, hi) is a contiguous slice of a list, and is sorted via
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000848 binary insertion.
849 On entry, must have lo <= start <= hi, and that [lo, start) is already
850 sorted (pass start == lo if you don't know!).
851 If docompare complains (returns CMPERROR) return -1, else 0.
852 Even in case of error, the output slice will be some permutation of
853 the input (nothing is lost or duplicated).
854*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000855
856static int
Fred Drakea2f55112000-07-09 15:16:51 +0000857binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
858 /* compare -- comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000859{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000860 /* assert lo <= start <= hi
861 assert [lo, start) is sorted */
862 register int k;
863 register PyObject **l, **p, **r;
864 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000865
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000866 if (lo == start)
867 ++start;
868 for (; start < hi; ++start) {
869 /* set l to where *start belongs */
870 l = lo;
871 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000872 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000873 do {
874 p = l + ((r - l) >> 1);
875 SETK(pivot, *p);
876 if (k < 0)
877 r = p;
878 else
879 l = p + 1;
880 } while (l < r);
881 /* Pivot should go at l -- slide over to make room.
882 Caution: using memmove is much slower under MSVC 5;
883 we're not usually moving many slots. */
884 for (p = start; p > l; --p)
885 *p = *(p-1);
886 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000887 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000888 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000889
890 fail:
891 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000892}
893
894/* samplesortslice is the sorting workhorse.
Guido van Rossum42812581998-06-17 14:15:44 +0000895 [lo, hi) is a contiguous slice of a list, to be sorted in place.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000896 On entry, must have lo <= hi,
897 If docompare complains (returns CMPERROR) return -1, else 0.
898 Even in case of error, the output slice will be some permutation of
899 the input (nothing is lost or duplicated).
900
901 samplesort is basically quicksort on steroids: a power of 2 close
902 to n/ln(n) is computed, and that many elements (less 1) are picked at
903 random from the array and sorted. These 2**k - 1 elements are then
904 used as preselected pivots for an equal number of quicksort
905 partitioning steps, partitioning the slice into 2**k chunks each of
906 size about ln(n). These small final chunks are then usually handled
907 by binarysort. Note that when k=1, this is roughly the same as an
908 ordinary quicksort using a random pivot, and when k=2 this is roughly
909 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
910 this a "median of n/ln(n)" quicksort. You can also view it as a kind
911 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
912
913 The large number of samples makes a quadratic-time case almost
914 impossible, and asymptotically drives the average-case number of
915 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
916 3 variant) down to N lg N.
917
918 We also play lots of low-level tricks to cut the number of compares.
919
920 Very obscure: To avoid using extra memory, the PPs are stored in the
921 array and shuffled around as partitioning proceeds. At the start of a
922 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
923 adjacent (either on the left or the right!) to a chunk of X elements
924 that are to be partitioned: P X or X P. In either case we need to
925 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
926 left, followed by the PP to be used for this step (that's the middle
927 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
928 P X or X P -> Psmall pivot X Plarge
929 and the order of the PPs must not be altered. It can take a while
930 to realize this isn't trivial! It can take even longer <wink> to
931 understand why the simple code below works, using only 2**(m-1) swaps.
932 The key is that the order of the X elements isn't necessarily
933 preserved: X can end up as some cyclic permutation of its original
934 order. That's OK, because X is unsorted anyway. If the order of X
935 had to be preserved too, the simplest method I know of using O(1)
936 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
937 Since len(X) is typically several times larger than 2**(m-1), that
938 would slow things down.
939*/
940
941struct SamplesortStackNode {
942 /* Represents a slice of the array, from (& including) lo up
943 to (but excluding) hi. "extra" additional & adjacent elements
944 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
945 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
946 already sorted, but nothing is known about the other elements
947 in [lo, hi). |extra| is always one less than a power of 2.
948 When extra is 0, we're out of PPs, and the slice must be
949 sorted by some other means. */
950 PyObject **lo;
951 PyObject **hi;
952 int extra;
953};
954
955/* The number of PPs we want is 2**k - 1, where 2**k is as close to
Guido van Rossum42812581998-06-17 14:15:44 +0000956 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000957 is undesirable, so cutoff values are canned in the "cutoff" table
958 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
959#define CUTOFFBASE 4
Guido van Rossum3aa23fd1999-01-14 19:01:53 +0000960static long cutoff[] = {
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000961 43, /* smallest N such that k == 4 */
962 106, /* etc */
963 250,
964 576,
965 1298,
966 2885,
967 6339,
968 13805,
969 29843,
970 64116,
971 137030,
972 291554,
973 617916,
974 1305130,
975 2748295,
976 5771662,
977 12091672,
978 25276798,
979 52734615,
980 109820537,
981 228324027,
982 473977813,
983 982548444, /* smallest N such that k == 26 */
984 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
985};
986
987static int
Fred Drakea2f55112000-07-09 15:16:51 +0000988samplesortslice(PyObject **lo, PyObject **hi, PyObject *compare)
989 /* compare -- comparison function object, or NULL for default */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000990{
991 register PyObject **l, **r;
992 register PyObject *tmp, *pivot;
993 register int k;
994 int n, extra, top, extraOnRight;
995 struct SamplesortStackNode stack[STACKSIZE];
996
997 /* assert lo <= hi */
998 n = hi - lo;
999
1000 /* ----------------------------------------------------------
1001 * Special cases
1002 * --------------------------------------------------------*/
1003 if (n < 2)
1004 return 0;
1005
1006 /* Set r to the largest value such that [lo,r) is sorted.
1007 This catches the already-sorted case, the all-the-same
1008 case, and the appended-a-few-elements-to-a-sorted-list case.
1009 If the array is unsorted, we're very likely to get out of
1010 the loop fast, so the test is cheap if it doesn't pay off.
1011 */
1012 /* assert lo < hi */
1013 for (r = lo+1; r < hi; ++r) {
1014 SETK(*r, *(r-1));
1015 if (k < 0)
1016 break;
1017 }
1018 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
1019 few unknowns, or few elements in total. */
1020 if (hi - r <= MAXMERGE || n < MINSIZE)
Guido van Rossum42812581998-06-17 14:15:44 +00001021 return binarysort(lo, hi, r, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001022
1023 /* Check for the array already being reverse-sorted. Typical
1024 benchmark-driven silliness <wink>. */
1025 /* assert lo < hi */
1026 for (r = lo+1; r < hi; ++r) {
1027 SETK(*(r-1), *r);
1028 if (k < 0)
1029 break;
1030 }
1031 if (hi - r <= MAXMERGE) {
1032 /* Reverse the reversed prefix, then insert the tail */
1033 PyObject **originalr = r;
1034 l = lo;
1035 do {
1036 --r;
1037 tmp = *l; *l = *r; *r = tmp;
1038 ++l;
1039 } while (l < r);
Guido van Rossum42812581998-06-17 14:15:44 +00001040 return binarysort(lo, hi, originalr, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001041 }
1042
1043 /* ----------------------------------------------------------
1044 * Normal case setup: a large array without obvious pattern.
1045 * --------------------------------------------------------*/
1046
1047 /* extra := a power of 2 ~= n/ln(n), less 1.
1048 First find the smallest extra s.t. n < cutoff[extra] */
1049 for (extra = 0;
1050 extra < sizeof(cutoff) / sizeof(cutoff[0]);
1051 ++extra) {
1052 if (n < cutoff[extra])
1053 break;
1054 /* note that if we fall out of the loop, the value of
1055 extra still makes *sense*, but may be smaller than
1056 we would like (but the array has more than ~= 2**31
1057 elements in this case!) */
1058 }
1059 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1060 have is CUTOFFBASE-1, so
1061 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1062 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
1063 /* assert extra > 0 and n >= extra */
1064
1065 /* Swap that many values to the start of the array. The
1066 selection of elements is pseudo-random, but the same on
1067 every run (this is intentional! timing algorithm changes is
1068 a pain if timing varies across runs). */
1069 {
1070 unsigned int seed = n / extra; /* arbitrary */
1071 unsigned int i;
1072 for (i = 0; i < (unsigned)extra; ++i) {
1073 /* j := random int in [i, n) */
1074 unsigned int j;
1075 seed = seed * 69069 + 7;
1076 j = i + seed % (n - i);
1077 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
1078 }
1079 }
1080
1081 /* Recursively sort the preselected pivots. */
Guido van Rossum42812581998-06-17 14:15:44 +00001082 if (samplesortslice(lo, lo + extra, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001083 goto fail;
1084
1085 top = 0; /* index of available stack slot */
1086 lo += extra; /* point to first unknown */
1087 extraOnRight = 0; /* the PPs are at the left end */
1088
1089 /* ----------------------------------------------------------
1090 * Partition [lo, hi), and repeat until out of work.
1091 * --------------------------------------------------------*/
1092 for (;;) {
1093 /* assert lo <= hi, so n >= 0 */
1094 n = hi - lo;
1095
1096 /* We may not want, or may not be able, to partition:
1097 If n is small, it's quicker to insert.
1098 If extra is 0, we're out of pivots, and *must* use
1099 another method.
1100 */
1101 if (n < MINPARTITIONSIZE || extra == 0) {
1102 if (n >= MINSIZE) {
1103 /* assert extra == 0
1104 This is rare, since the average size
1105 of a final block is only about
1106 ln(original n). */
Guido van Rossum42812581998-06-17 14:15:44 +00001107 if (samplesortslice(lo, hi, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001108 goto fail;
1109 }
1110 else {
1111 /* Binary insertion should be quicker,
1112 and we can take advantage of the PPs
1113 already being sorted. */
1114 if (extraOnRight && extra) {
1115 /* swap the PPs to the left end */
1116 k = extra;
1117 do {
1118 tmp = *lo;
1119 *lo = *hi;
1120 *hi = tmp;
1121 ++lo; ++hi;
1122 } while (--k);
1123 }
1124 if (binarysort(lo - extra, hi, lo,
Guido van Rossum42812581998-06-17 14:15:44 +00001125 compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001126 goto fail;
1127 }
1128
1129 /* Find another slice to work on. */
1130 if (--top < 0)
1131 break; /* no more -- done! */
1132 lo = stack[top].lo;
1133 hi = stack[top].hi;
1134 extra = stack[top].extra;
1135 extraOnRight = 0;
1136 if (extra < 0) {
1137 extraOnRight = 1;
1138 extra = -extra;
1139 }
1140 continue;
1141 }
1142
1143 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1144 Then our preselected pivot is at (extra-1)/2, and we
1145 want to move the PPs before that to the left end of
1146 the slice, and the PPs after that to the right end.
1147 The following section changes extra, lo, hi, and the
1148 slice such that:
1149 [lo-extra, lo) contains the smaller PPs.
1150 *lo == our PP.
1151 (lo, hi) contains the unknown elements.
1152 [hi, hi+extra) contains the larger PPs.
1153 */
1154 k = extra >>= 1; /* num PPs to move */
1155 if (extraOnRight) {
1156 /* Swap the smaller PPs to the left end.
1157 Note that this loop actually moves k+1 items:
1158 the last is our PP */
1159 do {
1160 tmp = *lo; *lo = *hi; *hi = tmp;
1161 ++lo; ++hi;
1162 } while (k--);
1163 }
1164 else {
1165 /* Swap the larger PPs to the right end. */
1166 while (k--) {
1167 --lo; --hi;
1168 tmp = *lo; *lo = *hi; *hi = tmp;
1169 }
1170 }
1171 --lo; /* *lo is now our PP */
1172 pivot = *lo;
1173
1174 /* Now an almost-ordinary quicksort partition step.
1175 Note that most of the time is spent here!
1176 Only odd thing is that we partition into < and >=,
1177 instead of the usual <= and >=. This helps when
1178 there are lots of duplicates of different values,
1179 because it eventually tends to make subfiles
1180 "pure" (all duplicates), and we special-case for
1181 duplicates later. */
1182 l = lo + 1;
1183 r = hi - 1;
1184 /* assert lo < l < r < hi (small n weeded out above) */
1185
1186 do {
1187 /* slide l right, looking for key >= pivot */
1188 do {
1189 SETK(*l, pivot);
1190 if (k < 0)
1191 ++l;
1192 else
1193 break;
1194 } while (l < r);
1195
1196 /* slide r left, looking for key < pivot */
1197 while (l < r) {
Guido van Rossum42812581998-06-17 14:15:44 +00001198 register PyObject *rval = *r--;
1199 SETK(rval, pivot);
1200 if (k < 0) {
1201 /* swap and advance */
1202 r[1] = *l;
1203 *l++ = rval;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001204 break;
Guido van Rossum42812581998-06-17 14:15:44 +00001205 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001206 }
1207
1208 } while (l < r);
1209
1210 /* assert lo < r <= l < hi
1211 assert r == l or r+1 == l
1212 everything to the left of l is < pivot, and
1213 everything to the right of r is >= pivot */
1214
1215 if (l == r) {
1216 SETK(*r, pivot);
1217 if (k < 0)
1218 ++l;
1219 else
1220 --r;
1221 }
1222 /* assert lo <= r and r+1 == l and l <= hi
1223 assert r == lo or a[r] < pivot
1224 assert a[lo] is pivot
1225 assert l == hi or a[l] >= pivot
1226 Swap the pivot into "the middle", so we can henceforth
1227 ignore it.
1228 */
1229 *lo = *r;
1230 *r = pivot;
1231
1232 /* The following is true now, & will be preserved:
1233 All in [lo,r) are < pivot
1234 All in [r,l) == pivot (& so can be ignored)
1235 All in [l,hi) are >= pivot */
1236
1237 /* Check for duplicates of the pivot. One compare is
1238 wasted if there are no duplicates, but can win big
1239 when there are.
1240 Tricky: we're sticking to "<" compares, so deduce
1241 equality indirectly. We know pivot <= *l, so they're
1242 equal iff not pivot < *l.
1243 */
1244 while (l < hi) {
1245 /* pivot <= *l known */
1246 SETK(pivot, *l);
1247 if (k < 0)
1248 break;
1249 else
1250 /* <= and not < implies == */
1251 ++l;
1252 }
1253
1254 /* assert lo <= r < l <= hi
1255 Partitions are [lo, r) and [l, hi) */
1256
1257 /* push fattest first; remember we still have extra PPs
1258 to the left of the left chunk and to the right of
1259 the right chunk! */
1260 /* assert top < STACKSIZE */
1261 if (r - lo <= hi - l) {
1262 /* second is bigger */
1263 stack[top].lo = l;
1264 stack[top].hi = hi;
1265 stack[top].extra = -extra;
1266 hi = r;
1267 extraOnRight = 0;
1268 }
1269 else {
1270 /* first is bigger */
1271 stack[top].lo = lo;
1272 stack[top].hi = r;
1273 stack[top].extra = extra;
1274 lo = l;
1275 extraOnRight = 1;
1276 }
1277 ++top;
1278
1279 } /* end of partitioning loop */
1280
1281 return 0;
1282
1283 fail:
1284 return -1;
1285}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001286
1287#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001288
1289staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001290
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001291static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001292listsort(PyListObject *self, PyObject *args)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001293{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001294 int err;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001295 PyObject *compare = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001296 PyTypeObject *savetype;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001297
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001298 if (args != NULL) {
Guido van Rossumc00a9382000-02-24 21:48:29 +00001299 if (!PyArg_ParseTuple(args, "|O:sort", &compare))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001300 return NULL;
1301 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001302 savetype = self->ob_type;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001303 self->ob_type = &immutable_list_type;
1304 err = samplesortslice(self->ob_item,
1305 self->ob_item + self->ob_size,
Guido van Rossum42812581998-06-17 14:15:44 +00001306 compare);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001307 self->ob_type = savetype;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001308 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001309 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001310 Py_INCREF(Py_None);
1311 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001312}
1313
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001314int
Fred Drakea2f55112000-07-09 15:16:51 +00001315PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001316{
1317 if (v == NULL || !PyList_Check(v)) {
1318 PyErr_BadInternalCall();
1319 return -1;
1320 }
1321 v = listsort((PyListObject *)v, (PyObject *)NULL);
1322 if (v == NULL)
1323 return -1;
1324 Py_DECREF(v);
1325 return 0;
1326}
1327
Guido van Rossumb86c5492001-02-12 22:06:02 +00001328static void
1329_listreverse(PyListObject *self)
Guido van Rossumed98d481991-03-06 13:07:53 +00001330{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001331 register PyObject **p, **q;
1332 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001333
Guido van Rossumed98d481991-03-06 13:07:53 +00001334 if (self->ob_size > 1) {
1335 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
Guido van Rossumb86c5492001-02-12 22:06:02 +00001336 p < q;
1337 p++, q--)
1338 {
Guido van Rossumed98d481991-03-06 13:07:53 +00001339 tmp = *p;
1340 *p = *q;
1341 *q = tmp;
1342 }
1343 }
Guido van Rossumb86c5492001-02-12 22:06:02 +00001344}
1345
1346static PyObject *
1347listreverse(PyListObject *self, PyObject *args)
1348{
1349 if (!PyArg_ParseTuple(args, ":reverse"))
1350 return NULL;
1351 _listreverse(self);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001352 Py_INCREF(Py_None);
1353 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001354}
1355
Guido van Rossum84c76f51990-10-30 13:32:20 +00001356int
Fred Drakea2f55112000-07-09 15:16:51 +00001357PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001358{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001359 if (v == NULL || !PyList_Check(v)) {
1360 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001361 return -1;
1362 }
Guido van Rossumb86c5492001-02-12 22:06:02 +00001363 _listreverse((PyListObject *)v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001364 return 0;
1365}
1366
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001367PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001368PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001369{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001370 PyObject *w;
1371 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001372 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001373 if (v == NULL || !PyList_Check(v)) {
1374 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001375 return NULL;
1376 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001377 n = ((PyListObject *)v)->ob_size;
1378 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001379 if (w == NULL)
1380 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001381 p = ((PyTupleObject *)w)->ob_item;
Thomas Wouters334fb892000-07-25 12:56:38 +00001382 memcpy((void *)p,
1383 (void *)((PyListObject *)v)->ob_item,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001384 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001385 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001386 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001387 p++;
1388 }
1389 return w;
1390}
1391
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001392static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001393listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00001394{
1395 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001396 PyObject *v;
1397
Tim Peters442914d2001-05-26 05:50:03 +00001398 if (!PyArg_ParseTuple(args, "O:index", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001399 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001400 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001401 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
1402 if (cmp > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001403 return PyInt_FromLong((long)i);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001404 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001405 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001406 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001407 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001408 return NULL;
1409}
1410
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001411static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001412listcount(PyListObject *self, PyObject *args)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001413{
1414 int count = 0;
1415 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001416 PyObject *v;
1417
Tim Peters442914d2001-05-26 05:50:03 +00001418 if (!PyArg_ParseTuple(args, "O:count", &v))
Guido van Rossume6f7d181991-10-20 20:20:40 +00001419 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001420 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001421 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
1422 if (cmp > 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001423 count++;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001424 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001425 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001426 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001427 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001428}
1429
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001430static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001431listremove(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00001432{
1433 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001434 PyObject *v;
1435
Tim Peters442914d2001-05-26 05:50:03 +00001436 if (!PyArg_ParseTuple(args, "O:remove", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001437 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001438 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001439 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
1440 if (cmp > 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001441 if (list_ass_slice(self, i, i+1,
1442 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001443 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001444 Py_INCREF(Py_None);
1445 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001446 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001447 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001448 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001449 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001450 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001451 return NULL;
1452}
1453
Jeremy Hylton8caad492000-06-23 14:18:11 +00001454static int
1455list_traverse(PyListObject *o, visitproc visit, void *arg)
1456{
1457 int i, err;
1458 PyObject *x;
1459
1460 for (i = o->ob_size; --i >= 0; ) {
1461 x = o->ob_item[i];
1462 if (x != NULL) {
1463 err = visit(x, arg);
1464 if (err)
1465 return err;
1466 }
1467 }
1468 return 0;
1469}
1470
1471static int
1472list_clear(PyListObject *lp)
1473{
1474 (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0);
1475 return 0;
1476}
1477
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001478static PyObject *
1479list_richcompare(PyObject *v, PyObject *w, int op)
1480{
1481 PyListObject *vl, *wl;
1482 int i;
1483
1484 if (!PyList_Check(v) || !PyList_Check(w)) {
1485 Py_INCREF(Py_NotImplemented);
1486 return Py_NotImplemented;
1487 }
1488
1489 vl = (PyListObject *)v;
1490 wl = (PyListObject *)w;
1491
1492 if (vl->ob_size != wl->ob_size && (op == Py_EQ || op == Py_NE)) {
1493 /* Shortcut: if the lengths differ, the lists differ */
1494 PyObject *res;
1495 if (op == Py_EQ)
1496 res = Py_False;
1497 else
1498 res = Py_True;
1499 Py_INCREF(res);
1500 return res;
1501 }
1502
1503 /* Search for the first index where items are different */
1504 for (i = 0; i < vl->ob_size && i < wl->ob_size; i++) {
1505 int k = PyObject_RichCompareBool(vl->ob_item[i],
1506 wl->ob_item[i], Py_EQ);
1507 if (k < 0)
1508 return NULL;
1509 if (!k)
1510 break;
1511 }
1512
1513 if (i >= vl->ob_size || i >= wl->ob_size) {
1514 /* No more items to compare -- compare sizes */
1515 int vs = vl->ob_size;
1516 int ws = wl->ob_size;
1517 int cmp;
1518 PyObject *res;
1519 switch (op) {
1520 case Py_LT: cmp = vs < ws; break;
Tim Peters6ee42342001-07-06 17:45:43 +00001521 case Py_LE: cmp = vs <= ws; break;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001522 case Py_EQ: cmp = vs == ws; break;
1523 case Py_NE: cmp = vs != ws; break;
1524 case Py_GT: cmp = vs > ws; break;
1525 case Py_GE: cmp = vs >= ws; break;
1526 default: return NULL; /* cannot happen */
1527 }
1528 if (cmp)
1529 res = Py_True;
1530 else
1531 res = Py_False;
1532 Py_INCREF(res);
1533 return res;
1534 }
1535
1536 /* We have an item that differs -- shortcuts for EQ/NE */
1537 if (op == Py_EQ) {
1538 Py_INCREF(Py_False);
1539 return Py_False;
1540 }
1541 if (op == Py_NE) {
1542 Py_INCREF(Py_True);
1543 return Py_True;
1544 }
1545
1546 /* Compare the final item again using the proper operator */
1547 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
1548}
1549
Tim Peters6d6c1a32001-08-02 04:15:00 +00001550/* Adapted from newer code by Tim */
1551static int
1552list_fill(PyListObject *result, PyObject *v)
1553{
1554 PyObject *it; /* iter(v) */
1555 int n; /* guess for result list size */
1556 int i;
1557
1558 n = result->ob_size;
1559
1560 /* Special-case list(a_list), for speed. */
1561 if (PyList_Check(v)) {
1562 if (v == (PyObject *)result)
1563 return 0; /* source is destination, we're done */
1564 return list_ass_slice(result, 0, n, v);
1565 }
1566
1567 /* Empty previous contents */
1568 if (n != 0) {
1569 if (list_ass_slice(result, 0, n, (PyObject *)NULL) != 0)
1570 return -1;
1571 }
1572
1573 /* Get iterator. There may be some low-level efficiency to be gained
1574 * by caching the tp_iternext slot instead of using PyIter_Next()
1575 * later, but premature optimization is the root etc.
1576 */
1577 it = PyObject_GetIter(v);
1578 if (it == NULL)
1579 return -1;
1580
1581 /* Guess a result list size. */
1582 n = -1; /* unknown */
1583 if (PySequence_Check(v) &&
1584 v->ob_type->tp_as_sequence->sq_length) {
1585 n = PySequence_Size(v);
1586 if (n < 0)
1587 PyErr_Clear();
1588 }
1589 if (n < 0)
1590 n = 8; /* arbitrary */
1591 NRESIZE(result->ob_item, PyObject*, n);
1592 if (result->ob_item == NULL)
1593 goto error;
1594 for (i = 0; i < n; i++)
1595 result->ob_item[i] = NULL;
1596 result->ob_size = n;
1597
1598 /* Run iterator to exhaustion. */
1599 for (i = 0; ; i++) {
1600 PyObject *item = PyIter_Next(it);
1601 if (item == NULL) {
1602 if (PyErr_Occurred())
1603 goto error;
1604 break;
1605 }
1606 if (i < n)
1607 PyList_SET_ITEM(result, i, item); /* steals ref */
1608 else {
1609 int status = ins1(result, result->ob_size, item);
1610 Py_DECREF(item); /* append creates a new ref */
1611 if (status < 0)
1612 goto error;
1613 }
1614 }
1615
1616 /* Cut back result list if initial guess was too large. */
1617 if (i < n && result != NULL) {
1618 if (list_ass_slice(result, i, n, (PyObject *)NULL) != 0)
1619 goto error;
1620 }
1621 Py_DECREF(it);
1622 return 0;
1623
1624 error:
1625 Py_DECREF(it);
1626 return -1;
1627}
1628
1629static int
1630list_init(PyListObject *self, PyObject *args, PyObject *kw)
1631{
1632 PyObject *arg = NULL;
1633 static char *kwlist[] = {"sequence", 0};
1634
1635 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
1636 return -1;
1637 if (arg != NULL)
1638 return list_fill(self, arg);
1639 if (self->ob_size > 0)
1640 return list_ass_slice(self, 0, self->ob_size, (PyObject*)NULL);
1641 return 0;
1642}
1643
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001644static char append_doc[] =
1645"L.append(object) -- append object to end";
Barry Warsawdedf6d61998-10-09 16:37:25 +00001646static char extend_doc[] =
1647"L.extend(list) -- extend list by appending list elements";
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001648static char insert_doc[] =
1649"L.insert(index, object) -- insert object before index";
1650static char pop_doc[] =
1651"L.pop([index]) -> item -- remove and return item at index (default last)";
1652static char remove_doc[] =
1653"L.remove(value) -- remove first occurrence of value";
1654static char index_doc[] =
1655"L.index(value) -> integer -- return index of first occurrence of value";
1656static char count_doc[] =
1657"L.count(value) -> integer -- return number of occurrences of value";
1658static char reverse_doc[] =
1659"L.reverse() -- reverse *IN PLACE*";
1660static char sort_doc[] =
1661"L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1662
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001663static PyMethodDef list_methods[] = {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001664 {"append", (PyCFunction)listappend, METH_VARARGS, append_doc},
1665 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
1666 {"extend", (PyCFunction)listextend, METH_VARARGS, extend_doc},
1667 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
1668 {"remove", (PyCFunction)listremove, METH_VARARGS, remove_doc},
1669 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
1670 {"count", (PyCFunction)listcount, METH_VARARGS, count_doc},
Tim Peters0e76ab22000-12-13 22:35:46 +00001671 {"reverse", (PyCFunction)listreverse, METH_VARARGS, reverse_doc},
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001672 {"sort", (PyCFunction)listsort, METH_VARARGS, sort_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001673 {NULL, NULL} /* sentinel */
1674};
1675
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001676static PySequenceMethods list_as_sequence = {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001677 (inquiry)list_length, /* sq_length */
1678 (binaryfunc)list_concat, /* sq_concat */
1679 (intargfunc)list_repeat, /* sq_repeat */
1680 (intargfunc)list_item, /* sq_item */
1681 (intintargfunc)list_slice, /* sq_slice */
1682 (intobjargproc)list_ass_item, /* sq_ass_item */
1683 (intintobjargproc)list_ass_slice, /* sq_ass_slice */
1684 (objobjproc)list_contains, /* sq_contains */
1685 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
1686 (intargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001687};
1688
Tim Peters6d6c1a32001-08-02 04:15:00 +00001689static char list_doc[] =
1690"list() -> new list\n"
1691"list(sequence) -> new list initialized from sequence's items";
1692
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001693PyTypeObject PyList_Type = {
1694 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001695 0,
1696 "list",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001697 sizeof(PyListObject) + PyGC_HEAD_SIZE,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001698 0,
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001699 (destructor)list_dealloc, /* tp_dealloc */
1700 (printfunc)list_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001701 0, /* tp_getattr */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001702 0, /* tp_setattr */
1703 0, /* tp_compare */
1704 (reprfunc)list_repr, /* tp_repr */
1705 0, /* tp_as_number */
1706 &list_as_sequence, /* tp_as_sequence */
1707 0, /* tp_as_mapping */
1708 0, /* tp_hash */
1709 0, /* tp_call */
1710 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001711 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001712 0, /* tp_setattro */
1713 0, /* tp_as_buffer */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001714 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC |
1715 Py_TPFLAGS_BASETYPE, /* tp_flags */
1716 list_doc, /* tp_doc */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001717 (traverseproc)list_traverse, /* tp_traverse */
1718 (inquiry)list_clear, /* tp_clear */
1719 list_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001720 0, /* tp_weaklistoffset */
1721 0, /* tp_iter */
1722 0, /* tp_iternext */
1723 list_methods, /* tp_methods */
1724 0, /* tp_members */
1725 0, /* tp_getset */
1726 0, /* tp_base */
1727 0, /* tp_dict */
1728 0, /* tp_descr_get */
1729 0, /* tp_descr_set */
1730 0, /* tp_dictoffset */
1731 (initproc)list_init, /* tp_init */
1732 PyType_GenericAlloc, /* tp_alloc */
1733 PyType_GenericNew, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001734};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001735
1736
1737/* During a sort, we really can't have anyone modifying the list; it could
1738 cause core dumps. Thus, we substitute a dummy type that raises an
1739 explanatory exception when a modifying operation is used. Caveat:
1740 comparisons may behave differently; but I guess it's a bad idea anyway to
1741 compare a list that's being sorted... */
1742
1743static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001744immutable_list_op(void)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001745{
1746 PyErr_SetString(PyExc_TypeError,
1747 "a list cannot be modified while it is being sorted");
1748 return NULL;
1749}
1750
1751static PyMethodDef immutable_list_methods[] = {
1752 {"append", (PyCFunction)immutable_list_op},
1753 {"insert", (PyCFunction)immutable_list_op},
1754 {"remove", (PyCFunction)immutable_list_op},
1755 {"index", (PyCFunction)listindex},
1756 {"count", (PyCFunction)listcount},
1757 {"reverse", (PyCFunction)immutable_list_op},
1758 {"sort", (PyCFunction)immutable_list_op},
1759 {NULL, NULL} /* sentinel */
1760};
1761
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001762static int
Fred Drakea2f55112000-07-09 15:16:51 +00001763immutable_list_ass(void)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001764{
1765 immutable_list_op();
1766 return -1;
1767}
1768
1769static PySequenceMethods immutable_list_as_sequence = {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001770 (inquiry)list_length, /* sq_length */
1771 (binaryfunc)list_concat, /* sq_concat */
1772 (intargfunc)list_repeat, /* sq_repeat */
1773 (intargfunc)list_item, /* sq_item */
1774 (intintargfunc)list_slice, /* sq_slice */
1775 (intobjargproc)immutable_list_ass, /* sq_ass_item */
1776 (intintobjargproc)immutable_list_ass, /* sq_ass_slice */
1777 (objobjproc)list_contains, /* sq_contains */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001778};
1779
1780static PyTypeObject immutable_list_type = {
1781 PyObject_HEAD_INIT(&PyType_Type)
1782 0,
1783 "list (immutable, during sort)",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001784 sizeof(PyListObject) + PyGC_HEAD_SIZE,
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001785 0,
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001786 0, /* Cannot happen */ /* tp_dealloc */
1787 (printfunc)list_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001788 0, /* tp_getattr */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001789 0, /* tp_setattr */
1790 0, /* Won't be called */ /* tp_compare */
1791 (reprfunc)list_repr, /* tp_repr */
1792 0, /* tp_as_number */
1793 &immutable_list_as_sequence, /* tp_as_sequence */
1794 0, /* tp_as_mapping */
1795 0, /* tp_hash */
1796 0, /* tp_call */
1797 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001798 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001799 0, /* tp_setattro */
1800 0, /* tp_as_buffer */
1801 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001802 list_doc, /* tp_doc */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001803 (traverseproc)list_traverse, /* tp_traverse */
1804 0, /* tp_clear */
1805 list_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001806 0, /* tp_weaklistoffset */
1807 0, /* tp_iter */
1808 0, /* tp_iternext */
1809 immutable_list_methods, /* tp_methods */
1810 0, /* tp_members */
1811 0, /* tp_getset */
1812 0, /* tp_base */
1813 0, /* tp_dict */
1814 0, /* tp_descr_get */
1815 0, /* tp_descr_set */
1816 0, /* tp_init */
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001817 /* NOTE: This is *not* the standard list_type struct! */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001818};