blob: 9fb3e8250b5ea088b57e9819f4a25dfe0e21d126 [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 Rossumc0b618a1997-05-02 03:12:38 +0000249 PyObject *s, *comma;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000251
252 i = Py_ReprEnter((PyObject*)v);
253 if (i != 0) {
254 if (i > 0)
255 return PyString_FromString("[...]");
256 return NULL;
257 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000258 s = PyString_FromString("[");
259 comma = PyString_FromString(", ");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260 for (i = 0; i < v->ob_size && s != NULL; i++) {
261 if (i > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000262 PyString_Concat(&s, comma);
263 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000265 Py_XDECREF(comma);
266 PyString_ConcatAndDel(&s, PyString_FromString("]"));
Guido van Rossumfb376de1998-04-10 22:47:27 +0000267 Py_ReprLeave((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268 return s;
269}
270
271static int
Fred Drakea2f55112000-07-09 15:16:51 +0000272list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273{
274 return a->ob_size;
275}
276
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000277
278
279static int
Fred Drakea2f55112000-07-09 15:16:51 +0000280list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000281{
Guido van Rossum65e1cea2001-01-17 22:11:59 +0000282 int i;
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000283
284 for (i = 0; i < a->ob_size; ++i) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +0000285 int cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
286 Py_EQ);
287 if (cmp > 0)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000288 return 1;
Guido van Rossum65e1cea2001-01-17 22:11:59 +0000289 else if (cmp < 0)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000290 return -1;
291 }
292 return 0;
293}
294
295
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000297list_item(PyListObject *a, int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298{
299 if (i < 0 || i >= a->ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000300 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000301 indexerr = PyString_FromString(
302 "list index out of range");
303 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304 return NULL;
305 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000306 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307 return a->ob_item[i];
308}
309
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000310static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000311list_slice(PyListObject *a, int ilow, int ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000312{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000313 PyListObject *np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314 int i;
315 if (ilow < 0)
316 ilow = 0;
317 else if (ilow > a->ob_size)
318 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319 if (ihigh < ilow)
320 ihigh = ilow;
321 else if (ihigh > a->ob_size)
322 ihigh = a->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000323 np = (PyListObject *) PyList_New(ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324 if (np == NULL)
325 return NULL;
326 for (i = ilow; i < ihigh; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000327 PyObject *v = a->ob_item[i];
328 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329 np->ob_item[i - ilow] = v;
330 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332}
333
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000334PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000335PyList_GetSlice(PyObject *a, int ilow, int ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000336{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000337 if (!PyList_Check(a)) {
338 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000339 return NULL;
340 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000342}
343
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000344static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000345list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346{
347 int size;
348 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000349 PyListObject *np;
350 if (!PyList_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000351 PyErr_Format(PyExc_TypeError,
Fred Drake914a2ed2000-06-01 14:31:03 +0000352 "can only concatenate list (not \"%.200s\") to list",
353 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354 return NULL;
355 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000356#define b ((PyListObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357 size = a->ob_size + b->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000358 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000360 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361 }
362 for (i = 0; i < a->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000363 PyObject *v = a->ob_item[i];
364 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365 np->ob_item[i] = v;
366 }
367 for (i = 0; i < b->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000368 PyObject *v = b->ob_item[i];
369 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370 np->ob_item[i + a->ob_size] = v;
371 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000372 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373#undef b
374}
375
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000376static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000377list_repeat(PyListObject *a, int n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000378{
379 int i, j;
380 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000381 PyListObject *np;
382 PyObject **p;
Guido van Rossumed98d481991-03-06 13:07:53 +0000383 if (n < 0)
384 n = 0;
385 size = a->ob_size * n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000387 if (np == NULL)
388 return NULL;
389 p = np->ob_item;
390 for (i = 0; i < n; i++) {
391 for (j = 0; j < a->ob_size; j++) {
392 *p = a->ob_item[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000394 p++;
395 }
396 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000398}
399
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400static int
Fred Drakea2f55112000-07-09 15:16:51 +0000401list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000403 /* Because [X]DECREF can recursively invoke list operations on
404 this list, we must postpone all [X]DECREF activity until
405 after the list is back in its canonical shape. Therefore
406 we must allocate an additional array, 'recycle', into which
407 we temporarily copy the items that are deleted from the
408 list. :-( */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000409 PyObject **recycle, **p;
410 PyObject **item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411 int n; /* Size of replacement list */
412 int d; /* Change in size */
413 int k; /* Loop index */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000414#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415 if (v == NULL)
416 n = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000417 else if (PyList_Check(v)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000418 n = b->ob_size;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000419 if (a == b) {
420 /* Special case "a[i:j] = a" -- copy b first */
421 int ret;
422 v = list_slice(b, 0, n);
423 ret = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000424 Py_DECREF(v);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000425 return ret;
426 }
427 }
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000428 else {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000429 PyErr_Format(PyExc_TypeError,
430 "must assign list (not \"%.200s\") to slice",
431 v->ob_type->tp_name);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000432 return -1;
433 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434 if (ilow < 0)
435 ilow = 0;
436 else if (ilow > a->ob_size)
437 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438 if (ihigh < ilow)
439 ihigh = ilow;
440 else if (ihigh > a->ob_size)
441 ihigh = a->ob_size;
442 item = a->ob_item;
443 d = n - (ihigh-ilow);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000444 if (ihigh > ilow)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445 p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000446 else
447 p = recycle = NULL;
448 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449 for (k = ilow; k < ihigh; k++)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000450 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451 if (d < 0) {
452 for (/*k = ihigh*/; k < a->ob_size; k++)
453 item[k+d] = item[k];
454 a->ob_size += d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456 a->ob_item = item;
457 }
458 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000459 else { /* Insert d items; recycle ihigh-ilow items */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000460 NRESIZE(item, PyObject *, a->ob_size + d);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000461 if (item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000462 if (recycle != NULL)
463 PyMem_DEL(recycle);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000465 return -1;
466 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000467 for (k = a->ob_size; --k >= ihigh; )
468 item[k+d] = item[k];
469 for (/*k = ihigh-1*/; k >= ilow; --k)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000470 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471 a->ob_item = item;
472 a->ob_size += d;
473 }
474 for (k = 0; k < n; k++, ilow++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475 PyObject *w = b->ob_item[k];
476 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477 item[ilow] = w;
478 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000479 if (recycle) {
480 while (--p >= recycle)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000481 Py_XDECREF(*p);
482 PyMem_DEL(recycle);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000483 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484 return 0;
485#undef b
486}
487
Guido van Rossum234f9421993-06-17 12:35:49 +0000488int
Fred Drakea2f55112000-07-09 15:16:51 +0000489PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000490{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000491 if (!PyList_Check(a)) {
492 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000493 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000494 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000495 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000496}
497
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000498static PyObject *
499list_inplace_repeat(PyListObject *self, int n)
500{
501 PyObject **items;
502 int size, i, j;
503
504
505 size = PyList_GET_SIZE(self);
506 if (size == 0) {
507 Py_INCREF(self);
508 return (PyObject *)self;
509 }
510
511 items = self->ob_item;
512
513 if (n < 1) {
514 self->ob_item = NULL;
515 self->ob_size = 0;
516 for (i = 0; i < size; i++)
517 Py_XDECREF(items[i]);
518 PyMem_DEL(items);
519 Py_INCREF(self);
520 return (PyObject *)self;
521 }
522
523 NRESIZE(items, PyObject*, size*n);
524 if (items == NULL) {
525 PyErr_NoMemory();
526 goto finally;
527 }
528 self->ob_item = items;
529 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
530 for (j = 0; j < size; j++) {
531 PyObject *o = PyList_GET_ITEM(self, j);
532 Py_INCREF(o);
533 PyList_SET_ITEM(self, self->ob_size++, o);
534 }
535 }
536 Py_INCREF(self);
537 return (PyObject *)self;
538 finally:
539 return NULL;
540}
541
Guido van Rossum4a450d01991-04-03 19:05:18 +0000542static int
Fred Drakea2f55112000-07-09 15:16:51 +0000543list_ass_item(PyListObject *a, int i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000544{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000545 PyObject *old_value;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000546 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547 PyErr_SetString(PyExc_IndexError,
548 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000549 return -1;
550 }
551 if (v == NULL)
552 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000553 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000554 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000555 a->ob_item[i] = v;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000556 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000557 return 0;
558}
559
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000561ins(PyListObject *self, int where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000562{
563 if (ins1(self, where, v) != 0)
564 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000565 Py_INCREF(Py_None);
566 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000567}
568
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000570listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571{
572 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000573 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000574 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000575 return NULL;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000576 return ins(self, i, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577}
578
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000580listappend(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582 PyObject *v;
Tim Peters442914d2001-05-26 05:50:03 +0000583 if (!PyArg_ParseTuple(args, "O:append", &v))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000584 return NULL;
585 return ins(self, (int) self->ob_size, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000586}
587
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000588static int
589listextend_internal(PyListObject *self, PyObject *b)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000590{
Barry Warsawdedf6d61998-10-09 16:37:25 +0000591 PyObject **items;
592 int selflen = PyList_GET_SIZE(self);
593 int blen;
594 register int i;
595
Jeremy Hyltondb60bb52001-01-03 22:32:16 +0000596 if (PyObject_Size(b) == 0) {
Barry Warsawdedf6d61998-10-09 16:37:25 +0000597 /* short circuit when b is empty */
Jeremy Hyltondb60bb52001-01-03 22:32:16 +0000598 Py_DECREF(b);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000599 return 0;
Jeremy Hyltondb60bb52001-01-03 22:32:16 +0000600 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000601
Barry Warsawdedf6d61998-10-09 16:37:25 +0000602 if (self == (PyListObject*)b) {
603 /* as in list_ass_slice() we must special case the
604 * situation: a.extend(a)
605 *
606 * XXX: I think this way ought to be faster than using
607 * list_slice() the way list_ass_slice() does.
608 */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000609 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000610 b = PyList_New(selflen);
611 if (!b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000612 return -1;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000613 for (i = 0; i < selflen; i++) {
614 PyObject *o = PyList_GET_ITEM(self, i);
615 Py_INCREF(o);
616 PyList_SET_ITEM(b, i, o);
617 }
618 }
Barry Warsawdedf6d61998-10-09 16:37:25 +0000619
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000620 blen = PyObject_Size(b);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000621
Barry Warsawdedf6d61998-10-09 16:37:25 +0000622 /* resize a using idiom */
623 items = self->ob_item;
624 NRESIZE(items, PyObject*, selflen + blen);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000625 if (items == NULL) {
Barry Warsawdedf6d61998-10-09 16:37:25 +0000626 PyErr_NoMemory();
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000627 Py_DECREF(b);
628 return -1;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000629 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000630
Barry Warsawdedf6d61998-10-09 16:37:25 +0000631 self->ob_item = items;
632
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000633 /* populate the end of self with b's items */
Barry Warsawdedf6d61998-10-09 16:37:25 +0000634 for (i = 0; i < blen; i++) {
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000635 PyObject *o = PySequence_Fast_GET_ITEM(b, i);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000636 Py_INCREF(o);
637 PyList_SET_ITEM(self, self->ob_size++, o);
638 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000639 Py_DECREF(b);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000640 return 0;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000641}
642
643
644static PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000645list_inplace_concat(PyListObject *self, PyObject *other)
646{
647 other = PySequence_Fast(other, "argument to += must be a sequence");
648 if (!other)
649 return NULL;
650
651 if (listextend_internal(self, other) < 0)
652 return NULL;
653
654 Py_INCREF(self);
655 return (PyObject *)self;
656}
657
658static PyObject *
659listextend(PyListObject *self, PyObject *args)
660{
661
662 PyObject *b;
663
664 if (!PyArg_ParseTuple(args, "O:extend", &b))
665 return NULL;
666
667 b = PySequence_Fast(b, "list.extend() argument must be a sequence");
668 if (!b)
669 return NULL;
670
671 if (listextend_internal(self, b) < 0)
672 return NULL;
673
674 Py_INCREF(Py_None);
675 return Py_None;
676}
677
678static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000679listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000680{
681 int i = -1;
682 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000683 if (!PyArg_ParseTuple(args, "|i:pop", &i))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000684 return NULL;
685 if (self->ob_size == 0) {
686 /* Special-case most common failure cause */
687 PyErr_SetString(PyExc_IndexError, "pop from empty list");
688 return NULL;
689 }
690 if (i < 0)
691 i += self->ob_size;
692 if (i < 0 || i >= self->ob_size) {
693 PyErr_SetString(PyExc_IndexError, "pop index out of range");
694 return NULL;
695 }
696 v = self->ob_item[i];
697 Py_INCREF(v);
698 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
699 Py_DECREF(v);
700 return NULL;
701 }
702 return v;
703}
704
Guido van Rossum3f236de1996-12-10 23:55:39 +0000705/* New quicksort implementation for arrays of object pointers.
706 Thanks to discussions with Tim Peters. */
707
708/* CMPERROR is returned by our comparison function when an error
709 occurred. This is the largest negative integer (0x80000000 on a
710 32-bit system). */
Guido van Rossum19700b61997-03-05 00:45:43 +0000711#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
Guido van Rossum3f236de1996-12-10 23:55:39 +0000712
713/* Comparison function. Takes care of calling a user-supplied
714 comparison function (any callable Python object). Calls the
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000715 standard comparison function, PyObject_Compare(), if the user-
716 supplied function is NULL. */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000717
718static int
Fred Drakea2f55112000-07-09 15:16:51 +0000719docompare(PyObject *x, PyObject *y, PyObject *compare)
Guido van Rossum3f236de1996-12-10 23:55:39 +0000720{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000721 PyObject *args, *res;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000722 int i;
723
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000724 if (compare == NULL) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +0000725 /* NOTE: we rely on the fact here that the sorting algorithm
726 only ever checks whether k<0, i.e., whether x<y. So we
727 invoke the rich comparison function with Py_LT ('<'), and
728 return -1 when it returns true and 0 when it returns
729 false. */
730 i = PyObject_RichCompareBool(x, y, Py_LT);
731 if (i < 0)
732 return CMPERROR;
733 else
734 return -i;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000735 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000736
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000737 args = Py_BuildValue("(OO)", x, y);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000738 if (args == NULL)
739 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000740 res = PyEval_CallObject(compare, args);
741 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000742 if (res == NULL)
743 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744 if (!PyInt_Check(res)) {
745 Py_DECREF(res);
746 PyErr_SetString(PyExc_TypeError,
Guido van Rossum9bcd1d71999-04-19 17:44:39 +0000747 "comparison function must return int");
Guido van Rossum3f236de1996-12-10 23:55:39 +0000748 return CMPERROR;
749 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750 i = PyInt_AsLong(res);
751 Py_DECREF(res);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000752 if (i < 0)
753 return -1;
754 if (i > 0)
755 return 1;
756 return 0;
757}
758
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000759/* MINSIZE is the smallest array that will get a full-blown samplesort
760 treatment; smaller arrays are sorted using binary insertion. It must
761 be at least 7 for the samplesort implementation to work. Binary
762 insertion does fewer compares, but can suffer O(N**2) data movement.
763 The more expensive compares, the larger MINSIZE should be. */
764#define MINSIZE 100
765
766/* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
767 partition; smaller slices are passed to binarysort. It must be at
768 least 2, and no larger than MINSIZE. Setting it higher reduces the #
769 of compares slowly, but increases the amount of data movement quickly.
770 The value here was chosen assuming a compare costs ~25x more than
771 swapping a pair of memory-resident pointers -- but under that assumption,
772 changing the value by a few dozen more or less has aggregate effect
773 under 1%. So the value is crucial, but not touchy <wink>. */
774#define MINPARTITIONSIZE 40
775
776/* MAXMERGE is the largest number of elements we'll always merge into
777 a known-to-be sorted chunk via binary insertion, regardless of the
778 size of that chunk. Given a chunk of N sorted elements, and a group
779 of K unknowns, the largest K for which it's better to do insertion
780 (than a full-blown sort) is a complicated function of N and K mostly
781 involving the expected number of compares and data moves under each
782 approach, and the relative cost of those operations on a specific
783 architecure. The fixed value here is conservative, and should be a
784 clear win regardless of architecture or N. */
785#define MAXMERGE 15
Guido van Rossum3f236de1996-12-10 23:55:39 +0000786
Guido van Rossum3f236de1996-12-10 23:55:39 +0000787/* STACKSIZE is the size of our work stack. A rough estimate is that
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000788 this allows us to sort arrays of size N where
789 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
790 for arrays of size 2**64. Because we push the biggest partition
791 first, the worst case occurs when all subarrays are always partitioned
792 exactly in two. */
793#define STACKSIZE 60
Guido van Rossum3f236de1996-12-10 23:55:39 +0000794
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000795
796#define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
797
798/* binarysort is the best method for sorting small arrays: it does
799 few compares, but can do data movement quadratic in the number of
800 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000801 [lo, hi) is a contiguous slice of a list, and is sorted via
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000802 binary insertion.
803 On entry, must have lo <= start <= hi, and that [lo, start) is already
804 sorted (pass start == lo if you don't know!).
805 If docompare complains (returns CMPERROR) return -1, else 0.
806 Even in case of error, the output slice will be some permutation of
807 the input (nothing is lost or duplicated).
808*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000809
810static int
Fred Drakea2f55112000-07-09 15:16:51 +0000811binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
812 /* compare -- comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000813{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000814 /* assert lo <= start <= hi
815 assert [lo, start) is sorted */
816 register int k;
817 register PyObject **l, **p, **r;
818 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000819
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000820 if (lo == start)
821 ++start;
822 for (; start < hi; ++start) {
823 /* set l to where *start belongs */
824 l = lo;
825 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000826 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000827 do {
828 p = l + ((r - l) >> 1);
829 SETK(pivot, *p);
830 if (k < 0)
831 r = p;
832 else
833 l = p + 1;
834 } while (l < r);
835 /* Pivot should go at l -- slide over to make room.
836 Caution: using memmove is much slower under MSVC 5;
837 we're not usually moving many slots. */
838 for (p = start; p > l; --p)
839 *p = *(p-1);
840 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000841 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000842 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000843
844 fail:
845 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000846}
847
848/* samplesortslice is the sorting workhorse.
Guido van Rossum42812581998-06-17 14:15:44 +0000849 [lo, hi) is a contiguous slice of a list, to be sorted in place.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000850 On entry, must have lo <= hi,
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
855 samplesort is basically quicksort on steroids: a power of 2 close
856 to n/ln(n) is computed, and that many elements (less 1) are picked at
857 random from the array and sorted. These 2**k - 1 elements are then
858 used as preselected pivots for an equal number of quicksort
859 partitioning steps, partitioning the slice into 2**k chunks each of
860 size about ln(n). These small final chunks are then usually handled
861 by binarysort. Note that when k=1, this is roughly the same as an
862 ordinary quicksort using a random pivot, and when k=2 this is roughly
863 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
864 this a "median of n/ln(n)" quicksort. You can also view it as a kind
865 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
866
867 The large number of samples makes a quadratic-time case almost
868 impossible, and asymptotically drives the average-case number of
869 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
870 3 variant) down to N lg N.
871
872 We also play lots of low-level tricks to cut the number of compares.
873
874 Very obscure: To avoid using extra memory, the PPs are stored in the
875 array and shuffled around as partitioning proceeds. At the start of a
876 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
877 adjacent (either on the left or the right!) to a chunk of X elements
878 that are to be partitioned: P X or X P. In either case we need to
879 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
880 left, followed by the PP to be used for this step (that's the middle
881 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
882 P X or X P -> Psmall pivot X Plarge
883 and the order of the PPs must not be altered. It can take a while
884 to realize this isn't trivial! It can take even longer <wink> to
885 understand why the simple code below works, using only 2**(m-1) swaps.
886 The key is that the order of the X elements isn't necessarily
887 preserved: X can end up as some cyclic permutation of its original
888 order. That's OK, because X is unsorted anyway. If the order of X
889 had to be preserved too, the simplest method I know of using O(1)
890 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
891 Since len(X) is typically several times larger than 2**(m-1), that
892 would slow things down.
893*/
894
895struct SamplesortStackNode {
896 /* Represents a slice of the array, from (& including) lo up
897 to (but excluding) hi. "extra" additional & adjacent elements
898 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
899 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
900 already sorted, but nothing is known about the other elements
901 in [lo, hi). |extra| is always one less than a power of 2.
902 When extra is 0, we're out of PPs, and the slice must be
903 sorted by some other means. */
904 PyObject **lo;
905 PyObject **hi;
906 int extra;
907};
908
909/* 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 +0000910 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000911 is undesirable, so cutoff values are canned in the "cutoff" table
912 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
913#define CUTOFFBASE 4
Guido van Rossum3aa23fd1999-01-14 19:01:53 +0000914static long cutoff[] = {
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000915 43, /* smallest N such that k == 4 */
916 106, /* etc */
917 250,
918 576,
919 1298,
920 2885,
921 6339,
922 13805,
923 29843,
924 64116,
925 137030,
926 291554,
927 617916,
928 1305130,
929 2748295,
930 5771662,
931 12091672,
932 25276798,
933 52734615,
934 109820537,
935 228324027,
936 473977813,
937 982548444, /* smallest N such that k == 26 */
938 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
939};
940
941static int
Fred Drakea2f55112000-07-09 15:16:51 +0000942samplesortslice(PyObject **lo, PyObject **hi, PyObject *compare)
943 /* compare -- comparison function object, or NULL for default */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000944{
945 register PyObject **l, **r;
946 register PyObject *tmp, *pivot;
947 register int k;
948 int n, extra, top, extraOnRight;
949 struct SamplesortStackNode stack[STACKSIZE];
950
951 /* assert lo <= hi */
952 n = hi - lo;
953
954 /* ----------------------------------------------------------
955 * Special cases
956 * --------------------------------------------------------*/
957 if (n < 2)
958 return 0;
959
960 /* Set r to the largest value such that [lo,r) is sorted.
961 This catches the already-sorted case, the all-the-same
962 case, and the appended-a-few-elements-to-a-sorted-list case.
963 If the array is unsorted, we're very likely to get out of
964 the loop fast, so the test is cheap if it doesn't pay off.
965 */
966 /* assert lo < hi */
967 for (r = lo+1; r < hi; ++r) {
968 SETK(*r, *(r-1));
969 if (k < 0)
970 break;
971 }
972 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
973 few unknowns, or few elements in total. */
974 if (hi - r <= MAXMERGE || n < MINSIZE)
Guido van Rossum42812581998-06-17 14:15:44 +0000975 return binarysort(lo, hi, r, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000976
977 /* Check for the array already being reverse-sorted. Typical
978 benchmark-driven silliness <wink>. */
979 /* assert lo < hi */
980 for (r = lo+1; r < hi; ++r) {
981 SETK(*(r-1), *r);
982 if (k < 0)
983 break;
984 }
985 if (hi - r <= MAXMERGE) {
986 /* Reverse the reversed prefix, then insert the tail */
987 PyObject **originalr = r;
988 l = lo;
989 do {
990 --r;
991 tmp = *l; *l = *r; *r = tmp;
992 ++l;
993 } while (l < r);
Guido van Rossum42812581998-06-17 14:15:44 +0000994 return binarysort(lo, hi, originalr, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000995 }
996
997 /* ----------------------------------------------------------
998 * Normal case setup: a large array without obvious pattern.
999 * --------------------------------------------------------*/
1000
1001 /* extra := a power of 2 ~= n/ln(n), less 1.
1002 First find the smallest extra s.t. n < cutoff[extra] */
1003 for (extra = 0;
1004 extra < sizeof(cutoff) / sizeof(cutoff[0]);
1005 ++extra) {
1006 if (n < cutoff[extra])
1007 break;
1008 /* note that if we fall out of the loop, the value of
1009 extra still makes *sense*, but may be smaller than
1010 we would like (but the array has more than ~= 2**31
1011 elements in this case!) */
1012 }
1013 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1014 have is CUTOFFBASE-1, so
1015 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1016 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
1017 /* assert extra > 0 and n >= extra */
1018
1019 /* Swap that many values to the start of the array. The
1020 selection of elements is pseudo-random, but the same on
1021 every run (this is intentional! timing algorithm changes is
1022 a pain if timing varies across runs). */
1023 {
1024 unsigned int seed = n / extra; /* arbitrary */
1025 unsigned int i;
1026 for (i = 0; i < (unsigned)extra; ++i) {
1027 /* j := random int in [i, n) */
1028 unsigned int j;
1029 seed = seed * 69069 + 7;
1030 j = i + seed % (n - i);
1031 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
1032 }
1033 }
1034
1035 /* Recursively sort the preselected pivots. */
Guido van Rossum42812581998-06-17 14:15:44 +00001036 if (samplesortslice(lo, lo + extra, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001037 goto fail;
1038
1039 top = 0; /* index of available stack slot */
1040 lo += extra; /* point to first unknown */
1041 extraOnRight = 0; /* the PPs are at the left end */
1042
1043 /* ----------------------------------------------------------
1044 * Partition [lo, hi), and repeat until out of work.
1045 * --------------------------------------------------------*/
1046 for (;;) {
1047 /* assert lo <= hi, so n >= 0 */
1048 n = hi - lo;
1049
1050 /* We may not want, or may not be able, to partition:
1051 If n is small, it's quicker to insert.
1052 If extra is 0, we're out of pivots, and *must* use
1053 another method.
1054 */
1055 if (n < MINPARTITIONSIZE || extra == 0) {
1056 if (n >= MINSIZE) {
1057 /* assert extra == 0
1058 This is rare, since the average size
1059 of a final block is only about
1060 ln(original n). */
Guido van Rossum42812581998-06-17 14:15:44 +00001061 if (samplesortslice(lo, hi, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001062 goto fail;
1063 }
1064 else {
1065 /* Binary insertion should be quicker,
1066 and we can take advantage of the PPs
1067 already being sorted. */
1068 if (extraOnRight && extra) {
1069 /* swap the PPs to the left end */
1070 k = extra;
1071 do {
1072 tmp = *lo;
1073 *lo = *hi;
1074 *hi = tmp;
1075 ++lo; ++hi;
1076 } while (--k);
1077 }
1078 if (binarysort(lo - extra, hi, lo,
Guido van Rossum42812581998-06-17 14:15:44 +00001079 compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001080 goto fail;
1081 }
1082
1083 /* Find another slice to work on. */
1084 if (--top < 0)
1085 break; /* no more -- done! */
1086 lo = stack[top].lo;
1087 hi = stack[top].hi;
1088 extra = stack[top].extra;
1089 extraOnRight = 0;
1090 if (extra < 0) {
1091 extraOnRight = 1;
1092 extra = -extra;
1093 }
1094 continue;
1095 }
1096
1097 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1098 Then our preselected pivot is at (extra-1)/2, and we
1099 want to move the PPs before that to the left end of
1100 the slice, and the PPs after that to the right end.
1101 The following section changes extra, lo, hi, and the
1102 slice such that:
1103 [lo-extra, lo) contains the smaller PPs.
1104 *lo == our PP.
1105 (lo, hi) contains the unknown elements.
1106 [hi, hi+extra) contains the larger PPs.
1107 */
1108 k = extra >>= 1; /* num PPs to move */
1109 if (extraOnRight) {
1110 /* Swap the smaller PPs to the left end.
1111 Note that this loop actually moves k+1 items:
1112 the last is our PP */
1113 do {
1114 tmp = *lo; *lo = *hi; *hi = tmp;
1115 ++lo; ++hi;
1116 } while (k--);
1117 }
1118 else {
1119 /* Swap the larger PPs to the right end. */
1120 while (k--) {
1121 --lo; --hi;
1122 tmp = *lo; *lo = *hi; *hi = tmp;
1123 }
1124 }
1125 --lo; /* *lo is now our PP */
1126 pivot = *lo;
1127
1128 /* Now an almost-ordinary quicksort partition step.
1129 Note that most of the time is spent here!
1130 Only odd thing is that we partition into < and >=,
1131 instead of the usual <= and >=. This helps when
1132 there are lots of duplicates of different values,
1133 because it eventually tends to make subfiles
1134 "pure" (all duplicates), and we special-case for
1135 duplicates later. */
1136 l = lo + 1;
1137 r = hi - 1;
1138 /* assert lo < l < r < hi (small n weeded out above) */
1139
1140 do {
1141 /* slide l right, looking for key >= pivot */
1142 do {
1143 SETK(*l, pivot);
1144 if (k < 0)
1145 ++l;
1146 else
1147 break;
1148 } while (l < r);
1149
1150 /* slide r left, looking for key < pivot */
1151 while (l < r) {
Guido van Rossum42812581998-06-17 14:15:44 +00001152 register PyObject *rval = *r--;
1153 SETK(rval, pivot);
1154 if (k < 0) {
1155 /* swap and advance */
1156 r[1] = *l;
1157 *l++ = rval;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001158 break;
Guido van Rossum42812581998-06-17 14:15:44 +00001159 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001160 }
1161
1162 } while (l < r);
1163
1164 /* assert lo < r <= l < hi
1165 assert r == l or r+1 == l
1166 everything to the left of l is < pivot, and
1167 everything to the right of r is >= pivot */
1168
1169 if (l == r) {
1170 SETK(*r, pivot);
1171 if (k < 0)
1172 ++l;
1173 else
1174 --r;
1175 }
1176 /* assert lo <= r and r+1 == l and l <= hi
1177 assert r == lo or a[r] < pivot
1178 assert a[lo] is pivot
1179 assert l == hi or a[l] >= pivot
1180 Swap the pivot into "the middle", so we can henceforth
1181 ignore it.
1182 */
1183 *lo = *r;
1184 *r = pivot;
1185
1186 /* The following is true now, & will be preserved:
1187 All in [lo,r) are < pivot
1188 All in [r,l) == pivot (& so can be ignored)
1189 All in [l,hi) are >= pivot */
1190
1191 /* Check for duplicates of the pivot. One compare is
1192 wasted if there are no duplicates, but can win big
1193 when there are.
1194 Tricky: we're sticking to "<" compares, so deduce
1195 equality indirectly. We know pivot <= *l, so they're
1196 equal iff not pivot < *l.
1197 */
1198 while (l < hi) {
1199 /* pivot <= *l known */
1200 SETK(pivot, *l);
1201 if (k < 0)
1202 break;
1203 else
1204 /* <= and not < implies == */
1205 ++l;
1206 }
1207
1208 /* assert lo <= r < l <= hi
1209 Partitions are [lo, r) and [l, hi) */
1210
1211 /* push fattest first; remember we still have extra PPs
1212 to the left of the left chunk and to the right of
1213 the right chunk! */
1214 /* assert top < STACKSIZE */
1215 if (r - lo <= hi - l) {
1216 /* second is bigger */
1217 stack[top].lo = l;
1218 stack[top].hi = hi;
1219 stack[top].extra = -extra;
1220 hi = r;
1221 extraOnRight = 0;
1222 }
1223 else {
1224 /* first is bigger */
1225 stack[top].lo = lo;
1226 stack[top].hi = r;
1227 stack[top].extra = extra;
1228 lo = l;
1229 extraOnRight = 1;
1230 }
1231 ++top;
1232
1233 } /* end of partitioning loop */
1234
1235 return 0;
1236
1237 fail:
1238 return -1;
1239}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001240
1241#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001242
1243staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001244
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001245static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001246listsort(PyListObject *self, PyObject *args)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001247{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001248 int err;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001249 PyObject *compare = NULL;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001250
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001251 if (args != NULL) {
Guido van Rossumc00a9382000-02-24 21:48:29 +00001252 if (!PyArg_ParseTuple(args, "|O:sort", &compare))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001253 return NULL;
1254 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001255 self->ob_type = &immutable_list_type;
1256 err = samplesortslice(self->ob_item,
1257 self->ob_item + self->ob_size,
Guido van Rossum42812581998-06-17 14:15:44 +00001258 compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001259 self->ob_type = &PyList_Type;
1260 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001261 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001262 Py_INCREF(Py_None);
1263 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001264}
1265
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001266int
Fred Drakea2f55112000-07-09 15:16:51 +00001267PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001268{
1269 if (v == NULL || !PyList_Check(v)) {
1270 PyErr_BadInternalCall();
1271 return -1;
1272 }
1273 v = listsort((PyListObject *)v, (PyObject *)NULL);
1274 if (v == NULL)
1275 return -1;
1276 Py_DECREF(v);
1277 return 0;
1278}
1279
Guido van Rossumb86c5492001-02-12 22:06:02 +00001280static void
1281_listreverse(PyListObject *self)
Guido van Rossumed98d481991-03-06 13:07:53 +00001282{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001283 register PyObject **p, **q;
1284 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001285
Guido van Rossumed98d481991-03-06 13:07:53 +00001286 if (self->ob_size > 1) {
1287 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
Guido van Rossumb86c5492001-02-12 22:06:02 +00001288 p < q;
1289 p++, q--)
1290 {
Guido van Rossumed98d481991-03-06 13:07:53 +00001291 tmp = *p;
1292 *p = *q;
1293 *q = tmp;
1294 }
1295 }
Guido van Rossumb86c5492001-02-12 22:06:02 +00001296}
1297
1298static PyObject *
1299listreverse(PyListObject *self, PyObject *args)
1300{
1301 if (!PyArg_ParseTuple(args, ":reverse"))
1302 return NULL;
1303 _listreverse(self);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001304 Py_INCREF(Py_None);
1305 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001306}
1307
Guido van Rossum84c76f51990-10-30 13:32:20 +00001308int
Fred Drakea2f55112000-07-09 15:16:51 +00001309PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001310{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001311 if (v == NULL || !PyList_Check(v)) {
1312 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001313 return -1;
1314 }
Guido van Rossumb86c5492001-02-12 22:06:02 +00001315 _listreverse((PyListObject *)v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001316 return 0;
1317}
1318
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001319PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001320PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001321{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001322 PyObject *w;
1323 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001324 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001325 if (v == NULL || !PyList_Check(v)) {
1326 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001327 return NULL;
1328 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001329 n = ((PyListObject *)v)->ob_size;
1330 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001331 if (w == NULL)
1332 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001333 p = ((PyTupleObject *)w)->ob_item;
Thomas Wouters334fb892000-07-25 12:56:38 +00001334 memcpy((void *)p,
1335 (void *)((PyListObject *)v)->ob_item,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001336 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001337 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001338 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001339 p++;
1340 }
1341 return w;
1342}
1343
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001344static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001345listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00001346{
1347 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001348 PyObject *v;
1349
Tim Peters442914d2001-05-26 05:50:03 +00001350 if (!PyArg_ParseTuple(args, "O:index", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001351 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001352 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001353 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
1354 if (cmp > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001355 return PyInt_FromLong((long)i);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001356 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001357 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001358 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001359 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001360 return NULL;
1361}
1362
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001363static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001364listcount(PyListObject *self, PyObject *args)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001365{
1366 int count = 0;
1367 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001368 PyObject *v;
1369
Tim Peters442914d2001-05-26 05:50:03 +00001370 if (!PyArg_ParseTuple(args, "O:count", &v))
Guido van Rossume6f7d181991-10-20 20:20:40 +00001371 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001372 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001373 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
1374 if (cmp > 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001375 count++;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001376 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001377 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001378 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001379 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001380}
1381
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001382static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001383listremove(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00001384{
1385 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001386 PyObject *v;
1387
Tim Peters442914d2001-05-26 05:50:03 +00001388 if (!PyArg_ParseTuple(args, "O:remove", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001389 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001390 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001391 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
1392 if (cmp > 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001393 if (list_ass_slice(self, i, i+1,
1394 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001395 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001396 Py_INCREF(Py_None);
1397 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001398 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001399 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001400 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001401 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001402 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001403 return NULL;
1404}
1405
Jeremy Hylton8caad492000-06-23 14:18:11 +00001406static int
1407list_traverse(PyListObject *o, visitproc visit, void *arg)
1408{
1409 int i, err;
1410 PyObject *x;
1411
1412 for (i = o->ob_size; --i >= 0; ) {
1413 x = o->ob_item[i];
1414 if (x != NULL) {
1415 err = visit(x, arg);
1416 if (err)
1417 return err;
1418 }
1419 }
1420 return 0;
1421}
1422
1423static int
1424list_clear(PyListObject *lp)
1425{
1426 (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0);
1427 return 0;
1428}
1429
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001430static PyObject *
1431list_richcompare(PyObject *v, PyObject *w, int op)
1432{
1433 PyListObject *vl, *wl;
1434 int i;
1435
1436 if (!PyList_Check(v) || !PyList_Check(w)) {
1437 Py_INCREF(Py_NotImplemented);
1438 return Py_NotImplemented;
1439 }
1440
1441 vl = (PyListObject *)v;
1442 wl = (PyListObject *)w;
1443
1444 if (vl->ob_size != wl->ob_size && (op == Py_EQ || op == Py_NE)) {
1445 /* Shortcut: if the lengths differ, the lists differ */
1446 PyObject *res;
1447 if (op == Py_EQ)
1448 res = Py_False;
1449 else
1450 res = Py_True;
1451 Py_INCREF(res);
1452 return res;
1453 }
1454
1455 /* Search for the first index where items are different */
1456 for (i = 0; i < vl->ob_size && i < wl->ob_size; i++) {
1457 int k = PyObject_RichCompareBool(vl->ob_item[i],
1458 wl->ob_item[i], Py_EQ);
1459 if (k < 0)
1460 return NULL;
1461 if (!k)
1462 break;
1463 }
1464
1465 if (i >= vl->ob_size || i >= wl->ob_size) {
1466 /* No more items to compare -- compare sizes */
1467 int vs = vl->ob_size;
1468 int ws = wl->ob_size;
1469 int cmp;
1470 PyObject *res;
1471 switch (op) {
1472 case Py_LT: cmp = vs < ws; break;
1473 case Py_LE: cmp = ws <= ws; break;
1474 case Py_EQ: cmp = vs == ws; break;
1475 case Py_NE: cmp = vs != ws; break;
1476 case Py_GT: cmp = vs > ws; break;
1477 case Py_GE: cmp = vs >= ws; break;
1478 default: return NULL; /* cannot happen */
1479 }
1480 if (cmp)
1481 res = Py_True;
1482 else
1483 res = Py_False;
1484 Py_INCREF(res);
1485 return res;
1486 }
1487
1488 /* We have an item that differs -- shortcuts for EQ/NE */
1489 if (op == Py_EQ) {
1490 Py_INCREF(Py_False);
1491 return Py_False;
1492 }
1493 if (op == Py_NE) {
1494 Py_INCREF(Py_True);
1495 return Py_True;
1496 }
1497
1498 /* Compare the final item again using the proper operator */
1499 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
1500}
1501
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001502static char append_doc[] =
1503"L.append(object) -- append object to end";
Barry Warsawdedf6d61998-10-09 16:37:25 +00001504static char extend_doc[] =
1505"L.extend(list) -- extend list by appending list elements";
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001506static char insert_doc[] =
1507"L.insert(index, object) -- insert object before index";
1508static char pop_doc[] =
1509"L.pop([index]) -> item -- remove and return item at index (default last)";
1510static char remove_doc[] =
1511"L.remove(value) -- remove first occurrence of value";
1512static char index_doc[] =
1513"L.index(value) -> integer -- return index of first occurrence of value";
1514static char count_doc[] =
1515"L.count(value) -> integer -- return number of occurrences of value";
1516static char reverse_doc[] =
1517"L.reverse() -- reverse *IN PLACE*";
1518static char sort_doc[] =
1519"L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1520
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001521static PyMethodDef list_methods[] = {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001522 {"append", (PyCFunction)listappend, METH_VARARGS, append_doc},
1523 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
1524 {"extend", (PyCFunction)listextend, METH_VARARGS, extend_doc},
1525 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
1526 {"remove", (PyCFunction)listremove, METH_VARARGS, remove_doc},
1527 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
1528 {"count", (PyCFunction)listcount, METH_VARARGS, count_doc},
Tim Peters0e76ab22000-12-13 22:35:46 +00001529 {"reverse", (PyCFunction)listreverse, METH_VARARGS, reverse_doc},
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001530 {"sort", (PyCFunction)listsort, METH_VARARGS, sort_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001531 {NULL, NULL} /* sentinel */
1532};
1533
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001534static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001535list_getattr(PyListObject *f, char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001536{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001537 return Py_FindMethod(list_methods, (PyObject *)f, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001538}
1539
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001540static PySequenceMethods list_as_sequence = {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001541 (inquiry)list_length, /* sq_length */
1542 (binaryfunc)list_concat, /* sq_concat */
1543 (intargfunc)list_repeat, /* sq_repeat */
1544 (intargfunc)list_item, /* sq_item */
1545 (intintargfunc)list_slice, /* sq_slice */
1546 (intobjargproc)list_ass_item, /* sq_ass_item */
1547 (intintobjargproc)list_ass_slice, /* sq_ass_slice */
1548 (objobjproc)list_contains, /* sq_contains */
1549 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
1550 (intargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001551};
1552
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001553PyTypeObject PyList_Type = {
1554 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001555 0,
1556 "list",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001557 sizeof(PyListObject) + PyGC_HEAD_SIZE,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001558 0,
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001559 (destructor)list_dealloc, /* tp_dealloc */
1560 (printfunc)list_print, /* tp_print */
1561 (getattrfunc)list_getattr, /* tp_getattr */
1562 0, /* tp_setattr */
1563 0, /* tp_compare */
1564 (reprfunc)list_repr, /* tp_repr */
1565 0, /* tp_as_number */
1566 &list_as_sequence, /* tp_as_sequence */
1567 0, /* tp_as_mapping */
1568 0, /* tp_hash */
1569 0, /* tp_call */
1570 0, /* tp_str */
1571 0, /* tp_getattro */
1572 0, /* tp_setattro */
1573 0, /* tp_as_buffer */
1574 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1575 0, /* tp_doc */
1576 (traverseproc)list_traverse, /* tp_traverse */
1577 (inquiry)list_clear, /* tp_clear */
1578 list_richcompare, /* tp_richcompare */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001579};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001580
1581
1582/* During a sort, we really can't have anyone modifying the list; it could
1583 cause core dumps. Thus, we substitute a dummy type that raises an
1584 explanatory exception when a modifying operation is used. Caveat:
1585 comparisons may behave differently; but I guess it's a bad idea anyway to
1586 compare a list that's being sorted... */
1587
1588static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001589immutable_list_op(void)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001590{
1591 PyErr_SetString(PyExc_TypeError,
1592 "a list cannot be modified while it is being sorted");
1593 return NULL;
1594}
1595
1596static PyMethodDef immutable_list_methods[] = {
1597 {"append", (PyCFunction)immutable_list_op},
1598 {"insert", (PyCFunction)immutable_list_op},
1599 {"remove", (PyCFunction)immutable_list_op},
1600 {"index", (PyCFunction)listindex},
1601 {"count", (PyCFunction)listcount},
1602 {"reverse", (PyCFunction)immutable_list_op},
1603 {"sort", (PyCFunction)immutable_list_op},
1604 {NULL, NULL} /* sentinel */
1605};
1606
1607static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001608immutable_list_getattr(PyListObject *f, char *name)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001609{
1610 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
1611}
1612
1613static int
Fred Drakea2f55112000-07-09 15:16:51 +00001614immutable_list_ass(void)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001615{
1616 immutable_list_op();
1617 return -1;
1618}
1619
1620static PySequenceMethods immutable_list_as_sequence = {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001621 (inquiry)list_length, /* sq_length */
1622 (binaryfunc)list_concat, /* sq_concat */
1623 (intargfunc)list_repeat, /* sq_repeat */
1624 (intargfunc)list_item, /* sq_item */
1625 (intintargfunc)list_slice, /* sq_slice */
1626 (intobjargproc)immutable_list_ass, /* sq_ass_item */
1627 (intintobjargproc)immutable_list_ass, /* sq_ass_slice */
1628 (objobjproc)list_contains, /* sq_contains */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001629};
1630
1631static PyTypeObject immutable_list_type = {
1632 PyObject_HEAD_INIT(&PyType_Type)
1633 0,
1634 "list (immutable, during sort)",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001635 sizeof(PyListObject) + PyGC_HEAD_SIZE,
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001636 0,
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001637 0, /* Cannot happen */ /* tp_dealloc */
1638 (printfunc)list_print, /* tp_print */
1639 (getattrfunc)immutable_list_getattr, /* tp_getattr */
1640 0, /* tp_setattr */
1641 0, /* Won't be called */ /* tp_compare */
1642 (reprfunc)list_repr, /* tp_repr */
1643 0, /* tp_as_number */
1644 &immutable_list_as_sequence, /* tp_as_sequence */
1645 0, /* tp_as_mapping */
1646 0, /* tp_hash */
1647 0, /* tp_call */
1648 0, /* tp_str */
1649 0, /* tp_getattro */
1650 0, /* tp_setattro */
1651 0, /* tp_as_buffer */
1652 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1653 0, /* tp_doc */
1654 (traverseproc)list_traverse, /* tp_traverse */
1655 0, /* tp_clear */
1656 list_richcompare, /* tp_richcompare */
1657 /* NOTE: This is *not* the standard list_type struct! */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001658};