blob: 163ba2a8194c48c59f981af00540468c89af5beb [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 */
74 op = (PyListObject *) PyObject_MALLOC(sizeof(PyListObject));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000076 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077 }
78 if (size <= 0) {
79 op->ob_item = NULL;
80 }
81 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +000082 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +000084 PyObject_FREE(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086 }
87 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000088 PyObject_INIT_VAR(op, &PyList_Type, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089 for (i = 0; i < size; i++)
90 op->ob_item[i] = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092}
93
94int
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095PyList_Size(op)
96 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098 if (!PyList_Check(op)) {
99 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100 return -1;
101 }
102 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000103 return ((PyListObject *)op) -> ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104}
105
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000106static PyObject *indexerr;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000107
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000108PyObject *
109PyList_GetItem(op, i)
110 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 int i;
112{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113 if (!PyList_Check(op)) {
114 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115 return NULL;
116 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000117 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000118 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000119 indexerr = PyString_FromString(
120 "list index out of range");
121 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122 return NULL;
123 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000124 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125}
126
127int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000128PyList_SetItem(op, i, newitem)
129 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130 register int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000131 register PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000133 register PyObject *olditem;
134 register PyObject **p;
135 if (!PyList_Check(op)) {
136 Py_XDECREF(newitem);
137 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000138 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000140 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
141 Py_XDECREF(newitem);
142 PyErr_SetString(PyExc_IndexError,
143 "list assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000144 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000146 p = ((PyListObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000147 olditem = *p;
148 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150 return 0;
151}
152
153static int
154ins1(self, where, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000155 PyListObject *self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158{
159 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160 PyObject **items;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000161 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000163 return -1;
164 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165 items = self->ob_item;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000166 NRESIZE(items, PyObject *, self->ob_size+1);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000167 if (items == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000168 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000169 return -1;
170 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171 if (where < 0)
172 where = 0;
173 if (where > self->ob_size)
174 where = self->ob_size;
175 for (i = self->ob_size; --i >= where; )
176 items[i+1] = items[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000177 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000178 items[where] = v;
179 self->ob_item = items;
180 self->ob_size++;
181 return 0;
182}
183
184int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000185PyList_Insert(op, where, newitem)
186 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000188 PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000190 if (!PyList_Check(op)) {
191 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000192 return -1;
193 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000194 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195}
196
197int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000198PyList_Append(op, newitem)
199 PyObject *op;
200 PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000201{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000202 if (!PyList_Check(op)) {
203 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000204 return -1;
205 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000206 return ins1((PyListObject *)op,
207 (int) ((PyListObject *)op)->ob_size, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208}
209
210/* Methods */
211
212static void
213list_dealloc(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 PyListObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000215{
216 int i;
Guido van Rossumd724b232000-03-13 16:01:29 +0000217 Py_TRASHCAN_SAFE_BEGIN(op)
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 Rossumb18618d2000-05-03 23:44:39 +0000227 PyMem_FREE(op->ob_item);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000228 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000229 PyObject_DEL(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000230 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231}
232
Guido van Rossum90933611991-06-07 16:10:43 +0000233static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234list_print(op, fp, flags)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235 PyListObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236 FILE *fp;
237 int flags;
238{
239 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000240
241 i = Py_ReprEnter((PyObject*)op);
242 if (i != 0) {
243 if (i < 0)
244 return i;
245 fprintf(fp, "[...]");
246 return 0;
247 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248 fprintf(fp, "[");
Guido van Rossum90933611991-06-07 16:10:43 +0000249 for (i = 0; i < op->ob_size; i++) {
250 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251 fprintf(fp, ", ");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000252 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
253 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000254 return -1;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000255 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256 }
257 fprintf(fp, "]");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000258 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000259 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260}
261
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000262static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263list_repr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000264 PyListObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000266 PyObject *s, *comma;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000268
269 i = Py_ReprEnter((PyObject*)v);
270 if (i != 0) {
271 if (i > 0)
272 return PyString_FromString("[...]");
273 return NULL;
274 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275 s = PyString_FromString("[");
276 comma = PyString_FromString(", ");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277 for (i = 0; i < v->ob_size && s != NULL; i++) {
278 if (i > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000279 PyString_Concat(&s, comma);
280 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282 Py_XDECREF(comma);
283 PyString_ConcatAndDel(&s, PyString_FromString("]"));
Guido van Rossumfb376de1998-04-10 22:47:27 +0000284 Py_ReprLeave((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285 return s;
286}
287
288static int
289list_compare(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000290 PyListObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292 int i;
Guido van Rossumae621ff1998-05-28 20:18:46 +0000293 for (i = 0; i < v->ob_size && i < w->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000294 int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295 if (cmp != 0)
296 return cmp;
297 }
298 return v->ob_size - w->ob_size;
299}
300
301static int
302list_length(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000303 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304{
305 return a->ob_size;
306}
307
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000308
309
310static int
311list_contains(a, el)
312 PyListObject *a;
313 PyObject *el;
314{
315 int i, cmp;
316
317 for (i = 0; i < a->ob_size; ++i) {
318 cmp = PyObject_Compare(el, PyList_GET_ITEM(a, i));
319 if (cmp == 0)
320 return 1;
321 if (PyErr_Occurred())
322 return -1;
323 }
324 return 0;
325}
326
327
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000328static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329list_item(a, i)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000330 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331 int i;
332{
333 if (i < 0 || i >= a->ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000334 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000335 indexerr = PyString_FromString(
336 "list index out of range");
337 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338 return NULL;
339 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000340 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341 return a->ob_item[i];
342}
343
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000344static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345list_slice(a, ilow, ihigh)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000346 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347 int ilow, ihigh;
348{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000349 PyListObject *np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350 int i;
351 if (ilow < 0)
352 ilow = 0;
353 else if (ilow > a->ob_size)
354 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355 if (ihigh < ilow)
356 ihigh = ilow;
357 else if (ihigh > a->ob_size)
358 ihigh = a->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000359 np = (PyListObject *) PyList_New(ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360 if (np == NULL)
361 return NULL;
362 for (i = ilow; i < ihigh; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000363 PyObject *v = a->ob_item[i];
364 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365 np->ob_item[i - ilow] = v;
366 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000367 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000368}
369
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000370PyObject *
371PyList_GetSlice(a, ilow, ihigh)
372 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000373 int ilow, ihigh;
374{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000375 if (!PyList_Check(a)) {
376 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000377 return NULL;
378 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000379 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000380}
381
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383list_concat(a, bb)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000384 PyListObject *a;
385 PyObject *bb;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386{
387 int size;
388 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389 PyListObject *np;
390 if (!PyList_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000391 PyErr_Format(PyExc_TypeError,
Fred Drake914a2ed2000-06-01 14:31:03 +0000392 "can only concatenate list (not \"%.200s\") to list",
393 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394 return NULL;
395 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396#define b ((PyListObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397 size = a->ob_size + b->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000398 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000400 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401 }
402 for (i = 0; i < a->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000403 PyObject *v = a->ob_item[i];
404 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405 np->ob_item[i] = v;
406 }
407 for (i = 0; i < b->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000408 PyObject *v = b->ob_item[i];
409 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410 np->ob_item[i + a->ob_size] = v;
411 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413#undef b
414}
415
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000416static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +0000417list_repeat(a, n)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000418 PyListObject *a;
Guido van Rossumed98d481991-03-06 13:07:53 +0000419 int n;
420{
421 int i, j;
422 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000423 PyListObject *np;
424 PyObject **p;
Guido van Rossumed98d481991-03-06 13:07:53 +0000425 if (n < 0)
426 n = 0;
427 size = a->ob_size * n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000428 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000429 if (np == NULL)
430 return NULL;
431 p = np->ob_item;
432 for (i = 0; i < n; i++) {
433 for (j = 0; j < a->ob_size; j++) {
434 *p = a->ob_item[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000435 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000436 p++;
437 }
438 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000440}
441
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443list_ass_slice(a, ilow, ihigh, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000444 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000448 /* Because [X]DECREF can recursively invoke list operations on
449 this list, we must postpone all [X]DECREF activity until
450 after the list is back in its canonical shape. Therefore
451 we must allocate an additional array, 'recycle', into which
452 we temporarily copy the items that are deleted from the
453 list. :-( */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454 PyObject **recycle, **p;
455 PyObject **item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456 int n; /* Size of replacement list */
457 int d; /* Change in size */
458 int k; /* Loop index */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000459#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460 if (v == NULL)
461 n = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000462 else if (PyList_Check(v)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463 n = b->ob_size;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000464 if (a == b) {
465 /* Special case "a[i:j] = a" -- copy b first */
466 int ret;
467 v = list_slice(b, 0, n);
468 ret = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000469 Py_DECREF(v);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000470 return ret;
471 }
472 }
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000473 else {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000474 PyErr_Format(PyExc_TypeError,
475 "must assign list (not \"%.200s\") to slice",
476 v->ob_type->tp_name);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000477 return -1;
478 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479 if (ilow < 0)
480 ilow = 0;
481 else if (ilow > a->ob_size)
482 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483 if (ihigh < ilow)
484 ihigh = ilow;
485 else if (ihigh > a->ob_size)
486 ihigh = a->ob_size;
487 item = a->ob_item;
488 d = n - (ihigh-ilow);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000489 if (ihigh > ilow)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000490 p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000491 else
492 p = recycle = NULL;
493 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494 for (k = ilow; k < ihigh; k++)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000495 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496 if (d < 0) {
497 for (/*k = ihigh*/; k < a->ob_size; k++)
498 item[k+d] = item[k];
499 a->ob_size += d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000500 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000501 a->ob_item = item;
502 }
503 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000504 else { /* Insert d items; recycle ihigh-ilow items */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000505 NRESIZE(item, PyObject *, a->ob_size + d);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000506 if (item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000507 if (recycle != NULL)
508 PyMem_DEL(recycle);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000509 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000510 return -1;
511 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512 for (k = a->ob_size; --k >= ihigh; )
513 item[k+d] = item[k];
514 for (/*k = ihigh-1*/; k >= ilow; --k)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000515 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000516 a->ob_item = item;
517 a->ob_size += d;
518 }
519 for (k = 0; k < n; k++, ilow++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000520 PyObject *w = b->ob_item[k];
521 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000522 item[ilow] = w;
523 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000524 if (recycle) {
525 while (--p >= recycle)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 Py_XDECREF(*p);
527 PyMem_DEL(recycle);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000528 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000529 return 0;
530#undef b
531}
532
Guido van Rossum234f9421993-06-17 12:35:49 +0000533int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534PyList_SetSlice(a, ilow, ihigh, v)
535 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000536 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537 PyObject *v;
Guido van Rossum234f9421993-06-17 12:35:49 +0000538{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000539 if (!PyList_Check(a)) {
540 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000541 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000542 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000543 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000544}
545
Guido van Rossum4a450d01991-04-03 19:05:18 +0000546static int
547list_ass_item(a, i, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000548 PyListObject *a;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000549 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000550 PyObject *v;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000551{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552 PyObject *old_value;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000553 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 PyErr_SetString(PyExc_IndexError,
555 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000556 return -1;
557 }
558 if (v == NULL)
559 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000561 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000562 a->ob_item[i] = v;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000563 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000564 return 0;
565}
566
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000567static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568ins(self, where, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 PyListObject *self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000570 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572{
573 if (ins1(self, where, v) != 0)
574 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000575 Py_INCREF(Py_None);
576 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577}
578
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000580listinsert(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{
584 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000585 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000586 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587 return NULL;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000588 return ins(self, i, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589}
590
Guido van Rossumef93b872000-03-13 15:41:59 +0000591/* Define NO_STRICT_LIST_APPEND to enable multi-argument append() */
592
593#ifndef NO_STRICT_LIST_APPEND
594#define PyArg_ParseTuple_Compat1 PyArg_ParseTuple
595#else
596#define PyArg_ParseTuple_Compat1(args, format, ret) \
597( \
598 PyTuple_GET_SIZE(args) > 1 ? (*ret = args, 1) : \
599 PyTuple_GET_SIZE(args) == 1 ? (*ret = PyTuple_GET_ITEM(args, 0), 1) : \
600 PyArg_ParseTuple(args, format, ret) \
601)
602#endif
603
604
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000605static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000606listappend(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000607 PyListObject *self;
608 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000609{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610 PyObject *v;
Guido van Rossumef93b872000-03-13 15:41:59 +0000611 if (!PyArg_ParseTuple_Compat1(args, "O:append", &v))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000612 return NULL;
613 return ins(self, (int) self->ob_size, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000614}
615
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000616static PyObject *
Barry Warsawdedf6d61998-10-09 16:37:25 +0000617listextend(self, args)
618 PyListObject *self;
619 PyObject *args;
620{
621 PyObject *b = NULL, *res = NULL;
622 PyObject **items;
623 int selflen = PyList_GET_SIZE(self);
624 int blen;
625 register int i;
626
Guido van Rossumc00a9382000-02-24 21:48:29 +0000627 if (!PyArg_ParseTuple(args, "O:extend", &b))
Barry Warsawdedf6d61998-10-09 16:37:25 +0000628 return NULL;
629
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000630 b = PySequence_Fast(b, "list.extend() argument must be a sequence");
631 if (!b)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000632 return NULL;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000633
634 if (PyObject_Length(b) == 0)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000635 /* short circuit when b is empty */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000636 goto ok;
637
Barry Warsawdedf6d61998-10-09 16:37:25 +0000638 if (self == (PyListObject*)b) {
639 /* as in list_ass_slice() we must special case the
640 * situation: a.extend(a)
641 *
642 * XXX: I think this way ought to be faster than using
643 * list_slice() the way list_ass_slice() does.
644 */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000645 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000646 b = PyList_New(selflen);
647 if (!b)
648 return NULL;
649 for (i = 0; i < selflen; i++) {
650 PyObject *o = PyList_GET_ITEM(self, i);
651 Py_INCREF(o);
652 PyList_SET_ITEM(b, i, o);
653 }
654 }
Barry Warsawdedf6d61998-10-09 16:37:25 +0000655
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000656 blen = PyObject_Length(b);
657
Barry Warsawdedf6d61998-10-09 16:37:25 +0000658 /* resize a using idiom */
659 items = self->ob_item;
660 NRESIZE(items, PyObject*, selflen + blen);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000661 if (items == NULL) {
Barry Warsawdedf6d61998-10-09 16:37:25 +0000662 PyErr_NoMemory();
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000663 goto failed;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000664 }
665 self->ob_item = items;
666
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000667 /* populate the end of self with b's items */
Barry Warsawdedf6d61998-10-09 16:37:25 +0000668 for (i = 0; i < blen; i++) {
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000669 PyObject *o = PySequence_Fast_GET_ITEM(b, i);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000670 Py_INCREF(o);
671 PyList_SET_ITEM(self, self->ob_size++, o);
672 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000673 ok:
Barry Warsawdedf6d61998-10-09 16:37:25 +0000674 res = Py_None;
675 Py_INCREF(res);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000676 failed:
677 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000678 return res;
679}
680
681
682static PyObject *
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000683listpop(self, args)
684 PyListObject *self;
685 PyObject *args;
686{
687 int i = -1;
688 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000689 if (!PyArg_ParseTuple(args, "|i:pop", &i))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000690 return NULL;
691 if (self->ob_size == 0) {
692 /* Special-case most common failure cause */
693 PyErr_SetString(PyExc_IndexError, "pop from empty list");
694 return NULL;
695 }
696 if (i < 0)
697 i += self->ob_size;
698 if (i < 0 || i >= self->ob_size) {
699 PyErr_SetString(PyExc_IndexError, "pop index out of range");
700 return NULL;
701 }
702 v = self->ob_item[i];
703 Py_INCREF(v);
704 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
705 Py_DECREF(v);
706 return NULL;
707 }
708 return v;
709}
710
Guido van Rossum3f236de1996-12-10 23:55:39 +0000711/* New quicksort implementation for arrays of object pointers.
712 Thanks to discussions with Tim Peters. */
713
714/* CMPERROR is returned by our comparison function when an error
715 occurred. This is the largest negative integer (0x80000000 on a
716 32-bit system). */
Guido van Rossum19700b61997-03-05 00:45:43 +0000717#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
Guido van Rossum3f236de1996-12-10 23:55:39 +0000718
719/* Comparison function. Takes care of calling a user-supplied
720 comparison function (any callable Python object). Calls the
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000721 standard comparison function, PyObject_Compare(), if the user-
722 supplied function is NULL. */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000723
724static int
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000725docompare(x, y, compare)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000726 PyObject *x;
727 PyObject *y;
728 PyObject *compare;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000729{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000730 PyObject *args, *res;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000731 int i;
732
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000733 if (compare == NULL) {
734 i = PyObject_Compare(x, y);
735 if (i && PyErr_Occurred())
736 i = CMPERROR;
737 return i;
738 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000739
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000740 args = Py_BuildValue("(OO)", x, y);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000741 if (args == NULL)
742 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000743 res = PyEval_CallObject(compare, args);
744 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000745 if (res == NULL)
746 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000747 if (!PyInt_Check(res)) {
748 Py_DECREF(res);
749 PyErr_SetString(PyExc_TypeError,
Guido van Rossum9bcd1d71999-04-19 17:44:39 +0000750 "comparison function must return int");
Guido van Rossum3f236de1996-12-10 23:55:39 +0000751 return CMPERROR;
752 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753 i = PyInt_AsLong(res);
754 Py_DECREF(res);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000755 if (i < 0)
756 return -1;
757 if (i > 0)
758 return 1;
759 return 0;
760}
761
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000762/* MINSIZE is the smallest array that will get a full-blown samplesort
763 treatment; smaller arrays are sorted using binary insertion. It must
764 be at least 7 for the samplesort implementation to work. Binary
765 insertion does fewer compares, but can suffer O(N**2) data movement.
766 The more expensive compares, the larger MINSIZE should be. */
767#define MINSIZE 100
768
769/* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
770 partition; smaller slices are passed to binarysort. It must be at
771 least 2, and no larger than MINSIZE. Setting it higher reduces the #
772 of compares slowly, but increases the amount of data movement quickly.
773 The value here was chosen assuming a compare costs ~25x more than
774 swapping a pair of memory-resident pointers -- but under that assumption,
775 changing the value by a few dozen more or less has aggregate effect
776 under 1%. So the value is crucial, but not touchy <wink>. */
777#define MINPARTITIONSIZE 40
778
779/* MAXMERGE is the largest number of elements we'll always merge into
780 a known-to-be sorted chunk via binary insertion, regardless of the
781 size of that chunk. Given a chunk of N sorted elements, and a group
782 of K unknowns, the largest K for which it's better to do insertion
783 (than a full-blown sort) is a complicated function of N and K mostly
784 involving the expected number of compares and data moves under each
785 approach, and the relative cost of those operations on a specific
786 architecure. The fixed value here is conservative, and should be a
787 clear win regardless of architecture or N. */
788#define MAXMERGE 15
Guido van Rossum3f236de1996-12-10 23:55:39 +0000789
Guido van Rossum3f236de1996-12-10 23:55:39 +0000790/* STACKSIZE is the size of our work stack. A rough estimate is that
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000791 this allows us to sort arrays of size N where
792 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
793 for arrays of size 2**64. Because we push the biggest partition
794 first, the worst case occurs when all subarrays are always partitioned
795 exactly in two. */
796#define STACKSIZE 60
Guido van Rossum3f236de1996-12-10 23:55:39 +0000797
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000798
799#define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
800
801/* binarysort is the best method for sorting small arrays: it does
802 few compares, but can do data movement quadratic in the number of
803 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000804 [lo, hi) is a contiguous slice of a list, and is sorted via
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000805 binary insertion.
806 On entry, must have lo <= start <= hi, and that [lo, start) is already
807 sorted (pass start == lo if you don't know!).
808 If docompare complains (returns CMPERROR) return -1, else 0.
809 Even in case of error, the output slice will be some permutation of
810 the input (nothing is lost or duplicated).
811*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000812
813static int
Guido van Rossum42812581998-06-17 14:15:44 +0000814binarysort(lo, hi, start, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000815 PyObject **lo;
816 PyObject **hi;
817 PyObject **start;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818 PyObject *compare;/* Comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000819{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000820 /* assert lo <= start <= hi
821 assert [lo, start) is sorted */
822 register int k;
823 register PyObject **l, **p, **r;
824 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000825
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000826 if (lo == start)
827 ++start;
828 for (; start < hi; ++start) {
829 /* set l to where *start belongs */
830 l = lo;
831 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000832 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000833 do {
834 p = l + ((r - l) >> 1);
835 SETK(pivot, *p);
836 if (k < 0)
837 r = p;
838 else
839 l = p + 1;
840 } while (l < r);
841 /* Pivot should go at l -- slide over to make room.
842 Caution: using memmove is much slower under MSVC 5;
843 we're not usually moving many slots. */
844 for (p = start; p > l; --p)
845 *p = *(p-1);
846 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000847 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000848 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000849
850 fail:
851 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000852}
853
854/* samplesortslice is the sorting workhorse.
Guido van Rossum42812581998-06-17 14:15:44 +0000855 [lo, hi) is a contiguous slice of a list, to be sorted in place.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000856 On entry, must have lo <= hi,
857 If docompare complains (returns CMPERROR) return -1, else 0.
858 Even in case of error, the output slice will be some permutation of
859 the input (nothing is lost or duplicated).
860
861 samplesort is basically quicksort on steroids: a power of 2 close
862 to n/ln(n) is computed, and that many elements (less 1) are picked at
863 random from the array and sorted. These 2**k - 1 elements are then
864 used as preselected pivots for an equal number of quicksort
865 partitioning steps, partitioning the slice into 2**k chunks each of
866 size about ln(n). These small final chunks are then usually handled
867 by binarysort. Note that when k=1, this is roughly the same as an
868 ordinary quicksort using a random pivot, and when k=2 this is roughly
869 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
870 this a "median of n/ln(n)" quicksort. You can also view it as a kind
871 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
872
873 The large number of samples makes a quadratic-time case almost
874 impossible, and asymptotically drives the average-case number of
875 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
876 3 variant) down to N lg N.
877
878 We also play lots of low-level tricks to cut the number of compares.
879
880 Very obscure: To avoid using extra memory, the PPs are stored in the
881 array and shuffled around as partitioning proceeds. At the start of a
882 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
883 adjacent (either on the left or the right!) to a chunk of X elements
884 that are to be partitioned: P X or X P. In either case we need to
885 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
886 left, followed by the PP to be used for this step (that's the middle
887 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
888 P X or X P -> Psmall pivot X Plarge
889 and the order of the PPs must not be altered. It can take a while
890 to realize this isn't trivial! It can take even longer <wink> to
891 understand why the simple code below works, using only 2**(m-1) swaps.
892 The key is that the order of the X elements isn't necessarily
893 preserved: X can end up as some cyclic permutation of its original
894 order. That's OK, because X is unsorted anyway. If the order of X
895 had to be preserved too, the simplest method I know of using O(1)
896 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
897 Since len(X) is typically several times larger than 2**(m-1), that
898 would slow things down.
899*/
900
901struct SamplesortStackNode {
902 /* Represents a slice of the array, from (& including) lo up
903 to (but excluding) hi. "extra" additional & adjacent elements
904 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
905 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
906 already sorted, but nothing is known about the other elements
907 in [lo, hi). |extra| is always one less than a power of 2.
908 When extra is 0, we're out of PPs, and the slice must be
909 sorted by some other means. */
910 PyObject **lo;
911 PyObject **hi;
912 int extra;
913};
914
915/* 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 +0000916 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000917 is undesirable, so cutoff values are canned in the "cutoff" table
918 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
919#define CUTOFFBASE 4
Guido van Rossum3aa23fd1999-01-14 19:01:53 +0000920static long cutoff[] = {
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000921 43, /* smallest N such that k == 4 */
922 106, /* etc */
923 250,
924 576,
925 1298,
926 2885,
927 6339,
928 13805,
929 29843,
930 64116,
931 137030,
932 291554,
933 617916,
934 1305130,
935 2748295,
936 5771662,
937 12091672,
938 25276798,
939 52734615,
940 109820537,
941 228324027,
942 473977813,
943 982548444, /* smallest N such that k == 26 */
944 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
945};
946
947static int
Guido van Rossum42812581998-06-17 14:15:44 +0000948samplesortslice(lo, hi, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000949 PyObject **lo;
950 PyObject **hi;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000951 PyObject *compare;/* Comparison function object, or NULL for default */
952{
953 register PyObject **l, **r;
954 register PyObject *tmp, *pivot;
955 register int k;
956 int n, extra, top, extraOnRight;
957 struct SamplesortStackNode stack[STACKSIZE];
958
959 /* assert lo <= hi */
960 n = hi - lo;
961
962 /* ----------------------------------------------------------
963 * Special cases
964 * --------------------------------------------------------*/
965 if (n < 2)
966 return 0;
967
968 /* Set r to the largest value such that [lo,r) is sorted.
969 This catches the already-sorted case, the all-the-same
970 case, and the appended-a-few-elements-to-a-sorted-list case.
971 If the array is unsorted, we're very likely to get out of
972 the loop fast, so the test is cheap if it doesn't pay off.
973 */
974 /* assert lo < hi */
975 for (r = lo+1; r < hi; ++r) {
976 SETK(*r, *(r-1));
977 if (k < 0)
978 break;
979 }
980 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
981 few unknowns, or few elements in total. */
982 if (hi - r <= MAXMERGE || n < MINSIZE)
Guido van Rossum42812581998-06-17 14:15:44 +0000983 return binarysort(lo, hi, r, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000984
985 /* Check for the array already being reverse-sorted. Typical
986 benchmark-driven silliness <wink>. */
987 /* assert lo < hi */
988 for (r = lo+1; r < hi; ++r) {
989 SETK(*(r-1), *r);
990 if (k < 0)
991 break;
992 }
993 if (hi - r <= MAXMERGE) {
994 /* Reverse the reversed prefix, then insert the tail */
995 PyObject **originalr = r;
996 l = lo;
997 do {
998 --r;
999 tmp = *l; *l = *r; *r = tmp;
1000 ++l;
1001 } while (l < r);
Guido van Rossum42812581998-06-17 14:15:44 +00001002 return binarysort(lo, hi, originalr, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001003 }
1004
1005 /* ----------------------------------------------------------
1006 * Normal case setup: a large array without obvious pattern.
1007 * --------------------------------------------------------*/
1008
1009 /* extra := a power of 2 ~= n/ln(n), less 1.
1010 First find the smallest extra s.t. n < cutoff[extra] */
1011 for (extra = 0;
1012 extra < sizeof(cutoff) / sizeof(cutoff[0]);
1013 ++extra) {
1014 if (n < cutoff[extra])
1015 break;
1016 /* note that if we fall out of the loop, the value of
1017 extra still makes *sense*, but may be smaller than
1018 we would like (but the array has more than ~= 2**31
1019 elements in this case!) */
1020 }
1021 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1022 have is CUTOFFBASE-1, so
1023 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1024 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
1025 /* assert extra > 0 and n >= extra */
1026
1027 /* Swap that many values to the start of the array. The
1028 selection of elements is pseudo-random, but the same on
1029 every run (this is intentional! timing algorithm changes is
1030 a pain if timing varies across runs). */
1031 {
1032 unsigned int seed = n / extra; /* arbitrary */
1033 unsigned int i;
1034 for (i = 0; i < (unsigned)extra; ++i) {
1035 /* j := random int in [i, n) */
1036 unsigned int j;
1037 seed = seed * 69069 + 7;
1038 j = i + seed % (n - i);
1039 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
1040 }
1041 }
1042
1043 /* Recursively sort the preselected pivots. */
Guido van Rossum42812581998-06-17 14:15:44 +00001044 if (samplesortslice(lo, lo + extra, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001045 goto fail;
1046
1047 top = 0; /* index of available stack slot */
1048 lo += extra; /* point to first unknown */
1049 extraOnRight = 0; /* the PPs are at the left end */
1050
1051 /* ----------------------------------------------------------
1052 * Partition [lo, hi), and repeat until out of work.
1053 * --------------------------------------------------------*/
1054 for (;;) {
1055 /* assert lo <= hi, so n >= 0 */
1056 n = hi - lo;
1057
1058 /* We may not want, or may not be able, to partition:
1059 If n is small, it's quicker to insert.
1060 If extra is 0, we're out of pivots, and *must* use
1061 another method.
1062 */
1063 if (n < MINPARTITIONSIZE || extra == 0) {
1064 if (n >= MINSIZE) {
1065 /* assert extra == 0
1066 This is rare, since the average size
1067 of a final block is only about
1068 ln(original n). */
Guido van Rossum42812581998-06-17 14:15:44 +00001069 if (samplesortslice(lo, hi, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001070 goto fail;
1071 }
1072 else {
1073 /* Binary insertion should be quicker,
1074 and we can take advantage of the PPs
1075 already being sorted. */
1076 if (extraOnRight && extra) {
1077 /* swap the PPs to the left end */
1078 k = extra;
1079 do {
1080 tmp = *lo;
1081 *lo = *hi;
1082 *hi = tmp;
1083 ++lo; ++hi;
1084 } while (--k);
1085 }
1086 if (binarysort(lo - extra, hi, lo,
Guido van Rossum42812581998-06-17 14:15:44 +00001087 compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001088 goto fail;
1089 }
1090
1091 /* Find another slice to work on. */
1092 if (--top < 0)
1093 break; /* no more -- done! */
1094 lo = stack[top].lo;
1095 hi = stack[top].hi;
1096 extra = stack[top].extra;
1097 extraOnRight = 0;
1098 if (extra < 0) {
1099 extraOnRight = 1;
1100 extra = -extra;
1101 }
1102 continue;
1103 }
1104
1105 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1106 Then our preselected pivot is at (extra-1)/2, and we
1107 want to move the PPs before that to the left end of
1108 the slice, and the PPs after that to the right end.
1109 The following section changes extra, lo, hi, and the
1110 slice such that:
1111 [lo-extra, lo) contains the smaller PPs.
1112 *lo == our PP.
1113 (lo, hi) contains the unknown elements.
1114 [hi, hi+extra) contains the larger PPs.
1115 */
1116 k = extra >>= 1; /* num PPs to move */
1117 if (extraOnRight) {
1118 /* Swap the smaller PPs to the left end.
1119 Note that this loop actually moves k+1 items:
1120 the last is our PP */
1121 do {
1122 tmp = *lo; *lo = *hi; *hi = tmp;
1123 ++lo; ++hi;
1124 } while (k--);
1125 }
1126 else {
1127 /* Swap the larger PPs to the right end. */
1128 while (k--) {
1129 --lo; --hi;
1130 tmp = *lo; *lo = *hi; *hi = tmp;
1131 }
1132 }
1133 --lo; /* *lo is now our PP */
1134 pivot = *lo;
1135
1136 /* Now an almost-ordinary quicksort partition step.
1137 Note that most of the time is spent here!
1138 Only odd thing is that we partition into < and >=,
1139 instead of the usual <= and >=. This helps when
1140 there are lots of duplicates of different values,
1141 because it eventually tends to make subfiles
1142 "pure" (all duplicates), and we special-case for
1143 duplicates later. */
1144 l = lo + 1;
1145 r = hi - 1;
1146 /* assert lo < l < r < hi (small n weeded out above) */
1147
1148 do {
1149 /* slide l right, looking for key >= pivot */
1150 do {
1151 SETK(*l, pivot);
1152 if (k < 0)
1153 ++l;
1154 else
1155 break;
1156 } while (l < r);
1157
1158 /* slide r left, looking for key < pivot */
1159 while (l < r) {
Guido van Rossum42812581998-06-17 14:15:44 +00001160 register PyObject *rval = *r--;
1161 SETK(rval, pivot);
1162 if (k < 0) {
1163 /* swap and advance */
1164 r[1] = *l;
1165 *l++ = rval;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001166 break;
Guido van Rossum42812581998-06-17 14:15:44 +00001167 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001168 }
1169
1170 } while (l < r);
1171
1172 /* assert lo < r <= l < hi
1173 assert r == l or r+1 == l
1174 everything to the left of l is < pivot, and
1175 everything to the right of r is >= pivot */
1176
1177 if (l == r) {
1178 SETK(*r, pivot);
1179 if (k < 0)
1180 ++l;
1181 else
1182 --r;
1183 }
1184 /* assert lo <= r and r+1 == l and l <= hi
1185 assert r == lo or a[r] < pivot
1186 assert a[lo] is pivot
1187 assert l == hi or a[l] >= pivot
1188 Swap the pivot into "the middle", so we can henceforth
1189 ignore it.
1190 */
1191 *lo = *r;
1192 *r = pivot;
1193
1194 /* The following is true now, & will be preserved:
1195 All in [lo,r) are < pivot
1196 All in [r,l) == pivot (& so can be ignored)
1197 All in [l,hi) are >= pivot */
1198
1199 /* Check for duplicates of the pivot. One compare is
1200 wasted if there are no duplicates, but can win big
1201 when there are.
1202 Tricky: we're sticking to "<" compares, so deduce
1203 equality indirectly. We know pivot <= *l, so they're
1204 equal iff not pivot < *l.
1205 */
1206 while (l < hi) {
1207 /* pivot <= *l known */
1208 SETK(pivot, *l);
1209 if (k < 0)
1210 break;
1211 else
1212 /* <= and not < implies == */
1213 ++l;
1214 }
1215
1216 /* assert lo <= r < l <= hi
1217 Partitions are [lo, r) and [l, hi) */
1218
1219 /* push fattest first; remember we still have extra PPs
1220 to the left of the left chunk and to the right of
1221 the right chunk! */
1222 /* assert top < STACKSIZE */
1223 if (r - lo <= hi - l) {
1224 /* second is bigger */
1225 stack[top].lo = l;
1226 stack[top].hi = hi;
1227 stack[top].extra = -extra;
1228 hi = r;
1229 extraOnRight = 0;
1230 }
1231 else {
1232 /* first is bigger */
1233 stack[top].lo = lo;
1234 stack[top].hi = r;
1235 stack[top].extra = extra;
1236 lo = l;
1237 extraOnRight = 1;
1238 }
1239 ++top;
1240
1241 } /* end of partitioning loop */
1242
1243 return 0;
1244
1245 fail:
1246 return -1;
1247}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001248
1249#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001250
1251staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001252
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001253static PyObject *
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001254listsort(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001255 PyListObject *self;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001256 PyObject *args;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001257{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001258 int err;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001259 PyObject *compare = NULL;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001260
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001261 if (args != NULL) {
Guido van Rossumc00a9382000-02-24 21:48:29 +00001262 if (!PyArg_ParseTuple(args, "|O:sort", &compare))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001263 return NULL;
1264 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001265 self->ob_type = &immutable_list_type;
1266 err = samplesortslice(self->ob_item,
1267 self->ob_item + self->ob_size,
Guido van Rossum42812581998-06-17 14:15:44 +00001268 compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001269 self->ob_type = &PyList_Type;
1270 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001271 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001272 Py_INCREF(Py_None);
1273 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001274}
1275
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001276int
1277PyList_Sort(v)
1278 PyObject *v;
1279{
1280 if (v == NULL || !PyList_Check(v)) {
1281 PyErr_BadInternalCall();
1282 return -1;
1283 }
1284 v = listsort((PyListObject *)v, (PyObject *)NULL);
1285 if (v == NULL)
1286 return -1;
1287 Py_DECREF(v);
1288 return 0;
1289}
1290
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001291static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001292listreverse(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001293 PyListObject *self;
1294 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001295{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001296 register PyObject **p, **q;
1297 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001298
Guido van Rossumc00a9382000-02-24 21:48:29 +00001299 if (!PyArg_ParseTuple(args, ":reverse"))
Guido van Rossumed98d481991-03-06 13:07:53 +00001300 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001301
1302 if (self->ob_size > 1) {
1303 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
1304 p < q; p++, q--) {
1305 tmp = *p;
1306 *p = *q;
1307 *q = tmp;
1308 }
1309 }
1310
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001311 Py_INCREF(Py_None);
1312 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001313}
1314
Guido van Rossum84c76f51990-10-30 13:32:20 +00001315int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001316PyList_Reverse(v)
1317 PyObject *v;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001318{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001319 if (v == NULL || !PyList_Check(v)) {
1320 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001321 return -1;
1322 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001323 v = listreverse((PyListObject *)v, (PyObject *)NULL);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001324 if (v == NULL)
1325 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001326 Py_DECREF(v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001327 return 0;
1328}
1329
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001330PyObject *
1331PyList_AsTuple(v)
1332 PyObject *v;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001333{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001334 PyObject *w;
1335 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001336 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001337 if (v == NULL || !PyList_Check(v)) {
1338 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001339 return NULL;
1340 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001341 n = ((PyListObject *)v)->ob_size;
1342 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001343 if (w == NULL)
1344 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001345 p = ((PyTupleObject *)w)->ob_item;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001346 memcpy((ANY *)p,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001347 (ANY *)((PyListObject *)v)->ob_item,
1348 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001349 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001350 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001351 p++;
1352 }
1353 return w;
1354}
1355
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001356static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001357listindex(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001358 PyListObject *self;
1359 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001360{
1361 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001362 PyObject *v;
1363
Guido van Rossumef93b872000-03-13 15:41:59 +00001364 if (!PyArg_ParseTuple_Compat1(args, "O:index", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001365 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001366 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001367 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001368 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001369 if (PyErr_Occurred())
1370 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001371 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001372 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001373 return NULL;
1374}
1375
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001376static PyObject *
Guido van Rossume6f7d181991-10-20 20:20:40 +00001377listcount(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001378 PyListObject *self;
1379 PyObject *args;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001380{
1381 int count = 0;
1382 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001383 PyObject *v;
1384
Guido van Rossumef93b872000-03-13 15:41:59 +00001385 if (!PyArg_ParseTuple_Compat1(args, "O:count", &v))
Guido van Rossume6f7d181991-10-20 20:20:40 +00001386 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001387 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001388 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001389 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001390 if (PyErr_Occurred())
1391 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001392 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001393 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001394}
1395
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001396static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001397listremove(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001398 PyListObject *self;
1399 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001400{
1401 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001402 PyObject *v;
1403
Guido van Rossumef93b872000-03-13 15:41:59 +00001404 if (!PyArg_ParseTuple_Compat1(args, "O:remove", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001405 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001406 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001407 if (PyObject_Compare(self->ob_item[i], v) == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001408 if (list_ass_slice(self, i, i+1,
1409 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001410 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001411 Py_INCREF(Py_None);
1412 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001413 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001414 if (PyErr_Occurred())
1415 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001416 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001417 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001418 return NULL;
1419}
1420
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001421static char append_doc[] =
1422"L.append(object) -- append object to end";
Barry Warsawdedf6d61998-10-09 16:37:25 +00001423static char extend_doc[] =
1424"L.extend(list) -- extend list by appending list elements";
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001425static char insert_doc[] =
1426"L.insert(index, object) -- insert object before index";
1427static char pop_doc[] =
1428"L.pop([index]) -> item -- remove and return item at index (default last)";
1429static char remove_doc[] =
1430"L.remove(value) -- remove first occurrence of value";
1431static char index_doc[] =
1432"L.index(value) -> integer -- return index of first occurrence of value";
1433static char count_doc[] =
1434"L.count(value) -> integer -- return number of occurrences of value";
1435static char reverse_doc[] =
1436"L.reverse() -- reverse *IN PLACE*";
1437static char sort_doc[] =
1438"L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1439
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001440static PyMethodDef list_methods[] = {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001441 {"append", (PyCFunction)listappend, 1, append_doc},
1442 {"insert", (PyCFunction)listinsert, 1, insert_doc},
Barry Warsawdedf6d61998-10-09 16:37:25 +00001443 {"extend", (PyCFunction)listextend, 1, extend_doc},
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001444 {"pop", (PyCFunction)listpop, 1, pop_doc},
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001445 {"remove", (PyCFunction)listremove, 1, remove_doc},
1446 {"index", (PyCFunction)listindex, 1, index_doc},
1447 {"count", (PyCFunction)listcount, 1, count_doc},
1448 {"reverse", (PyCFunction)listreverse, 1, reverse_doc},
1449 {"sort", (PyCFunction)listsort, 1, sort_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001450 {NULL, NULL} /* sentinel */
1451};
1452
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001453static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001454list_getattr(f, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001455 PyListObject *f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001456 char *name;
1457{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001458 return Py_FindMethod(list_methods, (PyObject *)f, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001459}
1460
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001461static PySequenceMethods list_as_sequence = {
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001462 (inquiry)list_length, /*sq_length*/
1463 (binaryfunc)list_concat, /*sq_concat*/
1464 (intargfunc)list_repeat, /*sq_repeat*/
1465 (intargfunc)list_item, /*sq_item*/
1466 (intintargfunc)list_slice, /*sq_slice*/
1467 (intobjargproc)list_ass_item, /*sq_ass_item*/
1468 (intintobjargproc)list_ass_slice, /*sq_ass_slice*/
Jeremy Hylton37b1a262000-04-27 21:41:03 +00001469 (objobjproc)list_contains, /*sq_contains*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001470};
1471
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001472PyTypeObject PyList_Type = {
1473 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001474 0,
1475 "list",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001476 sizeof(PyListObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001477 0,
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001478 (destructor)list_dealloc, /*tp_dealloc*/
1479 (printfunc)list_print, /*tp_print*/
1480 (getattrfunc)list_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001481 0, /*tp_setattr*/
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001482 (cmpfunc)list_compare, /*tp_compare*/
1483 (reprfunc)list_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001484 0, /*tp_as_number*/
1485 &list_as_sequence, /*tp_as_sequence*/
1486 0, /*tp_as_mapping*/
Fred Drake56780252000-06-15 14:50:20 +00001487 0, /*tp_hash*/
1488 0, /*tp_call*/
1489 0, /*tp_str*/
1490 0, /*tp_getattro*/
1491 0, /*tp_setattro*/
1492 0, /*tp_as_buffer*/
1493 Py_TPFLAGS_DEFAULT, /*tp_flags*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001494};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001495
1496
1497/* During a sort, we really can't have anyone modifying the list; it could
1498 cause core dumps. Thus, we substitute a dummy type that raises an
1499 explanatory exception when a modifying operation is used. Caveat:
1500 comparisons may behave differently; but I guess it's a bad idea anyway to
1501 compare a list that's being sorted... */
1502
1503static PyObject *
1504immutable_list_op(/*No args!*/)
1505{
1506 PyErr_SetString(PyExc_TypeError,
1507 "a list cannot be modified while it is being sorted");
1508 return NULL;
1509}
1510
1511static PyMethodDef immutable_list_methods[] = {
1512 {"append", (PyCFunction)immutable_list_op},
1513 {"insert", (PyCFunction)immutable_list_op},
1514 {"remove", (PyCFunction)immutable_list_op},
1515 {"index", (PyCFunction)listindex},
1516 {"count", (PyCFunction)listcount},
1517 {"reverse", (PyCFunction)immutable_list_op},
1518 {"sort", (PyCFunction)immutable_list_op},
1519 {NULL, NULL} /* sentinel */
1520};
1521
1522static PyObject *
1523immutable_list_getattr(f, name)
1524 PyListObject *f;
1525 char *name;
1526{
1527 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
1528}
1529
1530static int
1531immutable_list_ass(/*No args!*/)
1532{
1533 immutable_list_op();
1534 return -1;
1535}
1536
1537static PySequenceMethods immutable_list_as_sequence = {
1538 (inquiry)list_length, /*sq_length*/
1539 (binaryfunc)list_concat, /*sq_concat*/
1540 (intargfunc)list_repeat, /*sq_repeat*/
1541 (intargfunc)list_item, /*sq_item*/
1542 (intintargfunc)list_slice, /*sq_slice*/
1543 (intobjargproc)immutable_list_ass, /*sq_ass_item*/
1544 (intintobjargproc)immutable_list_ass, /*sq_ass_slice*/
Fred Drake56780252000-06-15 14:50:20 +00001545 (objobjproc)list_contains, /*sq_contains*/
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001546};
1547
1548static PyTypeObject immutable_list_type = {
1549 PyObject_HEAD_INIT(&PyType_Type)
1550 0,
1551 "list (immutable, during sort)",
1552 sizeof(PyListObject),
1553 0,
1554 0, /*tp_dealloc*/ /* Cannot happen */
1555 (printfunc)list_print, /*tp_print*/
1556 (getattrfunc)immutable_list_getattr, /*tp_getattr*/
1557 0, /*tp_setattr*/
1558 0, /*tp_compare*/ /* Won't be called */
1559 (reprfunc)list_repr, /*tp_repr*/
1560 0, /*tp_as_number*/
1561 &immutable_list_as_sequence, /*tp_as_sequence*/
1562 0, /*tp_as_mapping*/
Fred Drake56780252000-06-15 14:50:20 +00001563 0, /*tp_hash*/
1564 0, /*tp_call*/
1565 0, /*tp_str*/
1566 0, /*tp_getattro*/
1567 0, /*tp_setattro*/
1568 0, /*tp_as_buffer*/
1569 Py_TPFLAGS_DEFAULT, /*tp_flags*/
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001570};
Guido van Rossum42812581998-06-17 14:15:44 +00001571