blob: 0342cdcb38630582be561102313b5120104d5864 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* List object implementation */
33
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034#include "Python.h"
35
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000036#ifdef STDC_HEADERS
37#include <stddef.h>
38#else
39#include <sys/types.h> /* For size_t */
40#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Guido van Rossumc0b618a1997-05-02 03:12:38 +000042#define ROUNDUP(n, PyTryBlock) \
43 ((((n)+(PyTryBlock)-1)/(PyTryBlock))*(PyTryBlock))
Guido van Rossuma46d51d1995-01-26 22:59:43 +000044
45static int
Guido van Rossuma27d1121997-08-25 18:36:23 +000046roundupsize(n)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000047 int n;
48{
49 if (n < 500)
50 return ROUNDUP(n, 10);
51 else
52 return ROUNDUP(n, 100);
53}
54
Guido van Rossuma27d1121997-08-25 18:36:23 +000055#define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems))
Guido van Rossuma46d51d1995-01-26 22:59:43 +000056
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057PyObject *
58PyList_New(size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059 int size;
60{
61 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062 PyListObject *op;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000063 size_t nbytes;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 if (size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 return NULL;
67 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000068 nbytes = size * sizeof(PyObject *);
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000069 /* Check for overflow */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000070 if (nbytes / sizeof(PyObject *) != (size_t)size) {
71 return PyErr_NoMemory();
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000072 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000073 op = (PyListObject *) malloc(sizeof(PyListObject));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 }
77 if (size <= 0) {
78 op->ob_item = NULL;
79 }
80 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000081 op->ob_item = (PyObject **) malloc(nbytes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082 if (op->ob_item == NULL) {
83 free((ANY *)op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 }
86 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 op->ob_type = &PyList_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 op->ob_size = size;
89 for (i = 0; i < size; i++)
90 op->ob_item[i] = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091 _Py_NewReference(op);
92 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093}
94
95int
Guido van Rossumc0b618a1997-05-02 03:12:38 +000096PyList_Size(op)
97 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099 if (!PyList_Check(op)) {
100 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101 return -1;
102 }
103 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000104 return ((PyListObject *)op) -> ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105}
106
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000107static PyObject *indexerr;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000108
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109PyObject *
110PyList_GetItem(op, i)
111 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112 int i;
113{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000114 if (!PyList_Check(op)) {
115 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116 return NULL;
117 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000118 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000119 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 indexerr = PyString_FromString(
121 "list index out of range");
122 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123 return NULL;
124 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000125 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126}
127
128int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129PyList_SetItem(op, i, newitem)
130 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131 register int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000132 register PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134 register PyObject *olditem;
135 register PyObject **p;
136 if (!PyList_Check(op)) {
137 Py_XDECREF(newitem);
138 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000139 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000141 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
142 Py_XDECREF(newitem);
143 PyErr_SetString(PyExc_IndexError,
144 "list assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000145 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000147 p = ((PyListObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000148 olditem = *p;
149 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151 return 0;
152}
153
154static int
155ins1(self, where, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000156 PyListObject *self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000158 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159{
160 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161 PyObject **items;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000162 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000164 return -1;
165 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166 items = self->ob_item;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000167 NRESIZE(items, PyObject *, self->ob_size+1);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000168 if (items == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000169 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000170 return -1;
171 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172 if (where < 0)
173 where = 0;
174 if (where > self->ob_size)
175 where = self->ob_size;
176 for (i = self->ob_size; --i >= where; )
177 items[i+1] = items[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179 items[where] = v;
180 self->ob_item = items;
181 self->ob_size++;
182 return 0;
183}
184
185int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000186PyList_Insert(op, where, newitem)
187 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000189 PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191 if (!PyList_Check(op)) {
192 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000193 return -1;
194 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000195 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000196}
197
198int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000199PyList_Append(op, newitem)
200 PyObject *op;
201 PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 if (!PyList_Check(op)) {
204 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000205 return -1;
206 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 return ins1((PyListObject *)op,
208 (int) ((PyListObject *)op)->ob_size, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209}
210
211/* Methods */
212
213static void
214list_dealloc(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 PyListObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216{
217 int i;
Jack Jansen7874d1f1995-01-19 12:09:27 +0000218 if (op->ob_item != NULL) {
Guido van Rossumfa717011999-06-09 15:19:34 +0000219 /* Do it backwards, for Christian Tismer.
220 There's a simple test case where somehow this reduces
221 thrashing when a *very* large list is created and
222 immediately deleted. */
223 i = op->ob_size;
224 while (--i >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225 Py_XDECREF(op->ob_item[i]);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000226 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227 free((ANY *)op->ob_item);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000228 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229 free((ANY *)op);
230}
231
Guido van Rossum90933611991-06-07 16:10:43 +0000232static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233list_print(op, fp, flags)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000234 PyListObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235 FILE *fp;
236 int flags;
237{
238 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000239
240 i = Py_ReprEnter((PyObject*)op);
241 if (i != 0) {
242 if (i < 0)
243 return i;
244 fprintf(fp, "[...]");
245 return 0;
246 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247 fprintf(fp, "[");
Guido van Rossum90933611991-06-07 16:10:43 +0000248 for (i = 0; i < op->ob_size; i++) {
249 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 fprintf(fp, ", ");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000251 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
252 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000253 return -1;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000254 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255 }
256 fprintf(fp, "]");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000257 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000258 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259}
260
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000261static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262list_repr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000263 PyListObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000265 PyObject *s, *comma;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000267
268 i = Py_ReprEnter((PyObject*)v);
269 if (i != 0) {
270 if (i > 0)
271 return PyString_FromString("[...]");
272 return NULL;
273 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000274 s = PyString_FromString("[");
275 comma = PyString_FromString(", ");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276 for (i = 0; i < v->ob_size && s != NULL; i++) {
277 if (i > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000278 PyString_Concat(&s, comma);
279 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000281 Py_XDECREF(comma);
282 PyString_ConcatAndDel(&s, PyString_FromString("]"));
Guido van Rossumfb376de1998-04-10 22:47:27 +0000283 Py_ReprLeave((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284 return s;
285}
286
287static int
288list_compare(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000289 PyListObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291 int i;
Guido van Rossumae621ff1998-05-28 20:18:46 +0000292 for (i = 0; i < v->ob_size && i < w->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000293 int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294 if (cmp != 0)
295 return cmp;
296 }
297 return v->ob_size - w->ob_size;
298}
299
300static int
301list_length(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000302 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303{
304 return a->ob_size;
305}
306
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000307static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308list_item(a, i)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000309 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310 int i;
311{
312 if (i < 0 || i >= a->ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000313 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000314 indexerr = PyString_FromString(
315 "list index out of range");
316 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317 return NULL;
318 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000319 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320 return a->ob_item[i];
321}
322
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000323static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324list_slice(a, ilow, ihigh)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000325 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326 int ilow, ihigh;
327{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000328 PyListObject *np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329 int i;
330 if (ilow < 0)
331 ilow = 0;
332 else if (ilow > a->ob_size)
333 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334 if (ihigh < ilow)
335 ihigh = ilow;
336 else if (ihigh > a->ob_size)
337 ihigh = a->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000338 np = (PyListObject *) PyList_New(ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339 if (np == NULL)
340 return NULL;
341 for (i = ilow; i < ihigh; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000342 PyObject *v = a->ob_item[i];
343 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344 np->ob_item[i - ilow] = v;
345 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000346 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347}
348
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000349PyObject *
350PyList_GetSlice(a, ilow, ihigh)
351 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000352 int ilow, ihigh;
353{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000354 if (!PyList_Check(a)) {
355 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000356 return NULL;
357 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000358 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000359}
360
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000361static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362list_concat(a, bb)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000363 PyListObject *a;
364 PyObject *bb;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365{
366 int size;
367 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000368 PyListObject *np;
369 if (!PyList_Check(bb)) {
370 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371 return NULL;
372 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000373#define b ((PyListObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374 size = a->ob_size + b->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000375 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000377 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378 }
379 for (i = 0; i < a->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380 PyObject *v = a->ob_item[i];
381 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382 np->ob_item[i] = v;
383 }
384 for (i = 0; i < b->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000385 PyObject *v = b->ob_item[i];
386 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387 np->ob_item[i + a->ob_size] = v;
388 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390#undef b
391}
392
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +0000394list_repeat(a, n)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000395 PyListObject *a;
Guido van Rossumed98d481991-03-06 13:07:53 +0000396 int n;
397{
398 int i, j;
399 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400 PyListObject *np;
401 PyObject **p;
Guido van Rossumed98d481991-03-06 13:07:53 +0000402 if (n < 0)
403 n = 0;
404 size = a->ob_size * n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000406 if (np == NULL)
407 return NULL;
408 p = np->ob_item;
409 for (i = 0; i < n; i++) {
410 for (j = 0; j < a->ob_size; j++) {
411 *p = a->ob_item[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000413 p++;
414 }
415 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000416 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000417}
418
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000420list_ass_slice(a, ilow, ihigh, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000421 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000422 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000423 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000425 /* Because [X]DECREF can recursively invoke list operations on
426 this list, we must postpone all [X]DECREF activity until
427 after the list is back in its canonical shape. Therefore
428 we must allocate an additional array, 'recycle', into which
429 we temporarily copy the items that are deleted from the
430 list. :-( */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431 PyObject **recycle, **p;
432 PyObject **item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000433 int n; /* Size of replacement list */
434 int d; /* Change in size */
435 int k; /* Loop index */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000436#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437 if (v == NULL)
438 n = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439 else if (PyList_Check(v)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440 n = b->ob_size;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000441 if (a == b) {
442 /* Special case "a[i:j] = a" -- copy b first */
443 int ret;
444 v = list_slice(b, 0, n);
445 ret = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446 Py_DECREF(v);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000447 return ret;
448 }
449 }
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000450 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451 PyErr_BadArgument();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000452 return -1;
453 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000454 if (ilow < 0)
455 ilow = 0;
456 else if (ilow > a->ob_size)
457 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458 if (ihigh < ilow)
459 ihigh = ilow;
460 else if (ihigh > a->ob_size)
461 ihigh = a->ob_size;
462 item = a->ob_item;
463 d = n - (ihigh-ilow);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000464 if (ihigh > ilow)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465 p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000466 else
467 p = recycle = NULL;
468 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469 for (k = ilow; k < ihigh; k++)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000470 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471 if (d < 0) {
472 for (/*k = ihigh*/; k < a->ob_size; k++)
473 item[k+d] = item[k];
474 a->ob_size += d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476 a->ob_item = item;
477 }
478 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000479 else { /* Insert d items; recycle ihigh-ilow items */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000480 NRESIZE(item, PyObject *, a->ob_size + d);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000481 if (item == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000482 PyMem_XDEL(recycle);
483 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000484 return -1;
485 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486 for (k = a->ob_size; --k >= ihigh; )
487 item[k+d] = item[k];
488 for (/*k = ihigh-1*/; k >= ilow; --k)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000489 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000490 a->ob_item = item;
491 a->ob_size += d;
492 }
493 for (k = 0; k < n; k++, ilow++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000494 PyObject *w = b->ob_item[k];
495 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496 item[ilow] = w;
497 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000498 if (recycle) {
499 while (--p >= recycle)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000500 Py_XDECREF(*p);
501 PyMem_DEL(recycle);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000502 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000503 return 0;
504#undef b
505}
506
Guido van Rossum234f9421993-06-17 12:35:49 +0000507int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000508PyList_SetSlice(a, ilow, ihigh, v)
509 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000510 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000511 PyObject *v;
Guido van Rossum234f9421993-06-17 12:35:49 +0000512{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513 if (!PyList_Check(a)) {
514 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000515 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000516 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000517 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000518}
519
Guido van Rossum4a450d01991-04-03 19:05:18 +0000520static int
521list_ass_item(a, i, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000522 PyListObject *a;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000523 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524 PyObject *v;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000525{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 PyObject *old_value;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000527 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000528 PyErr_SetString(PyExc_IndexError,
529 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000530 return -1;
531 }
532 if (v == NULL)
533 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000535 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000536 a->ob_item[i] = v;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000538 return 0;
539}
540
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000541static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000542ins(self, where, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000543 PyListObject *self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000545 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546{
547 if (ins1(self, where, v) != 0)
548 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000549 Py_INCREF(Py_None);
550 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551}
552
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000553static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554listinsert(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000555 PyListObject *self;
556 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000557{
558 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000559 PyObject *v;
560 if (!PyArg_Parse(args, "(iO)", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000561 return NULL;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000562 return ins(self, i, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563}
564
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000565static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566listappend(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000567 PyListObject *self;
568 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000570 PyObject *v;
571 if (!PyArg_Parse(args, "O", &v))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000572 return NULL;
573 return ins(self, (int) self->ob_size, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574}
575
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000576static PyObject *
Barry Warsawdedf6d61998-10-09 16:37:25 +0000577listextend(self, args)
578 PyListObject *self;
579 PyObject *args;
580{
581 PyObject *b = NULL, *res = NULL;
582 PyObject **items;
583 int selflen = PyList_GET_SIZE(self);
584 int blen;
585 register int i;
586
587 if (!PyArg_ParseTuple(args, "O", &b))
588 return NULL;
589
590 if (!PyList_Check(b)) {
591 PyErr_SetString(PyExc_TypeError,
592 "list.extend() argument must be a list");
593 return NULL;
594 }
595 if (PyList_GET_SIZE(b) == 0) {
596 /* short circuit when b is empty */
597 Py_INCREF(Py_None);
598 return Py_None;
599 }
600 if (self == (PyListObject*)b) {
601 /* as in list_ass_slice() we must special case the
602 * situation: a.extend(a)
603 *
604 * XXX: I think this way ought to be faster than using
605 * list_slice() the way list_ass_slice() does.
606 */
607 b = PyList_New(selflen);
608 if (!b)
609 return NULL;
610 for (i = 0; i < selflen; i++) {
611 PyObject *o = PyList_GET_ITEM(self, i);
612 Py_INCREF(o);
613 PyList_SET_ITEM(b, i, o);
614 }
615 }
616 else
617 /* we want b to have the same refcount semantics for the
618 * Py_XDECREF() in the finally clause regardless of which
619 * branch in the above conditional we took.
620 */
621 Py_INCREF(b);
622
623 blen = PyList_GET_SIZE(b);
624 /* resize a using idiom */
625 items = self->ob_item;
626 NRESIZE(items, PyObject*, selflen + blen);
627 if (items == NULL ) {
628 PyErr_NoMemory();
629 goto finally;
630 }
631 self->ob_item = items;
632
633 /* populate the end self with b's items */
634 for (i = 0; i < blen; i++) {
635 PyObject *o = PyList_GET_ITEM(b, i);
636 Py_INCREF(o);
637 PyList_SET_ITEM(self, self->ob_size++, o);
638 }
639 res = Py_None;
640 Py_INCREF(res);
641 finally:
642 Py_XDECREF(b);
643 return res;
644}
645
646
647static PyObject *
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000648listpop(self, args)
649 PyListObject *self;
650 PyObject *args;
651{
652 int i = -1;
653 PyObject *v;
654 if (!PyArg_ParseTuple(args, "|i", &i))
655 return NULL;
656 if (self->ob_size == 0) {
657 /* Special-case most common failure cause */
658 PyErr_SetString(PyExc_IndexError, "pop from empty list");
659 return NULL;
660 }
661 if (i < 0)
662 i += self->ob_size;
663 if (i < 0 || i >= self->ob_size) {
664 PyErr_SetString(PyExc_IndexError, "pop index out of range");
665 return NULL;
666 }
667 v = self->ob_item[i];
668 Py_INCREF(v);
669 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
670 Py_DECREF(v);
671 return NULL;
672 }
673 return v;
674}
675
Guido van Rossum3f236de1996-12-10 23:55:39 +0000676/* New quicksort implementation for arrays of object pointers.
677 Thanks to discussions with Tim Peters. */
678
679/* CMPERROR is returned by our comparison function when an error
680 occurred. This is the largest negative integer (0x80000000 on a
681 32-bit system). */
Guido van Rossum19700b61997-03-05 00:45:43 +0000682#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
Guido van Rossum3f236de1996-12-10 23:55:39 +0000683
684/* Comparison function. Takes care of calling a user-supplied
685 comparison function (any callable Python object). Calls the
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000686 standard comparison function, PyObject_Compare(), if the user-
687 supplied function is NULL. */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000688
689static int
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000690docompare(x, y, compare)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000691 PyObject *x;
692 PyObject *y;
693 PyObject *compare;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000694{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695 PyObject *args, *res;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000696 int i;
697
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000698 if (compare == NULL) {
699 i = PyObject_Compare(x, y);
700 if (i && PyErr_Occurred())
701 i = CMPERROR;
702 return i;
703 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000704
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000705 args = Py_BuildValue("(OO)", x, y);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000706 if (args == NULL)
707 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000708 res = PyEval_CallObject(compare, args);
709 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000710 if (res == NULL)
711 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712 if (!PyInt_Check(res)) {
713 Py_DECREF(res);
714 PyErr_SetString(PyExc_TypeError,
Guido van Rossum9bcd1d71999-04-19 17:44:39 +0000715 "comparison function must return int");
Guido van Rossum3f236de1996-12-10 23:55:39 +0000716 return CMPERROR;
717 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718 i = PyInt_AsLong(res);
719 Py_DECREF(res);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000720 if (i < 0)
721 return -1;
722 if (i > 0)
723 return 1;
724 return 0;
725}
726
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000727/* MINSIZE is the smallest array that will get a full-blown samplesort
728 treatment; smaller arrays are sorted using binary insertion. It must
729 be at least 7 for the samplesort implementation to work. Binary
730 insertion does fewer compares, but can suffer O(N**2) data movement.
731 The more expensive compares, the larger MINSIZE should be. */
732#define MINSIZE 100
733
734/* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
735 partition; smaller slices are passed to binarysort. It must be at
736 least 2, and no larger than MINSIZE. Setting it higher reduces the #
737 of compares slowly, but increases the amount of data movement quickly.
738 The value here was chosen assuming a compare costs ~25x more than
739 swapping a pair of memory-resident pointers -- but under that assumption,
740 changing the value by a few dozen more or less has aggregate effect
741 under 1%. So the value is crucial, but not touchy <wink>. */
742#define MINPARTITIONSIZE 40
743
744/* MAXMERGE is the largest number of elements we'll always merge into
745 a known-to-be sorted chunk via binary insertion, regardless of the
746 size of that chunk. Given a chunk of N sorted elements, and a group
747 of K unknowns, the largest K for which it's better to do insertion
748 (than a full-blown sort) is a complicated function of N and K mostly
749 involving the expected number of compares and data moves under each
750 approach, and the relative cost of those operations on a specific
751 architecure. The fixed value here is conservative, and should be a
752 clear win regardless of architecture or N. */
753#define MAXMERGE 15
Guido van Rossum3f236de1996-12-10 23:55:39 +0000754
Guido van Rossum3f236de1996-12-10 23:55:39 +0000755/* STACKSIZE is the size of our work stack. A rough estimate is that
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000756 this allows us to sort arrays of size N where
757 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
758 for arrays of size 2**64. Because we push the biggest partition
759 first, the worst case occurs when all subarrays are always partitioned
760 exactly in two. */
761#define STACKSIZE 60
Guido van Rossum3f236de1996-12-10 23:55:39 +0000762
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000763
764#define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
765
766/* binarysort is the best method for sorting small arrays: it does
767 few compares, but can do data movement quadratic in the number of
768 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000769 [lo, hi) is a contiguous slice of a list, and is sorted via
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000770 binary insertion.
771 On entry, must have lo <= start <= hi, and that [lo, start) is already
772 sorted (pass start == lo if you don't know!).
773 If docompare complains (returns CMPERROR) return -1, else 0.
774 Even in case of error, the output slice will be some permutation of
775 the input (nothing is lost or duplicated).
776*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000777
778static int
Guido van Rossum42812581998-06-17 14:15:44 +0000779binarysort(lo, hi, start, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000780 PyObject **lo;
781 PyObject **hi;
782 PyObject **start;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000783 PyObject *compare;/* Comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000784{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000785 /* assert lo <= start <= hi
786 assert [lo, start) is sorted */
787 register int k;
788 register PyObject **l, **p, **r;
789 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000790
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000791 if (lo == start)
792 ++start;
793 for (; start < hi; ++start) {
794 /* set l to where *start belongs */
795 l = lo;
796 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000797 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000798 do {
799 p = l + ((r - l) >> 1);
800 SETK(pivot, *p);
801 if (k < 0)
802 r = p;
803 else
804 l = p + 1;
805 } while (l < r);
806 /* Pivot should go at l -- slide over to make room.
807 Caution: using memmove is much slower under MSVC 5;
808 we're not usually moving many slots. */
809 for (p = start; p > l; --p)
810 *p = *(p-1);
811 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000812 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000813 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000814
815 fail:
816 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000817}
818
819/* samplesortslice is the sorting workhorse.
Guido van Rossum42812581998-06-17 14:15:44 +0000820 [lo, hi) is a contiguous slice of a list, to be sorted in place.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000821 On entry, must have lo <= hi,
822 If docompare complains (returns CMPERROR) return -1, else 0.
823 Even in case of error, the output slice will be some permutation of
824 the input (nothing is lost or duplicated).
825
826 samplesort is basically quicksort on steroids: a power of 2 close
827 to n/ln(n) is computed, and that many elements (less 1) are picked at
828 random from the array and sorted. These 2**k - 1 elements are then
829 used as preselected pivots for an equal number of quicksort
830 partitioning steps, partitioning the slice into 2**k chunks each of
831 size about ln(n). These small final chunks are then usually handled
832 by binarysort. Note that when k=1, this is roughly the same as an
833 ordinary quicksort using a random pivot, and when k=2 this is roughly
834 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
835 this a "median of n/ln(n)" quicksort. You can also view it as a kind
836 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
837
838 The large number of samples makes a quadratic-time case almost
839 impossible, and asymptotically drives the average-case number of
840 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
841 3 variant) down to N lg N.
842
843 We also play lots of low-level tricks to cut the number of compares.
844
845 Very obscure: To avoid using extra memory, the PPs are stored in the
846 array and shuffled around as partitioning proceeds. At the start of a
847 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
848 adjacent (either on the left or the right!) to a chunk of X elements
849 that are to be partitioned: P X or X P. In either case we need to
850 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
851 left, followed by the PP to be used for this step (that's the middle
852 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
853 P X or X P -> Psmall pivot X Plarge
854 and the order of the PPs must not be altered. It can take a while
855 to realize this isn't trivial! It can take even longer <wink> to
856 understand why the simple code below works, using only 2**(m-1) swaps.
857 The key is that the order of the X elements isn't necessarily
858 preserved: X can end up as some cyclic permutation of its original
859 order. That's OK, because X is unsorted anyway. If the order of X
860 had to be preserved too, the simplest method I know of using O(1)
861 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
862 Since len(X) is typically several times larger than 2**(m-1), that
863 would slow things down.
864*/
865
866struct SamplesortStackNode {
867 /* Represents a slice of the array, from (& including) lo up
868 to (but excluding) hi. "extra" additional & adjacent elements
869 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
870 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
871 already sorted, but nothing is known about the other elements
872 in [lo, hi). |extra| is always one less than a power of 2.
873 When extra is 0, we're out of PPs, and the slice must be
874 sorted by some other means. */
875 PyObject **lo;
876 PyObject **hi;
877 int extra;
878};
879
880/* 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 +0000881 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000882 is undesirable, so cutoff values are canned in the "cutoff" table
883 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
884#define CUTOFFBASE 4
Guido van Rossum3aa23fd1999-01-14 19:01:53 +0000885static long cutoff[] = {
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000886 43, /* smallest N such that k == 4 */
887 106, /* etc */
888 250,
889 576,
890 1298,
891 2885,
892 6339,
893 13805,
894 29843,
895 64116,
896 137030,
897 291554,
898 617916,
899 1305130,
900 2748295,
901 5771662,
902 12091672,
903 25276798,
904 52734615,
905 109820537,
906 228324027,
907 473977813,
908 982548444, /* smallest N such that k == 26 */
909 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
910};
911
912static int
Guido van Rossum42812581998-06-17 14:15:44 +0000913samplesortslice(lo, hi, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000914 PyObject **lo;
915 PyObject **hi;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000916 PyObject *compare;/* Comparison function object, or NULL for default */
917{
918 register PyObject **l, **r;
919 register PyObject *tmp, *pivot;
920 register int k;
921 int n, extra, top, extraOnRight;
922 struct SamplesortStackNode stack[STACKSIZE];
923
924 /* assert lo <= hi */
925 n = hi - lo;
926
927 /* ----------------------------------------------------------
928 * Special cases
929 * --------------------------------------------------------*/
930 if (n < 2)
931 return 0;
932
933 /* Set r to the largest value such that [lo,r) is sorted.
934 This catches the already-sorted case, the all-the-same
935 case, and the appended-a-few-elements-to-a-sorted-list case.
936 If the array is unsorted, we're very likely to get out of
937 the loop fast, so the test is cheap if it doesn't pay off.
938 */
939 /* assert lo < hi */
940 for (r = lo+1; r < hi; ++r) {
941 SETK(*r, *(r-1));
942 if (k < 0)
943 break;
944 }
945 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
946 few unknowns, or few elements in total. */
947 if (hi - r <= MAXMERGE || n < MINSIZE)
Guido van Rossum42812581998-06-17 14:15:44 +0000948 return binarysort(lo, hi, r, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000949
950 /* Check for the array already being reverse-sorted. Typical
951 benchmark-driven silliness <wink>. */
952 /* assert lo < hi */
953 for (r = lo+1; r < hi; ++r) {
954 SETK(*(r-1), *r);
955 if (k < 0)
956 break;
957 }
958 if (hi - r <= MAXMERGE) {
959 /* Reverse the reversed prefix, then insert the tail */
960 PyObject **originalr = r;
961 l = lo;
962 do {
963 --r;
964 tmp = *l; *l = *r; *r = tmp;
965 ++l;
966 } while (l < r);
Guido van Rossum42812581998-06-17 14:15:44 +0000967 return binarysort(lo, hi, originalr, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000968 }
969
970 /* ----------------------------------------------------------
971 * Normal case setup: a large array without obvious pattern.
972 * --------------------------------------------------------*/
973
974 /* extra := a power of 2 ~= n/ln(n), less 1.
975 First find the smallest extra s.t. n < cutoff[extra] */
976 for (extra = 0;
977 extra < sizeof(cutoff) / sizeof(cutoff[0]);
978 ++extra) {
979 if (n < cutoff[extra])
980 break;
981 /* note that if we fall out of the loop, the value of
982 extra still makes *sense*, but may be smaller than
983 we would like (but the array has more than ~= 2**31
984 elements in this case!) */
985 }
986 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
987 have is CUTOFFBASE-1, so
988 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
989 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
990 /* assert extra > 0 and n >= extra */
991
992 /* Swap that many values to the start of the array. The
993 selection of elements is pseudo-random, but the same on
994 every run (this is intentional! timing algorithm changes is
995 a pain if timing varies across runs). */
996 {
997 unsigned int seed = n / extra; /* arbitrary */
998 unsigned int i;
999 for (i = 0; i < (unsigned)extra; ++i) {
1000 /* j := random int in [i, n) */
1001 unsigned int j;
1002 seed = seed * 69069 + 7;
1003 j = i + seed % (n - i);
1004 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
1005 }
1006 }
1007
1008 /* Recursively sort the preselected pivots. */
Guido van Rossum42812581998-06-17 14:15:44 +00001009 if (samplesortslice(lo, lo + extra, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001010 goto fail;
1011
1012 top = 0; /* index of available stack slot */
1013 lo += extra; /* point to first unknown */
1014 extraOnRight = 0; /* the PPs are at the left end */
1015
1016 /* ----------------------------------------------------------
1017 * Partition [lo, hi), and repeat until out of work.
1018 * --------------------------------------------------------*/
1019 for (;;) {
1020 /* assert lo <= hi, so n >= 0 */
1021 n = hi - lo;
1022
1023 /* We may not want, or may not be able, to partition:
1024 If n is small, it's quicker to insert.
1025 If extra is 0, we're out of pivots, and *must* use
1026 another method.
1027 */
1028 if (n < MINPARTITIONSIZE || extra == 0) {
1029 if (n >= MINSIZE) {
1030 /* assert extra == 0
1031 This is rare, since the average size
1032 of a final block is only about
1033 ln(original n). */
Guido van Rossum42812581998-06-17 14:15:44 +00001034 if (samplesortslice(lo, hi, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001035 goto fail;
1036 }
1037 else {
1038 /* Binary insertion should be quicker,
1039 and we can take advantage of the PPs
1040 already being sorted. */
1041 if (extraOnRight && extra) {
1042 /* swap the PPs to the left end */
1043 k = extra;
1044 do {
1045 tmp = *lo;
1046 *lo = *hi;
1047 *hi = tmp;
1048 ++lo; ++hi;
1049 } while (--k);
1050 }
1051 if (binarysort(lo - extra, hi, lo,
Guido van Rossum42812581998-06-17 14:15:44 +00001052 compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001053 goto fail;
1054 }
1055
1056 /* Find another slice to work on. */
1057 if (--top < 0)
1058 break; /* no more -- done! */
1059 lo = stack[top].lo;
1060 hi = stack[top].hi;
1061 extra = stack[top].extra;
1062 extraOnRight = 0;
1063 if (extra < 0) {
1064 extraOnRight = 1;
1065 extra = -extra;
1066 }
1067 continue;
1068 }
1069
1070 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1071 Then our preselected pivot is at (extra-1)/2, and we
1072 want to move the PPs before that to the left end of
1073 the slice, and the PPs after that to the right end.
1074 The following section changes extra, lo, hi, and the
1075 slice such that:
1076 [lo-extra, lo) contains the smaller PPs.
1077 *lo == our PP.
1078 (lo, hi) contains the unknown elements.
1079 [hi, hi+extra) contains the larger PPs.
1080 */
1081 k = extra >>= 1; /* num PPs to move */
1082 if (extraOnRight) {
1083 /* Swap the smaller PPs to the left end.
1084 Note that this loop actually moves k+1 items:
1085 the last is our PP */
1086 do {
1087 tmp = *lo; *lo = *hi; *hi = tmp;
1088 ++lo; ++hi;
1089 } while (k--);
1090 }
1091 else {
1092 /* Swap the larger PPs to the right end. */
1093 while (k--) {
1094 --lo; --hi;
1095 tmp = *lo; *lo = *hi; *hi = tmp;
1096 }
1097 }
1098 --lo; /* *lo is now our PP */
1099 pivot = *lo;
1100
1101 /* Now an almost-ordinary quicksort partition step.
1102 Note that most of the time is spent here!
1103 Only odd thing is that we partition into < and >=,
1104 instead of the usual <= and >=. This helps when
1105 there are lots of duplicates of different values,
1106 because it eventually tends to make subfiles
1107 "pure" (all duplicates), and we special-case for
1108 duplicates later. */
1109 l = lo + 1;
1110 r = hi - 1;
1111 /* assert lo < l < r < hi (small n weeded out above) */
1112
1113 do {
1114 /* slide l right, looking for key >= pivot */
1115 do {
1116 SETK(*l, pivot);
1117 if (k < 0)
1118 ++l;
1119 else
1120 break;
1121 } while (l < r);
1122
1123 /* slide r left, looking for key < pivot */
1124 while (l < r) {
Guido van Rossum42812581998-06-17 14:15:44 +00001125 register PyObject *rval = *r--;
1126 SETK(rval, pivot);
1127 if (k < 0) {
1128 /* swap and advance */
1129 r[1] = *l;
1130 *l++ = rval;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001131 break;
Guido van Rossum42812581998-06-17 14:15:44 +00001132 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001133 }
1134
1135 } while (l < r);
1136
1137 /* assert lo < r <= l < hi
1138 assert r == l or r+1 == l
1139 everything to the left of l is < pivot, and
1140 everything to the right of r is >= pivot */
1141
1142 if (l == r) {
1143 SETK(*r, pivot);
1144 if (k < 0)
1145 ++l;
1146 else
1147 --r;
1148 }
1149 /* assert lo <= r and r+1 == l and l <= hi
1150 assert r == lo or a[r] < pivot
1151 assert a[lo] is pivot
1152 assert l == hi or a[l] >= pivot
1153 Swap the pivot into "the middle", so we can henceforth
1154 ignore it.
1155 */
1156 *lo = *r;
1157 *r = pivot;
1158
1159 /* The following is true now, & will be preserved:
1160 All in [lo,r) are < pivot
1161 All in [r,l) == pivot (& so can be ignored)
1162 All in [l,hi) are >= pivot */
1163
1164 /* Check for duplicates of the pivot. One compare is
1165 wasted if there are no duplicates, but can win big
1166 when there are.
1167 Tricky: we're sticking to "<" compares, so deduce
1168 equality indirectly. We know pivot <= *l, so they're
1169 equal iff not pivot < *l.
1170 */
1171 while (l < hi) {
1172 /* pivot <= *l known */
1173 SETK(pivot, *l);
1174 if (k < 0)
1175 break;
1176 else
1177 /* <= and not < implies == */
1178 ++l;
1179 }
1180
1181 /* assert lo <= r < l <= hi
1182 Partitions are [lo, r) and [l, hi) */
1183
1184 /* push fattest first; remember we still have extra PPs
1185 to the left of the left chunk and to the right of
1186 the right chunk! */
1187 /* assert top < STACKSIZE */
1188 if (r - lo <= hi - l) {
1189 /* second is bigger */
1190 stack[top].lo = l;
1191 stack[top].hi = hi;
1192 stack[top].extra = -extra;
1193 hi = r;
1194 extraOnRight = 0;
1195 }
1196 else {
1197 /* first is bigger */
1198 stack[top].lo = lo;
1199 stack[top].hi = r;
1200 stack[top].extra = extra;
1201 lo = l;
1202 extraOnRight = 1;
1203 }
1204 ++top;
1205
1206 } /* end of partitioning loop */
1207
1208 return 0;
1209
1210 fail:
1211 return -1;
1212}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001213
1214#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001215
1216staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001217
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001218static PyObject *
Guido van Rossum3f236de1996-12-10 23:55:39 +00001219listsort(self, compare)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001220 PyListObject *self;
1221 PyObject *compare;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001222{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001223 int err;
1224
1225 self->ob_type = &immutable_list_type;
1226 err = samplesortslice(self->ob_item,
1227 self->ob_item + self->ob_size,
Guido van Rossum42812581998-06-17 14:15:44 +00001228 compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001229 self->ob_type = &PyList_Type;
1230 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001231 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001232 Py_INCREF(Py_None);
1233 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001234}
1235
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001236int
1237PyList_Sort(v)
1238 PyObject *v;
1239{
1240 if (v == NULL || !PyList_Check(v)) {
1241 PyErr_BadInternalCall();
1242 return -1;
1243 }
1244 v = listsort((PyListObject *)v, (PyObject *)NULL);
1245 if (v == NULL)
1246 return -1;
1247 Py_DECREF(v);
1248 return 0;
1249}
1250
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001251static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001252listreverse(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001253 PyListObject *self;
1254 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001255{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001256 register PyObject **p, **q;
1257 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001258
1259 if (args != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001260 PyErr_BadArgument();
Guido van Rossumed98d481991-03-06 13:07:53 +00001261 return NULL;
1262 }
1263
1264 if (self->ob_size > 1) {
1265 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
1266 p < q; p++, q--) {
1267 tmp = *p;
1268 *p = *q;
1269 *q = tmp;
1270 }
1271 }
1272
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001273 Py_INCREF(Py_None);
1274 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001275}
1276
Guido van Rossum84c76f51990-10-30 13:32:20 +00001277int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001278PyList_Reverse(v)
1279 PyObject *v;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001280{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001281 if (v == NULL || !PyList_Check(v)) {
1282 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001283 return -1;
1284 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001285 v = listreverse((PyListObject *)v, (PyObject *)NULL);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001286 if (v == NULL)
1287 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001288 Py_DECREF(v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001289 return 0;
1290}
1291
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001292PyObject *
1293PyList_AsTuple(v)
1294 PyObject *v;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001295{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001296 PyObject *w;
1297 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001298 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001299 if (v == NULL || !PyList_Check(v)) {
1300 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001301 return NULL;
1302 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001303 n = ((PyListObject *)v)->ob_size;
1304 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001305 if (w == NULL)
1306 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001307 p = ((PyTupleObject *)w)->ob_item;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001308 memcpy((ANY *)p,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001309 (ANY *)((PyListObject *)v)->ob_item,
1310 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001311 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001312 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001313 p++;
1314 }
1315 return w;
1316}
1317
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001318static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001319listindex(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001320 PyListObject *self;
1321 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001322{
1323 int i;
1324
1325 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001326 PyErr_BadArgument();
Guido van Rossumed98d481991-03-06 13:07:53 +00001327 return NULL;
1328 }
1329 for (i = 0; i < self->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001330 if (PyObject_Compare(self->ob_item[i], args) == 0)
1331 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001332 if (PyErr_Occurred())
1333 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001334 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001335 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001336 return NULL;
1337}
1338
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001339static PyObject *
Guido van Rossume6f7d181991-10-20 20:20:40 +00001340listcount(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001341 PyListObject *self;
1342 PyObject *args;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001343{
1344 int count = 0;
1345 int i;
1346
1347 if (args == NULL) {
Guido van Rossum9bcd1d71999-04-19 17:44:39 +00001348 PyErr_SetString(PyExc_TypeError,
1349 "list.count(x): argument missing");
Guido van Rossume6f7d181991-10-20 20:20:40 +00001350 return NULL;
1351 }
1352 for (i = 0; i < self->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001353 if (PyObject_Compare(self->ob_item[i], args) == 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001354 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001355 if (PyErr_Occurred())
1356 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001357 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001358 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001359}
1360
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001361static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001362listremove(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001363 PyListObject *self;
1364 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001365{
1366 int i;
1367
1368 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001369 PyErr_BadArgument();
Guido van Rossumed98d481991-03-06 13:07:53 +00001370 return NULL;
1371 }
1372 for (i = 0; i < self->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001373 if (PyObject_Compare(self->ob_item[i], args) == 0) {
1374 if (list_ass_slice(self, i, i+1,
1375 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001376 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001377 Py_INCREF(Py_None);
1378 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001379 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001380 if (PyErr_Occurred())
1381 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001382 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001383 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001384 return NULL;
1385}
1386
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001387static char append_doc[] =
1388"L.append(object) -- append object to end";
Barry Warsawdedf6d61998-10-09 16:37:25 +00001389static char extend_doc[] =
1390"L.extend(list) -- extend list by appending list elements";
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001391static char insert_doc[] =
1392"L.insert(index, object) -- insert object before index";
1393static char pop_doc[] =
1394"L.pop([index]) -> item -- remove and return item at index (default last)";
1395static char remove_doc[] =
1396"L.remove(value) -- remove first occurrence of value";
1397static char index_doc[] =
1398"L.index(value) -> integer -- return index of first occurrence of value";
1399static char count_doc[] =
1400"L.count(value) -> integer -- return number of occurrences of value";
1401static char reverse_doc[] =
1402"L.reverse() -- reverse *IN PLACE*";
1403static char sort_doc[] =
1404"L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1405
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001406static PyMethodDef list_methods[] = {
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001407 {"append", (PyCFunction)listappend, 0, append_doc},
1408 {"insert", (PyCFunction)listinsert, 0, insert_doc},
Barry Warsawdedf6d61998-10-09 16:37:25 +00001409 {"extend", (PyCFunction)listextend, 1, extend_doc},
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001410 {"pop", (PyCFunction)listpop, 1, pop_doc},
1411 {"remove", (PyCFunction)listremove, 0, remove_doc},
1412 {"index", (PyCFunction)listindex, 0, index_doc},
1413 {"count", (PyCFunction)listcount, 0, count_doc},
1414 {"reverse", (PyCFunction)listreverse, 0, reverse_doc},
1415 {"sort", (PyCFunction)listsort, 0, sort_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001416 {NULL, NULL} /* sentinel */
1417};
1418
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001419static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001420list_getattr(f, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001421 PyListObject *f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001422 char *name;
1423{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001424 return Py_FindMethod(list_methods, (PyObject *)f, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001425}
1426
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001427static PySequenceMethods list_as_sequence = {
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001428 (inquiry)list_length, /*sq_length*/
1429 (binaryfunc)list_concat, /*sq_concat*/
1430 (intargfunc)list_repeat, /*sq_repeat*/
1431 (intargfunc)list_item, /*sq_item*/
1432 (intintargfunc)list_slice, /*sq_slice*/
1433 (intobjargproc)list_ass_item, /*sq_ass_item*/
1434 (intintobjargproc)list_ass_slice, /*sq_ass_slice*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001435};
1436
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001437PyTypeObject PyList_Type = {
1438 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001439 0,
1440 "list",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001441 sizeof(PyListObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001442 0,
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001443 (destructor)list_dealloc, /*tp_dealloc*/
1444 (printfunc)list_print, /*tp_print*/
1445 (getattrfunc)list_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001446 0, /*tp_setattr*/
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001447 (cmpfunc)list_compare, /*tp_compare*/
1448 (reprfunc)list_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001449 0, /*tp_as_number*/
1450 &list_as_sequence, /*tp_as_sequence*/
1451 0, /*tp_as_mapping*/
1452};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001453
1454
1455/* During a sort, we really can't have anyone modifying the list; it could
1456 cause core dumps. Thus, we substitute a dummy type that raises an
1457 explanatory exception when a modifying operation is used. Caveat:
1458 comparisons may behave differently; but I guess it's a bad idea anyway to
1459 compare a list that's being sorted... */
1460
1461static PyObject *
1462immutable_list_op(/*No args!*/)
1463{
1464 PyErr_SetString(PyExc_TypeError,
1465 "a list cannot be modified while it is being sorted");
1466 return NULL;
1467}
1468
1469static PyMethodDef immutable_list_methods[] = {
1470 {"append", (PyCFunction)immutable_list_op},
1471 {"insert", (PyCFunction)immutable_list_op},
1472 {"remove", (PyCFunction)immutable_list_op},
1473 {"index", (PyCFunction)listindex},
1474 {"count", (PyCFunction)listcount},
1475 {"reverse", (PyCFunction)immutable_list_op},
1476 {"sort", (PyCFunction)immutable_list_op},
1477 {NULL, NULL} /* sentinel */
1478};
1479
1480static PyObject *
1481immutable_list_getattr(f, name)
1482 PyListObject *f;
1483 char *name;
1484{
1485 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
1486}
1487
1488static int
1489immutable_list_ass(/*No args!*/)
1490{
1491 immutable_list_op();
1492 return -1;
1493}
1494
1495static PySequenceMethods immutable_list_as_sequence = {
1496 (inquiry)list_length, /*sq_length*/
1497 (binaryfunc)list_concat, /*sq_concat*/
1498 (intargfunc)list_repeat, /*sq_repeat*/
1499 (intargfunc)list_item, /*sq_item*/
1500 (intintargfunc)list_slice, /*sq_slice*/
1501 (intobjargproc)immutable_list_ass, /*sq_ass_item*/
1502 (intintobjargproc)immutable_list_ass, /*sq_ass_slice*/
1503};
1504
1505static PyTypeObject immutable_list_type = {
1506 PyObject_HEAD_INIT(&PyType_Type)
1507 0,
1508 "list (immutable, during sort)",
1509 sizeof(PyListObject),
1510 0,
1511 0, /*tp_dealloc*/ /* Cannot happen */
1512 (printfunc)list_print, /*tp_print*/
1513 (getattrfunc)immutable_list_getattr, /*tp_getattr*/
1514 0, /*tp_setattr*/
1515 0, /*tp_compare*/ /* Won't be called */
1516 (reprfunc)list_repr, /*tp_repr*/
1517 0, /*tp_as_number*/
1518 &immutable_list_as_sequence, /*tp_as_sequence*/
1519 0, /*tp_as_mapping*/
1520};
Guido van Rossum42812581998-06-17 14:15:44 +00001521