Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* List object implementation */ |
| 2 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" |
| 4 | |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 5 | #ifdef STDC_HEADERS |
| 6 | #include <stddef.h> |
| 7 | #else |
| 8 | #include <sys/types.h> /* For size_t */ |
| 9 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 11 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 12 | roundupsize(int n) |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 13 | { |
Tim Peters | 65b8b84 | 2001-05-26 05:28:40 +0000 | [diff] [blame] | 14 | unsigned int nbits = 0; |
| 15 | unsigned int n2 = (unsigned int)n >> 5; |
| 16 | |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 17 | /* Round up: |
Tim Peters | 65b8b84 | 2001-05-26 05:28:40 +0000 | [diff] [blame] | 18 | * If n < 256, to a multiple of 8. |
| 19 | * If n < 2048, to a multiple of 64. |
| 20 | * If n < 16384, to a multiple of 512. |
| 21 | * If n < 131072, to a multiple of 4096. |
| 22 | * If n < 1048576, to a multiple of 32768. |
| 23 | * If n < 8388608, to a multiple of 262144. |
| 24 | * If n < 67108864, to a multiple of 2097152. |
| 25 | * If n < 536870912, to a multiple of 16777216. |
| 26 | * ... |
| 27 | * If n < 2**(5+3*i), to a multiple of 2**(3*i). |
| 28 | * |
| 29 | * This over-allocates proportional to the list size, making room |
| 30 | * for additional growth. The over-allocation is mild, but is |
| 31 | * enough to give linear-time amortized behavior over a long |
| 32 | * sequence of appends() in the presence of a poorly-performing |
| 33 | * system realloc() (which is a reality, e.g., across all flavors |
| 34 | * of Windows, with Win9x behavior being particularly bad -- and |
| 35 | * we've still got address space fragmentation problems on Win9x |
| 36 | * even with this scheme, although it requires much longer lists to |
| 37 | * provoke them than it used to). |
| 38 | */ |
| 39 | do { |
| 40 | n2 >>= 3; |
| 41 | nbits += 3; |
| 42 | } while (n2); |
| 43 | return ((n >> nbits) + 1) << nbits; |
| 44 | } |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 45 | |
Neal Norwitz | d4e5be5 | 2002-05-22 23:19:17 +0000 | [diff] [blame] | 46 | #define NRESIZE(var, type, nitems) \ |
| 47 | do { \ |
| 48 | size_t _new_size = roundupsize(nitems); \ |
| 49 | if (_new_size <= ((~(size_t)0) / sizeof(type))) \ |
| 50 | PyMem_RESIZE(var, type, _new_size); \ |
| 51 | else \ |
| 52 | var = NULL; \ |
| 53 | } while (0) |
Guido van Rossum | a46d51d | 1995-01-26 22:59:43 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 55 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 56 | PyList_New(int size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 58 | PyListObject *op; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 59 | size_t nbytes; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 60 | if (size < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 61 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | return NULL; |
| 63 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 64 | nbytes = size * sizeof(PyObject *); |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 65 | /* Check for overflow */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 66 | if (nbytes / sizeof(PyObject *) != (size_t)size) { |
| 67 | return PyErr_NoMemory(); |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 68 | } |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 69 | op = PyObject_GC_New(PyListObject, &PyList_Type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | if (op == NULL) { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 71 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | } |
| 73 | if (size <= 0) { |
| 74 | op->ob_item = NULL; |
| 75 | } |
| 76 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 77 | op->ob_item = (PyObject **) PyMem_MALLOC(nbytes); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 78 | if (op->ob_item == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 79 | return PyErr_NoMemory(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 80 | } |
Neal Norwitz | 35fc760 | 2002-06-13 21:11:11 +0000 | [diff] [blame] | 81 | memset(op->ob_item, 0, sizeof(*op->ob_item) * size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 82 | } |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 83 | op->ob_size = size; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 84 | _PyObject_GC_TRACK(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 85 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 89 | PyList_Size(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 91 | if (!PyList_Check(op)) { |
| 92 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | return -1; |
| 94 | } |
| 95 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 96 | return ((PyListObject *)op) -> ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 99 | static PyObject *indexerr; |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 101 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 102 | PyList_GetItem(PyObject *op, int i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 104 | if (!PyList_Check(op)) { |
| 105 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 106 | return NULL; |
| 107 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 108 | if (i < 0 || i >= ((PyListObject *)op) -> ob_size) { |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 109 | if (indexerr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 110 | indexerr = PyString_FromString( |
| 111 | "list index out of range"); |
| 112 | PyErr_SetObject(PyExc_IndexError, indexerr); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | return NULL; |
| 114 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 115 | return ((PyListObject *)op) -> ob_item[i]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 119 | PyList_SetItem(register PyObject *op, register int i, |
| 120 | register PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 122 | register PyObject *olditem; |
| 123 | register PyObject **p; |
| 124 | if (!PyList_Check(op)) { |
| 125 | Py_XDECREF(newitem); |
| 126 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 127 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 128 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 129 | if (i < 0 || i >= ((PyListObject *)op) -> ob_size) { |
| 130 | Py_XDECREF(newitem); |
| 131 | PyErr_SetString(PyExc_IndexError, |
| 132 | "list assignment index out of range"); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 133 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 134 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 135 | p = ((PyListObject *)op) -> ob_item + i; |
Guido van Rossum | 5fe6058 | 1995-03-09 12:12:50 +0000 | [diff] [blame] | 136 | olditem = *p; |
| 137 | *p = newitem; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 138 | Py_XDECREF(olditem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 143 | ins1(PyListObject *self, int where, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 144 | { |
| 145 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 146 | PyObject **items; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 147 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 148 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 149 | return -1; |
| 150 | } |
Trent Mick | a584664 | 2000-08-13 22:47:45 +0000 | [diff] [blame] | 151 | if (self->ob_size == INT_MAX) { |
| 152 | PyErr_SetString(PyExc_OverflowError, |
| 153 | "cannot add more objects to list"); |
| 154 | return -1; |
| 155 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 156 | items = self->ob_item; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 157 | NRESIZE(items, PyObject *, self->ob_size+1); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 158 | if (items == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 159 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 160 | return -1; |
| 161 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 162 | if (where < 0) |
| 163 | where = 0; |
| 164 | if (where > self->ob_size) |
| 165 | where = self->ob_size; |
| 166 | for (i = self->ob_size; --i >= where; ) |
| 167 | items[i+1] = items[i]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 168 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 169 | items[where] = v; |
| 170 | self->ob_item = items; |
| 171 | self->ob_size++; |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 176 | PyList_Insert(PyObject *op, int where, PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 177 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 178 | if (!PyList_Check(op)) { |
| 179 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 180 | return -1; |
| 181 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 182 | return ins1((PyListObject *)op, where, newitem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 186 | PyList_Append(PyObject *op, PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 187 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 188 | if (!PyList_Check(op)) { |
| 189 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 190 | return -1; |
| 191 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 192 | return ins1((PyListObject *)op, |
| 193 | (int) ((PyListObject *)op)->ob_size, newitem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* Methods */ |
| 197 | |
| 198 | static void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 199 | list_dealloc(PyListObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 200 | { |
| 201 | int i; |
Guido van Rossum | ff413af | 2002-03-28 20:34:59 +0000 | [diff] [blame] | 202 | PyObject_GC_UnTrack(op); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 203 | Py_TRASHCAN_SAFE_BEGIN(op) |
Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 204 | if (op->ob_item != NULL) { |
Guido van Rossum | fa71701 | 1999-06-09 15:19:34 +0000 | [diff] [blame] | 205 | /* Do it backwards, for Christian Tismer. |
| 206 | There's a simple test case where somehow this reduces |
| 207 | thrashing when a *very* large list is created and |
| 208 | immediately deleted. */ |
| 209 | i = op->ob_size; |
| 210 | while (--i >= 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 211 | Py_XDECREF(op->ob_item[i]); |
Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 212 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 213 | PyMem_FREE(op->ob_item); |
Jack Jansen | 7874d1f | 1995-01-19 12:09:27 +0000 | [diff] [blame] | 214 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 215 | op->ob_type->tp_free((PyObject *)op); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 216 | Py_TRASHCAN_SAFE_END(op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 219 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 220 | list_print(PyListObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 221 | { |
| 222 | int i; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 223 | |
| 224 | i = Py_ReprEnter((PyObject*)op); |
| 225 | if (i != 0) { |
| 226 | if (i < 0) |
| 227 | return i; |
| 228 | fprintf(fp, "[...]"); |
| 229 | return 0; |
| 230 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 231 | fprintf(fp, "["); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 232 | for (i = 0; i < op->ob_size; i++) { |
| 233 | if (i > 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 234 | fprintf(fp, ", "); |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 235 | if (PyObject_Print(op->ob_item[i], fp, 0) != 0) { |
| 236 | Py_ReprLeave((PyObject *)op); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 237 | return -1; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 238 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 239 | } |
| 240 | fprintf(fp, "]"); |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 241 | Py_ReprLeave((PyObject *)op); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 242 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 245 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 246 | list_repr(PyListObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 247 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 248 | int i; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 249 | PyObject *s, *temp; |
| 250 | PyObject *pieces = NULL, *result = NULL; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 251 | |
| 252 | i = Py_ReprEnter((PyObject*)v); |
| 253 | if (i != 0) { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 254 | return i > 0 ? PyString_FromString("[...]") : NULL; |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 255 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 256 | |
| 257 | if (v->ob_size == 0) { |
| 258 | result = PyString_FromString("[]"); |
| 259 | goto Done; |
| 260 | } |
| 261 | |
| 262 | pieces = PyList_New(0); |
| 263 | if (pieces == NULL) |
| 264 | goto Done; |
| 265 | |
| 266 | /* Do repr() on each element. Note that this may mutate the list, |
| 267 | so must refetch the list size on each iteration. */ |
| 268 | for (i = 0; i < v->ob_size; ++i) { |
| 269 | int status; |
| 270 | s = PyObject_Repr(v->ob_item[i]); |
| 271 | if (s == NULL) |
| 272 | goto Done; |
| 273 | status = PyList_Append(pieces, s); |
| 274 | Py_DECREF(s); /* append created a new ref */ |
| 275 | if (status < 0) |
| 276 | goto Done; |
| 277 | } |
| 278 | |
| 279 | /* Add "[]" decorations to the first and last items. */ |
| 280 | assert(PyList_GET_SIZE(pieces) > 0); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 281 | s = PyString_FromString("["); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 282 | if (s == NULL) |
| 283 | goto Done; |
| 284 | temp = PyList_GET_ITEM(pieces, 0); |
| 285 | PyString_ConcatAndDel(&s, temp); |
| 286 | PyList_SET_ITEM(pieces, 0, s); |
| 287 | if (s == NULL) |
| 288 | goto Done; |
| 289 | |
| 290 | s = PyString_FromString("]"); |
| 291 | if (s == NULL) |
| 292 | goto Done; |
| 293 | temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); |
| 294 | PyString_ConcatAndDel(&temp, s); |
| 295 | PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp); |
| 296 | if (temp == NULL) |
| 297 | goto Done; |
| 298 | |
| 299 | /* Paste them all together with ", " between. */ |
| 300 | s = PyString_FromString(", "); |
| 301 | if (s == NULL) |
| 302 | goto Done; |
| 303 | result = _PyString_Join(s, pieces); |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 304 | Py_DECREF(s); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 305 | |
| 306 | Done: |
| 307 | Py_XDECREF(pieces); |
Guido van Rossum | fb376de | 1998-04-10 22:47:27 +0000 | [diff] [blame] | 308 | Py_ReprLeave((PyObject *)v); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 309 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 313 | list_length(PyListObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 314 | { |
| 315 | return a->ob_size; |
| 316 | } |
| 317 | |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 318 | |
| 319 | |
| 320 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 321 | list_contains(PyListObject *a, PyObject *el) |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 322 | { |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 323 | int i; |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 324 | |
| 325 | for (i = 0; i < a->ob_size; ++i) { |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 326 | int cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i), |
| 327 | Py_EQ); |
| 328 | if (cmp > 0) |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 329 | return 1; |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 330 | else if (cmp < 0) |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 331 | return -1; |
| 332 | } |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 337 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 338 | list_item(PyListObject *a, int i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 339 | { |
| 340 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | 929f1b8 | 1996-08-09 20:51:27 +0000 | [diff] [blame] | 341 | if (indexerr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 342 | indexerr = PyString_FromString( |
| 343 | "list index out of range"); |
| 344 | PyErr_SetObject(PyExc_IndexError, indexerr); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 345 | return NULL; |
| 346 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 347 | Py_INCREF(a->ob_item[i]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 348 | return a->ob_item[i]; |
| 349 | } |
| 350 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 351 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 352 | list_slice(PyListObject *a, int ilow, int ihigh) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 353 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 354 | PyListObject *np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 355 | int i; |
| 356 | if (ilow < 0) |
| 357 | ilow = 0; |
| 358 | else if (ilow > a->ob_size) |
| 359 | ilow = a->ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 360 | if (ihigh < ilow) |
| 361 | ihigh = ilow; |
| 362 | else if (ihigh > a->ob_size) |
| 363 | ihigh = a->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 364 | np = (PyListObject *) PyList_New(ihigh - ilow); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 365 | if (np == NULL) |
| 366 | return NULL; |
| 367 | for (i = ilow; i < ihigh; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 368 | PyObject *v = a->ob_item[i]; |
| 369 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 370 | np->ob_item[i - ilow] = v; |
| 371 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 372 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 375 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 376 | PyList_GetSlice(PyObject *a, int ilow, int ihigh) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 377 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 378 | if (!PyList_Check(a)) { |
| 379 | PyErr_BadInternalCall(); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 380 | return NULL; |
| 381 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 382 | return list_slice((PyListObject *)a, ilow, ihigh); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 385 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 386 | list_concat(PyListObject *a, PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 387 | { |
| 388 | int size; |
| 389 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 390 | PyListObject *np; |
| 391 | if (!PyList_Check(bb)) { |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 392 | PyErr_Format(PyExc_TypeError, |
Fred Drake | 914a2ed | 2000-06-01 14:31:03 +0000 | [diff] [blame] | 393 | "can only concatenate list (not \"%.200s\") to list", |
| 394 | bb->ob_type->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 395 | return NULL; |
| 396 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 397 | #define b ((PyListObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 398 | size = a->ob_size + b->ob_size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 399 | np = (PyListObject *) PyList_New(size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 400 | if (np == NULL) { |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 401 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 402 | } |
| 403 | for (i = 0; i < a->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 404 | PyObject *v = a->ob_item[i]; |
| 405 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 406 | np->ob_item[i] = v; |
| 407 | } |
| 408 | for (i = 0; i < b->ob_size; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 409 | PyObject *v = b->ob_item[i]; |
| 410 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 411 | np->ob_item[i + a->ob_size] = v; |
| 412 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 413 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 414 | #undef b |
| 415 | } |
| 416 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 417 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 418 | list_repeat(PyListObject *a, int n) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 419 | { |
| 420 | int i, j; |
| 421 | int size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 422 | PyListObject *np; |
| 423 | PyObject **p; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 424 | if (n < 0) |
| 425 | n = 0; |
| 426 | size = a->ob_size * n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 427 | np = (PyListObject *) PyList_New(size); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 428 | if (np == NULL) |
| 429 | return NULL; |
| 430 | p = np->ob_item; |
| 431 | for (i = 0; i < n; i++) { |
| 432 | for (j = 0; j < a->ob_size; j++) { |
| 433 | *p = a->ob_item[j]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 434 | Py_INCREF(*p); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 435 | p++; |
| 436 | } |
| 437 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 438 | return (PyObject *) np; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 441 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 442 | list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 443 | { |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 444 | /* Because [X]DECREF can recursively invoke list operations on |
| 445 | this list, we must postpone all [X]DECREF activity until |
| 446 | after the list is back in its canonical shape. Therefore |
| 447 | we must allocate an additional array, 'recycle', into which |
| 448 | we temporarily copy the items that are deleted from the |
| 449 | list. :-( */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 450 | PyObject **recycle, **p; |
| 451 | PyObject **item; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 452 | int n; /* Size of replacement list */ |
| 453 | int d; /* Change in size */ |
| 454 | int k; /* Loop index */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 455 | #define b ((PyListObject *)v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 456 | if (v == NULL) |
| 457 | n = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 458 | else if (PyList_Check(v)) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 459 | n = b->ob_size; |
Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 460 | if (a == b) { |
| 461 | /* Special case "a[i:j] = a" -- copy b first */ |
| 462 | int ret; |
| 463 | v = list_slice(b, 0, n); |
| 464 | ret = list_ass_slice(a, ilow, ihigh, v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 465 | Py_DECREF(v); |
Guido van Rossum | 32dffaa | 1991-12-24 13:27:34 +0000 | [diff] [blame] | 466 | return ret; |
| 467 | } |
| 468 | } |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 469 | else { |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 470 | PyErr_Format(PyExc_TypeError, |
| 471 | "must assign list (not \"%.200s\") to slice", |
| 472 | v->ob_type->tp_name); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 473 | return -1; |
| 474 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 475 | if (ilow < 0) |
| 476 | ilow = 0; |
| 477 | else if (ilow > a->ob_size) |
| 478 | ilow = a->ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 479 | if (ihigh < ilow) |
| 480 | ihigh = ilow; |
| 481 | else if (ihigh > a->ob_size) |
| 482 | ihigh = a->ob_size; |
| 483 | item = a->ob_item; |
| 484 | d = n - (ihigh-ilow); |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 485 | if (ihigh > ilow) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 486 | p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow)); |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 487 | else |
| 488 | p = recycle = NULL; |
| 489 | if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 490 | for (k = ilow; k < ihigh; 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 | if (d < 0) { |
| 493 | for (/*k = ihigh*/; k < a->ob_size; k++) |
| 494 | item[k+d] = item[k]; |
| 495 | a->ob_size += d; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 496 | NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 497 | a->ob_item = item; |
| 498 | } |
| 499 | } |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 500 | else { /* Insert d items; recycle ihigh-ilow items */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 501 | NRESIZE(item, PyObject *, a->ob_size + d); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 502 | if (item == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 503 | if (recycle != NULL) |
| 504 | PyMem_DEL(recycle); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 505 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 506 | return -1; |
| 507 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 508 | for (k = a->ob_size; --k >= ihigh; ) |
| 509 | item[k+d] = item[k]; |
| 510 | for (/*k = ihigh-1*/; k >= ilow; --k) |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 511 | *p++ = item[k]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 512 | a->ob_item = item; |
| 513 | a->ob_size += d; |
| 514 | } |
| 515 | for (k = 0; k < n; k++, ilow++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 516 | PyObject *w = b->ob_item[k]; |
| 517 | Py_XINCREF(w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 518 | item[ilow] = w; |
| 519 | } |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 520 | if (recycle) { |
| 521 | while (--p >= recycle) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 522 | Py_XDECREF(*p); |
| 523 | PyMem_DEL(recycle); |
Guido van Rossum | ae7bf1a | 1995-01-17 10:21:11 +0000 | [diff] [blame] | 524 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 525 | if (a->ob_size == 0 && a->ob_item != NULL) { |
| 526 | PyMem_FREE(a->ob_item); |
| 527 | a->ob_item = NULL; |
| 528 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 529 | return 0; |
| 530 | #undef b |
| 531 | } |
| 532 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 533 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 534 | PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 535 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 536 | if (!PyList_Check(a)) { |
| 537 | PyErr_BadInternalCall(); |
Guido van Rossum | 1fc238a | 1993-07-29 08:25:09 +0000 | [diff] [blame] | 538 | return -1; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 539 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 540 | return list_ass_slice((PyListObject *)a, ilow, ihigh, v); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 543 | static PyObject * |
| 544 | list_inplace_repeat(PyListObject *self, int n) |
| 545 | { |
| 546 | PyObject **items; |
| 547 | int size, i, j; |
| 548 | |
| 549 | |
| 550 | size = PyList_GET_SIZE(self); |
| 551 | if (size == 0) { |
| 552 | Py_INCREF(self); |
| 553 | return (PyObject *)self; |
| 554 | } |
| 555 | |
| 556 | items = self->ob_item; |
| 557 | |
| 558 | if (n < 1) { |
| 559 | self->ob_item = NULL; |
| 560 | self->ob_size = 0; |
| 561 | for (i = 0; i < size; i++) |
| 562 | Py_XDECREF(items[i]); |
| 563 | PyMem_DEL(items); |
| 564 | Py_INCREF(self); |
| 565 | return (PyObject *)self; |
| 566 | } |
| 567 | |
| 568 | NRESIZE(items, PyObject*, size*n); |
| 569 | if (items == NULL) { |
| 570 | PyErr_NoMemory(); |
| 571 | goto finally; |
| 572 | } |
| 573 | self->ob_item = items; |
| 574 | for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */ |
| 575 | for (j = 0; j < size; j++) { |
| 576 | PyObject *o = PyList_GET_ITEM(self, j); |
| 577 | Py_INCREF(o); |
| 578 | PyList_SET_ITEM(self, self->ob_size++, o); |
| 579 | } |
| 580 | } |
| 581 | Py_INCREF(self); |
| 582 | return (PyObject *)self; |
| 583 | finally: |
| 584 | return NULL; |
| 585 | } |
| 586 | |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 587 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 588 | list_ass_item(PyListObject *a, int i, PyObject *v) |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 589 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 590 | PyObject *old_value; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 591 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 592 | PyErr_SetString(PyExc_IndexError, |
| 593 | "list assignment index out of range"); |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 594 | return -1; |
| 595 | } |
| 596 | if (v == NULL) |
| 597 | return list_ass_slice(a, i, i+1, v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 598 | Py_INCREF(v); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 599 | old_value = a->ob_item[i]; |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 600 | a->ob_item[i] = v; |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 601 | Py_DECREF(old_value); |
Guido van Rossum | 4a450d0 | 1991-04-03 19:05:18 +0000 | [diff] [blame] | 602 | return 0; |
| 603 | } |
| 604 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 605 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 606 | ins(PyListObject *self, int where, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 607 | { |
| 608 | if (ins1(self, where, v) != 0) |
| 609 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 610 | Py_INCREF(Py_None); |
| 611 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 614 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 615 | listinsert(PyListObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 616 | { |
| 617 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 618 | PyObject *v; |
Guido van Rossum | c00a938 | 2000-02-24 21:48:29 +0000 | [diff] [blame] | 619 | if (!PyArg_ParseTuple(args, "iO:insert", &i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 620 | return NULL; |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 621 | return ins(self, i, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 624 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 625 | listappend(PyListObject *self, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 626 | { |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 627 | return ins(self, (int) self->ob_size, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 630 | static int |
| 631 | listextend_internal(PyListObject *self, PyObject *b) |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 632 | { |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 633 | PyObject **items; |
| 634 | int selflen = PyList_GET_SIZE(self); |
| 635 | int blen; |
| 636 | register int i; |
| 637 | |
Jeremy Hylton | db60bb5 | 2001-01-03 22:32:16 +0000 | [diff] [blame] | 638 | if (PyObject_Size(b) == 0) { |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 639 | /* short circuit when b is empty */ |
Jeremy Hylton | db60bb5 | 2001-01-03 22:32:16 +0000 | [diff] [blame] | 640 | Py_DECREF(b); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 641 | return 0; |
Jeremy Hylton | db60bb5 | 2001-01-03 22:32:16 +0000 | [diff] [blame] | 642 | } |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 643 | |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 644 | if (self == (PyListObject*)b) { |
| 645 | /* as in list_ass_slice() we must special case the |
| 646 | * situation: a.extend(a) |
| 647 | * |
| 648 | * XXX: I think this way ought to be faster than using |
| 649 | * list_slice() the way list_ass_slice() does. |
| 650 | */ |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 651 | Py_DECREF(b); |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 652 | b = PyList_New(selflen); |
| 653 | if (!b) |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 654 | return -1; |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 655 | for (i = 0; i < selflen; i++) { |
| 656 | PyObject *o = PyList_GET_ITEM(self, i); |
| 657 | Py_INCREF(o); |
| 658 | PyList_SET_ITEM(b, i, o); |
| 659 | } |
| 660 | } |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 661 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 662 | blen = PyObject_Size(b); |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 663 | |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 664 | /* resize a using idiom */ |
| 665 | items = self->ob_item; |
| 666 | NRESIZE(items, PyObject*, selflen + blen); |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 667 | if (items == NULL) { |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 668 | PyErr_NoMemory(); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 669 | Py_DECREF(b); |
| 670 | return -1; |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 671 | } |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 672 | |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 673 | self->ob_item = items; |
| 674 | |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 675 | /* populate the end of self with b's items */ |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 676 | for (i = 0; i < blen; i++) { |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 677 | PyObject *o = PySequence_Fast_GET_ITEM(b, i); |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 678 | Py_INCREF(o); |
| 679 | PyList_SET_ITEM(self, self->ob_size++, o); |
| 680 | } |
Andrew M. Kuchling | 74042d6 | 2000-06-18 18:43:14 +0000 | [diff] [blame] | 681 | Py_DECREF(b); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 682 | return 0; |
Barry Warsaw | dedf6d6 | 1998-10-09 16:37:25 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | |
| 686 | static PyObject * |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 687 | list_inplace_concat(PyListObject *self, PyObject *other) |
| 688 | { |
Tim Peters | 1af03e9 | 2001-05-26 19:37:54 +0000 | [diff] [blame] | 689 | other = PySequence_Fast(other, "argument to += must be iterable"); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 690 | if (!other) |
| 691 | return NULL; |
| 692 | |
| 693 | if (listextend_internal(self, other) < 0) |
| 694 | return NULL; |
| 695 | |
| 696 | Py_INCREF(self); |
| 697 | return (PyObject *)self; |
| 698 | } |
| 699 | |
| 700 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 701 | listextend(PyListObject *self, PyObject *b) |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 702 | { |
| 703 | |
Tim Peters | 1af03e9 | 2001-05-26 19:37:54 +0000 | [diff] [blame] | 704 | b = PySequence_Fast(b, "list.extend() argument must be iterable"); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 705 | if (!b) |
| 706 | return NULL; |
| 707 | |
| 708 | if (listextend_internal(self, b) < 0) |
| 709 | return NULL; |
| 710 | |
| 711 | Py_INCREF(Py_None); |
| 712 | return Py_None; |
| 713 | } |
| 714 | |
| 715 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 716 | listpop(PyListObject *self, PyObject *args) |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 717 | { |
| 718 | int i = -1; |
| 719 | PyObject *v; |
Guido van Rossum | c00a938 | 2000-02-24 21:48:29 +0000 | [diff] [blame] | 720 | if (!PyArg_ParseTuple(args, "|i:pop", &i)) |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 721 | return NULL; |
| 722 | if (self->ob_size == 0) { |
| 723 | /* Special-case most common failure cause */ |
| 724 | PyErr_SetString(PyExc_IndexError, "pop from empty list"); |
| 725 | return NULL; |
| 726 | } |
| 727 | if (i < 0) |
| 728 | i += self->ob_size; |
| 729 | if (i < 0 || i >= self->ob_size) { |
| 730 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 731 | return NULL; |
| 732 | } |
| 733 | v = self->ob_item[i]; |
| 734 | Py_INCREF(v); |
| 735 | if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { |
| 736 | Py_DECREF(v); |
| 737 | return NULL; |
| 738 | } |
| 739 | return v; |
| 740 | } |
| 741 | |
Tim Peters | 8e2e7ca | 2002-07-19 02:33:08 +0000 | [diff] [blame] | 742 | /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */ |
| 743 | static void |
| 744 | reverse_slice(PyObject **lo, PyObject **hi) |
| 745 | { |
| 746 | assert(lo && hi); |
| 747 | |
| 748 | --hi; |
| 749 | while (lo < hi) { |
| 750 | PyObject *t = *lo; |
| 751 | *lo = *hi; |
| 752 | *hi = t; |
| 753 | ++lo; |
| 754 | --hi; |
| 755 | } |
| 756 | } |
| 757 | |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 758 | /* New quicksort implementation for arrays of object pointers. |
| 759 | Thanks to discussions with Tim Peters. */ |
| 760 | |
| 761 | /* CMPERROR is returned by our comparison function when an error |
| 762 | occurred. This is the largest negative integer (0x80000000 on a |
| 763 | 32-bit system). */ |
Guido van Rossum | 19700b6 | 1997-03-05 00:45:43 +0000 | [diff] [blame] | 764 | #define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) ) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 765 | |
| 766 | /* Comparison function. Takes care of calling a user-supplied |
| 767 | comparison function (any callable Python object). Calls the |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 768 | standard comparison function, PyObject_Compare(), if the user- |
| 769 | supplied function is NULL. */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 770 | |
| 771 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 772 | docompare(PyObject *x, PyObject *y, PyObject *compare) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 773 | { |
Tim Peters | f2a0473 | 2002-07-11 21:46:16 +0000 | [diff] [blame] | 774 | PyObject *res; |
| 775 | PyObject *args; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 776 | int i; |
| 777 | |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 778 | if (compare == NULL) { |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 779 | /* NOTE: we rely on the fact here that the sorting algorithm |
| 780 | only ever checks whether k<0, i.e., whether x<y. So we |
| 781 | invoke the rich comparison function with Py_LT ('<'), and |
| 782 | return -1 when it returns true and 0 when it returns |
| 783 | false. */ |
| 784 | i = PyObject_RichCompareBool(x, y, Py_LT); |
| 785 | if (i < 0) |
| 786 | return CMPERROR; |
| 787 | else |
| 788 | return -i; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 789 | } |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 790 | |
Tim Peters | f2a0473 | 2002-07-11 21:46:16 +0000 | [diff] [blame] | 791 | args = PyTuple_New(2); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 792 | if (args == NULL) |
| 793 | return CMPERROR; |
Tim Peters | f2a0473 | 2002-07-11 21:46:16 +0000 | [diff] [blame] | 794 | Py_INCREF(x); |
| 795 | Py_INCREF(y); |
| 796 | PyTuple_SET_ITEM(args, 0, x); |
| 797 | PyTuple_SET_ITEM(args, 1, y); |
Tim Peters | 58cf361 | 2002-07-15 05:16:13 +0000 | [diff] [blame] | 798 | res = PyObject_Call(compare, args, NULL); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 799 | Py_DECREF(args); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 800 | if (res == NULL) |
| 801 | return CMPERROR; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 802 | if (!PyInt_Check(res)) { |
| 803 | Py_DECREF(res); |
| 804 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 9bcd1d7 | 1999-04-19 17:44:39 +0000 | [diff] [blame] | 805 | "comparison function must return int"); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 806 | return CMPERROR; |
| 807 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 808 | i = PyInt_AsLong(res); |
| 809 | Py_DECREF(res); |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 810 | if (i < 0) |
| 811 | return -1; |
| 812 | if (i > 0) |
| 813 | return 1; |
| 814 | return 0; |
| 815 | } |
| 816 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 817 | /* MINSIZE is the smallest array that will get a full-blown samplesort |
| 818 | treatment; smaller arrays are sorted using binary insertion. It must |
| 819 | be at least 7 for the samplesort implementation to work. Binary |
| 820 | insertion does fewer compares, but can suffer O(N**2) data movement. |
| 821 | The more expensive compares, the larger MINSIZE should be. */ |
| 822 | #define MINSIZE 100 |
| 823 | |
| 824 | /* MINPARTITIONSIZE is the smallest array slice samplesort will bother to |
| 825 | partition; smaller slices are passed to binarysort. It must be at |
| 826 | least 2, and no larger than MINSIZE. Setting it higher reduces the # |
| 827 | of compares slowly, but increases the amount of data movement quickly. |
| 828 | The value here was chosen assuming a compare costs ~25x more than |
| 829 | swapping a pair of memory-resident pointers -- but under that assumption, |
| 830 | changing the value by a few dozen more or less has aggregate effect |
| 831 | under 1%. So the value is crucial, but not touchy <wink>. */ |
| 832 | #define MINPARTITIONSIZE 40 |
| 833 | |
| 834 | /* MAXMERGE is the largest number of elements we'll always merge into |
| 835 | a known-to-be sorted chunk via binary insertion, regardless of the |
| 836 | size of that chunk. Given a chunk of N sorted elements, and a group |
| 837 | of K unknowns, the largest K for which it's better to do insertion |
| 838 | (than a full-blown sort) is a complicated function of N and K mostly |
| 839 | involving the expected number of compares and data moves under each |
| 840 | approach, and the relative cost of those operations on a specific |
| 841 | architecure. The fixed value here is conservative, and should be a |
| 842 | clear win regardless of architecture or N. */ |
| 843 | #define MAXMERGE 15 |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 844 | |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 845 | /* 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] | 846 | this allows us to sort arrays of size N where |
| 847 | N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough |
| 848 | for arrays of size 2**64. Because we push the biggest partition |
| 849 | first, the worst case occurs when all subarrays are always partitioned |
| 850 | exactly in two. */ |
| 851 | #define STACKSIZE 60 |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 852 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 853 | |
| 854 | #define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail |
| 855 | |
| 856 | /* binarysort is the best method for sorting small arrays: it does |
| 857 | few compares, but can do data movement quadratic in the number of |
| 858 | elements. |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 859 | [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] | 860 | binary insertion. |
| 861 | On entry, must have lo <= start <= hi, and that [lo, start) is already |
| 862 | sorted (pass start == lo if you don't know!). |
| 863 | If docompare complains (returns CMPERROR) return -1, else 0. |
| 864 | Even in case of error, the output slice will be some permutation of |
| 865 | the input (nothing is lost or duplicated). |
| 866 | */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 867 | |
| 868 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 869 | binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare) |
| 870 | /* compare -- comparison function object, or NULL for default */ |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 871 | { |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 872 | /* assert lo <= start <= hi |
| 873 | assert [lo, start) is sorted */ |
| 874 | register int k; |
| 875 | register PyObject **l, **p, **r; |
| 876 | register PyObject *pivot; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 877 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 878 | if (lo == start) |
| 879 | ++start; |
| 880 | for (; start < hi; ++start) { |
| 881 | /* set l to where *start belongs */ |
| 882 | l = lo; |
| 883 | r = start; |
Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 884 | pivot = *r; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 885 | do { |
| 886 | p = l + ((r - l) >> 1); |
| 887 | SETK(pivot, *p); |
| 888 | if (k < 0) |
| 889 | r = p; |
| 890 | else |
| 891 | l = p + 1; |
| 892 | } while (l < r); |
| 893 | /* Pivot should go at l -- slide over to make room. |
| 894 | Caution: using memmove is much slower under MSVC 5; |
| 895 | we're not usually moving many slots. */ |
| 896 | for (p = start; p > l; --p) |
| 897 | *p = *(p-1); |
| 898 | *l = pivot; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 899 | } |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 900 | return 0; |
Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 901 | |
| 902 | fail: |
| 903 | return -1; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /* samplesortslice is the sorting workhorse. |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 907 | [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] | 908 | On entry, must have lo <= hi, |
| 909 | If docompare complains (returns CMPERROR) return -1, else 0. |
| 910 | Even in case of error, the output slice will be some permutation of |
| 911 | the input (nothing is lost or duplicated). |
| 912 | |
| 913 | samplesort is basically quicksort on steroids: a power of 2 close |
| 914 | to n/ln(n) is computed, and that many elements (less 1) are picked at |
| 915 | random from the array and sorted. These 2**k - 1 elements are then |
| 916 | used as preselected pivots for an equal number of quicksort |
| 917 | partitioning steps, partitioning the slice into 2**k chunks each of |
| 918 | size about ln(n). These small final chunks are then usually handled |
| 919 | by binarysort. Note that when k=1, this is roughly the same as an |
| 920 | ordinary quicksort using a random pivot, and when k=2 this is roughly |
| 921 | a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes |
| 922 | this a "median of n/ln(n)" quicksort. You can also view it as a kind |
| 923 | of bucket sort, where 2**k-1 bucket boundaries are picked dynamically. |
| 924 | |
| 925 | The large number of samples makes a quadratic-time case almost |
| 926 | impossible, and asymptotically drives the average-case number of |
| 927 | compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of- |
| 928 | 3 variant) down to N lg N. |
| 929 | |
| 930 | We also play lots of low-level tricks to cut the number of compares. |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 931 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 932 | Very obscure: To avoid using extra memory, the PPs are stored in the |
| 933 | array and shuffled around as partitioning proceeds. At the start of a |
| 934 | partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order, |
| 935 | adjacent (either on the left or the right!) to a chunk of X elements |
| 936 | that are to be partitioned: P X or X P. In either case we need to |
| 937 | shuffle things *in place* so that the 2**(m-1) smaller PPs are on the |
| 938 | left, followed by the PP to be used for this step (that's the middle |
| 939 | of the PPs), followed by X, followed by the 2**(m-1) larger PPs: |
| 940 | P X or X P -> Psmall pivot X Plarge |
| 941 | and the order of the PPs must not be altered. It can take a while |
| 942 | to realize this isn't trivial! It can take even longer <wink> to |
| 943 | understand why the simple code below works, using only 2**(m-1) swaps. |
| 944 | The key is that the order of the X elements isn't necessarily |
| 945 | preserved: X can end up as some cyclic permutation of its original |
| 946 | order. That's OK, because X is unsorted anyway. If the order of X |
| 947 | had to be preserved too, the simplest method I know of using O(1) |
| 948 | scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes. |
| 949 | Since len(X) is typically several times larger than 2**(m-1), that |
| 950 | would slow things down. |
| 951 | */ |
| 952 | |
| 953 | struct SamplesortStackNode { |
| 954 | /* Represents a slice of the array, from (& including) lo up |
| 955 | to (but excluding) hi. "extra" additional & adjacent elements |
| 956 | are pre-selected pivots (PPs), spanning [lo-extra, lo) if |
| 957 | extra > 0, or [hi, hi-extra) if extra < 0. The PPs are |
| 958 | already sorted, but nothing is known about the other elements |
| 959 | in [lo, hi). |extra| is always one less than a power of 2. |
| 960 | When extra is 0, we're out of PPs, and the slice must be |
| 961 | sorted by some other means. */ |
| 962 | PyObject **lo; |
| 963 | PyObject **hi; |
| 964 | int extra; |
| 965 | }; |
| 966 | |
| 967 | /* 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] | 968 | 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] | 969 | is undesirable, so cutoff values are canned in the "cutoff" table |
| 970 | below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */ |
| 971 | #define CUTOFFBASE 4 |
Guido van Rossum | 3aa23fd | 1999-01-14 19:01:53 +0000 | [diff] [blame] | 972 | static long cutoff[] = { |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 973 | 43, /* smallest N such that k == 4 */ |
| 974 | 106, /* etc */ |
| 975 | 250, |
| 976 | 576, |
| 977 | 1298, |
| 978 | 2885, |
| 979 | 6339, |
| 980 | 13805, |
| 981 | 29843, |
| 982 | 64116, |
| 983 | 137030, |
| 984 | 291554, |
| 985 | 617916, |
| 986 | 1305130, |
| 987 | 2748295, |
| 988 | 5771662, |
| 989 | 12091672, |
| 990 | 25276798, |
| 991 | 52734615, |
| 992 | 109820537, |
| 993 | 228324027, |
| 994 | 473977813, |
| 995 | 982548444, /* smallest N such that k == 26 */ |
| 996 | 2034159050 /* largest N that fits in signed 32-bit; k == 27 */ |
| 997 | }; |
| 998 | |
| 999 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1000 | samplesortslice(PyObject **lo, PyObject **hi, PyObject *compare) |
| 1001 | /* compare -- comparison function object, or NULL for default */ |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1002 | { |
| 1003 | register PyObject **l, **r; |
| 1004 | register PyObject *tmp, *pivot; |
| 1005 | register int k; |
| 1006 | int n, extra, top, extraOnRight; |
| 1007 | struct SamplesortStackNode stack[STACKSIZE]; |
| 1008 | |
| 1009 | /* assert lo <= hi */ |
| 1010 | n = hi - lo; |
| 1011 | |
| 1012 | /* ---------------------------------------------------------- |
| 1013 | * Special cases |
| 1014 | * --------------------------------------------------------*/ |
| 1015 | if (n < 2) |
| 1016 | return 0; |
| 1017 | |
| 1018 | /* Set r to the largest value such that [lo,r) is sorted. |
| 1019 | This catches the already-sorted case, the all-the-same |
| 1020 | case, and the appended-a-few-elements-to-a-sorted-list case. |
| 1021 | If the array is unsorted, we're very likely to get out of |
| 1022 | the loop fast, so the test is cheap if it doesn't pay off. |
| 1023 | */ |
| 1024 | /* assert lo < hi */ |
| 1025 | for (r = lo+1; r < hi; ++r) { |
| 1026 | SETK(*r, *(r-1)); |
| 1027 | if (k < 0) |
| 1028 | break; |
| 1029 | } |
| 1030 | /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are |
| 1031 | few unknowns, or few elements in total. */ |
| 1032 | if (hi - r <= MAXMERGE || n < MINSIZE) |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1033 | return binarysort(lo, hi, r, compare); |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1034 | |
| 1035 | /* Check for the array already being reverse-sorted. Typical |
| 1036 | benchmark-driven silliness <wink>. */ |
| 1037 | /* assert lo < hi */ |
| 1038 | for (r = lo+1; r < hi; ++r) { |
| 1039 | SETK(*(r-1), *r); |
| 1040 | if (k < 0) |
| 1041 | break; |
| 1042 | } |
| 1043 | if (hi - r <= MAXMERGE) { |
| 1044 | /* Reverse the reversed prefix, then insert the tail */ |
Tim Peters | 8e2e7ca | 2002-07-19 02:33:08 +0000 | [diff] [blame] | 1045 | reverse_slice(lo, r); |
| 1046 | return binarysort(lo, hi, r, compare); |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | /* ---------------------------------------------------------- |
| 1050 | * Normal case setup: a large array without obvious pattern. |
| 1051 | * --------------------------------------------------------*/ |
| 1052 | |
| 1053 | /* extra := a power of 2 ~= n/ln(n), less 1. |
| 1054 | First find the smallest extra s.t. n < cutoff[extra] */ |
| 1055 | for (extra = 0; |
| 1056 | extra < sizeof(cutoff) / sizeof(cutoff[0]); |
| 1057 | ++extra) { |
| 1058 | if (n < cutoff[extra]) |
| 1059 | break; |
| 1060 | /* note that if we fall out of the loop, the value of |
| 1061 | extra still makes *sense*, but may be smaller than |
| 1062 | we would like (but the array has more than ~= 2**31 |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1063 | elements in this case!) */ |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1064 | } |
| 1065 | /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can |
| 1066 | have is CUTOFFBASE-1, so |
| 1067 | assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */ |
| 1068 | extra = (1 << (extra - 1 + CUTOFFBASE)) - 1; |
| 1069 | /* assert extra > 0 and n >= extra */ |
| 1070 | |
| 1071 | /* Swap that many values to the start of the array. The |
| 1072 | selection of elements is pseudo-random, but the same on |
| 1073 | every run (this is intentional! timing algorithm changes is |
| 1074 | a pain if timing varies across runs). */ |
| 1075 | { |
| 1076 | unsigned int seed = n / extra; /* arbitrary */ |
| 1077 | unsigned int i; |
| 1078 | for (i = 0; i < (unsigned)extra; ++i) { |
| 1079 | /* j := random int in [i, n) */ |
| 1080 | unsigned int j; |
| 1081 | seed = seed * 69069 + 7; |
| 1082 | j = i + seed % (n - i); |
| 1083 | tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | /* Recursively sort the preselected pivots. */ |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1088 | if (samplesortslice(lo, lo + extra, compare) < 0) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1089 | goto fail; |
| 1090 | |
| 1091 | top = 0; /* index of available stack slot */ |
| 1092 | lo += extra; /* point to first unknown */ |
| 1093 | extraOnRight = 0; /* the PPs are at the left end */ |
| 1094 | |
| 1095 | /* ---------------------------------------------------------- |
| 1096 | * Partition [lo, hi), and repeat until out of work. |
| 1097 | * --------------------------------------------------------*/ |
| 1098 | for (;;) { |
| 1099 | /* assert lo <= hi, so n >= 0 */ |
| 1100 | n = hi - lo; |
| 1101 | |
| 1102 | /* We may not want, or may not be able, to partition: |
| 1103 | If n is small, it's quicker to insert. |
| 1104 | If extra is 0, we're out of pivots, and *must* use |
| 1105 | another method. |
| 1106 | */ |
| 1107 | if (n < MINPARTITIONSIZE || extra == 0) { |
| 1108 | if (n >= MINSIZE) { |
| 1109 | /* assert extra == 0 |
| 1110 | This is rare, since the average size |
| 1111 | of a final block is only about |
| 1112 | ln(original n). */ |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1113 | if (samplesortslice(lo, hi, compare) < 0) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1114 | goto fail; |
| 1115 | } |
| 1116 | else { |
| 1117 | /* Binary insertion should be quicker, |
| 1118 | and we can take advantage of the PPs |
| 1119 | already being sorted. */ |
| 1120 | if (extraOnRight && extra) { |
| 1121 | /* swap the PPs to the left end */ |
| 1122 | k = extra; |
| 1123 | do { |
| 1124 | tmp = *lo; |
| 1125 | *lo = *hi; |
| 1126 | *hi = tmp; |
| 1127 | ++lo; ++hi; |
| 1128 | } while (--k); |
| 1129 | } |
| 1130 | if (binarysort(lo - extra, hi, lo, |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1131 | compare) < 0) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1132 | goto fail; |
| 1133 | } |
| 1134 | |
| 1135 | /* Find another slice to work on. */ |
| 1136 | if (--top < 0) |
| 1137 | break; /* no more -- done! */ |
| 1138 | lo = stack[top].lo; |
| 1139 | hi = stack[top].hi; |
| 1140 | extra = stack[top].extra; |
| 1141 | extraOnRight = 0; |
| 1142 | if (extra < 0) { |
| 1143 | extraOnRight = 1; |
| 1144 | extra = -extra; |
| 1145 | } |
| 1146 | continue; |
| 1147 | } |
| 1148 | |
| 1149 | /* Pretend the PPs are indexed 0, 1, ..., extra-1. |
| 1150 | Then our preselected pivot is at (extra-1)/2, and we |
| 1151 | want to move the PPs before that to the left end of |
| 1152 | the slice, and the PPs after that to the right end. |
| 1153 | The following section changes extra, lo, hi, and the |
| 1154 | slice such that: |
| 1155 | [lo-extra, lo) contains the smaller PPs. |
| 1156 | *lo == our PP. |
| 1157 | (lo, hi) contains the unknown elements. |
| 1158 | [hi, hi+extra) contains the larger PPs. |
| 1159 | */ |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1160 | k = extra >>= 1; /* num PPs to move */ |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1161 | if (extraOnRight) { |
| 1162 | /* Swap the smaller PPs to the left end. |
| 1163 | Note that this loop actually moves k+1 items: |
| 1164 | the last is our PP */ |
| 1165 | do { |
| 1166 | tmp = *lo; *lo = *hi; *hi = tmp; |
| 1167 | ++lo; ++hi; |
| 1168 | } while (k--); |
| 1169 | } |
| 1170 | else { |
| 1171 | /* Swap the larger PPs to the right end. */ |
| 1172 | while (k--) { |
| 1173 | --lo; --hi; |
| 1174 | tmp = *lo; *lo = *hi; *hi = tmp; |
| 1175 | } |
| 1176 | } |
| 1177 | --lo; /* *lo is now our PP */ |
| 1178 | pivot = *lo; |
| 1179 | |
| 1180 | /* Now an almost-ordinary quicksort partition step. |
| 1181 | Note that most of the time is spent here! |
| 1182 | Only odd thing is that we partition into < and >=, |
| 1183 | instead of the usual <= and >=. This helps when |
| 1184 | there are lots of duplicates of different values, |
| 1185 | because it eventually tends to make subfiles |
| 1186 | "pure" (all duplicates), and we special-case for |
| 1187 | duplicates later. */ |
| 1188 | l = lo + 1; |
| 1189 | r = hi - 1; |
| 1190 | /* assert lo < l < r < hi (small n weeded out above) */ |
| 1191 | |
| 1192 | do { |
| 1193 | /* slide l right, looking for key >= pivot */ |
| 1194 | do { |
| 1195 | SETK(*l, pivot); |
| 1196 | if (k < 0) |
| 1197 | ++l; |
| 1198 | else |
| 1199 | break; |
| 1200 | } while (l < r); |
| 1201 | |
| 1202 | /* slide r left, looking for key < pivot */ |
| 1203 | while (l < r) { |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1204 | register PyObject *rval = *r--; |
| 1205 | SETK(rval, pivot); |
| 1206 | if (k < 0) { |
| 1207 | /* swap and advance */ |
| 1208 | r[1] = *l; |
| 1209 | *l++ = rval; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1210 | break; |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1211 | } |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | } while (l < r); |
| 1215 | |
| 1216 | /* assert lo < r <= l < hi |
| 1217 | assert r == l or r+1 == l |
| 1218 | everything to the left of l is < pivot, and |
| 1219 | everything to the right of r is >= pivot */ |
| 1220 | |
| 1221 | if (l == r) { |
| 1222 | SETK(*r, pivot); |
| 1223 | if (k < 0) |
| 1224 | ++l; |
| 1225 | else |
| 1226 | --r; |
| 1227 | } |
| 1228 | /* assert lo <= r and r+1 == l and l <= hi |
| 1229 | assert r == lo or a[r] < pivot |
| 1230 | assert a[lo] is pivot |
| 1231 | assert l == hi or a[l] >= pivot |
| 1232 | Swap the pivot into "the middle", so we can henceforth |
| 1233 | ignore it. |
| 1234 | */ |
| 1235 | *lo = *r; |
| 1236 | *r = pivot; |
| 1237 | |
| 1238 | /* The following is true now, & will be preserved: |
| 1239 | All in [lo,r) are < pivot |
| 1240 | All in [r,l) == pivot (& so can be ignored) |
| 1241 | All in [l,hi) are >= pivot */ |
| 1242 | |
| 1243 | /* Check for duplicates of the pivot. One compare is |
| 1244 | wasted if there are no duplicates, but can win big |
| 1245 | when there are. |
| 1246 | Tricky: we're sticking to "<" compares, so deduce |
| 1247 | equality indirectly. We know pivot <= *l, so they're |
| 1248 | equal iff not pivot < *l. |
| 1249 | */ |
| 1250 | while (l < hi) { |
| 1251 | /* pivot <= *l known */ |
| 1252 | SETK(pivot, *l); |
| 1253 | if (k < 0) |
| 1254 | break; |
| 1255 | else |
| 1256 | /* <= and not < implies == */ |
| 1257 | ++l; |
| 1258 | } |
| 1259 | |
| 1260 | /* assert lo <= r < l <= hi |
| 1261 | Partitions are [lo, r) and [l, hi) */ |
| 1262 | |
| 1263 | /* push fattest first; remember we still have extra PPs |
| 1264 | to the left of the left chunk and to the right of |
| 1265 | the right chunk! */ |
| 1266 | /* assert top < STACKSIZE */ |
| 1267 | if (r - lo <= hi - l) { |
| 1268 | /* second is bigger */ |
| 1269 | stack[top].lo = l; |
| 1270 | stack[top].hi = hi; |
| 1271 | stack[top].extra = -extra; |
| 1272 | hi = r; |
| 1273 | extraOnRight = 0; |
| 1274 | } |
| 1275 | else { |
| 1276 | /* first is bigger */ |
| 1277 | stack[top].lo = lo; |
| 1278 | stack[top].hi = r; |
| 1279 | stack[top].extra = extra; |
| 1280 | lo = l; |
| 1281 | extraOnRight = 1; |
| 1282 | } |
| 1283 | ++top; |
| 1284 | |
| 1285 | } /* end of partitioning loop */ |
| 1286 | |
| 1287 | return 0; |
| 1288 | |
| 1289 | fail: |
| 1290 | return -1; |
| 1291 | } |
Guido van Rossum | a119c0d | 1998-05-29 17:56:32 +0000 | [diff] [blame] | 1292 | |
| 1293 | #undef SETK |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1294 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1295 | static PyTypeObject immutable_list_type; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1296 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1297 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1298 | listsort(PyListObject *self, PyObject *args) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1299 | { |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1300 | int err; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1301 | PyObject *compare = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1302 | PyTypeObject *savetype; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1303 | |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1304 | if (args != NULL) { |
Guido van Rossum | c00a938 | 2000-02-24 21:48:29 +0000 | [diff] [blame] | 1305 | if (!PyArg_ParseTuple(args, "|O:sort", &compare)) |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1306 | return NULL; |
| 1307 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1308 | savetype = self->ob_type; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1309 | self->ob_type = &immutable_list_type; |
| 1310 | err = samplesortslice(self->ob_item, |
| 1311 | self->ob_item + self->ob_size, |
Guido van Rossum | 4281258 | 1998-06-17 14:15:44 +0000 | [diff] [blame] | 1312 | compare); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1313 | self->ob_type = savetype; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1314 | if (err < 0) |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1315 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1316 | Py_INCREF(Py_None); |
| 1317 | return Py_None; |
Guido van Rossum | 3f236de | 1996-12-10 23:55:39 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1320 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1321 | PyList_Sort(PyObject *v) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1322 | { |
| 1323 | if (v == NULL || !PyList_Check(v)) { |
| 1324 | PyErr_BadInternalCall(); |
| 1325 | return -1; |
| 1326 | } |
| 1327 | v = listsort((PyListObject *)v, (PyObject *)NULL); |
| 1328 | if (v == NULL) |
| 1329 | return -1; |
| 1330 | Py_DECREF(v); |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
Guido van Rossum | b86c549 | 2001-02-12 22:06:02 +0000 | [diff] [blame] | 1334 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1335 | listreverse(PyListObject *self) |
Guido van Rossum | b86c549 | 2001-02-12 22:06:02 +0000 | [diff] [blame] | 1336 | { |
Tim Peters | 8e2e7ca | 2002-07-19 02:33:08 +0000 | [diff] [blame] | 1337 | reverse_slice(self->ob_item, self->ob_item + self->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1338 | Py_INCREF(Py_None); |
| 1339 | return Py_None; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Guido van Rossum | 84c76f5 | 1990-10-30 13:32:20 +0000 | [diff] [blame] | 1342 | int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1343 | PyList_Reverse(PyObject *v) |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1344 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1345 | if (v == NULL || !PyList_Check(v)) { |
| 1346 | PyErr_BadInternalCall(); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1347 | return -1; |
| 1348 | } |
Tim Peters | 8e2e7ca | 2002-07-19 02:33:08 +0000 | [diff] [blame] | 1349 | listreverse((PyListObject *)v); |
Guido van Rossum | b0fe3a9 | 1995-01-17 16:34:45 +0000 | [diff] [blame] | 1350 | return 0; |
| 1351 | } |
| 1352 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1353 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1354 | PyList_AsTuple(PyObject *v) |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1355 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1356 | PyObject *w; |
| 1357 | PyObject **p; |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1358 | int n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1359 | if (v == NULL || !PyList_Check(v)) { |
| 1360 | PyErr_BadInternalCall(); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1361 | return NULL; |
| 1362 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1363 | n = ((PyListObject *)v)->ob_size; |
| 1364 | w = PyTuple_New(n); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1365 | if (w == NULL) |
| 1366 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1367 | p = ((PyTupleObject *)w)->ob_item; |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1368 | memcpy((void *)p, |
| 1369 | (void *)((PyListObject *)v)->ob_item, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1370 | n*sizeof(PyObject *)); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1371 | while (--n >= 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1372 | Py_INCREF(*p); |
Guido van Rossum | 6cd2fe0 | 1994-08-29 12:45:32 +0000 | [diff] [blame] | 1373 | p++; |
| 1374 | } |
| 1375 | return w; |
| 1376 | } |
| 1377 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1378 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1379 | listindex(PyListObject *self, PyObject *v) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1380 | { |
| 1381 | int i; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1382 | |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1383 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1384 | int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); |
| 1385 | if (cmp > 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1386 | return PyInt_FromLong((long)i); |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1387 | else if (cmp < 0) |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1388 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1389 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1390 | PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list"); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1391 | return NULL; |
| 1392 | } |
| 1393 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1394 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1395 | listcount(PyListObject *self, PyObject *v) |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1396 | { |
| 1397 | int count = 0; |
| 1398 | int i; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1399 | |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1400 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1401 | int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); |
| 1402 | if (cmp > 0) |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1403 | count++; |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1404 | else if (cmp < 0) |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1405 | return NULL; |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1406 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1407 | return PyInt_FromLong((long)count); |
Guido van Rossum | e6f7d18 | 1991-10-20 20:20:40 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1410 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1411 | listremove(PyListObject *self, PyObject *v) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1412 | { |
| 1413 | int i; |
Guido van Rossum | 4aa24f9 | 2000-02-24 15:23:03 +0000 | [diff] [blame] | 1414 | |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1415 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1416 | int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); |
| 1417 | if (cmp > 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1418 | if (list_ass_slice(self, i, i+1, |
| 1419 | (PyObject *)NULL) != 0) |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1420 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1421 | Py_INCREF(Py_None); |
| 1422 | return Py_None; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1423 | } |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1424 | else if (cmp < 0) |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1425 | return NULL; |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1426 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1427 | PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list"); |
Guido van Rossum | ed98d48 | 1991-03-06 13:07:53 +0000 | [diff] [blame] | 1428 | return NULL; |
| 1429 | } |
| 1430 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1431 | static int |
| 1432 | list_traverse(PyListObject *o, visitproc visit, void *arg) |
| 1433 | { |
| 1434 | int i, err; |
| 1435 | PyObject *x; |
| 1436 | |
| 1437 | for (i = o->ob_size; --i >= 0; ) { |
| 1438 | x = o->ob_item[i]; |
| 1439 | if (x != NULL) { |
| 1440 | err = visit(x, arg); |
| 1441 | if (err) |
| 1442 | return err; |
| 1443 | } |
| 1444 | } |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | static int |
| 1449 | list_clear(PyListObject *lp) |
| 1450 | { |
| 1451 | (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0); |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1455 | static PyObject * |
| 1456 | list_richcompare(PyObject *v, PyObject *w, int op) |
| 1457 | { |
| 1458 | PyListObject *vl, *wl; |
| 1459 | int i; |
| 1460 | |
| 1461 | if (!PyList_Check(v) || !PyList_Check(w)) { |
| 1462 | Py_INCREF(Py_NotImplemented); |
| 1463 | return Py_NotImplemented; |
| 1464 | } |
| 1465 | |
| 1466 | vl = (PyListObject *)v; |
| 1467 | wl = (PyListObject *)w; |
| 1468 | |
| 1469 | if (vl->ob_size != wl->ob_size && (op == Py_EQ || op == Py_NE)) { |
| 1470 | /* Shortcut: if the lengths differ, the lists differ */ |
| 1471 | PyObject *res; |
| 1472 | if (op == Py_EQ) |
| 1473 | res = Py_False; |
| 1474 | else |
| 1475 | res = Py_True; |
| 1476 | Py_INCREF(res); |
| 1477 | return res; |
| 1478 | } |
| 1479 | |
| 1480 | /* Search for the first index where items are different */ |
| 1481 | for (i = 0; i < vl->ob_size && i < wl->ob_size; i++) { |
| 1482 | int k = PyObject_RichCompareBool(vl->ob_item[i], |
| 1483 | wl->ob_item[i], Py_EQ); |
| 1484 | if (k < 0) |
| 1485 | return NULL; |
| 1486 | if (!k) |
| 1487 | break; |
| 1488 | } |
| 1489 | |
| 1490 | if (i >= vl->ob_size || i >= wl->ob_size) { |
| 1491 | /* No more items to compare -- compare sizes */ |
| 1492 | int vs = vl->ob_size; |
| 1493 | int ws = wl->ob_size; |
| 1494 | int cmp; |
| 1495 | PyObject *res; |
| 1496 | switch (op) { |
| 1497 | case Py_LT: cmp = vs < ws; break; |
Tim Peters | 6ee4234 | 2001-07-06 17:45:43 +0000 | [diff] [blame] | 1498 | case Py_LE: cmp = vs <= ws; break; |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1499 | case Py_EQ: cmp = vs == ws; break; |
| 1500 | case Py_NE: cmp = vs != ws; break; |
| 1501 | case Py_GT: cmp = vs > ws; break; |
| 1502 | case Py_GE: cmp = vs >= ws; break; |
| 1503 | default: return NULL; /* cannot happen */ |
| 1504 | } |
| 1505 | if (cmp) |
| 1506 | res = Py_True; |
| 1507 | else |
| 1508 | res = Py_False; |
| 1509 | Py_INCREF(res); |
| 1510 | return res; |
| 1511 | } |
| 1512 | |
| 1513 | /* We have an item that differs -- shortcuts for EQ/NE */ |
| 1514 | if (op == Py_EQ) { |
| 1515 | Py_INCREF(Py_False); |
| 1516 | return Py_False; |
| 1517 | } |
| 1518 | if (op == Py_NE) { |
| 1519 | Py_INCREF(Py_True); |
| 1520 | return Py_True; |
| 1521 | } |
| 1522 | |
| 1523 | /* Compare the final item again using the proper operator */ |
| 1524 | return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op); |
| 1525 | } |
| 1526 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1527 | /* Adapted from newer code by Tim */ |
| 1528 | static int |
| 1529 | list_fill(PyListObject *result, PyObject *v) |
| 1530 | { |
| 1531 | PyObject *it; /* iter(v) */ |
| 1532 | int n; /* guess for result list size */ |
| 1533 | int i; |
| 1534 | |
| 1535 | n = result->ob_size; |
| 1536 | |
| 1537 | /* Special-case list(a_list), for speed. */ |
| 1538 | if (PyList_Check(v)) { |
| 1539 | if (v == (PyObject *)result) |
| 1540 | return 0; /* source is destination, we're done */ |
| 1541 | return list_ass_slice(result, 0, n, v); |
| 1542 | } |
| 1543 | |
| 1544 | /* Empty previous contents */ |
| 1545 | if (n != 0) { |
| 1546 | if (list_ass_slice(result, 0, n, (PyObject *)NULL) != 0) |
| 1547 | return -1; |
| 1548 | } |
| 1549 | |
| 1550 | /* Get iterator. There may be some low-level efficiency to be gained |
| 1551 | * by caching the tp_iternext slot instead of using PyIter_Next() |
| 1552 | * later, but premature optimization is the root etc. |
| 1553 | */ |
| 1554 | it = PyObject_GetIter(v); |
| 1555 | if (it == NULL) |
| 1556 | return -1; |
| 1557 | |
| 1558 | /* Guess a result list size. */ |
| 1559 | n = -1; /* unknown */ |
| 1560 | if (PySequence_Check(v) && |
| 1561 | v->ob_type->tp_as_sequence->sq_length) { |
| 1562 | n = PySequence_Size(v); |
| 1563 | if (n < 0) |
| 1564 | PyErr_Clear(); |
| 1565 | } |
| 1566 | if (n < 0) |
| 1567 | n = 8; /* arbitrary */ |
| 1568 | NRESIZE(result->ob_item, PyObject*, n); |
Neal Norwitz | d4e5be5 | 2002-05-22 23:19:17 +0000 | [diff] [blame] | 1569 | if (result->ob_item == NULL) { |
| 1570 | PyErr_NoMemory(); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1571 | goto error; |
Neal Norwitz | d4e5be5 | 2002-05-22 23:19:17 +0000 | [diff] [blame] | 1572 | } |
Neal Norwitz | 35fc760 | 2002-06-13 21:11:11 +0000 | [diff] [blame] | 1573 | memset(result->ob_item, 0, sizeof(*result->ob_item) * n); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1574 | result->ob_size = n; |
| 1575 | |
| 1576 | /* Run iterator to exhaustion. */ |
| 1577 | for (i = 0; ; i++) { |
| 1578 | PyObject *item = PyIter_Next(it); |
| 1579 | if (item == NULL) { |
| 1580 | if (PyErr_Occurred()) |
| 1581 | goto error; |
| 1582 | break; |
| 1583 | } |
| 1584 | if (i < n) |
| 1585 | PyList_SET_ITEM(result, i, item); /* steals ref */ |
| 1586 | else { |
| 1587 | int status = ins1(result, result->ob_size, item); |
| 1588 | Py_DECREF(item); /* append creates a new ref */ |
| 1589 | if (status < 0) |
| 1590 | goto error; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | /* Cut back result list if initial guess was too large. */ |
| 1595 | if (i < n && result != NULL) { |
| 1596 | if (list_ass_slice(result, i, n, (PyObject *)NULL) != 0) |
| 1597 | goto error; |
| 1598 | } |
| 1599 | Py_DECREF(it); |
| 1600 | return 0; |
| 1601 | |
| 1602 | error: |
| 1603 | Py_DECREF(it); |
| 1604 | return -1; |
| 1605 | } |
| 1606 | |
| 1607 | static int |
| 1608 | list_init(PyListObject *self, PyObject *args, PyObject *kw) |
| 1609 | { |
| 1610 | PyObject *arg = NULL; |
| 1611 | static char *kwlist[] = {"sequence", 0}; |
| 1612 | |
| 1613 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg)) |
| 1614 | return -1; |
| 1615 | if (arg != NULL) |
| 1616 | return list_fill(self, arg); |
| 1617 | if (self->ob_size > 0) |
| 1618 | return list_ass_slice(self, 0, self->ob_size, (PyObject*)NULL); |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
Guido van Rossum | dbb53d9 | 2001-12-03 16:32:18 +0000 | [diff] [blame] | 1622 | static long |
| 1623 | list_nohash(PyObject *self) |
| 1624 | { |
| 1625 | PyErr_SetString(PyExc_TypeError, "list objects are unhashable"); |
| 1626 | return -1; |
| 1627 | } |
| 1628 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1629 | PyDoc_STRVAR(append_doc, |
| 1630 | "L.append(object) -- append object to end"); |
| 1631 | PyDoc_STRVAR(extend_doc, |
| 1632 | "L.extend(list) -- extend list by appending list elements"); |
| 1633 | PyDoc_STRVAR(insert_doc, |
| 1634 | "L.insert(index, object) -- insert object before index"); |
| 1635 | PyDoc_STRVAR(pop_doc, |
| 1636 | "L.pop([index]) -> item -- remove and return item at index (default last)"); |
| 1637 | PyDoc_STRVAR(remove_doc, |
| 1638 | "L.remove(value) -- remove first occurrence of value"); |
| 1639 | PyDoc_STRVAR(index_doc, |
| 1640 | "L.index(value) -> integer -- return index of first occurrence of value"); |
| 1641 | PyDoc_STRVAR(count_doc, |
| 1642 | "L.count(value) -> integer -- return number of occurrences of value"); |
| 1643 | PyDoc_STRVAR(reverse_doc, |
| 1644 | "L.reverse() -- reverse *IN PLACE*"); |
| 1645 | PyDoc_STRVAR(sort_doc, |
| 1646 | "L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1"); |
Guido van Rossum | 3dd7f3f | 1998-06-30 15:36:32 +0000 | [diff] [blame] | 1647 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1648 | static PyMethodDef list_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1649 | {"append", (PyCFunction)listappend, METH_O, append_doc}, |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1650 | {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1651 | {"extend", (PyCFunction)listextend, METH_O, extend_doc}, |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1652 | {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1653 | {"remove", (PyCFunction)listremove, METH_O, remove_doc}, |
| 1654 | {"index", (PyCFunction)listindex, METH_O, index_doc}, |
| 1655 | {"count", (PyCFunction)listcount, METH_O, count_doc}, |
| 1656 | {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc}, |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1657 | {"sort", (PyCFunction)listsort, METH_VARARGS, sort_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1658 | {NULL, NULL} /* sentinel */ |
| 1659 | }; |
| 1660 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1661 | static PySequenceMethods list_as_sequence = { |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1662 | (inquiry)list_length, /* sq_length */ |
| 1663 | (binaryfunc)list_concat, /* sq_concat */ |
| 1664 | (intargfunc)list_repeat, /* sq_repeat */ |
| 1665 | (intargfunc)list_item, /* sq_item */ |
| 1666 | (intintargfunc)list_slice, /* sq_slice */ |
| 1667 | (intobjargproc)list_ass_item, /* sq_ass_item */ |
| 1668 | (intintobjargproc)list_ass_slice, /* sq_ass_slice */ |
| 1669 | (objobjproc)list_contains, /* sq_contains */ |
| 1670 | (binaryfunc)list_inplace_concat, /* sq_inplace_concat */ |
| 1671 | (intargfunc)list_inplace_repeat, /* sq_inplace_repeat */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1672 | }; |
| 1673 | |
Neal Norwitz | 2c2e827 | 2002-06-14 02:04:18 +0000 | [diff] [blame] | 1674 | PyDoc_STRVAR(list_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1675 | "list() -> new list\n" |
Neal Norwitz | 2c2e827 | 2002-06-14 02:04:18 +0000 | [diff] [blame] | 1676 | "list(sequence) -> new list initialized from sequence's items"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1677 | |
Jeremy Hylton | a4b4c3b | 2002-07-13 03:51:17 +0000 | [diff] [blame] | 1678 | static PyObject *list_iter(PyObject *seq); |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 1679 | |
Jeremy Hylton | a4b4c3b | 2002-07-13 03:51:17 +0000 | [diff] [blame] | 1680 | static PyObject * |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1681 | list_subscript(PyListObject* self, PyObject* item) |
| 1682 | { |
| 1683 | if (PyInt_Check(item)) { |
| 1684 | long i = PyInt_AS_LONG(item); |
| 1685 | if (i < 0) |
| 1686 | i += PyList_GET_SIZE(self); |
| 1687 | return list_item(self, i); |
| 1688 | } |
| 1689 | else if (PyLong_Check(item)) { |
| 1690 | long i = PyLong_AsLong(item); |
| 1691 | if (i == -1 && PyErr_Occurred()) |
| 1692 | return NULL; |
| 1693 | if (i < 0) |
| 1694 | i += PyList_GET_SIZE(self); |
| 1695 | return list_item(self, i); |
| 1696 | } |
| 1697 | else if (PySlice_Check(item)) { |
| 1698 | int start, stop, step, slicelength, cur, i; |
| 1699 | PyObject* result; |
| 1700 | PyObject* it; |
| 1701 | |
| 1702 | if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, |
| 1703 | &start, &stop, &step, &slicelength) < 0) { |
| 1704 | return NULL; |
| 1705 | } |
| 1706 | |
| 1707 | if (slicelength <= 0) { |
| 1708 | return PyList_New(0); |
| 1709 | } |
| 1710 | else { |
| 1711 | result = PyList_New(slicelength); |
| 1712 | if (!result) return NULL; |
| 1713 | |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1714 | for (cur = start, i = 0; i < slicelength; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1715 | cur += step, i++) { |
| 1716 | it = PyList_GET_ITEM(self, cur); |
| 1717 | Py_INCREF(it); |
| 1718 | PyList_SET_ITEM(result, i, it); |
| 1719 | } |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1720 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1721 | return result; |
| 1722 | } |
| 1723 | } |
| 1724 | else { |
| 1725 | PyErr_SetString(PyExc_TypeError, |
| 1726 | "list indices must be integers"); |
| 1727 | return NULL; |
| 1728 | } |
| 1729 | } |
| 1730 | |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1731 | static int |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1732 | list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) |
| 1733 | { |
| 1734 | if (PyInt_Check(item)) { |
| 1735 | long i = PyInt_AS_LONG(item); |
| 1736 | if (i < 0) |
| 1737 | i += PyList_GET_SIZE(self); |
| 1738 | return list_ass_item(self, i, value); |
| 1739 | } |
| 1740 | else if (PyLong_Check(item)) { |
| 1741 | long i = PyLong_AsLong(item); |
| 1742 | if (i == -1 && PyErr_Occurred()) |
| 1743 | return -1; |
| 1744 | if (i < 0) |
| 1745 | i += PyList_GET_SIZE(self); |
| 1746 | return list_ass_item(self, i, value); |
| 1747 | } |
| 1748 | else if (PySlice_Check(item)) { |
| 1749 | int start, stop, step, slicelength; |
| 1750 | |
| 1751 | if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, |
| 1752 | &start, &stop, &step, &slicelength) < 0) { |
| 1753 | return -1; |
| 1754 | } |
| 1755 | |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1756 | /* treat L[slice(a,b)] = v _exactly_ like L[a:b] = v */ |
| 1757 | if (step == 1 && ((PySliceObject*)item)->step == Py_None) |
| 1758 | return list_ass_slice(self, start, stop, value); |
| 1759 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1760 | if (value == NULL) { |
| 1761 | /* delete slice */ |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1762 | PyObject **garbage, **it; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1763 | int cur, i, j; |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1764 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1765 | if (slicelength <= 0) |
| 1766 | return 0; |
| 1767 | |
| 1768 | if (step < 0) { |
| 1769 | stop = start + 1; |
| 1770 | start = stop + step*(slicelength - 1) - 1; |
| 1771 | step = -step; |
| 1772 | } |
| 1773 | |
| 1774 | garbage = (PyObject**) |
| 1775 | PyMem_MALLOC(slicelength*sizeof(PyObject*)); |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1776 | |
| 1777 | /* drawing pictures might help |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1778 | understand these for loops */ |
Guido van Rossum | 75a20b1 | 2002-06-11 12:22:28 +0000 | [diff] [blame] | 1779 | for (cur = start, i = 0; |
| 1780 | cur < stop; |
| 1781 | cur += step, i++) |
| 1782 | { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1783 | garbage[i] = PyList_GET_ITEM(self, cur); |
| 1784 | |
| 1785 | for (j = 0; j < step; j++) { |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1786 | PyList_SET_ITEM(self, cur + j - i, |
Guido van Rossum | 75a20b1 | 2002-06-11 12:22:28 +0000 | [diff] [blame] | 1787 | PyList_GET_ITEM(self, |
| 1788 | cur + j + 1)); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1789 | } |
| 1790 | } |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1791 | for (cur = start + slicelength*step + 1; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1792 | cur < self->ob_size; cur++) { |
| 1793 | PyList_SET_ITEM(self, cur - slicelength, |
| 1794 | PyList_GET_ITEM(self, cur)); |
| 1795 | } |
| 1796 | self->ob_size -= slicelength; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1797 | it = self->ob_item; |
| 1798 | NRESIZE(it, PyObject*, self->ob_size); |
| 1799 | self->ob_item = it; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1800 | |
| 1801 | for (i = 0; i < slicelength; i++) { |
| 1802 | Py_DECREF(garbage[i]); |
| 1803 | } |
| 1804 | PyMem_FREE(garbage); |
| 1805 | |
| 1806 | return 0; |
| 1807 | } |
| 1808 | else { |
| 1809 | /* assign slice */ |
| 1810 | PyObject **garbage, *ins; |
| 1811 | int cur, i; |
| 1812 | |
| 1813 | if (!PyList_Check(value)) { |
| 1814 | PyErr_Format(PyExc_TypeError, |
| 1815 | "must assign list (not \"%.200s\") to slice", |
| 1816 | value->ob_type->tp_name); |
| 1817 | return -1; |
| 1818 | } |
| 1819 | |
| 1820 | if (PyList_GET_SIZE(value) != slicelength) { |
| 1821 | PyErr_Format(PyExc_ValueError, |
| 1822 | "attempt to assign list of size %d to extended slice of size %d", |
| 1823 | PyList_Size(value), slicelength); |
| 1824 | return -1; |
| 1825 | } |
| 1826 | |
| 1827 | if (!slicelength) |
| 1828 | return 0; |
| 1829 | |
| 1830 | /* protect against a[::-1] = a */ |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1831 | if (self == (PyListObject*)value) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1832 | value = list_slice((PyListObject*)value, 0, |
| 1833 | PyList_GET_SIZE(value)); |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1834 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1835 | else { |
| 1836 | Py_INCREF(value); |
| 1837 | } |
| 1838 | |
| 1839 | garbage = (PyObject**) |
| 1840 | PyMem_MALLOC(slicelength*sizeof(PyObject*)); |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1841 | |
| 1842 | for (cur = start, i = 0; i < slicelength; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1843 | cur += step, i++) { |
| 1844 | garbage[i] = PyList_GET_ITEM(self, cur); |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1845 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1846 | ins = PyList_GET_ITEM(value, i); |
| 1847 | Py_INCREF(ins); |
| 1848 | PyList_SET_ITEM(self, cur, ins); |
| 1849 | } |
| 1850 | |
| 1851 | for (i = 0; i < slicelength; i++) { |
| 1852 | Py_DECREF(garbage[i]); |
| 1853 | } |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1854 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1855 | PyMem_FREE(garbage); |
| 1856 | Py_DECREF(value); |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1857 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1858 | return 0; |
| 1859 | } |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1860 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1861 | else { |
Tim Peters | 3b01a12 | 2002-07-19 02:35:45 +0000 | [diff] [blame^] | 1862 | PyErr_SetString(PyExc_TypeError, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1863 | "list indices must be integers"); |
| 1864 | return -1; |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | static PyMappingMethods list_as_mapping = { |
| 1869 | (inquiry)list_length, |
| 1870 | (binaryfunc)list_subscript, |
| 1871 | (objobjargproc)list_ass_subscript |
| 1872 | }; |
| 1873 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1874 | PyTypeObject PyList_Type = { |
| 1875 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1876 | 0, |
| 1877 | "list", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 1878 | sizeof(PyListObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1879 | 0, |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1880 | (destructor)list_dealloc, /* tp_dealloc */ |
| 1881 | (printfunc)list_print, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1882 | 0, /* tp_getattr */ |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1883 | 0, /* tp_setattr */ |
| 1884 | 0, /* tp_compare */ |
| 1885 | (reprfunc)list_repr, /* tp_repr */ |
| 1886 | 0, /* tp_as_number */ |
| 1887 | &list_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1888 | &list_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | dbb53d9 | 2001-12-03 16:32:18 +0000 | [diff] [blame] | 1889 | list_nohash, /* tp_hash */ |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1890 | 0, /* tp_call */ |
| 1891 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1892 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1893 | 0, /* tp_setattro */ |
| 1894 | 0, /* tp_as_buffer */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 1895 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1896 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1897 | list_doc, /* tp_doc */ |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1898 | (traverseproc)list_traverse, /* tp_traverse */ |
| 1899 | (inquiry)list_clear, /* tp_clear */ |
| 1900 | list_richcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1901 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 1902 | list_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1903 | 0, /* tp_iternext */ |
| 1904 | list_methods, /* tp_methods */ |
| 1905 | 0, /* tp_members */ |
| 1906 | 0, /* tp_getset */ |
| 1907 | 0, /* tp_base */ |
| 1908 | 0, /* tp_dict */ |
| 1909 | 0, /* tp_descr_get */ |
| 1910 | 0, /* tp_descr_set */ |
| 1911 | 0, /* tp_dictoffset */ |
| 1912 | (initproc)list_init, /* tp_init */ |
| 1913 | PyType_GenericAlloc, /* tp_alloc */ |
| 1914 | PyType_GenericNew, /* tp_new */ |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 1915 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1916 | }; |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1917 | |
| 1918 | |
| 1919 | /* During a sort, we really can't have anyone modifying the list; it could |
| 1920 | cause core dumps. Thus, we substitute a dummy type that raises an |
| 1921 | explanatory exception when a modifying operation is used. Caveat: |
| 1922 | comparisons may behave differently; but I guess it's a bad idea anyway to |
| 1923 | compare a list that's being sorted... */ |
| 1924 | |
| 1925 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1926 | immutable_list_op(void) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1927 | { |
| 1928 | PyErr_SetString(PyExc_TypeError, |
| 1929 | "a list cannot be modified while it is being sorted"); |
| 1930 | return NULL; |
| 1931 | } |
| 1932 | |
| 1933 | static PyMethodDef immutable_list_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1934 | {"append", (PyCFunction)immutable_list_op, METH_VARARGS}, |
| 1935 | {"insert", (PyCFunction)immutable_list_op, METH_VARARGS}, |
Tim Peters | 52e0717 | 2001-08-30 06:15:32 +0000 | [diff] [blame] | 1936 | {"extend", (PyCFunction)immutable_list_op, METH_O}, |
| 1937 | {"pop", (PyCFunction)immutable_list_op, METH_VARARGS}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1938 | {"remove", (PyCFunction)immutable_list_op, METH_VARARGS}, |
| 1939 | {"index", (PyCFunction)listindex, METH_O}, |
| 1940 | {"count", (PyCFunction)listcount, METH_O}, |
| 1941 | {"reverse", (PyCFunction)immutable_list_op, METH_VARARGS}, |
| 1942 | {"sort", (PyCFunction)immutable_list_op, METH_VARARGS}, |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1943 | {NULL, NULL} /* sentinel */ |
| 1944 | }; |
| 1945 | |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1946 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1947 | immutable_list_ass(void) |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1948 | { |
| 1949 | immutable_list_op(); |
| 1950 | return -1; |
| 1951 | } |
| 1952 | |
| 1953 | static PySequenceMethods immutable_list_as_sequence = { |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1954 | (inquiry)list_length, /* sq_length */ |
| 1955 | (binaryfunc)list_concat, /* sq_concat */ |
| 1956 | (intargfunc)list_repeat, /* sq_repeat */ |
| 1957 | (intargfunc)list_item, /* sq_item */ |
| 1958 | (intintargfunc)list_slice, /* sq_slice */ |
| 1959 | (intobjargproc)immutable_list_ass, /* sq_ass_item */ |
| 1960 | (intintobjargproc)immutable_list_ass, /* sq_ass_slice */ |
| 1961 | (objobjproc)list_contains, /* sq_contains */ |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1962 | }; |
| 1963 | |
| 1964 | static PyTypeObject immutable_list_type = { |
| 1965 | PyObject_HEAD_INIT(&PyType_Type) |
| 1966 | 0, |
| 1967 | "list (immutable, during sort)", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 1968 | sizeof(PyListObject), |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 1969 | 0, |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1970 | 0, /* Cannot happen */ /* tp_dealloc */ |
| 1971 | (printfunc)list_print, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1972 | 0, /* tp_getattr */ |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1973 | 0, /* tp_setattr */ |
| 1974 | 0, /* Won't be called */ /* tp_compare */ |
| 1975 | (reprfunc)list_repr, /* tp_repr */ |
| 1976 | 0, /* tp_as_number */ |
| 1977 | &immutable_list_as_sequence, /* tp_as_sequence */ |
| 1978 | 0, /* tp_as_mapping */ |
Guido van Rossum | dbb53d9 | 2001-12-03 16:32:18 +0000 | [diff] [blame] | 1979 | list_nohash, /* tp_hash */ |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1980 | 0, /* tp_call */ |
| 1981 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1982 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1983 | 0, /* tp_setattro */ |
| 1984 | 0, /* tp_as_buffer */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 1985 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1986 | list_doc, /* tp_doc */ |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 1987 | (traverseproc)list_traverse, /* tp_traverse */ |
| 1988 | 0, /* tp_clear */ |
| 1989 | list_richcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1990 | 0, /* tp_weaklistoffset */ |
| 1991 | 0, /* tp_iter */ |
| 1992 | 0, /* tp_iternext */ |
| 1993 | immutable_list_methods, /* tp_methods */ |
| 1994 | 0, /* tp_members */ |
| 1995 | 0, /* tp_getset */ |
| 1996 | 0, /* tp_base */ |
| 1997 | 0, /* tp_dict */ |
| 1998 | 0, /* tp_descr_get */ |
| 1999 | 0, /* tp_descr_set */ |
| 2000 | 0, /* tp_init */ |
Guido van Rossum | 65e1cea | 2001-01-17 22:11:59 +0000 | [diff] [blame] | 2001 | /* NOTE: This is *not* the standard list_type struct! */ |
Guido van Rossum | 4c4e7df | 1998-06-16 15:18:28 +0000 | [diff] [blame] | 2002 | }; |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2003 | |
| 2004 | |
| 2005 | /*********************** List Iterator **************************/ |
| 2006 | |
| 2007 | typedef struct { |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2008 | PyObject_HEAD |
| 2009 | long it_index; |
Guido van Rossum | 86103ae | 2002-07-16 20:07:32 +0000 | [diff] [blame] | 2010 | PyListObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2011 | } listiterobject; |
| 2012 | |
| 2013 | PyTypeObject PyListIter_Type; |
| 2014 | |
Guido van Rossum | 5086e49 | 2002-07-16 15:56:52 +0000 | [diff] [blame] | 2015 | static PyObject * |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2016 | list_iter(PyObject *seq) |
| 2017 | { |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2018 | listiterobject *it; |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2019 | |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2020 | if (!PyList_Check(seq)) { |
| 2021 | PyErr_BadInternalCall(); |
| 2022 | return NULL; |
| 2023 | } |
| 2024 | it = PyObject_GC_New(listiterobject, &PyListIter_Type); |
| 2025 | if (it == NULL) |
| 2026 | return NULL; |
| 2027 | it->it_index = 0; |
| 2028 | Py_INCREF(seq); |
| 2029 | it->it_seq = (PyListObject *)seq; |
| 2030 | _PyObject_GC_TRACK(it); |
| 2031 | return (PyObject *)it; |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | static void |
| 2035 | listiter_dealloc(listiterobject *it) |
| 2036 | { |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2037 | _PyObject_GC_UNTRACK(it); |
Guido van Rossum | 86103ae | 2002-07-16 20:07:32 +0000 | [diff] [blame] | 2038 | Py_XDECREF(it->it_seq); |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2039 | PyObject_GC_Del(it); |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | static int |
| 2043 | listiter_traverse(listiterobject *it, visitproc visit, void *arg) |
| 2044 | { |
Guido van Rossum | 86103ae | 2002-07-16 20:07:32 +0000 | [diff] [blame] | 2045 | if (it->it_seq == NULL) |
| 2046 | return 0; |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2047 | return visit((PyObject *)it->it_seq, arg); |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | |
| 2051 | static PyObject * |
| 2052 | listiter_getiter(PyObject *it) |
| 2053 | { |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2054 | Py_INCREF(it); |
| 2055 | return it; |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | static PyObject * |
Tim Peters | 93b2cc4 | 2002-06-01 05:22:55 +0000 | [diff] [blame] | 2059 | listiter_next(listiterobject *it) |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2060 | { |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2061 | PyListObject *seq; |
| 2062 | PyObject *item; |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2063 | |
Tim Peters | 93b2cc4 | 2002-06-01 05:22:55 +0000 | [diff] [blame] | 2064 | assert(it != NULL); |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2065 | seq = it->it_seq; |
Guido van Rossum | 86103ae | 2002-07-16 20:07:32 +0000 | [diff] [blame] | 2066 | if (seq == NULL) |
| 2067 | return NULL; |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2068 | assert(PyList_Check(seq)); |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2069 | |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2070 | if (it->it_index < PyList_GET_SIZE(seq)) { |
Tim Peters | 93b2cc4 | 2002-06-01 05:22:55 +0000 | [diff] [blame] | 2071 | item = PyList_GET_ITEM(seq, it->it_index); |
| 2072 | ++it->it_index; |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2073 | Py_INCREF(item); |
| 2074 | return item; |
| 2075 | } |
Guido van Rossum | 86103ae | 2002-07-16 20:07:32 +0000 | [diff] [blame] | 2076 | |
| 2077 | Py_DECREF(seq); |
| 2078 | it->it_seq = NULL; |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2079 | return NULL; |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2082 | PyTypeObject PyListIter_Type = { |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2083 | PyObject_HEAD_INIT(&PyType_Type) |
| 2084 | 0, /* ob_size */ |
| 2085 | "listiterator", /* tp_name */ |
| 2086 | sizeof(listiterobject), /* tp_basicsize */ |
| 2087 | 0, /* tp_itemsize */ |
| 2088 | /* methods */ |
| 2089 | (destructor)listiter_dealloc, /* tp_dealloc */ |
| 2090 | 0, /* tp_print */ |
| 2091 | 0, /* tp_getattr */ |
| 2092 | 0, /* tp_setattr */ |
| 2093 | 0, /* tp_compare */ |
| 2094 | 0, /* tp_repr */ |
| 2095 | 0, /* tp_as_number */ |
| 2096 | 0, /* tp_as_sequence */ |
| 2097 | 0, /* tp_as_mapping */ |
| 2098 | 0, /* tp_hash */ |
| 2099 | 0, /* tp_call */ |
| 2100 | 0, /* tp_str */ |
| 2101 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2102 | 0, /* tp_setattro */ |
| 2103 | 0, /* tp_as_buffer */ |
| 2104 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2105 | 0, /* tp_doc */ |
| 2106 | (traverseproc)listiter_traverse, /* tp_traverse */ |
| 2107 | 0, /* tp_clear */ |
| 2108 | 0, /* tp_richcompare */ |
| 2109 | 0, /* tp_weaklistoffset */ |
| 2110 | (getiterfunc)listiter_getiter, /* tp_iter */ |
| 2111 | (iternextfunc)listiter_next, /* tp_iternext */ |
Guido van Rossum | 86103ae | 2002-07-16 20:07:32 +0000 | [diff] [blame] | 2112 | 0, /* tp_methods */ |
Guido van Rossum | 6b6272c | 2002-07-16 20:10:23 +0000 | [diff] [blame] | 2113 | 0, /* tp_members */ |
| 2114 | 0, /* tp_getset */ |
| 2115 | 0, /* tp_base */ |
| 2116 | 0, /* tp_dict */ |
| 2117 | 0, /* tp_descr_get */ |
| 2118 | 0, /* tp_descr_set */ |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 2119 | }; |