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