blob: 7d0fbc10a021beab61b4c7074f45d2b2e3834cea [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 Rossumef93b872000-03-13 15:41:59 +0000579/* Define NO_STRICT_LIST_APPEND to enable multi-argument append() */
580
581#ifndef NO_STRICT_LIST_APPEND
582#define PyArg_ParseTuple_Compat1 PyArg_ParseTuple
583#else
584#define PyArg_ParseTuple_Compat1(args, format, ret) \
585( \
586 PyTuple_GET_SIZE(args) > 1 ? (*ret = args, 1) : \
587 PyTuple_GET_SIZE(args) == 1 ? (*ret = PyTuple_GET_ITEM(args, 0), 1) : \
588 PyArg_ParseTuple(args, format, ret) \
589)
590#endif
591
592
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000594listappend(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000595{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000596 PyObject *v;
Guido van Rossumef93b872000-03-13 15:41:59 +0000597 if (!PyArg_ParseTuple_Compat1(args, "O:append", &v))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000598 return NULL;
599 return ins(self, (int) self->ob_size, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000600}
601
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000602static int
603listextend_internal(PyListObject *self, PyObject *b)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000604{
Barry Warsawdedf6d61998-10-09 16:37:25 +0000605 PyObject **items;
606 int selflen = PyList_GET_SIZE(self);
607 int blen;
608 register int i;
609
Jeremy Hyltondb60bb52001-01-03 22:32:16 +0000610 if (PyObject_Size(b) == 0) {
Barry Warsawdedf6d61998-10-09 16:37:25 +0000611 /* short circuit when b is empty */
Jeremy Hyltondb60bb52001-01-03 22:32:16 +0000612 Py_DECREF(b);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000613 return 0;
Jeremy Hyltondb60bb52001-01-03 22:32:16 +0000614 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000615
Barry Warsawdedf6d61998-10-09 16:37:25 +0000616 if (self == (PyListObject*)b) {
617 /* as in list_ass_slice() we must special case the
618 * situation: a.extend(a)
619 *
620 * XXX: I think this way ought to be faster than using
621 * list_slice() the way list_ass_slice() does.
622 */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000623 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000624 b = PyList_New(selflen);
625 if (!b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000626 return -1;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000627 for (i = 0; i < selflen; i++) {
628 PyObject *o = PyList_GET_ITEM(self, i);
629 Py_INCREF(o);
630 PyList_SET_ITEM(b, i, o);
631 }
632 }
Barry Warsawdedf6d61998-10-09 16:37:25 +0000633
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000634 blen = PyObject_Size(b);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000635
Barry Warsawdedf6d61998-10-09 16:37:25 +0000636 /* resize a using idiom */
637 items = self->ob_item;
638 NRESIZE(items, PyObject*, selflen + blen);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000639 if (items == NULL) {
Barry Warsawdedf6d61998-10-09 16:37:25 +0000640 PyErr_NoMemory();
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000641 Py_DECREF(b);
642 return -1;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000643 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000644
Barry Warsawdedf6d61998-10-09 16:37:25 +0000645 self->ob_item = items;
646
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000647 /* populate the end of self with b's items */
Barry Warsawdedf6d61998-10-09 16:37:25 +0000648 for (i = 0; i < blen; i++) {
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000649 PyObject *o = PySequence_Fast_GET_ITEM(b, i);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000650 Py_INCREF(o);
651 PyList_SET_ITEM(self, self->ob_size++, o);
652 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000653 Py_DECREF(b);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000654 return 0;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000655}
656
657
658static PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000659list_inplace_concat(PyListObject *self, PyObject *other)
660{
661 other = PySequence_Fast(other, "argument to += must be a sequence");
662 if (!other)
663 return NULL;
664
665 if (listextend_internal(self, other) < 0)
666 return NULL;
667
668 Py_INCREF(self);
669 return (PyObject *)self;
670}
671
672static PyObject *
673listextend(PyListObject *self, PyObject *args)
674{
675
676 PyObject *b;
677
678 if (!PyArg_ParseTuple(args, "O:extend", &b))
679 return NULL;
680
681 b = PySequence_Fast(b, "list.extend() argument must be a sequence");
682 if (!b)
683 return NULL;
684
685 if (listextend_internal(self, b) < 0)
686 return NULL;
687
688 Py_INCREF(Py_None);
689 return Py_None;
690}
691
692static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000693listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000694{
695 int i = -1;
696 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000697 if (!PyArg_ParseTuple(args, "|i:pop", &i))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000698 return NULL;
699 if (self->ob_size == 0) {
700 /* Special-case most common failure cause */
701 PyErr_SetString(PyExc_IndexError, "pop from empty list");
702 return NULL;
703 }
704 if (i < 0)
705 i += self->ob_size;
706 if (i < 0 || i >= self->ob_size) {
707 PyErr_SetString(PyExc_IndexError, "pop index out of range");
708 return NULL;
709 }
710 v = self->ob_item[i];
711 Py_INCREF(v);
712 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
713 Py_DECREF(v);
714 return NULL;
715 }
716 return v;
717}
718
Guido van Rossum3f236de1996-12-10 23:55:39 +0000719/* New quicksort implementation for arrays of object pointers.
720 Thanks to discussions with Tim Peters. */
721
722/* CMPERROR is returned by our comparison function when an error
723 occurred. This is the largest negative integer (0x80000000 on a
724 32-bit system). */
Guido van Rossum19700b61997-03-05 00:45:43 +0000725#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
Guido van Rossum3f236de1996-12-10 23:55:39 +0000726
727/* Comparison function. Takes care of calling a user-supplied
728 comparison function (any callable Python object). Calls the
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000729 standard comparison function, PyObject_Compare(), if the user-
730 supplied function is NULL. */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000731
732static int
Fred Drakea2f55112000-07-09 15:16:51 +0000733docompare(PyObject *x, PyObject *y, PyObject *compare)
Guido van Rossum3f236de1996-12-10 23:55:39 +0000734{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000735 PyObject *args, *res;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000736 int i;
737
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000738 if (compare == NULL) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +0000739 /* NOTE: we rely on the fact here that the sorting algorithm
740 only ever checks whether k<0, i.e., whether x<y. So we
741 invoke the rich comparison function with Py_LT ('<'), and
742 return -1 when it returns true and 0 when it returns
743 false. */
744 i = PyObject_RichCompareBool(x, y, Py_LT);
745 if (i < 0)
746 return CMPERROR;
747 else
748 return -i;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000749 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000750
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000751 args = Py_BuildValue("(OO)", x, y);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000752 if (args == NULL)
753 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000754 res = PyEval_CallObject(compare, args);
755 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000756 if (res == NULL)
757 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000758 if (!PyInt_Check(res)) {
759 Py_DECREF(res);
760 PyErr_SetString(PyExc_TypeError,
Guido van Rossum9bcd1d71999-04-19 17:44:39 +0000761 "comparison function must return int");
Guido van Rossum3f236de1996-12-10 23:55:39 +0000762 return CMPERROR;
763 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000764 i = PyInt_AsLong(res);
765 Py_DECREF(res);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000766 if (i < 0)
767 return -1;
768 if (i > 0)
769 return 1;
770 return 0;
771}
772
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000773/* MINSIZE is the smallest array that will get a full-blown samplesort
774 treatment; smaller arrays are sorted using binary insertion. It must
775 be at least 7 for the samplesort implementation to work. Binary
776 insertion does fewer compares, but can suffer O(N**2) data movement.
777 The more expensive compares, the larger MINSIZE should be. */
778#define MINSIZE 100
779
780/* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
781 partition; smaller slices are passed to binarysort. It must be at
782 least 2, and no larger than MINSIZE. Setting it higher reduces the #
783 of compares slowly, but increases the amount of data movement quickly.
784 The value here was chosen assuming a compare costs ~25x more than
785 swapping a pair of memory-resident pointers -- but under that assumption,
786 changing the value by a few dozen more or less has aggregate effect
787 under 1%. So the value is crucial, but not touchy <wink>. */
788#define MINPARTITIONSIZE 40
789
790/* MAXMERGE is the largest number of elements we'll always merge into
791 a known-to-be sorted chunk via binary insertion, regardless of the
792 size of that chunk. Given a chunk of N sorted elements, and a group
793 of K unknowns, the largest K for which it's better to do insertion
794 (than a full-blown sort) is a complicated function of N and K mostly
795 involving the expected number of compares and data moves under each
796 approach, and the relative cost of those operations on a specific
797 architecure. The fixed value here is conservative, and should be a
798 clear win regardless of architecture or N. */
799#define MAXMERGE 15
Guido van Rossum3f236de1996-12-10 23:55:39 +0000800
Guido van Rossum3f236de1996-12-10 23:55:39 +0000801/* STACKSIZE is the size of our work stack. A rough estimate is that
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000802 this allows us to sort arrays of size N where
803 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
804 for arrays of size 2**64. Because we push the biggest partition
805 first, the worst case occurs when all subarrays are always partitioned
806 exactly in two. */
807#define STACKSIZE 60
Guido van Rossum3f236de1996-12-10 23:55:39 +0000808
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000809
810#define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
811
812/* binarysort is the best method for sorting small arrays: it does
813 few compares, but can do data movement quadratic in the number of
814 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000815 [lo, hi) is a contiguous slice of a list, and is sorted via
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000816 binary insertion.
817 On entry, must have lo <= start <= hi, and that [lo, start) is already
818 sorted (pass start == lo if you don't know!).
819 If docompare complains (returns CMPERROR) return -1, else 0.
820 Even in case of error, the output slice will be some permutation of
821 the input (nothing is lost or duplicated).
822*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000823
824static int
Fred Drakea2f55112000-07-09 15:16:51 +0000825binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
826 /* compare -- comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000827{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000828 /* assert lo <= start <= hi
829 assert [lo, start) is sorted */
830 register int k;
831 register PyObject **l, **p, **r;
832 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000833
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000834 if (lo == start)
835 ++start;
836 for (; start < hi; ++start) {
837 /* set l to where *start belongs */
838 l = lo;
839 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000840 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000841 do {
842 p = l + ((r - l) >> 1);
843 SETK(pivot, *p);
844 if (k < 0)
845 r = p;
846 else
847 l = p + 1;
848 } while (l < r);
849 /* Pivot should go at l -- slide over to make room.
850 Caution: using memmove is much slower under MSVC 5;
851 we're not usually moving many slots. */
852 for (p = start; p > l; --p)
853 *p = *(p-1);
854 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000855 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000856 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000857
858 fail:
859 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000860}
861
862/* samplesortslice is the sorting workhorse.
Guido van Rossum42812581998-06-17 14:15:44 +0000863 [lo, hi) is a contiguous slice of a list, to be sorted in place.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000864 On entry, must have lo <= hi,
865 If docompare complains (returns CMPERROR) return -1, else 0.
866 Even in case of error, the output slice will be some permutation of
867 the input (nothing is lost or duplicated).
868
869 samplesort is basically quicksort on steroids: a power of 2 close
870 to n/ln(n) is computed, and that many elements (less 1) are picked at
871 random from the array and sorted. These 2**k - 1 elements are then
872 used as preselected pivots for an equal number of quicksort
873 partitioning steps, partitioning the slice into 2**k chunks each of
874 size about ln(n). These small final chunks are then usually handled
875 by binarysort. Note that when k=1, this is roughly the same as an
876 ordinary quicksort using a random pivot, and when k=2 this is roughly
877 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
878 this a "median of n/ln(n)" quicksort. You can also view it as a kind
879 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
880
881 The large number of samples makes a quadratic-time case almost
882 impossible, and asymptotically drives the average-case number of
883 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
884 3 variant) down to N lg N.
885
886 We also play lots of low-level tricks to cut the number of compares.
887
888 Very obscure: To avoid using extra memory, the PPs are stored in the
889 array and shuffled around as partitioning proceeds. At the start of a
890 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
891 adjacent (either on the left or the right!) to a chunk of X elements
892 that are to be partitioned: P X or X P. In either case we need to
893 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
894 left, followed by the PP to be used for this step (that's the middle
895 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
896 P X or X P -> Psmall pivot X Plarge
897 and the order of the PPs must not be altered. It can take a while
898 to realize this isn't trivial! It can take even longer <wink> to
899 understand why the simple code below works, using only 2**(m-1) swaps.
900 The key is that the order of the X elements isn't necessarily
901 preserved: X can end up as some cyclic permutation of its original
902 order. That's OK, because X is unsorted anyway. If the order of X
903 had to be preserved too, the simplest method I know of using O(1)
904 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
905 Since len(X) is typically several times larger than 2**(m-1), that
906 would slow things down.
907*/
908
909struct SamplesortStackNode {
910 /* Represents a slice of the array, from (& including) lo up
911 to (but excluding) hi. "extra" additional & adjacent elements
912 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
913 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
914 already sorted, but nothing is known about the other elements
915 in [lo, hi). |extra| is always one less than a power of 2.
916 When extra is 0, we're out of PPs, and the slice must be
917 sorted by some other means. */
918 PyObject **lo;
919 PyObject **hi;
920 int extra;
921};
922
923/* 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 +0000924 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000925 is undesirable, so cutoff values are canned in the "cutoff" table
926 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
927#define CUTOFFBASE 4
Guido van Rossum3aa23fd1999-01-14 19:01:53 +0000928static long cutoff[] = {
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000929 43, /* smallest N such that k == 4 */
930 106, /* etc */
931 250,
932 576,
933 1298,
934 2885,
935 6339,
936 13805,
937 29843,
938 64116,
939 137030,
940 291554,
941 617916,
942 1305130,
943 2748295,
944 5771662,
945 12091672,
946 25276798,
947 52734615,
948 109820537,
949 228324027,
950 473977813,
951 982548444, /* smallest N such that k == 26 */
952 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
953};
954
955static int
Fred Drakea2f55112000-07-09 15:16:51 +0000956samplesortslice(PyObject **lo, PyObject **hi, PyObject *compare)
957 /* compare -- comparison function object, or NULL for default */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000958{
959 register PyObject **l, **r;
960 register PyObject *tmp, *pivot;
961 register int k;
962 int n, extra, top, extraOnRight;
963 struct SamplesortStackNode stack[STACKSIZE];
964
965 /* assert lo <= hi */
966 n = hi - lo;
967
968 /* ----------------------------------------------------------
969 * Special cases
970 * --------------------------------------------------------*/
971 if (n < 2)
972 return 0;
973
974 /* Set r to the largest value such that [lo,r) is sorted.
975 This catches the already-sorted case, the all-the-same
976 case, and the appended-a-few-elements-to-a-sorted-list case.
977 If the array is unsorted, we're very likely to get out of
978 the loop fast, so the test is cheap if it doesn't pay off.
979 */
980 /* assert lo < hi */
981 for (r = lo+1; r < hi; ++r) {
982 SETK(*r, *(r-1));
983 if (k < 0)
984 break;
985 }
986 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
987 few unknowns, or few elements in total. */
988 if (hi - r <= MAXMERGE || n < MINSIZE)
Guido van Rossum42812581998-06-17 14:15:44 +0000989 return binarysort(lo, hi, r, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000990
991 /* Check for the array already being reverse-sorted. Typical
992 benchmark-driven silliness <wink>. */
993 /* assert lo < hi */
994 for (r = lo+1; r < hi; ++r) {
995 SETK(*(r-1), *r);
996 if (k < 0)
997 break;
998 }
999 if (hi - r <= MAXMERGE) {
1000 /* Reverse the reversed prefix, then insert the tail */
1001 PyObject **originalr = r;
1002 l = lo;
1003 do {
1004 --r;
1005 tmp = *l; *l = *r; *r = tmp;
1006 ++l;
1007 } while (l < r);
Guido van Rossum42812581998-06-17 14:15:44 +00001008 return binarysort(lo, hi, originalr, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001009 }
1010
1011 /* ----------------------------------------------------------
1012 * Normal case setup: a large array without obvious pattern.
1013 * --------------------------------------------------------*/
1014
1015 /* extra := a power of 2 ~= n/ln(n), less 1.
1016 First find the smallest extra s.t. n < cutoff[extra] */
1017 for (extra = 0;
1018 extra < sizeof(cutoff) / sizeof(cutoff[0]);
1019 ++extra) {
1020 if (n < cutoff[extra])
1021 break;
1022 /* note that if we fall out of the loop, the value of
1023 extra still makes *sense*, but may be smaller than
1024 we would like (but the array has more than ~= 2**31
1025 elements in this case!) */
1026 }
1027 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1028 have is CUTOFFBASE-1, so
1029 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1030 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
1031 /* assert extra > 0 and n >= extra */
1032
1033 /* Swap that many values to the start of the array. The
1034 selection of elements is pseudo-random, but the same on
1035 every run (this is intentional! timing algorithm changes is
1036 a pain if timing varies across runs). */
1037 {
1038 unsigned int seed = n / extra; /* arbitrary */
1039 unsigned int i;
1040 for (i = 0; i < (unsigned)extra; ++i) {
1041 /* j := random int in [i, n) */
1042 unsigned int j;
1043 seed = seed * 69069 + 7;
1044 j = i + seed % (n - i);
1045 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
1046 }
1047 }
1048
1049 /* Recursively sort the preselected pivots. */
Guido van Rossum42812581998-06-17 14:15:44 +00001050 if (samplesortslice(lo, lo + extra, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001051 goto fail;
1052
1053 top = 0; /* index of available stack slot */
1054 lo += extra; /* point to first unknown */
1055 extraOnRight = 0; /* the PPs are at the left end */
1056
1057 /* ----------------------------------------------------------
1058 * Partition [lo, hi), and repeat until out of work.
1059 * --------------------------------------------------------*/
1060 for (;;) {
1061 /* assert lo <= hi, so n >= 0 */
1062 n = hi - lo;
1063
1064 /* We may not want, or may not be able, to partition:
1065 If n is small, it's quicker to insert.
1066 If extra is 0, we're out of pivots, and *must* use
1067 another method.
1068 */
1069 if (n < MINPARTITIONSIZE || extra == 0) {
1070 if (n >= MINSIZE) {
1071 /* assert extra == 0
1072 This is rare, since the average size
1073 of a final block is only about
1074 ln(original n). */
Guido van Rossum42812581998-06-17 14:15:44 +00001075 if (samplesortslice(lo, hi, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001076 goto fail;
1077 }
1078 else {
1079 /* Binary insertion should be quicker,
1080 and we can take advantage of the PPs
1081 already being sorted. */
1082 if (extraOnRight && extra) {
1083 /* swap the PPs to the left end */
1084 k = extra;
1085 do {
1086 tmp = *lo;
1087 *lo = *hi;
1088 *hi = tmp;
1089 ++lo; ++hi;
1090 } while (--k);
1091 }
1092 if (binarysort(lo - extra, hi, lo,
Guido van Rossum42812581998-06-17 14:15:44 +00001093 compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001094 goto fail;
1095 }
1096
1097 /* Find another slice to work on. */
1098 if (--top < 0)
1099 break; /* no more -- done! */
1100 lo = stack[top].lo;
1101 hi = stack[top].hi;
1102 extra = stack[top].extra;
1103 extraOnRight = 0;
1104 if (extra < 0) {
1105 extraOnRight = 1;
1106 extra = -extra;
1107 }
1108 continue;
1109 }
1110
1111 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1112 Then our preselected pivot is at (extra-1)/2, and we
1113 want to move the PPs before that to the left end of
1114 the slice, and the PPs after that to the right end.
1115 The following section changes extra, lo, hi, and the
1116 slice such that:
1117 [lo-extra, lo) contains the smaller PPs.
1118 *lo == our PP.
1119 (lo, hi) contains the unknown elements.
1120 [hi, hi+extra) contains the larger PPs.
1121 */
1122 k = extra >>= 1; /* num PPs to move */
1123 if (extraOnRight) {
1124 /* Swap the smaller PPs to the left end.
1125 Note that this loop actually moves k+1 items:
1126 the last is our PP */
1127 do {
1128 tmp = *lo; *lo = *hi; *hi = tmp;
1129 ++lo; ++hi;
1130 } while (k--);
1131 }
1132 else {
1133 /* Swap the larger PPs to the right end. */
1134 while (k--) {
1135 --lo; --hi;
1136 tmp = *lo; *lo = *hi; *hi = tmp;
1137 }
1138 }
1139 --lo; /* *lo is now our PP */
1140 pivot = *lo;
1141
1142 /* Now an almost-ordinary quicksort partition step.
1143 Note that most of the time is spent here!
1144 Only odd thing is that we partition into < and >=,
1145 instead of the usual <= and >=. This helps when
1146 there are lots of duplicates of different values,
1147 because it eventually tends to make subfiles
1148 "pure" (all duplicates), and we special-case for
1149 duplicates later. */
1150 l = lo + 1;
1151 r = hi - 1;
1152 /* assert lo < l < r < hi (small n weeded out above) */
1153
1154 do {
1155 /* slide l right, looking for key >= pivot */
1156 do {
1157 SETK(*l, pivot);
1158 if (k < 0)
1159 ++l;
1160 else
1161 break;
1162 } while (l < r);
1163
1164 /* slide r left, looking for key < pivot */
1165 while (l < r) {
Guido van Rossum42812581998-06-17 14:15:44 +00001166 register PyObject *rval = *r--;
1167 SETK(rval, pivot);
1168 if (k < 0) {
1169 /* swap and advance */
1170 r[1] = *l;
1171 *l++ = rval;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001172 break;
Guido van Rossum42812581998-06-17 14:15:44 +00001173 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001174 }
1175
1176 } while (l < r);
1177
1178 /* assert lo < r <= l < hi
1179 assert r == l or r+1 == l
1180 everything to the left of l is < pivot, and
1181 everything to the right of r is >= pivot */
1182
1183 if (l == r) {
1184 SETK(*r, pivot);
1185 if (k < 0)
1186 ++l;
1187 else
1188 --r;
1189 }
1190 /* assert lo <= r and r+1 == l and l <= hi
1191 assert r == lo or a[r] < pivot
1192 assert a[lo] is pivot
1193 assert l == hi or a[l] >= pivot
1194 Swap the pivot into "the middle", so we can henceforth
1195 ignore it.
1196 */
1197 *lo = *r;
1198 *r = pivot;
1199
1200 /* The following is true now, & will be preserved:
1201 All in [lo,r) are < pivot
1202 All in [r,l) == pivot (& so can be ignored)
1203 All in [l,hi) are >= pivot */
1204
1205 /* Check for duplicates of the pivot. One compare is
1206 wasted if there are no duplicates, but can win big
1207 when there are.
1208 Tricky: we're sticking to "<" compares, so deduce
1209 equality indirectly. We know pivot <= *l, so they're
1210 equal iff not pivot < *l.
1211 */
1212 while (l < hi) {
1213 /* pivot <= *l known */
1214 SETK(pivot, *l);
1215 if (k < 0)
1216 break;
1217 else
1218 /* <= and not < implies == */
1219 ++l;
1220 }
1221
1222 /* assert lo <= r < l <= hi
1223 Partitions are [lo, r) and [l, hi) */
1224
1225 /* push fattest first; remember we still have extra PPs
1226 to the left of the left chunk and to the right of
1227 the right chunk! */
1228 /* assert top < STACKSIZE */
1229 if (r - lo <= hi - l) {
1230 /* second is bigger */
1231 stack[top].lo = l;
1232 stack[top].hi = hi;
1233 stack[top].extra = -extra;
1234 hi = r;
1235 extraOnRight = 0;
1236 }
1237 else {
1238 /* first is bigger */
1239 stack[top].lo = lo;
1240 stack[top].hi = r;
1241 stack[top].extra = extra;
1242 lo = l;
1243 extraOnRight = 1;
1244 }
1245 ++top;
1246
1247 } /* end of partitioning loop */
1248
1249 return 0;
1250
1251 fail:
1252 return -1;
1253}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001254
1255#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001256
1257staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001258
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001259static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001260listsort(PyListObject *self, PyObject *args)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001261{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001262 int err;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001263 PyObject *compare = NULL;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001264
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001265 if (args != NULL) {
Guido van Rossumc00a9382000-02-24 21:48:29 +00001266 if (!PyArg_ParseTuple(args, "|O:sort", &compare))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001267 return NULL;
1268 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001269 self->ob_type = &immutable_list_type;
1270 err = samplesortslice(self->ob_item,
1271 self->ob_item + self->ob_size,
Guido van Rossum42812581998-06-17 14:15:44 +00001272 compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001273 self->ob_type = &PyList_Type;
1274 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001275 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001276 Py_INCREF(Py_None);
1277 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001278}
1279
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001280int
Fred Drakea2f55112000-07-09 15:16:51 +00001281PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001282{
1283 if (v == NULL || !PyList_Check(v)) {
1284 PyErr_BadInternalCall();
1285 return -1;
1286 }
1287 v = listsort((PyListObject *)v, (PyObject *)NULL);
1288 if (v == NULL)
1289 return -1;
1290 Py_DECREF(v);
1291 return 0;
1292}
1293
Guido van Rossumb86c5492001-02-12 22:06:02 +00001294static void
1295_listreverse(PyListObject *self)
Guido van Rossumed98d481991-03-06 13:07:53 +00001296{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001297 register PyObject **p, **q;
1298 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001299
Guido van Rossumed98d481991-03-06 13:07:53 +00001300 if (self->ob_size > 1) {
1301 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
Guido van Rossumb86c5492001-02-12 22:06:02 +00001302 p < q;
1303 p++, q--)
1304 {
Guido van Rossumed98d481991-03-06 13:07:53 +00001305 tmp = *p;
1306 *p = *q;
1307 *q = tmp;
1308 }
1309 }
Guido van Rossumb86c5492001-02-12 22:06:02 +00001310}
1311
1312static PyObject *
1313listreverse(PyListObject *self, PyObject *args)
1314{
1315 if (!PyArg_ParseTuple(args, ":reverse"))
1316 return NULL;
1317 _listreverse(self);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001318 Py_INCREF(Py_None);
1319 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001320}
1321
Guido van Rossum84c76f51990-10-30 13:32:20 +00001322int
Fred Drakea2f55112000-07-09 15:16:51 +00001323PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001324{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001325 if (v == NULL || !PyList_Check(v)) {
1326 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001327 return -1;
1328 }
Guido van Rossumb86c5492001-02-12 22:06:02 +00001329 _listreverse((PyListObject *)v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001330 return 0;
1331}
1332
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001333PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001334PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001335{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001336 PyObject *w;
1337 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001338 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001339 if (v == NULL || !PyList_Check(v)) {
1340 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001341 return NULL;
1342 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001343 n = ((PyListObject *)v)->ob_size;
1344 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001345 if (w == NULL)
1346 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001347 p = ((PyTupleObject *)w)->ob_item;
Thomas Wouters334fb892000-07-25 12:56:38 +00001348 memcpy((void *)p,
1349 (void *)((PyListObject *)v)->ob_item,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001350 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001351 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001352 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001353 p++;
1354 }
1355 return w;
1356}
1357
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001358static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001359listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00001360{
1361 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001362 PyObject *v;
1363
Guido van Rossumef93b872000-03-13 15:41:59 +00001364 if (!PyArg_ParseTuple_Compat1(args, "O:index", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001365 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001366 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001367 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
1368 if (cmp > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001369 return PyInt_FromLong((long)i);
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001370 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001371 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001372 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001373 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001374 return NULL;
1375}
1376
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001377static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001378listcount(PyListObject *self, PyObject *args)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001379{
1380 int count = 0;
1381 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001382 PyObject *v;
1383
Guido van Rossumef93b872000-03-13 15:41:59 +00001384 if (!PyArg_ParseTuple_Compat1(args, "O:count", &v))
Guido van Rossume6f7d181991-10-20 20:20:40 +00001385 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001386 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001387 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
1388 if (cmp > 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001389 count++;
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001390 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001391 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001392 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001393 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001394}
1395
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001396static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001397listremove(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00001398{
1399 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001400 PyObject *v;
1401
Guido van Rossumef93b872000-03-13 15:41:59 +00001402 if (!PyArg_ParseTuple_Compat1(args, "O:remove", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001403 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001404 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001405 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
1406 if (cmp > 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001407 if (list_ass_slice(self, i, i+1,
1408 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001409 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001410 Py_INCREF(Py_None);
1411 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001412 }
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001413 else if (cmp < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001414 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001415 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001416 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001417 return NULL;
1418}
1419
Jeremy Hylton8caad492000-06-23 14:18:11 +00001420static int
1421list_traverse(PyListObject *o, visitproc visit, void *arg)
1422{
1423 int i, err;
1424 PyObject *x;
1425
1426 for (i = o->ob_size; --i >= 0; ) {
1427 x = o->ob_item[i];
1428 if (x != NULL) {
1429 err = visit(x, arg);
1430 if (err)
1431 return err;
1432 }
1433 }
1434 return 0;
1435}
1436
1437static int
1438list_clear(PyListObject *lp)
1439{
1440 (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0);
1441 return 0;
1442}
1443
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001444static PyObject *
1445list_richcompare(PyObject *v, PyObject *w, int op)
1446{
1447 PyListObject *vl, *wl;
1448 int i;
1449
1450 if (!PyList_Check(v) || !PyList_Check(w)) {
1451 Py_INCREF(Py_NotImplemented);
1452 return Py_NotImplemented;
1453 }
1454
1455 vl = (PyListObject *)v;
1456 wl = (PyListObject *)w;
1457
1458 if (vl->ob_size != wl->ob_size && (op == Py_EQ || op == Py_NE)) {
1459 /* Shortcut: if the lengths differ, the lists differ */
1460 PyObject *res;
1461 if (op == Py_EQ)
1462 res = Py_False;
1463 else
1464 res = Py_True;
1465 Py_INCREF(res);
1466 return res;
1467 }
1468
1469 /* Search for the first index where items are different */
1470 for (i = 0; i < vl->ob_size && i < wl->ob_size; i++) {
1471 int k = PyObject_RichCompareBool(vl->ob_item[i],
1472 wl->ob_item[i], Py_EQ);
1473 if (k < 0)
1474 return NULL;
1475 if (!k)
1476 break;
1477 }
1478
1479 if (i >= vl->ob_size || i >= wl->ob_size) {
1480 /* No more items to compare -- compare sizes */
1481 int vs = vl->ob_size;
1482 int ws = wl->ob_size;
1483 int cmp;
1484 PyObject *res;
1485 switch (op) {
1486 case Py_LT: cmp = vs < ws; break;
1487 case Py_LE: cmp = ws <= ws; break;
1488 case Py_EQ: cmp = vs == ws; break;
1489 case Py_NE: cmp = vs != ws; break;
1490 case Py_GT: cmp = vs > ws; break;
1491 case Py_GE: cmp = vs >= ws; break;
1492 default: return NULL; /* cannot happen */
1493 }
1494 if (cmp)
1495 res = Py_True;
1496 else
1497 res = Py_False;
1498 Py_INCREF(res);
1499 return res;
1500 }
1501
1502 /* We have an item that differs -- shortcuts for EQ/NE */
1503 if (op == Py_EQ) {
1504 Py_INCREF(Py_False);
1505 return Py_False;
1506 }
1507 if (op == Py_NE) {
1508 Py_INCREF(Py_True);
1509 return Py_True;
1510 }
1511
1512 /* Compare the final item again using the proper operator */
1513 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
1514}
1515
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001516static char append_doc[] =
1517"L.append(object) -- append object to end";
Barry Warsawdedf6d61998-10-09 16:37:25 +00001518static char extend_doc[] =
1519"L.extend(list) -- extend list by appending list elements";
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001520static char insert_doc[] =
1521"L.insert(index, object) -- insert object before index";
1522static char pop_doc[] =
1523"L.pop([index]) -> item -- remove and return item at index (default last)";
1524static char remove_doc[] =
1525"L.remove(value) -- remove first occurrence of value";
1526static char index_doc[] =
1527"L.index(value) -> integer -- return index of first occurrence of value";
1528static char count_doc[] =
1529"L.count(value) -> integer -- return number of occurrences of value";
1530static char reverse_doc[] =
1531"L.reverse() -- reverse *IN PLACE*";
1532static char sort_doc[] =
1533"L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1534
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001535static PyMethodDef list_methods[] = {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001536 {"append", (PyCFunction)listappend, METH_VARARGS, append_doc},
1537 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
1538 {"extend", (PyCFunction)listextend, METH_VARARGS, extend_doc},
1539 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
1540 {"remove", (PyCFunction)listremove, METH_VARARGS, remove_doc},
1541 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
1542 {"count", (PyCFunction)listcount, METH_VARARGS, count_doc},
Tim Peters0e76ab22000-12-13 22:35:46 +00001543 {"reverse", (PyCFunction)listreverse, METH_VARARGS, reverse_doc},
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001544 {"sort", (PyCFunction)listsort, METH_VARARGS, sort_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001545 {NULL, NULL} /* sentinel */
1546};
1547
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001548static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001549list_getattr(PyListObject *f, char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001550{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001551 return Py_FindMethod(list_methods, (PyObject *)f, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001552}
1553
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001554static PySequenceMethods list_as_sequence = {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001555 (inquiry)list_length, /* sq_length */
1556 (binaryfunc)list_concat, /* sq_concat */
1557 (intargfunc)list_repeat, /* sq_repeat */
1558 (intargfunc)list_item, /* sq_item */
1559 (intintargfunc)list_slice, /* sq_slice */
1560 (intobjargproc)list_ass_item, /* sq_ass_item */
1561 (intintobjargproc)list_ass_slice, /* sq_ass_slice */
1562 (objobjproc)list_contains, /* sq_contains */
1563 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
1564 (intargfunc)list_inplace_repeat, /* sq_inplace_repeat */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001565};
1566
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001567PyTypeObject PyList_Type = {
1568 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001569 0,
1570 "list",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001571 sizeof(PyListObject) + PyGC_HEAD_SIZE,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001572 0,
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001573 (destructor)list_dealloc, /* tp_dealloc */
1574 (printfunc)list_print, /* tp_print */
1575 (getattrfunc)list_getattr, /* tp_getattr */
1576 0, /* tp_setattr */
1577 0, /* tp_compare */
1578 (reprfunc)list_repr, /* tp_repr */
1579 0, /* tp_as_number */
1580 &list_as_sequence, /* tp_as_sequence */
1581 0, /* tp_as_mapping */
1582 0, /* tp_hash */
1583 0, /* tp_call */
1584 0, /* tp_str */
1585 0, /* tp_getattro */
1586 0, /* tp_setattro */
1587 0, /* tp_as_buffer */
1588 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1589 0, /* tp_doc */
1590 (traverseproc)list_traverse, /* tp_traverse */
1591 (inquiry)list_clear, /* tp_clear */
1592 list_richcompare, /* tp_richcompare */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001593};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001594
1595
1596/* During a sort, we really can't have anyone modifying the list; it could
1597 cause core dumps. Thus, we substitute a dummy type that raises an
1598 explanatory exception when a modifying operation is used. Caveat:
1599 comparisons may behave differently; but I guess it's a bad idea anyway to
1600 compare a list that's being sorted... */
1601
1602static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001603immutable_list_op(void)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001604{
1605 PyErr_SetString(PyExc_TypeError,
1606 "a list cannot be modified while it is being sorted");
1607 return NULL;
1608}
1609
1610static PyMethodDef immutable_list_methods[] = {
1611 {"append", (PyCFunction)immutable_list_op},
1612 {"insert", (PyCFunction)immutable_list_op},
1613 {"remove", (PyCFunction)immutable_list_op},
1614 {"index", (PyCFunction)listindex},
1615 {"count", (PyCFunction)listcount},
1616 {"reverse", (PyCFunction)immutable_list_op},
1617 {"sort", (PyCFunction)immutable_list_op},
1618 {NULL, NULL} /* sentinel */
1619};
1620
1621static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001622immutable_list_getattr(PyListObject *f, char *name)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001623{
1624 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
1625}
1626
1627static int
Fred Drakea2f55112000-07-09 15:16:51 +00001628immutable_list_ass(void)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001629{
1630 immutable_list_op();
1631 return -1;
1632}
1633
1634static PySequenceMethods immutable_list_as_sequence = {
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001635 (inquiry)list_length, /* sq_length */
1636 (binaryfunc)list_concat, /* sq_concat */
1637 (intargfunc)list_repeat, /* sq_repeat */
1638 (intargfunc)list_item, /* sq_item */
1639 (intintargfunc)list_slice, /* sq_slice */
1640 (intobjargproc)immutable_list_ass, /* sq_ass_item */
1641 (intintobjargproc)immutable_list_ass, /* sq_ass_slice */
1642 (objobjproc)list_contains, /* sq_contains */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001643};
1644
1645static PyTypeObject immutable_list_type = {
1646 PyObject_HEAD_INIT(&PyType_Type)
1647 0,
1648 "list (immutable, during sort)",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001649 sizeof(PyListObject) + PyGC_HEAD_SIZE,
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001650 0,
Guido van Rossum65e1cea2001-01-17 22:11:59 +00001651 0, /* Cannot happen */ /* tp_dealloc */
1652 (printfunc)list_print, /* tp_print */
1653 (getattrfunc)immutable_list_getattr, /* tp_getattr */
1654 0, /* tp_setattr */
1655 0, /* Won't be called */ /* tp_compare */
1656 (reprfunc)list_repr, /* tp_repr */
1657 0, /* tp_as_number */
1658 &immutable_list_as_sequence, /* tp_as_sequence */
1659 0, /* tp_as_mapping */
1660 0, /* tp_hash */
1661 0, /* tp_call */
1662 0, /* tp_str */
1663 0, /* tp_getattro */
1664 0, /* tp_setattro */
1665 0, /* tp_as_buffer */
1666 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1667 0, /* tp_doc */
1668 (traverseproc)list_traverse, /* tp_traverse */
1669 0, /* tp_clear */
1670 list_richcompare, /* tp_richcompare */
1671 /* NOTE: This is *not* the standard list_type struct! */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001672};