blob: 5a704fedf3f49e4374ef742ea1de47630229a255 [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
Jack Jansene9791602000-08-22 21:51:22 +000020#ifdef HAVE_LIMITS_H
21#include <limits.h>
22#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000023
Guido van Rossumc0b618a1997-05-02 03:12:38 +000024#define ROUNDUP(n, PyTryBlock) \
25 ((((n)+(PyTryBlock)-1)/(PyTryBlock))*(PyTryBlock))
Guido van Rossuma46d51d1995-01-26 22:59:43 +000026
27static int
Fred Drakea2f55112000-07-09 15:16:51 +000028roundupsize(int n)
Guido van Rossuma46d51d1995-01-26 22:59:43 +000029{
30 if (n < 500)
31 return ROUNDUP(n, 10);
32 else
33 return ROUNDUP(n, 100);
34}
35
Guido van Rossuma27d1121997-08-25 18:36:23 +000036#define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems))
Guido van Rossuma46d51d1995-01-26 22:59:43 +000037
Guido van Rossumc0b618a1997-05-02 03:12:38 +000038PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000039PyList_New(int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040{
41 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000042 PyListObject *op;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +000043 size_t nbytes;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044 if (size < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046 return NULL;
47 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048 nbytes = size * sizeof(PyObject *);
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000049 /* Check for overflow */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050 if (nbytes / sizeof(PyObject *) != (size_t)size) {
51 return PyErr_NoMemory();
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000052 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000053 /* PyObject_NewVar is inlined */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +000054 op = (PyListObject *) PyObject_MALLOC(sizeof(PyListObject)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000055 + PyGC_HEAD_SIZE);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000059 op = (PyListObject *) PyObject_FROM_GC(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060 if (size <= 0) {
61 op->ob_item = NULL;
62 }
63 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +000064 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065 if (op->ob_item == NULL) {
Vladimir Marangozov467a67e2000-07-15 03:31:31 +000066 PyObject_FREE(PyObject_AS_GC(op));
Guido van Rossumc0b618a1997-05-02 03:12:38 +000067 return PyErr_NoMemory();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068 }
69 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000070 PyObject_INIT_VAR(op, &PyList_Type, size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071 for (i = 0; i < size; i++)
72 op->ob_item[i] = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000073 PyObject_GC_Init(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075}
76
77int
Fred Drakea2f55112000-07-09 15:16:51 +000078PyList_Size(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 *
Fred Drakea2f55112000-07-09 15:16:51 +000091PyList_GetItem(PyObject *op, int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000093 if (!PyList_Check(op)) {
94 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095 return NULL;
96 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +000098 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099 indexerr = PyString_FromString(
100 "list index out of range");
101 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102 return NULL;
103 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000104 return ((PyListObject *)op) -> ob_item[i];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105}
106
107int
Fred Drakea2f55112000-07-09 15:16:51 +0000108PyList_SetItem(register PyObject *op, register int i,
109 register PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000111 register PyObject *olditem;
112 register PyObject **p;
113 if (!PyList_Check(op)) {
114 Py_XDECREF(newitem);
115 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000116 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000118 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
119 Py_XDECREF(newitem);
120 PyErr_SetString(PyExc_IndexError,
121 "list assignment index out of range");
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000122 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000124 p = ((PyListObject *)op) -> ob_item + i;
Guido van Rossum5fe60581995-03-09 12:12:50 +0000125 olditem = *p;
126 *p = newitem;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000127 Py_XDECREF(olditem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128 return 0;
129}
130
131static int
Fred Drakea2f55112000-07-09 15:16:51 +0000132ins1(PyListObject *self, int where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133{
134 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000135 PyObject **items;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000136 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000137 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000138 return -1;
139 }
Trent Micka5846642000-08-13 22:47:45 +0000140 if (self->ob_size == INT_MAX) {
141 PyErr_SetString(PyExc_OverflowError,
142 "cannot add more objects to list");
143 return -1;
144 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145 items = self->ob_item;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000146 NRESIZE(items, PyObject *, self->ob_size+1);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000147 if (items == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000149 return -1;
150 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151 if (where < 0)
152 where = 0;
153 if (where > self->ob_size)
154 where = self->ob_size;
155 for (i = self->ob_size; --i >= where; )
156 items[i+1] = items[i];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158 items[where] = v;
159 self->ob_item = items;
160 self->ob_size++;
161 return 0;
162}
163
164int
Fred Drakea2f55112000-07-09 15:16:51 +0000165PyList_Insert(PyObject *op, int where, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000167 if (!PyList_Check(op)) {
168 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000169 return -1;
170 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000171 return ins1((PyListObject *)op, where, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172}
173
174int
Fred Drakea2f55112000-07-09 15:16:51 +0000175PyList_Append(PyObject *op, PyObject *newitem)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000177 if (!PyList_Check(op)) {
178 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000179 return -1;
180 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000181 return ins1((PyListObject *)op,
182 (int) ((PyListObject *)op)->ob_size, newitem);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000183}
184
185/* Methods */
186
187static void
Fred Drakea2f55112000-07-09 15:16:51 +0000188list_dealloc(PyListObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189{
190 int i;
Guido van Rossumd724b232000-03-13 16:01:29 +0000191 Py_TRASHCAN_SAFE_BEGIN(op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000192 PyObject_GC_Fini(op);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000193 if (op->ob_item != NULL) {
Guido van Rossumfa717011999-06-09 15:19:34 +0000194 /* Do it backwards, for Christian Tismer.
195 There's a simple test case where somehow this reduces
196 thrashing when a *very* large list is created and
197 immediately deleted. */
198 i = op->ob_size;
199 while (--i >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000200 Py_XDECREF(op->ob_item[i]);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000201 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000202 PyMem_FREE(op->ob_item);
Jack Jansen7874d1f1995-01-19 12:09:27 +0000203 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000204 op = (PyListObject *) PyObject_AS_GC(op);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000205 PyObject_DEL(op);
Guido van Rossumd724b232000-03-13 16:01:29 +0000206 Py_TRASHCAN_SAFE_END(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207}
208
Guido van Rossum90933611991-06-07 16:10:43 +0000209static int
Fred Drakea2f55112000-07-09 15:16:51 +0000210list_print(PyListObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211{
212 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000213
214 i = Py_ReprEnter((PyObject*)op);
215 if (i != 0) {
216 if (i < 0)
217 return i;
218 fprintf(fp, "[...]");
219 return 0;
220 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221 fprintf(fp, "[");
Guido van Rossum90933611991-06-07 16:10:43 +0000222 for (i = 0; i < op->ob_size; i++) {
223 if (i > 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224 fprintf(fp, ", ");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000225 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
226 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000227 return -1;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000228 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229 }
230 fprintf(fp, "]");
Guido van Rossumfb376de1998-04-10 22:47:27 +0000231 Py_ReprLeave((PyObject *)op);
Guido van Rossum90933611991-06-07 16:10:43 +0000232 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233}
234
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000236list_repr(PyListObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238 PyObject *s, *comma;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239 int i;
Guido van Rossumfb376de1998-04-10 22:47:27 +0000240
241 i = Py_ReprEnter((PyObject*)v);
242 if (i != 0) {
243 if (i > 0)
244 return PyString_FromString("[...]");
245 return NULL;
246 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000247 s = PyString_FromString("[");
248 comma = PyString_FromString(", ");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249 for (i = 0; i < v->ob_size && s != NULL; i++) {
250 if (i > 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000251 PyString_Concat(&s, comma);
252 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000254 Py_XDECREF(comma);
255 PyString_ConcatAndDel(&s, PyString_FromString("]"));
Guido van Rossumfb376de1998-04-10 22:47:27 +0000256 Py_ReprLeave((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257 return s;
258}
259
260static int
Fred Drakea2f55112000-07-09 15:16:51 +0000261list_compare(PyListObject *v, PyListObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263 int i;
Jack Jansene9791602000-08-22 21:51:22 +0000264
Guido van Rossumae621ff1998-05-28 20:18:46 +0000265 for (i = 0; i < v->ob_size && i < w->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000266 int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267 if (cmp != 0)
268 return cmp;
269 }
270 return v->ob_size - w->ob_size;
271}
272
273static int
Fred Drakea2f55112000-07-09 15:16:51 +0000274list_length(PyListObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275{
276 return a->ob_size;
277}
278
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000279
280
281static int
Fred Drakea2f55112000-07-09 15:16:51 +0000282list_contains(PyListObject *a, PyObject *el)
Jeremy Hylton37b1a262000-04-27 21:41:03 +0000283{
284 int i, cmp;
285
286 for (i = 0; i < a->ob_size; ++i) {
287 cmp = PyObject_Compare(el, PyList_GET_ITEM(a, i));
288 if (cmp == 0)
289 return 1;
290 if (PyErr_Occurred())
291 return -1;
292 }
293 return 0;
294}
295
296
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000297static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000298list_item(PyListObject *a, int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299{
300 if (i < 0 || i >= a->ob_size) {
Guido van Rossum929f1b81996-08-09 20:51:27 +0000301 if (indexerr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000302 indexerr = PyString_FromString(
303 "list index out of range");
304 PyErr_SetObject(PyExc_IndexError, indexerr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305 return NULL;
306 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000307 Py_INCREF(a->ob_item[i]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308 return a->ob_item[i];
309}
310
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000312list_slice(PyListObject *a, int ilow, int ihigh)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000314 PyListObject *np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315 int i;
316 if (ilow < 0)
317 ilow = 0;
318 else if (ilow > a->ob_size)
319 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320 if (ihigh < ilow)
321 ihigh = ilow;
322 else if (ihigh > a->ob_size)
323 ihigh = a->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000324 np = (PyListObject *) PyList_New(ihigh - ilow);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 if (np == NULL)
326 return NULL;
327 for (i = ilow; i < ihigh; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000328 PyObject *v = a->ob_item[i];
329 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330 np->ob_item[i - ilow] = v;
331 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000332 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333}
334
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000335PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000336PyList_GetSlice(PyObject *a, int ilow, int ihigh)
Guido van Rossum234f9421993-06-17 12:35:49 +0000337{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000338 if (!PyList_Check(a)) {
339 PyErr_BadInternalCall();
Guido van Rossum234f9421993-06-17 12:35:49 +0000340 return NULL;
341 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000342 return list_slice((PyListObject *)a, ilow, ihigh);
Guido van Rossum234f9421993-06-17 12:35:49 +0000343}
344
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000345static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000346list_concat(PyListObject *a, PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347{
348 int size;
349 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000350 PyListObject *np;
351 if (!PyList_Check(bb)) {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000352 PyErr_Format(PyExc_TypeError,
Fred Drake914a2ed2000-06-01 14:31:03 +0000353 "can only concatenate list (not \"%.200s\") to list",
354 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355 return NULL;
356 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000357#define b ((PyListObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358 size = a->ob_size + b->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000359 np = (PyListObject *) PyList_New(size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360 if (np == NULL) {
Guido van Rossum90933611991-06-07 16:10:43 +0000361 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362 }
363 for (i = 0; i < a->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364 PyObject *v = a->ob_item[i];
365 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366 np->ob_item[i] = v;
367 }
368 for (i = 0; i < b->ob_size; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369 PyObject *v = b->ob_item[i];
370 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371 np->ob_item[i + a->ob_size] = v;
372 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000373 return (PyObject *)np;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374#undef b
375}
376
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000377static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000378list_repeat(PyListObject *a, int n)
Guido van Rossumed98d481991-03-06 13:07:53 +0000379{
380 int i, j;
381 int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382 PyListObject *np;
383 PyObject **p;
Guido van Rossumed98d481991-03-06 13:07:53 +0000384 if (n < 0)
385 n = 0;
386 size = a->ob_size * n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000387 np = (PyListObject *) PyList_New(size);
Guido van Rossumed98d481991-03-06 13:07:53 +0000388 if (np == NULL)
389 return NULL;
390 p = np->ob_item;
391 for (i = 0; i < n; i++) {
392 for (j = 0; j < a->ob_size; j++) {
393 *p = a->ob_item[j];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000394 Py_INCREF(*p);
Guido van Rossumed98d481991-03-06 13:07:53 +0000395 p++;
396 }
397 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000398 return (PyObject *) np;
Guido van Rossumed98d481991-03-06 13:07:53 +0000399}
400
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401static int
Fred Drakea2f55112000-07-09 15:16:51 +0000402list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403{
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000404 /* Because [X]DECREF can recursively invoke list operations on
405 this list, we must postpone all [X]DECREF activity until
406 after the list is back in its canonical shape. Therefore
407 we must allocate an additional array, 'recycle', into which
408 we temporarily copy the items that are deleted from the
409 list. :-( */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410 PyObject **recycle, **p;
411 PyObject **item;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412 int n; /* Size of replacement list */
413 int d; /* Change in size */
414 int k; /* Loop index */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000415#define b ((PyListObject *)v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416 if (v == NULL)
417 n = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000418 else if (PyList_Check(v)) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419 n = b->ob_size;
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000420 if (a == b) {
421 /* Special case "a[i:j] = a" -- copy b first */
422 int ret;
423 v = list_slice(b, 0, n);
424 ret = list_ass_slice(a, ilow, ihigh, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425 Py_DECREF(v);
Guido van Rossum32dffaa1991-12-24 13:27:34 +0000426 return ret;
427 }
428 }
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000429 else {
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000430 PyErr_Format(PyExc_TypeError,
431 "must assign list (not \"%.200s\") to slice",
432 v->ob_type->tp_name);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000433 return -1;
434 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435 if (ilow < 0)
436 ilow = 0;
437 else if (ilow > a->ob_size)
438 ilow = a->ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439 if (ihigh < ilow)
440 ihigh = ilow;
441 else if (ihigh > a->ob_size)
442 ihigh = a->ob_size;
443 item = a->ob_item;
444 d = n - (ihigh-ilow);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000445 if (ihigh > ilow)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446 p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000447 else
448 p = recycle = NULL;
449 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450 for (k = ilow; k < ihigh; k++)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000451 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000452 if (d < 0) {
453 for (/*k = ihigh*/; k < a->ob_size; k++)
454 item[k+d] = item[k];
455 a->ob_size += d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457 a->ob_item = item;
458 }
459 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000460 else { /* Insert d items; recycle ihigh-ilow items */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461 NRESIZE(item, PyObject *, a->ob_size + d);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000462 if (item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000463 if (recycle != NULL)
464 PyMem_DEL(recycle);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000466 return -1;
467 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468 for (k = a->ob_size; --k >= ihigh; )
469 item[k+d] = item[k];
470 for (/*k = ihigh-1*/; k >= ilow; --k)
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000471 *p++ = item[k];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472 a->ob_item = item;
473 a->ob_size += d;
474 }
475 for (k = 0; k < n; k++, ilow++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000476 PyObject *w = b->ob_item[k];
477 Py_XINCREF(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478 item[ilow] = w;
479 }
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000480 if (recycle) {
481 while (--p >= recycle)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000482 Py_XDECREF(*p);
483 PyMem_DEL(recycle);
Guido van Rossumae7bf1a1995-01-17 10:21:11 +0000484 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000485 return 0;
486#undef b
487}
488
Guido van Rossum234f9421993-06-17 12:35:49 +0000489int
Fred Drakea2f55112000-07-09 15:16:51 +0000490PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v)
Guido van Rossum234f9421993-06-17 12:35:49 +0000491{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000492 if (!PyList_Check(a)) {
493 PyErr_BadInternalCall();
Guido van Rossum1fc238a1993-07-29 08:25:09 +0000494 return -1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000495 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
Guido van Rossum234f9421993-06-17 12:35:49 +0000497}
498
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000499static PyObject *
500list_inplace_repeat(PyListObject *self, int n)
501{
502 PyObject **items;
503 int size, i, j;
504
505
506 size = PyList_GET_SIZE(self);
507 if (size == 0) {
508 Py_INCREF(self);
509 return (PyObject *)self;
510 }
511
512 items = self->ob_item;
513
514 if (n < 1) {
515 self->ob_item = NULL;
516 self->ob_size = 0;
517 for (i = 0; i < size; i++)
518 Py_XDECREF(items[i]);
519 PyMem_DEL(items);
520 Py_INCREF(self);
521 return (PyObject *)self;
522 }
523
524 NRESIZE(items, PyObject*, size*n);
525 if (items == NULL) {
526 PyErr_NoMemory();
527 goto finally;
528 }
529 self->ob_item = items;
530 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
531 for (j = 0; j < size; j++) {
532 PyObject *o = PyList_GET_ITEM(self, j);
533 Py_INCREF(o);
534 PyList_SET_ITEM(self, self->ob_size++, o);
535 }
536 }
537 Py_INCREF(self);
538 return (PyObject *)self;
539 finally:
540 return NULL;
541}
542
Guido van Rossum4a450d01991-04-03 19:05:18 +0000543static int
Fred Drakea2f55112000-07-09 15:16:51 +0000544list_ass_item(PyListObject *a, int i, PyObject *v)
Guido van Rossum4a450d01991-04-03 19:05:18 +0000545{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000546 PyObject *old_value;
Guido van Rossum4a450d01991-04-03 19:05:18 +0000547 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000548 PyErr_SetString(PyExc_IndexError,
549 "list assignment index out of range");
Guido van Rossum4a450d01991-04-03 19:05:18 +0000550 return -1;
551 }
552 if (v == NULL)
553 return list_ass_slice(a, i, i+1, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 Py_INCREF(v);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000555 old_value = a->ob_item[i];
Guido van Rossum4a450d01991-04-03 19:05:18 +0000556 a->ob_item[i] = v;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000557 Py_DECREF(old_value);
Guido van Rossum4a450d01991-04-03 19:05:18 +0000558 return 0;
559}
560
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000561static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000562ins(PyListObject *self, int where, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563{
564 if (ins1(self, where, v) != 0)
565 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000566 Py_INCREF(Py_None);
567 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568}
569
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000570static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000571listinsert(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572{
573 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000574 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000575 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576 return NULL;
Guido van Rossumbf80e541993-02-08 15:49:17 +0000577 return ins(self, i, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578}
579
Guido van Rossumef93b872000-03-13 15:41:59 +0000580/* Define NO_STRICT_LIST_APPEND to enable multi-argument append() */
581
582#ifndef NO_STRICT_LIST_APPEND
583#define PyArg_ParseTuple_Compat1 PyArg_ParseTuple
584#else
585#define PyArg_ParseTuple_Compat1(args, format, ret) \
586( \
587 PyTuple_GET_SIZE(args) > 1 ? (*ret = args, 1) : \
588 PyTuple_GET_SIZE(args) == 1 ? (*ret = PyTuple_GET_ITEM(args, 0), 1) : \
589 PyArg_ParseTuple(args, format, ret) \
590)
591#endif
592
593
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000595listappend(PyListObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000596{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000597 PyObject *v;
Guido van Rossumef93b872000-03-13 15:41:59 +0000598 if (!PyArg_ParseTuple_Compat1(args, "O:append", &v))
Guido van Rossumbf80e541993-02-08 15:49:17 +0000599 return NULL;
600 return ins(self, (int) self->ob_size, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000601}
602
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000603static int
604listextend_internal(PyListObject *self, PyObject *b)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000605{
Barry Warsawdedf6d61998-10-09 16:37:25 +0000606 PyObject **items;
607 int selflen = PyList_GET_SIZE(self);
608 int blen;
609 register int i;
610
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000611 if (PyObject_Size(b) == 0)
Barry Warsawdedf6d61998-10-09 16:37:25 +0000612 /* short circuit when b is empty */
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000613 return 0;
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000614
Barry Warsawdedf6d61998-10-09 16:37:25 +0000615 if (self == (PyListObject*)b) {
616 /* as in list_ass_slice() we must special case the
617 * situation: a.extend(a)
618 *
619 * XXX: I think this way ought to be faster than using
620 * list_slice() the way list_ass_slice() does.
621 */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000622 Py_DECREF(b);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000623 b = PyList_New(selflen);
624 if (!b)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000625 return -1;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000626 for (i = 0; i < selflen; i++) {
627 PyObject *o = PyList_GET_ITEM(self, i);
628 Py_INCREF(o);
629 PyList_SET_ITEM(b, i, o);
630 }
631 }
Barry Warsawdedf6d61998-10-09 16:37:25 +0000632
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000633 blen = PyObject_Size(b);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000634
Barry Warsawdedf6d61998-10-09 16:37:25 +0000635 /* resize a using idiom */
636 items = self->ob_item;
637 NRESIZE(items, PyObject*, selflen + blen);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000638 if (items == NULL) {
Barry Warsawdedf6d61998-10-09 16:37:25 +0000639 PyErr_NoMemory();
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000640 Py_DECREF(b);
641 return -1;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000642 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000643
Barry Warsawdedf6d61998-10-09 16:37:25 +0000644 self->ob_item = items;
645
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000646 /* populate the end of self with b's items */
Barry Warsawdedf6d61998-10-09 16:37:25 +0000647 for (i = 0; i < blen; i++) {
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000648 PyObject *o = PySequence_Fast_GET_ITEM(b, i);
Barry Warsawdedf6d61998-10-09 16:37:25 +0000649 Py_INCREF(o);
650 PyList_SET_ITEM(self, self->ob_size++, o);
651 }
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000652 Py_DECREF(b);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000653 return 0;
Barry Warsawdedf6d61998-10-09 16:37:25 +0000654}
655
656
657static PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000658list_inplace_concat(PyListObject *self, PyObject *other)
659{
660 other = PySequence_Fast(other, "argument to += must be a sequence");
661 if (!other)
662 return NULL;
663
664 if (listextend_internal(self, other) < 0)
665 return NULL;
666
667 Py_INCREF(self);
668 return (PyObject *)self;
669}
670
671static PyObject *
672listextend(PyListObject *self, PyObject *args)
673{
674
675 PyObject *b;
676
677 if (!PyArg_ParseTuple(args, "O:extend", &b))
678 return NULL;
679
680 b = PySequence_Fast(b, "list.extend() argument must be a sequence");
681 if (!b)
682 return NULL;
683
684 if (listextend_internal(self, b) < 0)
685 return NULL;
686
687 Py_INCREF(Py_None);
688 return Py_None;
689}
690
691static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000692listpop(PyListObject *self, PyObject *args)
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000693{
694 int i = -1;
695 PyObject *v;
Guido van Rossumc00a9382000-02-24 21:48:29 +0000696 if (!PyArg_ParseTuple(args, "|i:pop", &i))
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +0000697 return NULL;
698 if (self->ob_size == 0) {
699 /* Special-case most common failure cause */
700 PyErr_SetString(PyExc_IndexError, "pop from empty list");
701 return NULL;
702 }
703 if (i < 0)
704 i += self->ob_size;
705 if (i < 0 || i >= self->ob_size) {
706 PyErr_SetString(PyExc_IndexError, "pop index out of range");
707 return NULL;
708 }
709 v = self->ob_item[i];
710 Py_INCREF(v);
711 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
712 Py_DECREF(v);
713 return NULL;
714 }
715 return v;
716}
717
Guido van Rossum3f236de1996-12-10 23:55:39 +0000718/* New quicksort implementation for arrays of object pointers.
719 Thanks to discussions with Tim Peters. */
720
721/* CMPERROR is returned by our comparison function when an error
722 occurred. This is the largest negative integer (0x80000000 on a
723 32-bit system). */
Guido van Rossum19700b61997-03-05 00:45:43 +0000724#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
Guido van Rossum3f236de1996-12-10 23:55:39 +0000725
726/* Comparison function. Takes care of calling a user-supplied
727 comparison function (any callable Python object). Calls the
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000728 standard comparison function, PyObject_Compare(), if the user-
729 supplied function is NULL. */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000730
731static int
Fred Drakea2f55112000-07-09 15:16:51 +0000732docompare(PyObject *x, PyObject *y, 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
Fred Drakea2f55112000-07-09 15:16:51 +0000818binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
819 /* compare -- comparison function object, or NULL for default */
Guido van Rossum3f236de1996-12-10 23:55:39 +0000820{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000821 /* assert lo <= start <= hi
822 assert [lo, start) is sorted */
823 register int k;
824 register PyObject **l, **p, **r;
825 register PyObject *pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000826
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000827 if (lo == start)
828 ++start;
829 for (; start < hi; ++start) {
830 /* set l to where *start belongs */
831 l = lo;
832 r = start;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000833 pivot = *r;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000834 do {
835 p = l + ((r - l) >> 1);
836 SETK(pivot, *p);
837 if (k < 0)
838 r = p;
839 else
840 l = p + 1;
841 } while (l < r);
842 /* Pivot should go at l -- slide over to make room.
843 Caution: using memmove is much slower under MSVC 5;
844 we're not usually moving many slots. */
845 for (p = start; p > l; --p)
846 *p = *(p-1);
847 *l = pivot;
Guido van Rossum3f236de1996-12-10 23:55:39 +0000848 }
Guido van Rossum3f236de1996-12-10 23:55:39 +0000849 return 0;
Guido van Rossuma119c0d1998-05-29 17:56:32 +0000850
851 fail:
852 return -1;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000853}
854
855/* samplesortslice is the sorting workhorse.
Guido van Rossum42812581998-06-17 14:15:44 +0000856 [lo, hi) is a contiguous slice of a list, to be sorted in place.
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000857 On entry, must have lo <= hi,
858 If docompare complains (returns CMPERROR) return -1, else 0.
859 Even in case of error, the output slice will be some permutation of
860 the input (nothing is lost or duplicated).
861
862 samplesort is basically quicksort on steroids: a power of 2 close
863 to n/ln(n) is computed, and that many elements (less 1) are picked at
864 random from the array and sorted. These 2**k - 1 elements are then
865 used as preselected pivots for an equal number of quicksort
866 partitioning steps, partitioning the slice into 2**k chunks each of
867 size about ln(n). These small final chunks are then usually handled
868 by binarysort. Note that when k=1, this is roughly the same as an
869 ordinary quicksort using a random pivot, and when k=2 this is roughly
870 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
871 this a "median of n/ln(n)" quicksort. You can also view it as a kind
872 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
873
874 The large number of samples makes a quadratic-time case almost
875 impossible, and asymptotically drives the average-case number of
876 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
877 3 variant) down to N lg N.
878
879 We also play lots of low-level tricks to cut the number of compares.
880
881 Very obscure: To avoid using extra memory, the PPs are stored in the
882 array and shuffled around as partitioning proceeds. At the start of a
883 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
884 adjacent (either on the left or the right!) to a chunk of X elements
885 that are to be partitioned: P X or X P. In either case we need to
886 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
887 left, followed by the PP to be used for this step (that's the middle
888 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
889 P X or X P -> Psmall pivot X Plarge
890 and the order of the PPs must not be altered. It can take a while
891 to realize this isn't trivial! It can take even longer <wink> to
892 understand why the simple code below works, using only 2**(m-1) swaps.
893 The key is that the order of the X elements isn't necessarily
894 preserved: X can end up as some cyclic permutation of its original
895 order. That's OK, because X is unsorted anyway. If the order of X
896 had to be preserved too, the simplest method I know of using O(1)
897 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
898 Since len(X) is typically several times larger than 2**(m-1), that
899 would slow things down.
900*/
901
902struct SamplesortStackNode {
903 /* Represents a slice of the array, from (& including) lo up
904 to (but excluding) hi. "extra" additional & adjacent elements
905 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
906 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
907 already sorted, but nothing is known about the other elements
908 in [lo, hi). |extra| is always one less than a power of 2.
909 When extra is 0, we're out of PPs, and the slice must be
910 sorted by some other means. */
911 PyObject **lo;
912 PyObject **hi;
913 int extra;
914};
915
916/* 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 +0000917 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000918 is undesirable, so cutoff values are canned in the "cutoff" table
919 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
920#define CUTOFFBASE 4
Guido van Rossum3aa23fd1999-01-14 19:01:53 +0000921static long cutoff[] = {
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000922 43, /* smallest N such that k == 4 */
923 106, /* etc */
924 250,
925 576,
926 1298,
927 2885,
928 6339,
929 13805,
930 29843,
931 64116,
932 137030,
933 291554,
934 617916,
935 1305130,
936 2748295,
937 5771662,
938 12091672,
939 25276798,
940 52734615,
941 109820537,
942 228324027,
943 473977813,
944 982548444, /* smallest N such that k == 26 */
945 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
946};
947
948static int
Fred Drakea2f55112000-07-09 15:16:51 +0000949samplesortslice(PyObject **lo, PyObject **hi, PyObject *compare)
950 /* compare -- comparison function object, or NULL for default */
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000951{
952 register PyObject **l, **r;
953 register PyObject *tmp, *pivot;
954 register int k;
955 int n, extra, top, extraOnRight;
956 struct SamplesortStackNode stack[STACKSIZE];
957
958 /* assert lo <= hi */
959 n = hi - lo;
960
961 /* ----------------------------------------------------------
962 * Special cases
963 * --------------------------------------------------------*/
964 if (n < 2)
965 return 0;
966
967 /* Set r to the largest value such that [lo,r) is sorted.
968 This catches the already-sorted case, the all-the-same
969 case, and the appended-a-few-elements-to-a-sorted-list case.
970 If the array is unsorted, we're very likely to get out of
971 the loop fast, so the test is cheap if it doesn't pay off.
972 */
973 /* assert lo < hi */
974 for (r = lo+1; r < hi; ++r) {
975 SETK(*r, *(r-1));
976 if (k < 0)
977 break;
978 }
979 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
980 few unknowns, or few elements in total. */
981 if (hi - r <= MAXMERGE || n < MINSIZE)
Guido van Rossum42812581998-06-17 14:15:44 +0000982 return binarysort(lo, hi, r, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +0000983
984 /* Check for the array already being reverse-sorted. Typical
985 benchmark-driven silliness <wink>. */
986 /* assert lo < hi */
987 for (r = lo+1; r < hi; ++r) {
988 SETK(*(r-1), *r);
989 if (k < 0)
990 break;
991 }
992 if (hi - r <= MAXMERGE) {
993 /* Reverse the reversed prefix, then insert the tail */
994 PyObject **originalr = r;
995 l = lo;
996 do {
997 --r;
998 tmp = *l; *l = *r; *r = tmp;
999 ++l;
1000 } while (l < r);
Guido van Rossum42812581998-06-17 14:15:44 +00001001 return binarysort(lo, hi, originalr, compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001002 }
1003
1004 /* ----------------------------------------------------------
1005 * Normal case setup: a large array without obvious pattern.
1006 * --------------------------------------------------------*/
1007
1008 /* extra := a power of 2 ~= n/ln(n), less 1.
1009 First find the smallest extra s.t. n < cutoff[extra] */
1010 for (extra = 0;
1011 extra < sizeof(cutoff) / sizeof(cutoff[0]);
1012 ++extra) {
1013 if (n < cutoff[extra])
1014 break;
1015 /* note that if we fall out of the loop, the value of
1016 extra still makes *sense*, but may be smaller than
1017 we would like (but the array has more than ~= 2**31
1018 elements in this case!) */
1019 }
1020 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
1021 have is CUTOFFBASE-1, so
1022 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
1023 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
1024 /* assert extra > 0 and n >= extra */
1025
1026 /* Swap that many values to the start of the array. The
1027 selection of elements is pseudo-random, but the same on
1028 every run (this is intentional! timing algorithm changes is
1029 a pain if timing varies across runs). */
1030 {
1031 unsigned int seed = n / extra; /* arbitrary */
1032 unsigned int i;
1033 for (i = 0; i < (unsigned)extra; ++i) {
1034 /* j := random int in [i, n) */
1035 unsigned int j;
1036 seed = seed * 69069 + 7;
1037 j = i + seed % (n - i);
1038 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
1039 }
1040 }
1041
1042 /* Recursively sort the preselected pivots. */
Guido van Rossum42812581998-06-17 14:15:44 +00001043 if (samplesortslice(lo, lo + extra, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001044 goto fail;
1045
1046 top = 0; /* index of available stack slot */
1047 lo += extra; /* point to first unknown */
1048 extraOnRight = 0; /* the PPs are at the left end */
1049
1050 /* ----------------------------------------------------------
1051 * Partition [lo, hi), and repeat until out of work.
1052 * --------------------------------------------------------*/
1053 for (;;) {
1054 /* assert lo <= hi, so n >= 0 */
1055 n = hi - lo;
1056
1057 /* We may not want, or may not be able, to partition:
1058 If n is small, it's quicker to insert.
1059 If extra is 0, we're out of pivots, and *must* use
1060 another method.
1061 */
1062 if (n < MINPARTITIONSIZE || extra == 0) {
1063 if (n >= MINSIZE) {
1064 /* assert extra == 0
1065 This is rare, since the average size
1066 of a final block is only about
1067 ln(original n). */
Guido van Rossum42812581998-06-17 14:15:44 +00001068 if (samplesortslice(lo, hi, compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001069 goto fail;
1070 }
1071 else {
1072 /* Binary insertion should be quicker,
1073 and we can take advantage of the PPs
1074 already being sorted. */
1075 if (extraOnRight && extra) {
1076 /* swap the PPs to the left end */
1077 k = extra;
1078 do {
1079 tmp = *lo;
1080 *lo = *hi;
1081 *hi = tmp;
1082 ++lo; ++hi;
1083 } while (--k);
1084 }
1085 if (binarysort(lo - extra, hi, lo,
Guido van Rossum42812581998-06-17 14:15:44 +00001086 compare) < 0)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001087 goto fail;
1088 }
1089
1090 /* Find another slice to work on. */
1091 if (--top < 0)
1092 break; /* no more -- done! */
1093 lo = stack[top].lo;
1094 hi = stack[top].hi;
1095 extra = stack[top].extra;
1096 extraOnRight = 0;
1097 if (extra < 0) {
1098 extraOnRight = 1;
1099 extra = -extra;
1100 }
1101 continue;
1102 }
1103
1104 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
1105 Then our preselected pivot is at (extra-1)/2, and we
1106 want to move the PPs before that to the left end of
1107 the slice, and the PPs after that to the right end.
1108 The following section changes extra, lo, hi, and the
1109 slice such that:
1110 [lo-extra, lo) contains the smaller PPs.
1111 *lo == our PP.
1112 (lo, hi) contains the unknown elements.
1113 [hi, hi+extra) contains the larger PPs.
1114 */
1115 k = extra >>= 1; /* num PPs to move */
1116 if (extraOnRight) {
1117 /* Swap the smaller PPs to the left end.
1118 Note that this loop actually moves k+1 items:
1119 the last is our PP */
1120 do {
1121 tmp = *lo; *lo = *hi; *hi = tmp;
1122 ++lo; ++hi;
1123 } while (k--);
1124 }
1125 else {
1126 /* Swap the larger PPs to the right end. */
1127 while (k--) {
1128 --lo; --hi;
1129 tmp = *lo; *lo = *hi; *hi = tmp;
1130 }
1131 }
1132 --lo; /* *lo is now our PP */
1133 pivot = *lo;
1134
1135 /* Now an almost-ordinary quicksort partition step.
1136 Note that most of the time is spent here!
1137 Only odd thing is that we partition into < and >=,
1138 instead of the usual <= and >=. This helps when
1139 there are lots of duplicates of different values,
1140 because it eventually tends to make subfiles
1141 "pure" (all duplicates), and we special-case for
1142 duplicates later. */
1143 l = lo + 1;
1144 r = hi - 1;
1145 /* assert lo < l < r < hi (small n weeded out above) */
1146
1147 do {
1148 /* slide l right, looking for key >= pivot */
1149 do {
1150 SETK(*l, pivot);
1151 if (k < 0)
1152 ++l;
1153 else
1154 break;
1155 } while (l < r);
1156
1157 /* slide r left, looking for key < pivot */
1158 while (l < r) {
Guido van Rossum42812581998-06-17 14:15:44 +00001159 register PyObject *rval = *r--;
1160 SETK(rval, pivot);
1161 if (k < 0) {
1162 /* swap and advance */
1163 r[1] = *l;
1164 *l++ = rval;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001165 break;
Guido van Rossum42812581998-06-17 14:15:44 +00001166 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001167 }
1168
1169 } while (l < r);
1170
1171 /* assert lo < r <= l < hi
1172 assert r == l or r+1 == l
1173 everything to the left of l is < pivot, and
1174 everything to the right of r is >= pivot */
1175
1176 if (l == r) {
1177 SETK(*r, pivot);
1178 if (k < 0)
1179 ++l;
1180 else
1181 --r;
1182 }
1183 /* assert lo <= r and r+1 == l and l <= hi
1184 assert r == lo or a[r] < pivot
1185 assert a[lo] is pivot
1186 assert l == hi or a[l] >= pivot
1187 Swap the pivot into "the middle", so we can henceforth
1188 ignore it.
1189 */
1190 *lo = *r;
1191 *r = pivot;
1192
1193 /* The following is true now, & will be preserved:
1194 All in [lo,r) are < pivot
1195 All in [r,l) == pivot (& so can be ignored)
1196 All in [l,hi) are >= pivot */
1197
1198 /* Check for duplicates of the pivot. One compare is
1199 wasted if there are no duplicates, but can win big
1200 when there are.
1201 Tricky: we're sticking to "<" compares, so deduce
1202 equality indirectly. We know pivot <= *l, so they're
1203 equal iff not pivot < *l.
1204 */
1205 while (l < hi) {
1206 /* pivot <= *l known */
1207 SETK(pivot, *l);
1208 if (k < 0)
1209 break;
1210 else
1211 /* <= and not < implies == */
1212 ++l;
1213 }
1214
1215 /* assert lo <= r < l <= hi
1216 Partitions are [lo, r) and [l, hi) */
1217
1218 /* push fattest first; remember we still have extra PPs
1219 to the left of the left chunk and to the right of
1220 the right chunk! */
1221 /* assert top < STACKSIZE */
1222 if (r - lo <= hi - l) {
1223 /* second is bigger */
1224 stack[top].lo = l;
1225 stack[top].hi = hi;
1226 stack[top].extra = -extra;
1227 hi = r;
1228 extraOnRight = 0;
1229 }
1230 else {
1231 /* first is bigger */
1232 stack[top].lo = lo;
1233 stack[top].hi = r;
1234 stack[top].extra = extra;
1235 lo = l;
1236 extraOnRight = 1;
1237 }
1238 ++top;
1239
1240 } /* end of partitioning loop */
1241
1242 return 0;
1243
1244 fail:
1245 return -1;
1246}
Guido van Rossuma119c0d1998-05-29 17:56:32 +00001247
1248#undef SETK
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001249
1250staticforward PyTypeObject immutable_list_type;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001251
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001252static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001253listsort(PyListObject *self, PyObject *args)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001254{
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001255 int err;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001256 PyObject *compare = NULL;
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001257
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001258 if (args != NULL) {
Guido van Rossumc00a9382000-02-24 21:48:29 +00001259 if (!PyArg_ParseTuple(args, "|O:sort", &compare))
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001260 return NULL;
1261 }
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001262 self->ob_type = &immutable_list_type;
1263 err = samplesortslice(self->ob_item,
1264 self->ob_item + self->ob_size,
Guido van Rossum42812581998-06-17 14:15:44 +00001265 compare);
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001266 self->ob_type = &PyList_Type;
1267 if (err < 0)
Guido van Rossum3f236de1996-12-10 23:55:39 +00001268 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001269 Py_INCREF(Py_None);
1270 return Py_None;
Guido van Rossum3f236de1996-12-10 23:55:39 +00001271}
1272
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001273int
Fred Drakea2f55112000-07-09 15:16:51 +00001274PyList_Sort(PyObject *v)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001275{
1276 if (v == NULL || !PyList_Check(v)) {
1277 PyErr_BadInternalCall();
1278 return -1;
1279 }
1280 v = listsort((PyListObject *)v, (PyObject *)NULL);
1281 if (v == NULL)
1282 return -1;
1283 Py_DECREF(v);
1284 return 0;
1285}
1286
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001287static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001288listreverse(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00001289{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001290 register PyObject **p, **q;
1291 register PyObject *tmp;
Guido van Rossumed98d481991-03-06 13:07:53 +00001292
Guido van Rossumc00a9382000-02-24 21:48:29 +00001293 if (!PyArg_ParseTuple(args, ":reverse"))
Guido van Rossumed98d481991-03-06 13:07:53 +00001294 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001295
1296 if (self->ob_size > 1) {
1297 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
1298 p < q; p++, q--) {
1299 tmp = *p;
1300 *p = *q;
1301 *q = tmp;
1302 }
1303 }
1304
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001305 Py_INCREF(Py_None);
1306 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001307}
1308
Guido van Rossum84c76f51990-10-30 13:32:20 +00001309int
Fred Drakea2f55112000-07-09 15:16:51 +00001310PyList_Reverse(PyObject *v)
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001311{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001312 if (v == NULL || !PyList_Check(v)) {
1313 PyErr_BadInternalCall();
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001314 return -1;
1315 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001316 v = listreverse((PyListObject *)v, (PyObject *)NULL);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001317 if (v == NULL)
1318 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001319 Py_DECREF(v);
Guido van Rossumb0fe3a91995-01-17 16:34:45 +00001320 return 0;
1321}
1322
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001323PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001324PyList_AsTuple(PyObject *v)
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001325{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001326 PyObject *w;
1327 PyObject **p;
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001328 int n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001329 if (v == NULL || !PyList_Check(v)) {
1330 PyErr_BadInternalCall();
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001331 return NULL;
1332 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001333 n = ((PyListObject *)v)->ob_size;
1334 w = PyTuple_New(n);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001335 if (w == NULL)
1336 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001337 p = ((PyTupleObject *)w)->ob_item;
Thomas Wouters334fb892000-07-25 12:56:38 +00001338 memcpy((void *)p,
1339 (void *)((PyListObject *)v)->ob_item,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001340 n*sizeof(PyObject *));
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001341 while (--n >= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001342 Py_INCREF(*p);
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001343 p++;
1344 }
1345 return w;
1346}
1347
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001348static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001349listindex(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00001350{
1351 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001352 PyObject *v;
1353
Guido van Rossumef93b872000-03-13 15:41:59 +00001354 if (!PyArg_ParseTuple_Compat1(args, "O:index", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001355 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001356 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001357 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001358 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001359 if (PyErr_Occurred())
1360 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001361 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001362 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001363 return NULL;
1364}
1365
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001366static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001367listcount(PyListObject *self, PyObject *args)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001368{
1369 int count = 0;
1370 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001371 PyObject *v;
1372
Guido van Rossumef93b872000-03-13 15:41:59 +00001373 if (!PyArg_ParseTuple_Compat1(args, "O:count", &v))
Guido van Rossume6f7d181991-10-20 20:20:40 +00001374 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001375 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001376 if (PyObject_Compare(self->ob_item[i], v) == 0)
Guido van Rossume6f7d181991-10-20 20:20:40 +00001377 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001378 if (PyErr_Occurred())
1379 return NULL;
Guido van Rossume6f7d181991-10-20 20:20:40 +00001380 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001381 return PyInt_FromLong((long)count);
Guido van Rossume6f7d181991-10-20 20:20:40 +00001382}
1383
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001384static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001385listremove(PyListObject *self, PyObject *args)
Guido van Rossumed98d481991-03-06 13:07:53 +00001386{
1387 int i;
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001388 PyObject *v;
1389
Guido van Rossumef93b872000-03-13 15:41:59 +00001390 if (!PyArg_ParseTuple_Compat1(args, "O:remove", &v))
Guido van Rossumed98d481991-03-06 13:07:53 +00001391 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001392 for (i = 0; i < self->ob_size; i++) {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001393 if (PyObject_Compare(self->ob_item[i], v) == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001394 if (list_ass_slice(self, i, i+1,
1395 (PyObject *)NULL) != 0)
Guido van Rossumed98d481991-03-06 13:07:53 +00001396 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001397 Py_INCREF(Py_None);
1398 return Py_None;
Guido van Rossumed98d481991-03-06 13:07:53 +00001399 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001400 if (PyErr_Occurred())
1401 return NULL;
Guido van Rossumed98d481991-03-06 13:07:53 +00001402 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001403 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
Guido van Rossumed98d481991-03-06 13:07:53 +00001404 return NULL;
1405}
1406
Jeremy Hylton8caad492000-06-23 14:18:11 +00001407static int
1408list_traverse(PyListObject *o, visitproc visit, void *arg)
1409{
1410 int i, err;
1411 PyObject *x;
1412
1413 for (i = o->ob_size; --i >= 0; ) {
1414 x = o->ob_item[i];
1415 if (x != NULL) {
1416 err = visit(x, arg);
1417 if (err)
1418 return err;
1419 }
1420 }
1421 return 0;
1422}
1423
1424static int
1425list_clear(PyListObject *lp)
1426{
1427 (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0);
1428 return 0;
1429}
1430
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001431static char append_doc[] =
1432"L.append(object) -- append object to end";
Barry Warsawdedf6d61998-10-09 16:37:25 +00001433static char extend_doc[] =
1434"L.extend(list) -- extend list by appending list elements";
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001435static char insert_doc[] =
1436"L.insert(index, object) -- insert object before index";
1437static char pop_doc[] =
1438"L.pop([index]) -> item -- remove and return item at index (default last)";
1439static char remove_doc[] =
1440"L.remove(value) -- remove first occurrence of value";
1441static char index_doc[] =
1442"L.index(value) -> integer -- return index of first occurrence of value";
1443static char count_doc[] =
1444"L.count(value) -> integer -- return number of occurrences of value";
1445static char reverse_doc[] =
1446"L.reverse() -- reverse *IN PLACE*";
1447static char sort_doc[] =
1448"L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
1449
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001450static PyMethodDef list_methods[] = {
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001451 {"append", (PyCFunction)listappend, 1, append_doc},
1452 {"insert", (PyCFunction)listinsert, 1, insert_doc},
Barry Warsawdedf6d61998-10-09 16:37:25 +00001453 {"extend", (PyCFunction)listextend, 1, extend_doc},
Guido van Rossum3dd7f3f1998-06-30 15:36:32 +00001454 {"pop", (PyCFunction)listpop, 1, pop_doc},
Guido van Rossum4aa24f92000-02-24 15:23:03 +00001455 {"remove", (PyCFunction)listremove, 1, remove_doc},
1456 {"index", (PyCFunction)listindex, 1, index_doc},
1457 {"count", (PyCFunction)listcount, 1, count_doc},
1458 {"reverse", (PyCFunction)listreverse, 1, reverse_doc},
1459 {"sort", (PyCFunction)listsort, 1, sort_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001460 {NULL, NULL} /* sentinel */
1461};
1462
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001463static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001464list_getattr(PyListObject *f, char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001465{
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*/
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001478 (binaryfunc)list_inplace_concat, /*sq_inplace_concat*/
1479 (intargfunc)list_inplace_repeat, /*sq_inplace_repeat*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001480};
1481
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001482PyTypeObject PyList_Type = {
1483 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001484 0,
1485 "list",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001486 sizeof(PyListObject) + PyGC_HEAD_SIZE,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001487 0,
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001488 (destructor)list_dealloc, /*tp_dealloc*/
1489 (printfunc)list_print, /*tp_print*/
1490 (getattrfunc)list_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001491 0, /*tp_setattr*/
Guido van Rossum6cd2fe01994-08-29 12:45:32 +00001492 (cmpfunc)list_compare, /*tp_compare*/
1493 (reprfunc)list_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001494 0, /*tp_as_number*/
1495 &list_as_sequence, /*tp_as_sequence*/
1496 0, /*tp_as_mapping*/
Fred Drake56780252000-06-15 14:50:20 +00001497 0, /*tp_hash*/
1498 0, /*tp_call*/
1499 0, /*tp_str*/
1500 0, /*tp_getattro*/
1501 0, /*tp_setattro*/
1502 0, /*tp_as_buffer*/
Jeremy Hyltond08b4c42000-06-23 19:37:02 +00001503 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
Jeremy Hylton8caad492000-06-23 14:18:11 +00001504 0, /* tp_doc */
1505 (traverseproc)list_traverse, /* tp_traverse */
1506 (inquiry)list_clear, /* tp_clear */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001507};
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001508
1509
1510/* During a sort, we really can't have anyone modifying the list; it could
1511 cause core dumps. Thus, we substitute a dummy type that raises an
1512 explanatory exception when a modifying operation is used. Caveat:
1513 comparisons may behave differently; but I guess it's a bad idea anyway to
1514 compare a list that's being sorted... */
1515
1516static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001517immutable_list_op(void)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001518{
1519 PyErr_SetString(PyExc_TypeError,
1520 "a list cannot be modified while it is being sorted");
1521 return NULL;
1522}
1523
1524static PyMethodDef immutable_list_methods[] = {
1525 {"append", (PyCFunction)immutable_list_op},
1526 {"insert", (PyCFunction)immutable_list_op},
1527 {"remove", (PyCFunction)immutable_list_op},
1528 {"index", (PyCFunction)listindex},
1529 {"count", (PyCFunction)listcount},
1530 {"reverse", (PyCFunction)immutable_list_op},
1531 {"sort", (PyCFunction)immutable_list_op},
1532 {NULL, NULL} /* sentinel */
1533};
1534
1535static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001536immutable_list_getattr(PyListObject *f, char *name)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001537{
1538 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
1539}
1540
1541static int
Fred Drakea2f55112000-07-09 15:16:51 +00001542immutable_list_ass(void)
Guido van Rossum4c4e7df1998-06-16 15:18:28 +00001543{
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};