Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* List object implementation */ |
| 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | #include "Python.h" |
| 35 | |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 36 | #ifdef STDC_HEADERS |
| 37 | #include <stddef.h> |
| 38 | #else |
| 39 | #include <sys/types.h> /* For size_t */ |
| 40 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 42 | #define ROUNDUP(n, PyTryBlock) \ |
| 43 | ((((n)+(PyTryBlock)-1)/(PyTryBlock))*(PyTryBlock)) |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 44 | |
| 45 | static int |
Guido van Rossum | a27d112 | 1997-08-25 18:36:23 +0000 | [diff] [blame] | 46 | roundupsize(n) |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 47 | int n; |
| 48 | { |
| 49 | if (n < 500) |
| 50 | return ROUNDUP(n, 10); |
| 51 | else |
| 52 | return ROUNDUP(n, 100); |
| 53 | } |
| 54 | |
Guido van Rossum | a27d112 | 1997-08-25 18:36:23 +0000 | [diff] [blame] | 55 | #define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems)) |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 57 | PyObject * |
| 58 | PyList_New(size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | int size; |
| 60 | { |
| 61 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | PyListObject *op; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 63 | size_t nbytes; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 64 | if (size < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 65 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | return NULL; |
| 67 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 68 | nbytes = size * sizeof(PyObject *); |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 69 | /* Check for overflow */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 70 | if (nbytes / sizeof(PyObject *) != (size_t)size) { |
| 71 | return PyErr_NoMemory(); |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 72 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 73 | op = (PyListObject *) malloc(sizeof(PyListObject)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | if (op == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 75 | return PyErr_NoMemory(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | } |
| 77 | if (size <= 0) { |
| 78 | op->ob_item = NULL; |
| 79 | } |
| 80 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 81 | op->ob_item = (PyObject **) malloc(nbytes); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 82 | if (op->ob_item == NULL) { |
| 83 | free((ANY *)op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 84 | return PyErr_NoMemory(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 87 | op->ob_type = &PyList_Type; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | op->ob_size = size; |
| 89 | for (i = 0; i < size; i++) |
| 90 | op->ob_item[i] = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 91 | _Py_NewReference(op); |
| 92 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 96 | PyList_Size(op) |
| 97 | PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 99 | if (!PyList_Check(op)) { |
| 100 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | return -1; |
| 102 | } |
| 103 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 104 | return ((PyListObject *)op) -> ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 107 | static PyObject *indexerr; |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 109 | PyObject * |
| 110 | PyList_GetItem(op, i) |
| 111 | PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 112 | int i; |
| 113 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 114 | if (!PyList_Check(op)) { |
| 115 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | return NULL; |
| 117 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 118 | if (i < 0 || i >= ((PyListObject *)op) -> ob_size) { |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 119 | if (indexerr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 120 | indexerr = PyString_FromString( |
| 121 | "list index out of range"); |
| 122 | PyErr_SetObject(PyExc_IndexError, indexerr); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | return NULL; |
| 124 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 125 | return ((PyListObject *)op) -> ob_item[i]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 129 | PyList_SetItem(op, i, newitem) |
| 130 | register PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 131 | register int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 132 | register PyObject *newitem; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 134 | register PyObject *olditem; |
| 135 | register PyObject **p; |
| 136 | if (!PyList_Check(op)) { |
| 137 | Py_XDECREF(newitem); |
| 138 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 139 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 140 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 141 | if (i < 0 || i >= ((PyListObject *)op) -> ob_size) { |
| 142 | Py_XDECREF(newitem); |
| 143 | PyErr_SetString(PyExc_IndexError, |
| 144 | "list assignment index out of range"); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 145 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 146 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 147 | p = ((PyListObject *)op) -> ob_item + i; |
Guido van Rossum | 5fe6058 | 1995-03-09 12:12:50 +0000 | [diff] [blame] | 148 | olditem = *p; |
| 149 | *p = newitem; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 150 | Py_XDECREF(olditem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static int |
| 155 | ins1(self, where, v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 156 | PyListObject *self; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 157 | int where; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 158 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 159 | { |
| 160 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 161 | PyObject **items; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 162 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 163 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 164 | return -1; |
| 165 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 166 | items = self->ob_item; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 167 | NRESIZE(items, PyObject *, self->ob_size+1); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 168 | if (items == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 169 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 170 | return -1; |
| 171 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 172 | if (where < 0) |
| 173 | where = 0; |
| 174 | if (where > self->ob_size) |
| 175 | where = self->ob_size; |
| 176 | for (i = self->ob_size; --i >= where; ) |
| 177 | items[i+1] = items[i]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 178 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 179 | items[where] = v; |
| 180 | self->ob_item = items; |
| 181 | self->ob_size++; |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 186 | PyList_Insert(op, where, newitem) |
| 187 | PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 188 | int where; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 189 | PyObject *newitem; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 190 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 191 | if (!PyList_Check(op)) { |
| 192 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 193 | return -1; |
| 194 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 195 | return ins1((PyListObject *)op, where, newitem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 199 | PyList_Append(op, newitem) |
| 200 | PyObject *op; |
| 201 | PyObject *newitem; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 202 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 203 | if (!PyList_Check(op)) { |
| 204 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 205 | return -1; |
| 206 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 207 | return ins1((PyListObject *)op, |
| 208 | (int) ((PyListObject *)op)->ob_size, newitem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* Methods */ |
| 212 | |
| 213 | static void |
| 214 | list_dealloc(op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 215 | PyListObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 216 | { |
| 217 | int i; |
Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 218 | if (op->ob_item != NULL) { |
| 219 | for (i = 0; i < op->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 220 | Py_XDECREF(op->ob_item[i]); |
Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 221 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | free((ANY *)op->ob_item); |
Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 223 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 224 | free((ANY *)op); |
| 225 | } |
| 226 | |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 227 | static int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 228 | list_print(op, fp, flags) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 229 | PyListObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 230 | FILE *fp; |
| 231 | int flags; |
| 232 | { |
| 233 | int i; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 234 | |
| 235 | i = Py_ReprEnter((PyObject*)op); |
| 236 | if (i != 0) { |
| 237 | if (i < 0) |
| 238 | return i; |
| 239 | fprintf(fp, "[...]"); |
| 240 | return 0; |
| 241 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 242 | fprintf(fp, "["); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 243 | for (i = 0; i < op->ob_size; i++) { |
| 244 | if (i > 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | fprintf(fp, ", "); |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 246 | if (PyObject_Print(op->ob_item[i], fp, 0) != 0) { |
| 247 | Py_ReprLeave((PyObject *)op); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 248 | return -1; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 249 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 250 | } |
| 251 | fprintf(fp, "]"); |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 252 | Py_ReprLeave((PyObject *)op); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 253 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 256 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | list_repr(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 258 | PyListObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 259 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 260 | PyObject *s, *comma; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 261 | int i; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 262 | |
| 263 | i = Py_ReprEnter((PyObject*)v); |
| 264 | if (i != 0) { |
| 265 | if (i > 0) |
| 266 | return PyString_FromString("[...]"); |
| 267 | return NULL; |
| 268 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 269 | s = PyString_FromString("["); |
| 270 | comma = PyString_FromString(", "); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 271 | for (i = 0; i < v->ob_size && s != NULL; i++) { |
| 272 | if (i > 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 273 | PyString_Concat(&s, comma); |
| 274 | PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i])); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 275 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 276 | Py_XDECREF(comma); |
| 277 | PyString_ConcatAndDel(&s, PyString_FromString("]")); |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 278 | Py_ReprLeave((PyObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 279 | return s; |
| 280 | } |
| 281 | |
| 282 | static int |
| 283 | list_compare(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 284 | PyListObject *v, *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 285 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 286 | int i; |
Guido van Rossum | ae621ff | 1998-05-28 20:18:46 +0000 | [diff] [blame] | 287 | 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] | 288 | 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] | 289 | if (cmp != 0) |
| 290 | return cmp; |
| 291 | } |
| 292 | return v->ob_size - w->ob_size; |
| 293 | } |
| 294 | |
| 295 | static int |
| 296 | list_length(a) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 297 | PyListObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 298 | { |
| 299 | return a->ob_size; |
| 300 | } |
| 301 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 302 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 303 | list_item(a, i) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 304 | PyListObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 305 | int i; |
| 306 | { |
| 307 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 308 | if (indexerr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 309 | indexerr = PyString_FromString( |
| 310 | "list index out of range"); |
| 311 | PyErr_SetObject(PyExc_IndexError, indexerr); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 312 | return NULL; |
| 313 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 314 | Py_INCREF(a->ob_item[i]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | return a->ob_item[i]; |
| 316 | } |
| 317 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 318 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 319 | list_slice(a, ilow, ihigh) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 320 | PyListObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 321 | int ilow, ihigh; |
| 322 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 323 | PyListObject *np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 324 | int i; |
| 325 | if (ilow < 0) |
| 326 | ilow = 0; |
| 327 | else if (ilow > a->ob_size) |
| 328 | ilow = a->ob_size; |
| 329 | if (ihigh < 0) |
| 330 | ihigh = 0; |
| 331 | if (ihigh < ilow) |
| 332 | ihigh = ilow; |
| 333 | else if (ihigh > a->ob_size) |
| 334 | ihigh = a->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 335 | np = (PyListObject *) PyList_New(ihigh - ilow); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 336 | if (np == NULL) |
| 337 | return NULL; |
| 338 | for (i = ilow; i < ihigh; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 339 | PyObject *v = a->ob_item[i]; |
| 340 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | np->ob_item[i - ilow] = v; |
| 342 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 343 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 346 | PyObject * |
| 347 | PyList_GetSlice(a, ilow, ihigh) |
| 348 | PyObject *a; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 349 | int ilow, ihigh; |
| 350 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 351 | if (!PyList_Check(a)) { |
| 352 | PyErr_BadInternalCall(); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 353 | return NULL; |
| 354 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 355 | return list_slice((PyListObject *)a, ilow, ihigh); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 358 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 359 | list_concat(a, bb) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 360 | PyListObject *a; |
| 361 | PyObject *bb; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 362 | { |
| 363 | int size; |
| 364 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 365 | PyListObject *np; |
| 366 | if (!PyList_Check(bb)) { |
| 367 | PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 368 | return NULL; |
| 369 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 370 | #define b ((PyListObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 371 | size = a->ob_size + b->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 372 | np = (PyListObject *) PyList_New(size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 373 | if (np == NULL) { |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 374 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 375 | } |
| 376 | for (i = 0; i < a->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 377 | PyObject *v = a->ob_item[i]; |
| 378 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 379 | np->ob_item[i] = v; |
| 380 | } |
| 381 | for (i = 0; i < b->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 382 | PyObject *v = b->ob_item[i]; |
| 383 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 384 | np->ob_item[i + a->ob_size] = v; |
| 385 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 386 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 387 | #undef b |
| 388 | } |
| 389 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 390 | static PyObject * |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 391 | list_repeat(a, n) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 392 | PyListObject *a; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 393 | int n; |
| 394 | { |
| 395 | int i, j; |
| 396 | int size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 397 | PyListObject *np; |
| 398 | PyObject **p; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 399 | if (n < 0) |
| 400 | n = 0; |
| 401 | size = a->ob_size * n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 402 | np = (PyListObject *) PyList_New(size); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 403 | if (np == NULL) |
| 404 | return NULL; |
| 405 | p = np->ob_item; |
| 406 | for (i = 0; i < n; i++) { |
| 407 | for (j = 0; j < a->ob_size; j++) { |
| 408 | *p = a->ob_item[j]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 409 | Py_INCREF(*p); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 410 | p++; |
| 411 | } |
| 412 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 413 | return (PyObject *) np; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 416 | static int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 417 | list_ass_slice(a, ilow, ihigh, v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 418 | PyListObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 419 | int ilow, ihigh; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 420 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 421 | { |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 422 | /* Because [X]DECREF can recursively invoke list operations on |
| 423 | this list, we must postpone all [X]DECREF activity until |
| 424 | after the list is back in its canonical shape. Therefore |
| 425 | we must allocate an additional array, 'recycle', into which |
| 426 | we temporarily copy the items that are deleted from the |
| 427 | list. :-( */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 428 | PyObject **recycle, **p; |
| 429 | PyObject **item; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 430 | int n; /* Size of replacement list */ |
| 431 | int d; /* Change in size */ |
| 432 | int k; /* Loop index */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 433 | #define b ((PyListObject *)v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 434 | if (v == NULL) |
| 435 | n = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 436 | else if (PyList_Check(v)) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 437 | n = b->ob_size; |
Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 438 | if (a == b) { |
| 439 | /* Special case "a[i:j] = a" -- copy b first */ |
| 440 | int ret; |
| 441 | v = list_slice(b, 0, n); |
| 442 | ret = list_ass_slice(a, ilow, ihigh, v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 443 | Py_DECREF(v); |
Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 444 | return ret; |
| 445 | } |
| 446 | } |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 447 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 448 | PyErr_BadArgument(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 449 | return -1; |
| 450 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 451 | if (ilow < 0) |
| 452 | ilow = 0; |
| 453 | else if (ilow > a->ob_size) |
| 454 | ilow = a->ob_size; |
| 455 | if (ihigh < 0) |
| 456 | ihigh = 0; |
| 457 | if (ihigh < ilow) |
| 458 | ihigh = ilow; |
| 459 | else if (ihigh > a->ob_size) |
| 460 | ihigh = a->ob_size; |
| 461 | item = a->ob_item; |
| 462 | d = n - (ihigh-ilow); |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 463 | if (ihigh > ilow) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 464 | p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow)); |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 465 | else |
| 466 | p = recycle = NULL; |
| 467 | if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 468 | for (k = ilow; k < ihigh; k++) |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 469 | *p++ = item[k]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 470 | if (d < 0) { |
| 471 | for (/*k = ihigh*/; k < a->ob_size; k++) |
| 472 | item[k+d] = item[k]; |
| 473 | a->ob_size += d; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 474 | NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 475 | a->ob_item = item; |
| 476 | } |
| 477 | } |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 478 | else { /* Insert d items; recycle ihigh-ilow items */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 479 | NRESIZE(item, PyObject *, a->ob_size + d); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 480 | if (item == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 481 | PyMem_XDEL(recycle); |
| 482 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 483 | return -1; |
| 484 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 485 | for (k = a->ob_size; --k >= ihigh; ) |
| 486 | item[k+d] = item[k]; |
| 487 | for (/*k = ihigh-1*/; k >= ilow; --k) |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 488 | *p++ = item[k]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 489 | a->ob_item = item; |
| 490 | a->ob_size += d; |
| 491 | } |
| 492 | for (k = 0; k < n; k++, ilow++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 493 | PyObject *w = b->ob_item[k]; |
| 494 | Py_XINCREF(w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 495 | item[ilow] = w; |
| 496 | } |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 497 | if (recycle) { |
| 498 | while (--p >= recycle) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 499 | Py_XDECREF(*p); |
| 500 | PyMem_DEL(recycle); |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 501 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 502 | return 0; |
| 503 | #undef b |
| 504 | } |
| 505 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 506 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 507 | PyList_SetSlice(a, ilow, ihigh, v) |
| 508 | PyObject *a; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 509 | int ilow, ihigh; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 510 | PyObject *v; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 511 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 512 | if (!PyList_Check(a)) { |
| 513 | PyErr_BadInternalCall(); |
Guido van Rossum | 1fc238a | 1993-07-29 08:25:09 +0000 | [diff] [blame] | 514 | return -1; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 515 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 516 | return list_ass_slice((PyListObject *)a, ilow, ihigh, v); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 519 | static int |
| 520 | list_ass_item(a, i, v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 521 | PyListObject *a; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 522 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 523 | PyObject *v; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 524 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 525 | PyObject *old_value; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 526 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 527 | PyErr_SetString(PyExc_IndexError, |
| 528 | "list assignment index out of range"); |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 529 | return -1; |
| 530 | } |
| 531 | if (v == NULL) |
| 532 | return list_ass_slice(a, i, i+1, v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 533 | Py_INCREF(v); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 534 | old_value = a->ob_item[i]; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 535 | a->ob_item[i] = v; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 536 | Py_DECREF(old_value); |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 537 | return 0; |
| 538 | } |
| 539 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 540 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 541 | ins(self, where, v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 542 | PyListObject *self; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 543 | int where; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 544 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 545 | { |
| 546 | if (ins1(self, where, v) != 0) |
| 547 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 548 | Py_INCREF(Py_None); |
| 549 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 552 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 553 | listinsert(self, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 554 | PyListObject *self; |
| 555 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 556 | { |
| 557 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 558 | PyObject *v; |
| 559 | if (!PyArg_Parse(args, "(iO)", &i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 560 | return NULL; |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 561 | return ins(self, i, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 564 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 565 | listappend(self, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 566 | PyListObject *self; |
| 567 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 568 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 569 | PyObject *v; |
| 570 | if (!PyArg_Parse(args, "O", &v)) |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 571 | return NULL; |
| 572 | return ins(self, (int) self->ob_size, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 575 | static PyObject * |
| 576 | listpop(self, args) |
| 577 | PyListObject *self; |
| 578 | PyObject *args; |
| 579 | { |
| 580 | int i = -1; |
| 581 | PyObject *v; |
| 582 | if (!PyArg_ParseTuple(args, "|i", &i)) |
| 583 | return NULL; |
| 584 | if (self->ob_size == 0) { |
| 585 | /* Special-case most common failure cause */ |
| 586 | PyErr_SetString(PyExc_IndexError, "pop from empty list"); |
| 587 | return NULL; |
| 588 | } |
| 589 | if (i < 0) |
| 590 | i += self->ob_size; |
| 591 | if (i < 0 || i >= self->ob_size) { |
| 592 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 593 | return NULL; |
| 594 | } |
| 595 | v = self->ob_item[i]; |
| 596 | Py_INCREF(v); |
| 597 | if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { |
| 598 | Py_DECREF(v); |
| 599 | return NULL; |
| 600 | } |
| 601 | return v; |
| 602 | } |
| 603 | |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 604 | /* New quicksort implementation for arrays of object pointers. |
| 605 | Thanks to discussions with Tim Peters. */ |
| 606 | |
| 607 | /* CMPERROR is returned by our comparison function when an error |
| 608 | occurred. This is the largest negative integer (0x80000000 on a |
| 609 | 32-bit system). */ |
Guido van Rossum | 19700b6 | 1997-03-05 00:45:43 +0000 | [diff] [blame] | 610 | #define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) ) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 611 | |
| 612 | /* Comparison function. Takes care of calling a user-supplied |
| 613 | comparison function (any callable Python object). Calls the |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 614 | standard comparison function, PyObject_Compare(), if the user- |
| 615 | supplied function is NULL. */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 616 | |
| 617 | static int |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 618 | docompare(x, y, compare) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 619 | PyObject *x; |
| 620 | PyObject *y; |
| 621 | PyObject *compare; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 622 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 623 | PyObject *args, *res; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 624 | int i; |
| 625 | |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 626 | if (compare == NULL) { |
| 627 | i = PyObject_Compare(x, y); |
| 628 | if (i && PyErr_Occurred()) |
| 629 | i = CMPERROR; |
| 630 | return i; |
| 631 | } |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 632 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 633 | args = Py_BuildValue("(OO)", x, y); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 634 | if (args == NULL) |
| 635 | return CMPERROR; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 636 | res = PyEval_CallObject(compare, args); |
| 637 | Py_DECREF(args); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 638 | if (res == NULL) |
| 639 | return CMPERROR; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 640 | if (!PyInt_Check(res)) { |
| 641 | Py_DECREF(res); |
| 642 | PyErr_SetString(PyExc_TypeError, |
| 643 | "comparison function should return int"); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 644 | return CMPERROR; |
| 645 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 646 | i = PyInt_AsLong(res); |
| 647 | Py_DECREF(res); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 648 | if (i < 0) |
| 649 | return -1; |
| 650 | if (i > 0) |
| 651 | return 1; |
| 652 | return 0; |
| 653 | } |
| 654 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 655 | /* MINSIZE is the smallest array that will get a full-blown samplesort |
| 656 | treatment; smaller arrays are sorted using binary insertion. It must |
| 657 | be at least 7 for the samplesort implementation to work. Binary |
| 658 | insertion does fewer compares, but can suffer O(N**2) data movement. |
| 659 | The more expensive compares, the larger MINSIZE should be. */ |
| 660 | #define MINSIZE 100 |
| 661 | |
| 662 | /* MINPARTITIONSIZE is the smallest array slice samplesort will bother to |
| 663 | partition; smaller slices are passed to binarysort. It must be at |
| 664 | least 2, and no larger than MINSIZE. Setting it higher reduces the # |
| 665 | of compares slowly, but increases the amount of data movement quickly. |
| 666 | The value here was chosen assuming a compare costs ~25x more than |
| 667 | swapping a pair of memory-resident pointers -- but under that assumption, |
| 668 | changing the value by a few dozen more or less has aggregate effect |
| 669 | under 1%. So the value is crucial, but not touchy <wink>. */ |
| 670 | #define MINPARTITIONSIZE 40 |
| 671 | |
| 672 | /* MAXMERGE is the largest number of elements we'll always merge into |
| 673 | a known-to-be sorted chunk via binary insertion, regardless of the |
| 674 | size of that chunk. Given a chunk of N sorted elements, and a group |
| 675 | of K unknowns, the largest K for which it's better to do insertion |
| 676 | (than a full-blown sort) is a complicated function of N and K mostly |
| 677 | involving the expected number of compares and data moves under each |
| 678 | approach, and the relative cost of those operations on a specific |
| 679 | architecure. The fixed value here is conservative, and should be a |
| 680 | clear win regardless of architecture or N. */ |
| 681 | #define MAXMERGE 15 |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 682 | |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 683 | /* 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] | 684 | this allows us to sort arrays of size N where |
| 685 | N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough |
| 686 | for arrays of size 2**64. Because we push the biggest partition |
| 687 | first, the worst case occurs when all subarrays are always partitioned |
| 688 | exactly in two. */ |
| 689 | #define STACKSIZE 60 |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 690 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 691 | |
| 692 | #define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail |
| 693 | |
| 694 | /* binarysort is the best method for sorting small arrays: it does |
| 695 | few compares, but can do data movement quadratic in the number of |
| 696 | elements. |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 697 | [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] | 698 | binary insertion. |
| 699 | On entry, must have lo <= start <= hi, and that [lo, start) is already |
| 700 | sorted (pass start == lo if you don't know!). |
| 701 | If docompare complains (returns CMPERROR) return -1, else 0. |
| 702 | Even in case of error, the output slice will be some permutation of |
| 703 | the input (nothing is lost or duplicated). |
| 704 | */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 705 | |
| 706 | static int |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 707 | binarysort(lo, hi, start, compare) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 708 | PyObject **lo; |
| 709 | PyObject **hi; |
| 710 | PyObject **start; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 711 | PyObject *compare;/* Comparison function object, or NULL for default */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 712 | { |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 713 | /* assert lo <= start <= hi |
| 714 | assert [lo, start) is sorted */ |
| 715 | register int k; |
| 716 | register PyObject **l, **p, **r; |
| 717 | register PyObject *pivot; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 718 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 719 | if (lo == start) |
| 720 | ++start; |
| 721 | for (; start < hi; ++start) { |
| 722 | /* set l to where *start belongs */ |
| 723 | l = lo; |
| 724 | r = start; |
Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 725 | pivot = *r; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 726 | do { |
| 727 | p = l + ((r - l) >> 1); |
| 728 | SETK(pivot, *p); |
| 729 | if (k < 0) |
| 730 | r = p; |
| 731 | else |
| 732 | l = p + 1; |
| 733 | } while (l < r); |
| 734 | /* Pivot should go at l -- slide over to make room. |
| 735 | Caution: using memmove is much slower under MSVC 5; |
| 736 | we're not usually moving many slots. */ |
| 737 | for (p = start; p > l; --p) |
| 738 | *p = *(p-1); |
| 739 | *l = pivot; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 740 | } |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 741 | return 0; |
Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 742 | |
| 743 | fail: |
| 744 | return -1; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | /* samplesortslice is the sorting workhorse. |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 748 | [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] | 749 | On entry, must have lo <= hi, |
| 750 | If docompare complains (returns CMPERROR) return -1, else 0. |
| 751 | Even in case of error, the output slice will be some permutation of |
| 752 | the input (nothing is lost or duplicated). |
| 753 | |
| 754 | samplesort is basically quicksort on steroids: a power of 2 close |
| 755 | to n/ln(n) is computed, and that many elements (less 1) are picked at |
| 756 | random from the array and sorted. These 2**k - 1 elements are then |
| 757 | used as preselected pivots for an equal number of quicksort |
| 758 | partitioning steps, partitioning the slice into 2**k chunks each of |
| 759 | size about ln(n). These small final chunks are then usually handled |
| 760 | by binarysort. Note that when k=1, this is roughly the same as an |
| 761 | ordinary quicksort using a random pivot, and when k=2 this is roughly |
| 762 | a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes |
| 763 | this a "median of n/ln(n)" quicksort. You can also view it as a kind |
| 764 | of bucket sort, where 2**k-1 bucket boundaries are picked dynamically. |
| 765 | |
| 766 | The large number of samples makes a quadratic-time case almost |
| 767 | impossible, and asymptotically drives the average-case number of |
| 768 | compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of- |
| 769 | 3 variant) down to N lg N. |
| 770 | |
| 771 | We also play lots of low-level tricks to cut the number of compares. |
| 772 | |
| 773 | Very obscure: To avoid using extra memory, the PPs are stored in the |
| 774 | array and shuffled around as partitioning proceeds. At the start of a |
| 775 | partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order, |
| 776 | adjacent (either on the left or the right!) to a chunk of X elements |
| 777 | that are to be partitioned: P X or X P. In either case we need to |
| 778 | shuffle things *in place* so that the 2**(m-1) smaller PPs are on the |
| 779 | left, followed by the PP to be used for this step (that's the middle |
| 780 | of the PPs), followed by X, followed by the 2**(m-1) larger PPs: |
| 781 | P X or X P -> Psmall pivot X Plarge |
| 782 | and the order of the PPs must not be altered. It can take a while |
| 783 | to realize this isn't trivial! It can take even longer <wink> to |
| 784 | understand why the simple code below works, using only 2**(m-1) swaps. |
| 785 | The key is that the order of the X elements isn't necessarily |
| 786 | preserved: X can end up as some cyclic permutation of its original |
| 787 | order. That's OK, because X is unsorted anyway. If the order of X |
| 788 | had to be preserved too, the simplest method I know of using O(1) |
| 789 | scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes. |
| 790 | Since len(X) is typically several times larger than 2**(m-1), that |
| 791 | would slow things down. |
| 792 | */ |
| 793 | |
| 794 | struct SamplesortStackNode { |
| 795 | /* Represents a slice of the array, from (& including) lo up |
| 796 | to (but excluding) hi. "extra" additional & adjacent elements |
| 797 | are pre-selected pivots (PPs), spanning [lo-extra, lo) if |
| 798 | extra > 0, or [hi, hi-extra) if extra < 0. The PPs are |
| 799 | already sorted, but nothing is known about the other elements |
| 800 | in [lo, hi). |extra| is always one less than a power of 2. |
| 801 | When extra is 0, we're out of PPs, and the slice must be |
| 802 | sorted by some other means. */ |
| 803 | PyObject **lo; |
| 804 | PyObject **hi; |
| 805 | int extra; |
| 806 | }; |
| 807 | |
| 808 | /* 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] | 809 | 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] | 810 | is undesirable, so cutoff values are canned in the "cutoff" table |
| 811 | below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */ |
| 812 | #define CUTOFFBASE 4 |
| 813 | static int cutoff[] = { |
| 814 | 43, /* smallest N such that k == 4 */ |
| 815 | 106, /* etc */ |
| 816 | 250, |
| 817 | 576, |
| 818 | 1298, |
| 819 | 2885, |
| 820 | 6339, |
| 821 | 13805, |
| 822 | 29843, |
| 823 | 64116, |
| 824 | 137030, |
| 825 | 291554, |
| 826 | 617916, |
| 827 | 1305130, |
| 828 | 2748295, |
| 829 | 5771662, |
| 830 | 12091672, |
| 831 | 25276798, |
| 832 | 52734615, |
| 833 | 109820537, |
| 834 | 228324027, |
| 835 | 473977813, |
| 836 | 982548444, /* smallest N such that k == 26 */ |
| 837 | 2034159050 /* largest N that fits in signed 32-bit; k == 27 */ |
| 838 | }; |
| 839 | |
| 840 | static int |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 841 | samplesortslice(lo, hi, compare) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 842 | PyObject **lo; |
| 843 | PyObject **hi; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 844 | PyObject *compare;/* Comparison function object, or NULL for default */ |
| 845 | { |
| 846 | register PyObject **l, **r; |
| 847 | register PyObject *tmp, *pivot; |
| 848 | register int k; |
| 849 | int n, extra, top, extraOnRight; |
| 850 | struct SamplesortStackNode stack[STACKSIZE]; |
| 851 | |
| 852 | /* assert lo <= hi */ |
| 853 | n = hi - lo; |
| 854 | |
| 855 | /* ---------------------------------------------------------- |
| 856 | * Special cases |
| 857 | * --------------------------------------------------------*/ |
| 858 | if (n < 2) |
| 859 | return 0; |
| 860 | |
| 861 | /* Set r to the largest value such that [lo,r) is sorted. |
| 862 | This catches the already-sorted case, the all-the-same |
| 863 | case, and the appended-a-few-elements-to-a-sorted-list case. |
| 864 | If the array is unsorted, we're very likely to get out of |
| 865 | the loop fast, so the test is cheap if it doesn't pay off. |
| 866 | */ |
| 867 | /* assert lo < hi */ |
| 868 | for (r = lo+1; r < hi; ++r) { |
| 869 | SETK(*r, *(r-1)); |
| 870 | if (k < 0) |
| 871 | break; |
| 872 | } |
| 873 | /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are |
| 874 | few unknowns, or few elements in total. */ |
| 875 | if (hi - r <= MAXMERGE || n < MINSIZE) |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 876 | return binarysort(lo, hi, r, compare); |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 877 | |
| 878 | /* Check for the array already being reverse-sorted. Typical |
| 879 | benchmark-driven silliness <wink>. */ |
| 880 | /* assert lo < hi */ |
| 881 | for (r = lo+1; r < hi; ++r) { |
| 882 | SETK(*(r-1), *r); |
| 883 | if (k < 0) |
| 884 | break; |
| 885 | } |
| 886 | if (hi - r <= MAXMERGE) { |
| 887 | /* Reverse the reversed prefix, then insert the tail */ |
| 888 | PyObject **originalr = r; |
| 889 | l = lo; |
| 890 | do { |
| 891 | --r; |
| 892 | tmp = *l; *l = *r; *r = tmp; |
| 893 | ++l; |
| 894 | } while (l < r); |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 895 | return binarysort(lo, hi, originalr, compare); |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /* ---------------------------------------------------------- |
| 899 | * Normal case setup: a large array without obvious pattern. |
| 900 | * --------------------------------------------------------*/ |
| 901 | |
| 902 | /* extra := a power of 2 ~= n/ln(n), less 1. |
| 903 | First find the smallest extra s.t. n < cutoff[extra] */ |
| 904 | for (extra = 0; |
| 905 | extra < sizeof(cutoff) / sizeof(cutoff[0]); |
| 906 | ++extra) { |
| 907 | if (n < cutoff[extra]) |
| 908 | break; |
| 909 | /* note that if we fall out of the loop, the value of |
| 910 | extra still makes *sense*, but may be smaller than |
| 911 | we would like (but the array has more than ~= 2**31 |
| 912 | elements in this case!) */ |
| 913 | } |
| 914 | /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can |
| 915 | have is CUTOFFBASE-1, so |
| 916 | assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */ |
| 917 | extra = (1 << (extra - 1 + CUTOFFBASE)) - 1; |
| 918 | /* assert extra > 0 and n >= extra */ |
| 919 | |
| 920 | /* Swap that many values to the start of the array. The |
| 921 | selection of elements is pseudo-random, but the same on |
| 922 | every run (this is intentional! timing algorithm changes is |
| 923 | a pain if timing varies across runs). */ |
| 924 | { |
| 925 | unsigned int seed = n / extra; /* arbitrary */ |
| 926 | unsigned int i; |
| 927 | for (i = 0; i < (unsigned)extra; ++i) { |
| 928 | /* j := random int in [i, n) */ |
| 929 | unsigned int j; |
| 930 | seed = seed * 69069 + 7; |
| 931 | j = i + seed % (n - i); |
| 932 | tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | /* Recursively sort the preselected pivots. */ |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 937 | if (samplesortslice(lo, lo + extra, compare) < 0) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 938 | goto fail; |
| 939 | |
| 940 | top = 0; /* index of available stack slot */ |
| 941 | lo += extra; /* point to first unknown */ |
| 942 | extraOnRight = 0; /* the PPs are at the left end */ |
| 943 | |
| 944 | /* ---------------------------------------------------------- |
| 945 | * Partition [lo, hi), and repeat until out of work. |
| 946 | * --------------------------------------------------------*/ |
| 947 | for (;;) { |
| 948 | /* assert lo <= hi, so n >= 0 */ |
| 949 | n = hi - lo; |
| 950 | |
| 951 | /* We may not want, or may not be able, to partition: |
| 952 | If n is small, it's quicker to insert. |
| 953 | If extra is 0, we're out of pivots, and *must* use |
| 954 | another method. |
| 955 | */ |
| 956 | if (n < MINPARTITIONSIZE || extra == 0) { |
| 957 | if (n >= MINSIZE) { |
| 958 | /* assert extra == 0 |
| 959 | This is rare, since the average size |
| 960 | of a final block is only about |
| 961 | ln(original n). */ |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 962 | if (samplesortslice(lo, hi, compare) < 0) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 963 | goto fail; |
| 964 | } |
| 965 | else { |
| 966 | /* Binary insertion should be quicker, |
| 967 | and we can take advantage of the PPs |
| 968 | already being sorted. */ |
| 969 | if (extraOnRight && extra) { |
| 970 | /* swap the PPs to the left end */ |
| 971 | k = extra; |
| 972 | do { |
| 973 | tmp = *lo; |
| 974 | *lo = *hi; |
| 975 | *hi = tmp; |
| 976 | ++lo; ++hi; |
| 977 | } while (--k); |
| 978 | } |
| 979 | if (binarysort(lo - extra, hi, lo, |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 980 | compare) < 0) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 981 | goto fail; |
| 982 | } |
| 983 | |
| 984 | /* Find another slice to work on. */ |
| 985 | if (--top < 0) |
| 986 | break; /* no more -- done! */ |
| 987 | lo = stack[top].lo; |
| 988 | hi = stack[top].hi; |
| 989 | extra = stack[top].extra; |
| 990 | extraOnRight = 0; |
| 991 | if (extra < 0) { |
| 992 | extraOnRight = 1; |
| 993 | extra = -extra; |
| 994 | } |
| 995 | continue; |
| 996 | } |
| 997 | |
| 998 | /* Pretend the PPs are indexed 0, 1, ..., extra-1. |
| 999 | Then our preselected pivot is at (extra-1)/2, and we |
| 1000 | want to move the PPs before that to the left end of |
| 1001 | the slice, and the PPs after that to the right end. |
| 1002 | The following section changes extra, lo, hi, and the |
| 1003 | slice such that: |
| 1004 | [lo-extra, lo) contains the smaller PPs. |
| 1005 | *lo == our PP. |
| 1006 | (lo, hi) contains the unknown elements. |
| 1007 | [hi, hi+extra) contains the larger PPs. |
| 1008 | */ |
| 1009 | k = extra >>= 1; /* num PPs to move */ |
| 1010 | if (extraOnRight) { |
| 1011 | /* Swap the smaller PPs to the left end. |
| 1012 | Note that this loop actually moves k+1 items: |
| 1013 | the last is our PP */ |
| 1014 | do { |
| 1015 | tmp = *lo; *lo = *hi; *hi = tmp; |
| 1016 | ++lo; ++hi; |
| 1017 | } while (k--); |
| 1018 | } |
| 1019 | else { |
| 1020 | /* Swap the larger PPs to the right end. */ |
| 1021 | while (k--) { |
| 1022 | --lo; --hi; |
| 1023 | tmp = *lo; *lo = *hi; *hi = tmp; |
| 1024 | } |
| 1025 | } |
| 1026 | --lo; /* *lo is now our PP */ |
| 1027 | pivot = *lo; |
| 1028 | |
| 1029 | /* Now an almost-ordinary quicksort partition step. |
| 1030 | Note that most of the time is spent here! |
| 1031 | Only odd thing is that we partition into < and >=, |
| 1032 | instead of the usual <= and >=. This helps when |
| 1033 | there are lots of duplicates of different values, |
| 1034 | because it eventually tends to make subfiles |
| 1035 | "pure" (all duplicates), and we special-case for |
| 1036 | duplicates later. */ |
| 1037 | l = lo + 1; |
| 1038 | r = hi - 1; |
| 1039 | /* assert lo < l < r < hi (small n weeded out above) */ |
| 1040 | |
| 1041 | do { |
| 1042 | /* slide l right, looking for key >= pivot */ |
| 1043 | do { |
| 1044 | SETK(*l, pivot); |
| 1045 | if (k < 0) |
| 1046 | ++l; |
| 1047 | else |
| 1048 | break; |
| 1049 | } while (l < r); |
| 1050 | |
| 1051 | /* slide r left, looking for key < pivot */ |
| 1052 | while (l < r) { |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1053 | register PyObject *rval = *r--; |
| 1054 | SETK(rval, pivot); |
| 1055 | if (k < 0) { |
| 1056 | /* swap and advance */ |
| 1057 | r[1] = *l; |
| 1058 | *l++ = rval; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1059 | break; |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1060 | } |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | } while (l < r); |
| 1064 | |
| 1065 | /* assert lo < r <= l < hi |
| 1066 | assert r == l or r+1 == l |
| 1067 | everything to the left of l is < pivot, and |
| 1068 | everything to the right of r is >= pivot */ |
| 1069 | |
| 1070 | if (l == r) { |
| 1071 | SETK(*r, pivot); |
| 1072 | if (k < 0) |
| 1073 | ++l; |
| 1074 | else |
| 1075 | --r; |
| 1076 | } |
| 1077 | /* assert lo <= r and r+1 == l and l <= hi |
| 1078 | assert r == lo or a[r] < pivot |
| 1079 | assert a[lo] is pivot |
| 1080 | assert l == hi or a[l] >= pivot |
| 1081 | Swap the pivot into "the middle", so we can henceforth |
| 1082 | ignore it. |
| 1083 | */ |
| 1084 | *lo = *r; |
| 1085 | *r = pivot; |
| 1086 | |
| 1087 | /* The following is true now, & will be preserved: |
| 1088 | All in [lo,r) are < pivot |
| 1089 | All in [r,l) == pivot (& so can be ignored) |
| 1090 | All in [l,hi) are >= pivot */ |
| 1091 | |
| 1092 | /* Check for duplicates of the pivot. One compare is |
| 1093 | wasted if there are no duplicates, but can win big |
| 1094 | when there are. |
| 1095 | Tricky: we're sticking to "<" compares, so deduce |
| 1096 | equality indirectly. We know pivot <= *l, so they're |
| 1097 | equal iff not pivot < *l. |
| 1098 | */ |
| 1099 | while (l < hi) { |
| 1100 | /* pivot <= *l known */ |
| 1101 | SETK(pivot, *l); |
| 1102 | if (k < 0) |
| 1103 | break; |
| 1104 | else |
| 1105 | /* <= and not < implies == */ |
| 1106 | ++l; |
| 1107 | } |
| 1108 | |
| 1109 | /* assert lo <= r < l <= hi |
| 1110 | Partitions are [lo, r) and [l, hi) */ |
| 1111 | |
| 1112 | /* push fattest first; remember we still have extra PPs |
| 1113 | to the left of the left chunk and to the right of |
| 1114 | the right chunk! */ |
| 1115 | /* assert top < STACKSIZE */ |
| 1116 | if (r - lo <= hi - l) { |
| 1117 | /* second is bigger */ |
| 1118 | stack[top].lo = l; |
| 1119 | stack[top].hi = hi; |
| 1120 | stack[top].extra = -extra; |
| 1121 | hi = r; |
| 1122 | extraOnRight = 0; |
| 1123 | } |
| 1124 | else { |
| 1125 | /* first is bigger */ |
| 1126 | stack[top].lo = lo; |
| 1127 | stack[top].hi = r; |
| 1128 | stack[top].extra = extra; |
| 1129 | lo = l; |
| 1130 | extraOnRight = 1; |
| 1131 | } |
| 1132 | ++top; |
| 1133 | |
| 1134 | } /* end of partitioning loop */ |
| 1135 | |
| 1136 | return 0; |
| 1137 | |
| 1138 | fail: |
| 1139 | return -1; |
| 1140 | } |
Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 1141 | |
| 1142 | #undef SETK |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1143 | |
| 1144 | staticforward PyTypeObject immutable_list_type; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1145 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1146 | static PyObject * |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1147 | listsort(self, compare) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1148 | PyListObject *self; |
| 1149 | PyObject *compare; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1150 | { |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1151 | int err; |
| 1152 | |
| 1153 | self->ob_type = &immutable_list_type; |
| 1154 | err = samplesortslice(self->ob_item, |
| 1155 | self->ob_item + self->ob_size, |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1156 | compare); |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1157 | self->ob_type = &PyList_Type; |
| 1158 | if (err < 0) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1159 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1160 | Py_INCREF(Py_None); |
| 1161 | return Py_None; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1164 | int |
| 1165 | PyList_Sort(v) |
| 1166 | PyObject *v; |
| 1167 | { |
| 1168 | if (v == NULL || !PyList_Check(v)) { |
| 1169 | PyErr_BadInternalCall(); |
| 1170 | return -1; |
| 1171 | } |
| 1172 | v = listsort((PyListObject *)v, (PyObject *)NULL); |
| 1173 | if (v == NULL) |
| 1174 | return -1; |
| 1175 | Py_DECREF(v); |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1179 | static PyObject * |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1180 | listreverse(self, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1181 | PyListObject *self; |
| 1182 | PyObject *args; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1183 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1184 | register PyObject **p, **q; |
| 1185 | register PyObject *tmp; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1186 | |
| 1187 | if (args != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1188 | PyErr_BadArgument(); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1189 | return NULL; |
| 1190 | } |
| 1191 | |
| 1192 | if (self->ob_size > 1) { |
| 1193 | for (p = self->ob_item, q = self->ob_item + self->ob_size - 1; |
| 1194 | p < q; p++, q--) { |
| 1195 | tmp = *p; |
| 1196 | *p = *q; |
| 1197 | *q = tmp; |
| 1198 | } |
| 1199 | } |
| 1200 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1201 | Py_INCREF(Py_None); |
| 1202 | return Py_None; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Guido van Rossum | 84c76f5 | 1990-10-30 13:32:20 +0000 | [diff] [blame] | 1205 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1206 | PyList_Reverse(v) |
| 1207 | PyObject *v; |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1208 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1209 | if (v == NULL || !PyList_Check(v)) { |
| 1210 | PyErr_BadInternalCall(); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1211 | return -1; |
| 1212 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1213 | v = listreverse((PyListObject *)v, (PyObject *)NULL); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1214 | if (v == NULL) |
| 1215 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1216 | Py_DECREF(v); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1217 | return 0; |
| 1218 | } |
| 1219 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1220 | PyObject * |
| 1221 | PyList_AsTuple(v) |
| 1222 | PyObject *v; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1223 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1224 | PyObject *w; |
| 1225 | PyObject **p; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1226 | int n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1227 | if (v == NULL || !PyList_Check(v)) { |
| 1228 | PyErr_BadInternalCall(); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1229 | return NULL; |
| 1230 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1231 | n = ((PyListObject *)v)->ob_size; |
| 1232 | w = PyTuple_New(n); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1233 | if (w == NULL) |
| 1234 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1235 | p = ((PyTupleObject *)w)->ob_item; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1236 | memcpy((ANY *)p, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1237 | (ANY *)((PyListObject *)v)->ob_item, |
| 1238 | n*sizeof(PyObject *)); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1239 | while (--n >= 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1240 | Py_INCREF(*p); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1241 | p++; |
| 1242 | } |
| 1243 | return w; |
| 1244 | } |
| 1245 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1246 | static PyObject * |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1247 | listindex(self, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1248 | PyListObject *self; |
| 1249 | PyObject *args; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1250 | { |
| 1251 | int i; |
| 1252 | |
| 1253 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1254 | PyErr_BadArgument(); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1255 | return NULL; |
| 1256 | } |
| 1257 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1258 | if (PyObject_Compare(self->ob_item[i], args) == 0) |
| 1259 | return PyInt_FromLong((long)i); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1260 | if (PyErr_Occurred()) |
| 1261 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1262 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1263 | PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list"); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1264 | return NULL; |
| 1265 | } |
| 1266 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1267 | static PyObject * |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1268 | listcount(self, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1269 | PyListObject *self; |
| 1270 | PyObject *args; |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1271 | { |
| 1272 | int count = 0; |
| 1273 | int i; |
| 1274 | |
| 1275 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1276 | PyErr_BadArgument(); |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1277 | return NULL; |
| 1278 | } |
| 1279 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1280 | if (PyObject_Compare(self->ob_item[i], args) == 0) |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1281 | count++; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1282 | if (PyErr_Occurred()) |
| 1283 | return NULL; |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1284 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1285 | return PyInt_FromLong((long)count); |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1288 | static PyObject * |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1289 | listremove(self, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1290 | PyListObject *self; |
| 1291 | PyObject *args; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1292 | { |
| 1293 | int i; |
| 1294 | |
| 1295 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1296 | PyErr_BadArgument(); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1297 | return NULL; |
| 1298 | } |
| 1299 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1300 | if (PyObject_Compare(self->ob_item[i], args) == 0) { |
| 1301 | if (list_ass_slice(self, i, i+1, |
| 1302 | (PyObject *)NULL) != 0) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1303 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1304 | Py_INCREF(Py_None); |
| 1305 | return Py_None; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1306 | } |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1307 | if (PyErr_Occurred()) |
| 1308 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1309 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1310 | PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list"); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1311 | return NULL; |
| 1312 | } |
| 1313 | |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 1314 | static char append_doc[] = |
| 1315 | "L.append(object) -- append object to end"; |
| 1316 | static char insert_doc[] = |
| 1317 | "L.insert(index, object) -- insert object before index"; |
| 1318 | static char pop_doc[] = |
| 1319 | "L.pop([index]) -> item -- remove and return item at index (default last)"; |
| 1320 | static char remove_doc[] = |
| 1321 | "L.remove(value) -- remove first occurrence of value"; |
| 1322 | static char index_doc[] = |
| 1323 | "L.index(value) -> integer -- return index of first occurrence of value"; |
| 1324 | static char count_doc[] = |
| 1325 | "L.count(value) -> integer -- return number of occurrences of value"; |
| 1326 | static char reverse_doc[] = |
| 1327 | "L.reverse() -- reverse *IN PLACE*"; |
| 1328 | static char sort_doc[] = |
| 1329 | "L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1"; |
| 1330 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1331 | static PyMethodDef list_methods[] = { |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 1332 | {"append", (PyCFunction)listappend, 0, append_doc}, |
| 1333 | {"insert", (PyCFunction)listinsert, 0, insert_doc}, |
| 1334 | {"pop", (PyCFunction)listpop, 1, pop_doc}, |
| 1335 | {"remove", (PyCFunction)listremove, 0, remove_doc}, |
| 1336 | {"index", (PyCFunction)listindex, 0, index_doc}, |
| 1337 | {"count", (PyCFunction)listcount, 0, count_doc}, |
| 1338 | {"reverse", (PyCFunction)listreverse, 0, reverse_doc}, |
| 1339 | {"sort", (PyCFunction)listsort, 0, sort_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1340 | {NULL, NULL} /* sentinel */ |
| 1341 | }; |
| 1342 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1343 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1344 | list_getattr(f, name) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1345 | PyListObject *f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1346 | char *name; |
| 1347 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1348 | return Py_FindMethod(list_methods, (PyObject *)f, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1351 | static PySequenceMethods list_as_sequence = { |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1352 | (inquiry)list_length, /*sq_length*/ |
| 1353 | (binaryfunc)list_concat, /*sq_concat*/ |
| 1354 | (intargfunc)list_repeat, /*sq_repeat*/ |
| 1355 | (intargfunc)list_item, /*sq_item*/ |
| 1356 | (intintargfunc)list_slice, /*sq_slice*/ |
| 1357 | (intobjargproc)list_ass_item, /*sq_ass_item*/ |
| 1358 | (intintobjargproc)list_ass_slice, /*sq_ass_slice*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1359 | }; |
| 1360 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1361 | PyTypeObject PyList_Type = { |
| 1362 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1363 | 0, |
| 1364 | "list", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1365 | sizeof(PyListObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1366 | 0, |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1367 | (destructor)list_dealloc, /*tp_dealloc*/ |
| 1368 | (printfunc)list_print, /*tp_print*/ |
| 1369 | (getattrfunc)list_getattr, /*tp_getattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1370 | 0, /*tp_setattr*/ |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1371 | (cmpfunc)list_compare, /*tp_compare*/ |
| 1372 | (reprfunc)list_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1373 | 0, /*tp_as_number*/ |
| 1374 | &list_as_sequence, /*tp_as_sequence*/ |
| 1375 | 0, /*tp_as_mapping*/ |
| 1376 | }; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1377 | |
| 1378 | |
| 1379 | /* During a sort, we really can't have anyone modifying the list; it could |
| 1380 | cause core dumps. Thus, we substitute a dummy type that raises an |
| 1381 | explanatory exception when a modifying operation is used. Caveat: |
| 1382 | comparisons may behave differently; but I guess it's a bad idea anyway to |
| 1383 | compare a list that's being sorted... */ |
| 1384 | |
| 1385 | static PyObject * |
| 1386 | immutable_list_op(/*No args!*/) |
| 1387 | { |
| 1388 | PyErr_SetString(PyExc_TypeError, |
| 1389 | "a list cannot be modified while it is being sorted"); |
| 1390 | return NULL; |
| 1391 | } |
| 1392 | |
| 1393 | static PyMethodDef immutable_list_methods[] = { |
| 1394 | {"append", (PyCFunction)immutable_list_op}, |
| 1395 | {"insert", (PyCFunction)immutable_list_op}, |
| 1396 | {"remove", (PyCFunction)immutable_list_op}, |
| 1397 | {"index", (PyCFunction)listindex}, |
| 1398 | {"count", (PyCFunction)listcount}, |
| 1399 | {"reverse", (PyCFunction)immutable_list_op}, |
| 1400 | {"sort", (PyCFunction)immutable_list_op}, |
| 1401 | {NULL, NULL} /* sentinel */ |
| 1402 | }; |
| 1403 | |
| 1404 | static PyObject * |
| 1405 | immutable_list_getattr(f, name) |
| 1406 | PyListObject *f; |
| 1407 | char *name; |
| 1408 | { |
| 1409 | return Py_FindMethod(immutable_list_methods, (PyObject *)f, name); |
| 1410 | } |
| 1411 | |
| 1412 | static int |
| 1413 | immutable_list_ass(/*No args!*/) |
| 1414 | { |
| 1415 | immutable_list_op(); |
| 1416 | return -1; |
| 1417 | } |
| 1418 | |
| 1419 | static PySequenceMethods immutable_list_as_sequence = { |
| 1420 | (inquiry)list_length, /*sq_length*/ |
| 1421 | (binaryfunc)list_concat, /*sq_concat*/ |
| 1422 | (intargfunc)list_repeat, /*sq_repeat*/ |
| 1423 | (intargfunc)list_item, /*sq_item*/ |
| 1424 | (intintargfunc)list_slice, /*sq_slice*/ |
| 1425 | (intobjargproc)immutable_list_ass, /*sq_ass_item*/ |
| 1426 | (intintobjargproc)immutable_list_ass, /*sq_ass_slice*/ |
| 1427 | }; |
| 1428 | |
| 1429 | static PyTypeObject immutable_list_type = { |
| 1430 | PyObject_HEAD_INIT(&PyType_Type) |
| 1431 | 0, |
| 1432 | "list (immutable, during sort)", |
| 1433 | sizeof(PyListObject), |
| 1434 | 0, |
| 1435 | 0, /*tp_dealloc*/ /* Cannot happen */ |
| 1436 | (printfunc)list_print, /*tp_print*/ |
| 1437 | (getattrfunc)immutable_list_getattr, /*tp_getattr*/ |
| 1438 | 0, /*tp_setattr*/ |
| 1439 | 0, /*tp_compare*/ /* Won't be called */ |
| 1440 | (reprfunc)list_repr, /*tp_repr*/ |
| 1441 | 0, /*tp_as_number*/ |
| 1442 | &immutable_list_as_sequence, /*tp_as_sequence*/ |
| 1443 | 0, /*tp_as_mapping*/ |
| 1444 | }; |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1445 | |