blob: a75100e7fbd2fb300786cec8f31b6d2d9308d7a6 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* List object implementation */
33
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034#include "Python.h"
35
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000036#ifdef STDC_HEADERS
37#include <stddef.h>
38#else
39#include <sys/types.h> /* For size_t */
40#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Guido van Rossumc0b618a1997-05-02 03:12:38 +000042#define ROUNDUP(n, PyTryBlock) \
43 ((((n)+(PyTryBlock)-1)/(PyTryBlock))*(PyTryBlock))
Guido van Rossuma46d51d1995-01-26 22:59:43 +000044
45static int
Guido van Rossuma27d1121997-08-25 18:36:23 +000046roundupsize(n)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000047 int n;
48{
49 if (n < 500)
50 return ROUNDUP(n, 10);
51 else
52 return ROUNDUP(n, 100);
53}
54
Guido van Rossuma27d1121997-08-25 18:36:23 +000055#define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems))
Guido van Rossuma46d51d1995-01-26 22:59:43 +000056
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057PyObject *
58PyList_New(size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059 int size;
60{
61 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062 PyListObject *op;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000063 size_t nbytes;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 if (size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 return NULL;
67 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000068 nbytes = size * sizeof(PyObject *);
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000069 /* Check for overflow */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000070 if (nbytes / sizeof(PyObject *) != (size_t)size) {
71 return PyErr_NoMemory();
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000072 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000073 /* PyObject_NewVar is inlined */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +000074 op = (PyListObject *) PyObject_MALLOC(sizeof(PyListObject)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000075 + PyGC_HEAD_SIZE);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000079 op = (PyListObject *) PyObject_FROM_GC(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080 if (size <= 0) {
81 op->ob_item = NULL;
82 }
83 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +000084 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +000086 PyObject_FREE(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 }
89 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000090 PyObject_INIT_VAR(op, &PyList_Type, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091 for (i = 0; i < size; i++)
92 op->ob_item[i] = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000093 PyObject_GC_Init(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000094 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095}
96
97int
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098PyList_Size(op)
99 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000101 if (!PyList_Check(op)) {
102 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103 return -1;
104 }
105 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000106 return ((PyListObject *)op) -> ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107}
108
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109static PyObject *indexerr;
Guido van Rossum929f1b81996-08-09 20:51:27 +0000110
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000111PyObject *
112PyList_GetItem(op, i)
113 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114 int i;
115{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000116 if (!PyList_Check(op)) {
117 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118 return NULL;
119 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000121 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000122 indexerr = PyString_FromString(
123 "list index out of range");
124 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125 return NULL;
126 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000127 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128}
129
130int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000131PyList_SetItem(op, i, newitem)
132 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133 register int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134 register PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000136 register PyObject *olditem;
137 register PyObject **p;
138 if (!PyList_Check(op)) {
139 Py_XDECREF(newitem);
140 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000141 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000143 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
144 Py_XDECREF(newitem);
145 PyErr_SetString(PyExc_IndexError,
146 "list assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000147 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149 p = ((PyListObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000150 olditem = *p;
151 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 return 0;
154}
155
156static int
157ins1(self, where, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000158 PyListObject *self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161{
162 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163 PyObject **items;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000164 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000166 return -1;
167 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168 items = self->ob_item;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000169 NRESIZE(items, PyObject *, self->ob_size+1);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000170 if (items == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000171 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000172 return -1;
173 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174 if (where < 0)
175 where = 0;
176 if (where > self->ob_size)
177 where = self->ob_size;
178 for (i = self->ob_size; --i >= where; )
179 items[i+1] = items[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000180 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181 items[where] = v;
182 self->ob_item = items;
183 self->ob_size++;
184 return 0;
185}
186
187int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000188PyList_Insert(op, where, newitem)
189 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191 PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000193 if (!PyList_Check(op)) {
194 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000195 return -1;
196 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000197 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198}
199
200int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000201PyList_Append(op, newitem)
202 PyObject *op;
203 PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 if (!PyList_Check(op)) {
206 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000207 return -1;
208 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209 return ins1((PyListObject *)op,
210 (int) ((PyListObject *)op)->ob_size, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211}
212
213/* Methods */
214
215static void
216list_dealloc(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000217 PyListObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218{
219 int i;
Guido van Rossumd724b232000-03-13 16:01:29 +0000220 Py_TRASHCAN_SAFE_BEGIN(op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000221 PyObject_GC_Fini(op);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000222 if (op->ob_item != NULL) {
Guido van Rossumfa717011999-06-09 15:19:34 +0000223 /* Do it backwards, for Christian Tismer.
224 There's a simple test case where somehow this reduces
225 thrashing when a *very* large list is created and
226 immediately deleted. */
227 i = op->ob_size;
228 while (--i >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229 Py_XDECREF(op->ob_item[i]);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000230 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000231 PyMem_FREE(op->ob_item);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000232 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000233 PyObject_DEL(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000234 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235}
236
Guido van Rossum90933611991-06-07 16:10:43 +0000237static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238list_print(op, fp, flags)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000239 PyListObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240 FILE *fp;
241 int flags;
242{
243 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000244
245 i = Py_ReprEnter((PyObject*)op);
246 if (i != 0) {
247 if (i < 0)
248 return i;
249 fprintf(fp, "[...]");
250 return 0;
251 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252 fprintf(fp, "[");
Guido van Rossum90933611991-06-07 16:10:43 +0000253 for (i = 0; i < op->ob_size; i++) {
254 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255 fprintf(fp, ", ");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000256 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
257 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000258 return -1;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000259 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260 }
261 fprintf(fp, "]");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000262 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000263 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264}
265
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000266static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267list_repr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000268 PyListObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000269{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000270 PyObject *s, *comma;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000272
273 i = Py_ReprEnter((PyObject*)v);
274 if (i != 0) {
275 if (i > 0)
276 return PyString_FromString("[...]");
277 return NULL;
278 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000279 s = PyString_FromString("[");
280 comma = PyString_FromString(", ");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281 for (i = 0; i < v->ob_size && s != NULL; i++) {
282 if (i > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000283 PyString_Concat(&s, comma);
284 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000286 Py_XDECREF(comma);
287 PyString_ConcatAndDel(&s, PyString_FromString("]"));
Guido van Rossumfb376de1998-04-10 22:47:27 +0000288 Py_ReprLeave((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289 return s;
290}
291
292static int
293list_compare(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000294 PyListObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296 int i;
Guido van Rossumae621ff1998-05-28 20:18:46 +0000297 for (i = 0; i < v->ob_size && i < w->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000298 int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299 if (cmp != 0)
300 return cmp;
301 }
302 return v->ob_size - w->ob_size;
303}
304
305static int
306list_length(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000307 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308{
309 return a->ob_size;
310}
311
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000312
313
314static int
315list_contains(a, el)
316 PyListObject *a;
317 PyObject *el;
318{
319 int i, cmp;
320
321 for (i = 0; i < a->ob_size; ++i) {
322 cmp = PyObject_Compare(el, PyList_GET_ITEM(a, i));
323 if (cmp == 0)
324 return 1;
325 if (PyErr_Occurred())
326 return -1;
327 }
328 return 0;
329}
330
331
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000332static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333list_item(a, i)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000334 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335 int i;
336{
337 if (i < 0 || i >= a->ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000338 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339 indexerr = PyString_FromString(
340 "list index out of range");
341 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342 return NULL;
343 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000344 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345 return a->ob_item[i];
346}
347
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000348static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349list_slice(a, ilow, ihigh)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000350 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351 int ilow, ihigh;
352{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000353 PyListObject *np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354 int i;
355 if (ilow < 0)
356 ilow = 0;
357 else if (ilow > a->ob_size)
358 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359 if (ihigh < ilow)
360 ihigh = ilow;
361 else if (ihigh > a->ob_size)
362 ihigh = a->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000363 np = (PyListObject *) PyList_New(ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364 if (np == NULL)
365 return NULL;
366 for (i = ilow; i < ihigh; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000367 PyObject *v = a->ob_item[i];
368 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369 np->ob_item[i - ilow] = v;
370 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000371 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372}
373
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000374PyObject *
375PyList_GetSlice(a, ilow, ihigh)
376 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000377 int ilow, ihigh;
378{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000379 if (!PyList_Check(a)) {
380 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000381 return NULL;
382 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000384}
385
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387list_concat(a, bb)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000388 PyListObject *a;
389 PyObject *bb;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390{
391 int size;
392 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393 PyListObject *np;
394 if (!PyList_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000395 PyErr_Format(PyExc_TypeError,
Fred Drake914a2ed2000-06-01 14:31:03 +0000396 "can only concatenate list (not \"%.200s\") to list",
397 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398 return NULL;
399 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400#define b ((PyListObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401 size = a->ob_size + b->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000404 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405 }
406 for (i = 0; i < a->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407 PyObject *v = a->ob_item[i];
408 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409 np->ob_item[i] = v;
410 }
411 for (i = 0; i < b->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412 PyObject *v = b->ob_item[i];
413 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414 np->ob_item[i + a->ob_size] = v;
415 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000416 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000417#undef b
418}
419
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000420static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +0000421list_repeat(a, n)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000422 PyListObject *a;
Guido van Rossumed98d481991-03-06 13:07:53 +0000423 int n;
424{
425 int i, j;
426 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000427 PyListObject *np;
428 PyObject **p;
Guido van Rossumed98d481991-03-06 13:07:53 +0000429 if (n < 0)
430 n = 0;
431 size = a->ob_size * n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000432 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000433 if (np == NULL)
434 return NULL;
435 p = np->ob_item;
436 for (i = 0; i < n; i++) {
437 for (j = 0; j < a->ob_size; j++) {
438 *p = a->ob_item[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000440 p++;
441 }
442 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000444}
445
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447list_ass_slice(a, ilow, ihigh, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000448 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000450 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000452 /* Because [X]DECREF can recursively invoke list operations on
453 this list, we must postpone all [X]DECREF activity until
454 after the list is back in its canonical shape. Therefore
455 we must allocate an additional array, 'recycle', into which
456 we temporarily copy the items that are deleted from the
457 list. :-( */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000458 PyObject **recycle, **p;
459 PyObject **item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460 int n; /* Size of replacement list */
461 int d; /* Change in size */
462 int k; /* Loop index */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000463#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000464 if (v == NULL)
465 n = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466 else if (PyList_Check(v)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000467 n = b->ob_size;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000468 if (a == b) {
469 /* Special case "a[i:j] = a" -- copy b first */
470 int ret;
471 v = list_slice(b, 0, n);
472 ret = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473 Py_DECREF(v);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000474 return ret;
475 }
476 }
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000477 else {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000478 PyErr_Format(PyExc_TypeError,
479 "must assign list (not \"%.200s\") to slice",
480 v->ob_type->tp_name);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000481 return -1;
482 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483 if (ilow < 0)
484 ilow = 0;
485 else if (ilow > a->ob_size)
486 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487 if (ihigh < ilow)
488 ihigh = ilow;
489 else if (ihigh > a->ob_size)
490 ihigh = a->ob_size;
491 item = a->ob_item;
492 d = n - (ihigh-ilow);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000493 if (ihigh > ilow)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000494 p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000495 else
496 p = recycle = NULL;
497 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498 for (k = ilow; k < ihigh; k++)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000499 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000500 if (d < 0) {
501 for (/*k = ihigh*/; k < a->ob_size; k++)
502 item[k+d] = item[k];
503 a->ob_size += d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000504 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000505 a->ob_item = item;
506 }
507 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000508 else { /* Insert d items; recycle ihigh-ilow items */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000509 NRESIZE(item, PyObject *, a->ob_size + d);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000510 if (item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000511 if (recycle != NULL)
512 PyMem_DEL(recycle);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000514 return -1;
515 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000516 for (k = a->ob_size; --k >= ihigh; )
517 item[k+d] = item[k];
518 for (/*k = ihigh-1*/; k >= ilow; --k)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000519 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520 a->ob_item = item;
521 a->ob_size += d;
522 }
523 for (k = 0; k < n; k++, ilow++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524 PyObject *w = b->ob_item[k];
525 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000526 item[ilow] = w;
527 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000528 if (recycle) {
529 while (--p >= recycle)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530 Py_XDECREF(*p);
531 PyMem_DEL(recycle);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000532 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000533 return 0;
534#undef b
535}
536
Guido van Rossum234f9421993-06-17 12:35:49 +0000537int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000538PyList_SetSlice(a, ilow, ihigh, v)
539 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000540 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000541 PyObject *v;
Guido van Rossum234f9421993-06-17 12:35:49 +0000542{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000543 if (!PyList_Check(a)) {
544 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000545 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000546 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000548}
549
Guido van Rossum4a450d01991-04-03 19:05:18 +0000550static int
551list_ass_item(a, i, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552 PyListObject *a;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000553 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 PyObject *v;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000555{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000556 PyObject *old_value;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000557 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 PyErr_SetString(PyExc_IndexError,
559 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000560 return -1;
561 }
562 if (v == NULL)
563 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000565 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000566 a->ob_item[i] = v;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000567 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000568 return 0;
569}
570
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572ins(self, where, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000573 PyListObject *self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000575 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576{
577 if (ins1(self, where, v) != 0)
578 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579 Py_INCREF(Py_None);
580 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581}
582
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000583static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584listinsert(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000585 PyListObject *self;
586 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587{
588 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000590 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591 return NULL;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000592 return ins(self, i, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000593}
594
Guido van Rossumef93b872000-03-13 15:41:59 +0000595/* Define NO_STRICT_LIST_APPEND to enable multi-argument append() */
596
597#ifndef NO_STRICT_LIST_APPEND
598#define PyArg_ParseTuple_Compat1 PyArg_ParseTuple
599#else
600#define PyArg_ParseTuple_Compat1(args, format, ret) \
601( \
602 PyTuple_GET_SIZE(args) > 1 ? (*ret = args, 1) : \
603 PyTuple_GET_SIZE(args) == 1 ? (*ret = PyTuple_GET_ITEM(args, 0), 1) : \
604 PyArg_ParseTuple(args, format, ret) \
605)
606#endif
607
608
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610listappend(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000611 PyListObject *self;
612 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000613{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000614 PyObject *v;
Guido van Rossumef93b872000-03-13 15:41:59 +0000615 if (!PyArg_ParseTuple_Compat1(args, "O:append", &v))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000616 return NULL;
617 return ins(self, (int) self->ob_size, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000618}
619
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000620static PyObject *
Barry Warsawdedf6d61998-10-09 16:37:25 +0000621listextend(self, args)
622 PyListObject *self;
623 PyObject *args;
624{
625 PyObject *b = NULL, *res = NULL;
626 PyObject **items;
627 int selflen = PyList_GET_SIZE(self);
628 int blen;
629 register int i;
630
Guido van Rossumc00a9382000-02-24 21:48:29 +0000631 if (!PyArg_ParseTuple(args, "O:extend", &b))
Barry Warsawdedf6d61998-10-09 16:37:25 +0000632 return NULL;
633
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000634 b = PySequence_Fast(b, "list.extend() argument must be a sequence");
635 if (!b)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000636 return NULL;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000637
638 if (PyObject_Length(b) == 0)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000639 /* short circuit when b is empty */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000640 goto ok;
641
Barry Warsawdedf6d61998-10-09 16:37:25 +0000642 if (self == (PyListObject*)b) {
643 /* as in list_ass_slice() we must special case the
644 * situation: a.extend(a)
645 *
646 * XXX: I think this way ought to be faster than using
647 * list_slice() the way list_ass_slice() does.
648 */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000649 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000650 b = PyList_New(selflen);
651 if (!b)
652 return NULL;
653 for (i = 0; i < selflen; i++) {
654 PyObject *o = PyList_GET_ITEM(self, i);
655 Py_INCREF(o);
656 PyList_SET_ITEM(b, i, o);
657 }
658 }
Barry Warsawdedf6d61998-10-09 16:37:25 +0000659
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000660 blen = PyObject_Length(b);
661
Barry Warsawdedf6d61998-10-09 16:37:25 +0000662 /* resize a using idiom */
663 items = self->ob_item;
664 NRESIZE(items, PyObject*, selflen + blen);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000665 if (items == NULL) {
Barry Warsawdedf6d61998-10-09 16:37:25 +0000666 PyErr_NoMemory();
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000667 goto failed;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000668 }
669 self->ob_item = items;
670
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000671 /* populate the end of self with b's items */
Barry Warsawdedf6d61998-10-09 16:37:25 +0000672 for (i = 0; i < blen; i++) {
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000673 PyObject *o = PySequence_Fast_GET_ITEM(b, i);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000674 Py_INCREF(o);
675 PyList_SET_ITEM(self, self->ob_size++, o);
676 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000677 ok:
Barry Warsawdedf6d61998-10-09 16:37:25 +0000678 res = Py_None;
679 Py_INCREF(res);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000680 failed:
681 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000682 return res;
683}
684
685
686static PyObject *
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000687listpop(self, args)
688 PyListObject *self;
689 PyObject *args;
690{
691 int i = -1;
692 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000693 if (!PyArg_ParseTuple(args, "|i:pop", &i))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000694 return NULL;
695 if (self->ob_size == 0) {
696 /* Special-case most common failure cause */
697 PyErr_SetString(PyExc_IndexError, "pop from empty list");
698 return NULL;
699 }
700 if (i < 0)
701 i += self->ob_size;
702 if (i < 0 || i >= self->ob_size) {
703 PyErr_SetString(PyExc_IndexError, "pop index out of range");
704 return NULL;
705 }
706 v = self->ob_item[i];
707 Py_INCREF(v);
708 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
709 Py_DECREF(v);
710 return NULL;
711 }
712 return v;
713}
714
Guido van Rossum3f236de1996-12-10 23:55:39 +0000715/* New quicksort implementation for arrays of object pointers.
716 Thanks to discussions with Tim Peters. */
717
718/* CMPERROR is returned by our comparison function when an error
719 occurred. This is the largest negative integer (0x80000000 on a
720 32-bit system). */
Guido van Rossum19700b61997-03-05 00:45:43 +0000721#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
Guido van Rossum3f236de1996-12-10 23:55:39 +0000722
723/* Comparison function. Takes care of calling a user-supplied
724 comparison function (any callable Python object). Calls the
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000725 standard comparison function, PyObject_Compare(), if the user-
726 supplied function is NULL. */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000727
728static int
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000729docompare(x, y, compare)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000730 PyObject *x;
731 PyObject *y;
732 PyObject *compare;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000733{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000734 PyObject *args, *res;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000735 int i;
736
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000737 if (compare == NULL) {
738 i = PyObject_Compare(x, y);
739 if (i && PyErr_Occurred())
740 i = CMPERROR;
741 return i;
742 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000743
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744 args = Py_BuildValue("(OO)", x, y);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000745 if (args == NULL)
746 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000747 res = PyEval_CallObject(compare, args);
748 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000749 if (res == NULL)
750 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000751 if (!PyInt_Check(res)) {
752 Py_DECREF(res);
753 PyErr_SetString(PyExc_TypeError,
Guido van Rossum9bcd1d71999-04-19 17:44:39 +0000754 "comparison function must return int");
Guido van Rossum3f236de1996-12-10 23:55:39 +0000755 return CMPERROR;
756 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000757 i = PyInt_AsLong(res);
758 Py_DECREF(res);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000759 if (i < 0)
760 return -1;
761 if (i > 0)
762 return 1;
763 return 0;
764}
765
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000766/* MINSIZE is the smallest array that will get a full-blown samplesort
767 treatment; smaller arrays are sorted using binary insertion. It must
768 be at least 7 for the samplesort implementation to work. Binary
769 insertion does fewer compares, but can suffer O(N**2) data movement.
770 The more expensive compares, the larger MINSIZE should be. */
771#define MINSIZE 100
772
773/* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
774 partition; smaller slices are passed to binarysort. It must be at
775 least 2, and no larger than MINSIZE. Setting it higher reduces the #
776 of compares slowly, but increases the amount of data movement quickly.
777 The value here was chosen assuming a compare costs ~25x more than
778 swapping a pair of memory-resident pointers -- but under that assumption,
779 changing the value by a few dozen more or less has aggregate effect
780 under 1%. So the value is crucial, but not touchy <wink>. */
781#define MINPARTITIONSIZE 40
782
783/* MAXMERGE is the largest number of elements we'll always merge into
784 a known-to-be sorted chunk via binary insertion, regardless of the
785 size of that chunk. Given a chunk of N sorted elements, and a group
786 of K unknowns, the largest K for which it's better to do insertion
787 (than a full-blown sort) is a complicated function of N and K mostly
788 involving the expected number of compares and data moves under each
789 approach, and the relative cost of those operations on a specific
790 architecure. The fixed value here is conservative, and should be a
791 clear win regardless of architecture or N. */
792#define MAXMERGE 15
Guido van Rossum3f236de1996-12-10 23:55:39 +0000793
Guido van Rossum3f236de1996-12-10 23:55:39 +0000794/* STACKSIZE is the size of our work stack. A rough estimate is that
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000795 this allows us to sort arrays of size N where
796 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
797 for arrays of size 2**64. Because we push the biggest partition
798 first, the worst case occurs when all subarrays are always partitioned
799 exactly in two. */
800#define STACKSIZE 60
Guido van Rossum3f236de1996-12-10 23:55:39 +0000801
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000802
803#define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
804
805/* binarysort is the best method for sorting small arrays: it does
806 few compares, but can do data movement quadratic in the number of
807 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000808 [lo, hi) is a contiguous slice of a list, and is sorted via
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000809 binary insertion.
810 On entry, must have lo <= start <= hi, and that [lo, start) is already
811 sorted (pass start == lo if you don't know!).
812 If docompare complains (returns CMPERROR) return -1, else 0.
813 Even in case of error, the output slice will be some permutation of
814 the input (nothing is lost or duplicated).
815*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000816
817static int
Guido van Rossum42812581998-06-17 14:15:44 +0000818binarysort(lo, hi, start, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000819 PyObject **lo;
820 PyObject **hi;
821 PyObject **start;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000822 PyObject *compare;/* Comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000823{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000824 /* assert lo <= start <= hi
825 assert [lo, start) is sorted */
826 register int k;
827 register PyObject **l, **p, **r;
828 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000829
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000830 if (lo == start)
831 ++start;
832 for (; start < hi; ++start) {
833 /* set l to where *start belongs */
834 l = lo;
835 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000836 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000837 do {
838 p = l + ((r - l) >> 1);
839 SETK(pivot, *p);
840 if (k < 0)
841 r = p;
842 else
843 l = p + 1;
844 } while (l < r);
845 /* Pivot should go at l -- slide over to make room.
846 Caution: using memmove is much slower under MSVC 5;
847 we're not usually moving many slots. */
848 for (p = start; p > l; --p)
849 *p = *(p-1);
850 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000851 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000852 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000853
854 fail:
855 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000856}
857
858/* samplesortslice is the sorting workhorse.
Guido van Rossum42812581998-06-17 14:15:44 +0000859 [lo, hi) is a contiguous slice of a list, to be sorted in place.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000860 On entry, must have lo <= hi,
861 If docompare complains (returns CMPERROR) return -1, else 0.
862 Even in case of error, the output slice will be some permutation of
863 the input (nothing is lost or duplicated).
864
865 samplesort is basically quicksort on steroids: a power of 2 close
866 to n/ln(n) is computed, and that many elements (less 1) are picked at
867 random from the array and sorted. These 2**k - 1 elements are then
868 used as preselected pivots for an equal number of quicksort
869 partitioning steps, partitioning the slice into 2**k chunks each of
870 size about ln(n). These small final chunks are then usually handled
871 by binarysort. Note that when k=1, this is roughly the same as an
872 ordinary quicksort using a random pivot, and when k=2 this is roughly
873 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
874 this a "median of n/ln(n)" quicksort. You can also view it as a kind
875 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
876
877 The large number of samples makes a quadratic-time case almost
878 impossible, and asymptotically drives the average-case number of
879 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
880 3 variant) down to N lg N.
881
882 We also play lots of low-level tricks to cut the number of compares.
883
884 Very obscure: To avoid using extra memory, the PPs are stored in the
885 array and shuffled around as partitioning proceeds. At the start of a
886 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
887 adjacent (either on the left or the right!) to a chunk of X elements
888 that are to be partitioned: P X or X P. In either case we need to
889 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
890 left, followed by the PP to be used for this step (that's the middle
891 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
892 P X or X P -> Psmall pivot X Plarge
893 and the order of the PPs must not be altered. It can take a while
894 to realize this isn't trivial! It can take even longer <wink> to
895 understand why the simple code below works, using only 2**(m-1) swaps.
896 The key is that the order of the X elements isn't necessarily
897 preserved: X can end up as some cyclic permutation of its original
898 order. That's OK, because X is unsorted anyway. If the order of X
899 had to be preserved too, the simplest method I know of using O(1)
900 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
901 Since len(X) is typically several times larger than 2**(m-1), that
902 would slow things down.
903*/
904
905struct SamplesortStackNode {
906 /* Represents a slice of the array, from (& including) lo up
907 to (but excluding) hi. "extra" additional & adjacent elements
908 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
909 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
910 already sorted, but nothing is known about the other elements
911 in [lo, hi). |extra| is always one less than a power of 2.
912 When extra is 0, we're out of PPs, and the slice must be
913 sorted by some other means. */
914 PyObject **lo;
915 PyObject **hi;
916 int extra;
917};
918
919/* 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 +0000920 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000921 is undesirable, so cutoff values are canned in the "cutoff" table
922 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
923#define CUTOFFBASE 4
Guido van Rossum3aa23fd1999-01-14 19:01:53 +0000924static long cutoff[] = {
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000925 43, /* smallest N such that k == 4 */
926 106, /* etc */
927 250,
928 576,
929 1298,
930 2885,
931 6339,
932 13805,
933 29843,
934 64116,
935 137030,
936 291554,
937 617916,
938 1305130,
939 2748295,
940 5771662,
941 12091672,
942 25276798,
943 52734615,
944 109820537,
945 228324027,
946 473977813,
947 982548444, /* smallest N such that k == 26 */
948 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
949};
950
951static int
Guido van Rossum42812581998-06-17 14:15:44 +0000952samplesortslice(lo, hi, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000953 PyObject **lo;
954 PyObject **hi;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000955 PyObject *compare;/* Comparison function object, or NULL for default */
956{
957 register PyObject **l, **r;
958 register PyObject *tmp, *pivot;
959 register int k;
960 int n, extra, top, extraOnRight;
961 struct SamplesortStackNode stack[STACKSIZE];
962
963 /* assert lo <= hi */
964 n = hi - lo;
965
966 /* ----------------------------------------------------------
967 * Special cases
968 * --------------------------------------------------------*/
969 if (n < 2)
970 return 0;
971
972 /* Set r to the largest value such that [lo,r) is sorted.
973 This catches the already-sorted case, the all-the-same
974 case, and the appended-a-few-elements-to-a-sorted-list case.
975 If the array is unsorted, we're very likely to get out of
976 the loop fast, so the test is cheap if it doesn't pay off.
977 */
978 /* assert lo < hi */
979 for (r = lo+1; r < hi; ++r) {
980 SETK(*r, *(r-1));
981 if (k < 0)
982 break;
983 }
984 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
985 few unknowns, or few elements in total. */
986 if (hi - r <= MAXMERGE || n < MINSIZE)
Guido van Rossum42812581998-06-17 14:15:44 +0000987 return binarysort(lo, hi, r, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000988
989 /* Check for the array already being reverse-sorted. Typical
990 benchmark-driven silliness <wink>. */
991 /* assert lo < hi */
992 for (r = lo+1; r < hi; ++r) {
993 SETK(*(r-1), *r);
994 if (k < 0)
995 break;
996 }
997 if (hi - r <= MAXMERGE) {
998 /* Reverse the reversed prefix, then insert the tail */
999 PyObject **originalr = r;
1000 l = lo;
1001 do {
1002 --r;
1003 tmp = *l; *l = *r; *r = tmp;
1004 ++l;
1005 } while (l < r);
Guido van Rossum42812581998-06-17 14:15:44 +00001006 return binarysort(lo, hi, originalr, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001007 }
1008
1009 /* ----------------------------------------------------------
1010 * Normal case setup: a large array without obvious pattern.
1011 * --------------------------------------------------------*/
1012
1013 /* extra := a power of 2 ~= n/ln(n), less 1.
1014 First find the smallest extra s.t. n < cutoff[extra] */
1015 for (extra = 0;
1016 extra < sizeof(cutoff) / sizeof(cutoff[0]);
1017 ++extra) {
1018 if (n < cutoff[extra])
1019 break;
1020 /* note that if we fall out of the loop, the value of
1021 extra still makes *sense*, but may be smaller than
1022 we would like (but the array has more than ~= 2**31
1023 elements in this case!) */
1024 }
1025 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1026 have is CUTOFFBASE-1, so
1027 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1028 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
1029 /* assert extra > 0 and n >= extra */
1030
1031 /* Swap that many values to the start of the array. The
1032 selection of elements is pseudo-random, but the same on
1033 every run (this is intentional! timing algorithm changes is
1034 a pain if timing varies across runs). */
1035 {
1036 unsigned int seed = n / extra; /* arbitrary */
1037 unsigned int i;
1038 for (i = 0; i < (unsigned)extra; ++i) {
1039 /* j := random int in [i, n) */
1040 unsigned int j;
1041 seed = seed * 69069 + 7;
1042 j = i + seed % (n - i);
1043 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
1044 }
1045 }
1046
1047 /* Recursively sort the preselected pivots. */
Guido van Rossum42812581998-06-17 14:15:44 +00001048 if (samplesortslice(lo, lo + extra, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001049 goto fail;
1050
1051 top = 0; /* index of available stack slot */
1052 lo += extra; /* point to first unknown */
1053 extraOnRight = 0; /* the PPs are at the left end */
1054
1055 /* ----------------------------------------------------------
1056 * Partition [lo, hi), and repeat until out of work.
1057 * --------------------------------------------------------*/
1058 for (;;) {
1059 /* assert lo <= hi, so n >= 0 */
1060 n = hi - lo;
1061
1062 /* We may not want, or may not be able, to partition:
1063 If n is small, it's quicker to insert.
1064 If extra is 0, we're out of pivots, and *must* use
1065 another method.
1066 */
1067 if (n < MINPARTITIONSIZE || extra == 0) {
1068 if (n >= MINSIZE) {
1069 /* assert extra == 0
1070 This is rare, since the average size
1071 of a final block is only about
1072 ln(original n). */
Guido van Rossum42812581998-06-17 14:15:44 +00001073 if (samplesortslice(lo, hi, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001074 goto fail;
1075 }
1076 else {
1077 /* Binary insertion should be quicker,
1078 and we can take advantage of the PPs
1079 already being sorted. */
1080 if (extraOnRight && extra) {
1081 /* swap the PPs to the left end */
1082 k = extra;
1083 do {
1084 tmp = *lo;
1085 *lo = *hi;
1086 *hi = tmp;
1087 ++lo; ++hi;
1088 } while (--k);
1089 }
1090 if (binarysort(lo - extra, hi, lo,
Guido van Rossum42812581998-06-17 14:15:44 +00001091 compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001092 goto fail;
1093 }
1094
1095 /* Find another slice to work on. */
1096 if (--top < 0)
1097 break; /* no more -- done! */
1098 lo = stack[top].lo;
1099 hi = stack[top].hi;
1100 extra = stack[top].extra;
1101 extraOnRight = 0;
1102 if (extra < 0) {
1103 extraOnRight = 1;
1104 extra = -extra;
1105 }
1106 continue;
1107 }
1108
1109 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1110 Then our preselected pivot is at (extra-1)/2, and we
1111 want to move the PPs before that to the left end of
1112 the slice, and the PPs after that to the right end.
1113 The following section changes extra, lo, hi, and the
1114 slice such that:
1115 [lo-extra, lo) contains the smaller PPs.
1116 *lo == our PP.
1117 (lo, hi) contains the unknown elements.
1118 [hi, hi+extra) contains the larger PPs.
1119 */
1120 k = extra >>= 1; /* num PPs to move */
1121 if (extraOnRight) {
1122 /* Swap the smaller PPs to the left end.
1123 Note that this loop actually moves k+1 items:
1124 the last is our PP */
1125 do {
1126 tmp = *lo; *lo = *hi; *hi = tmp;
1127 ++lo; ++hi;
1128 } while (k--);
1129 }
1130 else {
1131 /* Swap the larger PPs to the right end. */
1132 while (k--) {
1133 --lo; --hi;
1134 tmp = *lo; *lo = *hi; *hi = tmp;
1135 }
1136 }
1137 --lo; /* *lo is now our PP */
1138 pivot = *lo;
1139
1140 /* Now an almost-ordinary quicksort partition step.
1141 Note that most of the time is spent here!
1142 Only odd thing is that we partition into < and >=,
1143 instead of the usual <= and >=. This helps when
1144 there are lots of duplicates of different values,
1145 because it eventually tends to make subfiles
1146 "pure" (all duplicates), and we special-case for
1147 duplicates later. */
1148 l = lo + 1;
1149 r = hi - 1;
1150 /* assert lo < l < r < hi (small n weeded out above) */
1151
1152 do {
1153 /* slide l right, looking for key >= pivot */
1154 do {
1155 SETK(*l, pivot);
1156 if (k < 0)
1157 ++l;
1158 else
1159 break;
1160 } while (l < r);
1161
1162 /* slide r left, looking for key < pivot */
1163 while (l < r) {
Guido van Rossum42812581998-06-17 14:15:44 +00001164 register PyObject *rval = *r--;
1165 SETK(rval, pivot);
1166 if (k < 0) {
1167 /* swap and advance */
1168 r[1] = *l;
1169 *l++ = rval;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001170 break;
Guido van Rossum42812581998-06-17 14:15:44 +00001171 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001172 }
1173
1174 } while (l < r);
1175
1176 /* assert lo < r <= l < hi
1177 assert r == l or r+1 == l
1178 everything to the left of l is < pivot, and
1179 everything to the right of r is >= pivot */
1180
1181 if (l == r) {
1182 SETK(*r, pivot);
1183 if (k < 0)
1184 ++l;
1185 else
1186 --r;
1187 }
1188 /* assert lo <= r and r+1 == l and l <= hi
1189 assert r == lo or a[r] < pivot
1190 assert a[lo] is pivot
1191 assert l == hi or a[l] >= pivot
1192 Swap the pivot into "the middle", so we can henceforth
1193 ignore it.
1194 */
1195 *lo = *r;
1196 *r = pivot;
1197
1198 /* The following is true now, & will be preserved:
1199 All in [lo,r) are < pivot
1200 All in [r,l) == pivot (& so can be ignored)
1201 All in [l,hi) are >= pivot */
1202
1203 /* Check for duplicates of the pivot. One compare is
1204 wasted if there are no duplicates, but can win big
1205 when there are.
1206 Tricky: we're sticking to "<" compares, so deduce
1207 equality indirectly. We know pivot <= *l, so they're
1208 equal iff not pivot < *l.
1209 */
1210 while (l < hi) {
1211 /* pivot <= *l known */
1212 SETK(pivot, *l);
1213 if (k < 0)
1214 break;
1215 else
1216 /* <= and not < implies == */
1217 ++l;
1218 }
1219
1220 /* assert lo <= r < l <= hi
1221 Partitions are [lo, r) and [l, hi) */
1222
1223 /* push fattest first; remember we still have extra PPs
1224 to the left of the left chunk and to the right of
1225 the right chunk! */
1226 /* assert top < STACKSIZE */
1227 if (r - lo <= hi - l) {
1228 /* second is bigger */
1229 stack[top].lo = l;
1230 stack[top].hi = hi;
1231 stack[top].extra = -extra;
1232 hi = r;
1233 extraOnRight = 0;
1234 }
1235 else {
1236 /* first is bigger */
1237 stack[top].lo = lo;
1238 stack[top].hi = r;
1239 stack[top].extra = extra;
1240 lo = l;
1241 extraOnRight = 1;
1242 }
1243 ++top;
1244
1245 } /* end of partitioning loop */
1246
1247 return 0;
1248
1249 fail:
1250 return -1;
1251}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001252
1253#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001254
1255staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001256
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001257static PyObject *
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001258listsort(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001259 PyListObject *self;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001260 PyObject *args;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001261{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001262 int err;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001263 PyObject *compare = NULL;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001264
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001265 if (args != NULL) {
Guido van Rossumc00a9382000-02-24 21:48:29 +00001266 if (!PyArg_ParseTuple(args, "|O:sort", &compare))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001267 return NULL;
1268 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001269 self->ob_type = &immutable_list_type;
1270 err = samplesortslice(self->ob_item,
1271 self->ob_item + self->ob_size,
Guido van Rossum42812581998-06-17 14:15:44 +00001272 compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001273 self->ob_type = &PyList_Type;
1274 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001275 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001276 Py_INCREF(Py_None);
1277 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001278}
1279
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001280int
1281PyList_Sort(v)
1282 PyObject *v;
1283{
1284 if (v == NULL || !PyList_Check(v)) {
1285 PyErr_BadInternalCall();
1286 return -1;
1287 }
1288 v = listsort((PyListObject *)v, (PyObject *)NULL);
1289 if (v == NULL)
1290 return -1;
1291 Py_DECREF(v);
1292 return 0;
1293}
1294
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001295static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001296listreverse(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001297 PyListObject *self;
1298 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001299{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001300 register PyObject **p, **q;
1301 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001302
Guido van Rossumc00a9382000-02-24 21:48:29 +00001303 if (!PyArg_ParseTuple(args, ":reverse"))
Guido van Rossumed98d481991-03-06 13:07:53 +00001304 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001305
1306 if (self->ob_size > 1) {
1307 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
1308 p < q; p++, q--) {
1309 tmp = *p;
1310 *p = *q;
1311 *q = tmp;
1312 }
1313 }
1314
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001315 Py_INCREF(Py_None);
1316 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001317}
1318
Guido van Rossum84c76f51990-10-30 13:32:20 +00001319int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001320PyList_Reverse(v)
1321 PyObject *v;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001322{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001323 if (v == NULL || !PyList_Check(v)) {
1324 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001325 return -1;
1326 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001327 v = listreverse((PyListObject *)v, (PyObject *)NULL);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001328 if (v == NULL)
1329 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001330 Py_DECREF(v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001331 return 0;
1332}
1333
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001334PyObject *
1335PyList_AsTuple(v)
1336 PyObject *v;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001337{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001338 PyObject *w;
1339 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001340 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001341 if (v == NULL || !PyList_Check(v)) {
1342 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001343 return NULL;
1344 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001345 n = ((PyListObject *)v)->ob_size;
1346 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001347 if (w == NULL)
1348 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001349 p = ((PyTupleObject *)w)->ob_item;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001350 memcpy((ANY *)p,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001351 (ANY *)((PyListObject *)v)->ob_item,
1352 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001353 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001354 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001355 p++;
1356 }
1357 return w;
1358}
1359
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001360static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001361listindex(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001362 PyListObject *self;
1363 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001364{
1365 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001366 PyObject *v;
1367
Guido van Rossumef93b872000-03-13 15:41:59 +00001368 if (!PyArg_ParseTuple_Compat1(args, "O:index", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001369 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001370 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001371 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001372 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001373 if (PyErr_Occurred())
1374 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001375 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001376 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001377 return NULL;
1378}
1379
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001380static PyObject *
Guido van Rossume6f7d181991-10-20 20:20:40 +00001381listcount(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001382 PyListObject *self;
1383 PyObject *args;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001384{
1385 int count = 0;
1386 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001387 PyObject *v;
1388
Guido van Rossumef93b872000-03-13 15:41:59 +00001389 if (!PyArg_ParseTuple_Compat1(args, "O:count", &v))
Guido van Rossume6f7d181991-10-20 20:20:40 +00001390 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001391 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001392 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001393 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001394 if (PyErr_Occurred())
1395 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001396 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001397 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001398}
1399
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001400static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001401listremove(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001402 PyListObject *self;
1403 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001404{
1405 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001406 PyObject *v;
1407
Guido van Rossumef93b872000-03-13 15:41:59 +00001408 if (!PyArg_ParseTuple_Compat1(args, "O:remove", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001409 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001410 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001411 if (PyObject_Compare(self->ob_item[i], v) == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001412 if (list_ass_slice(self, i, i+1,
1413 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001414 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001415 Py_INCREF(Py_None);
1416 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001417 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001418 if (PyErr_Occurred())
1419 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001420 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001421 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001422 return NULL;
1423}
1424
Jeremy Hylton8caad492000-06-23 14:18:11 +00001425static int
1426list_traverse(PyListObject *o, visitproc visit, void *arg)
1427{
1428 int i, err;
1429 PyObject *x;
1430
1431 for (i = o->ob_size; --i >= 0; ) {
1432 x = o->ob_item[i];
1433 if (x != NULL) {
1434 err = visit(x, arg);
1435 if (err)
1436 return err;
1437 }
1438 }
1439 return 0;
1440}
1441
1442static int
1443list_clear(PyListObject *lp)
1444{
1445 (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0);
1446 return 0;
1447}
1448
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001449static char append_doc[] =
1450"L.append(object) -- append object to end";
Barry Warsawdedf6d61998-10-09 16:37:25 +00001451static char extend_doc[] =
1452"L.extend(list) -- extend list by appending list elements";
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001453static char insert_doc[] =
1454"L.insert(index, object) -- insert object before index";
1455static char pop_doc[] =
1456"L.pop([index]) -> item -- remove and return item at index (default last)";
1457static char remove_doc[] =
1458"L.remove(value) -- remove first occurrence of value";
1459static char index_doc[] =
1460"L.index(value) -> integer -- return index of first occurrence of value";
1461static char count_doc[] =
1462"L.count(value) -> integer -- return number of occurrences of value";
1463static char reverse_doc[] =
1464"L.reverse() -- reverse *IN PLACE*";
1465static char sort_doc[] =
1466"L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1467
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001468static PyMethodDef list_methods[] = {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001469 {"append", (PyCFunction)listappend, 1, append_doc},
1470 {"insert", (PyCFunction)listinsert, 1, insert_doc},
Barry Warsawdedf6d61998-10-09 16:37:25 +00001471 {"extend", (PyCFunction)listextend, 1, extend_doc},
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001472 {"pop", (PyCFunction)listpop, 1, pop_doc},
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001473 {"remove", (PyCFunction)listremove, 1, remove_doc},
1474 {"index", (PyCFunction)listindex, 1, index_doc},
1475 {"count", (PyCFunction)listcount, 1, count_doc},
1476 {"reverse", (PyCFunction)listreverse, 1, reverse_doc},
1477 {"sort", (PyCFunction)listsort, 1, sort_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001478 {NULL, NULL} /* sentinel */
1479};
1480
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001481static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001482list_getattr(f, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001483 PyListObject *f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001484 char *name;
1485{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001486 return Py_FindMethod(list_methods, (PyObject *)f, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001487}
1488
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001489static PySequenceMethods list_as_sequence = {
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001490 (inquiry)list_length, /*sq_length*/
1491 (binaryfunc)list_concat, /*sq_concat*/
1492 (intargfunc)list_repeat, /*sq_repeat*/
1493 (intargfunc)list_item, /*sq_item*/
1494 (intintargfunc)list_slice, /*sq_slice*/
1495 (intobjargproc)list_ass_item, /*sq_ass_item*/
1496 (intintobjargproc)list_ass_slice, /*sq_ass_slice*/
Jeremy Hylton37b1a262000-04-27 21:41:03 +00001497 (objobjproc)list_contains, /*sq_contains*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001498};
1499
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001500PyTypeObject PyList_Type = {
1501 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001502 0,
1503 "list",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001504 sizeof(PyListObject) + PyGC_HEAD_SIZE,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001505 0,
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001506 (destructor)list_dealloc, /*tp_dealloc*/
1507 (printfunc)list_print, /*tp_print*/
1508 (getattrfunc)list_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001509 0, /*tp_setattr*/
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001510 (cmpfunc)list_compare, /*tp_compare*/
1511 (reprfunc)list_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001512 0, /*tp_as_number*/
1513 &list_as_sequence, /*tp_as_sequence*/
1514 0, /*tp_as_mapping*/
Fred Drake56780252000-06-15 14:50:20 +00001515 0, /*tp_hash*/
1516 0, /*tp_call*/
1517 0, /*tp_str*/
1518 0, /*tp_getattro*/
1519 0, /*tp_setattro*/
1520 0, /*tp_as_buffer*/
Jeremy Hyltond08b4c42000-06-23 19:37:02 +00001521 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Jeremy Hylton8caad492000-06-23 14:18:11 +00001522 0, /* tp_doc */
1523 (traverseproc)list_traverse, /* tp_traverse */
1524 (inquiry)list_clear, /* tp_clear */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001525};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001526
1527
1528/* During a sort, we really can't have anyone modifying the list; it could
1529 cause core dumps. Thus, we substitute a dummy type that raises an
1530 explanatory exception when a modifying operation is used. Caveat:
1531 comparisons may behave differently; but I guess it's a bad idea anyway to
1532 compare a list that's being sorted... */
1533
1534static PyObject *
1535immutable_list_op(/*No args!*/)
1536{
1537 PyErr_SetString(PyExc_TypeError,
1538 "a list cannot be modified while it is being sorted");
1539 return NULL;
1540}
1541
1542static PyMethodDef immutable_list_methods[] = {
1543 {"append", (PyCFunction)immutable_list_op},
1544 {"insert", (PyCFunction)immutable_list_op},
1545 {"remove", (PyCFunction)immutable_list_op},
1546 {"index", (PyCFunction)listindex},
1547 {"count", (PyCFunction)listcount},
1548 {"reverse", (PyCFunction)immutable_list_op},
1549 {"sort", (PyCFunction)immutable_list_op},
1550 {NULL, NULL} /* sentinel */
1551};
1552
1553static PyObject *
1554immutable_list_getattr(f, name)
1555 PyListObject *f;
1556 char *name;
1557{
1558 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
1559}
1560
1561static int
1562immutable_list_ass(/*No args!*/)
1563{
1564 immutable_list_op();
1565 return -1;
1566}
1567
1568static PySequenceMethods immutable_list_as_sequence = {
1569 (inquiry)list_length, /*sq_length*/
1570 (binaryfunc)list_concat, /*sq_concat*/
1571 (intargfunc)list_repeat, /*sq_repeat*/
1572 (intargfunc)list_item, /*sq_item*/
1573 (intintargfunc)list_slice, /*sq_slice*/
1574 (intobjargproc)immutable_list_ass, /*sq_ass_item*/
1575 (intintobjargproc)immutable_list_ass, /*sq_ass_slice*/
Fred Drake56780252000-06-15 14:50:20 +00001576 (objobjproc)list_contains, /*sq_contains*/
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001577};
1578
1579static PyTypeObject immutable_list_type = {
1580 PyObject_HEAD_INIT(&PyType_Type)
1581 0,
1582 "list (immutable, during sort)",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001583 sizeof(PyListObject) + PyGC_HEAD_SIZE,
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001584 0,
1585 0, /*tp_dealloc*/ /* Cannot happen */
1586 (printfunc)list_print, /*tp_print*/
1587 (getattrfunc)immutable_list_getattr, /*tp_getattr*/
1588 0, /*tp_setattr*/
1589 0, /*tp_compare*/ /* Won't be called */
1590 (reprfunc)list_repr, /*tp_repr*/
1591 0, /*tp_as_number*/
1592 &immutable_list_as_sequence, /*tp_as_sequence*/
1593 0, /*tp_as_mapping*/
Fred Drake56780252000-06-15 14:50:20 +00001594 0, /*tp_hash*/
1595 0, /*tp_call*/
1596 0, /*tp_str*/
1597 0, /*tp_getattro*/
1598 0, /*tp_setattro*/
1599 0, /*tp_as_buffer*/
Jeremy Hyltond08b4c42000-06-23 19:37:02 +00001600 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Jeremy Hylton8caad492000-06-23 14:18:11 +00001601 0, /* tp_doc */
1602 (traverseproc)list_traverse, /* tp_traverse */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001603};
Guido van Rossum42812581998-06-17 14:15:44 +00001604