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