blob: d77b546edb881da88c451557cf2dc095ad13ecdc [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 Rossumbffd6832000-01-20 22:32:56 +000091 _Py_NewReference((PyObject *)op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092 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;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000560 if (!PyArg_ParseTuple(args, "iO:insert", &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 Rossumef93b872000-03-13 15:41:59 +0000565/* Define NO_STRICT_LIST_APPEND to enable multi-argument append() */
566
567#ifndef NO_STRICT_LIST_APPEND
568#define PyArg_ParseTuple_Compat1 PyArg_ParseTuple
569#else
570#define PyArg_ParseTuple_Compat1(args, format, ret) \
571( \
572 PyTuple_GET_SIZE(args) > 1 ? (*ret = args, 1) : \
573 PyTuple_GET_SIZE(args) == 1 ? (*ret = PyTuple_GET_ITEM(args, 0), 1) : \
574 PyArg_ParseTuple(args, format, ret) \
575)
576#endif
577
578
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000580listappend(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000581 PyListObject *self;
582 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000583{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000584 PyObject *v;
Guido van Rossumef93b872000-03-13 15:41:59 +0000585 if (!PyArg_ParseTuple_Compat1(args, "O:append", &v))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000586 return NULL;
587 return ins(self, (int) self->ob_size, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588}
589
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000590static PyObject *
Barry Warsawdedf6d61998-10-09 16:37:25 +0000591listextend(self, args)
592 PyListObject *self;
593 PyObject *args;
594{
595 PyObject *b = NULL, *res = NULL;
596 PyObject **items;
597 int selflen = PyList_GET_SIZE(self);
598 int blen;
599 register int i;
600
Guido van Rossumc00a9382000-02-24 21:48:29 +0000601 if (!PyArg_ParseTuple(args, "O:extend", &b))
Barry Warsawdedf6d61998-10-09 16:37:25 +0000602 return NULL;
603
604 if (!PyList_Check(b)) {
605 PyErr_SetString(PyExc_TypeError,
606 "list.extend() argument must be a list");
607 return NULL;
608 }
609 if (PyList_GET_SIZE(b) == 0) {
610 /* short circuit when b is empty */
611 Py_INCREF(Py_None);
612 return Py_None;
613 }
614 if (self == (PyListObject*)b) {
615 /* as in list_ass_slice() we must special case the
616 * situation: a.extend(a)
617 *
618 * XXX: I think this way ought to be faster than using
619 * list_slice() the way list_ass_slice() does.
620 */
621 b = PyList_New(selflen);
622 if (!b)
623 return NULL;
624 for (i = 0; i < selflen; i++) {
625 PyObject *o = PyList_GET_ITEM(self, i);
626 Py_INCREF(o);
627 PyList_SET_ITEM(b, i, o);
628 }
629 }
630 else
631 /* we want b to have the same refcount semantics for the
632 * Py_XDECREF() in the finally clause regardless of which
633 * branch in the above conditional we took.
634 */
635 Py_INCREF(b);
636
637 blen = PyList_GET_SIZE(b);
638 /* resize a using idiom */
639 items = self->ob_item;
640 NRESIZE(items, PyObject*, selflen + blen);
641 if (items == NULL ) {
642 PyErr_NoMemory();
643 goto finally;
644 }
645 self->ob_item = items;
646
647 /* populate the end self with b's items */
648 for (i = 0; i < blen; i++) {
649 PyObject *o = PyList_GET_ITEM(b, i);
650 Py_INCREF(o);
651 PyList_SET_ITEM(self, self->ob_size++, o);
652 }
653 res = Py_None;
654 Py_INCREF(res);
655 finally:
656 Py_XDECREF(b);
657 return res;
658}
659
660
661static PyObject *
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000662listpop(self, args)
663 PyListObject *self;
664 PyObject *args;
665{
666 int i = -1;
667 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000668 if (!PyArg_ParseTuple(args, "|i:pop", &i))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000669 return NULL;
670 if (self->ob_size == 0) {
671 /* Special-case most common failure cause */
672 PyErr_SetString(PyExc_IndexError, "pop from empty list");
673 return NULL;
674 }
675 if (i < 0)
676 i += self->ob_size;
677 if (i < 0 || i >= self->ob_size) {
678 PyErr_SetString(PyExc_IndexError, "pop index out of range");
679 return NULL;
680 }
681 v = self->ob_item[i];
682 Py_INCREF(v);
683 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
684 Py_DECREF(v);
685 return NULL;
686 }
687 return v;
688}
689
Guido van Rossum3f236de1996-12-10 23:55:39 +0000690/* New quicksort implementation for arrays of object pointers.
691 Thanks to discussions with Tim Peters. */
692
693/* CMPERROR is returned by our comparison function when an error
694 occurred. This is the largest negative integer (0x80000000 on a
695 32-bit system). */
Guido van Rossum19700b61997-03-05 00:45:43 +0000696#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
Guido van Rossum3f236de1996-12-10 23:55:39 +0000697
698/* Comparison function. Takes care of calling a user-supplied
699 comparison function (any callable Python object). Calls the
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000700 standard comparison function, PyObject_Compare(), if the user-
701 supplied function is NULL. */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000702
703static int
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000704docompare(x, y, compare)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000705 PyObject *x;
706 PyObject *y;
707 PyObject *compare;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000708{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000709 PyObject *args, *res;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000710 int i;
711
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000712 if (compare == NULL) {
713 i = PyObject_Compare(x, y);
714 if (i && PyErr_Occurred())
715 i = CMPERROR;
716 return i;
717 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000718
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000719 args = Py_BuildValue("(OO)", x, y);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000720 if (args == NULL)
721 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000722 res = PyEval_CallObject(compare, args);
723 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000724 if (res == NULL)
725 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000726 if (!PyInt_Check(res)) {
727 Py_DECREF(res);
728 PyErr_SetString(PyExc_TypeError,
Guido van Rossum9bcd1d71999-04-19 17:44:39 +0000729 "comparison function must return int");
Guido van Rossum3f236de1996-12-10 23:55:39 +0000730 return CMPERROR;
731 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000732 i = PyInt_AsLong(res);
733 Py_DECREF(res);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000734 if (i < 0)
735 return -1;
736 if (i > 0)
737 return 1;
738 return 0;
739}
740
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000741/* MINSIZE is the smallest array that will get a full-blown samplesort
742 treatment; smaller arrays are sorted using binary insertion. It must
743 be at least 7 for the samplesort implementation to work. Binary
744 insertion does fewer compares, but can suffer O(N**2) data movement.
745 The more expensive compares, the larger MINSIZE should be. */
746#define MINSIZE 100
747
748/* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
749 partition; smaller slices are passed to binarysort. It must be at
750 least 2, and no larger than MINSIZE. Setting it higher reduces the #
751 of compares slowly, but increases the amount of data movement quickly.
752 The value here was chosen assuming a compare costs ~25x more than
753 swapping a pair of memory-resident pointers -- but under that assumption,
754 changing the value by a few dozen more or less has aggregate effect
755 under 1%. So the value is crucial, but not touchy <wink>. */
756#define MINPARTITIONSIZE 40
757
758/* MAXMERGE is the largest number of elements we'll always merge into
759 a known-to-be sorted chunk via binary insertion, regardless of the
760 size of that chunk. Given a chunk of N sorted elements, and a group
761 of K unknowns, the largest K for which it's better to do insertion
762 (than a full-blown sort) is a complicated function of N and K mostly
763 involving the expected number of compares and data moves under each
764 approach, and the relative cost of those operations on a specific
765 architecure. The fixed value here is conservative, and should be a
766 clear win regardless of architecture or N. */
767#define MAXMERGE 15
Guido van Rossum3f236de1996-12-10 23:55:39 +0000768
Guido van Rossum3f236de1996-12-10 23:55:39 +0000769/* STACKSIZE is the size of our work stack. A rough estimate is that
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000770 this allows us to sort arrays of size N where
771 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
772 for arrays of size 2**64. Because we push the biggest partition
773 first, the worst case occurs when all subarrays are always partitioned
774 exactly in two. */
775#define STACKSIZE 60
Guido van Rossum3f236de1996-12-10 23:55:39 +0000776
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000777
778#define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
779
780/* binarysort is the best method for sorting small arrays: it does
781 few compares, but can do data movement quadratic in the number of
782 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000783 [lo, hi) is a contiguous slice of a list, and is sorted via
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000784 binary insertion.
785 On entry, must have lo <= start <= hi, and that [lo, start) is already
786 sorted (pass start == lo if you don't know!).
787 If docompare complains (returns CMPERROR) return -1, else 0.
788 Even in case of error, the output slice will be some permutation of
789 the input (nothing is lost or duplicated).
790*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000791
792static int
Guido van Rossum42812581998-06-17 14:15:44 +0000793binarysort(lo, hi, start, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000794 PyObject **lo;
795 PyObject **hi;
796 PyObject **start;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000797 PyObject *compare;/* Comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000798{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000799 /* assert lo <= start <= hi
800 assert [lo, start) is sorted */
801 register int k;
802 register PyObject **l, **p, **r;
803 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000804
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000805 if (lo == start)
806 ++start;
807 for (; start < hi; ++start) {
808 /* set l to where *start belongs */
809 l = lo;
810 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000811 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000812 do {
813 p = l + ((r - l) >> 1);
814 SETK(pivot, *p);
815 if (k < 0)
816 r = p;
817 else
818 l = p + 1;
819 } while (l < r);
820 /* Pivot should go at l -- slide over to make room.
821 Caution: using memmove is much slower under MSVC 5;
822 we're not usually moving many slots. */
823 for (p = start; p > l; --p)
824 *p = *(p-1);
825 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000826 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000827 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000828
829 fail:
830 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000831}
832
833/* samplesortslice is the sorting workhorse.
Guido van Rossum42812581998-06-17 14:15:44 +0000834 [lo, hi) is a contiguous slice of a list, to be sorted in place.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000835 On entry, must have lo <= hi,
836 If docompare complains (returns CMPERROR) return -1, else 0.
837 Even in case of error, the output slice will be some permutation of
838 the input (nothing is lost or duplicated).
839
840 samplesort is basically quicksort on steroids: a power of 2 close
841 to n/ln(n) is computed, and that many elements (less 1) are picked at
842 random from the array and sorted. These 2**k - 1 elements are then
843 used as preselected pivots for an equal number of quicksort
844 partitioning steps, partitioning the slice into 2**k chunks each of
845 size about ln(n). These small final chunks are then usually handled
846 by binarysort. Note that when k=1, this is roughly the same as an
847 ordinary quicksort using a random pivot, and when k=2 this is roughly
848 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
849 this a "median of n/ln(n)" quicksort. You can also view it as a kind
850 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
851
852 The large number of samples makes a quadratic-time case almost
853 impossible, and asymptotically drives the average-case number of
854 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
855 3 variant) down to N lg N.
856
857 We also play lots of low-level tricks to cut the number of compares.
858
859 Very obscure: To avoid using extra memory, the PPs are stored in the
860 array and shuffled around as partitioning proceeds. At the start of a
861 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
862 adjacent (either on the left or the right!) to a chunk of X elements
863 that are to be partitioned: P X or X P. In either case we need to
864 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
865 left, followed by the PP to be used for this step (that's the middle
866 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
867 P X or X P -> Psmall pivot X Plarge
868 and the order of the PPs must not be altered. It can take a while
869 to realize this isn't trivial! It can take even longer <wink> to
870 understand why the simple code below works, using only 2**(m-1) swaps.
871 The key is that the order of the X elements isn't necessarily
872 preserved: X can end up as some cyclic permutation of its original
873 order. That's OK, because X is unsorted anyway. If the order of X
874 had to be preserved too, the simplest method I know of using O(1)
875 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
876 Since len(X) is typically several times larger than 2**(m-1), that
877 would slow things down.
878*/
879
880struct SamplesortStackNode {
881 /* Represents a slice of the array, from (& including) lo up
882 to (but excluding) hi. "extra" additional & adjacent elements
883 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
884 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
885 already sorted, but nothing is known about the other elements
886 in [lo, hi). |extra| is always one less than a power of 2.
887 When extra is 0, we're out of PPs, and the slice must be
888 sorted by some other means. */
889 PyObject **lo;
890 PyObject **hi;
891 int extra;
892};
893
894/* 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 +0000895 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000896 is undesirable, so cutoff values are canned in the "cutoff" table
897 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
898#define CUTOFFBASE 4
Guido van Rossum3aa23fd1999-01-14 19:01:53 +0000899static long cutoff[] = {
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000900 43, /* smallest N such that k == 4 */
901 106, /* etc */
902 250,
903 576,
904 1298,
905 2885,
906 6339,
907 13805,
908 29843,
909 64116,
910 137030,
911 291554,
912 617916,
913 1305130,
914 2748295,
915 5771662,
916 12091672,
917 25276798,
918 52734615,
919 109820537,
920 228324027,
921 473977813,
922 982548444, /* smallest N such that k == 26 */
923 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
924};
925
926static int
Guido van Rossum42812581998-06-17 14:15:44 +0000927samplesortslice(lo, hi, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000928 PyObject **lo;
929 PyObject **hi;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000930 PyObject *compare;/* Comparison function object, or NULL for default */
931{
932 register PyObject **l, **r;
933 register PyObject *tmp, *pivot;
934 register int k;
935 int n, extra, top, extraOnRight;
936 struct SamplesortStackNode stack[STACKSIZE];
937
938 /* assert lo <= hi */
939 n = hi - lo;
940
941 /* ----------------------------------------------------------
942 * Special cases
943 * --------------------------------------------------------*/
944 if (n < 2)
945 return 0;
946
947 /* Set r to the largest value such that [lo,r) is sorted.
948 This catches the already-sorted case, the all-the-same
949 case, and the appended-a-few-elements-to-a-sorted-list case.
950 If the array is unsorted, we're very likely to get out of
951 the loop fast, so the test is cheap if it doesn't pay off.
952 */
953 /* assert lo < hi */
954 for (r = lo+1; r < hi; ++r) {
955 SETK(*r, *(r-1));
956 if (k < 0)
957 break;
958 }
959 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
960 few unknowns, or few elements in total. */
961 if (hi - r <= MAXMERGE || n < MINSIZE)
Guido van Rossum42812581998-06-17 14:15:44 +0000962 return binarysort(lo, hi, r, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000963
964 /* Check for the array already being reverse-sorted. Typical
965 benchmark-driven silliness <wink>. */
966 /* assert lo < hi */
967 for (r = lo+1; r < hi; ++r) {
968 SETK(*(r-1), *r);
969 if (k < 0)
970 break;
971 }
972 if (hi - r <= MAXMERGE) {
973 /* Reverse the reversed prefix, then insert the tail */
974 PyObject **originalr = r;
975 l = lo;
976 do {
977 --r;
978 tmp = *l; *l = *r; *r = tmp;
979 ++l;
980 } while (l < r);
Guido van Rossum42812581998-06-17 14:15:44 +0000981 return binarysort(lo, hi, originalr, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000982 }
983
984 /* ----------------------------------------------------------
985 * Normal case setup: a large array without obvious pattern.
986 * --------------------------------------------------------*/
987
988 /* extra := a power of 2 ~= n/ln(n), less 1.
989 First find the smallest extra s.t. n < cutoff[extra] */
990 for (extra = 0;
991 extra < sizeof(cutoff) / sizeof(cutoff[0]);
992 ++extra) {
993 if (n < cutoff[extra])
994 break;
995 /* note that if we fall out of the loop, the value of
996 extra still makes *sense*, but may be smaller than
997 we would like (but the array has more than ~= 2**31
998 elements in this case!) */
999 }
1000 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1001 have is CUTOFFBASE-1, so
1002 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1003 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
1004 /* assert extra > 0 and n >= extra */
1005
1006 /* Swap that many values to the start of the array. The
1007 selection of elements is pseudo-random, but the same on
1008 every run (this is intentional! timing algorithm changes is
1009 a pain if timing varies across runs). */
1010 {
1011 unsigned int seed = n / extra; /* arbitrary */
1012 unsigned int i;
1013 for (i = 0; i < (unsigned)extra; ++i) {
1014 /* j := random int in [i, n) */
1015 unsigned int j;
1016 seed = seed * 69069 + 7;
1017 j = i + seed % (n - i);
1018 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
1019 }
1020 }
1021
1022 /* Recursively sort the preselected pivots. */
Guido van Rossum42812581998-06-17 14:15:44 +00001023 if (samplesortslice(lo, lo + extra, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001024 goto fail;
1025
1026 top = 0; /* index of available stack slot */
1027 lo += extra; /* point to first unknown */
1028 extraOnRight = 0; /* the PPs are at the left end */
1029
1030 /* ----------------------------------------------------------
1031 * Partition [lo, hi), and repeat until out of work.
1032 * --------------------------------------------------------*/
1033 for (;;) {
1034 /* assert lo <= hi, so n >= 0 */
1035 n = hi - lo;
1036
1037 /* We may not want, or may not be able, to partition:
1038 If n is small, it's quicker to insert.
1039 If extra is 0, we're out of pivots, and *must* use
1040 another method.
1041 */
1042 if (n < MINPARTITIONSIZE || extra == 0) {
1043 if (n >= MINSIZE) {
1044 /* assert extra == 0
1045 This is rare, since the average size
1046 of a final block is only about
1047 ln(original n). */
Guido van Rossum42812581998-06-17 14:15:44 +00001048 if (samplesortslice(lo, hi, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001049 goto fail;
1050 }
1051 else {
1052 /* Binary insertion should be quicker,
1053 and we can take advantage of the PPs
1054 already being sorted. */
1055 if (extraOnRight && extra) {
1056 /* swap the PPs to the left end */
1057 k = extra;
1058 do {
1059 tmp = *lo;
1060 *lo = *hi;
1061 *hi = tmp;
1062 ++lo; ++hi;
1063 } while (--k);
1064 }
1065 if (binarysort(lo - extra, hi, lo,
Guido van Rossum42812581998-06-17 14:15:44 +00001066 compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001067 goto fail;
1068 }
1069
1070 /* Find another slice to work on. */
1071 if (--top < 0)
1072 break; /* no more -- done! */
1073 lo = stack[top].lo;
1074 hi = stack[top].hi;
1075 extra = stack[top].extra;
1076 extraOnRight = 0;
1077 if (extra < 0) {
1078 extraOnRight = 1;
1079 extra = -extra;
1080 }
1081 continue;
1082 }
1083
1084 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1085 Then our preselected pivot is at (extra-1)/2, and we
1086 want to move the PPs before that to the left end of
1087 the slice, and the PPs after that to the right end.
1088 The following section changes extra, lo, hi, and the
1089 slice such that:
1090 [lo-extra, lo) contains the smaller PPs.
1091 *lo == our PP.
1092 (lo, hi) contains the unknown elements.
1093 [hi, hi+extra) contains the larger PPs.
1094 */
1095 k = extra >>= 1; /* num PPs to move */
1096 if (extraOnRight) {
1097 /* Swap the smaller PPs to the left end.
1098 Note that this loop actually moves k+1 items:
1099 the last is our PP */
1100 do {
1101 tmp = *lo; *lo = *hi; *hi = tmp;
1102 ++lo; ++hi;
1103 } while (k--);
1104 }
1105 else {
1106 /* Swap the larger PPs to the right end. */
1107 while (k--) {
1108 --lo; --hi;
1109 tmp = *lo; *lo = *hi; *hi = tmp;
1110 }
1111 }
1112 --lo; /* *lo is now our PP */
1113 pivot = *lo;
1114
1115 /* Now an almost-ordinary quicksort partition step.
1116 Note that most of the time is spent here!
1117 Only odd thing is that we partition into < and >=,
1118 instead of the usual <= and >=. This helps when
1119 there are lots of duplicates of different values,
1120 because it eventually tends to make subfiles
1121 "pure" (all duplicates), and we special-case for
1122 duplicates later. */
1123 l = lo + 1;
1124 r = hi - 1;
1125 /* assert lo < l < r < hi (small n weeded out above) */
1126
1127 do {
1128 /* slide l right, looking for key >= pivot */
1129 do {
1130 SETK(*l, pivot);
1131 if (k < 0)
1132 ++l;
1133 else
1134 break;
1135 } while (l < r);
1136
1137 /* slide r left, looking for key < pivot */
1138 while (l < r) {
Guido van Rossum42812581998-06-17 14:15:44 +00001139 register PyObject *rval = *r--;
1140 SETK(rval, pivot);
1141 if (k < 0) {
1142 /* swap and advance */
1143 r[1] = *l;
1144 *l++ = rval;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001145 break;
Guido van Rossum42812581998-06-17 14:15:44 +00001146 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001147 }
1148
1149 } while (l < r);
1150
1151 /* assert lo < r <= l < hi
1152 assert r == l or r+1 == l
1153 everything to the left of l is < pivot, and
1154 everything to the right of r is >= pivot */
1155
1156 if (l == r) {
1157 SETK(*r, pivot);
1158 if (k < 0)
1159 ++l;
1160 else
1161 --r;
1162 }
1163 /* assert lo <= r and r+1 == l and l <= hi
1164 assert r == lo or a[r] < pivot
1165 assert a[lo] is pivot
1166 assert l == hi or a[l] >= pivot
1167 Swap the pivot into "the middle", so we can henceforth
1168 ignore it.
1169 */
1170 *lo = *r;
1171 *r = pivot;
1172
1173 /* The following is true now, & will be preserved:
1174 All in [lo,r) are < pivot
1175 All in [r,l) == pivot (& so can be ignored)
1176 All in [l,hi) are >= pivot */
1177
1178 /* Check for duplicates of the pivot. One compare is
1179 wasted if there are no duplicates, but can win big
1180 when there are.
1181 Tricky: we're sticking to "<" compares, so deduce
1182 equality indirectly. We know pivot <= *l, so they're
1183 equal iff not pivot < *l.
1184 */
1185 while (l < hi) {
1186 /* pivot <= *l known */
1187 SETK(pivot, *l);
1188 if (k < 0)
1189 break;
1190 else
1191 /* <= and not < implies == */
1192 ++l;
1193 }
1194
1195 /* assert lo <= r < l <= hi
1196 Partitions are [lo, r) and [l, hi) */
1197
1198 /* push fattest first; remember we still have extra PPs
1199 to the left of the left chunk and to the right of
1200 the right chunk! */
1201 /* assert top < STACKSIZE */
1202 if (r - lo <= hi - l) {
1203 /* second is bigger */
1204 stack[top].lo = l;
1205 stack[top].hi = hi;
1206 stack[top].extra = -extra;
1207 hi = r;
1208 extraOnRight = 0;
1209 }
1210 else {
1211 /* first is bigger */
1212 stack[top].lo = lo;
1213 stack[top].hi = r;
1214 stack[top].extra = extra;
1215 lo = l;
1216 extraOnRight = 1;
1217 }
1218 ++top;
1219
1220 } /* end of partitioning loop */
1221
1222 return 0;
1223
1224 fail:
1225 return -1;
1226}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001227
1228#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001229
1230staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001231
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001232static PyObject *
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001233listsort(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001234 PyListObject *self;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001235 PyObject *args;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001236{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001237 int err;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001238 PyObject *compare = NULL;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001239
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001240 if (args != NULL) {
Guido van Rossumc00a9382000-02-24 21:48:29 +00001241 if (!PyArg_ParseTuple(args, "|O:sort", &compare))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001242 return NULL;
1243 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001244 self->ob_type = &immutable_list_type;
1245 err = samplesortslice(self->ob_item,
1246 self->ob_item + self->ob_size,
Guido van Rossum42812581998-06-17 14:15:44 +00001247 compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001248 self->ob_type = &PyList_Type;
1249 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001250 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001251 Py_INCREF(Py_None);
1252 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001253}
1254
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001255int
1256PyList_Sort(v)
1257 PyObject *v;
1258{
1259 if (v == NULL || !PyList_Check(v)) {
1260 PyErr_BadInternalCall();
1261 return -1;
1262 }
1263 v = listsort((PyListObject *)v, (PyObject *)NULL);
1264 if (v == NULL)
1265 return -1;
1266 Py_DECREF(v);
1267 return 0;
1268}
1269
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001270static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001271listreverse(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001272 PyListObject *self;
1273 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001274{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001275 register PyObject **p, **q;
1276 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001277
Guido van Rossumc00a9382000-02-24 21:48:29 +00001278 if (!PyArg_ParseTuple(args, ":reverse"))
Guido van Rossumed98d481991-03-06 13:07:53 +00001279 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001280
1281 if (self->ob_size > 1) {
1282 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
1283 p < q; p++, q--) {
1284 tmp = *p;
1285 *p = *q;
1286 *q = tmp;
1287 }
1288 }
1289
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001290 Py_INCREF(Py_None);
1291 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001292}
1293
Guido van Rossum84c76f51990-10-30 13:32:20 +00001294int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001295PyList_Reverse(v)
1296 PyObject *v;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001297{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001298 if (v == NULL || !PyList_Check(v)) {
1299 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001300 return -1;
1301 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001302 v = listreverse((PyListObject *)v, (PyObject *)NULL);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001303 if (v == NULL)
1304 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001305 Py_DECREF(v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001306 return 0;
1307}
1308
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001309PyObject *
1310PyList_AsTuple(v)
1311 PyObject *v;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001312{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001313 PyObject *w;
1314 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001315 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001316 if (v == NULL || !PyList_Check(v)) {
1317 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001318 return NULL;
1319 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001320 n = ((PyListObject *)v)->ob_size;
1321 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001322 if (w == NULL)
1323 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001324 p = ((PyTupleObject *)w)->ob_item;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001325 memcpy((ANY *)p,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001326 (ANY *)((PyListObject *)v)->ob_item,
1327 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001328 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001329 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001330 p++;
1331 }
1332 return w;
1333}
1334
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001335static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001336listindex(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001337 PyListObject *self;
1338 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001339{
1340 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001341 PyObject *v;
1342
Guido van Rossumef93b872000-03-13 15:41:59 +00001343 if (!PyArg_ParseTuple_Compat1(args, "O:index", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001344 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001345 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001346 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001347 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001348 if (PyErr_Occurred())
1349 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001350 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001351 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001352 return NULL;
1353}
1354
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001355static PyObject *
Guido van Rossume6f7d181991-10-20 20:20:40 +00001356listcount(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001357 PyListObject *self;
1358 PyObject *args;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001359{
1360 int count = 0;
1361 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001362 PyObject *v;
1363
Guido van Rossumef93b872000-03-13 15:41:59 +00001364 if (!PyArg_ParseTuple_Compat1(args, "O:count", &v))
Guido van Rossume6f7d181991-10-20 20:20:40 +00001365 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001366 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001367 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001368 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001369 if (PyErr_Occurred())
1370 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001371 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001372 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001373}
1374
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001375static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001376listremove(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001377 PyListObject *self;
1378 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001379{
1380 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001381 PyObject *v;
1382
Guido van Rossumef93b872000-03-13 15:41:59 +00001383 if (!PyArg_ParseTuple_Compat1(args, "O:remove", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001384 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001385 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001386 if (PyObject_Compare(self->ob_item[i], v) == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001387 if (list_ass_slice(self, i, i+1,
1388 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001389 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001390 Py_INCREF(Py_None);
1391 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001392 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001393 if (PyErr_Occurred())
1394 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001395 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001396 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001397 return NULL;
1398}
1399
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001400static char append_doc[] =
1401"L.append(object) -- append object to end";
Barry Warsawdedf6d61998-10-09 16:37:25 +00001402static char extend_doc[] =
1403"L.extend(list) -- extend list by appending list elements";
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001404static char insert_doc[] =
1405"L.insert(index, object) -- insert object before index";
1406static char pop_doc[] =
1407"L.pop([index]) -> item -- remove and return item at index (default last)";
1408static char remove_doc[] =
1409"L.remove(value) -- remove first occurrence of value";
1410static char index_doc[] =
1411"L.index(value) -> integer -- return index of first occurrence of value";
1412static char count_doc[] =
1413"L.count(value) -> integer -- return number of occurrences of value";
1414static char reverse_doc[] =
1415"L.reverse() -- reverse *IN PLACE*";
1416static char sort_doc[] =
1417"L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1418
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001419static PyMethodDef list_methods[] = {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001420 {"append", (PyCFunction)listappend, 1, append_doc},
1421 {"insert", (PyCFunction)listinsert, 1, insert_doc},
Barry Warsawdedf6d61998-10-09 16:37:25 +00001422 {"extend", (PyCFunction)listextend, 1, extend_doc},
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001423 {"pop", (PyCFunction)listpop, 1, pop_doc},
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001424 {"remove", (PyCFunction)listremove, 1, remove_doc},
1425 {"index", (PyCFunction)listindex, 1, index_doc},
1426 {"count", (PyCFunction)listcount, 1, count_doc},
1427 {"reverse", (PyCFunction)listreverse, 1, reverse_doc},
1428 {"sort", (PyCFunction)listsort, 1, sort_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001429 {NULL, NULL} /* sentinel */
1430};
1431
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001432static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001433list_getattr(f, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001434 PyListObject *f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001435 char *name;
1436{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001437 return Py_FindMethod(list_methods, (PyObject *)f, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001438}
1439
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001440static PySequenceMethods list_as_sequence = {
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001441 (inquiry)list_length, /*sq_length*/
1442 (binaryfunc)list_concat, /*sq_concat*/
1443 (intargfunc)list_repeat, /*sq_repeat*/
1444 (intargfunc)list_item, /*sq_item*/
1445 (intintargfunc)list_slice, /*sq_slice*/
1446 (intobjargproc)list_ass_item, /*sq_ass_item*/
1447 (intintobjargproc)list_ass_slice, /*sq_ass_slice*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001448};
1449
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001450PyTypeObject PyList_Type = {
1451 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001452 0,
1453 "list",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001454 sizeof(PyListObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001455 0,
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001456 (destructor)list_dealloc, /*tp_dealloc*/
1457 (printfunc)list_print, /*tp_print*/
1458 (getattrfunc)list_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001459 0, /*tp_setattr*/
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001460 (cmpfunc)list_compare, /*tp_compare*/
1461 (reprfunc)list_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001462 0, /*tp_as_number*/
1463 &list_as_sequence, /*tp_as_sequence*/
1464 0, /*tp_as_mapping*/
1465};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001466
1467
1468/* During a sort, we really can't have anyone modifying the list; it could
1469 cause core dumps. Thus, we substitute a dummy type that raises an
1470 explanatory exception when a modifying operation is used. Caveat:
1471 comparisons may behave differently; but I guess it's a bad idea anyway to
1472 compare a list that's being sorted... */
1473
1474static PyObject *
1475immutable_list_op(/*No args!*/)
1476{
1477 PyErr_SetString(PyExc_TypeError,
1478 "a list cannot be modified while it is being sorted");
1479 return NULL;
1480}
1481
1482static PyMethodDef immutable_list_methods[] = {
1483 {"append", (PyCFunction)immutable_list_op},
1484 {"insert", (PyCFunction)immutable_list_op},
1485 {"remove", (PyCFunction)immutable_list_op},
1486 {"index", (PyCFunction)listindex},
1487 {"count", (PyCFunction)listcount},
1488 {"reverse", (PyCFunction)immutable_list_op},
1489 {"sort", (PyCFunction)immutable_list_op},
1490 {NULL, NULL} /* sentinel */
1491};
1492
1493static PyObject *
1494immutable_list_getattr(f, name)
1495 PyListObject *f;
1496 char *name;
1497{
1498 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
1499}
1500
1501static int
1502immutable_list_ass(/*No args!*/)
1503{
1504 immutable_list_op();
1505 return -1;
1506}
1507
1508static PySequenceMethods immutable_list_as_sequence = {
1509 (inquiry)list_length, /*sq_length*/
1510 (binaryfunc)list_concat, /*sq_concat*/
1511 (intargfunc)list_repeat, /*sq_repeat*/
1512 (intargfunc)list_item, /*sq_item*/
1513 (intintargfunc)list_slice, /*sq_slice*/
1514 (intobjargproc)immutable_list_ass, /*sq_ass_item*/
1515 (intintobjargproc)immutable_list_ass, /*sq_ass_slice*/
1516};
1517
1518static PyTypeObject immutable_list_type = {
1519 PyObject_HEAD_INIT(&PyType_Type)
1520 0,
1521 "list (immutable, during sort)",
1522 sizeof(PyListObject),
1523 0,
1524 0, /*tp_dealloc*/ /* Cannot happen */
1525 (printfunc)list_print, /*tp_print*/
1526 (getattrfunc)immutable_list_getattr, /*tp_getattr*/
1527 0, /*tp_setattr*/
1528 0, /*tp_compare*/ /* Won't be called */
1529 (reprfunc)list_repr, /*tp_repr*/
1530 0, /*tp_as_number*/
1531 &immutable_list_as_sequence, /*tp_as_sequence*/
1532 0, /*tp_as_mapping*/
1533};
Guido van Rossum42812581998-06-17 14:15:44 +00001534