blob: 4e9ad0cb57680ca8c045289951fcd0467dcc8ec1 [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 Rossum3dd7f3f1998-06-30 15:36:32 +0000575static PyObject *
576listpop(self, args)
577 PyListObject *self;
578 PyObject *args;
579{
580 int i = -1;
581 PyObject *v;
582 if (!PyArg_ParseTuple(args, "|i", &i))
583 return NULL;
584 if (self->ob_size == 0) {
585 /* Special-case most common failure cause */
586 PyErr_SetString(PyExc_IndexError, "pop from empty list");
587 return NULL;
588 }
589 if (i < 0)
590 i += self->ob_size;
591 if (i < 0 || i >= self->ob_size) {
592 PyErr_SetString(PyExc_IndexError, "pop index out of range");
593 return NULL;
594 }
595 v = self->ob_item[i];
596 Py_INCREF(v);
597 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
598 Py_DECREF(v);
599 return NULL;
600 }
601 return v;
602}
603
Guido van Rossum3f236de1996-12-10 23:55:39 +0000604/* New quicksort implementation for arrays of object pointers.
605 Thanks to discussions with Tim Peters. */
606
607/* CMPERROR is returned by our comparison function when an error
608 occurred. This is the largest negative integer (0x80000000 on a
609 32-bit system). */
Guido van Rossum19700b61997-03-05 00:45:43 +0000610#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
Guido van Rossum3f236de1996-12-10 23:55:39 +0000611
612/* Comparison function. Takes care of calling a user-supplied
613 comparison function (any callable Python object). Calls the
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000614 standard comparison function, PyObject_Compare(), if the user-
615 supplied function is NULL. */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000616
617static int
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000618docompare(x, y, compare)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000619 PyObject *x;
620 PyObject *y;
621 PyObject *compare;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000622{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000623 PyObject *args, *res;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000624 int i;
625
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000626 if (compare == NULL) {
627 i = PyObject_Compare(x, y);
628 if (i && PyErr_Occurred())
629 i = CMPERROR;
630 return i;
631 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000632
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633 args = Py_BuildValue("(OO)", x, y);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000634 if (args == NULL)
635 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000636 res = PyEval_CallObject(compare, args);
637 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000638 if (res == NULL)
639 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000640 if (!PyInt_Check(res)) {
641 Py_DECREF(res);
642 PyErr_SetString(PyExc_TypeError,
643 "comparison function should return int");
Guido van Rossum3f236de1996-12-10 23:55:39 +0000644 return CMPERROR;
645 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 i = PyInt_AsLong(res);
647 Py_DECREF(res);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000648 if (i < 0)
649 return -1;
650 if (i > 0)
651 return 1;
652 return 0;
653}
654
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000655/* MINSIZE is the smallest array that will get a full-blown samplesort
656 treatment; smaller arrays are sorted using binary insertion. It must
657 be at least 7 for the samplesort implementation to work. Binary
658 insertion does fewer compares, but can suffer O(N**2) data movement.
659 The more expensive compares, the larger MINSIZE should be. */
660#define MINSIZE 100
661
662/* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
663 partition; smaller slices are passed to binarysort. It must be at
664 least 2, and no larger than MINSIZE. Setting it higher reduces the #
665 of compares slowly, but increases the amount of data movement quickly.
666 The value here was chosen assuming a compare costs ~25x more than
667 swapping a pair of memory-resident pointers -- but under that assumption,
668 changing the value by a few dozen more or less has aggregate effect
669 under 1%. So the value is crucial, but not touchy <wink>. */
670#define MINPARTITIONSIZE 40
671
672/* MAXMERGE is the largest number of elements we'll always merge into
673 a known-to-be sorted chunk via binary insertion, regardless of the
674 size of that chunk. Given a chunk of N sorted elements, and a group
675 of K unknowns, the largest K for which it's better to do insertion
676 (than a full-blown sort) is a complicated function of N and K mostly
677 involving the expected number of compares and data moves under each
678 approach, and the relative cost of those operations on a specific
679 architecure. The fixed value here is conservative, and should be a
680 clear win regardless of architecture or N. */
681#define MAXMERGE 15
Guido van Rossum3f236de1996-12-10 23:55:39 +0000682
Guido van Rossum3f236de1996-12-10 23:55:39 +0000683/* STACKSIZE is the size of our work stack. A rough estimate is that
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000684 this allows us to sort arrays of size N where
685 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
686 for arrays of size 2**64. Because we push the biggest partition
687 first, the worst case occurs when all subarrays are always partitioned
688 exactly in two. */
689#define STACKSIZE 60
Guido van Rossum3f236de1996-12-10 23:55:39 +0000690
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000691
692#define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
693
694/* binarysort is the best method for sorting small arrays: it does
695 few compares, but can do data movement quadratic in the number of
696 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000697 [lo, hi) is a contiguous slice of a list, and is sorted via
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000698 binary insertion.
699 On entry, must have lo <= start <= hi, and that [lo, start) is already
700 sorted (pass start == lo if you don't know!).
701 If docompare complains (returns CMPERROR) return -1, else 0.
702 Even in case of error, the output slice will be some permutation of
703 the input (nothing is lost or duplicated).
704*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000705
706static int
Guido van Rossum42812581998-06-17 14:15:44 +0000707binarysort(lo, hi, start, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000708 PyObject **lo;
709 PyObject **hi;
710 PyObject **start;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000711 PyObject *compare;/* Comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000712{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000713 /* assert lo <= start <= hi
714 assert [lo, start) is sorted */
715 register int k;
716 register PyObject **l, **p, **r;
717 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000718
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000719 if (lo == start)
720 ++start;
721 for (; start < hi; ++start) {
722 /* set l to where *start belongs */
723 l = lo;
724 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000725 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000726 do {
727 p = l + ((r - l) >> 1);
728 SETK(pivot, *p);
729 if (k < 0)
730 r = p;
731 else
732 l = p + 1;
733 } while (l < r);
734 /* Pivot should go at l -- slide over to make room.
735 Caution: using memmove is much slower under MSVC 5;
736 we're not usually moving many slots. */
737 for (p = start; p > l; --p)
738 *p = *(p-1);
739 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000740 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000741 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000742
743 fail:
744 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000745}
746
747/* samplesortslice is the sorting workhorse.
Guido van Rossum42812581998-06-17 14:15:44 +0000748 [lo, hi) is a contiguous slice of a list, to be sorted in place.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000749 On entry, must have lo <= hi,
750 If docompare complains (returns CMPERROR) return -1, else 0.
751 Even in case of error, the output slice will be some permutation of
752 the input (nothing is lost or duplicated).
753
754 samplesort is basically quicksort on steroids: a power of 2 close
755 to n/ln(n) is computed, and that many elements (less 1) are picked at
756 random from the array and sorted. These 2**k - 1 elements are then
757 used as preselected pivots for an equal number of quicksort
758 partitioning steps, partitioning the slice into 2**k chunks each of
759 size about ln(n). These small final chunks are then usually handled
760 by binarysort. Note that when k=1, this is roughly the same as an
761 ordinary quicksort using a random pivot, and when k=2 this is roughly
762 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
763 this a "median of n/ln(n)" quicksort. You can also view it as a kind
764 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
765
766 The large number of samples makes a quadratic-time case almost
767 impossible, and asymptotically drives the average-case number of
768 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
769 3 variant) down to N lg N.
770
771 We also play lots of low-level tricks to cut the number of compares.
772
773 Very obscure: To avoid using extra memory, the PPs are stored in the
774 array and shuffled around as partitioning proceeds. At the start of a
775 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
776 adjacent (either on the left or the right!) to a chunk of X elements
777 that are to be partitioned: P X or X P. In either case we need to
778 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
779 left, followed by the PP to be used for this step (that's the middle
780 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
781 P X or X P -> Psmall pivot X Plarge
782 and the order of the PPs must not be altered. It can take a while
783 to realize this isn't trivial! It can take even longer <wink> to
784 understand why the simple code below works, using only 2**(m-1) swaps.
785 The key is that the order of the X elements isn't necessarily
786 preserved: X can end up as some cyclic permutation of its original
787 order. That's OK, because X is unsorted anyway. If the order of X
788 had to be preserved too, the simplest method I know of using O(1)
789 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
790 Since len(X) is typically several times larger than 2**(m-1), that
791 would slow things down.
792*/
793
794struct SamplesortStackNode {
795 /* Represents a slice of the array, from (& including) lo up
796 to (but excluding) hi. "extra" additional & adjacent elements
797 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
798 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
799 already sorted, but nothing is known about the other elements
800 in [lo, hi). |extra| is always one less than a power of 2.
801 When extra is 0, we're out of PPs, and the slice must be
802 sorted by some other means. */
803 PyObject **lo;
804 PyObject **hi;
805 int extra;
806};
807
808/* 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 +0000809 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000810 is undesirable, so cutoff values are canned in the "cutoff" table
811 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
812#define CUTOFFBASE 4
813static int cutoff[] = {
814 43, /* smallest N such that k == 4 */
815 106, /* etc */
816 250,
817 576,
818 1298,
819 2885,
820 6339,
821 13805,
822 29843,
823 64116,
824 137030,
825 291554,
826 617916,
827 1305130,
828 2748295,
829 5771662,
830 12091672,
831 25276798,
832 52734615,
833 109820537,
834 228324027,
835 473977813,
836 982548444, /* smallest N such that k == 26 */
837 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
838};
839
840static int
Guido van Rossum42812581998-06-17 14:15:44 +0000841samplesortslice(lo, hi, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000842 PyObject **lo;
843 PyObject **hi;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000844 PyObject *compare;/* Comparison function object, or NULL for default */
845{
846 register PyObject **l, **r;
847 register PyObject *tmp, *pivot;
848 register int k;
849 int n, extra, top, extraOnRight;
850 struct SamplesortStackNode stack[STACKSIZE];
851
852 /* assert lo <= hi */
853 n = hi - lo;
854
855 /* ----------------------------------------------------------
856 * Special cases
857 * --------------------------------------------------------*/
858 if (n < 2)
859 return 0;
860
861 /* Set r to the largest value such that [lo,r) is sorted.
862 This catches the already-sorted case, the all-the-same
863 case, and the appended-a-few-elements-to-a-sorted-list case.
864 If the array is unsorted, we're very likely to get out of
865 the loop fast, so the test is cheap if it doesn't pay off.
866 */
867 /* assert lo < hi */
868 for (r = lo+1; r < hi; ++r) {
869 SETK(*r, *(r-1));
870 if (k < 0)
871 break;
872 }
873 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
874 few unknowns, or few elements in total. */
875 if (hi - r <= MAXMERGE || n < MINSIZE)
Guido van Rossum42812581998-06-17 14:15:44 +0000876 return binarysort(lo, hi, r, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000877
878 /* Check for the array already being reverse-sorted. Typical
879 benchmark-driven silliness <wink>. */
880 /* assert lo < hi */
881 for (r = lo+1; r < hi; ++r) {
882 SETK(*(r-1), *r);
883 if (k < 0)
884 break;
885 }
886 if (hi - r <= MAXMERGE) {
887 /* Reverse the reversed prefix, then insert the tail */
888 PyObject **originalr = r;
889 l = lo;
890 do {
891 --r;
892 tmp = *l; *l = *r; *r = tmp;
893 ++l;
894 } while (l < r);
Guido van Rossum42812581998-06-17 14:15:44 +0000895 return binarysort(lo, hi, originalr, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000896 }
897
898 /* ----------------------------------------------------------
899 * Normal case setup: a large array without obvious pattern.
900 * --------------------------------------------------------*/
901
902 /* extra := a power of 2 ~= n/ln(n), less 1.
903 First find the smallest extra s.t. n < cutoff[extra] */
904 for (extra = 0;
905 extra < sizeof(cutoff) / sizeof(cutoff[0]);
906 ++extra) {
907 if (n < cutoff[extra])
908 break;
909 /* note that if we fall out of the loop, the value of
910 extra still makes *sense*, but may be smaller than
911 we would like (but the array has more than ~= 2**31
912 elements in this case!) */
913 }
914 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
915 have is CUTOFFBASE-1, so
916 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
917 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
918 /* assert extra > 0 and n >= extra */
919
920 /* Swap that many values to the start of the array. The
921 selection of elements is pseudo-random, but the same on
922 every run (this is intentional! timing algorithm changes is
923 a pain if timing varies across runs). */
924 {
925 unsigned int seed = n / extra; /* arbitrary */
926 unsigned int i;
927 for (i = 0; i < (unsigned)extra; ++i) {
928 /* j := random int in [i, n) */
929 unsigned int j;
930 seed = seed * 69069 + 7;
931 j = i + seed % (n - i);
932 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
933 }
934 }
935
936 /* Recursively sort the preselected pivots. */
Guido van Rossum42812581998-06-17 14:15:44 +0000937 if (samplesortslice(lo, lo + extra, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000938 goto fail;
939
940 top = 0; /* index of available stack slot */
941 lo += extra; /* point to first unknown */
942 extraOnRight = 0; /* the PPs are at the left end */
943
944 /* ----------------------------------------------------------
945 * Partition [lo, hi), and repeat until out of work.
946 * --------------------------------------------------------*/
947 for (;;) {
948 /* assert lo <= hi, so n >= 0 */
949 n = hi - lo;
950
951 /* We may not want, or may not be able, to partition:
952 If n is small, it's quicker to insert.
953 If extra is 0, we're out of pivots, and *must* use
954 another method.
955 */
956 if (n < MINPARTITIONSIZE || extra == 0) {
957 if (n >= MINSIZE) {
958 /* assert extra == 0
959 This is rare, since the average size
960 of a final block is only about
961 ln(original n). */
Guido van Rossum42812581998-06-17 14:15:44 +0000962 if (samplesortslice(lo, hi, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000963 goto fail;
964 }
965 else {
966 /* Binary insertion should be quicker,
967 and we can take advantage of the PPs
968 already being sorted. */
969 if (extraOnRight && extra) {
970 /* swap the PPs to the left end */
971 k = extra;
972 do {
973 tmp = *lo;
974 *lo = *hi;
975 *hi = tmp;
976 ++lo; ++hi;
977 } while (--k);
978 }
979 if (binarysort(lo - extra, hi, lo,
Guido van Rossum42812581998-06-17 14:15:44 +0000980 compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000981 goto fail;
982 }
983
984 /* Find another slice to work on. */
985 if (--top < 0)
986 break; /* no more -- done! */
987 lo = stack[top].lo;
988 hi = stack[top].hi;
989 extra = stack[top].extra;
990 extraOnRight = 0;
991 if (extra < 0) {
992 extraOnRight = 1;
993 extra = -extra;
994 }
995 continue;
996 }
997
998 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
999 Then our preselected pivot is at (extra-1)/2, and we
1000 want to move the PPs before that to the left end of
1001 the slice, and the PPs after that to the right end.
1002 The following section changes extra, lo, hi, and the
1003 slice such that:
1004 [lo-extra, lo) contains the smaller PPs.
1005 *lo == our PP.
1006 (lo, hi) contains the unknown elements.
1007 [hi, hi+extra) contains the larger PPs.
1008 */
1009 k = extra >>= 1; /* num PPs to move */
1010 if (extraOnRight) {
1011 /* Swap the smaller PPs to the left end.
1012 Note that this loop actually moves k+1 items:
1013 the last is our PP */
1014 do {
1015 tmp = *lo; *lo = *hi; *hi = tmp;
1016 ++lo; ++hi;
1017 } while (k--);
1018 }
1019 else {
1020 /* Swap the larger PPs to the right end. */
1021 while (k--) {
1022 --lo; --hi;
1023 tmp = *lo; *lo = *hi; *hi = tmp;
1024 }
1025 }
1026 --lo; /* *lo is now our PP */
1027 pivot = *lo;
1028
1029 /* Now an almost-ordinary quicksort partition step.
1030 Note that most of the time is spent here!
1031 Only odd thing is that we partition into < and >=,
1032 instead of the usual <= and >=. This helps when
1033 there are lots of duplicates of different values,
1034 because it eventually tends to make subfiles
1035 "pure" (all duplicates), and we special-case for
1036 duplicates later. */
1037 l = lo + 1;
1038 r = hi - 1;
1039 /* assert lo < l < r < hi (small n weeded out above) */
1040
1041 do {
1042 /* slide l right, looking for key >= pivot */
1043 do {
1044 SETK(*l, pivot);
1045 if (k < 0)
1046 ++l;
1047 else
1048 break;
1049 } while (l < r);
1050
1051 /* slide r left, looking for key < pivot */
1052 while (l < r) {
Guido van Rossum42812581998-06-17 14:15:44 +00001053 register PyObject *rval = *r--;
1054 SETK(rval, pivot);
1055 if (k < 0) {
1056 /* swap and advance */
1057 r[1] = *l;
1058 *l++ = rval;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001059 break;
Guido van Rossum42812581998-06-17 14:15:44 +00001060 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001061 }
1062
1063 } while (l < r);
1064
1065 /* assert lo < r <= l < hi
1066 assert r == l or r+1 == l
1067 everything to the left of l is < pivot, and
1068 everything to the right of r is >= pivot */
1069
1070 if (l == r) {
1071 SETK(*r, pivot);
1072 if (k < 0)
1073 ++l;
1074 else
1075 --r;
1076 }
1077 /* assert lo <= r and r+1 == l and l <= hi
1078 assert r == lo or a[r] < pivot
1079 assert a[lo] is pivot
1080 assert l == hi or a[l] >= pivot
1081 Swap the pivot into "the middle", so we can henceforth
1082 ignore it.
1083 */
1084 *lo = *r;
1085 *r = pivot;
1086
1087 /* The following is true now, & will be preserved:
1088 All in [lo,r) are < pivot
1089 All in [r,l) == pivot (& so can be ignored)
1090 All in [l,hi) are >= pivot */
1091
1092 /* Check for duplicates of the pivot. One compare is
1093 wasted if there are no duplicates, but can win big
1094 when there are.
1095 Tricky: we're sticking to "<" compares, so deduce
1096 equality indirectly. We know pivot <= *l, so they're
1097 equal iff not pivot < *l.
1098 */
1099 while (l < hi) {
1100 /* pivot <= *l known */
1101 SETK(pivot, *l);
1102 if (k < 0)
1103 break;
1104 else
1105 /* <= and not < implies == */
1106 ++l;
1107 }
1108
1109 /* assert lo <= r < l <= hi
1110 Partitions are [lo, r) and [l, hi) */
1111
1112 /* push fattest first; remember we still have extra PPs
1113 to the left of the left chunk and to the right of
1114 the right chunk! */
1115 /* assert top < STACKSIZE */
1116 if (r - lo <= hi - l) {
1117 /* second is bigger */
1118 stack[top].lo = l;
1119 stack[top].hi = hi;
1120 stack[top].extra = -extra;
1121 hi = r;
1122 extraOnRight = 0;
1123 }
1124 else {
1125 /* first is bigger */
1126 stack[top].lo = lo;
1127 stack[top].hi = r;
1128 stack[top].extra = extra;
1129 lo = l;
1130 extraOnRight = 1;
1131 }
1132 ++top;
1133
1134 } /* end of partitioning loop */
1135
1136 return 0;
1137
1138 fail:
1139 return -1;
1140}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001141
1142#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001143
1144staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001145
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001146static PyObject *
Guido van Rossum3f236de1996-12-10 23:55:39 +00001147listsort(self, compare)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001148 PyListObject *self;
1149 PyObject *compare;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001150{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001151 int err;
1152
1153 self->ob_type = &immutable_list_type;
1154 err = samplesortslice(self->ob_item,
1155 self->ob_item + self->ob_size,
Guido van Rossum42812581998-06-17 14:15:44 +00001156 compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001157 self->ob_type = &PyList_Type;
1158 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001159 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001160 Py_INCREF(Py_None);
1161 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001162}
1163
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001164int
1165PyList_Sort(v)
1166 PyObject *v;
1167{
1168 if (v == NULL || !PyList_Check(v)) {
1169 PyErr_BadInternalCall();
1170 return -1;
1171 }
1172 v = listsort((PyListObject *)v, (PyObject *)NULL);
1173 if (v == NULL)
1174 return -1;
1175 Py_DECREF(v);
1176 return 0;
1177}
1178
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001179static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001180listreverse(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001181 PyListObject *self;
1182 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001183{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001184 register PyObject **p, **q;
1185 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001186
1187 if (args != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001188 PyErr_BadArgument();
Guido van Rossumed98d481991-03-06 13:07:53 +00001189 return NULL;
1190 }
1191
1192 if (self->ob_size > 1) {
1193 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
1194 p < q; p++, q--) {
1195 tmp = *p;
1196 *p = *q;
1197 *q = tmp;
1198 }
1199 }
1200
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001201 Py_INCREF(Py_None);
1202 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001203}
1204
Guido van Rossum84c76f51990-10-30 13:32:20 +00001205int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001206PyList_Reverse(v)
1207 PyObject *v;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001208{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001209 if (v == NULL || !PyList_Check(v)) {
1210 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001211 return -1;
1212 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001213 v = listreverse((PyListObject *)v, (PyObject *)NULL);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001214 if (v == NULL)
1215 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001216 Py_DECREF(v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001217 return 0;
1218}
1219
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001220PyObject *
1221PyList_AsTuple(v)
1222 PyObject *v;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001223{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001224 PyObject *w;
1225 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001226 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001227 if (v == NULL || !PyList_Check(v)) {
1228 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001229 return NULL;
1230 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001231 n = ((PyListObject *)v)->ob_size;
1232 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001233 if (w == NULL)
1234 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001235 p = ((PyTupleObject *)w)->ob_item;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001236 memcpy((ANY *)p,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001237 (ANY *)((PyListObject *)v)->ob_item,
1238 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001239 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001240 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001241 p++;
1242 }
1243 return w;
1244}
1245
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001246static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001247listindex(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001248 PyListObject *self;
1249 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001250{
1251 int i;
1252
1253 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001254 PyErr_BadArgument();
Guido van Rossumed98d481991-03-06 13:07:53 +00001255 return NULL;
1256 }
1257 for (i = 0; i < self->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001258 if (PyObject_Compare(self->ob_item[i], args) == 0)
1259 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001260 if (PyErr_Occurred())
1261 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001262 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001263 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001264 return NULL;
1265}
1266
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001267static PyObject *
Guido van Rossume6f7d181991-10-20 20:20:40 +00001268listcount(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001269 PyListObject *self;
1270 PyObject *args;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001271{
1272 int count = 0;
1273 int i;
1274
1275 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001276 PyErr_BadArgument();
Guido van Rossume6f7d181991-10-20 20:20:40 +00001277 return NULL;
1278 }
1279 for (i = 0; i < self->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001280 if (PyObject_Compare(self->ob_item[i], args) == 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001281 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001282 if (PyErr_Occurred())
1283 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001284 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001285 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001286}
1287
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001288static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001289listremove(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001290 PyListObject *self;
1291 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001292{
1293 int i;
1294
1295 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001296 PyErr_BadArgument();
Guido van Rossumed98d481991-03-06 13:07:53 +00001297 return NULL;
1298 }
1299 for (i = 0; i < self->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001300 if (PyObject_Compare(self->ob_item[i], args) == 0) {
1301 if (list_ass_slice(self, i, i+1,
1302 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001303 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001304 Py_INCREF(Py_None);
1305 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001306 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001307 if (PyErr_Occurred())
1308 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001309 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001310 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001311 return NULL;
1312}
1313
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001314static char append_doc[] =
1315"L.append(object) -- append object to end";
1316static char insert_doc[] =
1317"L.insert(index, object) -- insert object before index";
1318static char pop_doc[] =
1319"L.pop([index]) -> item -- remove and return item at index (default last)";
1320static char remove_doc[] =
1321"L.remove(value) -- remove first occurrence of value";
1322static char index_doc[] =
1323"L.index(value) -> integer -- return index of first occurrence of value";
1324static char count_doc[] =
1325"L.count(value) -> integer -- return number of occurrences of value";
1326static char reverse_doc[] =
1327"L.reverse() -- reverse *IN PLACE*";
1328static char sort_doc[] =
1329"L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1330
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001331static PyMethodDef list_methods[] = {
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001332 {"append", (PyCFunction)listappend, 0, append_doc},
1333 {"insert", (PyCFunction)listinsert, 0, insert_doc},
1334 {"pop", (PyCFunction)listpop, 1, pop_doc},
1335 {"remove", (PyCFunction)listremove, 0, remove_doc},
1336 {"index", (PyCFunction)listindex, 0, index_doc},
1337 {"count", (PyCFunction)listcount, 0, count_doc},
1338 {"reverse", (PyCFunction)listreverse, 0, reverse_doc},
1339 {"sort", (PyCFunction)listsort, 0, sort_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001340 {NULL, NULL} /* sentinel */
1341};
1342
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001343static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001344list_getattr(f, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001345 PyListObject *f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001346 char *name;
1347{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001348 return Py_FindMethod(list_methods, (PyObject *)f, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001349}
1350
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001351static PySequenceMethods list_as_sequence = {
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001352 (inquiry)list_length, /*sq_length*/
1353 (binaryfunc)list_concat, /*sq_concat*/
1354 (intargfunc)list_repeat, /*sq_repeat*/
1355 (intargfunc)list_item, /*sq_item*/
1356 (intintargfunc)list_slice, /*sq_slice*/
1357 (intobjargproc)list_ass_item, /*sq_ass_item*/
1358 (intintobjargproc)list_ass_slice, /*sq_ass_slice*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001359};
1360
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001361PyTypeObject PyList_Type = {
1362 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001363 0,
1364 "list",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001365 sizeof(PyListObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001366 0,
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001367 (destructor)list_dealloc, /*tp_dealloc*/
1368 (printfunc)list_print, /*tp_print*/
1369 (getattrfunc)list_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001370 0, /*tp_setattr*/
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001371 (cmpfunc)list_compare, /*tp_compare*/
1372 (reprfunc)list_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001373 0, /*tp_as_number*/
1374 &list_as_sequence, /*tp_as_sequence*/
1375 0, /*tp_as_mapping*/
1376};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001377
1378
1379/* During a sort, we really can't have anyone modifying the list; it could
1380 cause core dumps. Thus, we substitute a dummy type that raises an
1381 explanatory exception when a modifying operation is used. Caveat:
1382 comparisons may behave differently; but I guess it's a bad idea anyway to
1383 compare a list that's being sorted... */
1384
1385static PyObject *
1386immutable_list_op(/*No args!*/)
1387{
1388 PyErr_SetString(PyExc_TypeError,
1389 "a list cannot be modified while it is being sorted");
1390 return NULL;
1391}
1392
1393static PyMethodDef immutable_list_methods[] = {
1394 {"append", (PyCFunction)immutable_list_op},
1395 {"insert", (PyCFunction)immutable_list_op},
1396 {"remove", (PyCFunction)immutable_list_op},
1397 {"index", (PyCFunction)listindex},
1398 {"count", (PyCFunction)listcount},
1399 {"reverse", (PyCFunction)immutable_list_op},
1400 {"sort", (PyCFunction)immutable_list_op},
1401 {NULL, NULL} /* sentinel */
1402};
1403
1404static PyObject *
1405immutable_list_getattr(f, name)
1406 PyListObject *f;
1407 char *name;
1408{
1409 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
1410}
1411
1412static int
1413immutable_list_ass(/*No args!*/)
1414{
1415 immutable_list_op();
1416 return -1;
1417}
1418
1419static PySequenceMethods immutable_list_as_sequence = {
1420 (inquiry)list_length, /*sq_length*/
1421 (binaryfunc)list_concat, /*sq_concat*/
1422 (intargfunc)list_repeat, /*sq_repeat*/
1423 (intargfunc)list_item, /*sq_item*/
1424 (intintargfunc)list_slice, /*sq_slice*/
1425 (intobjargproc)immutable_list_ass, /*sq_ass_item*/
1426 (intintobjargproc)immutable_list_ass, /*sq_ass_slice*/
1427};
1428
1429static PyTypeObject immutable_list_type = {
1430 PyObject_HEAD_INIT(&PyType_Type)
1431 0,
1432 "list (immutable, during sort)",
1433 sizeof(PyListObject),
1434 0,
1435 0, /*tp_dealloc*/ /* Cannot happen */
1436 (printfunc)list_print, /*tp_print*/
1437 (getattrfunc)immutable_list_getattr, /*tp_getattr*/
1438 0, /*tp_setattr*/
1439 0, /*tp_compare*/ /* Won't be called */
1440 (reprfunc)list_repr, /*tp_repr*/
1441 0, /*tp_as_number*/
1442 &immutable_list_as_sequence, /*tp_as_sequence*/
1443 0, /*tp_as_mapping*/
1444};
Guido van Rossum42812581998-06-17 14:15:44 +00001445