blob: d260a88947cde8bb883c61a86a6defaf2ef0c4d4 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* List object implementation */
12
Guido van Rossumc0b618a1997-05-02 03:12:38 +000013#include "Python.h"
14
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000015#ifdef STDC_HEADERS
16#include <stddef.h>
17#else
18#include <sys/types.h> /* For size_t */
19#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020
Guido van Rossumc0b618a1997-05-02 03:12:38 +000021#define ROUNDUP(n, PyTryBlock) \
22 ((((n)+(PyTryBlock)-1)/(PyTryBlock))*(PyTryBlock))
Guido van Rossuma46d51d1995-01-26 22:59:43 +000023
24static int
Guido van Rossuma27d1121997-08-25 18:36:23 +000025roundupsize(n)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000026 int n;
27{
28 if (n < 500)
29 return ROUNDUP(n, 10);
30 else
31 return ROUNDUP(n, 100);
32}
33
Guido van Rossuma27d1121997-08-25 18:36:23 +000034#define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems))
Guido van Rossuma46d51d1995-01-26 22:59:43 +000035
Guido van Rossumc0b618a1997-05-02 03:12:38 +000036PyObject *
37PyList_New(size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038 int size;
39{
40 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000041 PyListObject *op;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000042 size_t nbytes;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043 if (size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000044 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045 return NULL;
46 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000047 nbytes = size * sizeof(PyObject *);
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000048 /* Check for overflow */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049 if (nbytes / sizeof(PyObject *) != (size_t)size) {
50 return PyErr_NoMemory();
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000051 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000052 /* PyObject_NewVar is inlined */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +000053 op = (PyListObject *) PyObject_MALLOC(sizeof(PyListObject)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000054 + PyGC_HEAD_SIZE);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000056 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000058 op = (PyListObject *) PyObject_FROM_GC(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059 if (size <= 0) {
60 op->ob_item = NULL;
61 }
62 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +000063 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +000065 PyObject_FREE(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 }
68 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000069 PyObject_INIT_VAR(op, &PyList_Type, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070 for (i = 0; i < size; i++)
71 op->ob_item[i] = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000072 PyObject_GC_Init(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000073 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074}
75
76int
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077PyList_Size(op)
78 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000080 if (!PyList_Check(op)) {
81 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082 return -1;
83 }
84 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085 return ((PyListObject *)op) -> ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086}
87
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088static PyObject *indexerr;
Guido van Rossum929f1b81996-08-09 20:51:27 +000089
Guido van Rossumc0b618a1997-05-02 03:12:38 +000090PyObject *
91PyList_GetItem(op, i)
92 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 int i;
94{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095 if (!PyList_Check(op)) {
96 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097 return NULL;
98 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000100 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000101 indexerr = PyString_FromString(
102 "list index out of range");
103 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104 return NULL;
105 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000106 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107}
108
109int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110PyList_SetItem(op, i, newitem)
111 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112 register int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113 register PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115 register PyObject *olditem;
116 register PyObject **p;
117 if (!PyList_Check(op)) {
118 Py_XDECREF(newitem);
119 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000120 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000122 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
123 Py_XDECREF(newitem);
124 PyErr_SetString(PyExc_IndexError,
125 "list assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000126 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000128 p = ((PyListObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000129 olditem = *p;
130 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000131 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132 return 0;
133}
134
135static int
136ins1(self, where, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000137 PyListObject *self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000139 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140{
141 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000142 PyObject **items;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000143 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000144 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000145 return -1;
146 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147 items = self->ob_item;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 NRESIZE(items, PyObject *, self->ob_size+1);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000149 if (items == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000151 return -1;
152 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 if (where < 0)
154 where = 0;
155 if (where > self->ob_size)
156 where = self->ob_size;
157 for (i = self->ob_size; --i >= where; )
158 items[i+1] = items[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160 items[where] = v;
161 self->ob_item = items;
162 self->ob_size++;
163 return 0;
164}
165
166int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000167PyList_Insert(op, where, newitem)
168 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000170 PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000172 if (!PyList_Check(op)) {
173 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000174 return -1;
175 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000176 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177}
178
179int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000180PyList_Append(op, newitem)
181 PyObject *op;
182 PyObject *newitem;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000183{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000184 if (!PyList_Check(op)) {
185 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000186 return -1;
187 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000188 return ins1((PyListObject *)op,
189 (int) ((PyListObject *)op)->ob_size, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190}
191
192/* Methods */
193
194static void
195list_dealloc(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 PyListObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197{
198 int i;
Guido van Rossumd724b232000-03-13 16:01:29 +0000199 Py_TRASHCAN_SAFE_BEGIN(op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000200 PyObject_GC_Fini(op);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000201 if (op->ob_item != NULL) {
Guido van Rossumfa717011999-06-09 15:19:34 +0000202 /* Do it backwards, for Christian Tismer.
203 There's a simple test case where somehow this reduces
204 thrashing when a *very* large list is created and
205 immediately deleted. */
206 i = op->ob_size;
207 while (--i >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 Py_XDECREF(op->ob_item[i]);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000209 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000210 PyMem_FREE(op->ob_item);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000211 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000212 op = (PyListObject *) PyObject_AS_GC(op);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000213 PyObject_DEL(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000214 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000215}
216
Guido van Rossum90933611991-06-07 16:10:43 +0000217static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218list_print(op, fp, flags)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000219 PyListObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220 FILE *fp;
221 int flags;
222{
223 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000224
225 i = Py_ReprEnter((PyObject*)op);
226 if (i != 0) {
227 if (i < 0)
228 return i;
229 fprintf(fp, "[...]");
230 return 0;
231 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232 fprintf(fp, "[");
Guido van Rossum90933611991-06-07 16:10:43 +0000233 for (i = 0; i < op->ob_size; i++) {
234 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235 fprintf(fp, ", ");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000236 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
237 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000238 return -1;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000239 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240 }
241 fprintf(fp, "]");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000242 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000243 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244}
245
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000246static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247list_repr(v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000248 PyListObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000250 PyObject *s, *comma;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000252
253 i = Py_ReprEnter((PyObject*)v);
254 if (i != 0) {
255 if (i > 0)
256 return PyString_FromString("[...]");
257 return NULL;
258 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000259 s = PyString_FromString("[");
260 comma = PyString_FromString(", ");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261 for (i = 0; i < v->ob_size && s != NULL; i++) {
262 if (i > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000263 PyString_Concat(&s, comma);
264 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000266 Py_XDECREF(comma);
267 PyString_ConcatAndDel(&s, PyString_FromString("]"));
Guido van Rossumfb376de1998-04-10 22:47:27 +0000268 Py_ReprLeave((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000269 return s;
270}
271
272static int
273list_compare(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000274 PyListObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276 int i;
Guido van Rossumae621ff1998-05-28 20:18:46 +0000277 for (i = 0; i < v->ob_size && i < w->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000278 int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279 if (cmp != 0)
280 return cmp;
281 }
282 return v->ob_size - w->ob_size;
283}
284
285static int
286list_length(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000287 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288{
289 return a->ob_size;
290}
291
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000292
293
294static int
295list_contains(a, el)
296 PyListObject *a;
297 PyObject *el;
298{
299 int i, cmp;
300
301 for (i = 0; i < a->ob_size; ++i) {
302 cmp = PyObject_Compare(el, PyList_GET_ITEM(a, i));
303 if (cmp == 0)
304 return 1;
305 if (PyErr_Occurred())
306 return -1;
307 }
308 return 0;
309}
310
311
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000312static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313list_item(a, i)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000314 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315 int i;
316{
317 if (i < 0 || i >= a->ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000318 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000319 indexerr = PyString_FromString(
320 "list index out of range");
321 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322 return NULL;
323 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000324 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 return a->ob_item[i];
326}
327
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000328static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329list_slice(a, ilow, ihigh)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000330 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331 int ilow, ihigh;
332{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000333 PyListObject *np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334 int i;
335 if (ilow < 0)
336 ilow = 0;
337 else if (ilow > a->ob_size)
338 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339 if (ihigh < ilow)
340 ihigh = ilow;
341 else if (ihigh > a->ob_size)
342 ihigh = a->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000343 np = (PyListObject *) PyList_New(ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344 if (np == NULL)
345 return NULL;
346 for (i = ilow; i < ihigh; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000347 PyObject *v = a->ob_item[i];
348 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349 np->ob_item[i - ilow] = v;
350 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000351 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352}
353
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000354PyObject *
355PyList_GetSlice(a, ilow, ihigh)
356 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000357 int ilow, ihigh;
358{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000359 if (!PyList_Check(a)) {
360 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000361 return NULL;
362 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000363 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000364}
365
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000366static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367list_concat(a, bb)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000368 PyListObject *a;
369 PyObject *bb;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370{
371 int size;
372 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000373 PyListObject *np;
374 if (!PyList_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000375 PyErr_Format(PyExc_TypeError,
Fred Drake914a2ed2000-06-01 14:31:03 +0000376 "can only concatenate list (not \"%.200s\") to list",
377 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378 return NULL;
379 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380#define b ((PyListObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381 size = a->ob_size + b->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000384 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385 }
386 for (i = 0; i < a->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000387 PyObject *v = a->ob_item[i];
388 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389 np->ob_item[i] = v;
390 }
391 for (i = 0; i < b->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000392 PyObject *v = b->ob_item[i];
393 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394 np->ob_item[i + a->ob_size] = v;
395 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397#undef b
398}
399
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +0000401list_repeat(a, n)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402 PyListObject *a;
Guido van Rossumed98d481991-03-06 13:07:53 +0000403 int n;
404{
405 int i, j;
406 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407 PyListObject *np;
408 PyObject **p;
Guido van Rossumed98d481991-03-06 13:07:53 +0000409 if (n < 0)
410 n = 0;
411 size = a->ob_size * n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000413 if (np == NULL)
414 return NULL;
415 p = np->ob_item;
416 for (i = 0; i < n; i++) {
417 for (j = 0; j < a->ob_size; j++) {
418 *p = a->ob_item[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000419 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000420 p++;
421 }
422 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000423 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000424}
425
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000427list_ass_slice(a, ilow, ihigh, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000428 PyListObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000429 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000430 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000431{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000432 /* Because [X]DECREF can recursively invoke list operations on
433 this list, we must postpone all [X]DECREF activity until
434 after the list is back in its canonical shape. Therefore
435 we must allocate an additional array, 'recycle', into which
436 we temporarily copy the items that are deleted from the
437 list. :-( */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438 PyObject **recycle, **p;
439 PyObject **item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440 int n; /* Size of replacement list */
441 int d; /* Change in size */
442 int k; /* Loop index */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444 if (v == NULL)
445 n = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446 else if (PyList_Check(v)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447 n = b->ob_size;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000448 if (a == b) {
449 /* Special case "a[i:j] = a" -- copy b first */
450 int ret;
451 v = list_slice(b, 0, n);
452 ret = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000453 Py_DECREF(v);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000454 return ret;
455 }
456 }
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000457 else {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000458 PyErr_Format(PyExc_TypeError,
459 "must assign list (not \"%.200s\") to slice",
460 v->ob_type->tp_name);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000461 return -1;
462 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463 if (ilow < 0)
464 ilow = 0;
465 else if (ilow > a->ob_size)
466 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000467 if (ihigh < ilow)
468 ihigh = ilow;
469 else if (ihigh > a->ob_size)
470 ihigh = a->ob_size;
471 item = a->ob_item;
472 d = n - (ihigh-ilow);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000473 if (ihigh > ilow)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000474 p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000475 else
476 p = recycle = NULL;
477 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478 for (k = ilow; k < ihigh; k++)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000479 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480 if (d < 0) {
481 for (/*k = ihigh*/; k < a->ob_size; k++)
482 item[k+d] = item[k];
483 a->ob_size += d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000484 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000485 a->ob_item = item;
486 }
487 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000488 else { /* Insert d items; recycle ihigh-ilow items */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000489 NRESIZE(item, PyObject *, a->ob_size + d);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000490 if (item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000491 if (recycle != NULL)
492 PyMem_DEL(recycle);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000493 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000494 return -1;
495 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496 for (k = a->ob_size; --k >= ihigh; )
497 item[k+d] = item[k];
498 for (/*k = ihigh-1*/; k >= ilow; --k)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000499 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000500 a->ob_item = item;
501 a->ob_size += d;
502 }
503 for (k = 0; k < n; k++, ilow++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000504 PyObject *w = b->ob_item[k];
505 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000506 item[ilow] = w;
507 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000508 if (recycle) {
509 while (--p >= recycle)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510 Py_XDECREF(*p);
511 PyMem_DEL(recycle);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000512 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000513 return 0;
514#undef b
515}
516
Guido van Rossum234f9421993-06-17 12:35:49 +0000517int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000518PyList_SetSlice(a, ilow, ihigh, v)
519 PyObject *a;
Guido van Rossum234f9421993-06-17 12:35:49 +0000520 int ilow, ihigh;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000521 PyObject *v;
Guido van Rossum234f9421993-06-17 12:35:49 +0000522{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523 if (!PyList_Check(a)) {
524 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000525 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000526 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000527 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000528}
529
Guido van Rossum4a450d01991-04-03 19:05:18 +0000530static int
531list_ass_item(a, i, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000532 PyListObject *a;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000533 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534 PyObject *v;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000535{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536 PyObject *old_value;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000537 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000538 PyErr_SetString(PyExc_IndexError,
539 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000540 return -1;
541 }
542 if (v == NULL)
543 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000544 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000545 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000546 a->ob_item[i] = v;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000548 return 0;
549}
550
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000551static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000552ins(self, where, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000553 PyListObject *self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554 int where;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000555 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000556{
557 if (ins1(self, where, v) != 0)
558 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000559 Py_INCREF(Py_None);
560 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000561}
562
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000563static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564listinsert(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000565 PyListObject *self;
566 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000567{
568 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000570 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571 return NULL;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000572 return ins(self, i, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573}
574
Guido van Rossumef93b872000-03-13 15:41:59 +0000575/* Define NO_STRICT_LIST_APPEND to enable multi-argument append() */
576
577#ifndef NO_STRICT_LIST_APPEND
578#define PyArg_ParseTuple_Compat1 PyArg_ParseTuple
579#else
580#define PyArg_ParseTuple_Compat1(args, format, ret) \
581( \
582 PyTuple_GET_SIZE(args) > 1 ? (*ret = args, 1) : \
583 PyTuple_GET_SIZE(args) == 1 ? (*ret = PyTuple_GET_ITEM(args, 0), 1) : \
584 PyArg_ParseTuple(args, format, ret) \
585)
586#endif
587
588
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590listappend(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000591 PyListObject *self;
592 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000593{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594 PyObject *v;
Guido van Rossumef93b872000-03-13 15:41:59 +0000595 if (!PyArg_ParseTuple_Compat1(args, "O:append", &v))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000596 return NULL;
597 return ins(self, (int) self->ob_size, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000598}
599
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000600static PyObject *
Barry Warsawdedf6d61998-10-09 16:37:25 +0000601listextend(self, args)
602 PyListObject *self;
603 PyObject *args;
604{
605 PyObject *b = NULL, *res = NULL;
606 PyObject **items;
607 int selflen = PyList_GET_SIZE(self);
608 int blen;
609 register int i;
610
Guido van Rossumc00a9382000-02-24 21:48:29 +0000611 if (!PyArg_ParseTuple(args, "O:extend", &b))
Barry Warsawdedf6d61998-10-09 16:37:25 +0000612 return NULL;
613
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000614 b = PySequence_Fast(b, "list.extend() argument must be a sequence");
615 if (!b)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000616 return NULL;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000617
618 if (PyObject_Length(b) == 0)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000619 /* short circuit when b is empty */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000620 goto ok;
621
Barry Warsawdedf6d61998-10-09 16:37:25 +0000622 if (self == (PyListObject*)b) {
623 /* as in list_ass_slice() we must special case the
624 * situation: a.extend(a)
625 *
626 * XXX: I think this way ought to be faster than using
627 * list_slice() the way list_ass_slice() does.
628 */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000629 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000630 b = PyList_New(selflen);
631 if (!b)
632 return NULL;
633 for (i = 0; i < selflen; i++) {
634 PyObject *o = PyList_GET_ITEM(self, i);
635 Py_INCREF(o);
636 PyList_SET_ITEM(b, i, o);
637 }
638 }
Barry Warsawdedf6d61998-10-09 16:37:25 +0000639
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000640 blen = PyObject_Length(b);
641
Barry Warsawdedf6d61998-10-09 16:37:25 +0000642 /* resize a using idiom */
643 items = self->ob_item;
644 NRESIZE(items, PyObject*, selflen + blen);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000645 if (items == NULL) {
Barry Warsawdedf6d61998-10-09 16:37:25 +0000646 PyErr_NoMemory();
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000647 goto failed;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000648 }
649 self->ob_item = items;
650
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000651 /* populate the end of self with b's items */
Barry Warsawdedf6d61998-10-09 16:37:25 +0000652 for (i = 0; i < blen; i++) {
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000653 PyObject *o = PySequence_Fast_GET_ITEM(b, i);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000654 Py_INCREF(o);
655 PyList_SET_ITEM(self, self->ob_size++, o);
656 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000657 ok:
Barry Warsawdedf6d61998-10-09 16:37:25 +0000658 res = Py_None;
659 Py_INCREF(res);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000660 failed:
661 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000662 return res;
663}
664
665
666static PyObject *
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000667listpop(self, args)
668 PyListObject *self;
669 PyObject *args;
670{
671 int i = -1;
672 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000673 if (!PyArg_ParseTuple(args, "|i:pop", &i))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000674 return NULL;
675 if (self->ob_size == 0) {
676 /* Special-case most common failure cause */
677 PyErr_SetString(PyExc_IndexError, "pop from empty list");
678 return NULL;
679 }
680 if (i < 0)
681 i += self->ob_size;
682 if (i < 0 || i >= self->ob_size) {
683 PyErr_SetString(PyExc_IndexError, "pop index out of range");
684 return NULL;
685 }
686 v = self->ob_item[i];
687 Py_INCREF(v);
688 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
689 Py_DECREF(v);
690 return NULL;
691 }
692 return v;
693}
694
Guido van Rossum3f236de1996-12-10 23:55:39 +0000695/* New quicksort implementation for arrays of object pointers.
696 Thanks to discussions with Tim Peters. */
697
698/* CMPERROR is returned by our comparison function when an error
699 occurred. This is the largest negative integer (0x80000000 on a
700 32-bit system). */
Guido van Rossum19700b61997-03-05 00:45:43 +0000701#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
Guido van Rossum3f236de1996-12-10 23:55:39 +0000702
703/* Comparison function. Takes care of calling a user-supplied
704 comparison function (any callable Python object). Calls the
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000705 standard comparison function, PyObject_Compare(), if the user-
706 supplied function is NULL. */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000707
708static int
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000709docompare(x, y, compare)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710 PyObject *x;
711 PyObject *y;
712 PyObject *compare;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000713{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714 PyObject *args, *res;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000715 int i;
716
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000717 if (compare == NULL) {
718 i = PyObject_Compare(x, y);
719 if (i && PyErr_Occurred())
720 i = CMPERROR;
721 return i;
722 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000723
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000724 args = Py_BuildValue("(OO)", x, y);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000725 if (args == NULL)
726 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000727 res = PyEval_CallObject(compare, args);
728 Py_DECREF(args);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000729 if (res == NULL)
730 return CMPERROR;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731 if (!PyInt_Check(res)) {
732 Py_DECREF(res);
733 PyErr_SetString(PyExc_TypeError,
Guido van Rossum9bcd1d71999-04-19 17:44:39 +0000734 "comparison function must return int");
Guido van Rossum3f236de1996-12-10 23:55:39 +0000735 return CMPERROR;
736 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000737 i = PyInt_AsLong(res);
738 Py_DECREF(res);
Guido van Rossum3f236de1996-12-10 23:55:39 +0000739 if (i < 0)
740 return -1;
741 if (i > 0)
742 return 1;
743 return 0;
744}
745
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000746/* MINSIZE is the smallest array that will get a full-blown samplesort
747 treatment; smaller arrays are sorted using binary insertion. It must
748 be at least 7 for the samplesort implementation to work. Binary
749 insertion does fewer compares, but can suffer O(N**2) data movement.
750 The more expensive compares, the larger MINSIZE should be. */
751#define MINSIZE 100
752
753/* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
754 partition; smaller slices are passed to binarysort. It must be at
755 least 2, and no larger than MINSIZE. Setting it higher reduces the #
756 of compares slowly, but increases the amount of data movement quickly.
757 The value here was chosen assuming a compare costs ~25x more than
758 swapping a pair of memory-resident pointers -- but under that assumption,
759 changing the value by a few dozen more or less has aggregate effect
760 under 1%. So the value is crucial, but not touchy <wink>. */
761#define MINPARTITIONSIZE 40
762
763/* MAXMERGE is the largest number of elements we'll always merge into
764 a known-to-be sorted chunk via binary insertion, regardless of the
765 size of that chunk. Given a chunk of N sorted elements, and a group
766 of K unknowns, the largest K for which it's better to do insertion
767 (than a full-blown sort) is a complicated function of N and K mostly
768 involving the expected number of compares and data moves under each
769 approach, and the relative cost of those operations on a specific
770 architecure. The fixed value here is conservative, and should be a
771 clear win regardless of architecture or N. */
772#define MAXMERGE 15
Guido van Rossum3f236de1996-12-10 23:55:39 +0000773
Guido van Rossum3f236de1996-12-10 23:55:39 +0000774/* STACKSIZE is the size of our work stack. A rough estimate is that
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000775 this allows us to sort arrays of size N where
776 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
777 for arrays of size 2**64. Because we push the biggest partition
778 first, the worst case occurs when all subarrays are always partitioned
779 exactly in two. */
780#define STACKSIZE 60
Guido van Rossum3f236de1996-12-10 23:55:39 +0000781
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000782
783#define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
784
785/* binarysort is the best method for sorting small arrays: it does
786 few compares, but can do data movement quadratic in the number of
787 elements.
Guido van Rossum42812581998-06-17 14:15:44 +0000788 [lo, hi) is a contiguous slice of a list, and is sorted via
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000789 binary insertion.
790 On entry, must have lo <= start <= hi, and that [lo, start) is already
791 sorted (pass start == lo if you don't know!).
792 If docompare complains (returns CMPERROR) return -1, else 0.
793 Even in case of error, the output slice will be some permutation of
794 the input (nothing is lost or duplicated).
795*/
Guido van Rossum3f236de1996-12-10 23:55:39 +0000796
797static int
Guido van Rossum42812581998-06-17 14:15:44 +0000798binarysort(lo, hi, start, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000799 PyObject **lo;
800 PyObject **hi;
801 PyObject **start;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000802 PyObject *compare;/* Comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000803{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000804 /* assert lo <= start <= hi
805 assert [lo, start) is sorted */
806 register int k;
807 register PyObject **l, **p, **r;
808 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000809
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000810 if (lo == start)
811 ++start;
812 for (; start < hi; ++start) {
813 /* set l to where *start belongs */
814 l = lo;
815 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000816 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000817 do {
818 p = l + ((r - l) >> 1);
819 SETK(pivot, *p);
820 if (k < 0)
821 r = p;
822 else
823 l = p + 1;
824 } while (l < r);
825 /* Pivot should go at l -- slide over to make room.
826 Caution: using memmove is much slower under MSVC 5;
827 we're not usually moving many slots. */
828 for (p = start; p > l; --p)
829 *p = *(p-1);
830 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000831 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000832 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000833
834 fail:
835 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000836}
837
838/* samplesortslice is the sorting workhorse.
Guido van Rossum42812581998-06-17 14:15:44 +0000839 [lo, hi) is a contiguous slice of a list, to be sorted in place.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000840 On entry, must have lo <= hi,
841 If docompare complains (returns CMPERROR) return -1, else 0.
842 Even in case of error, the output slice will be some permutation of
843 the input (nothing is lost or duplicated).
844
845 samplesort is basically quicksort on steroids: a power of 2 close
846 to n/ln(n) is computed, and that many elements (less 1) are picked at
847 random from the array and sorted. These 2**k - 1 elements are then
848 used as preselected pivots for an equal number of quicksort
849 partitioning steps, partitioning the slice into 2**k chunks each of
850 size about ln(n). These small final chunks are then usually handled
851 by binarysort. Note that when k=1, this is roughly the same as an
852 ordinary quicksort using a random pivot, and when k=2 this is roughly
853 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
854 this a "median of n/ln(n)" quicksort. You can also view it as a kind
855 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
856
857 The large number of samples makes a quadratic-time case almost
858 impossible, and asymptotically drives the average-case number of
859 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
860 3 variant) down to N lg N.
861
862 We also play lots of low-level tricks to cut the number of compares.
863
864 Very obscure: To avoid using extra memory, the PPs are stored in the
865 array and shuffled around as partitioning proceeds. At the start of a
866 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
867 adjacent (either on the left or the right!) to a chunk of X elements
868 that are to be partitioned: P X or X P. In either case we need to
869 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
870 left, followed by the PP to be used for this step (that's the middle
871 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
872 P X or X P -> Psmall pivot X Plarge
873 and the order of the PPs must not be altered. It can take a while
874 to realize this isn't trivial! It can take even longer <wink> to
875 understand why the simple code below works, using only 2**(m-1) swaps.
876 The key is that the order of the X elements isn't necessarily
877 preserved: X can end up as some cyclic permutation of its original
878 order. That's OK, because X is unsorted anyway. If the order of X
879 had to be preserved too, the simplest method I know of using O(1)
880 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
881 Since len(X) is typically several times larger than 2**(m-1), that
882 would slow things down.
883*/
884
885struct SamplesortStackNode {
886 /* Represents a slice of the array, from (& including) lo up
887 to (but excluding) hi. "extra" additional & adjacent elements
888 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
889 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
890 already sorted, but nothing is known about the other elements
891 in [lo, hi). |extra| is always one less than a power of 2.
892 When extra is 0, we're out of PPs, and the slice must be
893 sorted by some other means. */
894 PyObject **lo;
895 PyObject **hi;
896 int extra;
897};
898
899/* 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 +0000900 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000901 is undesirable, so cutoff values are canned in the "cutoff" table
902 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
903#define CUTOFFBASE 4
Guido van Rossum3aa23fd1999-01-14 19:01:53 +0000904static long cutoff[] = {
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000905 43, /* smallest N such that k == 4 */
906 106, /* etc */
907 250,
908 576,
909 1298,
910 2885,
911 6339,
912 13805,
913 29843,
914 64116,
915 137030,
916 291554,
917 617916,
918 1305130,
919 2748295,
920 5771662,
921 12091672,
922 25276798,
923 52734615,
924 109820537,
925 228324027,
926 473977813,
927 982548444, /* smallest N such that k == 26 */
928 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
929};
930
931static int
Guido van Rossum42812581998-06-17 14:15:44 +0000932samplesortslice(lo, hi, compare)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000933 PyObject **lo;
934 PyObject **hi;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000935 PyObject *compare;/* Comparison function object, or NULL for default */
936{
937 register PyObject **l, **r;
938 register PyObject *tmp, *pivot;
939 register int k;
940 int n, extra, top, extraOnRight;
941 struct SamplesortStackNode stack[STACKSIZE];
942
943 /* assert lo <= hi */
944 n = hi - lo;
945
946 /* ----------------------------------------------------------
947 * Special cases
948 * --------------------------------------------------------*/
949 if (n < 2)
950 return 0;
951
952 /* Set r to the largest value such that [lo,r) is sorted.
953 This catches the already-sorted case, the all-the-same
954 case, and the appended-a-few-elements-to-a-sorted-list case.
955 If the array is unsorted, we're very likely to get out of
956 the loop fast, so the test is cheap if it doesn't pay off.
957 */
958 /* assert lo < hi */
959 for (r = lo+1; r < hi; ++r) {
960 SETK(*r, *(r-1));
961 if (k < 0)
962 break;
963 }
964 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
965 few unknowns, or few elements in total. */
966 if (hi - r <= MAXMERGE || n < MINSIZE)
Guido van Rossum42812581998-06-17 14:15:44 +0000967 return binarysort(lo, hi, r, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000968
969 /* Check for the array already being reverse-sorted. Typical
970 benchmark-driven silliness <wink>. */
971 /* assert lo < hi */
972 for (r = lo+1; r < hi; ++r) {
973 SETK(*(r-1), *r);
974 if (k < 0)
975 break;
976 }
977 if (hi - r <= MAXMERGE) {
978 /* Reverse the reversed prefix, then insert the tail */
979 PyObject **originalr = r;
980 l = lo;
981 do {
982 --r;
983 tmp = *l; *l = *r; *r = tmp;
984 ++l;
985 } while (l < r);
Guido van Rossum42812581998-06-17 14:15:44 +0000986 return binarysort(lo, hi, originalr, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000987 }
988
989 /* ----------------------------------------------------------
990 * Normal case setup: a large array without obvious pattern.
991 * --------------------------------------------------------*/
992
993 /* extra := a power of 2 ~= n/ln(n), less 1.
994 First find the smallest extra s.t. n < cutoff[extra] */
995 for (extra = 0;
996 extra < sizeof(cutoff) / sizeof(cutoff[0]);
997 ++extra) {
998 if (n < cutoff[extra])
999 break;
1000 /* note that if we fall out of the loop, the value of
1001 extra still makes *sense*, but may be smaller than
1002 we would like (but the array has more than ~= 2**31
1003 elements in this case!) */
1004 }
1005 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1006 have is CUTOFFBASE-1, so
1007 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1008 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
1009 /* assert extra > 0 and n >= extra */
1010
1011 /* Swap that many values to the start of the array. The
1012 selection of elements is pseudo-random, but the same on
1013 every run (this is intentional! timing algorithm changes is
1014 a pain if timing varies across runs). */
1015 {
1016 unsigned int seed = n / extra; /* arbitrary */
1017 unsigned int i;
1018 for (i = 0; i < (unsigned)extra; ++i) {
1019 /* j := random int in [i, n) */
1020 unsigned int j;
1021 seed = seed * 69069 + 7;
1022 j = i + seed % (n - i);
1023 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
1024 }
1025 }
1026
1027 /* Recursively sort the preselected pivots. */
Guido van Rossum42812581998-06-17 14:15:44 +00001028 if (samplesortslice(lo, lo + extra, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001029 goto fail;
1030
1031 top = 0; /* index of available stack slot */
1032 lo += extra; /* point to first unknown */
1033 extraOnRight = 0; /* the PPs are at the left end */
1034
1035 /* ----------------------------------------------------------
1036 * Partition [lo, hi), and repeat until out of work.
1037 * --------------------------------------------------------*/
1038 for (;;) {
1039 /* assert lo <= hi, so n >= 0 */
1040 n = hi - lo;
1041
1042 /* We may not want, or may not be able, to partition:
1043 If n is small, it's quicker to insert.
1044 If extra is 0, we're out of pivots, and *must* use
1045 another method.
1046 */
1047 if (n < MINPARTITIONSIZE || extra == 0) {
1048 if (n >= MINSIZE) {
1049 /* assert extra == 0
1050 This is rare, since the average size
1051 of a final block is only about
1052 ln(original n). */
Guido van Rossum42812581998-06-17 14:15:44 +00001053 if (samplesortslice(lo, hi, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001054 goto fail;
1055 }
1056 else {
1057 /* Binary insertion should be quicker,
1058 and we can take advantage of the PPs
1059 already being sorted. */
1060 if (extraOnRight && extra) {
1061 /* swap the PPs to the left end */
1062 k = extra;
1063 do {
1064 tmp = *lo;
1065 *lo = *hi;
1066 *hi = tmp;
1067 ++lo; ++hi;
1068 } while (--k);
1069 }
1070 if (binarysort(lo - extra, hi, lo,
Guido van Rossum42812581998-06-17 14:15:44 +00001071 compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001072 goto fail;
1073 }
1074
1075 /* Find another slice to work on. */
1076 if (--top < 0)
1077 break; /* no more -- done! */
1078 lo = stack[top].lo;
1079 hi = stack[top].hi;
1080 extra = stack[top].extra;
1081 extraOnRight = 0;
1082 if (extra < 0) {
1083 extraOnRight = 1;
1084 extra = -extra;
1085 }
1086 continue;
1087 }
1088
1089 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1090 Then our preselected pivot is at (extra-1)/2, and we
1091 want to move the PPs before that to the left end of
1092 the slice, and the PPs after that to the right end.
1093 The following section changes extra, lo, hi, and the
1094 slice such that:
1095 [lo-extra, lo) contains the smaller PPs.
1096 *lo == our PP.
1097 (lo, hi) contains the unknown elements.
1098 [hi, hi+extra) contains the larger PPs.
1099 */
1100 k = extra >>= 1; /* num PPs to move */
1101 if (extraOnRight) {
1102 /* Swap the smaller PPs to the left end.
1103 Note that this loop actually moves k+1 items:
1104 the last is our PP */
1105 do {
1106 tmp = *lo; *lo = *hi; *hi = tmp;
1107 ++lo; ++hi;
1108 } while (k--);
1109 }
1110 else {
1111 /* Swap the larger PPs to the right end. */
1112 while (k--) {
1113 --lo; --hi;
1114 tmp = *lo; *lo = *hi; *hi = tmp;
1115 }
1116 }
1117 --lo; /* *lo is now our PP */
1118 pivot = *lo;
1119
1120 /* Now an almost-ordinary quicksort partition step.
1121 Note that most of the time is spent here!
1122 Only odd thing is that we partition into < and >=,
1123 instead of the usual <= and >=. This helps when
1124 there are lots of duplicates of different values,
1125 because it eventually tends to make subfiles
1126 "pure" (all duplicates), and we special-case for
1127 duplicates later. */
1128 l = lo + 1;
1129 r = hi - 1;
1130 /* assert lo < l < r < hi (small n weeded out above) */
1131
1132 do {
1133 /* slide l right, looking for key >= pivot */
1134 do {
1135 SETK(*l, pivot);
1136 if (k < 0)
1137 ++l;
1138 else
1139 break;
1140 } while (l < r);
1141
1142 /* slide r left, looking for key < pivot */
1143 while (l < r) {
Guido van Rossum42812581998-06-17 14:15:44 +00001144 register PyObject *rval = *r--;
1145 SETK(rval, pivot);
1146 if (k < 0) {
1147 /* swap and advance */
1148 r[1] = *l;
1149 *l++ = rval;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001150 break;
Guido van Rossum42812581998-06-17 14:15:44 +00001151 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001152 }
1153
1154 } while (l < r);
1155
1156 /* assert lo < r <= l < hi
1157 assert r == l or r+1 == l
1158 everything to the left of l is < pivot, and
1159 everything to the right of r is >= pivot */
1160
1161 if (l == r) {
1162 SETK(*r, pivot);
1163 if (k < 0)
1164 ++l;
1165 else
1166 --r;
1167 }
1168 /* assert lo <= r and r+1 == l and l <= hi
1169 assert r == lo or a[r] < pivot
1170 assert a[lo] is pivot
1171 assert l == hi or a[l] >= pivot
1172 Swap the pivot into "the middle", so we can henceforth
1173 ignore it.
1174 */
1175 *lo = *r;
1176 *r = pivot;
1177
1178 /* The following is true now, & will be preserved:
1179 All in [lo,r) are < pivot
1180 All in [r,l) == pivot (& so can be ignored)
1181 All in [l,hi) are >= pivot */
1182
1183 /* Check for duplicates of the pivot. One compare is
1184 wasted if there are no duplicates, but can win big
1185 when there are.
1186 Tricky: we're sticking to "<" compares, so deduce
1187 equality indirectly. We know pivot <= *l, so they're
1188 equal iff not pivot < *l.
1189 */
1190 while (l < hi) {
1191 /* pivot <= *l known */
1192 SETK(pivot, *l);
1193 if (k < 0)
1194 break;
1195 else
1196 /* <= and not < implies == */
1197 ++l;
1198 }
1199
1200 /* assert lo <= r < l <= hi
1201 Partitions are [lo, r) and [l, hi) */
1202
1203 /* push fattest first; remember we still have extra PPs
1204 to the left of the left chunk and to the right of
1205 the right chunk! */
1206 /* assert top < STACKSIZE */
1207 if (r - lo <= hi - l) {
1208 /* second is bigger */
1209 stack[top].lo = l;
1210 stack[top].hi = hi;
1211 stack[top].extra = -extra;
1212 hi = r;
1213 extraOnRight = 0;
1214 }
1215 else {
1216 /* first is bigger */
1217 stack[top].lo = lo;
1218 stack[top].hi = r;
1219 stack[top].extra = extra;
1220 lo = l;
1221 extraOnRight = 1;
1222 }
1223 ++top;
1224
1225 } /* end of partitioning loop */
1226
1227 return 0;
1228
1229 fail:
1230 return -1;
1231}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001232
1233#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001234
1235staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001236
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001237static PyObject *
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001238listsort(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001239 PyListObject *self;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001240 PyObject *args;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001241{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001242 int err;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001243 PyObject *compare = NULL;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001244
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001245 if (args != NULL) {
Guido van Rossumc00a9382000-02-24 21:48:29 +00001246 if (!PyArg_ParseTuple(args, "|O:sort", &compare))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001247 return NULL;
1248 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001249 self->ob_type = &immutable_list_type;
1250 err = samplesortslice(self->ob_item,
1251 self->ob_item + self->ob_size,
Guido van Rossum42812581998-06-17 14:15:44 +00001252 compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001253 self->ob_type = &PyList_Type;
1254 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001255 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001256 Py_INCREF(Py_None);
1257 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001258}
1259
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001260int
1261PyList_Sort(v)
1262 PyObject *v;
1263{
1264 if (v == NULL || !PyList_Check(v)) {
1265 PyErr_BadInternalCall();
1266 return -1;
1267 }
1268 v = listsort((PyListObject *)v, (PyObject *)NULL);
1269 if (v == NULL)
1270 return -1;
1271 Py_DECREF(v);
1272 return 0;
1273}
1274
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001275static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001276listreverse(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001277 PyListObject *self;
1278 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001279{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001280 register PyObject **p, **q;
1281 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001282
Guido van Rossumc00a9382000-02-24 21:48:29 +00001283 if (!PyArg_ParseTuple(args, ":reverse"))
Guido van Rossumed98d481991-03-06 13:07:53 +00001284 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001285
1286 if (self->ob_size > 1) {
1287 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
1288 p < q; p++, q--) {
1289 tmp = *p;
1290 *p = *q;
1291 *q = tmp;
1292 }
1293 }
1294
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001295 Py_INCREF(Py_None);
1296 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001297}
1298
Guido van Rossum84c76f51990-10-30 13:32:20 +00001299int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001300PyList_Reverse(v)
1301 PyObject *v;
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001302{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001303 if (v == NULL || !PyList_Check(v)) {
1304 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001305 return -1;
1306 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001307 v = listreverse((PyListObject *)v, (PyObject *)NULL);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001308 if (v == NULL)
1309 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001310 Py_DECREF(v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001311 return 0;
1312}
1313
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001314PyObject *
1315PyList_AsTuple(v)
1316 PyObject *v;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001317{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001318 PyObject *w;
1319 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001320 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001321 if (v == NULL || !PyList_Check(v)) {
1322 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001323 return NULL;
1324 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001325 n = ((PyListObject *)v)->ob_size;
1326 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001327 if (w == NULL)
1328 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001329 p = ((PyTupleObject *)w)->ob_item;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001330 memcpy((ANY *)p,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001331 (ANY *)((PyListObject *)v)->ob_item,
1332 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001333 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001334 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001335 p++;
1336 }
1337 return w;
1338}
1339
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001340static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001341listindex(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001342 PyListObject *self;
1343 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001344{
1345 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001346 PyObject *v;
1347
Guido van Rossumef93b872000-03-13 15:41:59 +00001348 if (!PyArg_ParseTuple_Compat1(args, "O:index", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001349 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001350 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001351 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001352 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001353 if (PyErr_Occurred())
1354 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001355 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001356 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001357 return NULL;
1358}
1359
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001360static PyObject *
Guido van Rossume6f7d181991-10-20 20:20:40 +00001361listcount(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001362 PyListObject *self;
1363 PyObject *args;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001364{
1365 int count = 0;
1366 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001367 PyObject *v;
1368
Guido van Rossumef93b872000-03-13 15:41:59 +00001369 if (!PyArg_ParseTuple_Compat1(args, "O:count", &v))
Guido van Rossume6f7d181991-10-20 20:20:40 +00001370 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001371 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001372 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001373 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001374 if (PyErr_Occurred())
1375 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001376 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001377 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001378}
1379
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001380static PyObject *
Guido van Rossumed98d481991-03-06 13:07:53 +00001381listremove(self, args)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001382 PyListObject *self;
1383 PyObject *args;
Guido van Rossumed98d481991-03-06 13:07:53 +00001384{
1385 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001386 PyObject *v;
1387
Guido van Rossumef93b872000-03-13 15:41:59 +00001388 if (!PyArg_ParseTuple_Compat1(args, "O:remove", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001389 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001390 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001391 if (PyObject_Compare(self->ob_item[i], v) == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001392 if (list_ass_slice(self, i, i+1,
1393 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001394 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001395 Py_INCREF(Py_None);
1396 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001397 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001398 if (PyErr_Occurred())
1399 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001400 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001401 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001402 return NULL;
1403}
1404
Jeremy Hylton8caad492000-06-23 14:18:11 +00001405static int
1406list_traverse(PyListObject *o, visitproc visit, void *arg)
1407{
1408 int i, err;
1409 PyObject *x;
1410
1411 for (i = o->ob_size; --i >= 0; ) {
1412 x = o->ob_item[i];
1413 if (x != NULL) {
1414 err = visit(x, arg);
1415 if (err)
1416 return err;
1417 }
1418 }
1419 return 0;
1420}
1421
1422static int
1423list_clear(PyListObject *lp)
1424{
1425 (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0);
1426 return 0;
1427}
1428
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001429static char append_doc[] =
1430"L.append(object) -- append object to end";
Barry Warsawdedf6d61998-10-09 16:37:25 +00001431static char extend_doc[] =
1432"L.extend(list) -- extend list by appending list elements";
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001433static char insert_doc[] =
1434"L.insert(index, object) -- insert object before index";
1435static char pop_doc[] =
1436"L.pop([index]) -> item -- remove and return item at index (default last)";
1437static char remove_doc[] =
1438"L.remove(value) -- remove first occurrence of value";
1439static char index_doc[] =
1440"L.index(value) -> integer -- return index of first occurrence of value";
1441static char count_doc[] =
1442"L.count(value) -> integer -- return number of occurrences of value";
1443static char reverse_doc[] =
1444"L.reverse() -- reverse *IN PLACE*";
1445static char sort_doc[] =
1446"L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1447
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001448static PyMethodDef list_methods[] = {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001449 {"append", (PyCFunction)listappend, 1, append_doc},
1450 {"insert", (PyCFunction)listinsert, 1, insert_doc},
Barry Warsawdedf6d61998-10-09 16:37:25 +00001451 {"extend", (PyCFunction)listextend, 1, extend_doc},
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001452 {"pop", (PyCFunction)listpop, 1, pop_doc},
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001453 {"remove", (PyCFunction)listremove, 1, remove_doc},
1454 {"index", (PyCFunction)listindex, 1, index_doc},
1455 {"count", (PyCFunction)listcount, 1, count_doc},
1456 {"reverse", (PyCFunction)listreverse, 1, reverse_doc},
1457 {"sort", (PyCFunction)listsort, 1, sort_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001458 {NULL, NULL} /* sentinel */
1459};
1460
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001461static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001462list_getattr(f, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001463 PyListObject *f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001464 char *name;
1465{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001466 return Py_FindMethod(list_methods, (PyObject *)f, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001467}
1468
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001469static PySequenceMethods list_as_sequence = {
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001470 (inquiry)list_length, /*sq_length*/
1471 (binaryfunc)list_concat, /*sq_concat*/
1472 (intargfunc)list_repeat, /*sq_repeat*/
1473 (intargfunc)list_item, /*sq_item*/
1474 (intintargfunc)list_slice, /*sq_slice*/
1475 (intobjargproc)list_ass_item, /*sq_ass_item*/
1476 (intintobjargproc)list_ass_slice, /*sq_ass_slice*/
Jeremy Hylton37b1a262000-04-27 21:41:03 +00001477 (objobjproc)list_contains, /*sq_contains*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001478};
1479
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001480PyTypeObject PyList_Type = {
1481 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001482 0,
1483 "list",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001484 sizeof(PyListObject) + PyGC_HEAD_SIZE,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001485 0,
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001486 (destructor)list_dealloc, /*tp_dealloc*/
1487 (printfunc)list_print, /*tp_print*/
1488 (getattrfunc)list_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001489 0, /*tp_setattr*/
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001490 (cmpfunc)list_compare, /*tp_compare*/
1491 (reprfunc)list_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001492 0, /*tp_as_number*/
1493 &list_as_sequence, /*tp_as_sequence*/
1494 0, /*tp_as_mapping*/
Fred Drake56780252000-06-15 14:50:20 +00001495 0, /*tp_hash*/
1496 0, /*tp_call*/
1497 0, /*tp_str*/
1498 0, /*tp_getattro*/
1499 0, /*tp_setattro*/
1500 0, /*tp_as_buffer*/
Jeremy Hyltond08b4c42000-06-23 19:37:02 +00001501 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Jeremy Hylton8caad492000-06-23 14:18:11 +00001502 0, /* tp_doc */
1503 (traverseproc)list_traverse, /* tp_traverse */
1504 (inquiry)list_clear, /* tp_clear */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001505};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001506
1507
1508/* During a sort, we really can't have anyone modifying the list; it could
1509 cause core dumps. Thus, we substitute a dummy type that raises an
1510 explanatory exception when a modifying operation is used. Caveat:
1511 comparisons may behave differently; but I guess it's a bad idea anyway to
1512 compare a list that's being sorted... */
1513
1514static PyObject *
1515immutable_list_op(/*No args!*/)
1516{
1517 PyErr_SetString(PyExc_TypeError,
1518 "a list cannot be modified while it is being sorted");
1519 return NULL;
1520}
1521
1522static PyMethodDef immutable_list_methods[] = {
1523 {"append", (PyCFunction)immutable_list_op},
1524 {"insert", (PyCFunction)immutable_list_op},
1525 {"remove", (PyCFunction)immutable_list_op},
1526 {"index", (PyCFunction)listindex},
1527 {"count", (PyCFunction)listcount},
1528 {"reverse", (PyCFunction)immutable_list_op},
1529 {"sort", (PyCFunction)immutable_list_op},
1530 {NULL, NULL} /* sentinel */
1531};
1532
1533static PyObject *
1534immutable_list_getattr(f, name)
1535 PyListObject *f;
1536 char *name;
1537{
1538 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
1539}
1540
1541static int
1542immutable_list_ass(/*No args!*/)
1543{
1544 immutable_list_op();
1545 return -1;
1546}
1547
1548static PySequenceMethods immutable_list_as_sequence = {
1549 (inquiry)list_length, /*sq_length*/
1550 (binaryfunc)list_concat, /*sq_concat*/
1551 (intargfunc)list_repeat, /*sq_repeat*/
1552 (intargfunc)list_item, /*sq_item*/
1553 (intintargfunc)list_slice, /*sq_slice*/
1554 (intobjargproc)immutable_list_ass, /*sq_ass_item*/
1555 (intintobjargproc)immutable_list_ass, /*sq_ass_slice*/
Fred Drake56780252000-06-15 14:50:20 +00001556 (objobjproc)list_contains, /*sq_contains*/
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001557};
1558
1559static PyTypeObject immutable_list_type = {
1560 PyObject_HEAD_INIT(&PyType_Type)
1561 0,
1562 "list (immutable, during sort)",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001563 sizeof(PyListObject) + PyGC_HEAD_SIZE,
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001564 0,
1565 0, /*tp_dealloc*/ /* Cannot happen */
1566 (printfunc)list_print, /*tp_print*/
1567 (getattrfunc)immutable_list_getattr, /*tp_getattr*/
1568 0, /*tp_setattr*/
1569 0, /*tp_compare*/ /* Won't be called */
1570 (reprfunc)list_repr, /*tp_repr*/
1571 0, /*tp_as_number*/
1572 &immutable_list_as_sequence, /*tp_as_sequence*/
1573 0, /*tp_as_mapping*/
Fred Drake56780252000-06-15 14:50:20 +00001574 0, /*tp_hash*/
1575 0, /*tp_call*/
1576 0, /*tp_str*/
1577 0, /*tp_getattro*/
1578 0, /*tp_setattro*/
1579 0, /*tp_as_buffer*/
Jeremy Hyltond08b4c42000-06-23 19:37:02 +00001580 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Jeremy Hylton8caad492000-06-23 14:18:11 +00001581 0, /* tp_doc */
1582 (traverseproc)list_traverse, /* tp_traverse */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001583};
Guido van Rossum42812581998-06-17 14:15:44 +00001584