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; |
| 234 | fprintf(fp, "["); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 235 | for (i = 0; i < op->ob_size; i++) { |
| 236 | if (i > 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | fprintf(fp, ", "); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 238 | if (PyObject_Print(op->ob_item[i], fp, 0) != 0) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 239 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 240 | } |
| 241 | fprintf(fp, "]"); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 242 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 245 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 246 | list_repr(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 247 | PyListObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 248 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 249 | PyObject *s, *comma; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 250 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 251 | s = PyString_FromString("["); |
| 252 | comma = PyString_FromString(", "); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 253 | for (i = 0; i < v->ob_size && s != NULL; i++) { |
| 254 | if (i > 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 255 | PyString_Concat(&s, comma); |
| 256 | PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i])); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 258 | Py_XDECREF(comma); |
| 259 | PyString_ConcatAndDel(&s, PyString_FromString("]")); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 260 | return s; |
| 261 | } |
| 262 | |
| 263 | static int |
| 264 | list_compare(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 265 | PyListObject *v, *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 266 | { |
| 267 | int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size; |
| 268 | int i; |
| 269 | for (i = 0; i < len; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 270 | 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] | 271 | if (cmp != 0) |
| 272 | return cmp; |
| 273 | } |
| 274 | return v->ob_size - w->ob_size; |
| 275 | } |
| 276 | |
| 277 | static int |
| 278 | list_length(a) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 279 | PyListObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 280 | { |
| 281 | return a->ob_size; |
| 282 | } |
| 283 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 284 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 285 | list_item(a, i) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 286 | PyListObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 287 | int i; |
| 288 | { |
| 289 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 290 | if (indexerr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 291 | indexerr = PyString_FromString( |
| 292 | "list index out of range"); |
| 293 | PyErr_SetObject(PyExc_IndexError, indexerr); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 294 | return NULL; |
| 295 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 296 | Py_INCREF(a->ob_item[i]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 297 | return a->ob_item[i]; |
| 298 | } |
| 299 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 300 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 301 | list_slice(a, ilow, ihigh) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 302 | PyListObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 303 | int ilow, ihigh; |
| 304 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 305 | PyListObject *np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 306 | int i; |
| 307 | if (ilow < 0) |
| 308 | ilow = 0; |
| 309 | else if (ilow > a->ob_size) |
| 310 | ilow = a->ob_size; |
| 311 | if (ihigh < 0) |
| 312 | ihigh = 0; |
| 313 | if (ihigh < ilow) |
| 314 | ihigh = ilow; |
| 315 | else if (ihigh > a->ob_size) |
| 316 | ihigh = a->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 317 | np = (PyListObject *) PyList_New(ihigh - ilow); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | if (np == NULL) |
| 319 | return NULL; |
| 320 | for (i = ilow; i < ihigh; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 321 | PyObject *v = a->ob_item[i]; |
| 322 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 323 | np->ob_item[i - ilow] = v; |
| 324 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 325 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 328 | PyObject * |
| 329 | PyList_GetSlice(a, ilow, ihigh) |
| 330 | PyObject *a; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 331 | int ilow, ihigh; |
| 332 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 333 | if (!PyList_Check(a)) { |
| 334 | PyErr_BadInternalCall(); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 335 | return NULL; |
| 336 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 337 | return list_slice((PyListObject *)a, ilow, ihigh); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 340 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 341 | list_concat(a, bb) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 342 | PyListObject *a; |
| 343 | PyObject *bb; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 344 | { |
| 345 | int size; |
| 346 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 347 | PyListObject *np; |
| 348 | if (!PyList_Check(bb)) { |
| 349 | PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 350 | return NULL; |
| 351 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 352 | #define b ((PyListObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 353 | size = a->ob_size + b->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 354 | np = (PyListObject *) PyList_New(size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 355 | if (np == NULL) { |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 356 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 357 | } |
| 358 | for (i = 0; i < a->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 359 | PyObject *v = a->ob_item[i]; |
| 360 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 361 | np->ob_item[i] = v; |
| 362 | } |
| 363 | for (i = 0; i < b->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 364 | PyObject *v = b->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 + a->ob_size] = v; |
| 367 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 368 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 369 | #undef b |
| 370 | } |
| 371 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 372 | static PyObject * |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 373 | list_repeat(a, n) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 374 | PyListObject *a; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 375 | int n; |
| 376 | { |
| 377 | int i, j; |
| 378 | int size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 379 | PyListObject *np; |
| 380 | PyObject **p; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 381 | if (n < 0) |
| 382 | n = 0; |
| 383 | size = a->ob_size * n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 384 | np = (PyListObject *) PyList_New(size); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 385 | if (np == NULL) |
| 386 | return NULL; |
| 387 | p = np->ob_item; |
| 388 | for (i = 0; i < n; i++) { |
| 389 | for (j = 0; j < a->ob_size; j++) { |
| 390 | *p = a->ob_item[j]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 391 | Py_INCREF(*p); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 392 | p++; |
| 393 | } |
| 394 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 395 | return (PyObject *) np; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 398 | static int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 399 | list_ass_slice(a, ilow, ihigh, v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 400 | PyListObject *a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 401 | int ilow, ihigh; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 402 | 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 { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 430 | PyErr_BadArgument(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 431 | return -1; |
| 432 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 433 | if (ilow < 0) |
| 434 | ilow = 0; |
| 435 | else if (ilow > a->ob_size) |
| 436 | ilow = a->ob_size; |
| 437 | if (ihigh < 0) |
| 438 | ihigh = 0; |
| 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 | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 463 | PyMem_XDEL(recycle); |
| 464 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 465 | return -1; |
| 466 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 467 | for (k = a->ob_size; --k >= ihigh; ) |
| 468 | item[k+d] = item[k]; |
| 469 | for (/*k = ihigh-1*/; k >= ilow; --k) |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 470 | *p++ = item[k]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 471 | a->ob_item = item; |
| 472 | a->ob_size += d; |
| 473 | } |
| 474 | for (k = 0; k < n; k++, ilow++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 475 | PyObject *w = b->ob_item[k]; |
| 476 | Py_XINCREF(w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 477 | item[ilow] = w; |
| 478 | } |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 479 | if (recycle) { |
| 480 | while (--p >= recycle) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 481 | Py_XDECREF(*p); |
| 482 | PyMem_DEL(recycle); |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 483 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 484 | return 0; |
| 485 | #undef b |
| 486 | } |
| 487 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 488 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 489 | PyList_SetSlice(a, ilow, ihigh, v) |
| 490 | PyObject *a; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 491 | int ilow, ihigh; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 492 | PyObject *v; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 493 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 494 | if (!PyList_Check(a)) { |
| 495 | PyErr_BadInternalCall(); |
Guido van Rossum | 1fc238a | 1993-07-29 08:25:09 +0000 | [diff] [blame] | 496 | return -1; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 497 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 498 | return list_ass_slice((PyListObject *)a, ilow, ihigh, v); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 501 | static int |
| 502 | list_ass_item(a, i, v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 503 | PyListObject *a; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 504 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 505 | PyObject *v; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 506 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 507 | PyObject *old_value; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 508 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 509 | PyErr_SetString(PyExc_IndexError, |
| 510 | "list assignment index out of range"); |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 511 | return -1; |
| 512 | } |
| 513 | if (v == NULL) |
| 514 | return list_ass_slice(a, i, i+1, v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 515 | Py_INCREF(v); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 516 | old_value = a->ob_item[i]; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 517 | a->ob_item[i] = v; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 518 | Py_DECREF(old_value); |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 522 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 523 | ins(self, where, v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 524 | PyListObject *self; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 525 | int where; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 526 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 527 | { |
| 528 | if (ins1(self, where, v) != 0) |
| 529 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 530 | Py_INCREF(Py_None); |
| 531 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 534 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 535 | listinsert(self, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 536 | PyListObject *self; |
| 537 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 538 | { |
| 539 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 540 | PyObject *v; |
| 541 | if (!PyArg_Parse(args, "(iO)", &i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 542 | return NULL; |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 543 | return ins(self, i, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 546 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 547 | listappend(self, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 548 | PyListObject *self; |
| 549 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 550 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 551 | PyObject *v; |
| 552 | if (!PyArg_Parse(args, "O", &v)) |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 553 | return NULL; |
| 554 | return ins(self, (int) self->ob_size, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 557 | #define NEWSORT |
| 558 | |
| 559 | #ifdef NEWSORT |
| 560 | |
| 561 | /* New quicksort implementation for arrays of object pointers. |
| 562 | Thanks to discussions with Tim Peters. */ |
| 563 | |
| 564 | /* CMPERROR is returned by our comparison function when an error |
| 565 | occurred. This is the largest negative integer (0x80000000 on a |
| 566 | 32-bit system). */ |
Guido van Rossum | 19700b6 | 1997-03-05 00:45:43 +0000 | [diff] [blame] | 567 | #define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) ) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 568 | |
| 569 | /* Comparison function. Takes care of calling a user-supplied |
| 570 | comparison function (any callable Python object). Calls the |
| 571 | standard comparison function, cmpobject(), if the user-supplied |
| 572 | function is NULL. */ |
| 573 | |
| 574 | static int |
| 575 | docompare(x, y, compare) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 576 | PyObject *x; |
| 577 | PyObject *y; |
| 578 | PyObject *compare; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 579 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 580 | PyObject *args, *res; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 581 | int i; |
| 582 | |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 583 | if (compare == NULL) { |
| 584 | i = PyObject_Compare(x, y); |
| 585 | if (i && PyErr_Occurred()) |
| 586 | i = CMPERROR; |
| 587 | return i; |
| 588 | } |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 589 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 590 | args = Py_BuildValue("(OO)", x, y); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 591 | if (args == NULL) |
| 592 | return CMPERROR; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 593 | res = PyEval_CallObject(compare, args); |
| 594 | Py_DECREF(args); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 595 | if (res == NULL) |
| 596 | return CMPERROR; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 597 | if (!PyInt_Check(res)) { |
| 598 | Py_DECREF(res); |
| 599 | PyErr_SetString(PyExc_TypeError, |
| 600 | "comparison function should return int"); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 601 | return CMPERROR; |
| 602 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 603 | i = PyInt_AsLong(res); |
| 604 | Py_DECREF(res); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 605 | if (i < 0) |
| 606 | return -1; |
| 607 | if (i > 0) |
| 608 | return 1; |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | /* Straight insertion sort. More efficient for sorting small arrays. */ |
| 613 | |
| 614 | static int |
| 615 | insertionsort(array, size, compare) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 616 | PyObject **array; /* Start of array to sort */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 617 | int size; /* Number of elements to sort */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 618 | PyObject *compare;/* Comparison function object, or NULL for default */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 619 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 620 | register PyObject **a = array; |
| 621 | register PyObject **end = array+size; |
| 622 | register PyObject **p; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 623 | |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 624 | for (p = a+1; p < end; p++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 625 | register PyObject *key = *p; |
| 626 | register PyObject **q = p; |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 627 | while (--q >= a) { |
| 628 | register int k = docompare(*q, key, compare); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 629 | if (k == CMPERROR) |
| 630 | return -1; |
| 631 | if (k <= 0) |
| 632 | break; |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 633 | *(q+1) = *q; |
| 634 | *q = key; /* For consistency */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | /* MINSIZE is the smallest array we care to partition; smaller arrays |
Guido van Rossum | cc15b42 | 1996-12-16 03:32:39 +0000 | [diff] [blame] | 642 | are sorted using a straight insertion sort (above). It must be at |
| 643 | least 2 for the quicksort implementation to work. Assuming that |
| 644 | comparisons are more expensive than everything else (and this is a |
| 645 | good assumption for Python), it should be 10, which is the cutoff |
| 646 | point: quicksort requires more comparisons than insertion sort for |
| 647 | smaller arrays. */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 648 | #define MINSIZE 10 |
| 649 | |
| 650 | /* STACKSIZE is the size of our work stack. A rough estimate is that |
| 651 | this allows us to sort arrays of MINSIZE * 2**STACKSIZE, or large |
| 652 | enough. (Because of the way we push the biggest partition first, |
| 653 | the worst case occurs when all subarrays are always partitioned |
| 654 | exactly in two.) */ |
| 655 | #define STACKSIZE 64 |
| 656 | |
| 657 | /* Quicksort algorithm. Return -1 if an exception occurred; in this |
| 658 | case we leave the array partly sorted but otherwise in good health |
| 659 | (i.e. no items have been removed or duplicated). */ |
| 660 | |
| 661 | static int |
| 662 | quicksort(array, size, compare) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 663 | PyObject **array; /* Start of array to sort */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 664 | int size; /* Number of elements to sort */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 665 | PyObject *compare;/* Comparison function object, or NULL for default */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 666 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 667 | register PyObject *tmp, *pivot; |
| 668 | register PyObject **lo, **hi, **l, **r; |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 669 | int top, k, n, n2; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 670 | PyObject **lostack[STACKSIZE]; |
| 671 | PyObject **histack[STACKSIZE]; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 672 | |
| 673 | /* Start out with the whole array on the work stack */ |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 674 | lostack[0] = array; |
| 675 | histack[0] = array+size; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 676 | top = 1; |
| 677 | |
| 678 | /* Repeat until the work stack is empty */ |
| 679 | while (--top >= 0) { |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 680 | lo = lostack[top]; |
| 681 | hi = histack[top]; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 682 | |
| 683 | /* If it's a small one, use straight insertion sort */ |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 684 | n = hi - lo; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 685 | if (n < MINSIZE) { |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 686 | /* |
| 687 | * skip it. The insertion sort at the end will |
| 688 | * catch these |
| 689 | */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 690 | continue; |
| 691 | } |
| 692 | |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 693 | /* Choose median of first, middle and last item as pivot */ |
| 694 | |
| 695 | l = lo + (n>>1); /* Middle */ |
| 696 | r = hi - 1; /* Last */ |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 697 | |
| 698 | k = docompare(*l, *lo, compare); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 699 | if (k == CMPERROR) |
| 700 | return -1; |
| 701 | if (k < 0) |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 702 | { tmp = *lo; *lo = *l; *l = tmp; } |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 703 | |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 704 | k = docompare(*r, *l, compare); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 705 | if (k == CMPERROR) |
| 706 | return -1; |
| 707 | if (k < 0) |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 708 | { tmp = *r; *r = *l; *l = tmp; } |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 709 | |
| 710 | k = docompare(*l, *lo, compare); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 711 | if (k == CMPERROR) |
| 712 | return -1; |
| 713 | if (k < 0) |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 714 | { tmp = *l; *l = *lo; *lo = tmp; } |
| 715 | pivot = *l; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 716 | |
| 717 | /* Partition the array */ |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 718 | l = lo+1; |
| 719 | r = hi-2; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 720 | for (;;) { |
| 721 | /* Move left index to element > pivot */ |
Guido van Rossum | 044b9dc | 1998-02-25 17:50:03 +0000 | [diff] [blame] | 722 | while (l < hi) { |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 723 | k = docompare(*l, pivot, compare); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 724 | if (k == CMPERROR) |
| 725 | return -1; |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 726 | if (k >= 0) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 727 | break; |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 728 | l++; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 729 | } |
| 730 | /* Move right index to element < pivot */ |
Guido van Rossum | 044b9dc | 1998-02-25 17:50:03 +0000 | [diff] [blame] | 731 | while (r >= lo) { |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 732 | k = docompare(pivot, *r, compare); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 733 | if (k == CMPERROR) |
| 734 | return -1; |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 735 | if (k >= 0) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 736 | break; |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 737 | r--; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 738 | } |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 739 | |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 740 | /* If they met, we're through */ |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 741 | if (l < r) { |
| 742 | /* Swap elements and continue */ |
| 743 | tmp = *l; *l = *r; *r = tmp; |
| 744 | l++; r--; |
| 745 | } |
| 746 | else if (l == r) { |
| 747 | l++; r--; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 748 | break; |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | if (l > r) |
| 752 | break; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 755 | |
| 756 | /* We have now reached the following conditions: |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 757 | lo <= r < l <= hi |
| 758 | all x in [lo,r) are <= pivot |
| 759 | all x in [r,l) are == pivot |
| 760 | all x in [l,hi) are >= pivot |
| 761 | The partitions are [lo,r) and [l,hi) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 762 | */ |
| 763 | |
| 764 | /* Push biggest partition first */ |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 765 | n = r - lo; |
| 766 | n2 = hi - l; |
| 767 | if (n > n2) { |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 768 | /* First one is bigger */ |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 769 | if (n > MINSIZE) { |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 770 | lostack[top] = lo; |
| 771 | histack[top++] = r; |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 772 | if (n2 > MINSIZE) { |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 773 | lostack[top] = l; |
| 774 | histack[top++] = hi; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | } else { |
| 778 | /* Second one is bigger */ |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 779 | if (n2 > MINSIZE) { |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 780 | lostack[top] = l; |
| 781 | histack[top++] = hi; |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 782 | if (n > MINSIZE) { |
Guido van Rossum | 3176bb1 | 1996-12-11 23:57:39 +0000 | [diff] [blame] | 783 | lostack[top] = lo; |
| 784 | histack[top++] = r; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /* Should assert top < STACKSIZE-1 */ |
| 790 | } |
Guido van Rossum | 24e62e2 | 1997-12-10 15:14:24 +0000 | [diff] [blame] | 791 | |
| 792 | /* |
| 793 | * Ouch - even if I screwed up the quicksort above, the |
| 794 | * insertionsort below will cover up the problem - just a |
| 795 | * performance hit would be noticable. |
| 796 | */ |
| 797 | |
| 798 | /* insertionsort is pretty fast on the partially sorted list */ |
| 799 | |
| 800 | if (insertionsort(array, size, compare) < 0) |
| 801 | return -1; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 802 | |
| 803 | /* Succes */ |
| 804 | return 0; |
| 805 | } |
| 806 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 807 | static PyObject * |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 808 | listsort(self, compare) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 809 | PyListObject *self; |
| 810 | PyObject *compare; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 811 | { |
| 812 | /* XXX Don't you *dare* changing the list's length in compare()! */ |
| 813 | if (quicksort(self->ob_item, self->ob_size, compare) < 0) |
| 814 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 815 | Py_INCREF(Py_None); |
| 816 | return Py_None; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | #else /* !NEWSORT */ |
| 820 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 821 | static PyObject *comparefunc; |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 822 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 823 | static int |
| 824 | cmp(v, w) |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 825 | const ANY *v, *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 826 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 827 | PyObject *t, *res; |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 828 | long i; |
| 829 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 830 | if (PyErr_Occurred()) |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 831 | return 0; |
| 832 | |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 833 | if (comparefunc == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 834 | return PyObject_Compare(* (PyObject **) v, * (PyObject **) w); |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 835 | |
| 836 | /* Call the user-supplied comparison function */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 837 | t = Py_BuildValue("(OO)", * (PyObject **) v, * (PyObject **) w); |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 838 | if (t == NULL) |
| 839 | return 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 840 | res = PyEval_CallObject(comparefunc, t); |
| 841 | Py_DECREF(t); |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 842 | if (res == NULL) |
| 843 | return 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 844 | if (!PyInt_Check(res)) { |
| 845 | PyErr_SetString(PyExc_TypeError, |
| 846 | "comparison function should return int"); |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 847 | i = 0; |
| 848 | } |
| 849 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 850 | i = PyInt_AsLong(res); |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 851 | if (i < 0) |
| 852 | i = -1; |
| 853 | else if (i > 0) |
| 854 | i = 1; |
| 855 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 856 | Py_DECREF(res); |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 857 | return (int) i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 860 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 861 | listsort(self, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 862 | PyListObject *self; |
| 863 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 864 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 865 | PyObject *save_comparefunc; |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 866 | if (self->ob_size <= 1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 867 | Py_INCREF(Py_None); |
| 868 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 869 | } |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 870 | save_comparefunc = comparefunc; |
| 871 | comparefunc = args; |
| 872 | if (comparefunc != NULL) { |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 873 | /* Test the comparison function for obvious errors */ |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 874 | (void) cmp((ANY *)&self->ob_item[0], (ANY *)&self->ob_item[1]); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 875 | if (PyErr_Occurred()) { |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 876 | comparefunc = save_comparefunc; |
Guido van Rossum | e10a19e | 1992-08-03 19:05:37 +0000 | [diff] [blame] | 877 | return NULL; |
| 878 | } |
| 879 | } |
| 880 | qsort((char *)self->ob_item, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 881 | (int) self->ob_size, sizeof(PyObject *), cmp); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 882 | comparefunc = save_comparefunc; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 883 | if (PyErr_Occurred()) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 884 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 885 | Py_INCREF(Py_None); |
| 886 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 889 | #endif |
| 890 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 891 | static PyObject * |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 892 | listreverse(self, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 893 | PyListObject *self; |
| 894 | PyObject *args; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 895 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 896 | register PyObject **p, **q; |
| 897 | register PyObject *tmp; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 898 | |
| 899 | if (args != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 900 | PyErr_BadArgument(); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 901 | return NULL; |
| 902 | } |
| 903 | |
| 904 | if (self->ob_size > 1) { |
| 905 | for (p = self->ob_item, q = self->ob_item + self->ob_size - 1; |
| 906 | p < q; p++, q--) { |
| 907 | tmp = *p; |
| 908 | *p = *q; |
| 909 | *q = tmp; |
| 910 | } |
| 911 | } |
| 912 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 913 | Py_INCREF(Py_None); |
| 914 | return Py_None; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Guido van Rossum | 84c76f5 | 1990-10-30 13:32:20 +0000 | [diff] [blame] | 917 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 918 | PyList_Reverse(v) |
| 919 | PyObject *v; |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 920 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 921 | if (v == NULL || !PyList_Check(v)) { |
| 922 | PyErr_BadInternalCall(); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 923 | return -1; |
| 924 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 925 | v = listreverse((PyListObject *)v, (PyObject *)NULL); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 926 | if (v == NULL) |
| 927 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 928 | Py_DECREF(v); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 933 | PyList_Sort(v) |
| 934 | PyObject *v; |
Guido van Rossum | 84c76f5 | 1990-10-30 13:32:20 +0000 | [diff] [blame] | 935 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 936 | if (v == NULL || !PyList_Check(v)) { |
| 937 | PyErr_BadInternalCall(); |
Guido van Rossum | 84c76f5 | 1990-10-30 13:32:20 +0000 | [diff] [blame] | 938 | return -1; |
| 939 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 940 | v = listsort((PyListObject *)v, (PyObject *)NULL); |
Guido van Rossum | 84c76f5 | 1990-10-30 13:32:20 +0000 | [diff] [blame] | 941 | if (v == NULL) |
| 942 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 943 | Py_DECREF(v); |
Guido van Rossum | 84c76f5 | 1990-10-30 13:32:20 +0000 | [diff] [blame] | 944 | return 0; |
| 945 | } |
| 946 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 947 | PyObject * |
| 948 | PyList_AsTuple(v) |
| 949 | PyObject *v; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 950 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 951 | PyObject *w; |
| 952 | PyObject **p; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 953 | int n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 954 | if (v == NULL || !PyList_Check(v)) { |
| 955 | PyErr_BadInternalCall(); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 956 | return NULL; |
| 957 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 958 | n = ((PyListObject *)v)->ob_size; |
| 959 | w = PyTuple_New(n); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 960 | if (w == NULL) |
| 961 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 962 | p = ((PyTupleObject *)w)->ob_item; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 963 | memcpy((ANY *)p, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 964 | (ANY *)((PyListObject *)v)->ob_item, |
| 965 | n*sizeof(PyObject *)); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 966 | while (--n >= 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 967 | Py_INCREF(*p); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 968 | p++; |
| 969 | } |
| 970 | return w; |
| 971 | } |
| 972 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 973 | static PyObject * |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 974 | listindex(self, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 975 | PyListObject *self; |
| 976 | PyObject *args; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 977 | { |
| 978 | int i; |
| 979 | |
| 980 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 981 | PyErr_BadArgument(); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 982 | return NULL; |
| 983 | } |
| 984 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 985 | if (PyObject_Compare(self->ob_item[i], args) == 0) |
| 986 | return PyInt_FromLong((long)i); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 987 | if (PyErr_Occurred()) |
| 988 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 989 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 990 | PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list"); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 991 | return NULL; |
| 992 | } |
| 993 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 994 | static PyObject * |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 995 | listcount(self, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 996 | PyListObject *self; |
| 997 | PyObject *args; |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 998 | { |
| 999 | int count = 0; |
| 1000 | int i; |
| 1001 | |
| 1002 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1003 | PyErr_BadArgument(); |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1004 | return NULL; |
| 1005 | } |
| 1006 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1007 | if (PyObject_Compare(self->ob_item[i], args) == 0) |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1008 | count++; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1009 | if (PyErr_Occurred()) |
| 1010 | return NULL; |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1011 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1012 | return PyInt_FromLong((long)count); |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1015 | static PyObject * |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1016 | listremove(self, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1017 | PyListObject *self; |
| 1018 | PyObject *args; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1019 | { |
| 1020 | int i; |
| 1021 | |
| 1022 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1023 | PyErr_BadArgument(); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1024 | return NULL; |
| 1025 | } |
| 1026 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1027 | if (PyObject_Compare(self->ob_item[i], args) == 0) { |
| 1028 | if (list_ass_slice(self, i, i+1, |
| 1029 | (PyObject *)NULL) != 0) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1030 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1031 | Py_INCREF(Py_None); |
| 1032 | return Py_None; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1033 | } |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1034 | if (PyErr_Occurred()) |
| 1035 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1036 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1037 | PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list"); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1038 | return NULL; |
| 1039 | } |
| 1040 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1041 | static PyMethodDef list_methods[] = { |
| 1042 | {"append", (PyCFunction)listappend}, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1043 | {"insert", (PyCFunction)listinsert}, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1044 | {"remove", (PyCFunction)listremove}, |
Guido van Rossum | 5d8123f | 1997-07-13 03:58:01 +0000 | [diff] [blame] | 1045 | {"index", (PyCFunction)listindex}, |
| 1046 | {"count", (PyCFunction)listcount}, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1047 | {"reverse", (PyCFunction)listreverse}, |
Guido van Rossum | 5d8123f | 1997-07-13 03:58:01 +0000 | [diff] [blame] | 1048 | {"sort", (PyCFunction)listsort, 0}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1049 | {NULL, NULL} /* sentinel */ |
| 1050 | }; |
| 1051 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1052 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1053 | list_getattr(f, name) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1054 | PyListObject *f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1055 | char *name; |
| 1056 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1057 | return Py_FindMethod(list_methods, (PyObject *)f, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1060 | static PySequenceMethods list_as_sequence = { |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1061 | (inquiry)list_length, /*sq_length*/ |
| 1062 | (binaryfunc)list_concat, /*sq_concat*/ |
| 1063 | (intargfunc)list_repeat, /*sq_repeat*/ |
| 1064 | (intargfunc)list_item, /*sq_item*/ |
| 1065 | (intintargfunc)list_slice, /*sq_slice*/ |
| 1066 | (intobjargproc)list_ass_item, /*sq_ass_item*/ |
| 1067 | (intintobjargproc)list_ass_slice, /*sq_ass_slice*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1068 | }; |
| 1069 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1070 | PyTypeObject PyList_Type = { |
| 1071 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1072 | 0, |
| 1073 | "list", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1074 | sizeof(PyListObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1075 | 0, |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1076 | (destructor)list_dealloc, /*tp_dealloc*/ |
| 1077 | (printfunc)list_print, /*tp_print*/ |
| 1078 | (getattrfunc)list_getattr, /*tp_getattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1079 | 0, /*tp_setattr*/ |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1080 | (cmpfunc)list_compare, /*tp_compare*/ |
| 1081 | (reprfunc)list_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1082 | 0, /*tp_as_number*/ |
| 1083 | &list_as_sequence, /*tp_as_sequence*/ |
| 1084 | 0, /*tp_as_mapping*/ |
| 1085 | }; |