blob: 7917c7c7e0920e74c7a65eb9eb23d2f76214f7d7 [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.
668 [lo, hi) is a contiguous slice of the list, and is sorted via
669 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 Rossum4c4e7df1998-06-16 15:18:28 +0000678binarysort(lo, hi, start, list, compare)
679 PyObject **lo;
680 PyObject **hi;
681 PyObject **start;
682 PyListObject *list; /* Needed by docompare for paranoia checks */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000683 PyObject *compare;/* Comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000684{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000685 /* assert lo <= start <= hi
686 assert [lo, start) is sorted */
687 register int k;
688 register PyObject **l, **p, **r;
689 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000690
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000691 if (lo == start)
692 ++start;
693 for (; start < hi; ++start) {
694 /* set l to where *start belongs */
695 l = lo;
696 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000697 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000698 do {
699 p = l + ((r - l) >> 1);
700 SETK(pivot, *p);
701 if (k < 0)
702 r = p;
703 else
704 l = p + 1;
705 } while (l < r);
706 /* Pivot should go at l -- slide over to make room.
707 Caution: using memmove is much slower under MSVC 5;
708 we're not usually moving many slots. */
709 for (p = start; p > l; --p)
710 *p = *(p-1);
711 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000712 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000713 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000714
715 fail:
716 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000717}
718
719/* samplesortslice is the sorting workhorse.
720 [lo, hi) is a contiguous slice of the list, to be sorted in place.
721 On entry, must have lo <= hi,
722 If docompare complains (returns CMPERROR) return -1, else 0.
723 Even in case of error, the output slice will be some permutation of
724 the input (nothing is lost or duplicated).
725
726 samplesort is basically quicksort on steroids: a power of 2 close
727 to n/ln(n) is computed, and that many elements (less 1) are picked at
728 random from the array and sorted. These 2**k - 1 elements are then
729 used as preselected pivots for an equal number of quicksort
730 partitioning steps, partitioning the slice into 2**k chunks each of
731 size about ln(n). These small final chunks are then usually handled
732 by binarysort. Note that when k=1, this is roughly the same as an
733 ordinary quicksort using a random pivot, and when k=2 this is roughly
734 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
735 this a "median of n/ln(n)" quicksort. You can also view it as a kind
736 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
737
738 The large number of samples makes a quadratic-time case almost
739 impossible, and asymptotically drives the average-case number of
740 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
741 3 variant) down to N lg N.
742
743 We also play lots of low-level tricks to cut the number of compares.
744
745 Very obscure: To avoid using extra memory, the PPs are stored in the
746 array and shuffled around as partitioning proceeds. At the start of a
747 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
748 adjacent (either on the left or the right!) to a chunk of X elements
749 that are to be partitioned: P X or X P. In either case we need to
750 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
751 left, followed by the PP to be used for this step (that's the middle
752 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
753 P X or X P -> Psmall pivot X Plarge
754 and the order of the PPs must not be altered. It can take a while
755 to realize this isn't trivial! It can take even longer <wink> to
756 understand why the simple code below works, using only 2**(m-1) swaps.
757 The key is that the order of the X elements isn't necessarily
758 preserved: X can end up as some cyclic permutation of its original
759 order. That's OK, because X is unsorted anyway. If the order of X
760 had to be preserved too, the simplest method I know of using O(1)
761 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
762 Since len(X) is typically several times larger than 2**(m-1), that
763 would slow things down.
764*/
765
766struct SamplesortStackNode {
767 /* Represents a slice of the array, from (& including) lo up
768 to (but excluding) hi. "extra" additional & adjacent elements
769 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
770 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
771 already sorted, but nothing is known about the other elements
772 in [lo, hi). |extra| is always one less than a power of 2.
773 When extra is 0, we're out of PPs, and the slice must be
774 sorted by some other means. */
775 PyObject **lo;
776 PyObject **hi;
777 int extra;
778};
779
780/* The number of PPs we want is 2**k - 1, where 2**k is as close to
781 N / ln(N) as possible. So k ~= lg(N / ln(N). Calling libm routines
782 is undesirable, so cutoff values are canned in the "cutoff" table
783 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
784#define CUTOFFBASE 4
785static int cutoff[] = {
786 43, /* smallest N such that k == 4 */
787 106, /* etc */
788 250,
789 576,
790 1298,
791 2885,
792 6339,
793 13805,
794 29843,
795 64116,
796 137030,
797 291554,
798 617916,
799 1305130,
800 2748295,
801 5771662,
802 12091672,
803 25276798,
804 52734615,
805 109820537,
806 228324027,
807 473977813,
808 982548444, /* smallest N such that k == 26 */
809 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
810};
811
812static int
813samplesortslice(lo, hi, list, compare)
814 PyObject **lo;
815 PyObject **hi;
816 PyListObject *list; /* Needed by docompare for paranoia checks */
817 PyObject *compare;/* Comparison function object, or NULL for default */
818{
819 register PyObject **l, **r;
820 register PyObject *tmp, *pivot;
821 register int k;
822 int n, extra, top, extraOnRight;
823 struct SamplesortStackNode stack[STACKSIZE];
824
825 /* assert lo <= hi */
826 n = hi - lo;
827
828 /* ----------------------------------------------------------
829 * Special cases
830 * --------------------------------------------------------*/
831 if (n < 2)
832 return 0;
833
834 /* Set r to the largest value such that [lo,r) is sorted.
835 This catches the already-sorted case, the all-the-same
836 case, and the appended-a-few-elements-to-a-sorted-list case.
837 If the array is unsorted, we're very likely to get out of
838 the loop fast, so the test is cheap if it doesn't pay off.
839 */
840 /* assert lo < hi */
841 for (r = lo+1; r < hi; ++r) {
842 SETK(*r, *(r-1));
843 if (k < 0)
844 break;
845 }
846 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
847 few unknowns, or few elements in total. */
848 if (hi - r <= MAXMERGE || n < MINSIZE)
849 return binarysort(lo, hi, r, list, compare);
850
851 /* Check for the array already being reverse-sorted. Typical
852 benchmark-driven silliness <wink>. */
853 /* assert lo < hi */
854 for (r = lo+1; r < hi; ++r) {
855 SETK(*(r-1), *r);
856 if (k < 0)
857 break;
858 }
859 if (hi - r <= MAXMERGE) {
860 /* Reverse the reversed prefix, then insert the tail */
861 PyObject **originalr = r;
862 l = lo;
863 do {
864 --r;
865 tmp = *l; *l = *r; *r = tmp;
866 ++l;
867 } while (l < r);
868 return binarysort(lo, hi, originalr, list, compare);
869 }
870
871 /* ----------------------------------------------------------
872 * Normal case setup: a large array without obvious pattern.
873 * --------------------------------------------------------*/
874
875 /* extra := a power of 2 ~= n/ln(n), less 1.
876 First find the smallest extra s.t. n < cutoff[extra] */
877 for (extra = 0;
878 extra < sizeof(cutoff) / sizeof(cutoff[0]);
879 ++extra) {
880 if (n < cutoff[extra])
881 break;
882 /* note that if we fall out of the loop, the value of
883 extra still makes *sense*, but may be smaller than
884 we would like (but the array has more than ~= 2**31
885 elements in this case!) */
886 }
887 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
888 have is CUTOFFBASE-1, so
889 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
890 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
891 /* assert extra > 0 and n >= extra */
892
893 /* Swap that many values to the start of the array. The
894 selection of elements is pseudo-random, but the same on
895 every run (this is intentional! timing algorithm changes is
896 a pain if timing varies across runs). */
897 {
898 unsigned int seed = n / extra; /* arbitrary */
899 unsigned int i;
900 for (i = 0; i < (unsigned)extra; ++i) {
901 /* j := random int in [i, n) */
902 unsigned int j;
903 seed = seed * 69069 + 7;
904 j = i + seed % (n - i);
905 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
906 }
907 }
908
909 /* Recursively sort the preselected pivots. */
910 if (samplesortslice(lo, lo + extra, list, compare) < 0)
911 goto fail;
912
913 top = 0; /* index of available stack slot */
914 lo += extra; /* point to first unknown */
915 extraOnRight = 0; /* the PPs are at the left end */
916
917 /* ----------------------------------------------------------
918 * Partition [lo, hi), and repeat until out of work.
919 * --------------------------------------------------------*/
920 for (;;) {
921 /* assert lo <= hi, so n >= 0 */
922 n = hi - lo;
923
924 /* We may not want, or may not be able, to partition:
925 If n is small, it's quicker to insert.
926 If extra is 0, we're out of pivots, and *must* use
927 another method.
928 */
929 if (n < MINPARTITIONSIZE || extra == 0) {
930 if (n >= MINSIZE) {
931 /* assert extra == 0
932 This is rare, since the average size
933 of a final block is only about
934 ln(original n). */
935 if (samplesortslice(lo, hi, list,
936 compare) < 0)
937 goto fail;
938 }
939 else {
940 /* Binary insertion should be quicker,
941 and we can take advantage of the PPs
942 already being sorted. */
943 if (extraOnRight && extra) {
944 /* swap the PPs to the left end */
945 k = extra;
946 do {
947 tmp = *lo;
948 *lo = *hi;
949 *hi = tmp;
950 ++lo; ++hi;
951 } while (--k);
952 }
953 if (binarysort(lo - extra, hi, lo,
954 list, compare) < 0)
955 goto fail;
956 }
957
958 /* Find another slice to work on. */
959 if (--top < 0)
960 break; /* no more -- done! */
961 lo = stack[top].lo;
962 hi = stack[top].hi;
963 extra = stack[top].extra;
964 extraOnRight = 0;
965 if (extra < 0) {
966 extraOnRight = 1;
967 extra = -extra;
968 }
969 continue;
970 }
971
972 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
973 Then our preselected pivot is at (extra-1)/2, and we
974 want to move the PPs before that to the left end of
975 the slice, and the PPs after that to the right end.
976 The following section changes extra, lo, hi, and the
977 slice such that:
978 [lo-extra, lo) contains the smaller PPs.
979 *lo == our PP.
980 (lo, hi) contains the unknown elements.
981 [hi, hi+extra) contains the larger PPs.
982 */
983 k = extra >>= 1; /* num PPs to move */
984 if (extraOnRight) {
985 /* Swap the smaller PPs to the left end.
986 Note that this loop actually moves k+1 items:
987 the last is our PP */
988 do {
989 tmp = *lo; *lo = *hi; *hi = tmp;
990 ++lo; ++hi;
991 } while (k--);
992 }
993 else {
994 /* Swap the larger PPs to the right end. */
995 while (k--) {
996 --lo; --hi;
997 tmp = *lo; *lo = *hi; *hi = tmp;
998 }
999 }
1000 --lo; /* *lo is now our PP */
1001 pivot = *lo;
1002
1003 /* Now an almost-ordinary quicksort partition step.
1004 Note that most of the time is spent here!
1005 Only odd thing is that we partition into < and >=,
1006 instead of the usual <= and >=. This helps when
1007 there are lots of duplicates of different values,
1008 because it eventually tends to make subfiles
1009 "pure" (all duplicates), and we special-case for
1010 duplicates later. */
1011 l = lo + 1;
1012 r = hi - 1;
1013 /* assert lo < l < r < hi (small n weeded out above) */
1014
1015 do {
1016 /* slide l right, looking for key >= pivot */
1017 do {
1018 SETK(*l, pivot);
1019 if (k < 0)
1020 ++l;
1021 else
1022 break;
1023 } while (l < r);
1024
1025 /* slide r left, looking for key < pivot */
1026 while (l < r) {
1027 SETK(*r, pivot);
1028 if (k < 0)
1029 break;
1030 else
1031 --r;
1032 }
1033
1034 /* swap and advance both pointers */
1035 if (l < r) {
1036 tmp = *l; *l = *r; *r = tmp;
1037 ++l;
1038 --r;
1039 }
1040
1041 } while (l < r);
1042
1043 /* assert lo < r <= l < hi
1044 assert r == l or r+1 == l
1045 everything to the left of l is < pivot, and
1046 everything to the right of r is >= pivot */
1047
1048 if (l == r) {
1049 SETK(*r, pivot);
1050 if (k < 0)
1051 ++l;
1052 else
1053 --r;
1054 }
1055 /* assert lo <= r and r+1 == l and l <= hi
1056 assert r == lo or a[r] < pivot
1057 assert a[lo] is pivot
1058 assert l == hi or a[l] >= pivot
1059 Swap the pivot into "the middle", so we can henceforth
1060 ignore it.
1061 */
1062 *lo = *r;
1063 *r = pivot;
1064
1065 /* The following is true now, & will be preserved:
1066 All in [lo,r) are < pivot
1067 All in [r,l) == pivot (& so can be ignored)
1068 All in [l,hi) are >= pivot */
1069
1070 /* Check for duplicates of the pivot. One compare is
1071 wasted if there are no duplicates, but can win big
1072 when there are.
1073 Tricky: we're sticking to "<" compares, so deduce
1074 equality indirectly. We know pivot <= *l, so they're
1075 equal iff not pivot < *l.
1076 */
1077 while (l < hi) {
1078 /* pivot <= *l known */
1079 SETK(pivot, *l);
1080 if (k < 0)
1081 break;
1082 else
1083 /* <= and not < implies == */
1084 ++l;
1085 }
1086
1087 /* assert lo <= r < l <= hi
1088 Partitions are [lo, r) and [l, hi) */
1089
1090 /* push fattest first; remember we still have extra PPs
1091 to the left of the left chunk and to the right of
1092 the right chunk! */
1093 /* assert top < STACKSIZE */
1094 if (r - lo <= hi - l) {
1095 /* second is bigger */
1096 stack[top].lo = l;
1097 stack[top].hi = hi;
1098 stack[top].extra = -extra;
1099 hi = r;
1100 extraOnRight = 0;
1101 }
1102 else {
1103 /* first is bigger */
1104 stack[top].lo = lo;
1105 stack[top].hi = r;
1106 stack[top].extra = extra;
1107 lo = l;
1108 extraOnRight = 1;
1109 }
1110 ++top;
1111
1112 } /* end of partitioning loop */
1113
1114 return 0;
1115
1116 fail:
1117 return -1;
1118}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001119
1120#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001121
1122staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001123
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001124static PyObject *
Guido van Rossum3f236de1996-12-10 23:55:39 +00001125listsort(self, compare)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001126 PyListObject *self;
1127 PyObject *compare;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001128{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001129 int err;
1130
1131 self->ob_type = &immutable_list_type;
1132 err = samplesortslice(self->ob_item,
1133 self->ob_item + self->ob_size,
1134 self, compare);
1135 self->ob_type = &PyList_Type;
1136 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001137 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001138 Py_INCREF(Py_None);
1139 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001140}
1141
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001142int
1143PyList_Sort(v)
1144 PyObject *v;
1145{
1146 if (v == NULL || !PyList_Check(v)) {
1147 PyErr_BadInternalCall();
1148 return -1;
1149 }
1150 v = listsort((PyListObject *)v, (PyObject *)NULL);
1151 if (v == NULL)
1152 return -1;
1153 Py_DECREF(v);
1154 return 0;
1155}
1156
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001157static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001158listreverse(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001159 PyListObject *self;
1160 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001161{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001162 register PyObject **p, **q;
1163 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001164
1165 if (args != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001166 PyErr_BadArgument();
Guido van Rossumed98d481991-03-06 13:07:53 +00001167 return NULL;
1168 }
1169
1170 if (self->ob_size > 1) {
1171 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
1172 p < q; p++, q--) {
1173 tmp = *p;
1174 *p = *q;
1175 *q = tmp;
1176 }
1177 }
1178
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001179 Py_INCREF(Py_None);
1180 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001181}
1182
Guido van Rossum84c76f51990-10-30 13:32:20 +00001183int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001184PyList_Reverse(v)
1185 PyObject *v;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001186{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001187 if (v == NULL || !PyList_Check(v)) {
1188 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001189 return -1;
1190 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001191 v = listreverse((PyListObject *)v, (PyObject *)NULL);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001192 if (v == NULL)
1193 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001194 Py_DECREF(v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001195 return 0;
1196}
1197
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001198PyObject *
1199PyList_AsTuple(v)
1200 PyObject *v;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001201{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001202 PyObject *w;
1203 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001204 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001205 if (v == NULL || !PyList_Check(v)) {
1206 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001207 return NULL;
1208 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001209 n = ((PyListObject *)v)->ob_size;
1210 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001211 if (w == NULL)
1212 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001213 p = ((PyTupleObject *)w)->ob_item;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001214 memcpy((ANY *)p,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001215 (ANY *)((PyListObject *)v)->ob_item,
1216 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001217 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001218 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001219 p++;
1220 }
1221 return w;
1222}
1223
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001224static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001225listindex(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001226 PyListObject *self;
1227 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001228{
1229 int i;
1230
1231 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001232 PyErr_BadArgument();
Guido van Rossumed98d481991-03-06 13:07:53 +00001233 return NULL;
1234 }
1235 for (i = 0; i < self->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001236 if (PyObject_Compare(self->ob_item[i], args) == 0)
1237 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001238 if (PyErr_Occurred())
1239 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001240 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001241 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001242 return NULL;
1243}
1244
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001245static PyObject *
Guido van Rossume6f7d181991-10-20 20:20:40 +00001246listcount(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001247 PyListObject *self;
1248 PyObject *args;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001249{
1250 int count = 0;
1251 int i;
1252
1253 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001254 PyErr_BadArgument();
Guido van Rossume6f7d181991-10-20 20:20:40 +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)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001259 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001260 if (PyErr_Occurred())
1261 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001262 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001263 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001264}
1265
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001266static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001267listremove(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001268 PyListObject *self;
1269 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001270{
1271 int i;
1272
1273 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001274 PyErr_BadArgument();
Guido van Rossumed98d481991-03-06 13:07:53 +00001275 return NULL;
1276 }
1277 for (i = 0; i < self->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001278 if (PyObject_Compare(self->ob_item[i], args) == 0) {
1279 if (list_ass_slice(self, i, i+1,
1280 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001281 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001282 Py_INCREF(Py_None);
1283 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001284 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001285 if (PyErr_Occurred())
1286 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001287 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001288 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001289 return NULL;
1290}
1291
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001292static PyMethodDef list_methods[] = {
1293 {"append", (PyCFunction)listappend},
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001294 {"insert", (PyCFunction)listinsert},
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001295 {"remove", (PyCFunction)listremove},
Guido van Rossum5d8123f1997-07-13 03:58:01 +00001296 {"index", (PyCFunction)listindex},
1297 {"count", (PyCFunction)listcount},
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001298 {"reverse", (PyCFunction)listreverse},
Guido van Rossum5d8123f1997-07-13 03:58:01 +00001299 {"sort", (PyCFunction)listsort, 0},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001300 {NULL, NULL} /* sentinel */
1301};
1302
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001303static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001304list_getattr(f, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001305 PyListObject *f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001306 char *name;
1307{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001308 return Py_FindMethod(list_methods, (PyObject *)f, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001309}
1310
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001311static PySequenceMethods list_as_sequence = {
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001312 (inquiry)list_length, /*sq_length*/
1313 (binaryfunc)list_concat, /*sq_concat*/
1314 (intargfunc)list_repeat, /*sq_repeat*/
1315 (intargfunc)list_item, /*sq_item*/
1316 (intintargfunc)list_slice, /*sq_slice*/
1317 (intobjargproc)list_ass_item, /*sq_ass_item*/
1318 (intintobjargproc)list_ass_slice, /*sq_ass_slice*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001319};
1320
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001321PyTypeObject PyList_Type = {
1322 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001323 0,
1324 "list",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001325 sizeof(PyListObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001326 0,
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001327 (destructor)list_dealloc, /*tp_dealloc*/
1328 (printfunc)list_print, /*tp_print*/
1329 (getattrfunc)list_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001330 0, /*tp_setattr*/
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001331 (cmpfunc)list_compare, /*tp_compare*/
1332 (reprfunc)list_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001333 0, /*tp_as_number*/
1334 &list_as_sequence, /*tp_as_sequence*/
1335 0, /*tp_as_mapping*/
1336};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001337
1338
1339/* During a sort, we really can't have anyone modifying the list; it could
1340 cause core dumps. Thus, we substitute a dummy type that raises an
1341 explanatory exception when a modifying operation is used. Caveat:
1342 comparisons may behave differently; but I guess it's a bad idea anyway to
1343 compare a list that's being sorted... */
1344
1345static PyObject *
1346immutable_list_op(/*No args!*/)
1347{
1348 PyErr_SetString(PyExc_TypeError,
1349 "a list cannot be modified while it is being sorted");
1350 return NULL;
1351}
1352
1353static PyMethodDef immutable_list_methods[] = {
1354 {"append", (PyCFunction)immutable_list_op},
1355 {"insert", (PyCFunction)immutable_list_op},
1356 {"remove", (PyCFunction)immutable_list_op},
1357 {"index", (PyCFunction)listindex},
1358 {"count", (PyCFunction)listcount},
1359 {"reverse", (PyCFunction)immutable_list_op},
1360 {"sort", (PyCFunction)immutable_list_op},
1361 {NULL, NULL} /* sentinel */
1362};
1363
1364static PyObject *
1365immutable_list_getattr(f, name)
1366 PyListObject *f;
1367 char *name;
1368{
1369 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
1370}
1371
1372static int
1373immutable_list_ass(/*No args!*/)
1374{
1375 immutable_list_op();
1376 return -1;
1377}
1378
1379static PySequenceMethods immutable_list_as_sequence = {
1380 (inquiry)list_length, /*sq_length*/
1381 (binaryfunc)list_concat, /*sq_concat*/
1382 (intargfunc)list_repeat, /*sq_repeat*/
1383 (intargfunc)list_item, /*sq_item*/
1384 (intintargfunc)list_slice, /*sq_slice*/
1385 (intobjargproc)immutable_list_ass, /*sq_ass_item*/
1386 (intintobjargproc)immutable_list_ass, /*sq_ass_slice*/
1387};
1388
1389static PyTypeObject immutable_list_type = {
1390 PyObject_HEAD_INIT(&PyType_Type)
1391 0,
1392 "list (immutable, during sort)",
1393 sizeof(PyListObject),
1394 0,
1395 0, /*tp_dealloc*/ /* Cannot happen */
1396 (printfunc)list_print, /*tp_print*/
1397 (getattrfunc)immutable_list_getattr, /*tp_getattr*/
1398 0, /*tp_setattr*/
1399 0, /*tp_compare*/ /* Won't be called */
1400 (reprfunc)list_repr, /*tp_repr*/
1401 0, /*tp_as_number*/
1402 &immutable_list_as_sequence, /*tp_as_sequence*/
1403 0, /*tp_as_mapping*/
1404};