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