blob: 2cf381f74f367f238e927bc170db23f8b3c4ead6 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* List object implementation */
12
Guido van Rossumc0b618a1997-05-02 03:12:38 +000013#include "Python.h"
14
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000015#ifdef STDC_HEADERS
16#include <stddef.h>
17#else
18#include <sys/types.h> /* For size_t */
19#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020
Guido van Rossumc0b618a1997-05-02 03:12:38 +000021#define ROUNDUP(n, PyTryBlock) \
22 ((((n)+(PyTryBlock)-1)/(PyTryBlock))*(PyTryBlock))
Guido van Rossuma46d51d1995-01-26 22:59:43 +000023
24static int
Guido van Rossuma27d1121997-08-25 18:36:23 +000025roundupsize(n)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000026 int n;
27{
28 if (n < 500)
29 return ROUNDUP(n, 10);
30 else
31 return ROUNDUP(n, 100);
32}
33
Guido van Rossuma27d1121997-08-25 18:36:23 +000034#define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems))
Guido van Rossuma46d51d1995-01-26 22:59:43 +000035
Guido van Rossumc0b618a1997-05-02 03:12:38 +000036PyObject *
37PyList_New(size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038 int size;
39{
40 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000041 PyListObject *op;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000042 size_t nbytes;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043 if (size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000044 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045 return NULL;
46 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000047 nbytes = size * sizeof(PyObject *);
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000048 /* Check for overflow */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049 if (nbytes / sizeof(PyObject *) != (size_t)size) {
50 return PyErr_NoMemory();
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000051 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000052 /* PyObject_NewVar is inlined */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +000053 op = (PyListObject *) PyObject_MALLOC(sizeof(PyListObject)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000054 + PyGC_HEAD_SIZE);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000056 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000058 op = (PyListObject *) PyObject_FROM_GC(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059 if (size <= 0) {
60 op->ob_item = NULL;
61 }
62 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +000063 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +000065 PyObject_FREE(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 }
68 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000069 PyObject_INIT_VAR(op, &PyList_Type, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070 for (i = 0; i < size; i++)
71 op->ob_item[i] = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000072 PyObject_GC_Init(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000073 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074}
75
76int
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077PyList_Size(op)
78 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000080 if (!PyList_Check(op)) {
81 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082 return -1;
83 }
84 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085 return ((PyListObject *)op) -> ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086}
87
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088static PyObject *indexerr;
Guido van Rossum929f1b81996-08-09 20:51:27 +000089
Guido van Rossumc0b618a1997-05-02 03:12:38 +000090PyObject *
91PyList_GetItem(op, i)
92 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 int i;
94{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095 if (!PyList_Check(op)) {
96 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097 return NULL;
98 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000100 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000101 indexerr = PyString_FromString(
102 "list index out of range");
103 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104 return NULL;
105 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000106 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107}
108
109int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110PyList_SetItem(op, i, newitem)
111 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112 register int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113 register PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115 register PyObject *olditem;
116 register PyObject **p;
117 if (!PyList_Check(op)) {
118 Py_XDECREF(newitem);
119 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000120 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000122 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
123 Py_XDECREF(newitem);
124 PyErr_SetString(PyExc_IndexError,
125 "list assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000126 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000128 p = ((PyListObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000129 olditem = *p;
130 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000131 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132 return 0;
133}
134
135static int
136ins1(self, where, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000137 PyListObject *self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000139 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140{
141 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000142 PyObject **items;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000143 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000144 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000145 return -1;
146 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147 items = self->ob_item;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 NRESIZE(items, PyObject *, self->ob_size+1);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000149 if (items == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000151 return -1;
152 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 if (where < 0)
154 where = 0;
155 if (where > self->ob_size)
156 where = self->ob_size;
157 for (i = self->ob_size; --i >= where; )
158 items[i+1] = items[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160 items[where] = v;
161 self->ob_item = items;
162 self->ob_size++;
163 return 0;
164}
165
166int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000167PyList_Insert(op, where, newitem)
168 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000170 PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000172 if (!PyList_Check(op)) {
173 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000174 return -1;
175 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000176 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177}
178
179int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000180PyList_Append(op, newitem)
181 PyObject *op;
182 PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000183{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000184 if (!PyList_Check(op)) {
185 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000186 return -1;
187 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000188 return ins1((PyListObject *)op,
189 (int) ((PyListObject *)op)->ob_size, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190}
191
192/* Methods */
193
194static void
195list_dealloc(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 PyListObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197{
198 int i;
Guido van Rossumd724b232000-03-13 16:01:29 +0000199 Py_TRASHCAN_SAFE_BEGIN(op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000200 PyObject_GC_Fini(op);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000201 if (op->ob_item != NULL) {
Guido van Rossumfa717011999-06-09 15:19:34 +0000202 /* Do it backwards, for Christian Tismer.
203 There's a simple test case where somehow this reduces
204 thrashing when a *very* large list is created and
205 immediately deleted. */
206 i = op->ob_size;
207 while (--i >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 Py_XDECREF(op->ob_item[i]);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000209 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000210 PyMem_FREE(op->ob_item);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000211 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000212 PyObject_DEL(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000213 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214}
215
Guido van Rossum90933611991-06-07 16:10:43 +0000216static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217list_print(op, fp, flags)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218 PyListObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219 FILE *fp;
220 int flags;
221{
222 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000223
224 i = Py_ReprEnter((PyObject*)op);
225 if (i != 0) {
226 if (i < 0)
227 return i;
228 fprintf(fp, "[...]");
229 return 0;
230 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231 fprintf(fp, "[");
Guido van Rossum90933611991-06-07 16:10:43 +0000232 for (i = 0; i < op->ob_size; i++) {
233 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234 fprintf(fp, ", ");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000235 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
236 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000237 return -1;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000238 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239 }
240 fprintf(fp, "]");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000241 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000242 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243}
244
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000245static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246list_repr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000247 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
272list_compare(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000273 PyListObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275 int i;
Guido van Rossumae621ff1998-05-28 20:18:46 +0000276 for (i = 0; i < v->ob_size && i < w->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000277 int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278 if (cmp != 0)
279 return cmp;
280 }
281 return v->ob_size - w->ob_size;
282}
283
284static int
285list_length(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000286 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287{
288 return a->ob_size;
289}
290
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000291
292
293static int
294list_contains(a, el)
295 PyListObject *a;
296 PyObject *el;
297{
298 int i, cmp;
299
300 for (i = 0; i < a->ob_size; ++i) {
301 cmp = PyObject_Compare(el, PyList_GET_ITEM(a, i));
302 if (cmp == 0)
303 return 1;
304 if (PyErr_Occurred())
305 return -1;
306 }
307 return 0;
308}
309
310
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000312list_item(a, i)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000313 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314 int i;
315{
316 if (i < 0 || i >= a->ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000317 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000318 indexerr = PyString_FromString(
319 "list index out of range");
320 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321 return NULL;
322 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000323 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324 return a->ob_item[i];
325}
326
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000327static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328list_slice(a, ilow, ihigh)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000329 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330 int ilow, ihigh;
331{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000332 PyListObject *np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333 int i;
334 if (ilow < 0)
335 ilow = 0;
336 else if (ilow > a->ob_size)
337 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338 if (ihigh < ilow)
339 ihigh = ilow;
340 else if (ihigh > a->ob_size)
341 ihigh = a->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000342 np = (PyListObject *) PyList_New(ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343 if (np == NULL)
344 return NULL;
345 for (i = ilow; i < ihigh; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000346 PyObject *v = a->ob_item[i];
347 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348 np->ob_item[i - ilow] = v;
349 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000350 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351}
352
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000353PyObject *
354PyList_GetSlice(a, ilow, ihigh)
355 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000356 int ilow, ihigh;
357{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000358 if (!PyList_Check(a)) {
359 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000360 return NULL;
361 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000362 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000363}
364
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000365static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366list_concat(a, bb)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000367 PyListObject *a;
368 PyObject *bb;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369{
370 int size;
371 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000372 PyListObject *np;
373 if (!PyList_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000374 PyErr_Format(PyExc_TypeError,
Fred Drake914a2ed2000-06-01 14:31:03 +0000375 "can only concatenate list (not \"%.200s\") to list",
376 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377 return NULL;
378 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000379#define b ((PyListObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000380 size = a->ob_size + b->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000381 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000383 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384 }
385 for (i = 0; i < a->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386 PyObject *v = a->ob_item[i];
387 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388 np->ob_item[i] = v;
389 }
390 for (i = 0; i < b->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000391 PyObject *v = b->ob_item[i];
392 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393 np->ob_item[i + a->ob_size] = v;
394 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000395 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396#undef b
397}
398
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000399static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +0000400list_repeat(a, n)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000401 PyListObject *a;
Guido van Rossumed98d481991-03-06 13:07:53 +0000402 int n;
403{
404 int i, j;
405 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000406 PyListObject *np;
407 PyObject **p;
Guido van Rossumed98d481991-03-06 13:07:53 +0000408 if (n < 0)
409 n = 0;
410 size = a->ob_size * n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000411 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000412 if (np == NULL)
413 return NULL;
414 p = np->ob_item;
415 for (i = 0; i < n; i++) {
416 for (j = 0; j < a->ob_size; j++) {
417 *p = a->ob_item[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000418 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000419 p++;
420 }
421 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000422 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000423}
424
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426list_ass_slice(a, ilow, ihigh, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000427 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000429 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000431 /* Because [X]DECREF can recursively invoke list operations on
432 this list, we must postpone all [X]DECREF activity until
433 after the list is back in its canonical shape. Therefore
434 we must allocate an additional array, 'recycle', into which
435 we temporarily copy the items that are deleted from the
436 list. :-( */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000437 PyObject **recycle, **p;
438 PyObject **item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439 int n; /* Size of replacement list */
440 int d; /* Change in size */
441 int k; /* Loop index */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443 if (v == NULL)
444 n = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445 else if (PyList_Check(v)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446 n = b->ob_size;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000447 if (a == b) {
448 /* Special case "a[i:j] = a" -- copy b first */
449 int ret;
450 v = list_slice(b, 0, n);
451 ret = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000452 Py_DECREF(v);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000453 return ret;
454 }
455 }
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000456 else {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000457 PyErr_Format(PyExc_TypeError,
458 "must assign list (not \"%.200s\") to slice",
459 v->ob_type->tp_name);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000460 return -1;
461 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000462 if (ilow < 0)
463 ilow = 0;
464 else if (ilow > a->ob_size)
465 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466 if (ihigh < ilow)
467 ihigh = ilow;
468 else if (ihigh > a->ob_size)
469 ihigh = a->ob_size;
470 item = a->ob_item;
471 d = n - (ihigh-ilow);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000472 if (ihigh > ilow)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473 p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000474 else
475 p = recycle = NULL;
476 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477 for (k = ilow; k < ihigh; k++)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000478 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479 if (d < 0) {
480 for (/*k = ihigh*/; k < a->ob_size; k++)
481 item[k+d] = item[k];
482 a->ob_size += d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484 a->ob_item = item;
485 }
486 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000487 else { /* Insert d items; recycle ihigh-ilow items */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000488 NRESIZE(item, PyObject *, a->ob_size + d);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000489 if (item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000490 if (recycle != NULL)
491 PyMem_DEL(recycle);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000492 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000493 return -1;
494 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000495 for (k = a->ob_size; --k >= ihigh; )
496 item[k+d] = item[k];
497 for (/*k = ihigh-1*/; k >= ilow; --k)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000498 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000499 a->ob_item = item;
500 a->ob_size += d;
501 }
502 for (k = 0; k < n; k++, ilow++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000503 PyObject *w = b->ob_item[k];
504 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000505 item[ilow] = w;
506 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000507 if (recycle) {
508 while (--p >= recycle)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000509 Py_XDECREF(*p);
510 PyMem_DEL(recycle);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000511 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512 return 0;
513#undef b
514}
515
Guido van Rossum234f9421993-06-17 12:35:49 +0000516int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000517PyList_SetSlice(a, ilow, ihigh, v)
518 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000519 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000520 PyObject *v;
Guido van Rossum234f9421993-06-17 12:35:49 +0000521{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000522 if (!PyList_Check(a)) {
523 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000524 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000525 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000527}
528
Guido van Rossum4a450d01991-04-03 19:05:18 +0000529static int
530list_ass_item(a, i, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000531 PyListObject *a;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000532 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000533 PyObject *v;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000534{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000535 PyObject *old_value;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000536 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537 PyErr_SetString(PyExc_IndexError,
538 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000539 return -1;
540 }
541 if (v == NULL)
542 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000543 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000544 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000545 a->ob_item[i] = v;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000546 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000547 return 0;
548}
549
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000550static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551ins(self, where, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552 PyListObject *self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555{
556 if (ins1(self, where, v) != 0)
557 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 Py_INCREF(Py_None);
559 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560}
561
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563listinsert(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564 PyListObject *self;
565 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566{
567 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000569 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000570 return NULL;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000571 return ins(self, i, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572}
573
Guido van Rossumef93b872000-03-13 15:41:59 +0000574/* Define NO_STRICT_LIST_APPEND to enable multi-argument append() */
575
576#ifndef NO_STRICT_LIST_APPEND
577#define PyArg_ParseTuple_Compat1 PyArg_ParseTuple
578#else
579#define PyArg_ParseTuple_Compat1(args, format, ret) \
580( \
581 PyTuple_GET_SIZE(args) > 1 ? (*ret = args, 1) : \
582 PyTuple_GET_SIZE(args) == 1 ? (*ret = PyTuple_GET_ITEM(args, 0), 1) : \
583 PyArg_ParseTuple(args, format, ret) \
584)
585#endif
586
587
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000588static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589listappend(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590 PyListObject *self;
591 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000592{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593 PyObject *v;
Guido van Rossumef93b872000-03-13 15:41:59 +0000594 if (!PyArg_ParseTuple_Compat1(args, "O:append", &v))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000595 return NULL;
596 return ins(self, (int) self->ob_size, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000597}
598
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000599static PyObject *
Barry Warsawdedf6d61998-10-09 16:37:25 +0000600listextend(self, args)
601 PyListObject *self;
602 PyObject *args;
603{
604 PyObject *b = NULL, *res = NULL;
605 PyObject **items;
606 int selflen = PyList_GET_SIZE(self);
607 int blen;
608 register int i;
609
Guido van Rossumc00a9382000-02-24 21:48:29 +0000610 if (!PyArg_ParseTuple(args, "O:extend", &b))
Barry Warsawdedf6d61998-10-09 16:37:25 +0000611 return NULL;
612
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000613 b = PySequence_Fast(b, "list.extend() argument must be a sequence");
614 if (!b)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000615 return NULL;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000616
617 if (PyObject_Length(b) == 0)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000618 /* short circuit when b is empty */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000619 goto ok;
620
Barry Warsawdedf6d61998-10-09 16:37:25 +0000621 if (self == (PyListObject*)b) {
622 /* as in list_ass_slice() we must special case the
623 * situation: a.extend(a)
624 *
625 * XXX: I think this way ought to be faster than using
626 * list_slice() the way list_ass_slice() does.
627 */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000628 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000629 b = PyList_New(selflen);
630 if (!b)
631 return NULL;
632 for (i = 0; i < selflen; i++) {
633 PyObject *o = PyList_GET_ITEM(self, i);
634 Py_INCREF(o);
635 PyList_SET_ITEM(b, i, o);
636 }
637 }
Barry Warsawdedf6d61998-10-09 16:37:25 +0000638
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000639 blen = PyObject_Length(b);
640
Barry Warsawdedf6d61998-10-09 16:37:25 +0000641 /* resize a using idiom */
642 items = self->ob_item;
643 NRESIZE(items, PyObject*, selflen + blen);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000644 if (items == NULL) {
Barry Warsawdedf6d61998-10-09 16:37:25 +0000645 PyErr_NoMemory();
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000646 goto failed;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000647 }
648 self->ob_item = items;
649
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000650 /* populate the end of self with b's items */
Barry Warsawdedf6d61998-10-09 16:37:25 +0000651 for (i = 0; i < blen; i++) {
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000652 PyObject *o = PySequence_Fast_GET_ITEM(b, i);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000653 Py_INCREF(o);
654 PyList_SET_ITEM(self, self->ob_size++, o);
655 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000656 ok:
Barry Warsawdedf6d61998-10-09 16:37:25 +0000657 res = Py_None;
658 Py_INCREF(res);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000659 failed:
660 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000661 return res;
662}
663
664
665static PyObject *
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000666listpop(self, args)
667 PyListObject *self;
668 PyObject *args;
669{
670 int i = -1;
671 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000672 if (!PyArg_ParseTuple(args, "|i:pop", &i))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000673 return NULL;
674 if (self->ob_size == 0) {
675 /* Special-case most common failure cause */
676 PyErr_SetString(PyExc_IndexError, "pop from empty list");
677 return NULL;
678 }
679 if (i < 0)
680 i += self->ob_size;
681 if (i < 0 || i >= self->ob_size) {
682 PyErr_SetString(PyExc_IndexError, "pop index out of range");
683 return NULL;
684 }
685 v = self->ob_item[i];
686 Py_INCREF(v);
687 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
688 Py_DECREF(v);
689 return NULL;
690 }
691 return v;
692}
693
Guido van Rossum3f236de1996-12-10 23:55:39 +0000694/* New quicksort implementation for arrays of object pointers.
695 Thanks to discussions with Tim Peters. */
696
697/* CMPERROR is returned by our comparison function when an error
698 occurred. This is the largest negative integer (0x80000000 on a
699 32-bit system). */
Guido van Rossum19700b61997-03-05 00:45:43 +0000700#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
Guido van Rossum3f236de1996-12-10 23:55:39 +0000701
702/* Comparison function. Takes care of calling a user-supplied
703 comparison function (any callable Python object). Calls the
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000704 standard comparison function, PyObject_Compare(), if the user-
705 supplied function is NULL. */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000706
707static int
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000708docompare(x, y, compare)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000709 PyObject *x;
710 PyObject *y;
711 PyObject *compare;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000712{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000713 PyObject *args, *res;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000714 int i;
715
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000716 if (compare == NULL) {
717 i = PyObject_Compare(x, y);
718 if (i && PyErr_Occurred())
719 i = CMPERROR;
720 return i;
721 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000722
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723 args = Py_BuildValue("(OO)", x, y);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000724 if (args == NULL)
725 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000726 res = PyEval_CallObject(compare, args);
727 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000728 if (res == NULL)
729 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000730 if (!PyInt_Check(res)) {
731 Py_DECREF(res);
732 PyErr_SetString(PyExc_TypeError,
Guido van Rossum9bcd1d71999-04-19 17:44:39 +0000733 "comparison function must return int");
Guido van Rossum3f236de1996-12-10 23:55:39 +0000734 return CMPERROR;
735 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736 i = PyInt_AsLong(res);
737 Py_DECREF(res);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000738 if (i < 0)
739 return -1;
740 if (i > 0)
741 return 1;
742 return 0;
743}
744
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000745/* MINSIZE is the smallest array that will get a full-blown samplesort
746 treatment; smaller arrays are sorted using binary insertion. It must
747 be at least 7 for the samplesort implementation to work. Binary
748 insertion does fewer compares, but can suffer O(N**2) data movement.
749 The more expensive compares, the larger MINSIZE should be. */
750#define MINSIZE 100
751
752/* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
753 partition; smaller slices are passed to binarysort. It must be at
754 least 2, and no larger than MINSIZE. Setting it higher reduces the #
755 of compares slowly, but increases the amount of data movement quickly.
756 The value here was chosen assuming a compare costs ~25x more than
757 swapping a pair of memory-resident pointers -- but under that assumption,
758 changing the value by a few dozen more or less has aggregate effect
759 under 1%. So the value is crucial, but not touchy <wink>. */
760#define MINPARTITIONSIZE 40
761
762/* MAXMERGE is the largest number of elements we'll always merge into
763 a known-to-be sorted chunk via binary insertion, regardless of the
764 size of that chunk. Given a chunk of N sorted elements, and a group
765 of K unknowns, the largest K for which it's better to do insertion
766 (than a full-blown sort) is a complicated function of N and K mostly
767 involving the expected number of compares and data moves under each
768 approach, and the relative cost of those operations on a specific
769 architecure. The fixed value here is conservative, and should be a
770 clear win regardless of architecture or N. */
771#define MAXMERGE 15
Guido van Rossum3f236de1996-12-10 23:55:39 +0000772
Guido van Rossum3f236de1996-12-10 23:55:39 +0000773/* STACKSIZE is the size of our work stack. A rough estimate is that
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000774 this allows us to sort arrays of size N where
775 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
776 for arrays of size 2**64. Because we push the biggest partition
777 first, the worst case occurs when all subarrays are always partitioned
778 exactly in two. */
779#define STACKSIZE 60
Guido van Rossum3f236de1996-12-10 23:55:39 +0000780
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000781
782#define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
783
784/* binarysort is the best method for sorting small arrays: it does
785 few compares, but can do data movement quadratic in the number of
786 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000787 [lo, hi) is a contiguous slice of a list, and is sorted via
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000788 binary insertion.
789 On entry, must have lo <= start <= hi, and that [lo, start) is already
790 sorted (pass start == lo if you don't know!).
791 If docompare complains (returns CMPERROR) return -1, else 0.
792 Even in case of error, the output slice will be some permutation of
793 the input (nothing is lost or duplicated).
794*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000795
796static int
Guido van Rossum42812581998-06-17 14:15:44 +0000797binarysort(lo, hi, start, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000798 PyObject **lo;
799 PyObject **hi;
800 PyObject **start;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000801 PyObject *compare;/* Comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000802{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000803 /* assert lo <= start <= hi
804 assert [lo, start) is sorted */
805 register int k;
806 register PyObject **l, **p, **r;
807 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000808
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000809 if (lo == start)
810 ++start;
811 for (; start < hi; ++start) {
812 /* set l to where *start belongs */
813 l = lo;
814 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000815 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000816 do {
817 p = l + ((r - l) >> 1);
818 SETK(pivot, *p);
819 if (k < 0)
820 r = p;
821 else
822 l = p + 1;
823 } while (l < r);
824 /* Pivot should go at l -- slide over to make room.
825 Caution: using memmove is much slower under MSVC 5;
826 we're not usually moving many slots. */
827 for (p = start; p > l; --p)
828 *p = *(p-1);
829 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000830 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000831 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000832
833 fail:
834 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000835}
836
837/* samplesortslice is the sorting workhorse.
Guido van Rossum42812581998-06-17 14:15:44 +0000838 [lo, hi) is a contiguous slice of a list, to be sorted in place.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000839 On entry, must have lo <= hi,
840 If docompare complains (returns CMPERROR) return -1, else 0.
841 Even in case of error, the output slice will be some permutation of
842 the input (nothing is lost or duplicated).
843
844 samplesort is basically quicksort on steroids: a power of 2 close
845 to n/ln(n) is computed, and that many elements (less 1) are picked at
846 random from the array and sorted. These 2**k - 1 elements are then
847 used as preselected pivots for an equal number of quicksort
848 partitioning steps, partitioning the slice into 2**k chunks each of
849 size about ln(n). These small final chunks are then usually handled
850 by binarysort. Note that when k=1, this is roughly the same as an
851 ordinary quicksort using a random pivot, and when k=2 this is roughly
852 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
853 this a "median of n/ln(n)" quicksort. You can also view it as a kind
854 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
855
856 The large number of samples makes a quadratic-time case almost
857 impossible, and asymptotically drives the average-case number of
858 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
859 3 variant) down to N lg N.
860
861 We also play lots of low-level tricks to cut the number of compares.
862
863 Very obscure: To avoid using extra memory, the PPs are stored in the
864 array and shuffled around as partitioning proceeds. At the start of a
865 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
866 adjacent (either on the left or the right!) to a chunk of X elements
867 that are to be partitioned: P X or X P. In either case we need to
868 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
869 left, followed by the PP to be used for this step (that's the middle
870 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
871 P X or X P -> Psmall pivot X Plarge
872 and the order of the PPs must not be altered. It can take a while
873 to realize this isn't trivial! It can take even longer <wink> to
874 understand why the simple code below works, using only 2**(m-1) swaps.
875 The key is that the order of the X elements isn't necessarily
876 preserved: X can end up as some cyclic permutation of its original
877 order. That's OK, because X is unsorted anyway. If the order of X
878 had to be preserved too, the simplest method I know of using O(1)
879 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
880 Since len(X) is typically several times larger than 2**(m-1), that
881 would slow things down.
882*/
883
884struct SamplesortStackNode {
885 /* Represents a slice of the array, from (& including) lo up
886 to (but excluding) hi. "extra" additional & adjacent elements
887 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
888 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
889 already sorted, but nothing is known about the other elements
890 in [lo, hi). |extra| is always one less than a power of 2.
891 When extra is 0, we're out of PPs, and the slice must be
892 sorted by some other means. */
893 PyObject **lo;
894 PyObject **hi;
895 int extra;
896};
897
898/* 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 +0000899 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000900 is undesirable, so cutoff values are canned in the "cutoff" table
901 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
902#define CUTOFFBASE 4
Guido van Rossum3aa23fd1999-01-14 19:01:53 +0000903static long cutoff[] = {
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000904 43, /* smallest N such that k == 4 */
905 106, /* etc */
906 250,
907 576,
908 1298,
909 2885,
910 6339,
911 13805,
912 29843,
913 64116,
914 137030,
915 291554,
916 617916,
917 1305130,
918 2748295,
919 5771662,
920 12091672,
921 25276798,
922 52734615,
923 109820537,
924 228324027,
925 473977813,
926 982548444, /* smallest N such that k == 26 */
927 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
928};
929
930static int
Guido van Rossum42812581998-06-17 14:15:44 +0000931samplesortslice(lo, hi, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000932 PyObject **lo;
933 PyObject **hi;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000934 PyObject *compare;/* Comparison function object, or NULL for default */
935{
936 register PyObject **l, **r;
937 register PyObject *tmp, *pivot;
938 register int k;
939 int n, extra, top, extraOnRight;
940 struct SamplesortStackNode stack[STACKSIZE];
941
942 /* assert lo <= hi */
943 n = hi - lo;
944
945 /* ----------------------------------------------------------
946 * Special cases
947 * --------------------------------------------------------*/
948 if (n < 2)
949 return 0;
950
951 /* Set r to the largest value such that [lo,r) is sorted.
952 This catches the already-sorted case, the all-the-same
953 case, and the appended-a-few-elements-to-a-sorted-list case.
954 If the array is unsorted, we're very likely to get out of
955 the loop fast, so the test is cheap if it doesn't pay off.
956 */
957 /* assert lo < hi */
958 for (r = lo+1; r < hi; ++r) {
959 SETK(*r, *(r-1));
960 if (k < 0)
961 break;
962 }
963 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
964 few unknowns, or few elements in total. */
965 if (hi - r <= MAXMERGE || n < MINSIZE)
Guido van Rossum42812581998-06-17 14:15:44 +0000966 return binarysort(lo, hi, r, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000967
968 /* Check for the array already being reverse-sorted. Typical
969 benchmark-driven silliness <wink>. */
970 /* assert lo < hi */
971 for (r = lo+1; r < hi; ++r) {
972 SETK(*(r-1), *r);
973 if (k < 0)
974 break;
975 }
976 if (hi - r <= MAXMERGE) {
977 /* Reverse the reversed prefix, then insert the tail */
978 PyObject **originalr = r;
979 l = lo;
980 do {
981 --r;
982 tmp = *l; *l = *r; *r = tmp;
983 ++l;
984 } while (l < r);
Guido van Rossum42812581998-06-17 14:15:44 +0000985 return binarysort(lo, hi, originalr, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000986 }
987
988 /* ----------------------------------------------------------
989 * Normal case setup: a large array without obvious pattern.
990 * --------------------------------------------------------*/
991
992 /* extra := a power of 2 ~= n/ln(n), less 1.
993 First find the smallest extra s.t. n < cutoff[extra] */
994 for (extra = 0;
995 extra < sizeof(cutoff) / sizeof(cutoff[0]);
996 ++extra) {
997 if (n < cutoff[extra])
998 break;
999 /* note that if we fall out of the loop, the value of
1000 extra still makes *sense*, but may be smaller than
1001 we would like (but the array has more than ~= 2**31
1002 elements in this case!) */
1003 }
1004 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1005 have is CUTOFFBASE-1, so
1006 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1007 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
1008 /* assert extra > 0 and n >= extra */
1009
1010 /* Swap that many values to the start of the array. The
1011 selection of elements is pseudo-random, but the same on
1012 every run (this is intentional! timing algorithm changes is
1013 a pain if timing varies across runs). */
1014 {
1015 unsigned int seed = n / extra; /* arbitrary */
1016 unsigned int i;
1017 for (i = 0; i < (unsigned)extra; ++i) {
1018 /* j := random int in [i, n) */
1019 unsigned int j;
1020 seed = seed * 69069 + 7;
1021 j = i + seed % (n - i);
1022 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
1023 }
1024 }
1025
1026 /* Recursively sort the preselected pivots. */
Guido van Rossum42812581998-06-17 14:15:44 +00001027 if (samplesortslice(lo, lo + extra, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001028 goto fail;
1029
1030 top = 0; /* index of available stack slot */
1031 lo += extra; /* point to first unknown */
1032 extraOnRight = 0; /* the PPs are at the left end */
1033
1034 /* ----------------------------------------------------------
1035 * Partition [lo, hi), and repeat until out of work.
1036 * --------------------------------------------------------*/
1037 for (;;) {
1038 /* assert lo <= hi, so n >= 0 */
1039 n = hi - lo;
1040
1041 /* We may not want, or may not be able, to partition:
1042 If n is small, it's quicker to insert.
1043 If extra is 0, we're out of pivots, and *must* use
1044 another method.
1045 */
1046 if (n < MINPARTITIONSIZE || extra == 0) {
1047 if (n >= MINSIZE) {
1048 /* assert extra == 0
1049 This is rare, since the average size
1050 of a final block is only about
1051 ln(original n). */
Guido van Rossum42812581998-06-17 14:15:44 +00001052 if (samplesortslice(lo, hi, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001053 goto fail;
1054 }
1055 else {
1056 /* Binary insertion should be quicker,
1057 and we can take advantage of the PPs
1058 already being sorted. */
1059 if (extraOnRight && extra) {
1060 /* swap the PPs to the left end */
1061 k = extra;
1062 do {
1063 tmp = *lo;
1064 *lo = *hi;
1065 *hi = tmp;
1066 ++lo; ++hi;
1067 } while (--k);
1068 }
1069 if (binarysort(lo - extra, hi, lo,
Guido van Rossum42812581998-06-17 14:15:44 +00001070 compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001071 goto fail;
1072 }
1073
1074 /* Find another slice to work on. */
1075 if (--top < 0)
1076 break; /* no more -- done! */
1077 lo = stack[top].lo;
1078 hi = stack[top].hi;
1079 extra = stack[top].extra;
1080 extraOnRight = 0;
1081 if (extra < 0) {
1082 extraOnRight = 1;
1083 extra = -extra;
1084 }
1085 continue;
1086 }
1087
1088 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1089 Then our preselected pivot is at (extra-1)/2, and we
1090 want to move the PPs before that to the left end of
1091 the slice, and the PPs after that to the right end.
1092 The following section changes extra, lo, hi, and the
1093 slice such that:
1094 [lo-extra, lo) contains the smaller PPs.
1095 *lo == our PP.
1096 (lo, hi) contains the unknown elements.
1097 [hi, hi+extra) contains the larger PPs.
1098 */
1099 k = extra >>= 1; /* num PPs to move */
1100 if (extraOnRight) {
1101 /* Swap the smaller PPs to the left end.
1102 Note that this loop actually moves k+1 items:
1103 the last is our PP */
1104 do {
1105 tmp = *lo; *lo = *hi; *hi = tmp;
1106 ++lo; ++hi;
1107 } while (k--);
1108 }
1109 else {
1110 /* Swap the larger PPs to the right end. */
1111 while (k--) {
1112 --lo; --hi;
1113 tmp = *lo; *lo = *hi; *hi = tmp;
1114 }
1115 }
1116 --lo; /* *lo is now our PP */
1117 pivot = *lo;
1118
1119 /* Now an almost-ordinary quicksort partition step.
1120 Note that most of the time is spent here!
1121 Only odd thing is that we partition into < and >=,
1122 instead of the usual <= and >=. This helps when
1123 there are lots of duplicates of different values,
1124 because it eventually tends to make subfiles
1125 "pure" (all duplicates), and we special-case for
1126 duplicates later. */
1127 l = lo + 1;
1128 r = hi - 1;
1129 /* assert lo < l < r < hi (small n weeded out above) */
1130
1131 do {
1132 /* slide l right, looking for key >= pivot */
1133 do {
1134 SETK(*l, pivot);
1135 if (k < 0)
1136 ++l;
1137 else
1138 break;
1139 } while (l < r);
1140
1141 /* slide r left, looking for key < pivot */
1142 while (l < r) {
Guido van Rossum42812581998-06-17 14:15:44 +00001143 register PyObject *rval = *r--;
1144 SETK(rval, pivot);
1145 if (k < 0) {
1146 /* swap and advance */
1147 r[1] = *l;
1148 *l++ = rval;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001149 break;
Guido van Rossum42812581998-06-17 14:15:44 +00001150 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001151 }
1152
1153 } while (l < r);
1154
1155 /* assert lo < r <= l < hi
1156 assert r == l or r+1 == l
1157 everything to the left of l is < pivot, and
1158 everything to the right of r is >= pivot */
1159
1160 if (l == r) {
1161 SETK(*r, pivot);
1162 if (k < 0)
1163 ++l;
1164 else
1165 --r;
1166 }
1167 /* assert lo <= r and r+1 == l and l <= hi
1168 assert r == lo or a[r] < pivot
1169 assert a[lo] is pivot
1170 assert l == hi or a[l] >= pivot
1171 Swap the pivot into "the middle", so we can henceforth
1172 ignore it.
1173 */
1174 *lo = *r;
1175 *r = pivot;
1176
1177 /* The following is true now, & will be preserved:
1178 All in [lo,r) are < pivot
1179 All in [r,l) == pivot (& so can be ignored)
1180 All in [l,hi) are >= pivot */
1181
1182 /* Check for duplicates of the pivot. One compare is
1183 wasted if there are no duplicates, but can win big
1184 when there are.
1185 Tricky: we're sticking to "<" compares, so deduce
1186 equality indirectly. We know pivot <= *l, so they're
1187 equal iff not pivot < *l.
1188 */
1189 while (l < hi) {
1190 /* pivot <= *l known */
1191 SETK(pivot, *l);
1192 if (k < 0)
1193 break;
1194 else
1195 /* <= and not < implies == */
1196 ++l;
1197 }
1198
1199 /* assert lo <= r < l <= hi
1200 Partitions are [lo, r) and [l, hi) */
1201
1202 /* push fattest first; remember we still have extra PPs
1203 to the left of the left chunk and to the right of
1204 the right chunk! */
1205 /* assert top < STACKSIZE */
1206 if (r - lo <= hi - l) {
1207 /* second is bigger */
1208 stack[top].lo = l;
1209 stack[top].hi = hi;
1210 stack[top].extra = -extra;
1211 hi = r;
1212 extraOnRight = 0;
1213 }
1214 else {
1215 /* first is bigger */
1216 stack[top].lo = lo;
1217 stack[top].hi = r;
1218 stack[top].extra = extra;
1219 lo = l;
1220 extraOnRight = 1;
1221 }
1222 ++top;
1223
1224 } /* end of partitioning loop */
1225
1226 return 0;
1227
1228 fail:
1229 return -1;
1230}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001231
1232#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001233
1234staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001235
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001236static PyObject *
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001237listsort(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001238 PyListObject *self;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001239 PyObject *args;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001240{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001241 int err;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001242 PyObject *compare = NULL;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001243
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001244 if (args != NULL) {
Guido van Rossumc00a9382000-02-24 21:48:29 +00001245 if (!PyArg_ParseTuple(args, "|O:sort", &compare))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001246 return NULL;
1247 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001248 self->ob_type = &immutable_list_type;
1249 err = samplesortslice(self->ob_item,
1250 self->ob_item + self->ob_size,
Guido van Rossum42812581998-06-17 14:15:44 +00001251 compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001252 self->ob_type = &PyList_Type;
1253 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001254 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001255 Py_INCREF(Py_None);
1256 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001257}
1258
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001259int
1260PyList_Sort(v)
1261 PyObject *v;
1262{
1263 if (v == NULL || !PyList_Check(v)) {
1264 PyErr_BadInternalCall();
1265 return -1;
1266 }
1267 v = listsort((PyListObject *)v, (PyObject *)NULL);
1268 if (v == NULL)
1269 return -1;
1270 Py_DECREF(v);
1271 return 0;
1272}
1273
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001274static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001275listreverse(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001276 PyListObject *self;
1277 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001278{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001279 register PyObject **p, **q;
1280 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001281
Guido van Rossumc00a9382000-02-24 21:48:29 +00001282 if (!PyArg_ParseTuple(args, ":reverse"))
Guido van Rossumed98d481991-03-06 13:07:53 +00001283 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001284
1285 if (self->ob_size > 1) {
1286 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
1287 p < q; p++, q--) {
1288 tmp = *p;
1289 *p = *q;
1290 *q = tmp;
1291 }
1292 }
1293
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001294 Py_INCREF(Py_None);
1295 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001296}
1297
Guido van Rossum84c76f51990-10-30 13:32:20 +00001298int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001299PyList_Reverse(v)
1300 PyObject *v;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001301{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001302 if (v == NULL || !PyList_Check(v)) {
1303 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001304 return -1;
1305 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001306 v = listreverse((PyListObject *)v, (PyObject *)NULL);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001307 if (v == NULL)
1308 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001309 Py_DECREF(v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001310 return 0;
1311}
1312
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001313PyObject *
1314PyList_AsTuple(v)
1315 PyObject *v;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001316{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001317 PyObject *w;
1318 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001319 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001320 if (v == NULL || !PyList_Check(v)) {
1321 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001322 return NULL;
1323 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001324 n = ((PyListObject *)v)->ob_size;
1325 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001326 if (w == NULL)
1327 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001328 p = ((PyTupleObject *)w)->ob_item;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001329 memcpy((ANY *)p,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001330 (ANY *)((PyListObject *)v)->ob_item,
1331 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001332 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001333 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001334 p++;
1335 }
1336 return w;
1337}
1338
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001339static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001340listindex(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001341 PyListObject *self;
1342 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001343{
1344 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001345 PyObject *v;
1346
Guido van Rossumef93b872000-03-13 15:41:59 +00001347 if (!PyArg_ParseTuple_Compat1(args, "O:index", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001348 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001349 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001350 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001351 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001352 if (PyErr_Occurred())
1353 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001354 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001355 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001356 return NULL;
1357}
1358
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001359static PyObject *
Guido van Rossume6f7d181991-10-20 20:20:40 +00001360listcount(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001361 PyListObject *self;
1362 PyObject *args;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001363{
1364 int count = 0;
1365 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001366 PyObject *v;
1367
Guido van Rossumef93b872000-03-13 15:41:59 +00001368 if (!PyArg_ParseTuple_Compat1(args, "O:count", &v))
Guido van Rossume6f7d181991-10-20 20:20:40 +00001369 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001370 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001371 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001372 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001373 if (PyErr_Occurred())
1374 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001375 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001376 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001377}
1378
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001379static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001380listremove(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001381 PyListObject *self;
1382 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001383{
1384 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001385 PyObject *v;
1386
Guido van Rossumef93b872000-03-13 15:41:59 +00001387 if (!PyArg_ParseTuple_Compat1(args, "O:remove", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001388 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001389 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001390 if (PyObject_Compare(self->ob_item[i], v) == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001391 if (list_ass_slice(self, i, i+1,
1392 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001393 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001394 Py_INCREF(Py_None);
1395 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001396 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001397 if (PyErr_Occurred())
1398 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001399 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001400 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001401 return NULL;
1402}
1403
Jeremy Hylton8caad492000-06-23 14:18:11 +00001404static int
1405list_traverse(PyListObject *o, visitproc visit, void *arg)
1406{
1407 int i, err;
1408 PyObject *x;
1409
1410 for (i = o->ob_size; --i >= 0; ) {
1411 x = o->ob_item[i];
1412 if (x != NULL) {
1413 err = visit(x, arg);
1414 if (err)
1415 return err;
1416 }
1417 }
1418 return 0;
1419}
1420
1421static int
1422list_clear(PyListObject *lp)
1423{
1424 (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0);
1425 return 0;
1426}
1427
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001428static char append_doc[] =
1429"L.append(object) -- append object to end";
Barry Warsawdedf6d61998-10-09 16:37:25 +00001430static char extend_doc[] =
1431"L.extend(list) -- extend list by appending list elements";
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001432static char insert_doc[] =
1433"L.insert(index, object) -- insert object before index";
1434static char pop_doc[] =
1435"L.pop([index]) -> item -- remove and return item at index (default last)";
1436static char remove_doc[] =
1437"L.remove(value) -- remove first occurrence of value";
1438static char index_doc[] =
1439"L.index(value) -> integer -- return index of first occurrence of value";
1440static char count_doc[] =
1441"L.count(value) -> integer -- return number of occurrences of value";
1442static char reverse_doc[] =
1443"L.reverse() -- reverse *IN PLACE*";
1444static char sort_doc[] =
1445"L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1446
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001447static PyMethodDef list_methods[] = {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001448 {"append", (PyCFunction)listappend, 1, append_doc},
1449 {"insert", (PyCFunction)listinsert, 1, insert_doc},
Barry Warsawdedf6d61998-10-09 16:37:25 +00001450 {"extend", (PyCFunction)listextend, 1, extend_doc},
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001451 {"pop", (PyCFunction)listpop, 1, pop_doc},
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001452 {"remove", (PyCFunction)listremove, 1, remove_doc},
1453 {"index", (PyCFunction)listindex, 1, index_doc},
1454 {"count", (PyCFunction)listcount, 1, count_doc},
1455 {"reverse", (PyCFunction)listreverse, 1, reverse_doc},
1456 {"sort", (PyCFunction)listsort, 1, sort_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001457 {NULL, NULL} /* sentinel */
1458};
1459
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001460static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001461list_getattr(f, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001462 PyListObject *f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001463 char *name;
1464{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001465 return Py_FindMethod(list_methods, (PyObject *)f, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001466}
1467
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001468static PySequenceMethods list_as_sequence = {
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001469 (inquiry)list_length, /*sq_length*/
1470 (binaryfunc)list_concat, /*sq_concat*/
1471 (intargfunc)list_repeat, /*sq_repeat*/
1472 (intargfunc)list_item, /*sq_item*/
1473 (intintargfunc)list_slice, /*sq_slice*/
1474 (intobjargproc)list_ass_item, /*sq_ass_item*/
1475 (intintobjargproc)list_ass_slice, /*sq_ass_slice*/
Jeremy Hylton37b1a262000-04-27 21:41:03 +00001476 (objobjproc)list_contains, /*sq_contains*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001477};
1478
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001479PyTypeObject PyList_Type = {
1480 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001481 0,
1482 "list",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001483 sizeof(PyListObject) + PyGC_HEAD_SIZE,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001484 0,
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001485 (destructor)list_dealloc, /*tp_dealloc*/
1486 (printfunc)list_print, /*tp_print*/
1487 (getattrfunc)list_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001488 0, /*tp_setattr*/
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001489 (cmpfunc)list_compare, /*tp_compare*/
1490 (reprfunc)list_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001491 0, /*tp_as_number*/
1492 &list_as_sequence, /*tp_as_sequence*/
1493 0, /*tp_as_mapping*/
Fred Drake56780252000-06-15 14:50:20 +00001494 0, /*tp_hash*/
1495 0, /*tp_call*/
1496 0, /*tp_str*/
1497 0, /*tp_getattro*/
1498 0, /*tp_setattro*/
1499 0, /*tp_as_buffer*/
Jeremy Hyltond08b4c42000-06-23 19:37:02 +00001500 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Jeremy Hylton8caad492000-06-23 14:18:11 +00001501 0, /* tp_doc */
1502 (traverseproc)list_traverse, /* tp_traverse */
1503 (inquiry)list_clear, /* tp_clear */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001504};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001505
1506
1507/* During a sort, we really can't have anyone modifying the list; it could
1508 cause core dumps. Thus, we substitute a dummy type that raises an
1509 explanatory exception when a modifying operation is used. Caveat:
1510 comparisons may behave differently; but I guess it's a bad idea anyway to
1511 compare a list that's being sorted... */
1512
1513static PyObject *
1514immutable_list_op(/*No args!*/)
1515{
1516 PyErr_SetString(PyExc_TypeError,
1517 "a list cannot be modified while it is being sorted");
1518 return NULL;
1519}
1520
1521static PyMethodDef immutable_list_methods[] = {
1522 {"append", (PyCFunction)immutable_list_op},
1523 {"insert", (PyCFunction)immutable_list_op},
1524 {"remove", (PyCFunction)immutable_list_op},
1525 {"index", (PyCFunction)listindex},
1526 {"count", (PyCFunction)listcount},
1527 {"reverse", (PyCFunction)immutable_list_op},
1528 {"sort", (PyCFunction)immutable_list_op},
1529 {NULL, NULL} /* sentinel */
1530};
1531
1532static PyObject *
1533immutable_list_getattr(f, name)
1534 PyListObject *f;
1535 char *name;
1536{
1537 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
1538}
1539
1540static int
1541immutable_list_ass(/*No args!*/)
1542{
1543 immutable_list_op();
1544 return -1;
1545}
1546
1547static PySequenceMethods immutable_list_as_sequence = {
1548 (inquiry)list_length, /*sq_length*/
1549 (binaryfunc)list_concat, /*sq_concat*/
1550 (intargfunc)list_repeat, /*sq_repeat*/
1551 (intargfunc)list_item, /*sq_item*/
1552 (intintargfunc)list_slice, /*sq_slice*/
1553 (intobjargproc)immutable_list_ass, /*sq_ass_item*/
1554 (intintobjargproc)immutable_list_ass, /*sq_ass_slice*/
Fred Drake56780252000-06-15 14:50:20 +00001555 (objobjproc)list_contains, /*sq_contains*/
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001556};
1557
1558static PyTypeObject immutable_list_type = {
1559 PyObject_HEAD_INIT(&PyType_Type)
1560 0,
1561 "list (immutable, during sort)",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001562 sizeof(PyListObject) + PyGC_HEAD_SIZE,
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001563 0,
1564 0, /*tp_dealloc*/ /* Cannot happen */
1565 (printfunc)list_print, /*tp_print*/
1566 (getattrfunc)immutable_list_getattr, /*tp_getattr*/
1567 0, /*tp_setattr*/
1568 0, /*tp_compare*/ /* Won't be called */
1569 (reprfunc)list_repr, /*tp_repr*/
1570 0, /*tp_as_number*/
1571 &immutable_list_as_sequence, /*tp_as_sequence*/
1572 0, /*tp_as_mapping*/
Fred Drake56780252000-06-15 14:50:20 +00001573 0, /*tp_hash*/
1574 0, /*tp_call*/
1575 0, /*tp_str*/
1576 0, /*tp_getattro*/
1577 0, /*tp_setattro*/
1578 0, /*tp_as_buffer*/
Jeremy Hyltond08b4c42000-06-23 19:37:02 +00001579 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Jeremy Hylton8caad492000-06-23 14:18:11 +00001580 0, /* tp_doc */
1581 (traverseproc)list_traverse, /* tp_traverse */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001582};
Guido van Rossum42812581998-06-17 14:15:44 +00001583