Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | /* List object implementation */ |
| 12 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 13 | #include "Python.h" |
| 14 | |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 15 | #ifdef STDC_HEADERS |
| 16 | #include <stddef.h> |
| 17 | #else |
| 18 | #include <sys/types.h> /* For size_t */ |
| 19 | #endif |
Jack Jansen | e979160 | 2000-08-22 21:51:22 +0000 | [diff] [blame] | 20 | #ifdef HAVE_LIMITS_H |
| 21 | #include <limits.h> |
| 22 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 24 | #define ROUNDUP(n, PyTryBlock) \ |
| 25 | ((((n)+(PyTryBlock)-1)/(PyTryBlock))*(PyTryBlock)) |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 26 | |
| 27 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 28 | roundupsize(int n) |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 29 | { |
| 30 | if (n < 500) |
| 31 | return ROUNDUP(n, 10); |
| 32 | else |
| 33 | return ROUNDUP(n, 100); |
| 34 | } |
| 35 | |
Guido van Rossum | a27d112 | 1997-08-25 18:36:23 +0000 | [diff] [blame] | 36 | #define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems)) |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 38 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 39 | PyList_New(int size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | { |
| 41 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 42 | PyListObject *op; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 43 | size_t nbytes; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | if (size < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 45 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | return NULL; |
| 47 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 48 | nbytes = size * sizeof(PyObject *); |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 49 | /* Check for overflow */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 50 | if (nbytes / sizeof(PyObject *) != (size_t)size) { |
| 51 | return PyErr_NoMemory(); |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 52 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 53 | /* PyObject_NewVar is inlined */ |
Jeremy Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 54 | op = (PyListObject *) PyObject_MALLOC(sizeof(PyListObject) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 55 | + PyGC_HEAD_SIZE); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | if (op == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 57 | return PyErr_NoMemory(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 59 | op = (PyListObject *) PyObject_FROM_GC(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 60 | if (size <= 0) { |
| 61 | op->ob_item = NULL; |
| 62 | } |
| 63 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 64 | op->ob_item = (PyObject **) PyMem_MALLOC(nbytes); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | if (op->ob_item == NULL) { |
Vladimir Marangozov | 467a67e | 2000-07-15 03:31:31 +0000 | [diff] [blame] | 66 | PyObject_FREE(PyObject_AS_GC(op)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 67 | return PyErr_NoMemory(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 70 | PyObject_INIT_VAR(op, &PyList_Type, size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 71 | for (i = 0; i < size; i++) |
| 72 | op->ob_item[i] = NULL; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 73 | PyObject_GC_Init(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 74 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 78 | PyList_Size(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 80 | if (!PyList_Check(op)) { |
| 81 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 82 | return -1; |
| 83 | } |
| 84 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 85 | return ((PyListObject *)op) -> ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 88 | static PyObject *indexerr; |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 90 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 91 | PyList_GetItem(PyObject *op, int i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 93 | if (!PyList_Check(op)) { |
| 94 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | return NULL; |
| 96 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 97 | if (i < 0 || i >= ((PyListObject *)op) -> ob_size) { |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 98 | if (indexerr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 99 | indexerr = PyString_FromString( |
| 100 | "list index out of range"); |
| 101 | PyErr_SetObject(PyExc_IndexError, indexerr); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | return NULL; |
| 103 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 104 | return ((PyListObject *)op) -> ob_item[i]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 108 | PyList_SetItem(register PyObject *op, register int i, |
| 109 | register PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 111 | register PyObject *olditem; |
| 112 | register PyObject **p; |
| 113 | if (!PyList_Check(op)) { |
| 114 | Py_XDECREF(newitem); |
| 115 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 116 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 117 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 118 | 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 Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 122 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 124 | p = ((PyListObject *)op) -> ob_item + i; |
Guido van Rossum | 5fe6058 | 1995-03-09 12:12:50 +0000 | [diff] [blame] | 125 | olditem = *p; |
| 126 | *p = newitem; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 127 | Py_XDECREF(olditem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 132 | ins1(PyListObject *self, int where, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | { |
| 134 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 135 | PyObject **items; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 136 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 137 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 138 | return -1; |
| 139 | } |
Trent Mick | a584664 | 2000-08-13 22:47:45 +0000 | [diff] [blame] | 140 | if (self->ob_size == INT_MAX) { |
| 141 | PyErr_SetString(PyExc_OverflowError, |
| 142 | "cannot add more objects to list"); |
| 143 | return -1; |
| 144 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 145 | items = self->ob_item; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 146 | NRESIZE(items, PyObject *, self->ob_size+1); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 147 | if (items == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 148 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 149 | return -1; |
| 150 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 151 | 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 157 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 158 | items[where] = v; |
| 159 | self->ob_item = items; |
| 160 | self->ob_size++; |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 165 | PyList_Insert(PyObject *op, int where, PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 166 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 167 | if (!PyList_Check(op)) { |
| 168 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 169 | return -1; |
| 170 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 171 | return ins1((PyListObject *)op, where, newitem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 175 | PyList_Append(PyObject *op, PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 176 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 177 | if (!PyList_Check(op)) { |
| 178 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 179 | return -1; |
| 180 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 181 | return ins1((PyListObject *)op, |
| 182 | (int) ((PyListObject *)op)->ob_size, newitem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* Methods */ |
| 186 | |
| 187 | static void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 188 | list_dealloc(PyListObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 189 | { |
| 190 | int i; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 191 | Py_TRASHCAN_SAFE_BEGIN(op) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 192 | PyObject_GC_Fini(op); |
Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 193 | if (op->ob_item != NULL) { |
Guido van Rossum | fa71701 | 1999-06-09 15:19:34 +0000 | [diff] [blame] | 194 | /* 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 200 | Py_XDECREF(op->ob_item[i]); |
Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 201 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 202 | PyMem_FREE(op->ob_item); |
Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 203 | } |
Guido van Rossum | 4cc6ac7 | 2000-07-01 01:00:38 +0000 | [diff] [blame] | 204 | op = (PyListObject *) PyObject_AS_GC(op); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 205 | PyObject_DEL(op); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 206 | Py_TRASHCAN_SAFE_END(op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 209 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 210 | list_print(PyListObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 211 | { |
| 212 | int i; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 213 | |
| 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 221 | fprintf(fp, "["); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 222 | for (i = 0; i < op->ob_size; i++) { |
| 223 | if (i > 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 224 | fprintf(fp, ", "); |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 225 | if (PyObject_Print(op->ob_item[i], fp, 0) != 0) { |
| 226 | Py_ReprLeave((PyObject *)op); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 227 | return -1; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 228 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 229 | } |
| 230 | fprintf(fp, "]"); |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 231 | Py_ReprLeave((PyObject *)op); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 232 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 235 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 236 | list_repr(PyListObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 238 | PyObject *s, *comma; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 239 | int i; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 240 | |
| 241 | i = Py_ReprEnter((PyObject*)v); |
| 242 | if (i != 0) { |
| 243 | if (i > 0) |
| 244 | return PyString_FromString("[...]"); |
| 245 | return NULL; |
| 246 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 247 | s = PyString_FromString("["); |
| 248 | comma = PyString_FromString(", "); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 249 | for (i = 0; i < v->ob_size && s != NULL; i++) { |
| 250 | if (i > 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 251 | PyString_Concat(&s, comma); |
| 252 | PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i])); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 253 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 254 | Py_XDECREF(comma); |
| 255 | PyString_ConcatAndDel(&s, PyString_FromString("]")); |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 256 | Py_ReprLeave((PyObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | return s; |
| 258 | } |
| 259 | |
| 260 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 261 | list_compare(PyListObject *v, PyListObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 262 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | int i; |
Jack Jansen | e979160 | 2000-08-22 21:51:22 +0000 | [diff] [blame] | 264 | |
Guido van Rossum | ae621ff | 1998-05-28 20:18:46 +0000 | [diff] [blame] | 265 | for (i = 0; i < v->ob_size && i < w->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 266 | int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 267 | if (cmp != 0) |
| 268 | return cmp; |
| 269 | } |
| 270 | return v->ob_size - w->ob_size; |
| 271 | } |
| 272 | |
| 273 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 274 | list_length(PyListObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 275 | { |
| 276 | return a->ob_size; |
| 277 | } |
| 278 | |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 279 | |
| 280 | |
| 281 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 282 | list_contains(PyListObject *a, PyObject *el) |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 283 | { |
| 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 297 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 298 | list_item(PyListObject *a, int i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 299 | { |
| 300 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 301 | if (indexerr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 302 | indexerr = PyString_FromString( |
| 303 | "list index out of range"); |
| 304 | PyErr_SetObject(PyExc_IndexError, indexerr); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 305 | return NULL; |
| 306 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 307 | Py_INCREF(a->ob_item[i]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | return a->ob_item[i]; |
| 309 | } |
| 310 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 311 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 312 | list_slice(PyListObject *a, int ilow, int ihigh) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 313 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 314 | PyListObject *np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | int i; |
| 316 | if (ilow < 0) |
| 317 | ilow = 0; |
| 318 | else if (ilow > a->ob_size) |
| 319 | ilow = a->ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 320 | if (ihigh < ilow) |
| 321 | ihigh = ilow; |
| 322 | else if (ihigh > a->ob_size) |
| 323 | ihigh = a->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 324 | np = (PyListObject *) PyList_New(ihigh - ilow); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | if (np == NULL) |
| 326 | return NULL; |
| 327 | for (i = ilow; i < ihigh; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 328 | PyObject *v = a->ob_item[i]; |
| 329 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 330 | np->ob_item[i - ilow] = v; |
| 331 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 332 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 335 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 336 | PyList_GetSlice(PyObject *a, int ilow, int ihigh) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 337 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 338 | if (!PyList_Check(a)) { |
| 339 | PyErr_BadInternalCall(); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 340 | return NULL; |
| 341 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 342 | return list_slice((PyListObject *)a, ilow, ihigh); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 345 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 346 | list_concat(PyListObject *a, PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 347 | { |
| 348 | int size; |
| 349 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 350 | PyListObject *np; |
| 351 | if (!PyList_Check(bb)) { |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 352 | PyErr_Format(PyExc_TypeError, |
Fred Drake | 914a2ed | 2000-06-01 14:31:03 +0000 | [diff] [blame] | 353 | "can only concatenate list (not \"%.200s\") to list", |
| 354 | bb->ob_type->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 355 | return NULL; |
| 356 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 357 | #define b ((PyListObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 358 | size = a->ob_size + b->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 359 | np = (PyListObject *) PyList_New(size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 360 | if (np == NULL) { |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 361 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 362 | } |
| 363 | for (i = 0; i < a->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 364 | PyObject *v = a->ob_item[i]; |
| 365 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 366 | np->ob_item[i] = v; |
| 367 | } |
| 368 | for (i = 0; i < b->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 369 | PyObject *v = b->ob_item[i]; |
| 370 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 371 | np->ob_item[i + a->ob_size] = v; |
| 372 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 373 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 374 | #undef b |
| 375 | } |
| 376 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 377 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 378 | list_repeat(PyListObject *a, int n) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 379 | { |
| 380 | int i, j; |
| 381 | int size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 382 | PyListObject *np; |
| 383 | PyObject **p; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 384 | if (n < 0) |
| 385 | n = 0; |
| 386 | size = a->ob_size * n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 387 | np = (PyListObject *) PyList_New(size); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 388 | 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 394 | Py_INCREF(*p); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 395 | p++; |
| 396 | } |
| 397 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 398 | return (PyObject *) np; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 401 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 402 | list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 403 | { |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 404 | /* 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 410 | PyObject **recycle, **p; |
| 411 | PyObject **item; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 412 | int n; /* Size of replacement list */ |
| 413 | int d; /* Change in size */ |
| 414 | int k; /* Loop index */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 415 | #define b ((PyListObject *)v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 416 | if (v == NULL) |
| 417 | n = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 418 | else if (PyList_Check(v)) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 419 | n = b->ob_size; |
Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 420 | 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 425 | Py_DECREF(v); |
Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 426 | return ret; |
| 427 | } |
| 428 | } |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 429 | else { |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 430 | PyErr_Format(PyExc_TypeError, |
| 431 | "must assign list (not \"%.200s\") to slice", |
| 432 | v->ob_type->tp_name); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 433 | return -1; |
| 434 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 435 | if (ilow < 0) |
| 436 | ilow = 0; |
| 437 | else if (ilow > a->ob_size) |
| 438 | ilow = a->ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 439 | 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 Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 445 | if (ihigh > ilow) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 446 | p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow)); |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 447 | else |
| 448 | p = recycle = NULL; |
| 449 | if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 450 | for (k = ilow; k < ihigh; k++) |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 451 | *p++ = item[k]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 452 | 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 456 | NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 457 | a->ob_item = item; |
| 458 | } |
| 459 | } |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 460 | else { /* Insert d items; recycle ihigh-ilow items */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 461 | NRESIZE(item, PyObject *, a->ob_size + d); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 462 | if (item == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 463 | if (recycle != NULL) |
| 464 | PyMem_DEL(recycle); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 465 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 466 | return -1; |
| 467 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 468 | for (k = a->ob_size; --k >= ihigh; ) |
| 469 | item[k+d] = item[k]; |
| 470 | for (/*k = ihigh-1*/; k >= ilow; --k) |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 471 | *p++ = item[k]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 472 | a->ob_item = item; |
| 473 | a->ob_size += d; |
| 474 | } |
| 475 | for (k = 0; k < n; k++, ilow++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 476 | PyObject *w = b->ob_item[k]; |
| 477 | Py_XINCREF(w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 478 | item[ilow] = w; |
| 479 | } |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 480 | if (recycle) { |
| 481 | while (--p >= recycle) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 482 | Py_XDECREF(*p); |
| 483 | PyMem_DEL(recycle); |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 484 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 485 | return 0; |
| 486 | #undef b |
| 487 | } |
| 488 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 489 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 490 | PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 491 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 492 | if (!PyList_Check(a)) { |
| 493 | PyErr_BadInternalCall(); |
Guido van Rossum | 1fc238a | 1993-07-29 08:25:09 +0000 | [diff] [blame] | 494 | return -1; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 495 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 496 | return list_ass_slice((PyListObject *)a, ilow, ihigh, v); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 499 | static PyObject * |
| 500 | list_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 Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 543 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 544 | list_ass_item(PyListObject *a, int i, PyObject *v) |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 545 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 546 | PyObject *old_value; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 547 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 548 | PyErr_SetString(PyExc_IndexError, |
| 549 | "list assignment index out of range"); |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 550 | return -1; |
| 551 | } |
| 552 | if (v == NULL) |
| 553 | return list_ass_slice(a, i, i+1, v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 554 | Py_INCREF(v); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 555 | old_value = a->ob_item[i]; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 556 | a->ob_item[i] = v; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 557 | Py_DECREF(old_value); |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 558 | return 0; |
| 559 | } |
| 560 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 561 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 562 | ins(PyListObject *self, int where, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 563 | { |
| 564 | if (ins1(self, where, v) != 0) |
| 565 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 566 | Py_INCREF(Py_None); |
| 567 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 570 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 571 | listinsert(PyListObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 572 | { |
| 573 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 574 | PyObject *v; |
Guido van Rossum | c00a938 | 2000-02-24 21:48:29 +0000 | [diff] [blame] | 575 | if (!PyArg_ParseTuple(args, "iO:insert", &i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 576 | return NULL; |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 577 | return ins(self, i, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Guido van Rossum | ef93b87 | 2000-03-13 15:41:59 +0000 | [diff] [blame] | 580 | /* 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 594 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 595 | listappend(PyListObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 596 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 597 | PyObject *v; |
Guido van Rossum | ef93b87 | 2000-03-13 15:41:59 +0000 | [diff] [blame] | 598 | if (!PyArg_ParseTuple_Compat1(args, "O:append", &v)) |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 599 | return NULL; |
| 600 | return ins(self, (int) self->ob_size, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 603 | static int |
| 604 | listextend_internal(PyListObject *self, PyObject *b) |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 605 | { |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 606 | PyObject **items; |
| 607 | int selflen = PyList_GET_SIZE(self); |
| 608 | int blen; |
| 609 | register int i; |
| 610 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 611 | if (PyObject_Size(b) == 0) |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 612 | /* short circuit when b is empty */ |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 613 | return 0; |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 614 | |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 615 | 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. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 622 | Py_DECREF(b); |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 623 | b = PyList_New(selflen); |
| 624 | if (!b) |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 625 | return -1; |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 626 | 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 Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 632 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 633 | blen = PyObject_Size(b); |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 634 | |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 635 | /* resize a using idiom */ |
| 636 | items = self->ob_item; |
| 637 | NRESIZE(items, PyObject*, selflen + blen); |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 638 | if (items == NULL) { |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 639 | PyErr_NoMemory(); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 640 | Py_DECREF(b); |
| 641 | return -1; |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 642 | } |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 643 | |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 644 | self->ob_item = items; |
| 645 | |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 646 | /* populate the end of self with b's items */ |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 647 | for (i = 0; i < blen; i++) { |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 648 | PyObject *o = PySequence_Fast_GET_ITEM(b, i); |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 649 | Py_INCREF(o); |
| 650 | PyList_SET_ITEM(self, self->ob_size++, o); |
| 651 | } |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 652 | Py_DECREF(b); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 653 | return 0; |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | |
| 657 | static PyObject * |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 658 | list_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 | |
| 671 | static PyObject * |
| 672 | listextend(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 | |
| 691 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 692 | listpop(PyListObject *self, PyObject *args) |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 693 | { |
| 694 | int i = -1; |
| 695 | PyObject *v; |
Guido van Rossum | c00a938 | 2000-02-24 21:48:29 +0000 | [diff] [blame] | 696 | if (!PyArg_ParseTuple(args, "|i:pop", &i)) |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 697 | 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 Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 718 | /* 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 Rossum | 19700b6 | 1997-03-05 00:45:43 +0000 | [diff] [blame] | 724 | #define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) ) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 725 | |
| 726 | /* Comparison function. Takes care of calling a user-supplied |
| 727 | comparison function (any callable Python object). Calls the |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 728 | standard comparison function, PyObject_Compare(), if the user- |
| 729 | supplied function is NULL. */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 730 | |
| 731 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 732 | docompare(PyObject *x, PyObject *y, PyObject *compare) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 733 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 734 | PyObject *args, *res; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 735 | int i; |
| 736 | |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 737 | if (compare == NULL) { |
| 738 | i = PyObject_Compare(x, y); |
| 739 | if (i && PyErr_Occurred()) |
| 740 | i = CMPERROR; |
| 741 | return i; |
| 742 | } |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 743 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 744 | args = Py_BuildValue("(OO)", x, y); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 745 | if (args == NULL) |
| 746 | return CMPERROR; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 747 | res = PyEval_CallObject(compare, args); |
| 748 | Py_DECREF(args); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 749 | if (res == NULL) |
| 750 | return CMPERROR; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 751 | if (!PyInt_Check(res)) { |
| 752 | Py_DECREF(res); |
| 753 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 9bcd1d7 | 1999-04-19 17:44:39 +0000 | [diff] [blame] | 754 | "comparison function must return int"); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 755 | return CMPERROR; |
| 756 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 757 | i = PyInt_AsLong(res); |
| 758 | Py_DECREF(res); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 759 | if (i < 0) |
| 760 | return -1; |
| 761 | if (i > 0) |
| 762 | return 1; |
| 763 | return 0; |
| 764 | } |
| 765 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 766 | /* 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 Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 793 | |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 794 | /* STACKSIZE is the size of our work stack. A rough estimate is that |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 795 | 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 Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 801 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 802 | |
| 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 Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 808 | [lo, hi) is a contiguous slice of a list, and is sorted via |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 809 | 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 Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 816 | |
| 817 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 818 | binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare) |
| 819 | /* compare -- comparison function object, or NULL for default */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 820 | { |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 821 | /* 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 Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 826 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 827 | 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 Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 833 | pivot = *r; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 834 | 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 Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 848 | } |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 849 | return 0; |
Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 850 | |
| 851 | fail: |
| 852 | return -1; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /* samplesortslice is the sorting workhorse. |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 856 | [lo, hi) is a contiguous slice of a list, to be sorted in place. |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 857 | 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 | |
| 902 | struct 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 Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 917 | N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 918 | 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 Rossum | 3aa23fd | 1999-01-14 19:01:53 +0000 | [diff] [blame] | 921 | static long cutoff[] = { |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 922 | 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 | |
| 948 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 949 | samplesortslice(PyObject **lo, PyObject **hi, PyObject *compare) |
| 950 | /* compare -- comparison function object, or NULL for default */ |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 951 | { |
| 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 Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 982 | return binarysort(lo, hi, r, compare); |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 983 | |
| 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 Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1001 | return binarysort(lo, hi, originalr, compare); |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1002 | } |
| 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 Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1043 | if (samplesortslice(lo, lo + extra, compare) < 0) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1044 | 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 Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1068 | if (samplesortslice(lo, hi, compare) < 0) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1069 | 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 Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1086 | compare) < 0) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1087 | 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 Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1159 | 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 Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1165 | break; |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1166 | } |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1167 | } |
| 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 Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 1247 | |
| 1248 | #undef SETK |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1249 | |
| 1250 | staticforward PyTypeObject immutable_list_type; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1251 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1252 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1253 | listsort(PyListObject *self, PyObject *args) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1254 | { |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1255 | int err; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1256 | PyObject *compare = NULL; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1257 | |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1258 | if (args != NULL) { |
Guido van Rossum | c00a938 | 2000-02-24 21:48:29 +0000 | [diff] [blame] | 1259 | if (!PyArg_ParseTuple(args, "|O:sort", &compare)) |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1260 | return NULL; |
| 1261 | } |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1262 | self->ob_type = &immutable_list_type; |
| 1263 | err = samplesortslice(self->ob_item, |
| 1264 | self->ob_item + self->ob_size, |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1265 | compare); |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1266 | self->ob_type = &PyList_Type; |
| 1267 | if (err < 0) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1268 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1269 | Py_INCREF(Py_None); |
| 1270 | return Py_None; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1273 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1274 | PyList_Sort(PyObject *v) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1275 | { |
| 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1287 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1288 | listreverse(PyListObject *self, PyObject *args) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1289 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1290 | register PyObject **p, **q; |
| 1291 | register PyObject *tmp; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1292 | |
Guido van Rossum | c00a938 | 2000-02-24 21:48:29 +0000 | [diff] [blame] | 1293 | if (!PyArg_ParseTuple(args, ":reverse")) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1294 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1295 | |
| 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1305 | Py_INCREF(Py_None); |
| 1306 | return Py_None; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Guido van Rossum | 84c76f5 | 1990-10-30 13:32:20 +0000 | [diff] [blame] | 1309 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1310 | PyList_Reverse(PyObject *v) |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1311 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1312 | if (v == NULL || !PyList_Check(v)) { |
| 1313 | PyErr_BadInternalCall(); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1314 | return -1; |
| 1315 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1316 | v = listreverse((PyListObject *)v, (PyObject *)NULL); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1317 | if (v == NULL) |
| 1318 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1319 | Py_DECREF(v); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1320 | return 0; |
| 1321 | } |
| 1322 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1323 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1324 | PyList_AsTuple(PyObject *v) |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1325 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1326 | PyObject *w; |
| 1327 | PyObject **p; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1328 | int n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1329 | if (v == NULL || !PyList_Check(v)) { |
| 1330 | PyErr_BadInternalCall(); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1331 | return NULL; |
| 1332 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1333 | n = ((PyListObject *)v)->ob_size; |
| 1334 | w = PyTuple_New(n); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1335 | if (w == NULL) |
| 1336 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1337 | p = ((PyTupleObject *)w)->ob_item; |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1338 | memcpy((void *)p, |
| 1339 | (void *)((PyListObject *)v)->ob_item, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1340 | n*sizeof(PyObject *)); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1341 | while (--n >= 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1342 | Py_INCREF(*p); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1343 | p++; |
| 1344 | } |
| 1345 | return w; |
| 1346 | } |
| 1347 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1348 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1349 | listindex(PyListObject *self, PyObject *args) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1350 | { |
| 1351 | int i; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1352 | PyObject *v; |
| 1353 | |
Guido van Rossum | ef93b87 | 2000-03-13 15:41:59 +0000 | [diff] [blame] | 1354 | if (!PyArg_ParseTuple_Compat1(args, "O:index", &v)) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1355 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1356 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1357 | if (PyObject_Compare(self->ob_item[i], v) == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1358 | return PyInt_FromLong((long)i); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1359 | if (PyErr_Occurred()) |
| 1360 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1361 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1362 | PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list"); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1363 | return NULL; |
| 1364 | } |
| 1365 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1366 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1367 | listcount(PyListObject *self, PyObject *args) |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1368 | { |
| 1369 | int count = 0; |
| 1370 | int i; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1371 | PyObject *v; |
| 1372 | |
Guido van Rossum | ef93b87 | 2000-03-13 15:41:59 +0000 | [diff] [blame] | 1373 | if (!PyArg_ParseTuple_Compat1(args, "O:count", &v)) |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1374 | return NULL; |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1375 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1376 | if (PyObject_Compare(self->ob_item[i], v) == 0) |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1377 | count++; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1378 | if (PyErr_Occurred()) |
| 1379 | return NULL; |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1380 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1381 | return PyInt_FromLong((long)count); |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1384 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1385 | listremove(PyListObject *self, PyObject *args) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1386 | { |
| 1387 | int i; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1388 | PyObject *v; |
| 1389 | |
Guido van Rossum | ef93b87 | 2000-03-13 15:41:59 +0000 | [diff] [blame] | 1390 | if (!PyArg_ParseTuple_Compat1(args, "O:remove", &v)) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1391 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1392 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1393 | if (PyObject_Compare(self->ob_item[i], v) == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1394 | if (list_ass_slice(self, i, i+1, |
| 1395 | (PyObject *)NULL) != 0) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1396 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1397 | Py_INCREF(Py_None); |
| 1398 | return Py_None; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1399 | } |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1400 | if (PyErr_Occurred()) |
| 1401 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1402 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1403 | PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list"); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1404 | return NULL; |
| 1405 | } |
| 1406 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1407 | static int |
| 1408 | list_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 | |
| 1424 | static int |
| 1425 | list_clear(PyListObject *lp) |
| 1426 | { |
| 1427 | (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0); |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 1431 | static char append_doc[] = |
| 1432 | "L.append(object) -- append object to end"; |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 1433 | static char extend_doc[] = |
| 1434 | "L.extend(list) -- extend list by appending list elements"; |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 1435 | static char insert_doc[] = |
| 1436 | "L.insert(index, object) -- insert object before index"; |
| 1437 | static char pop_doc[] = |
| 1438 | "L.pop([index]) -> item -- remove and return item at index (default last)"; |
| 1439 | static char remove_doc[] = |
| 1440 | "L.remove(value) -- remove first occurrence of value"; |
| 1441 | static char index_doc[] = |
| 1442 | "L.index(value) -> integer -- return index of first occurrence of value"; |
| 1443 | static char count_doc[] = |
| 1444 | "L.count(value) -> integer -- return number of occurrences of value"; |
| 1445 | static char reverse_doc[] = |
| 1446 | "L.reverse() -- reverse *IN PLACE*"; |
| 1447 | static char sort_doc[] = |
| 1448 | "L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1"; |
| 1449 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1450 | static PyMethodDef list_methods[] = { |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1451 | {"append", (PyCFunction)listappend, 1, append_doc}, |
| 1452 | {"insert", (PyCFunction)listinsert, 1, insert_doc}, |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 1453 | {"extend", (PyCFunction)listextend, 1, extend_doc}, |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 1454 | {"pop", (PyCFunction)listpop, 1, pop_doc}, |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1455 | {"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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1460 | {NULL, NULL} /* sentinel */ |
| 1461 | }; |
| 1462 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1463 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1464 | list_getattr(PyListObject *f, char *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1465 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1466 | return Py_FindMethod(list_methods, (PyObject *)f, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1469 | static PySequenceMethods list_as_sequence = { |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1470 | (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 Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 1477 | (objobjproc)list_contains, /*sq_contains*/ |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1478 | (binaryfunc)list_inplace_concat, /*sq_inplace_concat*/ |
| 1479 | (intargfunc)list_inplace_repeat, /*sq_inplace_repeat*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1480 | }; |
| 1481 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1482 | PyTypeObject PyList_Type = { |
| 1483 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1484 | 0, |
| 1485 | "list", |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1486 | sizeof(PyListObject) + PyGC_HEAD_SIZE, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1487 | 0, |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1488 | (destructor)list_dealloc, /*tp_dealloc*/ |
| 1489 | (printfunc)list_print, /*tp_print*/ |
| 1490 | (getattrfunc)list_getattr, /*tp_getattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1491 | 0, /*tp_setattr*/ |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1492 | (cmpfunc)list_compare, /*tp_compare*/ |
| 1493 | (reprfunc)list_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1494 | 0, /*tp_as_number*/ |
| 1495 | &list_as_sequence, /*tp_as_sequence*/ |
| 1496 | 0, /*tp_as_mapping*/ |
Fred Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 1497 | 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 Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 1503 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1504 | 0, /* tp_doc */ |
| 1505 | (traverseproc)list_traverse, /* tp_traverse */ |
| 1506 | (inquiry)list_clear, /* tp_clear */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1507 | }; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1508 | |
| 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 | |
| 1516 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1517 | immutable_list_op(void) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1518 | { |
| 1519 | PyErr_SetString(PyExc_TypeError, |
| 1520 | "a list cannot be modified while it is being sorted"); |
| 1521 | return NULL; |
| 1522 | } |
| 1523 | |
| 1524 | static 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 | |
| 1535 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1536 | immutable_list_getattr(PyListObject *f, char *name) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1537 | { |
| 1538 | return Py_FindMethod(immutable_list_methods, (PyObject *)f, name); |
| 1539 | } |
| 1540 | |
| 1541 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1542 | immutable_list_ass(void) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1543 | { |
| 1544 | immutable_list_op(); |
| 1545 | return -1; |
| 1546 | } |
| 1547 | |
| 1548 | static 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 Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 1556 | (objobjproc)list_contains, /*sq_contains*/ |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1557 | }; |
| 1558 | |
| 1559 | static PyTypeObject immutable_list_type = { |
| 1560 | PyObject_HEAD_INIT(&PyType_Type) |
| 1561 | 0, |
| 1562 | "list (immutable, during sort)", |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1563 | sizeof(PyListObject) + PyGC_HEAD_SIZE, |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1564 | 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 Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 1574 | 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 Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 1580 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1581 | 0, /* tp_doc */ |
| 1582 | (traverseproc)list_traverse, /* tp_traverse */ |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1583 | }; |