blob: 851e27cc3354fb24721410024cdd9eab5bd50755 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* List object implementation */
33
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034#include "Python.h"
35
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000036#ifdef STDC_HEADERS
37#include <stddef.h>
38#else
39#include <sys/types.h> /* For size_t */
40#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Guido van Rossumc0b618a1997-05-02 03:12:38 +000042#define ROUNDUP(n, PyTryBlock) \
43 ((((n)+(PyTryBlock)-1)/(PyTryBlock))*(PyTryBlock))
Guido van Rossuma46d51d1995-01-26 22:59:43 +000044
45static int
Guido van Rossuma27d1121997-08-25 18:36:23 +000046roundupsize(n)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000047 int n;
48{
49 if (n < 500)
50 return ROUNDUP(n, 10);
51 else
52 return ROUNDUP(n, 100);
53}
54
Guido van Rossuma27d1121997-08-25 18:36:23 +000055#define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems))
Guido van Rossuma46d51d1995-01-26 22:59:43 +000056
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057PyObject *
58PyList_New(size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059 int size;
60{
61 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062 PyListObject *op;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000063 size_t nbytes;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 if (size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 return NULL;
67 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000068 nbytes = size * sizeof(PyObject *);
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000069 /* Check for overflow */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000070 if (nbytes / sizeof(PyObject *) != (size_t)size) {
71 return PyErr_NoMemory();
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000072 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000073 op = (PyListObject *) malloc(sizeof(PyListObject));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 }
77 if (size <= 0) {
78 op->ob_item = NULL;
79 }
80 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000081 op->ob_item = (PyObject **) malloc(nbytes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082 if (op->ob_item == NULL) {
83 free((ANY *)op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 }
86 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 op->ob_type = &PyList_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 op->ob_size = size;
89 for (i = 0; i < size; i++)
90 op->ob_item[i] = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091 _Py_NewReference(op);
92 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093}
94
95int
Guido van Rossumc0b618a1997-05-02 03:12:38 +000096PyList_Size(op)
97 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099 if (!PyList_Check(op)) {
100 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101 return -1;
102 }
103 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000104 return ((PyListObject *)op) -> ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105}
106
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000107static PyObject *indexerr;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000108
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109PyObject *
110PyList_GetItem(op, i)
111 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112 int i;
113{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000114 if (!PyList_Check(op)) {
115 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116 return NULL;
117 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000118 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000119 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 indexerr = PyString_FromString(
121 "list index out of range");
122 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123 return NULL;
124 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000125 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126}
127
128int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129PyList_SetItem(op, i, newitem)
130 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131 register int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000132 register PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134 register PyObject *olditem;
135 register PyObject **p;
136 if (!PyList_Check(op)) {
137 Py_XDECREF(newitem);
138 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000139 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000141 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
142 Py_XDECREF(newitem);
143 PyErr_SetString(PyExc_IndexError,
144 "list assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000145 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000147 p = ((PyListObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000148 olditem = *p;
149 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151 return 0;
152}
153
154static int
155ins1(self, where, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000156 PyListObject *self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000158 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159{
160 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161 PyObject **items;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000162 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000164 return -1;
165 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166 items = self->ob_item;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000167 NRESIZE(items, PyObject *, self->ob_size+1);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000168 if (items == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000169 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000170 return -1;
171 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172 if (where < 0)
173 where = 0;
174 if (where > self->ob_size)
175 where = self->ob_size;
176 for (i = self->ob_size; --i >= where; )
177 items[i+1] = items[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179 items[where] = v;
180 self->ob_item = items;
181 self->ob_size++;
182 return 0;
183}
184
185int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000186PyList_Insert(op, where, newitem)
187 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000189 PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191 if (!PyList_Check(op)) {
192 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000193 return -1;
194 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000195 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000196}
197
198int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000199PyList_Append(op, newitem)
200 PyObject *op;
201 PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 if (!PyList_Check(op)) {
204 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000205 return -1;
206 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 return ins1((PyListObject *)op,
208 (int) ((PyListObject *)op)->ob_size, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209}
210
211/* Methods */
212
213static void
214list_dealloc(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 PyListObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216{
217 int i;
Jack Jansen7874d1f1995-01-19 12:09:27 +0000218 if (op->ob_item != NULL) {
219 for (i = 0; i < op->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000220 Py_XDECREF(op->ob_item[i]);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000221 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222 free((ANY *)op->ob_item);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000223 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224 free((ANY *)op);
225}
226
Guido van Rossum90933611991-06-07 16:10:43 +0000227static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228list_print(op, fp, flags)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229 PyListObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230 FILE *fp;
231 int flags;
232{
233 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000234
235 i = Py_ReprEnter((PyObject*)op);
236 if (i != 0) {
237 if (i < 0)
238 return i;
239 fprintf(fp, "[...]");
240 return 0;
241 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242 fprintf(fp, "[");
Guido van Rossum90933611991-06-07 16:10:43 +0000243 for (i = 0; i < op->ob_size; i++) {
244 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245 fprintf(fp, ", ");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000246 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
247 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000248 return -1;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000249 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 }
251 fprintf(fp, "]");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000252 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000253 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254}
255
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000256static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257list_repr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000258 PyListObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000260 PyObject *s, *comma;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000262
263 i = Py_ReprEnter((PyObject*)v);
264 if (i != 0) {
265 if (i > 0)
266 return PyString_FromString("[...]");
267 return NULL;
268 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000269 s = PyString_FromString("[");
270 comma = PyString_FromString(", ");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271 for (i = 0; i < v->ob_size && s != NULL; i++) {
272 if (i > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000273 PyString_Concat(&s, comma);
274 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000276 Py_XDECREF(comma);
277 PyString_ConcatAndDel(&s, PyString_FromString("]"));
Guido van Rossumfb376de1998-04-10 22:47:27 +0000278 Py_ReprLeave((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279 return s;
280}
281
282static int
283list_compare(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000284 PyListObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 int i;
Guido van Rossumae621ff1998-05-28 20:18:46 +0000287 for (i = 0; i < v->ob_size && i < w->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000288 int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289 if (cmp != 0)
290 return cmp;
291 }
292 return v->ob_size - w->ob_size;
293}
294
295static int
296list_length(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000297 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298{
299 return a->ob_size;
300}
301
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000302static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303list_item(a, i)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000304 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305 int i;
306{
307 if (i < 0 || i >= a->ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000308 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000309 indexerr = PyString_FromString(
310 "list index out of range");
311 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000312 return NULL;
313 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000314 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315 return a->ob_item[i];
316}
317
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000318static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319list_slice(a, ilow, ihigh)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321 int ilow, ihigh;
322{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000323 PyListObject *np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324 int i;
325 if (ilow < 0)
326 ilow = 0;
327 else if (ilow > a->ob_size)
328 ilow = a->ob_size;
329 if (ihigh < 0)
330 ihigh = 0;
331 if (ihigh < ilow)
332 ihigh = ilow;
333 else if (ihigh > a->ob_size)
334 ihigh = a->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000335 np = (PyListObject *) PyList_New(ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336 if (np == NULL)
337 return NULL;
338 for (i = ilow; i < ihigh; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339 PyObject *v = a->ob_item[i];
340 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341 np->ob_item[i - ilow] = v;
342 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000343 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344}
345
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000346PyObject *
347PyList_GetSlice(a, ilow, ihigh)
348 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000349 int ilow, ihigh;
350{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000351 if (!PyList_Check(a)) {
352 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000353 return NULL;
354 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000355 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000356}
357
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000358static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359list_concat(a, bb)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000360 PyListObject *a;
361 PyObject *bb;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362{
363 int size;
364 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000365 PyListObject *np;
366 if (!PyList_Check(bb)) {
367 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000368 return NULL;
369 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000370#define b ((PyListObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371 size = a->ob_size + b->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000372 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000374 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375 }
376 for (i = 0; i < a->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000377 PyObject *v = a->ob_item[i];
378 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000379 np->ob_item[i] = v;
380 }
381 for (i = 0; i < b->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382 PyObject *v = b->ob_item[i];
383 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384 np->ob_item[i + a->ob_size] = v;
385 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387#undef b
388}
389
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000390static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +0000391list_repeat(a, n)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000392 PyListObject *a;
Guido van Rossumed98d481991-03-06 13:07:53 +0000393 int n;
394{
395 int i, j;
396 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397 PyListObject *np;
398 PyObject **p;
Guido van Rossumed98d481991-03-06 13:07:53 +0000399 if (n < 0)
400 n = 0;
401 size = a->ob_size * n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000403 if (np == NULL)
404 return NULL;
405 p = np->ob_item;
406 for (i = 0; i < n; i++) {
407 for (j = 0; j < a->ob_size; j++) {
408 *p = a->ob_item[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000409 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000410 p++;
411 }
412 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000413 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000414}
415
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000417list_ass_slice(a, ilow, ihigh, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000418 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000420 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000421{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000422 /* Because [X]DECREF can recursively invoke list operations on
423 this list, we must postpone all [X]DECREF activity until
424 after the list is back in its canonical shape. Therefore
425 we must allocate an additional array, 'recycle', into which
426 we temporarily copy the items that are deleted from the
427 list. :-( */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000428 PyObject **recycle, **p;
429 PyObject **item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430 int n; /* Size of replacement list */
431 int d; /* Change in size */
432 int k; /* Loop index */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000433#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434 if (v == NULL)
435 n = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000436 else if (PyList_Check(v)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437 n = b->ob_size;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000438 if (a == b) {
439 /* Special case "a[i:j] = a" -- copy b first */
440 int ret;
441 v = list_slice(b, 0, n);
442 ret = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443 Py_DECREF(v);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000444 return ret;
445 }
446 }
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000447 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000448 PyErr_BadArgument();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000449 return -1;
450 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451 if (ilow < 0)
452 ilow = 0;
453 else if (ilow > a->ob_size)
454 ilow = a->ob_size;
455 if (ihigh < 0)
456 ihigh = 0;
457 if (ihigh < ilow)
458 ihigh = ilow;
459 else if (ihigh > a->ob_size)
460 ihigh = a->ob_size;
461 item = a->ob_item;
462 d = n - (ihigh-ilow);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000463 if (ihigh > ilow)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464 p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000465 else
466 p = recycle = NULL;
467 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468 for (k = ilow; k < ihigh; k++)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000469 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000470 if (d < 0) {
471 for (/*k = ihigh*/; k < a->ob_size; k++)
472 item[k+d] = item[k];
473 a->ob_size += d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000474 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000475 a->ob_item = item;
476 }
477 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000478 else { /* Insert d items; recycle ihigh-ilow items */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000479 NRESIZE(item, PyObject *, a->ob_size + d);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000480 if (item == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000481 PyMem_XDEL(recycle);
482 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000483 return -1;
484 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000485 for (k = a->ob_size; --k >= ihigh; )
486 item[k+d] = item[k];
487 for (/*k = ihigh-1*/; k >= ilow; --k)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000488 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489 a->ob_item = item;
490 a->ob_size += d;
491 }
492 for (k = 0; k < n; k++, ilow++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000493 PyObject *w = b->ob_item[k];
494 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000495 item[ilow] = w;
496 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000497 if (recycle) {
498 while (--p >= recycle)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000499 Py_XDECREF(*p);
500 PyMem_DEL(recycle);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000501 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000502 return 0;
503#undef b
504}
505
Guido van Rossum234f9421993-06-17 12:35:49 +0000506int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000507PyList_SetSlice(a, ilow, ihigh, v)
508 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000509 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510 PyObject *v;
Guido van Rossum234f9421993-06-17 12:35:49 +0000511{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000512 if (!PyList_Check(a)) {
513 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000514 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000515 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000516 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000517}
518
Guido van Rossum4a450d01991-04-03 19:05:18 +0000519static int
520list_ass_item(a, i, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000521 PyListObject *a;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000522 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523 PyObject *v;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000524{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000525 PyObject *old_value;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000526 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000527 PyErr_SetString(PyExc_IndexError,
528 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000529 return -1;
530 }
531 if (v == NULL)
532 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000533 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000534 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000535 a->ob_item[i] = v;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000537 return 0;
538}
539
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000540static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000541ins(self, where, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000542 PyListObject *self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000544 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545{
546 if (ins1(self, where, v) != 0)
547 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000548 Py_INCREF(Py_None);
549 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550}
551
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553listinsert(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 PyListObject *self;
555 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000556{
557 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 PyObject *v;
559 if (!PyArg_Parse(args, "(iO)", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560 return NULL;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000561 return ins(self, i, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000562}
563
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565listappend(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000566 PyListObject *self;
567 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 PyObject *v;
570 if (!PyArg_Parse(args, "O", &v))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000571 return NULL;
572 return ins(self, (int) self->ob_size, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573}
574
Guido van Rossum3f236de1996-12-10 23:55:39 +0000575/* New quicksort implementation for arrays of object pointers.
576 Thanks to discussions with Tim Peters. */
577
578/* CMPERROR is returned by our comparison function when an error
579 occurred. This is the largest negative integer (0x80000000 on a
580 32-bit system). */
Guido van Rossum19700b61997-03-05 00:45:43 +0000581#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
Guido van Rossum3f236de1996-12-10 23:55:39 +0000582
583/* Comparison function. Takes care of calling a user-supplied
584 comparison function (any callable Python object). Calls the
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000585 standard comparison function, PyObject_Compare(), if the user-
586 supplied function is NULL. */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000587
588static int
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000589docompare(x, y, compare)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590 PyObject *x;
591 PyObject *y;
592 PyObject *compare;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000593{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594 PyObject *args, *res;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000595 int i;
596
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000597 if (compare == NULL) {
598 i = PyObject_Compare(x, y);
599 if (i && PyErr_Occurred())
600 i = CMPERROR;
601 return i;
602 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000603
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000604 args = Py_BuildValue("(OO)", x, y);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000605 if (args == NULL)
606 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000607 res = PyEval_CallObject(compare, args);
608 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000609 if (res == NULL)
610 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000611 if (!PyInt_Check(res)) {
612 Py_DECREF(res);
613 PyErr_SetString(PyExc_TypeError,
614 "comparison function should return int");
Guido van Rossum3f236de1996-12-10 23:55:39 +0000615 return CMPERROR;
616 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000617 i = PyInt_AsLong(res);
618 Py_DECREF(res);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000619 if (i < 0)
620 return -1;
621 if (i > 0)
622 return 1;
623 return 0;
624}
625
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000626/* MINSIZE is the smallest array that will get a full-blown samplesort
627 treatment; smaller arrays are sorted using binary insertion. It must
628 be at least 7 for the samplesort implementation to work. Binary
629 insertion does fewer compares, but can suffer O(N**2) data movement.
630 The more expensive compares, the larger MINSIZE should be. */
631#define MINSIZE 100
632
633/* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
634 partition; smaller slices are passed to binarysort. It must be at
635 least 2, and no larger than MINSIZE. Setting it higher reduces the #
636 of compares slowly, but increases the amount of data movement quickly.
637 The value here was chosen assuming a compare costs ~25x more than
638 swapping a pair of memory-resident pointers -- but under that assumption,
639 changing the value by a few dozen more or less has aggregate effect
640 under 1%. So the value is crucial, but not touchy <wink>. */
641#define MINPARTITIONSIZE 40
642
643/* MAXMERGE is the largest number of elements we'll always merge into
644 a known-to-be sorted chunk via binary insertion, regardless of the
645 size of that chunk. Given a chunk of N sorted elements, and a group
646 of K unknowns, the largest K for which it's better to do insertion
647 (than a full-blown sort) is a complicated function of N and K mostly
648 involving the expected number of compares and data moves under each
649 approach, and the relative cost of those operations on a specific
650 architecure. The fixed value here is conservative, and should be a
651 clear win regardless of architecture or N. */
652#define MAXMERGE 15
Guido van Rossum3f236de1996-12-10 23:55:39 +0000653
Guido van Rossum3f236de1996-12-10 23:55:39 +0000654/* STACKSIZE is the size of our work stack. A rough estimate is that
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000655 this allows us to sort arrays of size N where
656 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
657 for arrays of size 2**64. Because we push the biggest partition
658 first, the worst case occurs when all subarrays are always partitioned
659 exactly in two. */
660#define STACKSIZE 60
Guido van Rossum3f236de1996-12-10 23:55:39 +0000661
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000662
663#define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
664
665/* binarysort is the best method for sorting small arrays: it does
666 few compares, but can do data movement quadratic in the number of
667 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000668 [lo, hi) is a contiguous slice of a list, and is sorted via
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000669 binary insertion.
670 On entry, must have lo <= start <= hi, and that [lo, start) is already
671 sorted (pass start == lo if you don't know!).
672 If docompare complains (returns CMPERROR) return -1, else 0.
673 Even in case of error, the output slice will be some permutation of
674 the input (nothing is lost or duplicated).
675*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000676
677static int
Guido van Rossum42812581998-06-17 14:15:44 +0000678binarysort(lo, hi, start, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000679 PyObject **lo;
680 PyObject **hi;
681 PyObject **start;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000682 PyObject *compare;/* Comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000683{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000684 /* assert lo <= start <= hi
685 assert [lo, start) is sorted */
686 register int k;
687 register PyObject **l, **p, **r;
688 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000689
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000690 if (lo == start)
691 ++start;
692 for (; start < hi; ++start) {
693 /* set l to where *start belongs */
694 l = lo;
695 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000696 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000697 do {
698 p = l + ((r - l) >> 1);
699 SETK(pivot, *p);
700 if (k < 0)
701 r = p;
702 else
703 l = p + 1;
704 } while (l < r);
705 /* Pivot should go at l -- slide over to make room.
706 Caution: using memmove is much slower under MSVC 5;
707 we're not usually moving many slots. */
708 for (p = start; p > l; --p)
709 *p = *(p-1);
710 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000711 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000712 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000713
714 fail:
715 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000716}
717
718/* samplesortslice is the sorting workhorse.
Guido van Rossum42812581998-06-17 14:15:44 +0000719 [lo, hi) is a contiguous slice of a list, to be sorted in place.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000720 On entry, must have lo <= hi,
721 If docompare complains (returns CMPERROR) return -1, else 0.
722 Even in case of error, the output slice will be some permutation of
723 the input (nothing is lost or duplicated).
724
725 samplesort is basically quicksort on steroids: a power of 2 close
726 to n/ln(n) is computed, and that many elements (less 1) are picked at
727 random from the array and sorted. These 2**k - 1 elements are then
728 used as preselected pivots for an equal number of quicksort
729 partitioning steps, partitioning the slice into 2**k chunks each of
730 size about ln(n). These small final chunks are then usually handled
731 by binarysort. Note that when k=1, this is roughly the same as an
732 ordinary quicksort using a random pivot, and when k=2 this is roughly
733 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
734 this a "median of n/ln(n)" quicksort. You can also view it as a kind
735 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
736
737 The large number of samples makes a quadratic-time case almost
738 impossible, and asymptotically drives the average-case number of
739 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
740 3 variant) down to N lg N.
741
742 We also play lots of low-level tricks to cut the number of compares.
743
744 Very obscure: To avoid using extra memory, the PPs are stored in the
745 array and shuffled around as partitioning proceeds. At the start of a
746 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
747 adjacent (either on the left or the right!) to a chunk of X elements
748 that are to be partitioned: P X or X P. In either case we need to
749 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
750 left, followed by the PP to be used for this step (that's the middle
751 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
752 P X or X P -> Psmall pivot X Plarge
753 and the order of the PPs must not be altered. It can take a while
754 to realize this isn't trivial! It can take even longer <wink> to
755 understand why the simple code below works, using only 2**(m-1) swaps.
756 The key is that the order of the X elements isn't necessarily
757 preserved: X can end up as some cyclic permutation of its original
758 order. That's OK, because X is unsorted anyway. If the order of X
759 had to be preserved too, the simplest method I know of using O(1)
760 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
761 Since len(X) is typically several times larger than 2**(m-1), that
762 would slow things down.
763*/
764
765struct SamplesortStackNode {
766 /* Represents a slice of the array, from (& including) lo up
767 to (but excluding) hi. "extra" additional & adjacent elements
768 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
769 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
770 already sorted, but nothing is known about the other elements
771 in [lo, hi). |extra| is always one less than a power of 2.
772 When extra is 0, we're out of PPs, and the slice must be
773 sorted by some other means. */
774 PyObject **lo;
775 PyObject **hi;
776 int extra;
777};
778
779/* 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 +0000780 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000781 is undesirable, so cutoff values are canned in the "cutoff" table
782 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
783#define CUTOFFBASE 4
784static int cutoff[] = {
785 43, /* smallest N such that k == 4 */
786 106, /* etc */
787 250,
788 576,
789 1298,
790 2885,
791 6339,
792 13805,
793 29843,
794 64116,
795 137030,
796 291554,
797 617916,
798 1305130,
799 2748295,
800 5771662,
801 12091672,
802 25276798,
803 52734615,
804 109820537,
805 228324027,
806 473977813,
807 982548444, /* smallest N such that k == 26 */
808 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
809};
810
811static int
Guido van Rossum42812581998-06-17 14:15:44 +0000812samplesortslice(lo, hi, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000813 PyObject **lo;
814 PyObject **hi;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000815 PyObject *compare;/* Comparison function object, or NULL for default */
816{
817 register PyObject **l, **r;
818 register PyObject *tmp, *pivot;
819 register int k;
820 int n, extra, top, extraOnRight;
821 struct SamplesortStackNode stack[STACKSIZE];
822
823 /* assert lo <= hi */
824 n = hi - lo;
825
826 /* ----------------------------------------------------------
827 * Special cases
828 * --------------------------------------------------------*/
829 if (n < 2)
830 return 0;
831
832 /* Set r to the largest value such that [lo,r) is sorted.
833 This catches the already-sorted case, the all-the-same
834 case, and the appended-a-few-elements-to-a-sorted-list case.
835 If the array is unsorted, we're very likely to get out of
836 the loop fast, so the test is cheap if it doesn't pay off.
837 */
838 /* assert lo < hi */
839 for (r = lo+1; r < hi; ++r) {
840 SETK(*r, *(r-1));
841 if (k < 0)
842 break;
843 }
844 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
845 few unknowns, or few elements in total. */
846 if (hi - r <= MAXMERGE || n < MINSIZE)
Guido van Rossum42812581998-06-17 14:15:44 +0000847 return binarysort(lo, hi, r, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000848
849 /* Check for the array already being reverse-sorted. Typical
850 benchmark-driven silliness <wink>. */
851 /* assert lo < hi */
852 for (r = lo+1; r < hi; ++r) {
853 SETK(*(r-1), *r);
854 if (k < 0)
855 break;
856 }
857 if (hi - r <= MAXMERGE) {
858 /* Reverse the reversed prefix, then insert the tail */
859 PyObject **originalr = r;
860 l = lo;
861 do {
862 --r;
863 tmp = *l; *l = *r; *r = tmp;
864 ++l;
865 } while (l < r);
Guido van Rossum42812581998-06-17 14:15:44 +0000866 return binarysort(lo, hi, originalr, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000867 }
868
869 /* ----------------------------------------------------------
870 * Normal case setup: a large array without obvious pattern.
871 * --------------------------------------------------------*/
872
873 /* extra := a power of 2 ~= n/ln(n), less 1.
874 First find the smallest extra s.t. n < cutoff[extra] */
875 for (extra = 0;
876 extra < sizeof(cutoff) / sizeof(cutoff[0]);
877 ++extra) {
878 if (n < cutoff[extra])
879 break;
880 /* note that if we fall out of the loop, the value of
881 extra still makes *sense*, but may be smaller than
882 we would like (but the array has more than ~= 2**31
883 elements in this case!) */
884 }
885 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
886 have is CUTOFFBASE-1, so
887 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
888 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
889 /* assert extra > 0 and n >= extra */
890
891 /* Swap that many values to the start of the array. The
892 selection of elements is pseudo-random, but the same on
893 every run (this is intentional! timing algorithm changes is
894 a pain if timing varies across runs). */
895 {
896 unsigned int seed = n / extra; /* arbitrary */
897 unsigned int i;
898 for (i = 0; i < (unsigned)extra; ++i) {
899 /* j := random int in [i, n) */
900 unsigned int j;
901 seed = seed * 69069 + 7;
902 j = i + seed % (n - i);
903 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
904 }
905 }
906
907 /* Recursively sort the preselected pivots. */
Guido van Rossum42812581998-06-17 14:15:44 +0000908 if (samplesortslice(lo, lo + extra, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000909 goto fail;
910
911 top = 0; /* index of available stack slot */
912 lo += extra; /* point to first unknown */
913 extraOnRight = 0; /* the PPs are at the left end */
914
915 /* ----------------------------------------------------------
916 * Partition [lo, hi), and repeat until out of work.
917 * --------------------------------------------------------*/
918 for (;;) {
919 /* assert lo <= hi, so n >= 0 */
920 n = hi - lo;
921
922 /* We may not want, or may not be able, to partition:
923 If n is small, it's quicker to insert.
924 If extra is 0, we're out of pivots, and *must* use
925 another method.
926 */
927 if (n < MINPARTITIONSIZE || extra == 0) {
928 if (n >= MINSIZE) {
929 /* assert extra == 0
930 This is rare, since the average size
931 of a final block is only about
932 ln(original n). */
Guido van Rossum42812581998-06-17 14:15:44 +0000933 if (samplesortslice(lo, hi, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000934 goto fail;
935 }
936 else {
937 /* Binary insertion should be quicker,
938 and we can take advantage of the PPs
939 already being sorted. */
940 if (extraOnRight && extra) {
941 /* swap the PPs to the left end */
942 k = extra;
943 do {
944 tmp = *lo;
945 *lo = *hi;
946 *hi = tmp;
947 ++lo; ++hi;
948 } while (--k);
949 }
950 if (binarysort(lo - extra, hi, lo,
Guido van Rossum42812581998-06-17 14:15:44 +0000951 compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000952 goto fail;
953 }
954
955 /* Find another slice to work on. */
956 if (--top < 0)
957 break; /* no more -- done! */
958 lo = stack[top].lo;
959 hi = stack[top].hi;
960 extra = stack[top].extra;
961 extraOnRight = 0;
962 if (extra < 0) {
963 extraOnRight = 1;
964 extra = -extra;
965 }
966 continue;
967 }
968
969 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
970 Then our preselected pivot is at (extra-1)/2, and we
971 want to move the PPs before that to the left end of
972 the slice, and the PPs after that to the right end.
973 The following section changes extra, lo, hi, and the
974 slice such that:
975 [lo-extra, lo) contains the smaller PPs.
976 *lo == our PP.
977 (lo, hi) contains the unknown elements.
978 [hi, hi+extra) contains the larger PPs.
979 */
980 k = extra >>= 1; /* num PPs to move */
981 if (extraOnRight) {
982 /* Swap the smaller PPs to the left end.
983 Note that this loop actually moves k+1 items:
984 the last is our PP */
985 do {
986 tmp = *lo; *lo = *hi; *hi = tmp;
987 ++lo; ++hi;
988 } while (k--);
989 }
990 else {
991 /* Swap the larger PPs to the right end. */
992 while (k--) {
993 --lo; --hi;
994 tmp = *lo; *lo = *hi; *hi = tmp;
995 }
996 }
997 --lo; /* *lo is now our PP */
998 pivot = *lo;
999
1000 /* Now an almost-ordinary quicksort partition step.
1001 Note that most of the time is spent here!
1002 Only odd thing is that we partition into < and >=,
1003 instead of the usual <= and >=. This helps when
1004 there are lots of duplicates of different values,
1005 because it eventually tends to make subfiles
1006 "pure" (all duplicates), and we special-case for
1007 duplicates later. */
1008 l = lo + 1;
1009 r = hi - 1;
1010 /* assert lo < l < r < hi (small n weeded out above) */
1011
1012 do {
1013 /* slide l right, looking for key >= pivot */
1014 do {
1015 SETK(*l, pivot);
1016 if (k < 0)
1017 ++l;
1018 else
1019 break;
1020 } while (l < r);
1021
1022 /* slide r left, looking for key < pivot */
1023 while (l < r) {
Guido van Rossum42812581998-06-17 14:15:44 +00001024 register PyObject *rval = *r--;
1025 SETK(rval, pivot);
1026 if (k < 0) {
1027 /* swap and advance */
1028 r[1] = *l;
1029 *l++ = rval;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001030 break;
Guido van Rossum42812581998-06-17 14:15:44 +00001031 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001032 }
1033
1034 } while (l < r);
1035
1036 /* assert lo < r <= l < hi
1037 assert r == l or r+1 == l
1038 everything to the left of l is < pivot, and
1039 everything to the right of r is >= pivot */
1040
1041 if (l == r) {
1042 SETK(*r, pivot);
1043 if (k < 0)
1044 ++l;
1045 else
1046 --r;
1047 }
1048 /* assert lo <= r and r+1 == l and l <= hi
1049 assert r == lo or a[r] < pivot
1050 assert a[lo] is pivot
1051 assert l == hi or a[l] >= pivot
1052 Swap the pivot into "the middle", so we can henceforth
1053 ignore it.
1054 */
1055 *lo = *r;
1056 *r = pivot;
1057
1058 /* The following is true now, & will be preserved:
1059 All in [lo,r) are < pivot
1060 All in [r,l) == pivot (& so can be ignored)
1061 All in [l,hi) are >= pivot */
1062
1063 /* Check for duplicates of the pivot. One compare is
1064 wasted if there are no duplicates, but can win big
1065 when there are.
1066 Tricky: we're sticking to "<" compares, so deduce
1067 equality indirectly. We know pivot <= *l, so they're
1068 equal iff not pivot < *l.
1069 */
1070 while (l < hi) {
1071 /* pivot <= *l known */
1072 SETK(pivot, *l);
1073 if (k < 0)
1074 break;
1075 else
1076 /* <= and not < implies == */
1077 ++l;
1078 }
1079
1080 /* assert lo <= r < l <= hi
1081 Partitions are [lo, r) and [l, hi) */
1082
1083 /* push fattest first; remember we still have extra PPs
1084 to the left of the left chunk and to the right of
1085 the right chunk! */
1086 /* assert top < STACKSIZE */
1087 if (r - lo <= hi - l) {
1088 /* second is bigger */
1089 stack[top].lo = l;
1090 stack[top].hi = hi;
1091 stack[top].extra = -extra;
1092 hi = r;
1093 extraOnRight = 0;
1094 }
1095 else {
1096 /* first is bigger */
1097 stack[top].lo = lo;
1098 stack[top].hi = r;
1099 stack[top].extra = extra;
1100 lo = l;
1101 extraOnRight = 1;
1102 }
1103 ++top;
1104
1105 } /* end of partitioning loop */
1106
1107 return 0;
1108
1109 fail:
1110 return -1;
1111}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001112
1113#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001114
1115staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001116
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001117static PyObject *
Guido van Rossum3f236de1996-12-10 23:55:39 +00001118listsort(self, compare)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001119 PyListObject *self;
1120 PyObject *compare;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001121{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001122 int err;
1123
1124 self->ob_type = &immutable_list_type;
1125 err = samplesortslice(self->ob_item,
1126 self->ob_item + self->ob_size,
Guido van Rossum42812581998-06-17 14:15:44 +00001127 compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001128 self->ob_type = &PyList_Type;
1129 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001130 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001131 Py_INCREF(Py_None);
1132 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001133}
1134
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001135int
1136PyList_Sort(v)
1137 PyObject *v;
1138{
1139 if (v == NULL || !PyList_Check(v)) {
1140 PyErr_BadInternalCall();
1141 return -1;
1142 }
1143 v = listsort((PyListObject *)v, (PyObject *)NULL);
1144 if (v == NULL)
1145 return -1;
1146 Py_DECREF(v);
1147 return 0;
1148}
1149
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001150static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001151listreverse(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001152 PyListObject *self;
1153 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001154{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001155 register PyObject **p, **q;
1156 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001157
1158 if (args != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001159 PyErr_BadArgument();
Guido van Rossumed98d481991-03-06 13:07:53 +00001160 return NULL;
1161 }
1162
1163 if (self->ob_size > 1) {
1164 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
1165 p < q; p++, q--) {
1166 tmp = *p;
1167 *p = *q;
1168 *q = tmp;
1169 }
1170 }
1171
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001172 Py_INCREF(Py_None);
1173 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001174}
1175
Guido van Rossum84c76f51990-10-30 13:32:20 +00001176int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001177PyList_Reverse(v)
1178 PyObject *v;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001179{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001180 if (v == NULL || !PyList_Check(v)) {
1181 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001182 return -1;
1183 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001184 v = listreverse((PyListObject *)v, (PyObject *)NULL);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001185 if (v == NULL)
1186 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001187 Py_DECREF(v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001188 return 0;
1189}
1190
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001191PyObject *
1192PyList_AsTuple(v)
1193 PyObject *v;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001194{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001195 PyObject *w;
1196 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001197 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001198 if (v == NULL || !PyList_Check(v)) {
1199 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001200 return NULL;
1201 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001202 n = ((PyListObject *)v)->ob_size;
1203 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001204 if (w == NULL)
1205 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001206 p = ((PyTupleObject *)w)->ob_item;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001207 memcpy((ANY *)p,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001208 (ANY *)((PyListObject *)v)->ob_item,
1209 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001210 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001211 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001212 p++;
1213 }
1214 return w;
1215}
1216
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001217static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001218listindex(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001219 PyListObject *self;
1220 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001221{
1222 int i;
1223
1224 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001225 PyErr_BadArgument();
Guido van Rossumed98d481991-03-06 13:07:53 +00001226 return NULL;
1227 }
1228 for (i = 0; i < self->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001229 if (PyObject_Compare(self->ob_item[i], args) == 0)
1230 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001231 if (PyErr_Occurred())
1232 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001233 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001234 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001235 return NULL;
1236}
1237
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001238static PyObject *
Guido van Rossume6f7d181991-10-20 20:20:40 +00001239listcount(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001240 PyListObject *self;
1241 PyObject *args;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001242{
1243 int count = 0;
1244 int i;
1245
1246 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001247 PyErr_BadArgument();
Guido van Rossume6f7d181991-10-20 20:20:40 +00001248 return NULL;
1249 }
1250 for (i = 0; i < self->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001251 if (PyObject_Compare(self->ob_item[i], args) == 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001252 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001253 if (PyErr_Occurred())
1254 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001255 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001256 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001257}
1258
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001259static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001260listremove(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001261 PyListObject *self;
1262 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001263{
1264 int i;
1265
1266 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001267 PyErr_BadArgument();
Guido van Rossumed98d481991-03-06 13:07:53 +00001268 return NULL;
1269 }
1270 for (i = 0; i < self->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001271 if (PyObject_Compare(self->ob_item[i], args) == 0) {
1272 if (list_ass_slice(self, i, i+1,
1273 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001274 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001275 Py_INCREF(Py_None);
1276 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001277 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001278 if (PyErr_Occurred())
1279 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001280 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001281 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001282 return NULL;
1283}
1284
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001285static PyMethodDef list_methods[] = {
1286 {"append", (PyCFunction)listappend},
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001287 {"insert", (PyCFunction)listinsert},
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001288 {"remove", (PyCFunction)listremove},
Guido van Rossum5d8123f1997-07-13 03:58:01 +00001289 {"index", (PyCFunction)listindex},
1290 {"count", (PyCFunction)listcount},
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001291 {"reverse", (PyCFunction)listreverse},
Guido van Rossum5d8123f1997-07-13 03:58:01 +00001292 {"sort", (PyCFunction)listsort, 0},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001293 {NULL, NULL} /* sentinel */
1294};
1295
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001296static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001297list_getattr(f, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001298 PyListObject *f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001299 char *name;
1300{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001301 return Py_FindMethod(list_methods, (PyObject *)f, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001302}
1303
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001304static PySequenceMethods list_as_sequence = {
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001305 (inquiry)list_length, /*sq_length*/
1306 (binaryfunc)list_concat, /*sq_concat*/
1307 (intargfunc)list_repeat, /*sq_repeat*/
1308 (intargfunc)list_item, /*sq_item*/
1309 (intintargfunc)list_slice, /*sq_slice*/
1310 (intobjargproc)list_ass_item, /*sq_ass_item*/
1311 (intintobjargproc)list_ass_slice, /*sq_ass_slice*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001312};
1313
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001314PyTypeObject PyList_Type = {
1315 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001316 0,
1317 "list",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001318 sizeof(PyListObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001319 0,
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001320 (destructor)list_dealloc, /*tp_dealloc*/
1321 (printfunc)list_print, /*tp_print*/
1322 (getattrfunc)list_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001323 0, /*tp_setattr*/
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001324 (cmpfunc)list_compare, /*tp_compare*/
1325 (reprfunc)list_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001326 0, /*tp_as_number*/
1327 &list_as_sequence, /*tp_as_sequence*/
1328 0, /*tp_as_mapping*/
1329};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001330
1331
1332/* During a sort, we really can't have anyone modifying the list; it could
1333 cause core dumps. Thus, we substitute a dummy type that raises an
1334 explanatory exception when a modifying operation is used. Caveat:
1335 comparisons may behave differently; but I guess it's a bad idea anyway to
1336 compare a list that's being sorted... */
1337
1338static PyObject *
1339immutable_list_op(/*No args!*/)
1340{
1341 PyErr_SetString(PyExc_TypeError,
1342 "a list cannot be modified while it is being sorted");
1343 return NULL;
1344}
1345
1346static PyMethodDef immutable_list_methods[] = {
1347 {"append", (PyCFunction)immutable_list_op},
1348 {"insert", (PyCFunction)immutable_list_op},
1349 {"remove", (PyCFunction)immutable_list_op},
1350 {"index", (PyCFunction)listindex},
1351 {"count", (PyCFunction)listcount},
1352 {"reverse", (PyCFunction)immutable_list_op},
1353 {"sort", (PyCFunction)immutable_list_op},
1354 {NULL, NULL} /* sentinel */
1355};
1356
1357static PyObject *
1358immutable_list_getattr(f, name)
1359 PyListObject *f;
1360 char *name;
1361{
1362 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
1363}
1364
1365static int
1366immutable_list_ass(/*No args!*/)
1367{
1368 immutable_list_op();
1369 return -1;
1370}
1371
1372static PySequenceMethods immutable_list_as_sequence = {
1373 (inquiry)list_length, /*sq_length*/
1374 (binaryfunc)list_concat, /*sq_concat*/
1375 (intargfunc)list_repeat, /*sq_repeat*/
1376 (intargfunc)list_item, /*sq_item*/
1377 (intintargfunc)list_slice, /*sq_slice*/
1378 (intobjargproc)immutable_list_ass, /*sq_ass_item*/
1379 (intintobjargproc)immutable_list_ass, /*sq_ass_slice*/
1380};
1381
1382static PyTypeObject immutable_list_type = {
1383 PyObject_HEAD_INIT(&PyType_Type)
1384 0,
1385 "list (immutable, during sort)",
1386 sizeof(PyListObject),
1387 0,
1388 0, /*tp_dealloc*/ /* Cannot happen */
1389 (printfunc)list_print, /*tp_print*/
1390 (getattrfunc)immutable_list_getattr, /*tp_getattr*/
1391 0, /*tp_setattr*/
1392 0, /*tp_compare*/ /* Won't be called */
1393 (reprfunc)list_repr, /*tp_repr*/
1394 0, /*tp_as_number*/
1395 &immutable_list_as_sequence, /*tp_as_sequence*/
1396 0, /*tp_as_mapping*/
1397};
Guido van Rossum42812581998-06-17 14:15:44 +00001398