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