blob: e9f12abf29b2fa56847c9312517062752461411a [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 Rossumb18618d2000-05-03 23:44:39 +000073 /* PyObject_NewVar is inlined */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +000074 op = (PyListObject *) PyObject_MALLOC(sizeof(PyListObject)
75 + PyGC_INFO_SIZE);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078 }
79 if (size <= 0) {
80 op->ob_item = NULL;
81 }
82 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +000083 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +000085 PyObject_FREE(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000086 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087 }
88 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000089 PyObject_INIT_VAR(op, &PyList_Type, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 for (i = 0; i < size; i++)
91 op->ob_item[i] = NULL;
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;
Guido van Rossumd724b232000-03-13 16:01:29 +0000218 Py_TRASHCAN_SAFE_BEGIN(op)
Jack Jansen7874d1f1995-01-19 12:09:27 +0000219 if (op->ob_item != NULL) {
Guido van Rossumfa717011999-06-09 15:19:34 +0000220 /* Do it backwards, for Christian Tismer.
221 There's a simple test case where somehow this reduces
222 thrashing when a *very* large list is created and
223 immediately deleted. */
224 i = op->ob_size;
225 while (--i >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226 Py_XDECREF(op->ob_item[i]);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000227 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000228 PyMem_FREE(op->ob_item);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000229 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000230 PyObject_DEL(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000231 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232}
233
Guido van Rossum90933611991-06-07 16:10:43 +0000234static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235list_print(op, fp, flags)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000236 PyListObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237 FILE *fp;
238 int flags;
239{
240 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000241
242 i = Py_ReprEnter((PyObject*)op);
243 if (i != 0) {
244 if (i < 0)
245 return i;
246 fprintf(fp, "[...]");
247 return 0;
248 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249 fprintf(fp, "[");
Guido van Rossum90933611991-06-07 16:10:43 +0000250 for (i = 0; i < op->ob_size; i++) {
251 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252 fprintf(fp, ", ");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000253 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
254 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000255 return -1;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000256 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257 }
258 fprintf(fp, "]");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000259 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000260 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261}
262
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000263static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264list_repr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000265 PyListObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000267 PyObject *s, *comma;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000269
270 i = Py_ReprEnter((PyObject*)v);
271 if (i != 0) {
272 if (i > 0)
273 return PyString_FromString("[...]");
274 return NULL;
275 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000276 s = PyString_FromString("[");
277 comma = PyString_FromString(", ");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278 for (i = 0; i < v->ob_size && s != NULL; i++) {
279 if (i > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280 PyString_Concat(&s, comma);
281 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000283 Py_XDECREF(comma);
284 PyString_ConcatAndDel(&s, PyString_FromString("]"));
Guido van Rossumfb376de1998-04-10 22:47:27 +0000285 Py_ReprLeave((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 return s;
287}
288
289static int
290list_compare(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000291 PyListObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000293 int i;
Guido van Rossumae621ff1998-05-28 20:18:46 +0000294 for (i = 0; i < v->ob_size && i < w->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000295 int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296 if (cmp != 0)
297 return cmp;
298 }
299 return v->ob_size - w->ob_size;
300}
301
302static int
303list_length(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000304 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305{
306 return a->ob_size;
307}
308
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000309
310
311static int
312list_contains(a, el)
313 PyListObject *a;
314 PyObject *el;
315{
316 int i, cmp;
317
318 for (i = 0; i < a->ob_size; ++i) {
319 cmp = PyObject_Compare(el, PyList_GET_ITEM(a, i));
320 if (cmp == 0)
321 return 1;
322 if (PyErr_Occurred())
323 return -1;
324 }
325 return 0;
326}
327
328
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000329static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330list_item(a, i)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332 int i;
333{
334 if (i < 0 || i >= a->ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000335 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000336 indexerr = PyString_FromString(
337 "list index out of range");
338 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339 return NULL;
340 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342 return a->ob_item[i];
343}
344
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000345static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346list_slice(a, ilow, ihigh)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000347 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348 int ilow, ihigh;
349{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000350 PyListObject *np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351 int i;
352 if (ilow < 0)
353 ilow = 0;
354 else if (ilow > a->ob_size)
355 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356 if (ihigh < ilow)
357 ihigh = ilow;
358 else if (ihigh > a->ob_size)
359 ihigh = a->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000360 np = (PyListObject *) PyList_New(ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361 if (np == NULL)
362 return NULL;
363 for (i = ilow; i < ihigh; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364 PyObject *v = a->ob_item[i];
365 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366 np->ob_item[i - ilow] = v;
367 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000368 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369}
370
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000371PyObject *
372PyList_GetSlice(a, ilow, ihigh)
373 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000374 int ilow, ihigh;
375{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000376 if (!PyList_Check(a)) {
377 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000378 return NULL;
379 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000381}
382
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384list_concat(a, bb)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000385 PyListObject *a;
386 PyObject *bb;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387{
388 int size;
389 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000390 PyListObject *np;
391 if (!PyList_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000392 PyErr_Format(PyExc_TypeError,
Fred Drake914a2ed2000-06-01 14:31:03 +0000393 "can only concatenate list (not \"%.200s\") to list",
394 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395 return NULL;
396 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397#define b ((PyListObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398 size = a->ob_size + b->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000399 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000401 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402 }
403 for (i = 0; i < a->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000404 PyObject *v = a->ob_item[i];
405 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406 np->ob_item[i] = v;
407 }
408 for (i = 0; i < b->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000409 PyObject *v = b->ob_item[i];
410 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411 np->ob_item[i + a->ob_size] = v;
412 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000413 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414#undef b
415}
416
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000417static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +0000418list_repeat(a, n)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000419 PyListObject *a;
Guido van Rossumed98d481991-03-06 13:07:53 +0000420 int n;
421{
422 int i, j;
423 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000424 PyListObject *np;
425 PyObject **p;
Guido van Rossumed98d481991-03-06 13:07:53 +0000426 if (n < 0)
427 n = 0;
428 size = a->ob_size * n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000429 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000430 if (np == NULL)
431 return NULL;
432 p = np->ob_item;
433 for (i = 0; i < n; i++) {
434 for (j = 0; j < a->ob_size; j++) {
435 *p = a->ob_item[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000436 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000437 p++;
438 }
439 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000440 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000441}
442
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444list_ass_slice(a, ilow, ihigh, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000447 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000449 /* Because [X]DECREF can recursively invoke list operations on
450 this list, we must postpone all [X]DECREF activity until
451 after the list is back in its canonical shape. Therefore
452 we must allocate an additional array, 'recycle', into which
453 we temporarily copy the items that are deleted from the
454 list. :-( */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455 PyObject **recycle, **p;
456 PyObject **item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457 int n; /* Size of replacement list */
458 int d; /* Change in size */
459 int k; /* Loop index */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000460#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461 if (v == NULL)
462 n = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000463 else if (PyList_Check(v)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000464 n = b->ob_size;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000465 if (a == b) {
466 /* Special case "a[i:j] = a" -- copy b first */
467 int ret;
468 v = list_slice(b, 0, n);
469 ret = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000470 Py_DECREF(v);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000471 return ret;
472 }
473 }
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000474 else {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000475 PyErr_Format(PyExc_TypeError,
476 "must assign list (not \"%.200s\") to slice",
477 v->ob_type->tp_name);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000478 return -1;
479 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480 if (ilow < 0)
481 ilow = 0;
482 else if (ilow > a->ob_size)
483 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484 if (ihigh < ilow)
485 ihigh = ilow;
486 else if (ihigh > a->ob_size)
487 ihigh = a->ob_size;
488 item = a->ob_item;
489 d = n - (ihigh-ilow);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000490 if (ihigh > ilow)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000491 p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000492 else
493 p = recycle = NULL;
494 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000495 for (k = ilow; k < ihigh; k++)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000496 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000497 if (d < 0) {
498 for (/*k = ihigh*/; k < a->ob_size; k++)
499 item[k+d] = item[k];
500 a->ob_size += d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000501 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000502 a->ob_item = item;
503 }
504 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000505 else { /* Insert d items; recycle ihigh-ilow items */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000506 NRESIZE(item, PyObject *, a->ob_size + d);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000507 if (item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000508 if (recycle != NULL)
509 PyMem_DEL(recycle);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000511 return -1;
512 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000513 for (k = a->ob_size; --k >= ihigh; )
514 item[k+d] = item[k];
515 for (/*k = ihigh-1*/; k >= ilow; --k)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000516 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517 a->ob_item = item;
518 a->ob_size += d;
519 }
520 for (k = 0; k < n; k++, ilow++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000521 PyObject *w = b->ob_item[k];
522 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000523 item[ilow] = w;
524 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000525 if (recycle) {
526 while (--p >= recycle)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000527 Py_XDECREF(*p);
528 PyMem_DEL(recycle);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000529 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000530 return 0;
531#undef b
532}
533
Guido van Rossum234f9421993-06-17 12:35:49 +0000534int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000535PyList_SetSlice(a, ilow, ihigh, v)
536 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000537 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000538 PyObject *v;
Guido van Rossum234f9421993-06-17 12:35:49 +0000539{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000540 if (!PyList_Check(a)) {
541 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000542 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000543 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000544 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000545}
546
Guido van Rossum4a450d01991-04-03 19:05:18 +0000547static int
548list_ass_item(a, i, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000549 PyListObject *a;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000550 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000551 PyObject *v;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000552{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000553 PyObject *old_value;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000554 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000555 PyErr_SetString(PyExc_IndexError,
556 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000557 return -1;
558 }
559 if (v == NULL)
560 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000561 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000562 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000563 a->ob_item[i] = v;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000565 return 0;
566}
567
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569ins(self, where, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000570 PyListObject *self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573{
574 if (ins1(self, where, v) != 0)
575 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000576 Py_INCREF(Py_None);
577 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578}
579
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000580static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581listinsert(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582 PyListObject *self;
583 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584{
585 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000587 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588 return NULL;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000589 return ins(self, i, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590}
591
Guido van Rossumef93b872000-03-13 15:41:59 +0000592/* Define NO_STRICT_LIST_APPEND to enable multi-argument append() */
593
594#ifndef NO_STRICT_LIST_APPEND
595#define PyArg_ParseTuple_Compat1 PyArg_ParseTuple
596#else
597#define PyArg_ParseTuple_Compat1(args, format, ret) \
598( \
599 PyTuple_GET_SIZE(args) > 1 ? (*ret = args, 1) : \
600 PyTuple_GET_SIZE(args) == 1 ? (*ret = PyTuple_GET_ITEM(args, 0), 1) : \
601 PyArg_ParseTuple(args, format, ret) \
602)
603#endif
604
605
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607listappend(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000608 PyListObject *self;
609 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000611 PyObject *v;
Guido van Rossumef93b872000-03-13 15:41:59 +0000612 if (!PyArg_ParseTuple_Compat1(args, "O:append", &v))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000613 return NULL;
614 return ins(self, (int) self->ob_size, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000615}
616
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000617static PyObject *
Barry Warsawdedf6d61998-10-09 16:37:25 +0000618listextend(self, args)
619 PyListObject *self;
620 PyObject *args;
621{
622 PyObject *b = NULL, *res = NULL;
623 PyObject **items;
624 int selflen = PyList_GET_SIZE(self);
625 int blen;
626 register int i;
627
Guido van Rossumc00a9382000-02-24 21:48:29 +0000628 if (!PyArg_ParseTuple(args, "O:extend", &b))
Barry Warsawdedf6d61998-10-09 16:37:25 +0000629 return NULL;
630
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000631 b = PySequence_Fast(b, "list.extend() argument must be a sequence");
632 if (!b)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000633 return NULL;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000634
635 if (PyObject_Length(b) == 0)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000636 /* short circuit when b is empty */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000637 goto ok;
638
Barry Warsawdedf6d61998-10-09 16:37:25 +0000639 if (self == (PyListObject*)b) {
640 /* as in list_ass_slice() we must special case the
641 * situation: a.extend(a)
642 *
643 * XXX: I think this way ought to be faster than using
644 * list_slice() the way list_ass_slice() does.
645 */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000646 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000647 b = PyList_New(selflen);
648 if (!b)
649 return NULL;
650 for (i = 0; i < selflen; i++) {
651 PyObject *o = PyList_GET_ITEM(self, i);
652 Py_INCREF(o);
653 PyList_SET_ITEM(b, i, o);
654 }
655 }
Barry Warsawdedf6d61998-10-09 16:37:25 +0000656
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000657 blen = PyObject_Length(b);
658
Barry Warsawdedf6d61998-10-09 16:37:25 +0000659 /* resize a using idiom */
660 items = self->ob_item;
661 NRESIZE(items, PyObject*, selflen + blen);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000662 if (items == NULL) {
Barry Warsawdedf6d61998-10-09 16:37:25 +0000663 PyErr_NoMemory();
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000664 goto failed;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000665 }
666 self->ob_item = items;
667
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000668 /* populate the end of self with b's items */
Barry Warsawdedf6d61998-10-09 16:37:25 +0000669 for (i = 0; i < blen; i++) {
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000670 PyObject *o = PySequence_Fast_GET_ITEM(b, i);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000671 Py_INCREF(o);
672 PyList_SET_ITEM(self, self->ob_size++, o);
673 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000674 ok:
Barry Warsawdedf6d61998-10-09 16:37:25 +0000675 res = Py_None;
676 Py_INCREF(res);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000677 failed:
678 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000679 return res;
680}
681
682
683static PyObject *
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000684listpop(self, args)
685 PyListObject *self;
686 PyObject *args;
687{
688 int i = -1;
689 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000690 if (!PyArg_ParseTuple(args, "|i:pop", &i))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000691 return NULL;
692 if (self->ob_size == 0) {
693 /* Special-case most common failure cause */
694 PyErr_SetString(PyExc_IndexError, "pop from empty list");
695 return NULL;
696 }
697 if (i < 0)
698 i += self->ob_size;
699 if (i < 0 || i >= self->ob_size) {
700 PyErr_SetString(PyExc_IndexError, "pop index out of range");
701 return NULL;
702 }
703 v = self->ob_item[i];
704 Py_INCREF(v);
705 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
706 Py_DECREF(v);
707 return NULL;
708 }
709 return v;
710}
711
Guido van Rossum3f236de1996-12-10 23:55:39 +0000712/* New quicksort implementation for arrays of object pointers.
713 Thanks to discussions with Tim Peters. */
714
715/* CMPERROR is returned by our comparison function when an error
716 occurred. This is the largest negative integer (0x80000000 on a
717 32-bit system). */
Guido van Rossum19700b61997-03-05 00:45:43 +0000718#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
Guido van Rossum3f236de1996-12-10 23:55:39 +0000719
720/* Comparison function. Takes care of calling a user-supplied
721 comparison function (any callable Python object). Calls the
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000722 standard comparison function, PyObject_Compare(), if the user-
723 supplied function is NULL. */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000724
725static int
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000726docompare(x, y, compare)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000727 PyObject *x;
728 PyObject *y;
729 PyObject *compare;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000730{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731 PyObject *args, *res;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000732 int i;
733
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000734 if (compare == NULL) {
735 i = PyObject_Compare(x, y);
736 if (i && PyErr_Occurred())
737 i = CMPERROR;
738 return i;
739 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000740
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000741 args = Py_BuildValue("(OO)", x, y);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000742 if (args == NULL)
743 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744 res = PyEval_CallObject(compare, args);
745 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000746 if (res == NULL)
747 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000748 if (!PyInt_Check(res)) {
749 Py_DECREF(res);
750 PyErr_SetString(PyExc_TypeError,
Guido van Rossum9bcd1d71999-04-19 17:44:39 +0000751 "comparison function must return int");
Guido van Rossum3f236de1996-12-10 23:55:39 +0000752 return CMPERROR;
753 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000754 i = PyInt_AsLong(res);
755 Py_DECREF(res);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000756 if (i < 0)
757 return -1;
758 if (i > 0)
759 return 1;
760 return 0;
761}
762
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000763/* MINSIZE is the smallest array that will get a full-blown samplesort
764 treatment; smaller arrays are sorted using binary insertion. It must
765 be at least 7 for the samplesort implementation to work. Binary
766 insertion does fewer compares, but can suffer O(N**2) data movement.
767 The more expensive compares, the larger MINSIZE should be. */
768#define MINSIZE 100
769
770/* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
771 partition; smaller slices are passed to binarysort. It must be at
772 least 2, and no larger than MINSIZE. Setting it higher reduces the #
773 of compares slowly, but increases the amount of data movement quickly.
774 The value here was chosen assuming a compare costs ~25x more than
775 swapping a pair of memory-resident pointers -- but under that assumption,
776 changing the value by a few dozen more or less has aggregate effect
777 under 1%. So the value is crucial, but not touchy <wink>. */
778#define MINPARTITIONSIZE 40
779
780/* MAXMERGE is the largest number of elements we'll always merge into
781 a known-to-be sorted chunk via binary insertion, regardless of the
782 size of that chunk. Given a chunk of N sorted elements, and a group
783 of K unknowns, the largest K for which it's better to do insertion
784 (than a full-blown sort) is a complicated function of N and K mostly
785 involving the expected number of compares and data moves under each
786 approach, and the relative cost of those operations on a specific
787 architecure. The fixed value here is conservative, and should be a
788 clear win regardless of architecture or N. */
789#define MAXMERGE 15
Guido van Rossum3f236de1996-12-10 23:55:39 +0000790
Guido van Rossum3f236de1996-12-10 23:55:39 +0000791/* STACKSIZE is the size of our work stack. A rough estimate is that
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000792 this allows us to sort arrays of size N where
793 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
794 for arrays of size 2**64. Because we push the biggest partition
795 first, the worst case occurs when all subarrays are always partitioned
796 exactly in two. */
797#define STACKSIZE 60
Guido van Rossum3f236de1996-12-10 23:55:39 +0000798
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000799
800#define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
801
802/* binarysort is the best method for sorting small arrays: it does
803 few compares, but can do data movement quadratic in the number of
804 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000805 [lo, hi) is a contiguous slice of a list, and is sorted via
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000806 binary insertion.
807 On entry, must have lo <= start <= hi, and that [lo, start) is already
808 sorted (pass start == lo if you don't know!).
809 If docompare complains (returns CMPERROR) return -1, else 0.
810 Even in case of error, the output slice will be some permutation of
811 the input (nothing is lost or duplicated).
812*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000813
814static int
Guido van Rossum42812581998-06-17 14:15:44 +0000815binarysort(lo, hi, start, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000816 PyObject **lo;
817 PyObject **hi;
818 PyObject **start;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000819 PyObject *compare;/* Comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000820{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000821 /* assert lo <= start <= hi
822 assert [lo, start) is sorted */
823 register int k;
824 register PyObject **l, **p, **r;
825 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000826
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000827 if (lo == start)
828 ++start;
829 for (; start < hi; ++start) {
830 /* set l to where *start belongs */
831 l = lo;
832 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000833 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000834 do {
835 p = l + ((r - l) >> 1);
836 SETK(pivot, *p);
837 if (k < 0)
838 r = p;
839 else
840 l = p + 1;
841 } while (l < r);
842 /* Pivot should go at l -- slide over to make room.
843 Caution: using memmove is much slower under MSVC 5;
844 we're not usually moving many slots. */
845 for (p = start; p > l; --p)
846 *p = *(p-1);
847 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000848 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000849 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000850
851 fail:
852 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000853}
854
855/* samplesortslice is the sorting workhorse.
Guido van Rossum42812581998-06-17 14:15:44 +0000856 [lo, hi) is a contiguous slice of a list, to be sorted in place.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000857 On entry, must have lo <= hi,
858 If docompare complains (returns CMPERROR) return -1, else 0.
859 Even in case of error, the output slice will be some permutation of
860 the input (nothing is lost or duplicated).
861
862 samplesort is basically quicksort on steroids: a power of 2 close
863 to n/ln(n) is computed, and that many elements (less 1) are picked at
864 random from the array and sorted. These 2**k - 1 elements are then
865 used as preselected pivots for an equal number of quicksort
866 partitioning steps, partitioning the slice into 2**k chunks each of
867 size about ln(n). These small final chunks are then usually handled
868 by binarysort. Note that when k=1, this is roughly the same as an
869 ordinary quicksort using a random pivot, and when k=2 this is roughly
870 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
871 this a "median of n/ln(n)" quicksort. You can also view it as a kind
872 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
873
874 The large number of samples makes a quadratic-time case almost
875 impossible, and asymptotically drives the average-case number of
876 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
877 3 variant) down to N lg N.
878
879 We also play lots of low-level tricks to cut the number of compares.
880
881 Very obscure: To avoid using extra memory, the PPs are stored in the
882 array and shuffled around as partitioning proceeds. At the start of a
883 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
884 adjacent (either on the left or the right!) to a chunk of X elements
885 that are to be partitioned: P X or X P. In either case we need to
886 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
887 left, followed by the PP to be used for this step (that's the middle
888 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
889 P X or X P -> Psmall pivot X Plarge
890 and the order of the PPs must not be altered. It can take a while
891 to realize this isn't trivial! It can take even longer <wink> to
892 understand why the simple code below works, using only 2**(m-1) swaps.
893 The key is that the order of the X elements isn't necessarily
894 preserved: X can end up as some cyclic permutation of its original
895 order. That's OK, because X is unsorted anyway. If the order of X
896 had to be preserved too, the simplest method I know of using O(1)
897 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
898 Since len(X) is typically several times larger than 2**(m-1), that
899 would slow things down.
900*/
901
902struct SamplesortStackNode {
903 /* Represents a slice of the array, from (& including) lo up
904 to (but excluding) hi. "extra" additional & adjacent elements
905 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
906 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
907 already sorted, but nothing is known about the other elements
908 in [lo, hi). |extra| is always one less than a power of 2.
909 When extra is 0, we're out of PPs, and the slice must be
910 sorted by some other means. */
911 PyObject **lo;
912 PyObject **hi;
913 int extra;
914};
915
916/* 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 +0000917 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000918 is undesirable, so cutoff values are canned in the "cutoff" table
919 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
920#define CUTOFFBASE 4
Guido van Rossum3aa23fd1999-01-14 19:01:53 +0000921static long cutoff[] = {
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000922 43, /* smallest N such that k == 4 */
923 106, /* etc */
924 250,
925 576,
926 1298,
927 2885,
928 6339,
929 13805,
930 29843,
931 64116,
932 137030,
933 291554,
934 617916,
935 1305130,
936 2748295,
937 5771662,
938 12091672,
939 25276798,
940 52734615,
941 109820537,
942 228324027,
943 473977813,
944 982548444, /* smallest N such that k == 26 */
945 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
946};
947
948static int
Guido van Rossum42812581998-06-17 14:15:44 +0000949samplesortslice(lo, hi, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000950 PyObject **lo;
951 PyObject **hi;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000952 PyObject *compare;/* Comparison function object, or NULL for default */
953{
954 register PyObject **l, **r;
955 register PyObject *tmp, *pivot;
956 register int k;
957 int n, extra, top, extraOnRight;
958 struct SamplesortStackNode stack[STACKSIZE];
959
960 /* assert lo <= hi */
961 n = hi - lo;
962
963 /* ----------------------------------------------------------
964 * Special cases
965 * --------------------------------------------------------*/
966 if (n < 2)
967 return 0;
968
969 /* Set r to the largest value such that [lo,r) is sorted.
970 This catches the already-sorted case, the all-the-same
971 case, and the appended-a-few-elements-to-a-sorted-list case.
972 If the array is unsorted, we're very likely to get out of
973 the loop fast, so the test is cheap if it doesn't pay off.
974 */
975 /* assert lo < hi */
976 for (r = lo+1; r < hi; ++r) {
977 SETK(*r, *(r-1));
978 if (k < 0)
979 break;
980 }
981 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
982 few unknowns, or few elements in total. */
983 if (hi - r <= MAXMERGE || n < MINSIZE)
Guido van Rossum42812581998-06-17 14:15:44 +0000984 return binarysort(lo, hi, r, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000985
986 /* Check for the array already being reverse-sorted. Typical
987 benchmark-driven silliness <wink>. */
988 /* assert lo < hi */
989 for (r = lo+1; r < hi; ++r) {
990 SETK(*(r-1), *r);
991 if (k < 0)
992 break;
993 }
994 if (hi - r <= MAXMERGE) {
995 /* Reverse the reversed prefix, then insert the tail */
996 PyObject **originalr = r;
997 l = lo;
998 do {
999 --r;
1000 tmp = *l; *l = *r; *r = tmp;
1001 ++l;
1002 } while (l < r);
Guido van Rossum42812581998-06-17 14:15:44 +00001003 return binarysort(lo, hi, originalr, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001004 }
1005
1006 /* ----------------------------------------------------------
1007 * Normal case setup: a large array without obvious pattern.
1008 * --------------------------------------------------------*/
1009
1010 /* extra := a power of 2 ~= n/ln(n), less 1.
1011 First find the smallest extra s.t. n < cutoff[extra] */
1012 for (extra = 0;
1013 extra < sizeof(cutoff) / sizeof(cutoff[0]);
1014 ++extra) {
1015 if (n < cutoff[extra])
1016 break;
1017 /* note that if we fall out of the loop, the value of
1018 extra still makes *sense*, but may be smaller than
1019 we would like (but the array has more than ~= 2**31
1020 elements in this case!) */
1021 }
1022 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1023 have is CUTOFFBASE-1, so
1024 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1025 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
1026 /* assert extra > 0 and n >= extra */
1027
1028 /* Swap that many values to the start of the array. The
1029 selection of elements is pseudo-random, but the same on
1030 every run (this is intentional! timing algorithm changes is
1031 a pain if timing varies across runs). */
1032 {
1033 unsigned int seed = n / extra; /* arbitrary */
1034 unsigned int i;
1035 for (i = 0; i < (unsigned)extra; ++i) {
1036 /* j := random int in [i, n) */
1037 unsigned int j;
1038 seed = seed * 69069 + 7;
1039 j = i + seed % (n - i);
1040 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
1041 }
1042 }
1043
1044 /* Recursively sort the preselected pivots. */
Guido van Rossum42812581998-06-17 14:15:44 +00001045 if (samplesortslice(lo, lo + extra, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001046 goto fail;
1047
1048 top = 0; /* index of available stack slot */
1049 lo += extra; /* point to first unknown */
1050 extraOnRight = 0; /* the PPs are at the left end */
1051
1052 /* ----------------------------------------------------------
1053 * Partition [lo, hi), and repeat until out of work.
1054 * --------------------------------------------------------*/
1055 for (;;) {
1056 /* assert lo <= hi, so n >= 0 */
1057 n = hi - lo;
1058
1059 /* We may not want, or may not be able, to partition:
1060 If n is small, it's quicker to insert.
1061 If extra is 0, we're out of pivots, and *must* use
1062 another method.
1063 */
1064 if (n < MINPARTITIONSIZE || extra == 0) {
1065 if (n >= MINSIZE) {
1066 /* assert extra == 0
1067 This is rare, since the average size
1068 of a final block is only about
1069 ln(original n). */
Guido van Rossum42812581998-06-17 14:15:44 +00001070 if (samplesortslice(lo, hi, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001071 goto fail;
1072 }
1073 else {
1074 /* Binary insertion should be quicker,
1075 and we can take advantage of the PPs
1076 already being sorted. */
1077 if (extraOnRight && extra) {
1078 /* swap the PPs to the left end */
1079 k = extra;
1080 do {
1081 tmp = *lo;
1082 *lo = *hi;
1083 *hi = tmp;
1084 ++lo; ++hi;
1085 } while (--k);
1086 }
1087 if (binarysort(lo - extra, hi, lo,
Guido van Rossum42812581998-06-17 14:15:44 +00001088 compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001089 goto fail;
1090 }
1091
1092 /* Find another slice to work on. */
1093 if (--top < 0)
1094 break; /* no more -- done! */
1095 lo = stack[top].lo;
1096 hi = stack[top].hi;
1097 extra = stack[top].extra;
1098 extraOnRight = 0;
1099 if (extra < 0) {
1100 extraOnRight = 1;
1101 extra = -extra;
1102 }
1103 continue;
1104 }
1105
1106 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1107 Then our preselected pivot is at (extra-1)/2, and we
1108 want to move the PPs before that to the left end of
1109 the slice, and the PPs after that to the right end.
1110 The following section changes extra, lo, hi, and the
1111 slice such that:
1112 [lo-extra, lo) contains the smaller PPs.
1113 *lo == our PP.
1114 (lo, hi) contains the unknown elements.
1115 [hi, hi+extra) contains the larger PPs.
1116 */
1117 k = extra >>= 1; /* num PPs to move */
1118 if (extraOnRight) {
1119 /* Swap the smaller PPs to the left end.
1120 Note that this loop actually moves k+1 items:
1121 the last is our PP */
1122 do {
1123 tmp = *lo; *lo = *hi; *hi = tmp;
1124 ++lo; ++hi;
1125 } while (k--);
1126 }
1127 else {
1128 /* Swap the larger PPs to the right end. */
1129 while (k--) {
1130 --lo; --hi;
1131 tmp = *lo; *lo = *hi; *hi = tmp;
1132 }
1133 }
1134 --lo; /* *lo is now our PP */
1135 pivot = *lo;
1136
1137 /* Now an almost-ordinary quicksort partition step.
1138 Note that most of the time is spent here!
1139 Only odd thing is that we partition into < and >=,
1140 instead of the usual <= and >=. This helps when
1141 there are lots of duplicates of different values,
1142 because it eventually tends to make subfiles
1143 "pure" (all duplicates), and we special-case for
1144 duplicates later. */
1145 l = lo + 1;
1146 r = hi - 1;
1147 /* assert lo < l < r < hi (small n weeded out above) */
1148
1149 do {
1150 /* slide l right, looking for key >= pivot */
1151 do {
1152 SETK(*l, pivot);
1153 if (k < 0)
1154 ++l;
1155 else
1156 break;
1157 } while (l < r);
1158
1159 /* slide r left, looking for key < pivot */
1160 while (l < r) {
Guido van Rossum42812581998-06-17 14:15:44 +00001161 register PyObject *rval = *r--;
1162 SETK(rval, pivot);
1163 if (k < 0) {
1164 /* swap and advance */
1165 r[1] = *l;
1166 *l++ = rval;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001167 break;
Guido van Rossum42812581998-06-17 14:15:44 +00001168 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001169 }
1170
1171 } while (l < r);
1172
1173 /* assert lo < r <= l < hi
1174 assert r == l or r+1 == l
1175 everything to the left of l is < pivot, and
1176 everything to the right of r is >= pivot */
1177
1178 if (l == r) {
1179 SETK(*r, pivot);
1180 if (k < 0)
1181 ++l;
1182 else
1183 --r;
1184 }
1185 /* assert lo <= r and r+1 == l and l <= hi
1186 assert r == lo or a[r] < pivot
1187 assert a[lo] is pivot
1188 assert l == hi or a[l] >= pivot
1189 Swap the pivot into "the middle", so we can henceforth
1190 ignore it.
1191 */
1192 *lo = *r;
1193 *r = pivot;
1194
1195 /* The following is true now, & will be preserved:
1196 All in [lo,r) are < pivot
1197 All in [r,l) == pivot (& so can be ignored)
1198 All in [l,hi) are >= pivot */
1199
1200 /* Check for duplicates of the pivot. One compare is
1201 wasted if there are no duplicates, but can win big
1202 when there are.
1203 Tricky: we're sticking to "<" compares, so deduce
1204 equality indirectly. We know pivot <= *l, so they're
1205 equal iff not pivot < *l.
1206 */
1207 while (l < hi) {
1208 /* pivot <= *l known */
1209 SETK(pivot, *l);
1210 if (k < 0)
1211 break;
1212 else
1213 /* <= and not < implies == */
1214 ++l;
1215 }
1216
1217 /* assert lo <= r < l <= hi
1218 Partitions are [lo, r) and [l, hi) */
1219
1220 /* push fattest first; remember we still have extra PPs
1221 to the left of the left chunk and to the right of
1222 the right chunk! */
1223 /* assert top < STACKSIZE */
1224 if (r - lo <= hi - l) {
1225 /* second is bigger */
1226 stack[top].lo = l;
1227 stack[top].hi = hi;
1228 stack[top].extra = -extra;
1229 hi = r;
1230 extraOnRight = 0;
1231 }
1232 else {
1233 /* first is bigger */
1234 stack[top].lo = lo;
1235 stack[top].hi = r;
1236 stack[top].extra = extra;
1237 lo = l;
1238 extraOnRight = 1;
1239 }
1240 ++top;
1241
1242 } /* end of partitioning loop */
1243
1244 return 0;
1245
1246 fail:
1247 return -1;
1248}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001249
1250#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001251
1252staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001253
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001254static PyObject *
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001255listsort(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001256 PyListObject *self;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001257 PyObject *args;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001258{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001259 int err;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001260 PyObject *compare = NULL;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001261
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001262 if (args != NULL) {
Guido van Rossumc00a9382000-02-24 21:48:29 +00001263 if (!PyArg_ParseTuple(args, "|O:sort", &compare))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001264 return NULL;
1265 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001266 self->ob_type = &immutable_list_type;
1267 err = samplesortslice(self->ob_item,
1268 self->ob_item + self->ob_size,
Guido van Rossum42812581998-06-17 14:15:44 +00001269 compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001270 self->ob_type = &PyList_Type;
1271 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001272 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001273 Py_INCREF(Py_None);
1274 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001275}
1276
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001277int
1278PyList_Sort(v)
1279 PyObject *v;
1280{
1281 if (v == NULL || !PyList_Check(v)) {
1282 PyErr_BadInternalCall();
1283 return -1;
1284 }
1285 v = listsort((PyListObject *)v, (PyObject *)NULL);
1286 if (v == NULL)
1287 return -1;
1288 Py_DECREF(v);
1289 return 0;
1290}
1291
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001292static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001293listreverse(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001294 PyListObject *self;
1295 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001296{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001297 register PyObject **p, **q;
1298 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001299
Guido van Rossumc00a9382000-02-24 21:48:29 +00001300 if (!PyArg_ParseTuple(args, ":reverse"))
Guido van Rossumed98d481991-03-06 13:07:53 +00001301 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001302
1303 if (self->ob_size > 1) {
1304 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
1305 p < q; p++, q--) {
1306 tmp = *p;
1307 *p = *q;
1308 *q = tmp;
1309 }
1310 }
1311
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001312 Py_INCREF(Py_None);
1313 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001314}
1315
Guido van Rossum84c76f51990-10-30 13:32:20 +00001316int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001317PyList_Reverse(v)
1318 PyObject *v;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001319{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001320 if (v == NULL || !PyList_Check(v)) {
1321 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001322 return -1;
1323 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001324 v = listreverse((PyListObject *)v, (PyObject *)NULL);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001325 if (v == NULL)
1326 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001327 Py_DECREF(v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001328 return 0;
1329}
1330
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001331PyObject *
1332PyList_AsTuple(v)
1333 PyObject *v;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001334{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001335 PyObject *w;
1336 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001337 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001338 if (v == NULL || !PyList_Check(v)) {
1339 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001340 return NULL;
1341 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001342 n = ((PyListObject *)v)->ob_size;
1343 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001344 if (w == NULL)
1345 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001346 p = ((PyTupleObject *)w)->ob_item;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001347 memcpy((ANY *)p,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001348 (ANY *)((PyListObject *)v)->ob_item,
1349 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001350 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001351 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001352 p++;
1353 }
1354 return w;
1355}
1356
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001357static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001358listindex(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001359 PyListObject *self;
1360 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001361{
1362 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001363 PyObject *v;
1364
Guido van Rossumef93b872000-03-13 15:41:59 +00001365 if (!PyArg_ParseTuple_Compat1(args, "O:index", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001366 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001367 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001368 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001369 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001370 if (PyErr_Occurred())
1371 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001372 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001373 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001374 return NULL;
1375}
1376
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001377static PyObject *
Guido van Rossume6f7d181991-10-20 20:20:40 +00001378listcount(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001379 PyListObject *self;
1380 PyObject *args;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001381{
1382 int count = 0;
1383 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001384 PyObject *v;
1385
Guido van Rossumef93b872000-03-13 15:41:59 +00001386 if (!PyArg_ParseTuple_Compat1(args, "O:count", &v))
Guido van Rossume6f7d181991-10-20 20:20:40 +00001387 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001388 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001389 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001390 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001391 if (PyErr_Occurred())
1392 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001393 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001394 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001395}
1396
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001397static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001398listremove(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001399 PyListObject *self;
1400 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001401{
1402 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001403 PyObject *v;
1404
Guido van Rossumef93b872000-03-13 15:41:59 +00001405 if (!PyArg_ParseTuple_Compat1(args, "O:remove", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001406 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001407 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001408 if (PyObject_Compare(self->ob_item[i], v) == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001409 if (list_ass_slice(self, i, i+1,
1410 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001411 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001412 Py_INCREF(Py_None);
1413 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001414 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001415 if (PyErr_Occurred())
1416 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001417 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001418 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001419 return NULL;
1420}
1421
Jeremy Hylton8caad492000-06-23 14:18:11 +00001422static int
1423list_traverse(PyListObject *o, visitproc visit, void *arg)
1424{
1425 int i, err;
1426 PyObject *x;
1427
1428 for (i = o->ob_size; --i >= 0; ) {
1429 x = o->ob_item[i];
1430 if (x != NULL) {
1431 err = visit(x, arg);
1432 if (err)
1433 return err;
1434 }
1435 }
1436 return 0;
1437}
1438
1439static int
1440list_clear(PyListObject *lp)
1441{
1442 (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0);
1443 return 0;
1444}
1445
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001446static char append_doc[] =
1447"L.append(object) -- append object to end";
Barry Warsawdedf6d61998-10-09 16:37:25 +00001448static char extend_doc[] =
1449"L.extend(list) -- extend list by appending list elements";
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001450static char insert_doc[] =
1451"L.insert(index, object) -- insert object before index";
1452static char pop_doc[] =
1453"L.pop([index]) -> item -- remove and return item at index (default last)";
1454static char remove_doc[] =
1455"L.remove(value) -- remove first occurrence of value";
1456static char index_doc[] =
1457"L.index(value) -> integer -- return index of first occurrence of value";
1458static char count_doc[] =
1459"L.count(value) -> integer -- return number of occurrences of value";
1460static char reverse_doc[] =
1461"L.reverse() -- reverse *IN PLACE*";
1462static char sort_doc[] =
1463"L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1464
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001465static PyMethodDef list_methods[] = {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001466 {"append", (PyCFunction)listappend, 1, append_doc},
1467 {"insert", (PyCFunction)listinsert, 1, insert_doc},
Barry Warsawdedf6d61998-10-09 16:37:25 +00001468 {"extend", (PyCFunction)listextend, 1, extend_doc},
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001469 {"pop", (PyCFunction)listpop, 1, pop_doc},
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001470 {"remove", (PyCFunction)listremove, 1, remove_doc},
1471 {"index", (PyCFunction)listindex, 1, index_doc},
1472 {"count", (PyCFunction)listcount, 1, count_doc},
1473 {"reverse", (PyCFunction)listreverse, 1, reverse_doc},
1474 {"sort", (PyCFunction)listsort, 1, sort_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001475 {NULL, NULL} /* sentinel */
1476};
1477
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001478static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001479list_getattr(f, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001480 PyListObject *f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001481 char *name;
1482{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001483 return Py_FindMethod(list_methods, (PyObject *)f, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001484}
1485
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001486static PySequenceMethods list_as_sequence = {
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001487 (inquiry)list_length, /*sq_length*/
1488 (binaryfunc)list_concat, /*sq_concat*/
1489 (intargfunc)list_repeat, /*sq_repeat*/
1490 (intargfunc)list_item, /*sq_item*/
1491 (intintargfunc)list_slice, /*sq_slice*/
1492 (intobjargproc)list_ass_item, /*sq_ass_item*/
1493 (intintobjargproc)list_ass_slice, /*sq_ass_slice*/
Jeremy Hylton37b1a262000-04-27 21:41:03 +00001494 (objobjproc)list_contains, /*sq_contains*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001495};
1496
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001497PyTypeObject PyList_Type = {
1498 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001499 0,
1500 "list",
Jeremy Hyltond08b4c42000-06-23 19:37:02 +00001501 sizeof(PyListObject) + PyGC_INFO_SIZE,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001502 0,
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001503 (destructor)list_dealloc, /*tp_dealloc*/
1504 (printfunc)list_print, /*tp_print*/
1505 (getattrfunc)list_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001506 0, /*tp_setattr*/
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001507 (cmpfunc)list_compare, /*tp_compare*/
1508 (reprfunc)list_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001509 0, /*tp_as_number*/
1510 &list_as_sequence, /*tp_as_sequence*/
1511 0, /*tp_as_mapping*/
Fred Drake56780252000-06-15 14:50:20 +00001512 0, /*tp_hash*/
1513 0, /*tp_call*/
1514 0, /*tp_str*/
1515 0, /*tp_getattro*/
1516 0, /*tp_setattro*/
1517 0, /*tp_as_buffer*/
Jeremy Hyltond08b4c42000-06-23 19:37:02 +00001518 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Jeremy Hylton8caad492000-06-23 14:18:11 +00001519 0, /* tp_doc */
1520 (traverseproc)list_traverse, /* tp_traverse */
1521 (inquiry)list_clear, /* tp_clear */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001522};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001523
1524
1525/* During a sort, we really can't have anyone modifying the list; it could
1526 cause core dumps. Thus, we substitute a dummy type that raises an
1527 explanatory exception when a modifying operation is used. Caveat:
1528 comparisons may behave differently; but I guess it's a bad idea anyway to
1529 compare a list that's being sorted... */
1530
1531static PyObject *
1532immutable_list_op(/*No args!*/)
1533{
1534 PyErr_SetString(PyExc_TypeError,
1535 "a list cannot be modified while it is being sorted");
1536 return NULL;
1537}
1538
1539static PyMethodDef immutable_list_methods[] = {
1540 {"append", (PyCFunction)immutable_list_op},
1541 {"insert", (PyCFunction)immutable_list_op},
1542 {"remove", (PyCFunction)immutable_list_op},
1543 {"index", (PyCFunction)listindex},
1544 {"count", (PyCFunction)listcount},
1545 {"reverse", (PyCFunction)immutable_list_op},
1546 {"sort", (PyCFunction)immutable_list_op},
1547 {NULL, NULL} /* sentinel */
1548};
1549
1550static PyObject *
1551immutable_list_getattr(f, name)
1552 PyListObject *f;
1553 char *name;
1554{
1555 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
1556}
1557
1558static int
1559immutable_list_ass(/*No args!*/)
1560{
1561 immutable_list_op();
1562 return -1;
1563}
1564
1565static PySequenceMethods immutable_list_as_sequence = {
1566 (inquiry)list_length, /*sq_length*/
1567 (binaryfunc)list_concat, /*sq_concat*/
1568 (intargfunc)list_repeat, /*sq_repeat*/
1569 (intargfunc)list_item, /*sq_item*/
1570 (intintargfunc)list_slice, /*sq_slice*/
1571 (intobjargproc)immutable_list_ass, /*sq_ass_item*/
1572 (intintobjargproc)immutable_list_ass, /*sq_ass_slice*/
Fred Drake56780252000-06-15 14:50:20 +00001573 (objobjproc)list_contains, /*sq_contains*/
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001574};
1575
1576static PyTypeObject immutable_list_type = {
1577 PyObject_HEAD_INIT(&PyType_Type)
1578 0,
1579 "list (immutable, during sort)",
Jeremy Hyltond08b4c42000-06-23 19:37:02 +00001580 sizeof(PyListObject) + PyGC_INFO_SIZE,
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001581 0,
1582 0, /*tp_dealloc*/ /* Cannot happen */
1583 (printfunc)list_print, /*tp_print*/
1584 (getattrfunc)immutable_list_getattr, /*tp_getattr*/
1585 0, /*tp_setattr*/
1586 0, /*tp_compare*/ /* Won't be called */
1587 (reprfunc)list_repr, /*tp_repr*/
1588 0, /*tp_as_number*/
1589 &immutable_list_as_sequence, /*tp_as_sequence*/
1590 0, /*tp_as_mapping*/
Fred Drake56780252000-06-15 14:50:20 +00001591 0, /*tp_hash*/
1592 0, /*tp_call*/
1593 0, /*tp_str*/
1594 0, /*tp_getattro*/
1595 0, /*tp_setattro*/
1596 0, /*tp_as_buffer*/
Jeremy Hyltond08b4c42000-06-23 19:37:02 +00001597 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Jeremy Hylton8caad492000-06-23 14:18:11 +00001598 0, /* tp_doc */
1599 (traverseproc)list_traverse, /* tp_traverse */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001600};
Guido van Rossum42812581998-06-17 14:15:44 +00001601