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